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 / hk21.c < prev    next >
C/C++ Source or Header  |  1990-05-26  |  19KB  |  917 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <direct.h>
  4. #include <setjmp.h>
  5. #include <signal.h>
  6. #include <modes.h>
  7. #include <errno.h>
  8. #include "mailbox.h"
  9.  
  10. /* #define REVERSE 1 */
  11.  
  12. #define MAXPATHS 6            /* maximum number of paths to mailbox */
  13. #define MAXCALLS 3500            /* maximum number of calls to be forwarded */
  14. #define TIMEOUT (5 * 60)    /* maximum # of seconds to wait for response */
  15. #define MAXTIME (20 * 60)    /* maximum # of seconds to forward */
  16. #define MAXBOX 30                /* maximum number of mailboxes */
  17.  
  18. struct userstruct user;
  19. char paths[MAXPATHS][80];
  20. char fwdcalls[MAXCALLS][7];
  21. int path_count = 0;
  22. int call_count = 0;
  23. int tnc_in, tnc_out, ctl_in, ctl_out;
  24. jmp_buf disconnected;
  25. int signalman(), quit = 0, interrupt = 0;
  26. int start_fwd, sec;
  27. char *priority, prior[] = "EP*";
  28. int bigmail;
  29. char *fwdfile[MAXBOX];
  30. int fwdsize[MAXBOX];
  31. int cmpcall();
  32. int bid_flag, mid_flag, ok_flag;
  33. extern struct dist_list *distboxes;
  34. char *maildir = "MAIL/";
  35.  
  36. main (argc, argv)
  37. char *argv[];
  38. {
  39.     int i;
  40.  
  41.     if (argc < 2) {
  42.         printf ("No port supplied.\n");
  43.         exit (0);
  44.         }
  45.     if (argc - 2 > MAXBOX) {
  46.         printf ("Too many mailboxes.\n");
  47.         exit (0);
  48.         }
  49.     chdir (HOME);
  50.     strcpy (user.uscall, "HK21");
  51.     user.usopt = ISSUPER;
  52. /*    intercept (signalman); */
  53.     start_fwd = tick();
  54.     read_dist_list ();
  55.     read_fwd_files (argc, argv);
  56.     if (argc - 2 <= 3) {
  57.         priority = "*";
  58.         bigmail = 1;
  59.         for (i = 2; i < argc; i++)
  60.             forward (argv[i], argv[1], fwdfile[i-2], fwdsize[i-2]);
  61.         }
  62.     else
  63.         for (bigmail = 0; bigmail <= 1; bigmail++)
  64.             for (priority = prior; *priority; priority++)
  65.                 for (i = 2; i < argc; i++) {
  66.                     if (*argv[i] != '*')
  67.                         if (forward (argv[i], argv[1],
  68.                                 fwdfile[i-2], fwdsize[i-2]) < 0)
  69.                             *argv[i] = '*';    /* error in forwarding, don't retry */
  70.                     if (quit) goto exit;
  71.                     }
  72. exit: i = i;    /* dummy */
  73.     }
  74.  
  75. pass_prior (head)
  76. struct msg_header *head;
  77. {
  78.     if (*priority == '*')
  79.         return 1;
  80.     if (*priority == 'P' && !bulletin (head))
  81.         return 1;
  82.     if (*priority == head->mhtype)
  83.         return 1;
  84.     return 0;
  85.     }
  86.  
  87. find_call (target)
  88. char *target;
  89. {
  90.     int i, p1, p2, comp;
  91.  
  92.     p1 = 0;  p2 = call_count-1;
  93. /* printf ("find_call (%s)\n", target); */
  94.     while (p1 <= p2) {
  95.         i = p1 + ((p2 - p1) >> 1);
  96.         comp = cmpcall (target, fwdcalls[i]);
  97. /* printf ("  %d %d %d %s %d\n", p1, i, p2, fwdcalls[i], comp); */
  98.         if (comp < 0)
  99.             p2 = i-1;
  100.         else if (comp > 0)
  101.             p1 = i+1;
  102.         else
  103.             break;
  104.         }
  105.     if (comp != 0) {
  106.         return -1;
  107.         }
  108.     while (i > 0 && cmpcall (target, fwdcalls[i-1]) == 0)
  109.         i--;
  110.     return (i);
  111.     }
  112.  
  113. test (head, mbox)
  114. struct msg_header *head;
  115. char *mbox;
  116. {
  117.     int i;
  118.     char *target;
  119.     struct dist_list *dp;
  120.  
  121.     if (!pass_prior (head)) {
  122.         return 0;
  123.         }
  124.     target = (*(head->mhbbs)) ? head->mhbbs : head->mhto;
  125.     if ((i = find_call (target)) != -1)
  126.         for ( ; i < call_count && cmpcall (target, fwdcalls[i]) == 0; i++)
  127.             if (_cmpnam (target, fwdcalls[i], strlen (fwdcalls[i])) == 0)
  128.                 goto found;
  129.     return 0;
  130. found:
  131.     for (dp = distboxes; dp; dp = dp->dlnext)
  132.         if (strcmp (head->mhbbs, dp->dlname) == 0) break;
  133.     if (dp) {
  134.         char w[10];
  135.  
  136.         strcpy (w, mbox);
  137.         upper (w);
  138.         get_ssid (w);
  139.         for (i = 0; i < dp->dlcount; i++)
  140.             if (strcmp (w, dp->dlbbs[i]) == 0) break;
  141.         if ((1 << i) & head->mhdist)
  142.             return 0;
  143.         }
  144.     if (!bigmail && head->mhsize > 1000) {
  145.         return 0;
  146.         }
  147.     return 1;
  148.     }
  149.  
  150. forward (mbox, port, file, size)
  151. char *mbox, *port, *file;
  152. int size;
  153. {
  154.     struct msg_header *head;
  155.     int i, flag;
  156.     char str[20], path[90];
  157.     
  158.     read_fwd_file (mbox, file, size);
  159.     flag = 0;
  160.     open_mail ();
  161.     while ((head = next_mail (is_nforwarded, 0)) != NULL)
  162.         if (test (head, mbox)) {
  163.             flag = 1;
  164.             break;
  165.             }
  166.     if (!flag) {
  167.         close_mail ();
  168.         return 0;
  169.         }
  170.     if (!open_channel (port)) {
  171.         close_mail ();
  172.         return -1;
  173.         }
  174. /*    write_path (ctl_out, "W32\n"); */
  175.     flag = 0;
  176.     if (setjmp (disconnected)) {
  177.         flag = 1;
  178.         goto exit;
  179.         }
  180.     bid_flag = mid_flag = ok_flag = 0;
  181.     if (connect (mbox, path)) {
  182.         reset_mail (0);
  183.         get_prompt ();
  184.         if (ok_flag) {
  185.             sprintf (str, "%s\n", SID);
  186.             write_path (tnc_out, str);
  187.             get_prompt ();
  188.             }
  189.         while ((head = next_mail (is_nforwarded, 0)) != NULL && !quit) {
  190.             if (test (head, mbox, port, path))
  191.                 if (!send_msg (head, mbox, port, path))
  192.                     flag = 1;
  193. #ifdef OMITTED                    
  194.             if ((tick() - start_fwd) / sec > MAXTIME &&
  195.                     *priority != 'E') {
  196.                 quit = 1;
  197.                 break;    /* enough forwarding for now */
  198.                 }
  199. #endif                
  200.             }
  201. #ifdef REVERSE
  202.         if (!flag) {
  203.             if (setjmp (disconnected)) {
  204.                 strcpy (user.uscall, "HK21");
  205.                 goto exit;
  206.                 }
  207.             reverse (mbox);
  208.             }
  209. #endif
  210.         disconnect ();
  211.         }
  212.     else
  213.         flag = 1;
  214. exit:
  215. /*
  216.     write_path (ctl_out, "W16\nW\n");
  217.     read_path (ctl_in, str, 20);
  218. */
  219.     close_channel (port);
  220.     close_mail (0);
  221.     return ((flag) ? -1 : 1);
  222.     }
  223.  
  224. send_msg (head, mbox, port, path)
  225. struct msg_header *head;
  226. char *mbox, *port, *path;
  227. {
  228.     char str[256], name[32], *p;
  229.     int f, len;
  230.     int time, date, tick;
  231.     short day;
  232.     struct fildes buffer;
  233.     struct dist_list *dp;
  234.  
  235. printf ("send_msg #%d\n", head->mhnr);
  236.     if ((f = open_msg (head)) < 0)
  237.         return 0;
  238.     p = str;
  239.     p += sprintf (p, "W %s", head->mhto);
  240.     p += sprintf (p, "\n");
  241.     write_path (tnc_out, str);
  242.     get_line (str);
  243.     get_line (str);
  244.     sprintf (str, "Relay from %s\n", MYCALL);
  245.     write_path (tnc_out, str);
  246.     get_line (str);
  247.     get_line (str);
  248.     print_header (head, tnc_out, 1);
  249.     ztime (&head->mhtime, &head->mhdate, &day);
  250.     sprintf (str, 
  251. "R:%02d%02d%02d/%02d%02dz @:%s.#NOCAL.CA.USA.NA Richmond OSKbox #:%d Z:94805\n",
  252.         (head->mhdate >> 16) % 100,
  253.         (head->mhdate >> 8) & 0xff,
  254.         head->mhdate & 0xff,
  255.         (head->mhtime >> 16) & 0xff,
  256.         (head->mhtime >> 8) & 0xff,
  257.         MYCALL,
  258.         head->mhnr);
  259.     write_path (tnc_out, str);
  260.     while ((len = read (f, str, 256)) > 0)
  261.         write (tnc_out, str, len);
  262.     close (f);
  263.     write_path (tnc_out, "\032\n");
  264.     log ("f %d %s %s", head->mhnr, port, path);
  265.  
  266. sent:
  267.     get_prompt ();
  268.     strcpy (str, mbox);
  269.     get_ssid (str);
  270.     if (!checkoff (head, str))
  271.         if (bulletin (head))
  272.             head->mhstat = 'F';
  273.         else
  274.             head->mhstat = 'X';
  275.     update_mail ();
  276.     return 1;
  277.     }
  278.  
  279. get_prompt ()
  280. {
  281.     char str[256];
  282.     char *f1, *f2, *f3, *f4, *p;
  283.  
  284. printf ("get_prompt\n");
  285.     while (1) {
  286.         get_line (str);
  287.         if ((f1 = strchr (str, '[')) == str && (f2 = strrchr (f1, ']')) &&
  288.                (f3 = strchr (f1, '-')) && f3 < f2 && (f4 = strrchr (f1, '-')) &&
  289.                f4 > f3) {
  290.             ok_flag = 1;
  291.             for (p = f4 + 1; p < f2; p++)
  292.                 switch (*p) {
  293.     case '$':    bid_flag++;  break;
  294.     case 'M':    mid_flag++;  break;
  295.     default:        break;
  296.                     }
  297.             }
  298.         else if ((f1 = strchr (str, '[')) == str && (f2 = strchr (f1, ']'))) {
  299.             *(f1+4) = '\0';
  300.             if (strcmp (f1+1, "MBL" == 0))
  301.                 ok_flag = bid_flag = 1;
  302.             }
  303.         if (str[strlen (str) - 1] == '>')
  304.             return;
  305.         }
  306.     }
  307.  
  308. #ifdef REVERSE
  309. reverse (mbox)
  310. {
  311.     char str[100], *p, w[40], mcall[9];
  312.     struct msg_header *head;
  313.     int date, time, tick;
  314.     short day;
  315.     int f;
  316.     int fromflag, makebid;
  317.     char type, to[7], from[7], bbs[7], bid[13];
  318.  
  319.     strcpy (mcall, mbox);
  320.     get_ssid (mcall);
  321.     strcpy (user.uscall, mcall);
  322.     while (1) {
  323.         write_path (tnc_out, "F>\n");
  324.         get_line (str);
  325.         if (toupper (*str) != 'S')
  326.             disconnect ();
  327.         fromflag = 0;
  328.         makebid = 0;
  329.         bbs[0] = '\0';
  330.         bid[0] = '\0';
  331.         p = str;
  332.         p += scanword (p, w, 40);
  333.         type = (isalpha (w[1])) ? toupper (w[1]) : ' ';
  334.         strcpy (from, mcall);
  335.         p += scanword (p, w, 40);
  336.         if (! *w)
  337.             disconnect ();    /* no addressee */
  338.         upper (w);
  339.         get_ssid (w);
  340.         strncpy (to, w, 7);
  341.         p += scanword (p, w, 40);
  342. again:
  343.         while (*w) {
  344.             if (*w == '@') {
  345.                 p += scanword (p, w, 40);
  346.                 if (*w) {
  347.                     upper (w);
  348.                     get_ssid (w);
  349.                     strncpy (bbs, w, 7);
  350.                     p += scanword (p, w, 40);
  351.                     goto again;
  352.                     }
  353.                 }
  354.             else if (*w == '<') {
  355.                 p += scanword (p, w, 40);
  356.                 if (*w) {
  357.                     upper (w);
  358.                     get_ssid (w);
  359.                     if ((user.usopt & ISBBS) || (user.usopt & ISSUPER))
  360.                         strncpy (from, w, 7);
  361.                     p += scanword (p, w, 40);
  362.                     fromflag++;
  363.                     goto again;
  364.                     }
  365.                 }
  366.             else if (*w == '$') {
  367.                 if (strlen (w) == 1) {
  368.                     p += scanword (p, w, 40);
  369.                     if (!*w)
  370.                         makebid++;
  371.                     }
  372.                 else
  373.                     strcpy (w, w+1);
  374.                 if (*w) {
  375.                     upper (w);
  376.                     strncpy (bid, w, 13);
  377.                     p += scanword (p, w, 40);
  378.                     goto again;
  379.                     }
  380.                 }
  381.             }
  382.         if (type == 'T') {
  383.             bid[0] = '\0';
  384.             makebid = 0;
  385.             }
  386.         if (bid[0] && (head = find_bid (bid))) {
  387.             write_path (tnc_out, "NO - duplicate BID\n");
  388.             checkoff (head, mbox);
  389.             continue;
  390.             }
  391.         head = new_mail ();
  392.         head->mhtype = type;
  393.         strcpy (head->mhfrom, from);
  394.         strcpy (head->mhto, to);
  395.         strcpy (head->mhbbs, bbs);
  396.         strcpy (head->mhbid, bid);
  397.         if (ok_flag)
  398.             sprintf (str, "OK %d\n", head->mhnr);
  399.         else
  400.             sprintf (str, "Enter title of message:\n");
  401.         write_path (tnc_out, str);
  402.         get_line (str);
  403.         strncpy (head->mhtit, str, 81);
  404.         strcpy (w, maildir);
  405.         header_to_name (head, w + strlen (maildir));
  406.         if ((f = create (w, S_IWRITE, S_IREAD+S_IWRITE)) == -1) {
  407.             sprintf (str, "Error %d creating message file %s.\n", errno, w);
  408.             write_path (tnc_out, str);
  409.             continue;
  410.             }
  411.         if (!ok_flag) {
  412.             sprintf (str, "Enter the message, ^Z to end.  It will be number %d\n",
  413.                 head->mhnr);
  414.             write_path (tnc_out, str);
  415.             }
  416.         if (getfile (f))
  417.             write (f, "\n", 1);
  418.         head->mhsize = _gs_size (f);
  419.         close (f);
  420.         header_to_name (head, w);
  421.         head->mhstat = 'N';
  422.         if (makebid && !head->mhbid[0])
  423.             sprintf (head->mhbid, "%d_%s", head->mhnr, MYCALL);
  424.         checkoff (head, mbox);
  425.         update_mail ();
  426.         log_send (head, fromflag);
  427.         }
  428.     }
  429.  
  430. getfile (f)
  431. {
  432.     char line[300], *p;
  433.     int len, time = 0;
  434.  
  435.     time = tick ();
  436.     while (1) {
  437.         if (_gs_rdy (ctl_in) > 0)
  438.             check_status ();
  439.         if (_gs_rdy (tnc_in) > 0) {
  440.             time = tick ();
  441.             len = readln (tnc_in, line, 256);
  442.             for (p = line; p - line < len; p++)
  443.                 if (*p == '\032') {
  444.                     write (f, line, p-line);
  445.                     goto exit;
  446.                     }
  447.             write (f, line, len);
  448.             }
  449.         else
  450.             sleep (3);
  451.         if ((tick() - time) / sec > TIMEOUT) {
  452. printf ("TIMEOUT\n");
  453.             write_path (ctl_out, "D\n");
  454.             time = tick ();
  455.             }
  456.         }
  457. exit:
  458.     return (p-line);
  459.     }
  460. #endif /* REVERSE */
  461.  
  462. get_line (str)
  463. char *str;
  464. {
  465.     int len;
  466.     int time = 0;
  467.     int status_disp, data_disp, data_send, data_ack, tries, state;
  468.  
  469.     time = tick ();
  470.     data_send = data_ack = -1;
  471.     while (1) {
  472.         if (data_send || data_ack) {
  473.             write_path (ctl_out, "L\n");
  474.             do read_path (ctl_in, str, 80); while (str[0] == '(');
  475. printf ("status %s\n", str);
  476.             scan_status (str, &status_disp, &data_disp, &data_send, 
  477.                                     &data_ack, &tries, &state);
  478.             if (state == 0 || quit)
  479.                 longjmp (disconnected, 2);
  480.             if (data_send || data_ack)
  481.                 time = tick ();
  482.             }
  483.         else if (_gs_rdy (ctl_in) > 0)
  484.             check_status ();
  485.         if (_gs_rdy (tnc_in) > 0) {
  486.             read_path (tnc_in, str, 256);
  487. printf ("get_line: '%s'\n", str);
  488.             return;
  489.             }
  490.         sleep (3);
  491.         if ((tick() - time) / sec > TIMEOUT) {
  492. printf ("TIMEOUT\n");
  493.             write_path (ctl_out, "D\n");
  494.             time = tick ();
  495.             }
  496.         }
  497.     }
  498.  
  499. disconnect ()
  500. {
  501. printf ("disconnect\n");
  502.     write_path (ctl_out, "D\n");
  503.     while (!check_disconnect ())
  504.         ;
  505.     }
  506.  
  507. check_status ()
  508. {
  509.     if (check_disconnect ())
  510.         longjmp (disconnected, 1);
  511.     }
  512.  
  513. check_disconnect ()
  514. {
  515.     char str[80];
  516.     char *p, w[20];
  517.     int len;
  518.  
  519.     read_path (ctl_in, str, 80);
  520. printf ("%s\n", str);
  521.     p = str;
  522.     p += scanword (p, w, 20);
  523.     p += scanword (p, w, 20);
  524.     if (strcmp (w, "CHANNEL") == 0) {
  525.         p += scanword (p, w, 20);
  526.         if (strcmp (w, "NOT") == 0) {
  527.             p += scanword (p, w, 20);
  528.             return (strcmp (w, "CONNECTED") == 0);
  529.             }
  530.         else
  531.             return 0;
  532.         }
  533.     else if (strcmp (w, "LINK") == 0) {
  534.         p += scanword (p, w, 20);
  535.         if (strcmp (w, "FAILURE") != 0)
  536.             return 0;
  537.         }
  538.     else if (strcmp (w, "DISCONNECTED") != 0)
  539.         return 0;
  540.     return 1;
  541.     }
  542.  
  543. connect (mbox, path)
  544. char *mbox, *path;
  545. {
  546.     int i;
  547.     char str[90];
  548.  
  549.     if (path_count == 0) {
  550.         strcpy (path, mbox);
  551.         return (try_connect (mbox) == 1);
  552.         }
  553.     else
  554.         for (i = 0; i < path_count; i++) {
  555.             sprintf (str, "%s %s", mbox, paths[i]);
  556.             switch (try_connect (str)) {
  557.         case 0:    break;
  558.         case 1:    strcpy (path, str);
  559.                 return 1;
  560.         case -1:return 0;
  561.                 }
  562.             }
  563.     return 0;
  564.     }
  565.  
  566. try_connect (fpath)
  567. char *fpath;
  568. {
  569.     char str[95], w[20], *p;
  570.     int len;
  571.     int tries = 0;
  572.  
  573. printf ("try_connect %s\n", fpath);
  574.     for (p = fpath; *p; p++)
  575.         if (*p == '*') break;
  576.     if (*p)
  577.         return (netrom (fpath));
  578. again:    sprintf (str, "C %s\n", fpath);
  579.     write_path (ctl_out, str);
  580.     read_path (ctl_in, str, 95);
  581. printf ("%s\n", str);
  582.     p = str + scanword (str, w, 20);
  583.     p += scanword (p, w, 20);
  584.     if (strcmp (w, "CONNECTED") == 0)
  585.         return 1;
  586.     else if (strcmp (w, "BUSY") == 0 || strcmp (w, "STATION") == 0) {
  587. /*
  588.         scanword (fpath, w, 20);  if (strcmp (w, "W0RLI") == 0) return -1;
  589.         if (++tries > 4) return -1;
  590.         sleep (60);
  591.         goto again;
  592. */
  593.         return -1;
  594.         }
  595.     else
  596.         return 0;
  597.     }
  598.  
  599. netrom (fpath)
  600. char *fpath;
  601. {
  602.     char w[20], digis[10][12], str[95], str2[256], *p;
  603.     int i, j, k, dcount, tries;
  604.  
  605. printf ("netrom %s\n", fpath);
  606.     p = fpath;
  607.     p += scanword (p, w, 10);
  608.     dcount = 0;
  609.     do {
  610.         p += scanword (p, digis[dcount], 12);
  611.         } while (digis[dcount++][0]);
  612.     strcpy (digis[dcount-1], w);
  613.     for (i = 0; i < dcount-1; i++)
  614.         if (digis[i][0] == '*') break;
  615.     sprintf (str, "C %s", (digis[i][0] == '*') ? digis[i] + 1 : digis[i]);
  616.     for (j = 0; j < i; j++) {
  617.         strcat (str, " ");
  618.         if (digis[j][0] == '*')
  619.             strcat (str, digis[j] + 1);
  620.         else
  621.             strcat (str, digis[j]);
  622.         }
  623.     strcat (str, "\n");
  624.     write_path (ctl_out, str);
  625.     read_path (ctl_in, str, 95);
  626. printf ("  %s\n", str);
  627.     p = str + scanword (str, w, 20);
  628.     p += scanword (p, w, 20);
  629.     if (strcmp (w, "CONNECTED") != 0)
  630.         return 0;
  631.     i++;
  632.     while (i < dcount) {
  633.         for (j = i; j < dcount-1; j++)
  634.             if (digis[j][0] == '*') break;
  635.         sprintf (str, "C %s", (digis[j][0] == '*') ? digis[j] + 1 : digis[j]);
  636.         for (k = i; k < j; k++) {
  637.             strcat (str, " ");
  638.             if (digis[k][0] == '*')
  639.                 strcat (str, digis[k] + 1);
  640.             else
  641.                 strcat (str, digis[k]);
  642.             }
  643.         strcat (str, "\n");
  644.         tries = 0;
  645. again:    write_path (tnc_out, str);
  646.         get_line (str2);
  647.         p = str2 + scanword (str2, w, 20);
  648.         p += scanword (p, w, 20);
  649.         if (strcmp (w, "Busy") == 0) {
  650. /*
  651.             if (strcmp (digis[dcount-1], "W0RLI") == 0) {disconnect(); return -1;}
  652.             if (++tries > 4) {
  653.                 disconnect ();
  654.                 return -1;
  655.                 }
  656.             sleep (60);
  657.             goto again;
  658. */
  659.             disconnect (); return -1;
  660.             }
  661.         else if (strcmp (w, "Connected") != 0) {
  662.             disconnect ();
  663.             return 0;
  664.             }
  665.         i = j + 1;
  666.         if (i == dcount - 1 && strcmp (digis[j], "*") == 0)
  667.             break;
  668.         }
  669.     if (dcount == 1)
  670.         write_path (ctl_out, "W50\n");
  671.     return 1;
  672.     }
  673.  
  674. open_channel (port)
  675. char *port;
  676. {
  677.     int hostcmd;
  678.     int i, pid;
  679.     char str[40];
  680.  
  681.     strcpy (str, "/pipe/command.");
  682.     strcat (str, port);
  683.     if ((hostcmd = open (str, 2)) < 0)
  684.         return (0);
  685.     pid = getpid ();
  686.     sprintf (str, "o%d\n", pid);
  687.     write_path (hostcmd, str);
  688.     close (hostcmd);
  689.     sprintf (str, "/pipe/data_in.%d", pid);
  690.     for (i = 0; i < 10; i++) {
  691.         if ((tnc_in = open (str, 3)) != -1) break;
  692.         sleep (1);
  693.         }
  694.     if (tnc_in == -1)
  695.         return (0);
  696.     sprintf (str, "/pipe/data_out.%d", pid);
  697.     tnc_out = open (str, 3);
  698.     sprintf (str, "/pipe/ctl_in.%d", pid);
  699.     ctl_in = open (str, 3);
  700.     sprintf (str, "/pipe/ctl_out.%d", pid);
  701.     ctl_out = open (str, 3);
  702.     return (1);
  703.     }
  704.  
  705. close_channel (port)
  706. char *port;
  707. {
  708.     int hostcmd;
  709.     int i, j, pid;
  710.     char str[40];
  711.  
  712.     strcpy (str, "/pipe/command.");
  713.     strcat (str, port);
  714.     if ((hostcmd = open (str, 2)) < 0)
  715.         return (0);
  716.     pid = getpid ();
  717.     sprintf (str, "c%d\n", pid);
  718.     write_path (hostcmd, str);
  719.     close (hostcmd);
  720.     sleep (5);
  721.     drain (tnc_in);
  722.     drain (tnc_out);
  723.     drain (ctl_in);
  724.     drain (ctl_out);
  725.     close (tnc_in);
  726.     close (tnc_out);
  727.     close (ctl_in);
  728.     close (ctl_out);
  729.     sprintf (str, "/pipe/data_in.%d", pid);
  730.     for (i = 0; i < 10; i++) {
  731.         if (access (str, 0) == -1) break;
  732.         sleep (1);
  733.         }
  734.     if (i == 10) return 0;
  735.     return (1);
  736.     }
  737.  
  738. drain (path)
  739. {
  740.     int i, j;
  741.     char c;
  742.  
  743.     while ((i = _gs_rdy (path)) > 0) {
  744.         for (j = 0; j < i; j++)
  745.             read (path, &c, 1);
  746.         sleep (1);
  747.         }
  748.     }
  749.  
  750. write_path (path, str)
  751. char *str;
  752. {
  753.     write (path, str, strlen (str));
  754. if (path == tnc_out) writeln (1, str, strlen (str));
  755.     }
  756.  
  757. read_path (path, str, len)
  758. char *str;
  759. {
  760.     int i;
  761.     i = readln (path, str, len);
  762.     if (i)
  763.         str[i-1] = '\0';
  764.     return i;
  765.     }
  766.  
  767. read_fwd_file (mbox, file, size)
  768. char *mbox, *file;
  769. {
  770.     char str[80], w[7], *p, *p2;
  771.     int i, f, len;
  772.  
  773. printf ("read_fwd_file %s\n", mbox);
  774.     path_count = call_count = 0;
  775.     strcpy (str, mbox);
  776.     get_ssid (str);
  777.     strcpy (fwdcalls[call_count++], str);
  778.     for (p2 = file; p2 < file + size; p2 += strlen (p2) + 1) {
  779.         strcpy (str, p2);
  780.         p = str + scanword (str, w, 7);
  781.         if (strcmp (w, "VIA") == 0) {
  782.             while (isspace (*p))
  783.                 p++;
  784.             if (path_count < MAXPATHS)
  785.                 strcpy (paths[path_count++], p);
  786.             }
  787.         else if (*w && *w != '@' && *w != ';') {
  788.             get_ssid (w);
  789.             if (call_count < MAXCALLS)
  790.                 strcpy (fwdcalls[call_count++], w);
  791.             }
  792.         }
  793.     qsort (fwdcalls, call_count, 7, cmpcall);
  794. /* for (i = 0; i < call_count; i++) printf ("  call %s\n", fwdcalls[i]); */
  795.     }
  796.  
  797. read_fwd_files (argc, argv)
  798. char *argv[];
  799. {
  800.     char str[80], w[7], *p;
  801.     int i, f, len;
  802.     char *mbox;
  803.  
  804.     for (i = 2; i < argc; i++) {
  805.         mbox = argv[i];
  806. printf ("processing %s\n", mbox);
  807.         upper (mbox);
  808.         strcpy (str, mbox);
  809.         for (p = str; *p; p++)
  810.             if (*p == '-')
  811.                 *p = '_';
  812.         strcat (str, ".fwd");
  813.         fwdfile[i-2] = (char *)malloc (1);
  814.         fwdsize[i-2] = read_file (str, &fwdfile[i-2], 0);
  815.         }
  816.     return (1);
  817.     }
  818.  
  819. read_file (name, pptr, size)
  820. char *name, **pptr;
  821. {
  822.     int f, fsize, tsize;
  823.     char *fptr, *p, line[80];
  824.     
  825. printf ("read_file (%s, %x(%x), %d)\n", name, pptr, *pptr, size);
  826.     if (*name == '/')
  827.         strcpy (line, name);
  828.     else
  829.         sprintf (line, "%s/%s", FWDIR, name);
  830.     if ((f = open (line, 1)) < 0)
  831.         return (size);
  832.     fsize = _gs_size (f);
  833.     fptr = (char *)malloc (size + fsize);
  834. printf ("  new ptr=%x size=%d\n", fptr, size);
  835.     if (size) movmem (*pptr, fptr, size);
  836.     read (f, fptr + size, fsize);
  837.     close (f);
  838.     for (p = fptr + size; p - fptr < size + fsize; p++) {
  839.         *p = toupper (*p);
  840.         if (*p == '\n')
  841.             *p = '\0';
  842.         }
  843.     free (*pptr);
  844.     *pptr = fptr;
  845.     tsize = size + fsize;
  846.     for (p = fptr + size; p - fptr < size + fsize; p += strlen (p) + 1) {
  847.         if (*p == '@') {
  848.             strcpy (line, p+1);
  849.             tsize = read_file (line, pptr, tsize);
  850.             p = *pptr + (p - fptr);
  851.             fptr = *pptr;
  852.             }
  853.         }
  854.     return (tsize);
  855.     }
  856.     
  857. signalman (signal)
  858. {
  859.     switch (signal) {
  860. case SIGQUIT:
  861.         quit = 1;
  862.         break;
  863. case SIGINT:
  864.         interrupt = 1;
  865.         break;
  866. default:
  867.         break;
  868.         
  869.         }
  870.     }
  871.  
  872. tick ()
  873. {
  874.     int time, date, i_tick;
  875.     short day;
  876.  
  877.     _sysdate (3, &time, &date, &day, &i_tick);
  878.     sec = i_tick >> 16;
  879.     return (time * sec + (i_tick & 0xFFFF));
  880.     }
  881.  
  882. scan_status (s, a, b, c, d, x, f)
  883. char *s;
  884. int *a, *b, *c, *d, *x, *f;
  885. {
  886.     char *p, w[20];
  887.  
  888.     p = s;
  889.     p += scanword (p, w, 20);
  890.     *a = atoi (w);
  891.     p += scanword (p, w, 20);
  892.     *b = atoi (w);
  893.     p += scanword (p, w, 20);
  894.     *c = atoi (w);
  895.     p += scanword (p, w, 20);
  896.     *d = atoi (w);
  897.     p += scanword (p, w, 20);
  898.     *x = atoi (w);
  899.     p += scanword (p, w, 20);
  900.     *f = atoi (w);
  901.     }
  902.  
  903. cmpcall (c1, c2)
  904. register char *c1, *c2;
  905. {
  906.     while (*c1 == *c2) {
  907.         if (*c1 == '\0') return 0;
  908.         c1++;
  909.         c2++;
  910.         }
  911.     if (*c1 == '?' || *c1 == '*' || *c2 == '?' || *c2 == '*')
  912.         return 0;
  913.     else
  914.         return (*c1 - *c2);
  915.     }
  916.  
  917.