home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / w0rli / mb1301 / echo.c < prev    next >
Text File  |  1991-02-03  |  3KB  |  159 lines

  1.  
  2. /*
  3.  *  ECHO.C - Example of a server for the W0RLI MailBox V10.xx
  4.  *
  5.  *  Copyright (C) 1989
  6.  *  H. N. Oredson
  7.  *  21390 Shannon Lane
  8.  *  West Linn, or 97068
  9.  *
  10.  *  This code may be freely used and copied for non-commercial uses.
  11.  *
  12.  *  SP ECHO @ BBS will generate a return message containing
  13.  *  the text and headers from the original message.
  14.  *
  15.  *  The SERVER.MB text required to activate this server is:
  16.  *
  17.  *  s echo echo.exe echo.out H8 echo.in H8
  18.  *
  19.  *  This first exports any messages addressed to ECHO to the file ECHO.OUT.
  20.  *  Next it runs the ECHO server.
  21.  *  Finally it imports the messages created by the ECHO server.
  22.  *  See SERVER.DOC for further details.
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30.  
  31. /*
  32.  *  Some buffer space.
  33.  */
  34.  
  35. #define ln_buf 512
  36.  
  37. char buf[ln_buf];
  38.  
  39. /*
  40.  *  End-of-message mark.
  41.  */
  42.  
  43. char *mark    = "/EX\n";
  44.  
  45. /*
  46.  *  File the MailBox will create, and this server will read.
  47.  */
  48.  
  49. char *infile  = "echo.out";
  50.  
  51. /*
  52.  *  File this server will create, and the MailBox will read.
  53.  */
  54.  
  55. char *outfile = "echo.in";
  56.  
  57.  
  58.  
  59. void main(int argc, char **argv)
  60. {
  61.   FILE *in, *out;
  62.   char *st, *tail;
  63.  
  64. /*
  65.  *  Open the input file.
  66.  *  If not possible, exit with return code 1 to signal
  67.  *  the MailBox that the server failed.
  68.  */
  69.  
  70.   if ((in = fopen(infile, "r")) == NULL) exit(1);
  71.  
  72. /*
  73.  *  Open the output file.
  74.  *  If not possible, exit with return code 1 to signal
  75.  *  the MailBox that the server failed.
  76.  */
  77.  
  78.   if ((out = fopen(outfile, "w")) == NULL) exit(1);
  79.  
  80.   st = fgets(buf, ln_buf, in);
  81.   while (st != NULL)
  82.   {
  83.  
  84. /*
  85.  *  Read the rfc-822 header placed into the message by the MailBox.
  86.  *  The end of header is marked by a blank line.
  87.  *  Do not place this header into the output file.
  88.  *  Use the "To:" address as the new "From:" address,
  89.  *  and the "From:" address as the new "To:" address.
  90.  *  Preserve the "Subject:"
  91.  *  This process is the entire function of this server.
  92.  */
  93.  
  94.     while(*buf != '\n')
  95.     {
  96.  
  97. /*
  98.  *  Find end of keyword.
  99.  *  Terminate string at end of keyword.
  100.  */
  101.  
  102.       tail = strchr(buf, ' ');
  103.       *tail++ = '\0';
  104.  
  105. /*
  106.  *  Make the new rfc-822 header.
  107.  */
  108.  
  109.       if      (!strcmp(buf, "To:"))      fprintf(out, "From: %s", tail);
  110.       else if (!strcmp(buf, "From:"))    fprintf(out, "To: %s", tail);
  111.       else if (!strcmp(buf, "Subject:")) fprintf(out, "Subject: %s", tail);
  112.       st = fgets(buf, ln_buf, in);
  113.     }
  114.  
  115. /*
  116.  *  Now that the rfc-822 header has been read, and a new one created,
  117.  *  copy the text of the message from the input to the output.
  118.  */
  119.  
  120.     while ((st != NULL) && stricmp(buf, mark))
  121.     {
  122.       fputs(buf, out);
  123.       st = fgets(buf, ln_buf, in);
  124.     }
  125.  
  126. /*
  127.  *  Mark the end of this message.
  128.  *  Go on to the next message.
  129.  */
  130.  
  131.     fputs(mark, out);
  132.     if (st != NULL) st = fgets(buf, ln_buf, in);
  133.   }
  134.  
  135. /*
  136.  *  All the incoming messages have been processed,
  137.  *  and an outgoing message created for each one.
  138.  */
  139.  
  140.   fclose(in);
  141.   fclose(out);
  142.  
  143. /*
  144.  *  Delete the input file.
  145.  *  The MailBox appends messages to this file, this server must delete it.
  146.  */
  147.  
  148.   unlink(infile);
  149.  
  150. /*
  151.  *  Exit with return code 0 to signal the MailBox that
  152.  *  the server completed it's processing normally.
  153.  *  The MailBox will then read our output file,
  154.  *  and create messages from it.
  155.  */
  156.  
  157.   exit(0);
  158. }
  159.