home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / LO241SRV.ZIP / POLLER.C < prev    next >
C/C++ Source or Header  |  1998-05-17  |  33KB  |  1,322 lines

  1.  
  2. // LoraBBS Version 2.41 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <dir.h>
  22. #include <io.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <dos.h>
  27. #include <time.h>
  28. #include <sys/stat.h>
  29.  
  30. #include <cxl\cxlvid.h>
  31. #include <cxl\cxlwin.h>
  32. #include <cxl\cxlstr.h>
  33.  
  34. #include "lsetup.h"
  35. #include "sched.h"
  36. #include "msgapi.h"
  37. #include "externs.h"
  38. #include "prototyp.h"
  39. #include "zmodem.h"
  40.  
  41. #define IEMSI_ON        0x0001
  42. #define IEMSI_MAILCHECK 0x0002
  43. #define IEMSI_FILECHECK 0x0004
  44. #define IEMSI_NEWS      0x0008
  45.  
  46. extern long timeout;
  47. extern int to_row, iemsi;
  48. extern word serial_no;
  49. extern char *VNUM, serial_id[3];
  50.  
  51. static void galileo_input (char *, int);
  52.  
  53. void m_print2 (char *, ...);
  54. void set_protocol_flags (int zone, int net, int node, int point);
  55. int esc_pressed (void);
  56. int do_script (char *phone_number);
  57. void emsi_handshake (int);
  58. void get_emsi_id (char *, int);
  59. int time_to_next_nonmail (void);
  60. void iemsi_handshake (void);
  61. int get_emsi_field (char *);
  62. int something_wrong=0;
  63. int sw_net=0,sw_node=0;
  64.  
  65. int poll(max_tries, bad, zone, net, node)
  66. int max_tries, bad, zone, net, node;
  67. {
  68.     int fd, tries, i, j, wh, m;
  69.     char buffer[70], jan, waz, ems,under_baud;
  70.     long t1, t2, tu, il;
  71.     NODEINFO ni;
  72.     sw_net=net;
  73.     sw_node=node;
  74.     something_wrong=0;
  75.  
  76.     if (CARRIER) {
  77.         dial_system ();
  78.  
  79.         for (i = 0; i < max_call; i++) {
  80.             if (zone == call_list[i].zone && net == call_list[i].net && node == call_list[i].node && !call_list[i].point)
  81.                 break;
  82.         }
  83.  
  84.         if (i >= max_call) {
  85.           call_list[i].zone = zone;
  86.           call_list[i].net = net;
  87.           call_list[i].node = node;
  88.           call_list[i].type |= MAIL_WILLGO;
  89.           max_call++;
  90.         }
  91.  
  92.         next_call = i;
  93.         goto online_out;
  94.     }
  95.  
  96.     if (!get_bbs_record(zone, net, node, 0))
  97.         return (0);
  98.  
  99.     if (bad && bad_call(net,node,0,0))
  100.         return (0);
  101.  
  102.     jan = config->janus;
  103.     waz = config->wazoo;
  104.     ems = config->emsi;
  105.     set_protocol_flags (zone, net, node, 0);
  106.  
  107.     function_active = 0;
  108.     dial_system ();
  109.     local_status ("Dialing");
  110.     sysinfo.today.outcalls++;
  111.     sysinfo.week.outcalls++;
  112.     sysinfo.month.outcalls++;
  113.     sysinfo.year.outcalls++;
  114.  
  115. //   for (i=0; i < MAX_ALIAS; i++)
  116. //      if (zone && config->alias[i].zone == zone)
  117. //         break;
  118. //   if (i == MAX_ALIAS)
  119. //      assumed = 0;
  120. //   else
  121. //      assumed = i;
  122.  
  123. //   for (i = 0; i < MAX_ALIAS; i++)
  124. //      if ( config->alias[i].point && config->alias[i].net == net && config->alias[i].node == node && (!remote_zone || config->alias[i].zone == zone) )
  125. //         break;
  126.  
  127. //   if (i < MAX_ALIAS)
  128. //      assumed = i;
  129.  
  130.     assumed = 0;
  131.  
  132.     sprintf (buffer, "%sNODES.DAT", config->net_info);
  133.     if ((fd = sh_open (buffer, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  134.         return (0);
  135.  
  136.     while (read (fd, (char *)&ni, sizeof (NODEINFO)) == sizeof (NODEINFO))
  137.         if (zone == ni.zone && net == ni.net && node == ni.node && ni.point == 0) {
  138.             assumed = ni.mailer_aka;
  139.             break;
  140.         }
  141.  
  142.     close (fd);
  143.  
  144.     for (tries = 0; tries < max_tries; tries++) {
  145.         if (tries) {
  146.             modem_hangup ();
  147.  
  148.             tu = timerset (300);
  149.             while (!timeup (tu)) {
  150.                 if (local_kbd == 0x1B) {
  151.                     local_kbd = -1;
  152.                     i = 0;
  153.                     modem_hangup ();
  154.                     break;
  155.                 }
  156.             }
  157.         }
  158.  
  159.         status_line(msgtxt[M_PROCESSING_NODE],zone,net,node,nodelist.name);
  160.         status_line(msgtxt[M_DIALING_NUMBER],nodelist.phone);
  161.  
  162.         wh = wopen (13, 9, 21, 45, 3, RED|_LGREY, BLUE|_LGREY);
  163.         wactiv (wh);
  164.         wtitle (" Dialing ", TCENTER, RED|_LGREY);
  165.  
  166.         sprintf (buffer, "%d:%d/%d", zone, net, node);
  167.         wcenters (1, BLUE|_LGREY, buffer);
  168.         wcenters (2, BLUE|_LGREY, nodelist.name);
  169.         wcenters (3, BLUE|_LGREY, nodelist.phone);
  170.  
  171.         CLEAR_INBOUND();
  172.         CLEAR_OUTBOUND();
  173.  
  174.         i = nodelist.rate * 300;
  175.         if ((long)i > speed)
  176.             i = (word)speed;
  177.         il=config->speed;
  178. //        if (!config->lock_baud)
  179.             com_baud (il);
  180.         rate = il;
  181.         answer_flag = 1;
  182.  
  183.         sprintf (buffer, "%d Baud", i);
  184.         wcenters (4, BLUE|_LGREY, buffer);
  185.  
  186.         if(ni.min_baud_rate){
  187.             sprintf (buffer, "Min. baud: %ld", ni.min_baud_rate);
  188.             wcenters (4, BLUE|_LGREY, buffer);
  189.         }
  190.  
  191.         if (nodelist.phone[0] == '\"') {
  192.             prints (7, 65, YELLOW|_BLACK, "ScriptExec");
  193.             i = do_script (nodelist.phone);
  194.         }
  195.         else {
  196.             prints (7, 65, YELLOW|_BLACK, "DialPhone");
  197.             dial_number (nodelist.modem, nodelist.phone);
  198.             prints (7, 65, YELLOW|_BLACK, "Waiting  ");
  199.             i = wait_for_connect(0);
  200.         }
  201.  
  202.         wclose ();
  203.  
  204.         if (i == ABORTED)
  205.             status_line ("!Aborted by operator");
  206.         else if (i == TIMEDOUT)
  207.             status_line ("!Modem timeout");
  208.             under_baud=0;
  209.         if (i > 0 && rate < ni.min_baud_rate){
  210.             i = ABORTED;
  211.             under_baud=1;
  212.             status_line ("!Baud rate under minimum allowed - Hangup!");
  213.             status_line ("!Connect: %ld Minimum: %ld",rate,ni.min_baud_rate);
  214.         }
  215.  
  216.         if (i > 0)
  217.             break;
  218.         else {
  219.             prints (7, 65, YELLOW|_BLACK, "Hangup    ");
  220.             modem_hangup ();
  221.             prints (7, 65, YELLOW|_BLACK, "Modem init");
  222.             initialize_modem ();
  223.             if (local_kbd == 0x1B || local_kbd == ' ') {
  224.                 local_kbd = -1;
  225.                 break;
  226.             }
  227.         }
  228.     }
  229.  
  230.     if (i <= 0) {
  231.         if (bad) {
  232.             if(under_baud)
  233.                 bad_call (net, node, 1, i);
  234.             else
  235.                 bad_call (net, node, 2, i);
  236.         }
  237.  
  238.         rate = config->speed;
  239.         if (!config->lock_baud)
  240.             com_baud (rate);
  241.         answer_flag = 0;
  242.  
  243.         config->janus = jan;
  244.         config->wazoo = waz;
  245.         config->emsi = ems;
  246.  
  247.         idle_system ();
  248.         timeout = 0L;
  249.         function_active = 99;
  250.         return (i);
  251.     }
  252.  
  253. online_out:
  254. //   set_prior (4);
  255.     local_status ("Online    ");
  256.  
  257.     if (mdm_flags == NULL) {
  258.         status_line(msgtxt[M_READY_CONNECT], rate, "", "");
  259.         mdm_flags = "";
  260.     }
  261.     else
  262.         status_line(msgtxt[M_READY_CONNECT], rate, "/", mdm_flags);
  263.  
  264.     if (!lock_baud)
  265.         com_baud(rate);
  266.  
  267.     remote_zone = called_zone = zone;
  268.     remote_node = called_node = node;
  269.     remote_net = called_net = net;
  270.     remote_point = 0;
  271.     remote_capabilities = 0;
  272.     local_mode = snooping = 0;
  273.  
  274.     filepath = config->norm_filepath;
  275.     request_list = config->norm_okfile;
  276.     max_requests = config->norm_max_requests;
  277.     max_kbytes = config->norm_max_kbytes;
  278.  
  279.     prints (7, 65, YELLOW|_BLACK, "Sync.  ");
  280.  
  281.     t1 = timerset(3000);
  282.     timeout = t1;
  283.     to_row = 8;
  284.     j = 'j';
  285.  
  286.     while(!timeup(t1) && CARRIER) {
  287.  
  288.         SENDBYTE (32);
  289.         SENDBYTE (13);
  290.         SENDBYTE (32);
  291.         SENDBYTE (13);
  292.  
  293.         while (CARRIER && !OUT_EMPTY ())
  294.             time_release ();
  295.  
  296.         t2 = timerset(300);
  297.         while (CARRIER && !timeup (t2) && !timeup (t1)) {
  298.             if (esc_pressed ())
  299.                 goto end_mail;
  300.  
  301.             i = TIMED_READ(1);
  302.  
  303.             if(i == YOOHOO)
  304.                 continue;
  305.  
  306.             time_release ();
  307.  
  308.             switch(i) {
  309.             case '*':
  310.                 if (!config->emsi)
  311.                     break;
  312.                 get_emsi_id (buffer, 14);
  313. //                if (!strnicmp (buffer, "**EMSI_REQA77E", 14)) {
  314.                 if (strstr (buffer, "EMSI_REQA77E")) {
  315.                     m_print2 ("**EMSI_INQC816\r");
  316.                     emsi_handshake (1);
  317.                 }
  318.                 break;
  319.             case ENQ:
  320.                 if (!config->wazoo)
  321.                     break;
  322.  
  323.                 if (send_YOOHOO(1)) {
  324.                     WaZOO(1);
  325.                     get_call_list();
  326.                     goto  end_mail;
  327.                 }
  328.                 break;
  329.             case 0x00:
  330.             case 0x01:
  331.             case 'C':
  332.                 if(j == 'C') {
  333.                     TIMED_READ(1);
  334.                     FTSC_sender(0);
  335.                     goto end_mail;
  336.                 }
  337.                 break;
  338.  
  339.             case 0xFE:
  340.             case -2:
  341.                 if(j == 0x01) {
  342.                     FTSC_sender(0);
  343.                     goto end_mail;
  344.                 }
  345.                 break;
  346.  
  347.             case 0xFF:
  348.             case -1:
  349.                 if(j == 0x00) {
  350.                     FTSC_sender(0);
  351.                     goto end_mail;
  352.                 }
  353.                 break;
  354.  
  355.             case NAK:
  356.                 if(j == NAK) {
  357.                     FTSC_sender(0);
  358.                     goto end_mail;
  359.                 }
  360.                 break;
  361.             }
  362.  
  363.             if((i != -1) && (i != 0xFF))
  364.                 j = i;
  365.         }
  366.  
  367.         if (config->emsi)
  368.             m_print2 ("**EMSI_INQC816\r**EMSI_INQC816\r");
  369.  
  370.         if (config->wazoo)
  371.             SENDBYTE (YOOHOO);
  372.         SENDBYTE (TSYNC);
  373.     }
  374.  
  375. bad_mail:
  376. //   set_prior (2);
  377.  
  378.     config->janus = jan;
  379.     config->wazoo = waz;
  380.     config->emsi = ems;
  381.  
  382.     status_line(msgtxt[M_NOBODY_HOME]);
  383.  
  384.     if (bad)
  385.         bad_call (net, node, 1, TIMEDOUT);
  386.  
  387.     modem_hangup ();
  388.     sysinfo.today.failed++;
  389.     sysinfo.week.failed++;
  390.     sysinfo.month.failed++;
  391.     sysinfo.year.failed++;
  392.  
  393.     idle_system ();
  394.     return (TIMEDOUT);
  395.  
  396. end_mail:
  397. //   set_prior (2);
  398.  
  399.     config->janus = jan;
  400.     config->wazoo = waz;
  401.     config->emsi = ems;
  402.  
  403.     for (i = 0; i < max_call; i++) {
  404.         if (zone == call_list[i].zone && net == call_list[i].net && node == call_list[i].node && !call_list[i].point)
  405.             break;
  406.     }
  407.     for (m = i + 1; m < max_call; m++, i++)
  408.         memcpy (&call_list[i], &call_list[m], sizeof (struct _call_list));
  409.     max_call--;
  410.  
  411.     idle_system ();
  412.     return(1);
  413. }
  414.  
  415. int bad_call(bnet,bnode,rwd,flags)
  416. int bnet, bnode, rwd, flags;
  417. {
  418.           int res, i, j, mc, mnc;
  419.     struct ffblk bad_dta;
  420.     FILE *bad_wazoo;
  421.           char *p, *HoldName, fname[50], fname1[50];
  422.  
  423.     HoldName = hold_area;
  424.     sprintf (fname, "%s%04x%04x.$$?", HoldName, bnet, bnode);
  425.     j = strlen (fname) - 1;
  426.     res = -1;
  427.  
  428.         if (cur_event > -1) {
  429.                 mc = e_ptrs[cur_event]->with_connect ? e_ptrs[cur_event]->with_connect : max_connects;
  430.                 mnc = e_ptrs[cur_event]->no_connect ? e_ptrs[cur_event]->no_connect : max_noconnects;
  431.         }
  432.  
  433.         i = 0;
  434.     if (!findfirst(fname,&bad_dta,0))
  435.         do {
  436.             if (isdigit (bad_dta.ff_name[11])) {
  437.                 fname[j] = bad_dta.ff_name[11];
  438.                 res = fname[j] - '0';
  439.                 break;
  440.             }
  441.         } while (!findnext(&bad_dta));
  442.  
  443.     if (res == -1)
  444.         fname[j] = '0';
  445.  
  446.     if (rwd > 0) {
  447.         strcpy (fname1, fname);
  448.         fname1[j]++;
  449.         if (fname1[j] > '9')
  450.             fname1[j] = '9';
  451.  
  452.         if (res == -1) {
  453.             if (rwd == 2)
  454.                                 res = cshopen (fname, O_CREAT + O_WRONLY + O_BINARY, S_IWRITE);
  455.             else
  456.                                 res = cshopen (fname1, O_CREAT + O_WRONLY + O_BINARY, S_IWRITE);
  457.             i = rwd - 1;
  458.                         write (res, (char *) &i, sizeof (short));
  459.                         write (res, (char *) &flags, sizeof (short));
  460.                         close (res);
  461.         }
  462.         else {
  463.             if (rwd == 2) {
  464.                                 i = shopen (fname, O_RDONLY + O_BINARY);
  465.                                 res = 0;
  466.                                 read (i, (char *) &res, sizeof (short));
  467.                 close (i);
  468.  
  469.                 ++res;
  470.  
  471.                                 i = cshopen (fname, O_CREAT + O_WRONLY + O_BINARY, S_IWRITE);
  472.                                 write (i, (char *) &res, sizeof (short));
  473.                                 write (i, (char *) &flags, sizeof (short));
  474.                                 close (i);
  475.             }
  476.             else {
  477.                 rename (fname, fname1);
  478.             }
  479.         }
  480.     }
  481.     else if (rwd == 0) {
  482.         if (res == -1)
  483.             return (0);
  484.                 if (res >= mc)
  485.             return (1);
  486.         res = 0;
  487.                 i = shopen (fname, O_RDONLY + O_BINARY);
  488.                 res = 0;
  489.                 read (i, (char *) &res, sizeof (short));
  490.         close (i);
  491.                 return (res >= mnc);
  492.     }
  493.     else {
  494.         if (res != -1) {
  495.             unlink (fname);
  496.         }
  497.  
  498.         sprintf (fname, "%s%04x%04x.Z", HoldName, bnet, bnode);
  499.         if (dexists (fname)) {
  500.             bad_wazoo = fopen (fname, "ra");
  501.             while (!feof (bad_wazoo)) {
  502.                 e_input[0] = '\0';
  503.                 if (!fgets (e_input, 64, bad_wazoo))
  504.                     break;
  505.                 p = strchr (e_input, ' ') + 1;
  506.                 p = strchr (p, ' ');
  507.                 *p = '\0';
  508.                 p = strchr (e_input, ' ') + 1;
  509.  
  510.                 strcpy (fname1, filepath);
  511.                 strcat (fname1, p);
  512.                 unlink (fname1);
  513.             }
  514.             fclose (bad_wazoo);
  515.             unlink (fname);
  516.         }
  517.     }
  518.  
  519.     return (0);
  520. }
  521.  
  522. int mail_session()
  523. {
  524.    int i, flag, oldsnoop;
  525.    char buffer[30], *pwpos;
  526.    long t1, t2;
  527.     char *emsi_req = "**EMSI_REQA77E\r", *iemsi_req = "**EMSI_IRQ8E08\r";
  528.  
  529.     if (cur_event >= 0 && (e_ptrs[cur_event]->behavior & MAT_NOMAIL24))
  530.       return (0);
  531.  
  532.    if (!config->mail_only[0])
  533.       strcpy (config->mail_only, msgtxt[M_NO_BBS]);
  534.    if (!config->enterbbs[0])
  535.       strcpy (config->enterbbs, msgtxt[M_PRESS_ESCAPE]);
  536.  
  537.    filepath = config->norm_filepath;
  538.    request_list = config->norm_okfile;
  539.    max_requests = config->norm_max_requests;
  540.    max_kbytes = config->norm_max_kbytes;
  541.  
  542.    pwpos = NULL;
  543.    iemsi = 0;
  544.    flag = 0;
  545.    assumed = 0;
  546.    got_arcmail = 0;
  547.    caller = 0;
  548.    oldsnoop = snooping;
  549.    snooping = 0;
  550.    remote_capabilities = 0;
  551.    find_event ();
  552.  
  553.    t1 = timerset(3000);
  554.    t2 = timerset(400);
  555.  
  556.    dial_system ();
  557.    function_active = 0;
  558.    timeout = t1;
  559.    to_row = 8;
  560.     local_status ("Connected");
  561.    prints (7, 65, YELLOW|_BLACK, "Sync.");
  562.  
  563.    if (config->override_pwd[0])
  564.       pwpos = config->override_pwd;
  565.    if (config->emsi)
  566.       m_print2 (emsi_req);
  567.  
  568.    while(!timeup(t1) && CARRIER) {
  569.       if (timeup(t2) && !flag) {
  570.          if (config->emsi)
  571.             m_print2 (emsi_req);
  572.          m_print2(msgtxt[M_ADDRESS],config->alias[0].zone,config->alias[0].net,config->alias[0].node,config->alias[0].point,VERSION);
  573.          if (config->banner[0] == '@')
  574.             read_file (&config->banner[1]);
  575.          else
  576.             m_print2("%s\n",config->banner);
  577.          if (cur_event >= 0 && !(e_ptrs[cur_event]->behavior & MAT_BBS)) {
  578.             if (config->mail_only[0] == '@')
  579.                read_file (&config->mail_only[1]);
  580.             else {
  581.                m_print2 ("\r");
  582.                m_print2 (config->mail_only, time_to_next_nonmail ());
  583.             }
  584.          }
  585.          else {
  586.             if (config->use_iemsi)
  587.                m_print2 (iemsi_req);
  588.             if (config->enterbbs[0] == '@')
  589.                read_file (&config->enterbbs[1]);
  590.             else
  591.                m_print2("\r%s",config->enterbbs);
  592.          }
  593.  
  594.          flag=1;
  595.       }
  596.  
  597.       i = TIMED_READ(1);
  598.  
  599.       switch (i) {
  600.          case -1:
  601.             break;
  602.  
  603.          case '*':
  604.             if (!config->emsi)
  605.                break;
  606.                 get_emsi_id (buffer, 14);
  607. //            status_line (">DEBUG: get_emsi_id = '%s'", buffer);
  608.  
  609.             if (!strnicmp (buffer, "**EMSI_INQC816", 14))
  610.                emsi_handshake (0);
  611.             else if (config->use_iemsi) {
  612.                if (!strnicmp (buffer, "**EMSI_ICI", 10)) {
  613.                   iemsi_handshake ();
  614.                   if (iemsi) {
  615.                      m_print2 ("\n\n");
  616.                      snooping = oldsnoop;
  617.                      timer (5);
  618.  
  619.                      return(0);
  620.                   }
  621.                }
  622.                else if (!strnicmp (buffer, "**EMSI_CLIFA8C", 14))
  623.                   m_print2 (iemsi_req);
  624.             }
  625.             break;
  626.  
  627.          case ' ':
  628.          case 0x0D:
  629.             if (!flag)
  630.                break;
  631.             if (cur_event >= 0 && !(e_ptrs[cur_event]->behavior & MAT_BBS)) {
  632.                if (config->mail_only[0] == '@')
  633.                   read_file (&config->mail_only[1]);
  634.                else {
  635.                   m_print2 ("\r");
  636.                   m_print2 (config->mail_only, time_to_next_nonmail ());
  637.                }
  638.             }
  639.             else {
  640.                if (config->use_iemsi)
  641.                   m_print2 (iemsi_req);
  642.                if (config->enterbbs[0] == '@')
  643.                   read_file (&config->enterbbs[1]);
  644.                else
  645.                   m_print2("\r%s",config->enterbbs);
  646.             }
  647.             break;
  648.  
  649.          case YOOHOO:
  650.             if (!timeup(t2) || !config->wazoo)
  651.                break;
  652.             remote_capabilities = 0;
  653.             if (get_YOOHOO(1))
  654.                WaZOO(0);
  655.             return(1);
  656.  
  657.          case TSYNC:
  658.             if (!timeup(t2))
  659.                 break;
  660.             FTSC_receiver (0);
  661.             return(1);
  662.  
  663.          case 0x1B:
  664.             if (!flag) {
  665.                if (config->emsi)
  666.                   m_print2 (emsi_req);
  667.                m_print2(msgtxt[M_ADDRESS],config->alias[0].zone,config->alias[0].net,config->alias[0].node,config->alias[0].point,VERSION);
  668.                if (config->banner[0] == '@')
  669.                   read_file (&config->banner[1]);
  670.                else
  671.                   m_print2("%s\n",config->banner);
  672.                if (cur_event >= 0 && !(e_ptrs[cur_event]->behavior & MAT_BBS)) {
  673.                   if (config->mail_only[0] == '@')
  674.                      read_file (&config->mail_only[1]);
  675.                   else {
  676.                      m_print2 ("\r");
  677.                      m_print2 (config->mail_only, time_to_next_nonmail ());
  678.                  }
  679.                }
  680.                else {
  681.                   if (config->use_iemsi)
  682.                      m_print2 (iemsi_req);
  683.                   if (config->enterbbs[0] == '@')
  684.                      read_file (&config->enterbbs[1]);
  685.                   else
  686.                      m_print2("\r%s",config->enterbbs);
  687.                }
  688.  
  689.                flag=1;
  690.             }
  691.             else {
  692.                if (cur_event >= 0 && !(e_ptrs[cur_event]->behavior & MAT_BBS))
  693.                   break;
  694.  
  695.                m_print2 ("\n\n");
  696.                snooping = oldsnoop;
  697.                timer (5);
  698.  
  699.                return (0);
  700.             }
  701.             break;
  702.  
  703.          default:
  704.             if (cur_event >= 0 && !(e_ptrs[cur_event]->behavior & MAT_BBS)) {
  705.                if (pwpos != NULL) {
  706.                   i &= 0xFF;
  707.  
  708.                   if (i == *pwpos) {
  709.                      pwpos++;
  710.                      if (*pwpos == '\0') {
  711.                         m_print2 ("\n\n");
  712.                         snooping = oldsnoop;
  713.                         timer (5);
  714.  
  715.                         return (0);
  716.                      }
  717.                   }
  718.                   else
  719.                      pwpos = config->override_pwd;
  720.                }
  721.             }
  722.             break;
  723.       }
  724.  
  725.       time_release ();
  726.    }
  727.  
  728.    if (!CARRIER)
  729.       return (1);
  730.  
  731.    if (cur_event >= 0 && !(e_ptrs[cur_event]->behavior & MAT_BBS))
  732.       return (1);
  733.  
  734.    m_print2 ("\n\n");
  735.    snooping = oldsnoop;
  736.    timer (5);
  737.  
  738.    return (0);
  739. }
  740.  
  741. int iemsi_session (void)
  742. {
  743.    int i, oldsnoop;
  744.    char buffer[30];
  745.    long t1, t2;
  746.    char *iemsi_req = "**EMSI_IRQ8E08\r";
  747.  
  748.    iemsi = 0;
  749.    assumed = 0;
  750.    got_arcmail = 0;
  751.    caller = 0;
  752.    oldsnoop = snooping;
  753.    snooping = 0;
  754.    remote_capabilities = 0;
  755.     find_event ();
  756.  
  757.    t2 = timerset (200);
  758.    t1 = timerset (400);
  759.  
  760.    dial_system ();
  761.    function_active = 0;
  762.    timeout = t1;
  763.    to_row = 8;
  764.     local_status ("Connected");
  765.    prints (7, 65, YELLOW|_BLACK, "Sync.");
  766.  
  767.    while (!timeup (t1) && CARRIER) {
  768.       if (timeup (t2))
  769.          m_print2 (iemsi_req);
  770.  
  771.       i = TIMED_READ(1);
  772.  
  773.       switch (i) {
  774.          case -1:
  775.             break;
  776.          case '*':
  777.                 get_emsi_id (buffer, 12);
  778.  
  779.             if (!strnicmp (buffer, "**EMSI_ICI", 10)) {
  780.                iemsi_handshake ();
  781.                if (iemsi) {
  782.                   m_print2 ("\n\n");
  783.                   snooping = oldsnoop;
  784.                   return (0);
  785.                }
  786.             }
  787.             break;
  788.  
  789.          case 0x0D:
  790.             case 0x1B:
  791.             m_print2 ("\n\n");
  792.             snooping = oldsnoop;
  793.             return(0);
  794.       }
  795.  
  796.       time_release ();
  797.    }
  798.  
  799.    if (!CARRIER)
  800.       return (1);
  801.  
  802.    m_print2 ("\n\n");
  803.    snooping = oldsnoop;
  804.    return (0);
  805. }
  806.  
  807. static void galileo_input (s, width)
  808. char *s;
  809. int width;
  810. {
  811.    char c, buffer[80], asc1[40];
  812.    int i = 0;
  813.    long t, lt;
  814.    struct tm *tim;
  815.  
  816.    t = timerset (300);
  817.    UNBUFFER_BYTES ();
  818.  
  819.    while (CARRIER && !timeup (t)) {
  820.       while (PEEKBYTE() == -1) {
  821.          if (!CARRIER || timeup (t))
  822.             return;
  823.          lt = time (NULL);
  824.          tim = localtime (<);
  825.             strcpy (asc1, asctime (tim));
  826.          asc1[strlen (asc1) - 1] = '\0';
  827.          sprintf (buffer, "%s   (%d)   ", asc1, timezone / 3600L);
  828.          tim = gmtime (<);
  829.          strcpy (asc1, asctime (tim));
  830.          asc1[strlen (asc1) - 1] = '\0';
  831.          strcat (buffer, asc1);
  832.          wcenters (5, YELLOW|_BLACK, buffer);
  833.          time_release ();
  834.       }
  835.  
  836.       c = (char)TIMED_READ(1);
  837.       t = timerset (300);
  838.  
  839.       if (c == 0x0D)
  840.          break;
  841.  
  842.       if (i >= width)
  843.          continue;
  844.  
  845.       if (c < 0x20)
  846.          continue;
  847.  
  848.       s[i++]=c;
  849.    }
  850.  
  851.    s[i]='\0';
  852. }
  853.  
  854. #define isLeap(x) ((x)%1000)?((((x)%100)?(((x)%4)?0:1):(((x)%400)?0:1))):(((x)%4000)?1:0)
  855.  
  856. int poll_galileo (max_connect, max_tries)
  857. int max_connect, max_tries;
  858. {
  859.    int tries, i, wh, yy, mm, dy, hh, ss, mi;
  860.     char buffer[85], stringa[85];
  861.    long t1, tu;
  862.    struct date dd;
  863.    struct time dt;
  864.    int mdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  865.  
  866.    function_active = 0;
  867.    dial_system ();
  868.    local_kbd = -1;
  869.     local_status ("ClockSync");
  870.  
  871.    init = config->galileo_init;
  872.    mdm_sendcmd (config->galileo_init);
  873.  
  874.    for(tries=0;tries < max_tries;tries++) {
  875.       if (tries) {
  876.          prints (7, 65, YELLOW|_BLACK, "Pausing  ");
  877.  
  878.          tu = timerset (300);
  879.          while (!timeup (tu)) {
  880.             if (local_kbd == 0x1B) {
  881.                local_kbd = -1;
  882.                i = 0;
  883.                modem_hangup ();
  884.                break;
  885.             }
  886.             time_release ();
  887.          }
  888.       }
  889.  
  890.       sysinfo.today.outcalls++;
  891.       sysinfo.week.outcalls++;
  892.       sysinfo.month.outcalls++;
  893.       sysinfo.year.outcalls++;
  894.  
  895.         status_line(msgtxt[M_DIALING_NUMBER],config->galileo);
  896.  
  897.       wh = wopen (13, 9, 20, 45, 3, RED|_LGREY, BLUE|_LGREY);
  898.       wactiv (wh);
  899.       wtitle (" Dialing ", TCENTER, RED|_LGREY);
  900.  
  901.       wcenters (1, BLUE|_LGREY, "- Atomic Clock Adjustment -");
  902.       wcenters (2, BLUE|_LGREY, "Ist. Galileo Ferraris");
  903.       wcenters (3, BLUE|_LGREY, config->galileo);
  904.  
  905.       CLEAR_INBOUND();
  906.       CLEAR_OUTBOUND();
  907.  
  908.       rate = 1200;
  909.       if (!config->lock_baud)
  910.          com_baud (rate);
  911.       answer_flag = 1;
  912.  
  913.       sprintf (buffer, "%u Baud", rate);
  914.       wcenters (4, BLUE|_LGREY, buffer);
  915.  
  916.       prints (7, 65, YELLOW|_BLACK, "DialPhone");
  917.       mdm_sendcmd (config->galileo_dial);
  918.       mdm_sendcmd (config->galileo);
  919.       mdm_sendcmd (config->galileo_suffix[0] == '\0' ? config->galileo_suffix : "\r");
  920.       prints (7, 65, YELLOW|_BLACK, "Waiting  ");
  921.       i = wait_for_connect (0);
  922.  
  923.       wclose ();
  924.  
  925.       if (i > 0)
  926.          break;
  927.       else {
  928.          prints (7, 65, YELLOW|_BLACK, "Hangup ");
  929.          modem_hangup ();
  930.             if (i == ABORTED) {
  931.             local_kbd = -1;
  932.             break;
  933.          }
  934.       }
  935.    }
  936.  
  937.    init = config->init;
  938.  
  939.    if (i <= 0) {
  940.       answer_flag = 0;
  941.  
  942.       idle_system ();
  943.       timeout = 0L;
  944.       function_active = 99;
  945.       return (i);
  946.    }
  947.  
  948. online_out:
  949.     local_status ("Online");
  950.  
  951.    if (mdm_flags == NULL) {
  952.         status_line(msgtxt[M_READY_CONNECT], rate, "", "");
  953.       mdm_flags = "";
  954.    }
  955.    else
  956.         status_line(msgtxt[M_READY_CONNECT], rate, "/", mdm_flags);
  957.  
  958.    if (!lock_baud)
  959.       com_baud(rate);
  960.  
  961.    wh = wopen (12, 0, 24, 79, 0, LGREY|_BLACK, WHITE|_BLACK);
  962.    wactiv (wh);
  963.    wtitle ("REMOTE CLOCK ADJUSTMENT", TLEFT, LCYAN|_BLACK);
  964.    printc (12, 0, LGREY|_BLACK, '├');
  965.     printc (12, 52, LGREY|_BLACK, '┴');
  966.    printc (12, 79, LGREY|_BLACK, '┤');
  967.  
  968.    wcenters (0, LGREY|_BLACK, "Ist. Galileo Ferraris");
  969.    sprintf (buffer, "Connected at %u baud", rate);
  970.    wcenters (1, LGREY|_BLACK, buffer);
  971.  
  972.    t1 = timerset(1500);
  973.    timeout = t1;
  974.  
  975.    while (!timeup(t1) && CARRIER) {
  976.       galileo_input (buffer, 80);
  977.       if (!CARRIER)
  978.          break;
  979.       galileo_input (stringa, 80);
  980.       if (!CARRIER)
  981.          break;
  982.  
  983.       if (!strncmp (buffer, stringa, 16)) {
  984.          sscanf (&stringa[17], "%2d", &ss);
  985.          sscanf (&stringa[37], "%4d%2d%2d%2d%2d", &yy, &mm, &dy, &hh, &mi);
  986.  
  987.          hh -= (int)(timezone / 3600L);
  988.          if (!config->solar && !strncmp (&stringa[20], "CEST", 4))
  989.             hh--;
  990.  
  991.          if (isLeap (yy))
  992.             mdays[2]++;
  993.  
  994.          if (hh >= 24) {
  995.             hh %= 24;
  996.             if (++dy > mdays[mm]) {
  997.                dy = 1;
  998.                if (++mm > 12) {
  999.                   mm = 1;
  1000.                         yy++;
  1001.                }
  1002.             }
  1003.          }
  1004.          else if (hh < 0) {
  1005.             hh += 24;
  1006.             if (--dy < 0) {
  1007.                if (--mm < 0) {
  1008.                   mm = 12;
  1009.                   yy--;
  1010.                   dy = 31;
  1011.                }
  1012.                else
  1013.                   dy = mdays[mm];
  1014.             }
  1015.          }
  1016.  
  1017.          dt.ti_hour = hh;
  1018.          dt.ti_min = mi;
  1019.          dt.ti_sec = ss;
  1020.          dt.ti_hund = 0;
  1021.          dd.da_year = yy;
  1022.          dd.da_mon = mm;
  1023.          dd.da_day = dy;
  1024.  
  1025.          setdate (&dd);
  1026.          settime (&dt);
  1027.  
  1028.             status_line ("+Remote clock: %02d-%02d-%02d %02d:%02d:%02d", dd.da_day, dd.da_mon, dd.da_year % 100, dt.ti_hour, dt.ti_min, dt.ti_sec);
  1029.  
  1030.          wclose ();
  1031.          modem_hangup ();
  1032.             status_line ("+Resync succesful");
  1033.  
  1034.          answer_flag = 0;
  1035.             rate = config->speed;
  1036.          if (!lock_baud)
  1037.             com_baud(rate);
  1038.  
  1039.          idle_system ();
  1040.          timeout = 0L;
  1041.          function_active = 99;
  1042.  
  1043.          return (1);
  1044.       }
  1045.    }
  1046.  
  1047.    wclose ();
  1048.    prints (7, 65, YELLOW|_BLACK, "Hangup ");
  1049.    modem_hangup ();
  1050.  
  1051.    answer_flag = 0;
  1052.    rate = config->speed;
  1053.    if (!lock_baud)
  1054.       com_baud(rate);
  1055.  
  1056.    idle_system ();
  1057.    timeout = 0L;
  1058.    function_active = 99;
  1059.  
  1060.    return (0);
  1061. }
  1062.  
  1063. void iemsi_handshake (void)
  1064. {
  1065.     int i, fd, tries, miemsi = 0;
  1066.     unsigned long crc;
  1067.     char string[2048], *p, addr[20], *filename;
  1068.     long t1, t2, ici;
  1069.     struct _usr iusr;
  1070.     struct _usridx usridx;
  1071.  
  1072.  
  1073.     memset ((char *)&iusr, 0, sizeof (struct _usr));
  1074.     prints (7, 65, YELLOW|_BLACK, "IEMSI/C1");
  1075.  
  1076.     tries = 0;
  1077.     t1 = timerset (1000);
  1078.     t2 = timerset (6000);
  1079.  
  1080. resend:
  1081.     if (tries++ > 6) {
  1082.         status_line ("!IEMSI Error: too may tries");
  1083.         return;
  1084.     }
  1085.  
  1086.     if (tries > 1) {
  1087.         m_print2 ("**EMSI_NAKEEC3\r");
  1088.         t1 = timerset (1000);
  1089.  
  1090.         while (CARRIER && !timeup (t1) && !timeup (t2)) {
  1091.             if (PEEKBYTE () == '*') {
  1092.                 get_emsi_id (string, 10);
  1093.             if (!strncmp (string, "**EMSI_ICI", 10) || !strncmp (string, "*EMSI_ICI", 9))
  1094.                break;
  1095.             if (!strncmp (string, "**EMSI_HBT", 10) || !strncmp (string, "**EMSI_ACK", 10))
  1096.                t1 = timerset (1000);
  1097.          }
  1098.          else if (PEEKBYTE () != -1) {
  1099.                 TIMED_READ(1);
  1100.             time_release ();
  1101.             }
  1102.       }
  1103.     }
  1104.  
  1105.    if (!get_emsi_field (string))         // Name
  1106.         goto resend;
  1107.    string[35] = '\0';
  1108.     strcpy (iusr.name, fancy_str (strbtrim (string)));
  1109.     if (iusr.name[strlen (iusr.name) - 1] == '.')
  1110.         iusr.name[strlen (iusr.name) - 1] = '\0';
  1111.  
  1112.     if (!get_emsi_field (string))         // Alias
  1113.         goto resend;
  1114.     string[35] = '\0';
  1115.     strcpy (iusr.handle, fancy_str (string));
  1116.     if (iusr.handle[strlen (iusr.handle) - 1] == '.')
  1117.         iusr.handle[strlen (iusr.handle) - 1] = '\0';
  1118.  
  1119.     if (!get_emsi_field (string))         // Location
  1120.         goto resend;
  1121.     string[25] = '\0';
  1122.     strcpy (iusr.city, string);
  1123.  
  1124.     if (!get_emsi_field (string))         // Data#
  1125.         goto resend;
  1126.     string[19] = '\0';
  1127.     strcpy (iusr.dataphone, string);
  1128.  
  1129.     if (!get_emsi_field (string))         // Voice#
  1130.         goto resend;
  1131.     string[19] = '\0';
  1132.     strcpy (iusr.voicephone, string);
  1133.  
  1134.    if (!get_emsi_field (string))         // Password
  1135.         goto resend;
  1136.    string[15] = '\0';
  1137.     strcpy (iusr.pwd, strupr (string));
  1138.  
  1139.    if (!get_emsi_field (string))         // Birthdate
  1140.         goto resend;
  1141.    string[9] = '\0';
  1142.     strcpy (iusr.birthdate, string);
  1143.  
  1144.    if (!get_emsi_field (string))         // CRTDEF
  1145.       goto resend;
  1146.  
  1147.    p = strtok (strupr (string), ",");
  1148.     if (strcmp (p, "AVT0"))
  1149.       iusr.color = iusr.avatar = 1;
  1150.    else if (strcmp (p, "ANSI"))
  1151.       iusr.color = iusr.ansi = 1;
  1152.     else if (strcmp (p, "VT52") || strcmp (p, "VT100"))
  1153.       iusr.ansi = 1;
  1154.  
  1155.    p = strtok (NULL, ",");
  1156.    iusr.len = atoi (p);
  1157.  
  1158.    p = strtok (NULL, ",");
  1159.    iusr.width = atoi (p);
  1160.  
  1161.    p = strtok (NULL, ",");
  1162.    iusr.nulls = atoi (p);
  1163.  
  1164.    if (!get_emsi_field (string))         // Protocols
  1165.       goto resend;
  1166.  
  1167.     if (!get_emsi_field (string))         // Capabilities
  1168.       goto resend;
  1169.  
  1170.    strupr (string);
  1171.     if (strstr (string, "ASCII8"))
  1172.       iusr.ibmset = 1;
  1173.  
  1174.     if (!get_emsi_field (string))         // Requests
  1175.       goto resend;
  1176.  
  1177.    strupr (string);
  1178.    if (strstr (string, "MORE"))
  1179.       iusr.more = 1;
  1180.    if (strstr (string, "HOT"))
  1181.       iusr.hotkey = 1;
  1182.     if (strstr (string, "CLR"))
  1183.       iusr.formfeed = 1;
  1184.    if (strstr (string, "FSED"))
  1185.       iusr.use_lore = 0;
  1186.     else
  1187.       iusr.use_lore = 1;
  1188.    if (strstr (string, "MAIL"))
  1189.       miemsi |= IEMSI_MAILCHECK;
  1190.    if (strstr (string, "FILE"))
  1191.       miemsi |= IEMSI_FILECHECK;
  1192.    if (strstr (string, "NEWS"))
  1193.       miemsi |= IEMSI_NEWS;
  1194.  
  1195.    if (!get_emsi_field (string))         // Software
  1196.       goto resend;
  1197.  
  1198.    if (!get_emsi_field (string))         // Xlattbl
  1199.       goto resend;
  1200.  
  1201.     prints (7, 65, YELLOW|_BLACK, "IEMSI/C2");
  1202.  
  1203.     strcpy (string, "{LoraBBS,");
  1204.    strcat (string, VNUM);
  1205.     activation_key ();
  1206.    if (registered) {
  1207.       sprintf (addr, ",%s%05u}{", serial_id[0] ? serial_id : "", serial_no);
  1208.         strcat (string, addr);
  1209.    }
  1210.    else
  1211.       strcat (string, ",Demo}{");
  1212.    strcat (string, system_name);
  1213.    strcat (string, "}{");
  1214.    if (location != NULL)
  1215.       strcat (string, location);
  1216.     strcat (string, "}{");
  1217.    strcat (string, sysop);
  1218.    strcat (string, "}{");
  1219.    sprintf (addr, "%08lX", time (NULL) - timezone);
  1220.     strcat (string, addr);
  1221.    strcat (string, "}{}{!}{}");
  1222.  
  1223.    sprintf (addr, "EMSI_ISI%04X", strlen (string));
  1224.    crc = 0xFFFFFFFFL;
  1225.    for (i = 0; i < strlen (addr); i++)
  1226.       crc = Z_32UpdateCRC (addr[i], crc);
  1227.  
  1228.    for (i = 0; i < strlen (string); i++)
  1229.       crc = Z_32UpdateCRC (string[i], crc);
  1230.  
  1231.    t1 = timerset (6000);
  1232.    timeout = t1;
  1233.    to_row = 8;
  1234.     tries = 1;
  1235.  
  1236.    for (;;) {
  1237.         m_print2 ("**EMSI_ISI%04X%s", strlen (string), string);
  1238.       CLEAR_INBOUND ();
  1239.         m_print2 ("%08lX\r", crc);
  1240.       FLUSH_OUTPUT ();
  1241.  
  1242.         if (tries++ > 10) {
  1243.             modem_hangup ();
  1244.             status_line ("!Trans. IEMSI failure");
  1245.          return;
  1246.       }
  1247.  
  1248.       t2 = timerset (1000);
  1249.  
  1250.         while (!timeup (t1)) {
  1251.          if (timeup (t2))
  1252.             break;
  1253.  
  1254.             if (PEEKBYTE() == '*') {
  1255.                 get_emsi_id (addr, 14);
  1256.                 if (!strnicmp (addr, "**EMSI_ICI", 10) || !strnicmp (addr, "*EMSI_ICI", 9)) {
  1257.                     CLEAR_INBOUND ();
  1258.                     m_print2 ("**EMSI_ACKA490\r");
  1259.                     break;
  1260.                 }
  1261.                 if (!strnicmp (addr, "**EMSI_ACKA490", 14) || !strnicmp (addr, "*EMSI_ACKA490", 13))
  1262.                     break;
  1263.                 if (!strnicmp (addr, "**EMSI_NAKEEC3", 14) || !strnicmp (addr, "*EMSI_NAKEEC3", 13))
  1264.                     break;
  1265.             }
  1266.             else if (PEEKBYTE () != -1) {
  1267.                 TIMED_READ(10);
  1268.                 time_release ();
  1269.             }
  1270.         }
  1271.  
  1272.         if (!strnicmp (addr, "**EMSI_ACKA490", 14) || !strnicmp (addr, "*EMSI_ACKA490", 13))
  1273.             break;
  1274.     }
  1275.  
  1276.     filename = string;
  1277.  
  1278.     if (iusr.handle && stricmp (iusr.handle, iusr.name)) {
  1279.         crc = crc_name (iusr.handle);
  1280.         sprintf (filename, "%s.IDX", config->user_file);
  1281.         fd = open (filename, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  1282.         while (read (fd, &usridx, sizeof (struct _usridx)) == sizeof (struct _usridx)) {
  1283.             if (usridx.id == crc || usridx.alias_id == crc) {
  1284.                 status_line (":Invalid IEMSI alias '%s'", iusr.handle);
  1285.                 strcpy(iusr.handle,iusr.name);
  1286.                 break;
  1287.             }
  1288.         }
  1289.         close (fd);
  1290.  
  1291.     }
  1292.  
  1293.     if (iusr.handle[0]){
  1294. //        strcpy (usr.handle, iusr.handle);
  1295.         iusr.alias_id = crc;
  1296.         sprintf (filename, "%s.IDX", config->user_file);
  1297.         fd = open (filename, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
  1298.         while (read (fd, &usridx, sizeof (struct _usridx)) == sizeof (struct _usridx)) {
  1299.             ici=tell(fd);
  1300.             if (usridx.id == usr.id) {
  1301.          lseek(fd,(ici-(sizeof(struct _usridx))),SEEK_SET);
  1302.                 usridx.alias_id = crc;
  1303.                 write (fd, (char *)&usridx, sizeof (struct _usridx));
  1304.                 break;
  1305.             }
  1306.         }
  1307.         close (fd);
  1308.     }
  1309.     memcpy ((char *)&usr, (char *)&iusr, sizeof (struct _usr));
  1310.     iemsi = miemsi | IEMSI_ON;
  1311. }
  1312.  
  1313. unsigned long string_crc(char *string, unsigned long crc)
  1314. {
  1315.  
  1316.     int i;
  1317.  
  1318.     for (i = 0; i < strlen (string); i++)
  1319.         crc = Z_32UpdateCRC (string[i], crc);
  1320.     return crc;
  1321. }
  1322.