home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / uqwk / part01 / mail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-11  |  4.0 KB  |  219 lines

  1. #include <stdio.h>
  2. #include "uqwk.h"
  3.  
  4. /*
  5.  *  All sorts of stuff to do mail processing
  6.  */
  7.  
  8. FILE *mail_fd;            /* For mail spool */
  9. struct mail_ent *last_mp;    /* Points to last mail list entry */
  10.  
  11. DoMail ()
  12. /*
  13.  *  Process mail into QWK packet
  14.  */
  15. {
  16.     struct mail_ent *mailp;
  17.  
  18.     /* Open the mail spool */
  19.     if (NULL == (mail_fd = fopen (mail_file, "r")))
  20.     {
  21.         fprintf (stderr, "%s: can't open %s\n", progname, mail_file);
  22.         perror (progname);
  23.         return (0);
  24.     }
  25.  
  26.     /* Define the mail "conference" */
  27.     NewConference (MAIL_CONF_NAME);
  28.  
  29.     /* Construct the mail linked list */
  30.     MakeMailList ();
  31.  
  32.     /* Walk through all the messages */
  33.     mailp = mail_list;
  34.  
  35.     while (mailp != NULL)
  36.     {
  37.         DoMessage (mailp);
  38.         mailp = mailp->next;
  39.     }
  40.  
  41.     fclose (mail_fd);
  42.     fclose (ndx_fd);
  43.  
  44.     /* Now empty the mail box */
  45.     if (!read_only)
  46.     {
  47.         if (NULL == (mail_fd = fopen (mail_file, "w")))
  48.         {
  49.             fprintf (stderr, "%s: can't write %s\n", progname,
  50.                                 mail_file);
  51.         }
  52.         else
  53.         {
  54.             fclose (mail_fd);
  55.         }
  56.     }
  57. }
  58.  
  59. MakeMailList ()
  60. /*
  61.  *  Construct linked list of pointers to individual messages in
  62.  *  the mail spool.
  63.  */
  64. {
  65.     long offset;
  66.  
  67.     last_mp = NULL;
  68.  
  69.     /* Read through, looking for "From" lines */
  70.     offset = ftell (mail_fd);
  71.     while (NULL != Fgets (buf, BUF_LEN, mail_fd))
  72.     {
  73.         if (!strncmp (buf, "From ",  5))
  74.         {
  75.             DoFromLine (offset);
  76.         }
  77.         offset = ftell (mail_fd);
  78.     }
  79.     if (last_mp != NULL) last_mp->end = offset;
  80. }
  81.  
  82. DoFromLine (offset)
  83. long offset;
  84. {
  85.     struct mail_ent *mp;
  86.  
  87.     /* Get space for new mail list entry */
  88.     if (NULL==(mp=(struct mail_ent *) malloc(sizeof(struct mail_ent))))
  89.     {
  90.         fprintf (stderr, "%s: out of memory\n", progname);
  91.         exit (0);
  92.     }
  93.  
  94.     /* Fill in offset */
  95.     mp->begin = offset;
  96.  
  97.     if (last_mp == NULL)
  98.     {
  99.         /* This is first message */
  100.         mail_list = mp;
  101.     }
  102.     else
  103.     {
  104.         /* Add to end of list */
  105.         last_mp->next = mp;
  106.         last_mp->end = offset;
  107.     }
  108.  
  109.     mp->next = NULL;
  110.     last_mp = mp;
  111. }
  112.  
  113. DoMessage (mp)
  114. struct mail_ent *mp;
  115. /*
  116.  *  Convert a message to QWK format
  117.  */
  118. {
  119.     struct qwk_hdr hdr;
  120.     char c[PATH_LEN], *eof, ndx[5];
  121.     int out_bytes, n;
  122.  
  123.     /* Write the ndx file entry */
  124.     inttoms (blk_cnt, ndx);
  125.     ndx[4] = conf_cnt-1;
  126.     fwrite (ndx, 5, 1, ndx_fd);
  127.  
  128.     Spaces (&hdr, 128);
  129.  
  130.     /* Fill in the header fields we can do now */
  131.     hdr.status = QWK_PRIVATE;
  132.     PadNum (msg_cnt, hdr.number, 7);
  133.     Spaces (hdr.password, 12);
  134.     Spaces (hdr.refer, 8);
  135.     hdr.flag = QWK_ACT_FLAG;
  136.     IntNum (conf_cnt-1, hdr.conference);
  137.     IntNum (msg_cnt+1, hdr.msg_num);
  138.     hdr.tag = ' ';
  139.  
  140.     msg_cnt++;
  141.  
  142.     /* Seek to start of message */
  143.     fseek (mail_fd, mp->begin, 0);
  144.  
  145.     /* Read the From line */
  146.     Fgets (buf, BUF_LEN, mail_fd);
  147.  
  148.     /* The second field of the From line is assumed to be who
  149.        sent the message */
  150.     sscanf (&buf[5], "%s", c);
  151.     PadString (c, hdr.from, 25);
  152.  
  153.     /* Now read through header lines, looking for ones we need */
  154.     eof = Fgets (buf, BUF_LEN, mail_fd);
  155.     while ( (0 != strlen(buf)) && (eof != NULL) )
  156.     {
  157.         if (!strncmp (buf, "Date: ", 6))
  158.         {
  159.             ParseDate (&buf[6], &hdr);
  160.         }
  161.         else if (!strncmp (buf, "To: ", 4))
  162.         {
  163.             PadString (&buf[4], hdr.to, 25);
  164.         }
  165.         else if (!strncmp (buf, "Subject: ", 9))
  166.         {
  167.             PadString (&buf[9], hdr.subject, 25);
  168.         }
  169. /*
  170.         else if (!strncmp (buf, "From: ", 6))
  171.         {
  172.             PadString (&buf[6], hdr.from, 25);
  173.         }
  174. */
  175.  
  176.         eof = Fgets (buf, BUF_LEN, mail_fd);
  177.     }
  178.     mp->text = ftell (mail_fd);
  179.  
  180.     /* Fill in block count */
  181.     if (inc_hdrs)
  182.     {
  183.         PadNum (2+(mp->end-mp->begin)/128, hdr.blocks, 6);
  184.         blk_cnt += (1+(mp->end - mp->begin)/128);
  185.     }
  186.     else
  187.     {
  188.         PadNum (2+(mp->end-mp->text)/128, hdr.blocks, 6);
  189.         blk_cnt += (1+(mp->end - mp->text)/128);
  190.     }
  191.  
  192.     /* Write out the message header */
  193.     fwrite (&hdr, 128, 1, msg_fd);
  194.     blk_cnt++;
  195.  
  196.     /* Now write the message text */
  197.     if (inc_hdrs) fseek (mail_fd, mp->begin, 0);
  198.     out_bytes = 0;
  199.  
  200.     eof = Fgets (buf, BUF_LEN, mail_fd);
  201.     do
  202.     {
  203.         n = strlen (buf);
  204.         fwrite (buf, n, 1, msg_fd);
  205.         out_bytes += n;
  206.         if (n < BUF_LEN-1)
  207.         {
  208.             fputc (QWK_EOL, msg_fd);
  209.             out_bytes++;
  210.         }
  211.         eof = Fgets (buf, BUF_LEN, mail_fd);
  212.     } while ( (strncmp(buf,"From ", 5)) && (NULL != eof) );
  213.  
  214.     /* Pad block as necessary */
  215.     n = out_bytes % 128;
  216.     for (;n<128;n++) fputc (' ', msg_fd);
  217. }
  218.  
  219.