home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mr2iuucp.zip / UNMBOX.C < prev    next >
C/C++ Source or Header  |  1996-10-23  |  6KB  |  223 lines

  1. /* ummbox.c
  2.  * program to take a uupc inbox and translate it into seperate mail messages
  3.  *
  4.  * Michael Taylor miket@pcug.org.au
  5.  *
  6.  * History:
  7.  * 1.0.00    Initial version.        12/10/1996
  8.  * 1.0.01    Fixes.                  19/10/1996
  9.  *              Fix parsing of UUPC variables
  10.  *              Add fromsep logic - NO fromsep should be defined!
  11.  *              Inprove get_next_fname so it does not
  12.  *              return a filename that already exists.
  13.  *              Rename UUPC mbox so it cannot be written
  14.  *              to by uuxqt/rmail.
  15.  *
  16.  * This program is freely redistributable - source and all!
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. static char version[] =
  24. "unmbox by Michael Taylor (miket@pcug.org.au) - Version 1.0.01";
  25.  
  26. /* logic
  27.  *
  28.  *    1. open MR/2 ICE mail-in control file "mr2i.ndx"
  29.  *    1. open mailbox.
  30.  *    2. if empty then stop.
  31.  *    3. find next file name to use.
  32.  *    4. read mailbox -
  33.  *        write out to new file.
  34.  *        increment file name.
  35.  *        repeat until mail box is empty.
  36.  *
  37.  */
  38.  
  39. char * get_next_fname (void);
  40. int get_uupc_vars (void);
  41. void err (char * str);
  42.  
  43. static char old_mailbox[256];
  44. static char mailbox[256];
  45. static char maildir[256];
  46. static char mailext[256];
  47.  
  48. static char * mailbox_name = NULL;
  49.  
  50. static FILE * uupcmfile;
  51.  
  52. static char basename[256];
  53. static char nxtfname[256];
  54.  
  55. char *
  56. get_next_fname () {
  57.         char *name1;
  58.  
  59.         if ((name1 = tempnam (basename, "mr2")) == NULL) {
  60.                 err ("Cannot create unique filename\n");
  61.                 return NULL;
  62.         }
  63.         strcpy (nxtfname, name1);
  64.         free (name1);
  65.     return (char *)nxtfname;
  66.  
  67. }
  68.  
  69. int
  70. get_uupc_vars () {
  71.     char buf[256];
  72.     int  i, cf;
  73.     char *p, *fn;
  74.     FILE *tmp;
  75.  
  76.     mailbox_name = getenv ("UNMB_MAIL");
  77.     if (mailbox_name != NULL) {
  78.         return (1);
  79.     }
  80.  
  81.     /*--------------------- load UUPC rc files ---------------------------*/
  82.     /* read the system file first */
  83.     for (cf = 0, i = 0; i < 2; i++) {
  84.         /* choose the file to open */
  85.         if (i == 0) {
  86.         fn = getenv ("UUPCSYSRC");
  87.         if (fn == NULL) {
  88.             err ("Enviroment variable UUPCSYSRC not defined\n");
  89.                 exit (EXIT_FAILURE);
  90.         }
  91.         } else {
  92.         fn = getenv ("UUPCUSRRC");
  93.         if (fn == NULL) {
  94.             err ("Enviroment variable UUPCUSRRC not defined\n");
  95.                 exit (EXIT_FAILURE);
  96.         }
  97.         }
  98.         if ((tmp = fopen (fn, "r")) != NULL) {
  99.             while (fgets (buf, 255, tmp)) {
  100.             p = buf + strlen (buf) - 1;
  101.             if (*p == '\n')
  102.                 *p = '\0';
  103.             if (p > buf)
  104.                 if (*(p-1) == '\n')
  105.                     *(p-1) = '\0';
  106.  
  107.             if (!cf && strnicmp (buf, "confdir=", 8) == 0) {
  108.             /* default root dir if mailbox var not found */
  109.                 cf = 1;
  110.                 strcpy (maildir, buf+8);
  111.                                 strcat (maildir, "\\mail"); /* 1.0.01 */
  112.             } else
  113.             if (strnicmp (buf, "mailbox=", 8) == 0) {
  114.             /* mailbox base name */
  115.                 cf = 1;
  116.                 strcpy (mailbox, buf+8);
  117.             } else
  118.             if (strnicmp (buf, "maildir=", 8) == 0) {
  119.             /* file name for mailbox */
  120.                 strcpy (maildir, buf+8);
  121.             } else
  122.             if (strnicmp (buf, "mailext=", 8) == 0) {
  123.             /* extension of file name for mailbox */
  124.                 strcpy (mailext, buf+8);
  125.             }
  126.             }
  127.             fclose (tmp);
  128.  
  129.         } else {
  130.         fprintf (stderr, "Cannot open %s\n", fn);
  131.         }
  132.     }
  133.  
  134.     if (cf) {
  135.         mailbox_name = maildir;
  136.     }
  137.     if (mailbox_name == NULL) {
  138.         return (0);
  139.     }
  140.  
  141.     strcat (maildir, "\\");
  142.     strcat (maildir, mailbox);
  143.     strcat (maildir, ".");
  144.         strcpy (old_mailbox, maildir);
  145.     strcat (old_mailbox, mailext);
  146.     strcat (maildir, "tmp");
  147.     
  148.     return (1);
  149. }
  150.  
  151. void
  152. err (char * str) {
  153.     FILE * errfile;
  154.  
  155.         if ((errfile = fopen ("mail\\getmail.err", "w")) == NULL) {
  156.             fprintf (stderr, "unmbox: error - cannot open file for error message\n");
  157.         exit (EXIT_FAILURE);
  158.            }
  159.         fprintf (errfile, "%s", str);
  160.         fclose (errfile);
  161. }
  162.  
  163.  
  164. static char x01x20[20] =
  165.     "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01";
  166. static char xbuf[20480];
  167.  
  168. int
  169. main (int argc, char * argv[]) {
  170.     FILE *    mfile, * infile, * errfile;
  171.     char    *p, *q, *r;
  172.     int    nlines, n;
  173.  
  174.         if (argc < 2) {
  175.         err ("usage: unmbox <SMTP directory>\n");
  176.         exit (EXIT_FAILURE);
  177.     }
  178.  
  179.         strcpy (basename, argv[1]);
  180.         
  181.         if (!get_uupc_vars ()) {
  182.         err ("error - cannot find UUPC mail box\n");
  183.         exit (EXIT_FAILURE);
  184.     }
  185.  
  186.         remove (mailbox_name);
  187.     if (rename (old_mailbox, mailbox_name)) {
  188. /*        err ("error - cannot access UUPC mailbox - try again later\n");*/
  189.         exit (EXIT_FAILURE);
  190.     }
  191.     if ((infile = fopen (mailbox_name, "r")) == NULL) {
  192. /*        err ("error - cannot open UUPC mailbox\n");*/
  193.         exit (EXIT_FAILURE);
  194.     }
  195.  
  196.     while (fgets (xbuf, 20479, infile)) {
  197.         if (0 == memcmp (xbuf, x01x20, 20))
  198.             continue;
  199.         if ((mfile = fopen (get_next_fname (), "w")) == NULL) {
  200.                         /* this error most likely occurs if the SMTP directory doesn't exist */
  201.                    err ("error - cannot open new file for mail\n");
  202.                         /* just to be nice - rename mailbox so that the user can try again later */
  203.                         rename (mailbox_name, old_mailbox);
  204.             exit (EXIT_FAILURE);
  205.         }
  206.         fputs (xbuf, mfile); nlines = 1;
  207.         while (fgets (xbuf, 20479, infile)) {
  208.             if (0 == memcmp (xbuf, x01x20, 20))
  209.                 break;
  210.                        fputs (xbuf, mfile); nlines++;
  211.         }
  212.  
  213.         fclose (mfile);
  214.         if (feof (infile))
  215.             break;
  216.     }
  217.  
  218.     fclose (mfile);
  219.     fclose (infile);
  220.  
  221.     exit (EXIT_SUCCESS);
  222.     return (EXIT_SUCCESS); /* gets rid of dumb compiler warning */
  223. }