home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / LO241SRV.ZIP / yoohoo.c < prev    next >
C/C++ Source or Header  |  1998-08-01  |  45KB  |  1,757 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 <mem.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <io.h>
  25. #include <fcntl.h>
  26. #include <alloc.h>
  27. #include <dos.h>
  28. #include <time.h>
  29.  
  30. #include <cxl\cxlvid.h>
  31. #include <cxl\cxlwin.h>
  32.  
  33. #include "lsetup.h"
  34. #include "sched.h"
  35. #include "msgapi.h"
  36. #include "externs.h"
  37. #include "prototyp.h"
  38. #include "zmodem.h"
  39.  
  40. #define MAX_EMSI_ADDR   30
  41.  
  42. #define isOPUS              5
  43. #define isMAX_PRODUCT    0xE2
  44. #define isLORA           0x4E
  45.  
  46. #include "version.h"
  47.  
  48. #define xcrc(crc,cp) ( crctab[((crc >> 8) & 255) ^ cp] ^ (crc << 8))
  49.  
  50. extern long timeout, b_mail, b_data, elapsed;
  51. extern int to_row, n_mail, n_data, got_maildata;
  52. extern word serial_no;
  53. extern char *VNUM, serial_id[3];
  54.  
  55. char remote_system[60];
  56. char remote_sysop[36];
  57. char remote_program[60];
  58. char remote_location[40];
  59. char *emsi_data_packet;
  60. int data_pointer,data_lenght;
  61.  
  62. struct _alias addrs[MAX_EMSI_ADDR + 1];
  63.  
  64. void build_node_queue (int zone, int net, int node, int point, int i);
  65. int n_getpassword(int, int, int, int);
  66. int n_password(char *, char *);
  67. int send_WaZOO(int);
  68. // int get_emsi_field (char *);
  69. int get_emsi_field2 (char *);
  70. void m_print2(char *format, ...);
  71. int get_bbs_local_record (int zone, int net, int node, int point);
  72. int esc_pressed(void);
  73. void check_duplicate_key(word);
  74.  
  75. static int n_password2(char *, char *);
  76. unsigned short get_emsi_data (char *);
  77.  
  78. int send_Hello(Sender)
  79. int Sender;
  80. {
  81.     int i, v, ntt_mail = 0, ntt_data = 0;
  82.     struct _Hello Hello;
  83.     char *sptr;
  84.     unsigned short crc, num_errs=0;
  85.     long response_timer, btt_mail = 0, btt_data = 0;
  86.  
  87.     memset( (char *)&Hello, 0, sizeof(struct _Hello) );
  88.  
  89.     Hello.signal         = 'o';
  90.    Hello.hello_version  =  1;
  91.    Hello.product        = isLORA;
  92.    Hello.product_maj    = MAJVERSION;
  93.     Hello.product_min    = MINVERSION;
  94.  
  95.    strncpy(Hello.my_name,system_name,57);
  96.     Hello.my_name[58]    = '\0';
  97.  
  98.    Hello.serial_no = serial_no;
  99.  
  100.    strncpy(Hello.sysop,sysop,19);
  101.    Hello.sysop[19]      = '\0';
  102.  
  103.    if (remote_point && remote_zone == config->alias[assumed].zone && remote_net == config->alias[assumed].net && remote_node == config->alias[assumed].node) {
  104.       remote_password[0] = '\0';
  105.       if (get_bbs_local_record (config->alias[assumed].zone, config->alias[assumed].fakenet, remote_point, 0)) {
  106.          if (nodelist.password[0] != '\0')
  107.             strcpy (remote_password, nodelist.password);
  108.       }
  109.       else if (get_bbs_local_record (remote_zone, remote_net, remote_node, remote_point)) {
  110.             if (nodelist.password[0] != '\0')
  111.             strcpy (remote_password, nodelist.password);
  112.       }
  113.       strncpy(Hello.my_password,remote_password,8);
  114.    }
  115.    else {
  116.       if (get_bbs_local_record (remote_zone, remote_net, remote_node, remote_point)) {
  117.          if (nodelist.password[0] != '\0')
  118.             strcpy (remote_password, nodelist.password);
  119.          else
  120.             remote_password[0] = '\0';
  121.       }
  122.       strncpy(Hello.my_password,remote_password,8);
  123.     }
  124.  
  125.    Hello.reserved2[0] = '\0';
  126.  
  127.    Hello.my_zone        = config->alias[assumed].zone;
  128.     Hello.my_net         = config->alias[assumed].net;
  129.    Hello.my_node        = config->alias[assumed].node;
  130.    Hello.my_point       = config->alias[assumed].point;
  131.  
  132.     Hello.tranx = time (NULL);
  133.  
  134.    if (Sender) {
  135.         build_node_queue (call_list[next_call].zone, call_list[next_call].net, call_list[next_call].node, call_list[next_call].point, next_call);
  136.       Hello.n_mail = call_list[next_call].n_mail;
  137.       Hello.b_mail = call_list[next_call].b_mail;
  138.       Hello.n_data = call_list[next_call].n_data;
  139.       Hello.b_data = call_list[next_call].b_data;
  140.    }
  141.    else {
  142.       build_node_queue (remote_zone, remote_net, remote_node, remote_point, -1);
  143.       for (i = 0; i < max_call; i++) {
  144.             if (remote_zone == call_list[i].zone && remote_net == call_list[i].net && remote_node == call_list[i].node && remote_point == call_list[i].point) {
  145.             ntt_mail += call_list[i].n_mail;
  146.             btt_mail += call_list[i].b_mail;
  147.             ntt_data += call_list[i].n_data;
  148.             btt_data += call_list[i].b_data;
  149.             break;
  150.          }
  151.       }
  152.  
  153.       if (remote_point) {
  154.          for (v = 0; v < MAX_ALIAS && config->alias[v].net; v++) {
  155.             if (remote_zone == config->alias[v].zone && remote_net == config->alias[v].net && remote_node == config->alias[v].node && config->alias[v].fakenet)
  156.                break;
  157.          }
  158.  
  159.          if (v < MAX_ALIAS && config->alias[v].net) {
  160.             build_node_queue (remote_zone, config->alias[v].fakenet, remote_point, 0, -1);
  161.             for (i = 0; i < max_call; i++) {
  162.                if (remote_zone == call_list[i].zone && config->alias[v].fakenet == call_list[i].net && remote_point == call_list[i].node && !call_list[i].point) {
  163.                         ntt_mail += call_list[i].n_mail;
  164.                   btt_mail += call_list[i].b_mail;
  165.                   ntt_data += call_list[i].n_data;
  166.                         btt_data += call_list[i].b_data;
  167.                   break;
  168.                     }
  169.             }
  170.          }
  171.       }
  172.  
  173.       Hello.n_mail = ntt_mail;
  174.       Hello.b_mail = btt_mail;
  175.       Hello.n_data = ntt_data;
  176.       Hello.b_data = btt_data;
  177.    }
  178.  
  179.    Hello.capabilities = ZED_ZAPPER|ZED_ZIPPER;
  180.    Hello.capabilities |= Y_DIETIFNA;
  181.  
  182.    if (mdm_flags == NULL)
  183.       Hello.capabilities |= DOES_IANUS;
  184.    else if (config->janus && strstr (mdm_flags, "Hst") == NULL)
  185.       Hello.capabilities |= DOES_IANUS;
  186.  
  187.    if (!Sender) {
  188.       if (remote_capabilities & Hello.capabilities & DOES_IANUS)
  189.          Hello.capabilities = DOES_IANUS;
  190.       else if (remote_capabilities & Hello.capabilities & ZED_ZAPPER)
  191.          Hello.capabilities = ZED_ZAPPER;
  192.       else if (remote_capabilities & Hello.capabilities & ZED_ZIPPER)
  193.             Hello.capabilities = ZED_ZIPPER;
  194.       else
  195.          Hello.capabilities = Y_DIETIFNA;
  196.    }
  197.  
  198.     if (cur_event == -1 || !(e_ptrs[cur_event]->behavior & MAT_NOREQ))
  199.       Hello.capabilities |= WZ_FREQ;
  200.  
  201.     XON_DISABLE ();
  202.  
  203. xmit_packet:
  204.    SENDBYTE (0x1f);
  205.  
  206.    sptr = (char *) (&Hello);
  207. //   SENDCHARS (sptr, 128, 1);
  208.  
  209.    /*--------------------------------------------------------------------*/
  210.    /* Calculate CRC while modem is sending its buffer                    */
  211.    /*--------------------------------------------------------------------*/
  212.     for (crc = i = 0; i < 128; i++) {
  213.       crc = xcrc (crc, (byte) sptr[i]);
  214.       SENDBYTE ( (byte) sptr[i] );
  215.    }
  216.  
  217.    CLEAR_INBOUND ();
  218.  
  219.    SENDBYTE ((unsigned char) (crc >> 8));
  220.    SENDBYTE ((unsigned char) (crc & 0xff));
  221.  
  222.     response_timer = timerset (4000);
  223.  
  224.    while (!timeup (response_timer) && CARRIER) {
  225.       if (!CHAR_AVAIL ()) {
  226.          if (local_kbd == 0x1B) {
  227.             SET_DTR_OFF ();
  228.                 sptr  = msgtxt[M_KBD_MSG];
  229.             goto no_response;
  230.          }
  231.          time_release ();
  232.          continue;
  233.         }
  234.  
  235.       switch (i = TIMED_READ (0)) {
  236.             case ACK:
  237.             return (1);
  238.  
  239.          case '?':
  240.          case ENQ:
  241.             if (++num_errs == 10) {
  242.                sptr = msgtxt[M_FUBAR_MSG];
  243.                goto no_response;
  244.             }
  245.             goto xmit_packet;
  246.  
  247.          default:
  248.             break;
  249.       }
  250.    }
  251.  
  252. no_response:
  253.  
  254.    return (0);
  255. }
  256.  
  257. /*------------------------------------------------------------------------*/
  258. /* SEND YOOHOO                                                            */
  259. /*------------------------------------------------------------------------*/
  260. int send_YOOHOO(instigator)
  261. int instigator;
  262. {
  263.     char a;
  264.    long val;
  265.  
  266.    prints (7, 65, YELLOW|_BLACK, "YooHoo");
  267.  
  268.     if (instigator) {
  269.       remote_zone = called_zone;
  270.       remote_net = called_net;
  271.         remote_node = called_node;
  272.       remote_point = 0;
  273.    }
  274.    else
  275.       prints (7, 65, YELLOW|_BLACK, "YooHoo/2U2");
  276.  
  277.    if (!send_Hello(instigator)) {
  278.         status_line("!Not send_Hello");
  279.       return(0);
  280.     }
  281.  
  282.    if (instigator) {
  283.       val=timerset(600);
  284.       while(!timeup(val)) {
  285.          a = (char )TIMED_READ(1);
  286.          if(a == (char )YOOHOO)
  287.             return(get_YOOHOO(0));
  288.       }
  289.  
  290.         status_line("!No YOOHOO/2U2");
  291.       modem_hangup();
  292.       return (0);
  293.    }
  294.  
  295.    return (1);
  296. }
  297.  
  298. int get_YOOHOO(plan_to_send_too)
  299. int plan_to_send_too;
  300. {
  301.    int i, c, ne, no, pp, zz;
  302.    struct _Hello Hello;
  303.     char string[50], *p;
  304.    byte *sptr, num_errs=0;
  305.    unsigned short crc, lsb, msb;
  306.    long timer_val, hello_timeout;
  307.    struct tm *tim;
  308.    struct date da;
  309.    struct time ti;
  310.  
  311.    CLEAR_INBOUND();
  312.    CLEAR_OUTBOUND();
  313.  
  314.     XON_DISABLE ();
  315.    _BRK_DISABLE ();
  316.  
  317.    prints (7, 65, YELLOW|_BLACK, "YooHoo");
  318.    if(!plan_to_send_too)
  319.       prints (7, 65, YELLOW|_BLACK, "YooHoo/2U2");
  320.  
  321.    SENDBYTE(ENQ);
  322.  
  323.    timer_val   = timerset(4000);
  324.  
  325. recv:
  326.    while(1) {
  327.       if (!CARRIER || timeup(timer_val))
  328.          goto failed;
  329.  
  330.       if ((c = TIMED_READ(20)) == 0x1F)
  331.          break;
  332.  
  333.       if (c >= 0) {
  334.          hello_timeout = timerset (1000);
  335.          while (((c = PEEKBYTE ()) >= 0) && (c != 0x1f) && (CARRIER)) {
  336.             if (timeup(hello_timeout))
  337.                break;
  338.                 MODEM_IN ();
  339.          }
  340.  
  341.          if (c != 0x1f) {
  342.             CLEAR_INBOUND ();
  343.             SENDBYTE (ENQ);
  344.          }
  345.       }
  346.  
  347.    }
  348.  
  349. loop:
  350.    sptr = (char *) (&Hello);
  351.  
  352.    hello_timeout = timerset (200);
  353.  
  354.    for (i = 0, crc = 0; i < 128; i++) {
  355.       while (PEEKBYTE () < 0) {
  356.          if (timeup (timer_val) || timeup (hello_timeout))
  357.             break;
  358.         
  359.          if (!CARRIER)
  360.             goto failed;
  361.  
  362.          time_release ();
  363.         }
  364.  
  365.         if (timeup (timer_val))
  366.             goto failed;
  367.         else if (timeup (hello_timeout))
  368.             break;
  369.  
  370.         c = MODEM_IN () & 0x00FF;
  371.  
  372.         sptr[i] = (char) c;
  373.         crc = xcrc (crc, (byte) c);
  374.  
  375.         hello_timeout = timerset (200);
  376.     }
  377.  
  378.     if (((msb = TIMED_READ (10)) < 0) || ((lsb = TIMED_READ (10)) < 0))
  379.       goto hello_error;
  380.  
  381.    if (((msb << 8) | lsb) == crc)
  382.         goto process_hello;
  383.  
  384. hello_error:
  385.    if (timeup(timer_val))
  386.       goto failed;
  387.  
  388.    if ((num_errs++) > 10)
  389.       goto failed;
  390.  
  391.    CLEAR_INBOUND ();
  392.    SENDBYTE ('?');
  393.    goto recv;
  394.  
  395. process_hello:
  396.    Hello.my_name[42] = '\0';
  397.    Hello.sysop[19] = '\0';
  398.    strcpy(remote_password,Hello.my_password);
  399.    remote_zone = Hello.my_zone;
  400.    remote_net = Hello.my_net;
  401.    remote_node = Hello.my_node;
  402.    remote_point = Hello.my_point;
  403.    remote_capabilities = (Hello.capabilities)|Y_DIETIFNA;
  404.  
  405.    if (plan_to_send_too) {
  406.       for (i=0; i < MAX_ALIAS; i++)
  407.          if (remote_zone && config->alias[i].zone == remote_zone)
  408.                 break;
  409.  
  410.       if (i == MAX_ALIAS)
  411.          assumed = 0;
  412.       else
  413.          assumed = i;
  414.  
  415.       for (i=0; i < MAX_ALIAS; i++)
  416.             if ( config->alias[i].fakenet == remote_net && (!remote_zone || config->alias[i].zone == remote_zone) )
  417.             break;
  418.       if (i != MAX_ALIAS)
  419.          assumed = i;
  420.  
  421.       if (remote_point) {
  422.          for (i=0; i < MAX_ALIAS; i++)
  423.             if ( config->alias[i].net == remote_net && config->alias[i].node == remote_node &&
  424.                  (!remote_zone || config->alias[i].zone == remote_zone)
  425.                )
  426.                break;
  427.          if (i != MAX_ALIAS)
  428.             assumed = i;
  429.       }
  430.    }
  431.  
  432.    if (remote_net == config->alias[assumed].fakenet)
  433.         status_line("*%s (%u:%u/%u.%u)",Hello.my_name,remote_zone,config->alias[assumed].net,config->alias[assumed].node,remote_node);
  434.    else
  435.         status_line("*%s (%u:%u/%u.%u)",Hello.my_name,remote_zone,remote_net,remote_node,remote_point);
  436.  
  437.    strcpy(remote_system, Hello.my_name);
  438.  
  439.    if (Hello.product == isOPUS) {
  440.       sprintf (remote_program, "Opus Version %d.%02d", Hello.product_maj,
  441.       (Hello.product_min == 48) ? 0 : Hello.product_min);
  442.    }
  443.     else if (Hello.product <= isMAX_PRODUCT) {
  444.       sprintf (remote_program,"%Fs Version %d.%02d",prodcode[Hello.product], Hello.product_maj, Hello.product_min);
  445.  
  446.       if (Hello.product == isLORA) {
  447.          sprintf (string, "/%05u", Hello.serial_no);
  448.          strcat (remote_program, string);
  449.       }
  450.     }
  451.    else
  452.       sprintf (remote_program,"Program '%02x' Version %d.%02d",Hello.product, Hello.product_maj, Hello.product_min);
  453.  
  454.     status_line(msgtxt[M_REMOTE_USES], remote_program);
  455.  
  456.     status_line(":Sysop: %s",Hello.sysop);
  457.    strcpy(remote_sysop, Hello.sysop);
  458.  
  459.    strcpy (string, "");
  460.    if (remote_capabilities & WZ_FREQ)
  461.       strcat (string, "FReqs ");
  462.    if (remote_capabilities & ZED_ZIPPER)
  463.       strcat (string, "ZedZIP ");
  464.    if (remote_capabilities & ZED_ZAPPER)
  465.       strcat (string, "ZedZAP ");
  466.    if (remote_capabilities & DOES_IANUS)
  467.       strcat (string, "Janus ");
  468.     status_line (":Offer: %s", string);
  469.  
  470.    got_maildata = 0;
  471.  
  472.    if (Hello.product == isLORA) {
  473.         if (Hello.tranx) {
  474. //        Hello.tranx -= timezone;          // TRANX forse corretto :-))
  475.             status_line(":Tranx: %08lX / %08lX", time (NULL), Hello.tranx);
  476.         }
  477.         else
  478.             status_line (":No transaction number presented");
  479.  
  480.         if (!plan_to_send_too)
  481.             check_duplicate_key (Hello.serial_no);
  482.  
  483.         n_mail = Hello.n_mail;
  484.         b_mail = Hello.b_mail;
  485.         n_data = Hello.n_data;
  486.         b_data = Hello.b_data;
  487.         got_maildata = 1;
  488.     }
  489.  
  490.     if (n_getpassword (Hello.my_zone, Hello.my_net, Hello.my_node, Hello.my_point)) {
  491.         remote_password[8] = '\0';
  492.         if (n_password (remote_password, Hello.my_password)) {
  493.          if (plan_to_send_too) {
  494.             modem_hangup ();
  495.             goto failed;
  496.          }
  497.          else
  498.                 status_line (msgtxt[M_PASSWORD_OVERRIDE]);
  499.       }
  500.    }
  501.    else if (Hello.my_point && Hello.my_net == config->alias[assumed].net && Hello.my_node == config->alias[assumed].node ) {
  502.         if (n_getpassword (config->alias[assumed].zone, config->alias[assumed].fakenet, Hello.my_point, 0)) {
  503.          remote_password[8] = '\0';
  504.          if (n_password (remote_password, Hello.my_password)) {
  505.             if (plan_to_send_too) {
  506.                modem_hangup ();
  507.                goto failed;
  508.             }
  509.             else
  510.                     status_line (msgtxt[M_PASSWORD_OVERRIDE]);
  511.          }
  512.       }
  513.     }
  514.  
  515.    strcpy (remote_location, nodelist.city);
  516.  
  517.    if (Hello.product == isLORA && Hello.product_maj >= 2 && Hello.product_min >= 30) {
  518.         tim = localtime (&Hello.tranx);
  519.       da.da_year = tim->tm_year;
  520.       da.da_day = tim->tm_mday;
  521.       da.da_mon = tim->tm_mon;
  522.       ti.ti_min = tim->tm_min;
  523.       ti.ti_hour = tim->tm_hour;
  524.       ti.ti_sec = tim->tm_sec;
  525.       ti.ti_hund = 0;
  526.  
  527.         status_line ("+Remote clock: %2d-%02d-%02d %02d:%02d:%02d", da.da_day, da.da_mon + 1, da.da_year % 100, ti.ti_hour, ti.ti_min, ti.ti_sec);
  528.  
  529.       ne = config->alias[0].net;
  530.       no = config->alias[0].node;
  531.       pp = config->alias[0].point;
  532.       zz = config->alias[0].zone;
  533.  
  534.       strcpy (string, config->resync_nodes);
  535.       if ((p = strtok (string, " ")) != NULL)
  536.          do {
  537.             parse_netnode2 (p, &zz, &ne, &no, &pp);
  538.             if (remote_net == ne && remote_node == no && remote_zone == zz && remote_point == pp)
  539.                break;
  540.          } while ((p = strtok (NULL, " ")) != NULL);
  541.  
  542.       if (p != NULL) {
  543.             elapsed += Hello.tranx - time (NULL);
  544.  
  545.             status_line ("+Resync clock with %d:%d/%d.%d", remote_zone, remote_net, remote_node, remote_point);
  546.          setdate (&da);
  547.          settime (&ti);
  548.         }
  549.    }
  550.  
  551.    CLEAR_INBOUND();
  552.  
  553.    SENDBYTE(ACK);
  554.    SENDBYTE(YOOHOO);
  555.  
  556.    if (plan_to_send_too) {
  557.       for(i=0; (CARRIER && i < 10); i++) {
  558.          if((c=TIMED_READ(5))==ENQ) {
  559.             prints (7, 65, YELLOW|_BLACK, "YooHoo/2U2");
  560.  
  561.             if(!send_Hello(0))
  562.                return(0);
  563.             return(1);
  564.          }
  565.          else {
  566.             if(c > 0)
  567.                CLEAR_INBOUND();
  568.             SENDBYTE(YOOHOO);
  569.          }
  570.       }
  571.       goto failed;
  572.    }
  573.  
  574.    return(1);
  575.  
  576. failed:
  577.     modem_hangup ();
  578.     return(0);
  579. }
  580.  
  581. int n_getpassword (zone, net, nodo, point)
  582. int zone, net, nodo, point;
  583. {
  584.     int i;
  585.     remote_password[0] = '\0';
  586.  
  587.     if (!(i = get_bbs_record (zone, net, nodo, point)))
  588.         return(0);
  589.     else{
  590.         if (filepath == config->norm_filepath) {
  591.             if (config->know_filepath[0])
  592.                 filepath = config->know_filepath;
  593.             if (config->know_okfile[0])
  594.                 request_list = config->know_okfile;
  595.             max_requests = config->know_max_requests;
  596.             max_kbytes = config->know_max_kbytes;
  597.         }
  598.         if(i==1){
  599. //         status_line("DKnow: %d:%d/%d.%d",zone, net, nodo, point);
  600.             return(0);
  601.         }
  602.         else{
  603.             strcpy (remote_password, nodelist.password);
  604.             if (remote_password[0] == '\0'){
  605. //            status_line ("DNo password for %d:%d/%d.%d",zone, net, nodo, point);
  606.                 return (0);
  607.             }
  608.         }
  609.     }
  610.     return(1);
  611. }
  612.  
  613. int n_password(ours,theirs)
  614. char *ours, *theirs;
  615. {
  616.     strupr (theirs);
  617.     strupr (ours);
  618.  
  619.     if (stricmp (theirs, ours)) {
  620.         status_line(msgtxt[M_PWD_ERROR],remote_zone,remote_net,remote_node,remote_point,theirs,ours);
  621.  
  622.         while (!OUT_EMPTY()) {
  623.             if (!CARRIER)
  624.                 return 0;
  625.             time_release();
  626.         }
  627.  
  628.         return(1);
  629.     }
  630.  
  631.    if (ours[0]) {
  632.         status_line (msgtxt[M_PROTECTED_SESSION]);
  633.       if (config->prot_filepath[0])
  634.          filepath = config->prot_filepath;
  635.       if (config->prot_okfile[0])
  636.          request_list = config->prot_okfile;
  637.       max_requests = config->prot_max_requests;
  638.       max_kbytes = config->prot_max_kbytes;
  639.    }
  640.  
  641.    return(0);
  642. }
  643.  
  644. static int n_password2(theirs,ours)
  645. char *theirs, *ours;
  646. {
  647.    strupr(theirs);
  648.    strupr(ours);
  649.  
  650.    if(stricmp(theirs,ours)) {
  651.         while (!OUT_EMPTY()) {
  652.          if (!CARRIER)
  653.             return 0;
  654.          time_release();
  655.       }
  656.  
  657.       return(1);
  658.    }
  659.  
  660.     if (ours[0]) {
  661.         if (config->prot_filepath[0])
  662.          filepath = config->prot_filepath;
  663.       if (config->prot_okfile[0])
  664.          request_list = config->prot_okfile;
  665.       max_requests = config->prot_max_requests;
  666.       max_kbytes = config->prot_max_kbytes;
  667.    }
  668.  
  669.    return(0);
  670. }
  671.  
  672. void get_emsi_id (s, width)
  673. char *s;
  674. int width;
  675. {
  676.     int i, m, c;
  677.  
  678.     while ((c = TIMED_READ (10)) == '*' && CARRIER)
  679.     ;
  680.  
  681.     if (c != 'E' || !CARRIER) {
  682.         s[0] = '\0';
  683.         return;
  684.     }
  685.     if ((c = TIMED_READ (10)) != 'M' || !CARRIER) {
  686.         s[0] = '\0';
  687.         return;
  688.     }
  689.     if ((c = TIMED_READ (10)) != 'S' || !CARRIER) {
  690.         s[0] = '\0';
  691.         return;
  692.     }
  693.     if ((c = TIMED_READ (10)) != 'I' || !CARRIER) {
  694.         s[0] = '\0';
  695.         return;
  696.     }
  697.     if ((c = TIMED_READ (10)) != '_' || !CARRIER) {
  698.         s[0] = '\0';
  699.         return;
  700.     }
  701.  
  702.  
  703. //   if (c != '*' || !CARRIER) {
  704. //      s[0] = '\0';
  705. //      return;
  706. //   }
  707.  
  708. //   while ((c = PEEKBYTE ()) == '*' && CARRIER)
  709. //      TIMED_READ (1);
  710.  
  711. //   if (c == '\r' || c == -1 || !CARRIER) {
  712. //      TIMED_READ (1);
  713. //      s[0] = '\0';
  714. //      return;
  715. //   }
  716.  
  717.     strcpy (s, "**EMSI_");
  718.  
  719.     for (i = strlen (s); i < width; i++) {
  720.         if ((c = TIMED_READ (10)) == -1 || !CARRIER) {
  721.             s[0] = '\0';
  722.             return;
  723.         }
  724.         if (c == 0x0D)
  725.             break;
  726.         s[i] = c;
  727.     }
  728.     s[i] = '\0';
  729. //   status_line("!id: %s",s);
  730.  
  731. /*
  732.     m = 2;
  733.     for (i = 0; i < 12; i++) {
  734.         c = TIMED_READ (10);
  735.         if (c == -1 || !CARRIER) {
  736.             s[0] = '\0';
  737.             return;
  738.         }
  739.         if (c == '*' && m == 2) {
  740.             i--;
  741.             continue;
  742.         }
  743.         if (c == '\r') {
  744.             s[m] = '\0';
  745.             return;
  746.         }
  747.         s[m++] = c;
  748.         if (m >= width) {
  749.             s[m] = '\0';
  750.             return;
  751.         }
  752.     }
  753.     s[m] = '\0';
  754. */
  755. }
  756.  
  757.  
  758.  
  759. /*
  760.     char c;
  761.     int i = 0;
  762.     long t;
  763.  
  764.     t = timerset (100);
  765.  
  766.    while (CARRIER && !timeup (t)) {
  767.       while (PEEKBYTE() == -1) {
  768.          if (!CARRIER || timeup (t)) {
  769.             s[i] = '\0';
  770.             return;
  771.          }
  772.       }
  773.  
  774.       c = (char)TIMED_READ (10);
  775.       t = timerset (100);
  776.  
  777.       if (c == 0x0D && i)
  778.          break;
  779.       else if (c == 0x0D) {
  780.          i = 0;
  781.          continue;
  782.         }
  783.  
  784.         s[i++] = c;
  785.  
  786.         if (i >= width)
  787.             break;
  788.     }
  789.  
  790.     s[i] = '\0';
  791. }
  792. */
  793.  
  794. int send_emsi_handshake (originator)
  795. int originator;
  796. {
  797.     int i, m, tries, v, ntt_mail = 0, ntt_data = 0;
  798.     unsigned short crc;
  799.     char string[2048], addr[60];
  800.     long t1, t2, btt_mail = 0, btt_data = 0;
  801.  
  802.  
  803.     tries = 0;
  804.     t1 = timerset (6000);
  805.  
  806. //   if (!originator)
  807. //      m_print ("**EMSI_INQC816\r");
  808.  
  809.     if (originator){
  810.         prints (7, 65, YELLOW|_BLACK, "EMSI/C1");
  811. //      status_line("!EMSI/C1");
  812.     }
  813.     else{
  814.         prints (7, 65, YELLOW|_BLACK, "EMSI/C2");
  815. //      status_line("!EMSI/C2");
  816.     }
  817.  
  818.     string[0] = '\0';
  819.     strcat (string, "{EMSI}{");
  820.  
  821.     for (m = 0; m < MAX_ALIAS && config->alias[m].net; m++) {
  822.         sprintf (addr, "%d:%d/%d.%d", config->alias[m].zone, config->alias[m].net, config->alias[m].node, config->alias[m].point);
  823.         if (m)
  824.             strcat (string, " ");
  825.         strcat (string, addr);
  826.     }
  827.  
  828.     strcat (string, "}{");
  829.     addr[0] = '\0';
  830.  
  831.     if (remote_point && remote_zone == config->alias[assumed].zone && remote_net == config->alias[assumed].net && remote_node == config->alias[assumed].node) {
  832.         remote_password[0] = '\0';
  833.         if (get_bbs_local_record (config->alias[assumed].zone, config->alias[assumed].fakenet, remote_point, 0)) {
  834.             if (nodelist.password[0] != '\0')
  835.                 strcpy (remote_password, nodelist.password);
  836.         }
  837.         else if (get_bbs_local_record (remote_zone, remote_net, remote_node, remote_point)) {
  838.             if (nodelist.password[0] != '\0')
  839.                 strcpy (remote_password, nodelist.password);
  840.         }
  841.         strcpy(addr,remote_password);
  842.     }
  843.     else {
  844.         if (get_bbs_local_record (remote_zone, remote_net, remote_node, remote_point)) {
  845.             if (nodelist.password[0] != '\0')
  846.                 strcpy (remote_password, nodelist.password);
  847.             else
  848.                 remote_password[0] = '\0';
  849.         }
  850.         strcpy(addr,remote_password);
  851.     }
  852.  
  853.     strcat (string, strupr(addr));
  854.  
  855.     strcat (string, "}{8N1,PUA");
  856.     if (cur_event != -1 && (e_ptrs[cur_event]->behavior & MAT_NOREQ))
  857.         strcat (string, ",HRQ");
  858.     strcat (string, "}{");
  859.     if (mdm_flags == NULL)
  860.         strcat (string, "JAN,");
  861.     else if (config->janus && strstr (mdm_flags, "Hst") == NULL)
  862.         strcat (string, "JAN,");
  863.     strcat (string, "ZAP,ZMO");
  864.     if (cur_event != -1 && (e_ptrs[cur_event]->behavior & MAT_NOREQ))
  865.         strcat (string, ",NRQ");
  866.     strcat (string, ",ARC,XMA,FNC}{4E}{LoraBBS-OS/2}{");
  867.     strcat (string, VNUM);
  868.     strcat (string, "}{");
  869.  
  870.     activation_key ();
  871.     if (registered) {
  872.         sprintf (addr, "%s%05u", serial_id[0] ? serial_id : "", serial_no);
  873.         strcat (string, addr);
  874.     }
  875.     else
  876.         strcat (string, "Demo");
  877.  
  878.     strcat (string, "}{IDENT}{[");
  879.     strcat (string, system_name);
  880.     strcat (string, "][");
  881.     if (location != NULL)
  882.         strcat (string, location);
  883.     strcat (string, "][");
  884.     strcat (string, sysop);
  885.     strcat (string, "][");
  886.     if (phone != NULL)
  887.         strcat (string, phone);
  888.     strcat (string, "][");
  889.     sprintf (addr, "%d", speed > 9600 ? 9600 : speed);
  890.     strcat (string, addr);
  891.     strcat (string, "][");
  892.     if (flags != NULL)
  893.         strcat (string, flags);
  894.     strcat (string, "]}");
  895.  
  896.     sprintf (addr, "{TRX#}{[%08lX]}", time (NULL));
  897.     strcat (string, addr);
  898.  
  899.     ntt_mail = 0;
  900.     btt_mail = 0L;
  901.     ntt_data = 0;
  902.     btt_data = 0L;
  903.  
  904.     if (originator) {
  905.         build_node_queue (call_list[next_call].zone, call_list[next_call].net, call_list[next_call].node, call_list[next_call].point, next_call);
  906.         sprintf (addr, "{MTX#}{[%d][%ld][%d][%ld]}", call_list[next_call].n_mail, call_list[next_call].b_mail, call_list[next_call].n_data, call_list[next_call].b_data);
  907.         strcat (string, addr);
  908.     }
  909.     else {
  910.         for (m = 0; m < MAX_EMSI_ADDR; m++) {
  911.             if (addrs[m].net == 0)
  912.                 break;
  913.  
  914.             build_node_queue (addrs[m].zone, addrs[m].net, addrs[m].node, addrs[m].point, -1);
  915.             for (i = 0; i < max_call; i++) {
  916.                 if (addrs[m].zone == call_list[i].zone && addrs[m].net == call_list[i].net && addrs[m].node == call_list[i].node && addrs[m].point == call_list[i].point) {
  917.                     ntt_mail += call_list[i].n_mail;
  918.                     btt_mail += call_list[i].b_mail;
  919.                     ntt_data += call_list[i].n_data;
  920.                     btt_data += call_list[i].b_data;
  921.                     break;
  922.                 }
  923.             }
  924.  
  925.             if (i >= max_call && addrs[m].point) {
  926.                 for (v = 0; v < MAX_ALIAS && config->alias[v].net; v++) {
  927.                     if (addrs[m].zone == config->alias[v].zone && addrs[m].net == config->alias[v].net && addrs[m].node == config->alias[v].node && config->alias[v].fakenet)
  928.                         break;
  929.                 }
  930.                 if (v >= MAX_ALIAS || !config->alias[v].net)
  931.                     continue;
  932.  
  933.                 for (i = 0; i < MAX_EMSI_ADDR && addrs[i].net; i++) {
  934.                     if (i == m)
  935.                         continue;
  936.                     if (addrs[m].zone == addrs[i].zone && config->alias[v].fakenet == addrs[i].net && addrs[m].point == addrs[i].node && !addrs[i].point)
  937.                         break;
  938.                 }
  939.  
  940.                 if (i < MAX_EMSI_ADDR && addrs[i].net)
  941.                     continue;
  942.  
  943.                 if (v < MAX_ALIAS && config->alias[v].net) {
  944.                     build_node_queue (addrs[m].zone, config->alias[v].fakenet, addrs[m].point, 0, -1);
  945.                     for (i = 0; i < max_call; i++) {
  946.                         if (addrs[m].zone == call_list[i].zone && config->alias[v].fakenet == call_list[i].net && addrs[m].point == call_list[i].node && !call_list[i].point) {
  947.                             ntt_mail += call_list[i].n_mail;
  948.                             btt_mail += call_list[i].b_mail;
  949.                             ntt_data += call_list[i].n_data;
  950.                             btt_data += call_list[i].b_data;
  951.                             break;
  952.                         }
  953.                     }
  954.                 }
  955.             }
  956.         }
  957.  
  958.         sprintf (addr, "{MTX#}{[%d][%ld][%d][%ld]}", ntt_mail, btt_mail, ntt_data, btt_data);
  959.         strcat (string, addr);
  960.     }
  961.  
  962.     sprintf (addr, "EMSI_DAT%04X", strlen (string));
  963.     for (i = crc = 0; i < strlen (addr); i++)
  964.         crc = xcrc (crc, (byte )addr[i]);
  965.  
  966.     for (i = 0; i < strlen (string); i++)
  967.         crc = xcrc (crc, (byte )string[i]);
  968.  
  969.     t1 = timerset (6000);
  970.     timeout = t1;
  971.     to_row = 8;
  972.  
  973.     for (;;) {
  974.       CLEAR_INBOUND ();
  975.       while (TIMED_READ (1) != -1)
  976.          ;
  977.       m_print2 ("**EMSI_DAT%04X%s", strlen (string), string);
  978.         UNBUFFER_BYTES ();
  979.         CLEAR_INBOUND ();
  980.       while (TIMED_READ (1) != -1)
  981.          ;
  982. //      status_line("!EMSI: sending DAT packet");
  983.         m_print2 ("%04X\r", crc);
  984.         FLUSH_OUTPUT ();
  985.  
  986.         if (tries++ > 10 || !CARRIER) {
  987.             modem_hangup ();
  988.             status_line ("!Trans. EMSI failure");
  989.             return (0);
  990.         }
  991.  
  992.         t2 = timerset (1000);
  993.  
  994.         while (!timeup (t1) && CARRIER) {
  995.             if (timeup (t2))
  996.                 break;
  997.  
  998.             if (esc_pressed ())
  999.                 return (0);
  1000.             if (PEEKBYTE() == '*') {
  1001.                 get_emsi_id (addr, 10);
  1002. //            status_line("!sendemsi: %s",addr);
  1003. //                if (!strnicmp (addr, "**EMSI_REQ", 10) || !strnicmp (addr, "*EMSI_REQ", 9))
  1004.                 if(strstr (addr, "EMSI_REQ"))
  1005.                     continue;
  1006. //                if (!strnicmp (addr, "**EMSI_DAT", 10) || !strnicmp (addr, "*EMSI_DAT", 9)) {
  1007.                 if (strstr (addr, "EMSI_DAT")) {
  1008. //            status_line("!sendemsi: DAT %s",addr);
  1009.                     CLEAR_INBOUND ();
  1010.                     m_print2 ("**EMSI_ACKA490\r");
  1011.                     break;
  1012.                 }
  1013. //                if (!strnicmp (addr, "**EMSI_ACKA490", 14) || !strnicmp (addr, "*EMSI_ACKA490", 13))
  1014.                 if (strstr (addr, "EMSI_ACK"))
  1015.                     break;
  1016. //                if (!strnicmp (addr, "**EMSI_NAKEEC3", 14) || !strnicmp (addr, "*EMSI_NAKEEC3", 13))
  1017.                 if (strstr (addr, "EMSI_NAK"))
  1018.                     break;
  1019.  
  1020.             }
  1021.             else if (PEEKBYTE () != -1)
  1022.             TIMED_READ(1);
  1023.          else {
  1024.             time_release ();
  1025.             if (local_kbd == 0x1B) {
  1026.                modem_hangup ();
  1027.                status_line ("!Trans. EMSI failure - ESC pressed");
  1028.                return (0);
  1029.             }
  1030.          }
  1031.         }
  1032.  
  1033. //        if (!strnicmp (addr, "**EMSI_ACKA490", 14) || !strnicmp (addr, "*EMSI_ACKA490", 13))
  1034.         if (strstr (addr, "EMSI_ACK")){
  1035. //         status_line("!send EMSI: ACK received");
  1036.             break;
  1037.         }
  1038.     }
  1039.     return (1);
  1040. }
  1041.  
  1042. int get_emsi_handshake (originator)
  1043. int originator;
  1044. {
  1045.     int i, m, tries, zz, no, ne, pp;
  1046.     char string[2048], *p, *a, version[40], akas[600], *from, *phone, *flags;
  1047.     char password[50], got_ident, knowaka, *got;
  1048.     long tranx, t1, t2;
  1049.     struct tm *tim;
  1050.     struct date da;
  1051.     struct time ti;
  1052.     unsigned short crc,remote_crc;
  1053.  
  1054.     tries = 0;
  1055.     t1 = timerset (1000);
  1056.     t2 = timerset (6000);
  1057.     emsi_data_packet = NULL;
  1058.  
  1059. resend:
  1060. //   status_line("*EMSI tries: %d",tries);
  1061.     if (tries++ > 6) {
  1062.         modem_hangup ();
  1063.         status_line ("!EMSI Error: too may tries");
  1064.         if(emsi_data_packet)
  1065.             free(emsi_data_packet);
  1066.         return (0);
  1067.     }
  1068.  
  1069.     if (!originator) {
  1070.         m_print2 ("**EMSI_REQA77E\r");
  1071. //      status_line("!sending EMSI_REQ");
  1072.         t1 = timerset (1000);
  1073.     }
  1074.     else {
  1075.         if (tries > 1) {
  1076.             m_print2 ("**EMSI_NAKEEC3\r");
  1077. //         status_line("!sending EMSI_NAK");
  1078.             t1 = timerset (1000);
  1079.         }
  1080.     }
  1081.  
  1082.     while (CARRIER && !timeup (t1) && !timeup (t2)) {
  1083.         if (esc_pressed ()) {
  1084.             if(emsi_data_packet)
  1085.                 free(emsi_data_packet);
  1086.             return (0);
  1087.         }
  1088.  
  1089.         if (PEEKBYTE () == '*') {
  1090.             get_emsi_id (string, 10);
  1091. //         status_line("!getemsi: %s",string);
  1092. //            if (!strncmp (string, "**EMSI_DAT", 10) || !strncmp (string, "*EMSI_DAT", 9))
  1093.             if (strstr (string, "EMSI_DAT")) {
  1094.                 break;
  1095.                 }
  1096. //            if (!strncmp (string, "**EMSI_HBT", 10) || !strncmp (string, "**EMSI_ACK", 10))
  1097.             if (strstr (string, "EMSI_HBT")){
  1098. //            status_line("!EMSI: got EMSI_HBT");
  1099.                 t1 = timerset (1000);
  1100.             }
  1101. //            if (!strncmp (string, "**EMSI_IRQ8E08", 10) || !strncmp (string, "*EMSI_IRQ8E08", 9))
  1102.             if (strstr (string, "EMSI_IRQ"))
  1103.                 continue;
  1104.             if (strstr (string, "EMSI_ACK"))
  1105.                 continue;
  1106.         }
  1107.         else if (PEEKBYTE () != -1)
  1108.             TIMED_READ(10);
  1109.         else {
  1110.             time_release ();
  1111.             if (local_kbd == 0x1B) {
  1112.                 modem_hangup ();
  1113.                 status_line ("!Recv. EMSI failure");
  1114.                 if(emsi_data_packet)
  1115.                     free(emsi_data_packet);
  1116.                 return (0);
  1117.             }
  1118.         }
  1119.     }
  1120.  
  1121.     if (timeup (t2)) {
  1122.         status_line ("!EMSI Error: %s", "timeout");
  1123.         modem_hangup ();
  1124.         if(emsi_data_packet)
  1125.             free(emsi_data_packet);
  1126.         return (0);
  1127.     }
  1128.     else if (!CARRIER) {
  1129.         status_line ("!EMSI Error: %s", "other end hung up");
  1130.         modem_hangup ();
  1131.         if(emsi_data_packet)
  1132.             free(emsi_data_packet);
  1133.         return (0);
  1134.     }
  1135.  
  1136.     if (timeup (t1))
  1137.         goto resend;
  1138.  
  1139.     if (originator){
  1140.         prints (7, 65, YELLOW|_BLACK, "EMSI/C2");
  1141. //      status_line("!EMSI/C2");
  1142.     }
  1143.     else{
  1144.         prints (7, 65, YELLOW|_BLACK, "EMSI/C1");
  1145. //      status_line("!EMSI/C1");
  1146.     }
  1147.  
  1148.     if(!emsi_data_packet)
  1149.         emsi_data_packet = (char *) malloc (2048);
  1150.  
  1151.     if(!(remote_crc = get_emsi_data(emsi_data_packet))&&!emsi_data_packet[0]) {
  1152.  
  1153.         if (esc_pressed ()) {
  1154.             if(emsi_data_packet)
  1155.                 free(emsi_data_packet);
  1156.             modem_hangup();
  1157.             status_line("!EMSI: Aborted by operator");
  1158.             return (0);
  1159.         }
  1160.  
  1161.         if (!CARRIER) {
  1162.             status_line ("!EMSI Error: %s", "other end hung up");
  1163.             modem_hangup ();
  1164.             if(emsi_data_packet)
  1165.                 free(emsi_data_packet);
  1166.             return (0);
  1167.         }
  1168.         else
  1169.             goto resend;
  1170.     }
  1171.  
  1172. //    if(!originator)                    // Send HBT during emsi_dat processing
  1173. //        m_print2 ("**EMSI_HBTEAEE\r");  // If we are the receiving system.
  1174.  
  1175.     data_pointer = 0;
  1176.  
  1177.     sprintf (string, "EMSI_DAT%04X", data_lenght);
  1178.     for (i = crc = 0; i < strlen (string); i++)
  1179.         crc = xcrc (crc, (byte )string[i]);
  1180.  
  1181.  
  1182.     for (i = 0; i < strlen (emsi_data_packet); i++)
  1183.         crc = xcrc (crc, (byte )emsi_data_packet[i]);
  1184.  
  1185.  
  1186.     {
  1187.         FILE *fpaux,*fptest;
  1188.         fptest= fopen("EMSIDUMP.DBG","rt");
  1189.         if(fptest) {
  1190.             fclose (fptest);
  1191.             fpaux = fopen("EMSIDUMP.LOG","ab+");
  1192.             fputs (emsi_data_packet,fpaux);
  1193.             fprintf(fpaux,"\n-------\n");
  1194.             fclose (fpaux);
  1195.         }
  1196.     }
  1197.  
  1198.     if (crc != remote_crc) {
  1199.         status_line("! Bad EMSI DAT crc!");
  1200.         status_line("! Remote crc: %0X ours: %0X",remote_crc,crc);
  1201.         goto resend;
  1202.     }
  1203.  
  1204.     if (!get_emsi_field2 (string))      /* fingerprint,            "EMSI"    */
  1205.         goto resend;
  1206.  
  1207.     if (stricmp (string, "EMSI"))
  1208.         goto resend;
  1209.  
  1210.     if (!get_emsi_field2 (string))      /* system_address_list,              */
  1211.         goto resend;
  1212.  
  1213.     memset ((char *)&addrs, 0, sizeof (struct _alias) * MAX_EMSI_ADDR + 1);
  1214.  
  1215.     if (strchr (string, ' ')) {
  1216.         i = 0;
  1217.         a = string;
  1218.         while ((p = strchr (a, ' ')) != NULL) {
  1219.             *p++ = '\0';
  1220.             if (i >= MAX_EMSI_ADDR)
  1221.                 break;
  1222.             if (!i) {
  1223.                 strncpy (akas, p, 399);
  1224.                 akas[399] = '\0';
  1225.             }
  1226.             parse_netnode (a, (int *)&addrs[i].zone, (int *)&addrs[i].net, (int *)&addrs[i].node, (int *)&addrs[i].point);
  1227.             i++;
  1228.             a = p;
  1229.         }
  1230.  
  1231.         if (strlen (a) && i < MAX_EMSI_ADDR)
  1232.             parse_netnode (a, (int *)&addrs[i].zone, (int *)&addrs[i].net, (int *)&addrs[i].node, (int *)&addrs[i].point);
  1233.     }
  1234.     else {
  1235.         akas[0] = '\0';
  1236.         parse_netnode (string, (int *)&addrs[0].zone, (int *)&addrs[0].net, (int *)&addrs[0].node, (int *)&addrs[0].point);
  1237.     }
  1238.  
  1239.     if (!get_emsi_field2 (string))     /* password,                         */
  1240.         goto resend;
  1241.     if (strlen (string) > 49)
  1242.         string[49] = '\0';
  1243.     strcpy (password, string);
  1244.  
  1245.     if (!get_emsi_field2 (string))     /* link_codes,                       */
  1246.         goto resend;
  1247.  
  1248.     if (!get_emsi_field2 (string))     /* compatibility_codes,              */
  1249.         goto resend;
  1250.  
  1251.     remote_capabilities = ZED_ZIPPER;
  1252.     if (strstr (string, "ZAP"))
  1253.         remote_capabilities |= ZED_ZAPPER;
  1254.     if (strstr (string, "JAN"))
  1255.         remote_capabilities |= DOES_IANUS;
  1256.      if (!strstr (string, "NRQ"))
  1257.         remote_capabilities |= WZ_FREQ;
  1258.  
  1259.     if (!get_emsi_field2 (string))      /* mailer_product_code,              */
  1260.         goto resend;
  1261.  
  1262.     if (!get_emsi_field2 (string))     /* mailer_name,                      */
  1263.         goto resend;
  1264.  
  1265.     if (!get_emsi_field2 (version))      /* mailer_version,                   */
  1266.         goto resend;
  1267.     sprintf (remote_program, "%s Version %s", string, version);
  1268.  
  1269.     if (!get_emsi_field2 (string))     /* mailer_serial_number:    ASCII1;  */
  1270.         goto resend;
  1271.  
  1272.     if (string[0] && (strlen (remote_program) + strlen (string) < 58)) {
  1273.         strcat (remote_program, "/");
  1274.         strcat (remote_program, string);
  1275.         if (!strnicmp (remote_program, "LoraBBS", 7) && originator)
  1276.             check_duplicate_key (atoi (string));
  1277.     }
  1278.  
  1279.     tranx = 0L;
  1280.     got_ident = 0;
  1281.     got_maildata = 0;
  1282.  
  1283.     while ( emsi_data_packet[data_pointer] != 0 && data_pointer<data_lenght) {
  1284.  
  1285.         if ( emsi_data_packet[data_pointer] == '{') {
  1286.             if (!get_emsi_field2 (string))
  1287.                 goto resend;
  1288.  
  1289.             if (!strcmp (string, "TRX#")) {
  1290.                 if (!get_emsi_field2 (string))
  1291.                     goto resend;
  1292.                 sscanf (&string[1], "%8lX", &tranx);
  1293. //                tranx -= timezone;
  1294.             }
  1295.             else if (!strcmp (string, "MTX#")) {
  1296.                 if (!get_emsi_field2 (string))
  1297.                     goto resend;
  1298.  
  1299.                 i = 0;
  1300.                 a = string;
  1301.                 if (*a == '[')
  1302.                     a++;
  1303.  
  1304.                 got = a;
  1305.                 while ((p = strchr (a, ']')) != NULL) {
  1306.                     if ( *(p + 1) == '[' || *(p + 1) == '}' || *(p + 1) == '\0' )
  1307.                         *p++ = '\0';
  1308.                     else {
  1309.                         a = ++p;
  1310.                         continue;
  1311.                     }
  1312.  
  1313.                     if (i == 0)
  1314.                         n_mail = atoi (got);
  1315.                     else if (i == 1)
  1316.                         b_mail = atol (got);
  1317.                     else if (i == 2)
  1318.                         n_data = atoi (got);
  1319.                     else if (i == 3)
  1320.                         b_data = atol (got);
  1321.  
  1322.                     if (*p == '[')
  1323.                         p++;
  1324.                     a = p;
  1325.                     got = a;
  1326.                     i++;
  1327.                 }
  1328.  
  1329.                 got_maildata = 1;
  1330.             }
  1331.             else if (!strcmp (string, "IDENT")) {
  1332.                 if (!get_emsi_field2 (string))
  1333.                     goto resend;
  1334.  
  1335.                 i = 0;
  1336.                 a = string;
  1337.                 if (*a == '[')
  1338.                     a++;
  1339.  
  1340.                 got = a;
  1341.                 while ((p = strchr (a, ']')) != NULL) {
  1342.                     if ( *(p + 1) == '[' || *(p + 1) == '}' || *(p + 1) == '\0' )
  1343.                         *p++ = '\0';
  1344.                     else {
  1345.                         a = ++p;
  1346.                         continue;
  1347.                     }
  1348.  
  1349.                     if (i == 0) {
  1350.                         if (strlen (got) > 49)
  1351.                             got[49] = '\0';
  1352.                         strcpy(remote_system, got);
  1353.                     }
  1354.                     else if (i == 1) {
  1355.                         from = got;
  1356.                         if (strlen (got) > 39)
  1357.                             got[39] = '\0';
  1358.                         strcpy (remote_location, from);
  1359.                     }
  1360.                     else if (i == 2) {
  1361.                         if (strlen (got) > 35)
  1362.                             got[35] = '\0';
  1363.                         strcpy(remote_sysop, got);
  1364.                     }
  1365.                     else if (i == 3){
  1366.                         phone = got;
  1367.                         }
  1368.                     else if (i == 4);
  1369.                     else if (i == 5){
  1370.                         flags = got;
  1371.                         }
  1372.                     else if (i == 6)
  1373.                         break;
  1374.  
  1375.                     if (*p == '[')
  1376.                         p++;
  1377.                     a = p;
  1378.                     got = a;
  1379.                     i++;
  1380.                 }
  1381.  
  1382.                 got_ident = 1;
  1383.             }
  1384.         }
  1385.  
  1386.         time_release ();
  1387.     }
  1388.     if (!got_ident)
  1389.         goto resend;
  1390.  
  1391.     CLEAR_INBOUND ();
  1392.     m_print2 ("**EMSI_ACKA490\r");
  1393.     m_print2 ("**EMSI_ACKA490\r");
  1394.  
  1395.     remote_zone = addrs[0].zone;
  1396.     remote_net = addrs[0].net;
  1397.     remote_node = addrs[0].node;
  1398.     remote_point = addrs[0].point;
  1399.  
  1400.     status_line("*%s (%u:%u/%u.%u)", remote_system, addrs[0].zone, addrs[0].net, addrs[0].node, addrs[0].point);
  1401.     if (akas[0])
  1402.         status_line(":Aka: %s", akas);
  1403.     status_line(msgtxt[M_REMOTE_USES], remote_program);
  1404.     status_line(":Sysop: %s from %s", remote_sysop, remote_location);
  1405.     status_line(" Phone: %s", phone);
  1406.     if (strlen (flags))
  1407.         status_line(" Flags: %s", flags);
  1408.  
  1409.     strcpy (string, "");
  1410.     if (remote_capabilities & WZ_FREQ)
  1411.         strcat (string, "FReqs ");
  1412.     if (remote_capabilities & ZED_ZIPPER)
  1413.         strcat (string, "ZedZIP ");
  1414.     if (remote_capabilities & ZED_ZAPPER)
  1415.         strcat (string, "ZedZAP ");
  1416.     if (remote_capabilities & DOES_IANUS)
  1417.         strcat (string, "Janus ");
  1418.     status_line (":Offer: %s", string);
  1419.  
  1420. //   remote_capabilities &= ~DOES_IANUS;
  1421.     if (remote_capabilities & ZED_ZAPPER)
  1422.         remote_capabilities &= ~ZED_ZIPPER;
  1423.  
  1424.     if (tranx)
  1425.         status_line (":Tranx: %08lX / %08lX", time (NULL), tranx);
  1426.     else
  1427.         status_line (":No transaction number presented");
  1428.  
  1429.     if (!originator) {
  1430.         assumed = -1;
  1431.  
  1432.         for (m = 0; m < MAX_EMSI_ADDR; m++) {
  1433.             if (!addrs[m].net)
  1434.                 break;
  1435.  
  1436.             remote_zone = addrs[m].zone;
  1437.             remote_net = addrs[m].net;
  1438.             remote_node = addrs[m].node;
  1439.             remote_point = addrs[m].point;
  1440.  
  1441.             if (remote_point) {
  1442.                 for (i = 0; i < MAX_ALIAS; i++)
  1443.                     if ( config->alias[i].net == remote_net && config->alias[i].node == remote_node && (!remote_zone || config->alias[i].zone == remote_zone) )
  1444.                         break;
  1445.  
  1446.                 if (i < MAX_ALIAS)
  1447.                     assumed = i;
  1448.             }
  1449.             else {
  1450.                 for (i = 0; i < MAX_ALIAS; i++)
  1451.                     if ( config->alias[i].fakenet == remote_net && (!remote_zone || config->alias[i].zone == remote_zone) )
  1452.                         break;
  1453.  
  1454.                 if (i < MAX_ALIAS)
  1455.                     assumed = i;
  1456.             }
  1457.  
  1458.             if (assumed != -1)
  1459.                 break;
  1460.         }
  1461.  
  1462.         if (assumed == -1) {
  1463.             assumed = 0;
  1464.             remote_zone = addrs[0].zone;
  1465.             remote_net = addrs[0].net;
  1466.             remote_node = addrs[0].node;
  1467.             remote_point = addrs[0].point;
  1468.  
  1469.             for (i = 0; i < MAX_ALIAS; i++)
  1470.                 if (remote_zone && config->alias[i].zone == remote_zone)
  1471.                     break;
  1472.  
  1473.             if (i < MAX_ALIAS)
  1474.                 assumed = i;
  1475.         }
  1476.     }
  1477.  
  1478.     knowaka = -1;
  1479.  
  1480.     for (i = 0; i < MAX_EMSI_ADDR; i++) {
  1481.         if (!addrs[i].net)
  1482.             break;
  1483.  
  1484.         remote_zone = addrs[i].zone;
  1485.         remote_net = addrs[i].net;
  1486.         remote_node = addrs[i].node;
  1487.         remote_point = addrs[i].point;
  1488.  
  1489.         if (n_getpassword (remote_zone, remote_net, remote_node, remote_point)) {
  1490.             if (knowaka == -1)
  1491.                 knowaka = i;
  1492.             if (!n_password2 (remote_password, password))
  1493.                 break;
  1494.         }
  1495.         else if (remote_point && remote_net == config->alias[assumed].net && remote_node == config->alias[assumed].node ) {
  1496.             if (n_getpassword(config->alias[assumed].zone, config->alias[assumed].fakenet, remote_point, 0)) {
  1497.                 if (knowaka == -1)
  1498.                     knowaka = i;
  1499.                 if (!n_password2(remote_password,password))
  1500.                     break;
  1501.             }
  1502.         }
  1503.     }
  1504.  
  1505.     if (addrs[i].net)
  1506.         knowaka = i;
  1507.  
  1508.     if (knowaka != -1) {
  1509.         remote_zone = addrs[knowaka].zone;
  1510.         remote_net = addrs[knowaka].net;
  1511.         remote_node = addrs[knowaka].node;
  1512.         remote_point = addrs[knowaka].point;
  1513.     }
  1514.     else {
  1515.         remote_zone = addrs[0].zone;
  1516.         remote_net = addrs[0].net;
  1517.         remote_node = addrs[0].node;
  1518.         remote_point = addrs[0].point;
  1519.     }
  1520.  
  1521.     if (n_getpassword (remote_zone, remote_net, remote_node, remote_point)) {
  1522.         if(n_password (remote_password, password)) {
  1523.             if (!originator) {
  1524.                 modem_hangup ();
  1525.                 if(emsi_data_packet)
  1526.                     free(emsi_data_packet);
  1527.                 return (0);
  1528.             }
  1529.             else
  1530.                 status_line (msgtxt[M_PASSWORD_OVERRIDE]);
  1531.         }
  1532.     }
  1533.     else if (remote_point && remote_net == config->alias[assumed].net && remote_node == config->alias[assumed].node) {
  1534.         if (n_getpassword (config->alias[assumed].zone, config->alias[assumed].fakenet, remote_point, 0)) {
  1535.             if (n_password (remote_password, password)) {
  1536.                 if (!originator) {
  1537.                     modem_hangup ();
  1538.                     if(emsi_data_packet)
  1539.                         free(emsi_data_packet);
  1540.                     return (0);
  1541.                 }
  1542.                 else
  1543.                     status_line (msgtxt[M_PASSWORD_OVERRIDE]);
  1544.             }
  1545.         }
  1546.     }
  1547.  
  1548.     if (tranx) {
  1549.         tim = localtime (&tranx);
  1550.         da.da_year = tim->tm_year;
  1551.         da.da_day = tim->tm_mday;
  1552.         da.da_mon = tim->tm_mon;
  1553.         ti.ti_min = tim->tm_min;
  1554.         ti.ti_hour = tim->tm_hour;
  1555.         ti.ti_sec = tim->tm_sec;
  1556.         ti.ti_hund = 0;
  1557.  
  1558.         status_line ("+Remote clock: %2d-%02d-%02d %02d:%02d:%02d", da.da_day, da.da_mon + 1, da.da_year % 100, ti.ti_hour, ti.ti_min, ti.ti_sec);
  1559.  
  1560.         ne = config->alias[0].net;
  1561.         no = config->alias[0].node;
  1562.         pp = config->alias[0].point;
  1563.         zz = config->alias[0].zone;
  1564.  
  1565.         strcpy (string, config->resync_nodes);
  1566.         if ((p = strtok (string, " ")) != NULL)
  1567.             do {
  1568.                 parse_netnode2 (p, &zz, &ne, &no, &pp);
  1569.                 for (m = 0; m < MAX_EMSI_ADDR; m++) {
  1570.                     if (!addrs[m].net)
  1571.                         break;
  1572.                     if (addrs[m].net == ne && addrs[m].node == no && addrs[m].zone == zz && addrs[m].point == pp)
  1573.                         break;
  1574.                 }
  1575.  
  1576.                 if (m < MAX_EMSI_ADDR && addrs[m].net)
  1577.                     break;
  1578.             } while ((p = strtok (NULL, " ")) != NULL);
  1579.  
  1580.         if (p != NULL) {
  1581.             elapsed += tranx - time (NULL);
  1582.  
  1583.             status_line ("+Resync clock with %d:%d/%d.%d", addrs[m].zone, addrs[m].net, addrs[m].node, addrs[m].point);
  1584.             setdate (&da);
  1585.             settime (&ti);
  1586.         }
  1587.     }
  1588.  
  1589. /*
  1590.     if (cur_event >= 0 && (e_ptrs[cur_event]->behavior & MAT_RESERV) && !originator) {
  1591.         for (m = 0; m < MAX_EMSI_ADDR; m++) {
  1592.             if (!addrs[m].net)
  1593.                 break;
  1594.             if (addrs[m].point == e_ptrs[cur_event]->res_point &&
  1595.                  addrs[m].net == e_ptrs[cur_event]->res_net &&
  1596.                  addrs[m].node == e_ptrs[cur_event]->res_node &&
  1597.                  addrs[m].zone == e_ptrs[cur_event]->res_zone && addrs[m].zone && e_ptrs[cur_event]->res_zone)
  1598.                 break;
  1599.         }
  1600.  
  1601.         if (!addrs[m].net) {
  1602.             status_line ("!Node not allowed in this slot");
  1603.             modem_hangup ();
  1604.             if(emsi_data_packet)
  1605.                 free(emsi_data_packet);
  1606.             return (0);
  1607.         }
  1608.     }
  1609. */
  1610.     if(emsi_data_packet)
  1611.             free(emsi_data_packet);
  1612.     return (1);
  1613. }
  1614.  
  1615.  
  1616.  
  1617. unsigned short get_emsi_data (char *data)
  1618. {
  1619.  
  1620.     short c,i;
  1621.     unsigned short value=0;
  1622.     char c1;
  1623.     long t1;
  1624.  
  1625.     data_lenght=0;
  1626.  
  1627.     t1 = timerset(2000);
  1628.  
  1629.     for(i=0;i<4;i++) {
  1630.  
  1631.         if(timeup(t1)) {
  1632.             status_line("!EMSI_DAT timeout!");
  1633.             data[0]=0;
  1634.             return(0);
  1635.         }
  1636.  
  1637.         if((c = TIMED_READ(10))==-1) {
  1638. //         status_line("!EMSI lenght data timeout");
  1639.             data[0]=0;
  1640.             return (0);
  1641.         }
  1642.         if (local_kbd == 0x1B) {
  1643.             data[0]=0;
  1644.             return (0);
  1645.         }
  1646.         value <<= 4 ;
  1647.         c1 = toupper ((char) c);
  1648.         value += (c1 >= 'A') ? (c1 - 55) : (c1 - '0');
  1649.     }
  1650.  
  1651.     if(value>2048) {
  1652.         status_line("! EMSI DAT packet too long: %d bytes",value);
  1653.         data[0]=0;
  1654.         return(0);
  1655.     }
  1656.  
  1657.     data_lenght=value;
  1658.  
  1659.     for(i=0;i<value;i++) {
  1660.  
  1661.         if(timeup(t1)) {
  1662. //         status_line("!EMSI_DAT timeout!");
  1663.             data[0]=0;
  1664.             return(0);
  1665.         }
  1666.  
  1667.         if((c = TIMED_READ(10))==-1) {
  1668. //         status_line("!EMSI data timeout");
  1669.             data[0]=0;
  1670.             return (0);
  1671.         }
  1672.         if (local_kbd == 0x1B) {
  1673.             data[0]=0;
  1674.             return (0);
  1675.         }
  1676.         data[i] = c;
  1677.     }
  1678.     data[i] = 0;
  1679.  
  1680.     value=0;
  1681.     for(i=0;i<4;i++) {
  1682.  
  1683.         if(timeup(t1)) {
  1684. //         status_line("!EMSI_DAT timeout!");
  1685.             data[0]=0;
  1686.             return(0);
  1687.         }
  1688.  
  1689.         if((c = TIMED_READ(10))==-1) {
  1690.             data[0]=0;
  1691. //         status_line("!EMSI DAT crc timeout");
  1692.             return (0);
  1693.         }
  1694.         if (local_kbd == 0x1B) {
  1695.             data[0]=0;
  1696.             return (0);
  1697.         }
  1698.         value <<= 4 ;
  1699.         c1 = toupper ((char) c);
  1700.         value += (c1 >= 'A') ? (c1 - 55) : (c1 - '0');
  1701.     }
  1702. //   if(TIMED_READ(10)!='\r')
  1703. //      status_line("DEMSI_DAT closing <CR> missing");
  1704.  
  1705.     return(value);
  1706. }
  1707.  
  1708.  
  1709. int get_emsi_field2 (char *s)
  1710. {
  1711.     int i = 0, start = 0, value;
  1712.     char c;
  1713.  
  1714.  
  1715.     while (data_pointer < data_lenght) {
  1716.  
  1717.         c = emsi_data_packet[data_pointer++];
  1718.  
  1719.         if (!start && c != '{')
  1720.             continue;
  1721.  
  1722.         if (c == '{' && !start) {
  1723.             start = 1;
  1724.             continue;
  1725.         }
  1726.  
  1727.         if (c == '}' && start) {
  1728.             if (emsi_data_packet[data_pointer] != '}')
  1729.                 break;
  1730.             else
  1731.                 c = emsi_data_packet[data_pointer++];
  1732.         }
  1733.  
  1734.         if (c == ']') {
  1735.             if (emsi_data_packet[data_pointer] == ']')
  1736.                 c = emsi_data_packet[data_pointer++];
  1737.         }
  1738.  
  1739.         if (c == '\\') {
  1740.             if ((c = emsi_data_packet[data_pointer++]) != '\\') {
  1741.                 c = toupper (c);
  1742.                 value = (c >= 'A') ? (c - 55) : (c - '0');
  1743.                 value *= 16;
  1744.                 c = emsi_data_packet[data_pointer++];
  1745.                 c = toupper (c);
  1746.                 value += (c >= 'A') ? (c - 55) : (c - '0');
  1747.                 c = (char)value;
  1748.             }
  1749.         }
  1750.  
  1751.         s[i++] = c;
  1752.     }
  1753.  
  1754.     s[i] = '\0';
  1755.     return (1);
  1756. }
  1757.