home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / MNLDOS.ZIP / src / msgtool.c < prev    next >
C/C++ Source or Header  |  2004-07-16  |  7KB  |  246 lines

  1. /* $Id: msgtool.c,v 1.6 2004/07/15 17:44:02 ozzmosis Exp $ */
  2.  
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7.  
  8. #include "makenl.h"
  9. #include "fts5.h"
  10. #include "msg.h"
  11. #include "fileutil.h"
  12.  
  13. #ifdef MALLOC_DEBUG
  14. #include "rmalloc.h"
  15. #endif
  16.  
  17. #ifdef DMALLOC
  18. #include "dmalloc.h"
  19. #endif
  20.  
  21. #define MSG_CRASH    0x0002
  22. #define MSG_PRIVATE  0x0001
  23. #define MSG_FILE     0x0010
  24. #define MSG_KILLSENT 0x0080
  25. #define MSG_LOCAL    0x0100
  26. #define MSG_HOLD     0x0400
  27.  
  28. struct MsgHdr
  29. {
  30.     char From[36], To[36], Subject[72], Date[20];
  31.     short TimesRead, DestNode, OrigNode, Cost, OrigNet, DestNet;
  32.     short DestZone, OrigZone, DestPoint, OrigPoint, Reply;
  33.     short Attribute, NextReply;
  34. };
  35.  
  36. int UsualMSGFlags;
  37. int MailerFlags;
  38. int NotifyAddress[3];
  39. int SubmitAddress[3];
  40. int MyAddress[3];
  41.  
  42. static char *MakeMSGFilename(char *outbuf, int num);
  43. static struct MsgHdr MyHeader =
  44.     { "MakeNL", "Coordinator", "subj", "date", 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0,
  46.     0, 0
  47. };
  48.  
  49. static int MSGnum;
  50. static FILE *MailFILE;
  51. static int MSGFlags;
  52.  
  53. static int SearchMaxMSG(const char *path)
  54. {
  55.     char *filename;
  56.     int maxnum = 0;
  57.     int aktnum;
  58.     struct _filefind f;
  59.     char searchmask[MYMAXDIR];
  60.  
  61.     myfnmerge(searchmask, NULL, NULL, "*", "MSG");
  62.     for (filename = os_findfirst(&f, path, searchmask);
  63.          filename != NULL; filename = os_findnext(&f))
  64.     {
  65.         getnumber(filename, &aktnum);
  66.         if (aktnum > maxnum)
  67.             maxnum = aktnum;
  68.     }
  69.     os_findclose(&f);
  70.  
  71.     return maxnum;
  72. }
  73.  
  74. int ParseAddress(const char *string, int out[3])
  75. {
  76.     char foo[2];
  77.     int temp[3];
  78.  
  79.     foo[0] = 0;
  80.     if (sscanf
  81.         (string, "%d:%d/%d%1s", &temp[A_ZONE], &temp[A_NET], &temp[A_NODE],
  82.          foo) == 3)
  83.     {
  84.         if (foo[0] != 0 || temp[A_ZONE] == 0 || temp[A_NET] == 0)
  85.             return -1;
  86.     }
  87.     else if (sscanf(string, "%d/%d%1s", &temp[A_NET], &temp[A_NODE], foo)
  88.              == 2)
  89.     {
  90.         temp[A_ZONE] = 0;
  91.         if (foo[0] != 0)
  92.             return -1;
  93.         if (temp[A_NET] == 0)
  94.             return -1;
  95.     }
  96.     else if (sscanf(string, "%d%1s", &temp[A_NODE], foo) != 0)
  97.     {
  98.         if (foo[0] != 0)
  99.             return -1;
  100.         temp[A_NET] = 0;
  101.         temp[A_ZONE] = 0;
  102.     }
  103.     else
  104.         return -1;
  105.     memcpy(out, temp, 3 * sizeof(int));
  106.  
  107.     return 0;
  108. }
  109.  
  110. FILE *OpenMSGFile(int adress[3], char *filename)
  111. {
  112.     char intlline[46];
  113.     char filenamebuf[MYMAXDIR];
  114.     int intl;
  115.  
  116.     if (filename)
  117.     {
  118.         strcpy(MyHeader.Subject, filename);
  119.         MSGFlags = (MailerFlags & MF_SUBMIT) >> MF_SHIFT_SUBMIT;
  120.         MyHeader.Attribute = (MSGFlags & MF_CRASH ? MSG_CRASH : 0) |
  121.             (MSGFlags & MF_HOLD ? MSG_HOLD : 0) |
  122.             MSG_PRIVATE | MSG_FILE | MSG_KILLSENT | MSG_LOCAL;
  123.     }
  124.     else
  125.     {
  126.         MyHeader.Attribute = MSG_PRIVATE | MSG_KILLSENT | MSG_LOCAL;
  127.         sprintf(MyHeader.Subject, "%s received", WorkFile);
  128.         MSGFlags = UsualMSGFlags;
  129.     }
  130.     if (!MSGFlags)
  131.         return (MailFILE = NULL);
  132.     if (!adress[A_ZONE])
  133.         adress[A_ZONE] = MyAddress[A_ZONE];
  134.     if (MyAddress[A_ZONE] == adress[A_ZONE])
  135.     {
  136.         intlline[0] = 0;
  137.         MyHeader.DestNet = adress[A_NET];
  138.         MyHeader.DestNode = adress[A_NODE];
  139.         intl = MailerFlags & (MF_INTL |
  140.                               (MF_INTL << MF_SHIFT_ERRORS) |
  141.                               (MF_INTL << MF_SHIFT_SUBMIT));
  142.     }
  143.     else
  144.     {
  145.         MyHeader.DestNet = MyAddress[A_ZONE];
  146.         MyHeader.DestNode = adress[A_ZONE];
  147.         intl = 1;
  148.     }
  149.     if (intl)
  150.     {
  151.         if (MyAddress[A_ZONE] == 0)
  152.         {
  153.             fprintf(stdout,
  154.                     "\nWARNING -- Don't know your zone, can't send interzone message to %d:%d/%d\n\n",
  155.                     adress[A_ZONE], adress[A_NET], adress[A_NODE]);
  156.             return (MailFILE = NULL);
  157.         }
  158.         sprintf(intlline, "\x01INTL %d:%d/%d %d:%d/%d\r\n", adress[A_ZONE],
  159.                 adress[A_NET], adress[A_NODE], MyAddress[A_ZONE],
  160.                 MyAddress[A_NET], MyAddress[A_NODE]);
  161.     }
  162.     MailFILE = fopen(MakeMSGFilename(filenamebuf, ++MSGnum), "wb");
  163.     if (!MailFILE)
  164.         die(254, 1, "Cannot create %s", filenamebuf);
  165.     fwrite(&MyHeader, sizeof(MyHeader), 1, MailFILE);
  166.     fputs(intlline, MailFILE);
  167.     if (!filename)
  168.         return MailFILE;
  169.     fclose(MailFILE);
  170.     return (FILE *) ! NULL;     /* Just say OK - but it *smells* */
  171. }
  172.  
  173. FILE *CloseMSGFile(int status)
  174. {
  175.     char filename[MYMAXDIR];
  176.  
  177.     if (MailFILE != NULL)
  178.     {
  179.         if (status >= 0)
  180.         {
  181.             if (status != 0)
  182.             {
  183.                 MSGFlags >>= MF_SHIFT_ERRORS; /* Use the error flags */
  184.                 strcat(MyHeader.Subject, " with errors");
  185.             }
  186.             else
  187.                 MSGFlags &= MF_RECEIPT;
  188.             if (MSGFlags)
  189.             {
  190.                 putc(0, MailFILE);
  191.                 fseek(MailFILE, 0L, SEEK_SET);
  192.                 MyHeader.Attribute |=
  193.                     (MSGFlags & MF_CRASH ? MSG_CRASH : 0) | (MSGFlags &
  194.                                                              MF_HOLD ?
  195.                                                              MSG_HOLD : 0);
  196.                 fwrite(&MyHeader, sizeof(MyHeader), 1, MailFILE);
  197.             }
  198.             else
  199.                 status = -1;
  200.         }
  201.         fclose(MailFILE);
  202.         if (status < 0)
  203.             unlink(MakeMSGFilename(filename, MSGnum--));
  204.     }
  205.     MailFILE = NULL;
  206.     return MailFILE;
  207. }
  208.  
  209. void BuildHeaders(void)
  210. {
  211.     static char *DOWNames[] =
  212.         { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  213.     static char *MonthNames[12] =
  214.         { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
  215.         "Sep", "Oct", "Nov", "Dec"
  216.     };
  217.     time_t akt_time;
  218.     struct tm *the_time;
  219.  
  220.     time(&akt_time);
  221.     the_time = localtime(&akt_time);
  222.     /* BUG FIXED: Y2K-bug changed tm_year to tm_year % 100 and its format
  223.        to %02d */
  224.     sprintf(MyHeader.Date, "%s %2d %s %02d %02d:%02d",
  225.             DOWNames[the_time->tm_wday], the_time->tm_mday,
  226.             MonthNames[the_time->tm_mon], the_time->tm_year % 100,
  227.             the_time->tm_hour, the_time->tm_min);
  228.     MyHeader.OrigNet = MyAddress[A_NET];
  229.     MyHeader.OrigNode = MyAddress[A_NODE];
  230.     MSGnum = SearchMaxMSG(MessageDir);
  231. }
  232.  
  233. char *MakeMSGFilename(char *outbuf, int num)
  234. {
  235.     char buffer[6];
  236.  
  237.     sprintf(buffer, "%u", num);
  238.     myfnmerge(outbuf, NULL, MessageDir, buffer,
  239. #ifndef __unix__
  240.               "MSG");
  241. #else
  242.               "msg");
  243. #endif
  244.     return outbuf;
  245. }
  246.