home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / g0bsx / reqfil.c < prev    next >
Text File  |  1989-11-03  |  5KB  |  232 lines

  1. /*
  2.  *  REQFIL.C - Server for the W0RLI MailBox V10.xx
  3.  *
  4.  *  Copyright (C) 1989
  5.  *  Peter Meiring
  6.  *  13 Venn Crescent,
  7.  *  Hartley, Plymouth.
  8.  *  PL3 5PJ
  9.  *
  10.  *  This code may be freely used and copied for non-commercial uses.
  11.  *
  12.  *  s reqfil reqfil.exe reqfil.out H8 reqfil.in H8
  13.  *
  14.  *  See SERVER.DOC for further details.
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21.  
  22. /*
  23.  *  Copyright storage:
  24.  */
  25.  
  26. char *copyright = "REQFIL 1.02  Copyright (c) Peter Meiring, G0BSX, 1989.\n";
  27.  
  28. /*
  29.  *  Some buffer space.
  30.  */
  31.  
  32. #define ln_buf 512
  33.  
  34. char buf[ln_buf];
  35.  
  36. /*
  37.  *  End-of-message mark.
  38.  */
  39.  
  40. char *mark    = "/EX\n";
  41. char base[64];  /* storage for the root path of the files section */
  42.  
  43. /*
  44.  *  File used by this program to read initialisation Data
  45.  */
  46.  
  47. char *datfile = "\\reqxxx.dat";
  48. /*
  49.  *  File the MailBox will create, and this server will read.
  50.  */
  51.  
  52. char *infile  = "reqfil.out";
  53.  
  54. /*
  55.  *  File this server will create, and the MailBox will read.
  56.  */
  57.  
  58. char *outfile = "reqfil.in";
  59.  
  60. /* List directory to file */
  61.  
  62. copyfile(filename,fp)
  63. FILE *fp;
  64. char *filename;
  65. {
  66.     char tmp[64];
  67.     char *st;
  68.     FILE *datafile;
  69.  
  70.     strcpy( tmp, base ) ;
  71.     strcat(tmp,filename);
  72.     if ((datafile = fopen(tmp, "r")) == NULL) {
  73.        fprintf(fp,"File: %s not found \n", tmp);
  74.        return 1;
  75.     }
  76.     st = fgets(buf, ln_buf, datafile) ;
  77.     while (st != NULL)
  78.     {
  79.        fputs(buf, fp);
  80.        st = fgets(buf, ln_buf, datafile);
  81.     }
  82.     fclose(datafile);
  83.     return 0;
  84. }
  85.  
  86. stripcr( line )
  87. char *line;
  88. {
  89.   int i;
  90.   for(i=1;i<strlen(line);i++)
  91.     if(line[i]=='\n')
  92.         line[i]='\0' ;
  93. }
  94.  
  95. main(argc, argv)
  96. int argc;
  97. char *argv[];
  98. {
  99.   FILE *in, *out;
  100.   char *st, *tail, *fname, *ptr ;
  101.  
  102. /*
  103.  *   Open and read the initialisation file: reqxxx.dat in the root directory
  104.  */
  105.  
  106.   puts(copyright);
  107.  
  108.   if((in = fopen(datfile, "r")) == NULL) exit(1);
  109.  
  110.   st = fgets(buf, ln_buf, in) ;
  111.  
  112.   while (st != NULL) {
  113.  
  114. /*
  115.  *  skip all comment and blank lines
  116.  */
  117.  
  118.     while( ((*buf == ';') || (*buf == '\n')) && (st != NULL))
  119.       st = fgets(buf, ln_buf, in) ;
  120.  
  121. /*
  122.  *  Extract parameters
  123.  */
  124.     if (!strncmp(buf, "textsdir", 8)) {
  125.         ptr = strchr(buf, ' ');
  126.     while( *ptr == ' ' ) *ptr++ = '\0';
  127.         strcpy( base, ptr);
  128.         stripcr( base );
  129.     }
  130.     st = fgets(buf, ln_buf, in) ;
  131.   }
  132.  
  133.   fclose(in);
  134.  
  135. /*
  136.  *  Open the input file.
  137.  *  If not possible, exit with return code 1 to signal
  138.  *  the MailBox that the server failed.
  139.  */
  140.  
  141.   if ((in = fopen(infile, "r")) == NULL) exit(1);
  142.  
  143. /*
  144.  *  Open the output file.
  145.  *  If not possible, exit with return code 1 to signal
  146.  *  the MailBox that the server failed.
  147.  */
  148.  
  149.   if ((out = fopen(outfile, "w")) == NULL) exit(1);
  150.  
  151.   st = fgets(buf, ln_buf, in);
  152.   while (st != NULL)
  153.   {
  154.  
  155. /*
  156.  *  Read the rfc-822 header placed into the message by the MailBox.
  157.  *  The end of header is marked by a blank line.
  158.  */
  159.  
  160.     while(*buf != '\n')
  161.     {
  162.  
  163. /*
  164.  *  Find end of keyword.
  165.  *  Terminate string at end of keyword.
  166.  */
  167.  
  168.       tail = strchr(buf, ' ');
  169.       *tail++ = '\0';
  170.  
  171. /*
  172.  *  Make the new rfc-822 header.
  173.  */
  174.  
  175.       if      (!strcmp(buf, "To:"))      fprintf(out, "From: %s", tail);
  176.       else if (!strcmp(buf, "From:"))    fprintf(out, "To: %s", tail);
  177.       else if (!strcmp(buf, "Subject:")) {
  178.         fname = tail ;
  179.         if( (tail = strchr( fname, ' ')) != NULL)
  180.            *tail = '\0';
  181.         fprintf(out, "Subject: File: %s", fname);
  182.       }
  183.       st = fgets(buf, ln_buf, in);
  184.     }
  185.  
  186. /*
  187.  *  Now that the rfc-822 header has been read, and a new one created,
  188.  *  copy the text of the message from the input to the output.
  189.  */
  190.     fputs(buf,out) ;
  191.     fprintf(out, "File: %s", fname);
  192.     stripcr(fname) ;
  193.     copyfile(fname,out) ;
  194.     while ((st != NULL) && stricmp(buf, mark))
  195.     {
  196.       st = fgets(buf, ln_buf, in);
  197.     }
  198.  
  199. /*
  200.  *  Mark the end of this message.
  201.  *  Go on to the next message.
  202.  */
  203.  
  204.     fputs(mark, out);
  205.     if (st != NULL) st = fgets(buf, ln_buf, in);
  206.   }
  207.  
  208. /*
  209.  *  All the incoming messages have been processed,
  210.  *  and an outgoing message created for each one.
  211.  */
  212.  
  213.   fclose(in);
  214.   fclose(out);
  215.  
  216. /*
  217.  *  Delete the input file.
  218.  *  The MailBox appends messages to this file, this server must delete it.
  219.  */
  220.  
  221.   unlink(infile);
  222.  
  223. /*
  224.  *  Exit with return code 0 to signal the MailBox that
  225.  *  the server completed it's processing normally.
  226.  *  The MailBox will then read our output file,
  227.  *  and create messages from it.
  228.  */
  229.  
  230.   exit(0);
  231. }
  232.