home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / nfsml202.zip / MX.C < prev    next >
Text File  |  1991-12-07  |  5KB  |  221 lines

  1. /* MX.C - (c) 1991 Nils Hammar */
  2.  
  3. /*
  4.     (MX = MaileXport)
  5.  
  6.     MX.C is a tiny C program that has to be started periodically
  7.     from cron on the UNIX computer that serves as PC-NFS server.
  8.     It must be the same computer as where the email directory
  9.     exists.
  10.  
  11.     The program examines the directory /usr/mail/pcsend, and
  12.     checks files that is supposed to be written by the program MAILRD
  13.     under MS-DOS.
  14.     It is important that the field "To: ..." is the first line of the
  15.     message to be sent! Any other location of this line is illegal
  16.     and will result in unpredictable effects.
  17.  
  18.     This program may be used by anybody as long as the source of the
  19.     program is specified.
  20.     I cannot guarantee that this programs will work in all environments,
  21.     and will not be responsible for any malfunctions or peculiar effects
  22.     that this program results in.
  23.  
  24.     USE THE PROGRAM AT YOUR OWN RISC!
  25.  
  26.     I recommend that this program is started each five minutes from cron.
  27.  
  28.     I didn't implement an infinite loop with a sleep because I think that
  29.     this allocates system resources in an ill-behaved way.
  30.     With this method you will always start a fresh program that isn't
  31.     infected with old data.
  32.  
  33.     Any modifications or enhancements done to this program may be sent to:
  34.     nils@f109f.mil.se, 4341@msg.abc.se or
  35.     Nils.Hammar@f90.n204.z2.fidonet.org
  36.     (Nils Hammar at 2:204/90@fidonet.org)
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41. #include <dirent.h>
  42. #include <sys/ino.h>
  43. #include <sys/stat.h>
  44. #include <time.h>
  45.  
  46. /*    A home-brewn string compare algorithm. */
  47.  
  48. int        subcompstr(compval, string)
  49. unsigned char    *compval, *string;
  50. {
  51.     int    i;
  52.  
  53.     if (strlen(compval) > strlen(string)) return(-1);
  54.  
  55.     i=0;
  56.     while( *(compval + i) == *(string + i) && *(compval + i) != 0) i++;
  57.  
  58.     if (*(compval + i) == 0) return(0);
  59.  
  60.     return(1);
  61. }
  62.  
  63. /*    Process files for a specific user. */
  64.  
  65. void    analyze(user)
  66. char    *user;
  67. {
  68.     FILE    *f, *g;
  69.     char    line[160],
  70.         touser[160],
  71.         tempfile[160],
  72.         tmp[160],
  73.         tempfile2[160];
  74.     long    tid[1];
  75.     int    i;
  76.  
  77.  
  78. /*    Create a temporary filename in /tmp.
  79.     Change here if /usr/mail is on a different disk, since it
  80.     isn't possible to move files between physical disks with the
  81.     "rename" function under all *nix versions. */
  82.  
  83.     sprintf(tempfile2, "/tmp/mxm.%lx", time(tid));
  84.  
  85. /*    Rename the file that we will process.
  86.     If this routine fails, don't worry and return.
  87.     (The file might be under processing from the user that owns
  88.     this file, or is this program maybe run twice???) */
  89.  
  90.     if (rename(user, tempfile2) != 0) return;
  91.  
  92. /*    If we lost the file after or during the rename,
  93.     then we will have to worry!!! */
  94.  
  95.     if ((f=fopen(tempfile2, "r")) == NULL)
  96.     {
  97.         perror(tempfile2);
  98.         exit(9);
  99.     }
  100.  
  101. /*    Create an another temporary file... */
  102.  
  103.     sprintf(tempfile, "/tmp/mxs.%lx", time(tid));
  104.  
  105.     if ((g=fopen(tempfile, "w")) == NULL)
  106.     {
  107.         perror(tempfile);
  108.         exit(9);
  109.     }
  110.  
  111. /*    Read the input file until it's empty. */
  112.  
  113.     strcpy(touser, "");
  114.     while(fgets(line, 158, f) != NULL)
  115.     {
  116. /*    If we caught a "To:"-field, it isn't a part of the message, it's
  117.     control information for this program! */
  118.  
  119.         if (subcompstr("To: ", line) == 0)
  120.         {
  121. /*    If it was the secont time we encoutered the "To:"-field,
  122.     we have a message to send. */
  123.  
  124.             if (*touser != 0)
  125.             {
  126.                 fclose(g);
  127.                 i=0;
  128.                 while(*(touser + i) != 0)
  129.                 {
  130.                     if (*(touser + i) < ' ')
  131.                         *(touser + i)=' ';
  132.                     i++;
  133.                 }
  134.                 sprintf(tmp, "mail %s < %s", touser, tempfile);
  135.                 system(tmp);
  136.                 unlink(tempfile);
  137.  
  138.                 if ((g=fopen(tempfile, "w")) == NULL)
  139.                 {
  140.                     perror(tempfile);
  141.                     exit(9);
  142.                 }
  143.             }
  144.  
  145. /*    Extract the username that will receive the message. */
  146.  
  147.             strcpy(touser, line+4);
  148.         }
  149.         else
  150.         {
  151.             i=0;
  152.             while(*(line + i) != 0)
  153.             {
  154.                 if (*(line + i) < ' ')
  155.                     *(line + i)=0;
  156.                 i++;
  157.             }
  158.  
  159.             fprintf(g, "%s\n", line);
  160.         }
  161.     }
  162.  
  163.     fclose(g);
  164.  
  165.     i=0;
  166.     while(*(touser + i) != 0)
  167.     {
  168.         if (*(touser + i) < ' ')
  169.             *(touser + i)=' ';
  170.         i++;
  171.     }
  172.  
  173. /*    If there was a message to send, do it. This is
  174.     necessary, since a message is terminated either by the
  175.     "To:"-field of the next message, or an end of file. */
  176.  
  177.     if (*touser != 0)
  178.     {
  179.         sprintf(tmp, "mail %s < %s", touser, tempfile);
  180.         system(tmp);
  181.     }
  182.  
  183. /*    Clean up! */
  184.  
  185.     unlink(tempfile);
  186.     fclose(f);
  187.     unlink(tempfile2);
  188. }
  189.  
  190. void    main()
  191. {
  192.     char        tmp[160],
  193.             toaddr[160],
  194.             path[160];
  195.     DIR        *dp;
  196.     struct dirent    *de;
  197.     struct stat    *buf;
  198.  
  199.     if((dp=opendir("/usr/mail/pcsend")) == NULL)
  200.     {
  201.         perror("/usr/mail/pcsend");
  202.         exit(9);
  203.     }
  204.  
  205. /*    This is the way we are working when we look for mail to send.
  206.     If anybody has a better idea, you can tell me... */
  207.  
  208.     while((de=readdir(dp)) != NULL)
  209.     {
  210.         strcpy(path, "/usr/mail/pcsend/");
  211.         strcat(path, de->d_name);
  212.  
  213.         if(strcmp(de->d_name, ".") != 0 &&
  214.             strcmp(de->d_name, "..") != 0 &&
  215.             *de->d_name != '0')
  216.         {
  217.             analyze(path);
  218.         }
  219.     }
  220. }
  221.