home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / OSKBox.lzh / MAILBOX / CC / msgproc.c < prev    next >
C/C++ Source or Header  |  1993-04-25  |  11KB  |  459 lines

  1. #include <stdio.h>
  2. #include <dir.h>
  3. #include <errno.h>
  4. #include <ctype.h>
  5. #include <modes.h>
  6. #include "mailbox.h"
  7.  
  8. #define MAX_AGE 5    /* Max #days for SALE, @ALLUS and @WW */
  9.  
  10. struct userstruct user;
  11.  
  12. int debugflag = 0;
  13. int bbsflag = 0;
  14. int sysopflag = 0;
  15. int quietflag = 0;
  16.  
  17. extern int xflag;
  18.  
  19. main (argc, argv)
  20. char *argv[];
  21. {
  22.     chdir (HOME);
  23.     strcpy (user.uscall, "MPROC");
  24.     user.usopt = ISSUPER;
  25.     xflag = 1;
  26.     if (argc < 2)
  27.         {
  28.         printf ("Process a new BBS message.\n\n");
  29.         printf ("form:  msgproc [<options>] <message #>\n");
  30.         printf ("\nOptions are:\n");
  31.         printf ("    -b        Sender is a BBS\n");
  32.         printf ("    -s        Sender is a sysop\n");
  33.         printf ("    -q        Supress output of messages\n");
  34.         printf ("    -d[d...]  Provide debugging output\n");
  35.         exit (0);
  36.         }
  37.     while (--argc > 0 && (++argv)[0][0]=='-')
  38.         {
  39.         char *cp = argv[0] + 1;
  40.         while (*cp)
  41.             switch (*cp++)
  42.                 {
  43.  
  44.         case 'd':
  45.                 debugflag++;
  46.                 continue;
  47.  
  48.         case 'b':
  49.                 bbsflag++;
  50.                 continue;
  51.  
  52.         case 's':
  53.                 sysopflag++;
  54.                 continue;
  55.  
  56.         case 'q':
  57.                 quietflag++;
  58.                 continue;
  59.  
  60.         default:
  61.                 fprintf(stderr, "Unknown flag '%c'\n", *(cp-1));
  62.                 continue;
  63.                 }
  64.             }
  65.         if (argc<=0)
  66.             {
  67.             fprintf (stderr, "Insufficient arguments\n");
  68.             exit(0);
  69.             }
  70.     open_mail ();
  71.     while (argc--)
  72.         msgproc (atoi (*argv++));
  73.     close_mail ();
  74.     }
  75.  
  76. msgproc (msgnum)
  77. {
  78.     struct msg_header *head;
  79.     int file;
  80.  
  81.     reset_mail ();
  82.     if (!(head = next_mail (is_num, msgnum)))
  83.         reject ("No record", msgnum);
  84.     if ((file = open_msg (head)) <= 0)
  85.         reject ("Can't open file", msgnum);
  86.     read_dist_list ();
  87.     copy_msg (head);
  88.     check_file (head, file);
  89.     check_hold (head);
  90.     build_haddr (head);
  91.     close (file);
  92.     }
  93.  
  94. check_file (head, f)
  95. struct msg_header *head;
  96. {
  97.     char str[256], out[256];
  98.     int len;
  99.     char bbs[7], *p;
  100.     int msg_num;
  101.     int repeat = 0;
  102.     int orig = 0, jorig;
  103.  
  104.     if (debugflag) printf ("check_file %d\n", head->mhnr);
  105.     lseek (f, 0L, 0);
  106.     while (len = readln (f, str, 256))
  107.         {
  108.         str[len-1] = '\0';
  109.         if (parse_head (str, out) > 1 && *out == 'R')
  110.             {
  111.             for (p = out; *p; p += strlen (p) + 1)
  112.                 {
  113.                 if (*p == '@')
  114.                     {
  115.                     scanword (p+1, bbs, sizeof (bbs));
  116.                     get_ssid (bbs);
  117.                     upper (bbs);
  118.                     if (strcmp (bbs, MYCALL) == 0)
  119.                         repeat++;
  120.                     checkoff (head, bbs);
  121.                     }
  122.                 else if (*p == 'R')
  123.                     orig = strToDate (p+1);
  124.                 else if (*p == '#')
  125.                     msg_num = atoi (p+1);
  126.                 }
  127.             }
  128.         }
  129.  
  130.     if (debugflag) printf ("  repeat=%d orig =%02d/%02d/%02d\n", repeat,
  131.      (orig>>8)&0xff, orig & 0xff, (orig>>16)-1900);
  132.  
  133.     if (repeat > 1)
  134.         {
  135.         head->mhstat = 'H';
  136.         update_mail ();
  137.         notify (head, "Seen this one already");
  138.         reject ("Seen this one already", head->mhnr);
  139.         }
  140.  
  141.     if ((is_to (head, "SALE") || is_at (head, "WW") || is_at (head, "ALLUS")) && orig)
  142.         {
  143.         int i;
  144.         int time, date, tick, day;
  145.         
  146.         _sysdate (1, &time, &date, &day, &tick);
  147.         _julian (&i, &orig);
  148.         orig += MAX_AGE;
  149.         gregorian (&i, &orig);
  150.         head->mhdate_xpir = orig;
  151.         update_mail ();
  152.         _julian (&i, &orig);
  153.         if (orig < date)
  154.             {
  155.             head->mhstat = 'X';
  156.             update_mail ();
  157.             log ("K  %d %s", head->mhnr, "Too old");
  158.             reject ("Bulletin too old", head->mhnr);
  159.             }
  160.         }
  161.         
  162.     if (bulletin (head) && *head->mhbid == '\0')
  163.         {
  164.         sprintf (head->mhbid, "%d_%s", msg_num, bbs);
  165.         if (debugflag) printf ("  synthesized bid='%s'\n", head->mhbid);
  166.         update_mail ();
  167.         log ("M %d $ %s", head->mhnr, head->mhbid);
  168.         }
  169.  
  170.     }
  171.  
  172. test_hold (head, line, action)
  173. struct msg_header *head;
  174. char *line;
  175. char **action;
  176.     {
  177.     char w[40], w2[40], *p = line;
  178.     
  179.     
  180.     while (*p)
  181.         {
  182.         p += scanword (p, w, 40);
  183.         if (w[0] == ':') break;
  184.         if (debugflag > 1)
  185.             printf ("    test %s\n", w);
  186.         if (w[0] == '!')
  187.             switch (toupper (w[1]))
  188.                 {
  189.     case '>':    if (is_to (head, w+2)) return 0;  break;
  190.     case '<':    if (is_from (head, w+2)) return 0;  break;
  191.     case '@':    if (is_at (head, w+2)) return 0;  break;
  192.     case '$':    if (strncmp (head->mhbid, w+2, strlen (w+2)) == 0) return 0;  break;
  193.     case 'L':    sprintf (w2, "*%s*", w+2);
  194.                 if (is_title (head, w2)) return 0;
  195.                 break;
  196.     case 'T':    if (head->mhtype == toupper (w[2])) return 0;  break;
  197.     case '-':    switch (toupper (w[2]))
  198.                     {
  199.         case 'B':    if (bbsflag) return 0;  break;
  200.         case 'S':    if (sysopflag) return 0;  break;
  201.                     }
  202.                 break;
  203.                 }
  204.         else
  205.             switch (toupper (w[0]))
  206.                 {
  207.     case '>':    if (!is_to (head, w+1)) return 0;  break;
  208.     case '<':    if (!is_from (head, w+1)) return 0;  break;
  209.     case '@':    if (!is_at (head, w+1)) return 0;  break;
  210.     case '$':    if (strncmp (head->mhbid, w+1, strlen (w+1)) != 0) return 0;  break;
  211.     case 'L':    sprintf (w2, "*%s*", w+1);
  212.                 if (!is_title (head, w2)) return 0;
  213.                 break;
  214.     case 'T':    if (head->mhtype != toupper (w[1])) return 0;  break;
  215.     case '-':    switch (toupper (w[1]))
  216.                     {
  217.         case 'B':    if (!bbsflag) return 0;  break;
  218.         case 'S':    if (!sysopflag) return 0;  break;
  219.                     }
  220.                 break;
  221.                 }
  222.         }
  223.     if (debugflag > 1)
  224.         printf ("    passed\n");
  225.     if (action)
  226.         *action = p;
  227.     return 1;
  228.     }
  229.  
  230. check_hold (head)
  231. struct msg_header *head;
  232. {
  233.     int f, len;
  234.     char line[80], w[40], *action, *comment = 0;
  235.  
  236.     if (debugflag) printf ("check_hold %d\n", head->mhnr);
  237.     sprintf (line, "%s/%s", DISDIR, "hold.lst");
  238.     if ((f = open (line, 1)) < 0)
  239.         return;
  240.     while (len = readln (f, line, 80))
  241.         {
  242.         line[len-1] = '\0';
  243.         if (debugflag>1) printf ("  %s?\n", line);
  244.         for (action = line; *action; action++)
  245.             if (*action == ';') break;
  246.         if (*action)
  247.             {
  248.             comment = action + 1;
  249.             *action = '\0';
  250.             if (debugflag>1) printf ("   comment='%s'\n", comment);
  251.             }
  252.         else
  253.             comment = 0;
  254.         if (test_hold (head, line, &action))
  255.             {
  256.             while (*action)
  257.                 {
  258.                 action += scanword (action, w, 40);
  259.                 if (debugflag > 1)
  260.                     printf ("  action = %s\n", w);
  261.                 switch (toupper (w[0]))
  262.                     {
  263.         case '>':    strncpy (head->mhto, w+1, sizeof (head->mhto));
  264.                     update_mail ();
  265.                     break;
  266.  
  267.         case '<':    strncpy (head->mhfrom, w+1, sizeof (head->mhfrom));
  268.                     update_mail ();
  269.                     break;
  270.  
  271.         case '@':    strncpy (head->mhbbs, w+1, sizeof (head->mhbbs));
  272.                     update_mail ();
  273.                     break;
  274.  
  275.         case 'T':    head->mhtype = toupper (w[1]);
  276.                     update_mail ();
  277.                     break;
  278.  
  279.         case 'R':    head->mhstat = toupper (w[1]);
  280.                     update_mail ();
  281.                     if (head->mhstat == 'H')
  282.                         {
  283.                         if (comment)
  284.                             notify (head, comment);
  285.                         else
  286.                             notify (head, "On sysop's hold list");
  287.                         }
  288.                     break;
  289.                     
  290.                     }
  291.                 if (toupper (w[0]) == 'R' && toupper (w[1]) == 'X')
  292.                     log ("K %d", head->mhnr);
  293.                 else
  294.                     log ("M %d %c %s", head->mhnr, toupper (w[0]), w+1);
  295.                 }
  296.             }
  297.         }
  298.     close (f);
  299.     return;
  300.     }    
  301.  
  302.     
  303. notify (head, msg)
  304. struct msg_header *head;
  305. char *msg;
  306. {
  307.     struct msg_header savehead, sendhead, *new_head;
  308.     char str[80];
  309.     int file;
  310.  
  311.     _strass (&savehead, head, sizeof (struct msg_header));
  312.     strcpy (sendhead.mhfrom, user.uscall);
  313.     strcpy (sendhead.mhto, MYCALL);
  314.     sendhead.mhtype = 'P';
  315.     sendhead.mhstat = 'N';
  316.     sendhead.mhbbs[0] = '\0';
  317.     sprintf (str, "Hold notification: Msg. #%d", savehead.mhnr);
  318.     strcpy (sendhead.mhtit, str);
  319.     reset_mail ();
  320.     if ((file = creat_msg (&sendhead, &new_head)) == -1)
  321.         return;
  322.     sprintf (str, "The following message has been put on hold for review:\n\n");
  323.     write (file, str, strlen (str));
  324.     print_header (&savehead, file, 1);
  325.     sprintf (str, "\nReason: %s\n", msg);
  326.     write (file, str, strlen (str));
  327.     new_head->mhsize = _gs_size (file);
  328.     close (file);
  329.     log_send (new_head, 0);
  330.     update_mail ();
  331.     reset_mail ();
  332.     next_mail (is_num, savehead.mhnr);
  333.     }
  334.  
  335. build_haddr (head)
  336. struct msg_header *head;
  337.     {
  338.     char w[40], *p;
  339.  
  340.     if (debugflag) printf ("build_haddr %d\n", head->mhnr);
  341.     scanaddr (head->mhbbs, w, 40);
  342.     if (*w && strcmp (w, head->mhbbs) == 0
  343.            && strcmp (w, MYCALL) != 0
  344.            && !bulletin (head))
  345.         {
  346.         /* Try to build hierarchical address for BBS */
  347.         DIR *dirptr;
  348.         struct direct *entry;
  349.         char *pat = "*";
  350.         int len, f1;
  351.         char name[40], line[80];
  352.             
  353.         if (debugflag) printf ("  BBS='%s', needs H-addr\n", head->mhbbs);
  354.         if ((dirptr = opendir ("statebbs")) == NULL)
  355.             return;        /* Error opening directory */
  356.         while (entry = readdir (dirptr))
  357.             if (_cmpnam (entry->d_name, pat, strlen (pat)) == 0)
  358.                 {
  359.                 sprintf (name, "statebbs/%s", entry->d_name);
  360.                 if ((f1 = open (name, 1)) > 0)
  361.                     {
  362.                     if (debugflag > 1) printf ("  trying '%s':", entry->d_name);
  363.                     while ((len = readln (f1, line, 256)) > 0)
  364.                         {
  365.                         line[len-1] = '\0';
  366.                         scanword (line, name, 40);
  367.                         upper (name);
  368.                         get_ssid (name);
  369.                         if (debugflag > 2) printf (" %s", name);
  370.                         if (_cmpnam (w, name, strlen (name)) == 0)
  371.                             {
  372.                             sprintf (head->mhbbs, "%s.%s", w, entry->d_name);
  373.                             upper (head->mhbbs);
  374.                             for (p = head->mhbbs; *p; p++)
  375.                                 if (*p == '_')
  376.                                     *p = '#';
  377.                             update_mail ();
  378.                             log ("M %d @ %s", head->mhnr, head->mhbbs);
  379.                             close (f1);
  380.                             if (debugflag) printf ("  New='%s'\n", head->mhbbs);
  381.                             goto gotit;
  382.                             }
  383.                         }
  384.                     close (f1);
  385.                     if (debugflag > 1) printf ("\n");
  386.                     }
  387.                 }
  388. gotit:    closedir (dirptr);
  389.         }
  390.     }
  391.  
  392. copy_msg (head)
  393. struct msg_header *head;
  394. {
  395.     struct msg_header newhead;
  396.     int f1, f2, len;
  397.     char line[256];
  398.     int msgnum = head->mhnr;
  399.  
  400.     if (debugflag) printf ("copy_msg %d\n", head->mhnr);
  401.     if ((is_to (head, "SYSOP") && is_at (head, "NCPA")) ||
  402.         (is_to (head, "SYSOP") && is_at (head, "ALLCAN")) ||
  403.         (is_to (head, "SYSOP") && is_at (head, "NBAY")) ||
  404.         (is_to (head, "SYSOP") && is_at (head, "NCGATE")) ||
  405.         (is_to (head, "SYSOP") && is_at (head, "ALLCA")) ||
  406.         (is_to (head, "SYSOP") && is_at (head, "ALLUS")) ||
  407.         (is_to (head, "BOARD") && is_at (head, "NCPA")))
  408.         {
  409.         if (debugflag) printf ("  Copying to %s\n", MYCALL);
  410.         if ((f1 = open_msg (head)) == -1)
  411.             {
  412.             if (debugflag) printf ("  Error %d opening message file\n", errno);
  413.             return;
  414.             }
  415.         strcpy (newhead.mhfrom, head->mhfrom);
  416.         strcpy (newhead.mhto, MYCALL);
  417.         strcpy (newhead.mhbbs, MYCALL);
  418.         sprintf (line, "Copy of #%d: %s", head->mhnr, head->mhtit);
  419.         strncpy (newhead.mhtit, line, 80);
  420.         strcpy (newhead.mhbid, "");
  421.         newhead.mhsize = head->mhsize;
  422.         newhead.mhtype = 'P';
  423.         newhead.mhstat = 'N';
  424.         if ((f2 = creat_msg (&newhead, &head)) == -1) {
  425.             if (debugflag) printf ("  Error %d creating message file\n", errno);
  426.             return;
  427.             }
  428.         while ((len = readln (f1, line, 256)) > 0)
  429.             writeln (f2, line, len);
  430.         close (f1);
  431.         close (f2);
  432.         log_send (head, 1);
  433.         update_mail ();
  434.         reset_mail ();
  435.         next_mail (is_num, msgnum);
  436.         }
  437.     }
  438.         
  439. reject (msg, msgnum)
  440. char *msg;
  441. {
  442.     if (!quietflag)
  443.         printf ("%d: %s\n", msgnum, msg);
  444.     close_mail ();
  445.     exit (0);
  446. }
  447.  
  448. strToDate (s)
  449. char *s;
  450. {
  451.     char *p;
  452.     int year, month, day;
  453.     
  454.     if (sscanf (s, "%2d%2d%2d", &year, &month, &day) != 3)
  455.         return (0);
  456.     return (((1900+year) << 16) + (month << 8) + day);
  457.     }
  458.  
  459.