home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BSRC_250.LZH / RECVSYNC.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  23KB  |  549 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Vince Perriello                 */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*          FidoNet(R) Mail Session Called System Synchronization           */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*                                                                          */
  45. /*--------------------------------------------------------------------------*/
  46.  
  47.  
  48.  /*
  49.     Receiver Synchronization state table.
  50.    
  51.     This logic is used by the called system. It will determine the
  52.     type of mail transfer which can be used in communicating with
  53.     the calling system.
  54.    
  55.     This stuff was originally copied from a file sent to us by TJ.
  56.     (Of course, then we hacked the heck out of it!!!)
  57.   
  58.     Thanks, Tom.
  59.   
  60.  
  61.  .-----+----------+-------------------------+-------------------------+-----.
  62.  | RS0 | SyncInit |                         | Start 5 second idle tmr | RS1 |
  63.  |-----+----------+-------------------------+-------------------------+-----|
  64.  | RS1 | IdleWait | 1. 5 sec tmr expired    | Take the initiative     | RS2 |
  65.  |     |          +-------------------------+-------------------------+-----|
  66.  |     |          | 2. Carrier lost         | Session aborted         | exit|
  67.  |     |          +-------------------------+-------------------------+-----|
  68.  |     |          | 3. Peek = YOOHOO        | Looks like a live WaZOO | RS3 |
  69.  |     |          +-------------------------+-------------------------+-----|
  70.  |     |          | 4. Peek = TSYNC         | Live FSC001, we think   | RS3 |
  71.  |     |          +-------------------------+-------------------------+-----|
  72.  |     |          | 5. Peek = CR, LF, space | He looks alive          | RS2 |
  73.  |     |          +-------------------------+-------------------------+-----|
  74.  |     |          | 6. Other character      | Eat it                  | RS1 |
  75.  |-----+----------+-------------------------+-------------------------+-----|
  76.  | RS2 |SendBanner| 1. Error returned       | Session aborted         | exit|
  77.  |     |          +-------------------------+-------------------------+-----|
  78.  |     |          | 2. Banner sent OK       |                         | RS3 |
  79.  |-----+----------+-------------------------+-------------------------+-----|
  80.  | RS3 |RecvInit  |                         | Init ext-mail scan ptrs | RS4 |
  81.  |     |          |                         | Start 20 sec timer      |     |
  82.  |     |          |                         | Init 10 sec timer       |     |
  83.  |-----+----------+-------------------------+-------------------------+-----|
  84.  | RS4 |SendSync  | 1. Error returned       | Session aborted         | exit|
  85.  |     |(xmit sync+-------------------------+-------------------------+-----|
  86.  |     |string)   | 2. String sent OK       | Watch for sender sync   | RS5 |
  87.  |-----+----------+-------------------------+-------------------------+-----|
  88.  | RS5 | WaitSync | 1. Carrier lost         | Session aborted         | exit|
  89.  |     |          +-------------------------+-------------------------+-----|
  90.  |     |          | 2. YOOHOO received      | WaZOO session selected  | exit|
  91.  |     |          +-------------------------+-------------------------+-----|
  92.  |     |          | 3. TSYNC received       | probable FSC001         | RS6 |
  93.  |     |          +-------------------------+-------------------------+-----|
  94.  |     |          | 4. CR received          | Still sync'ing          | RS4 |
  95.  |     |          +-------------------------+-------------------------+-----|
  96.  |     |          | 5. ESC received         | BBS session             | exit|
  97.  |     |          +-------------------------+-------------------------+-----|
  98.  |     |          | 6. Other character rcvd | check ext-mail string   | RS7 |
  99.  |     |          +-------------------------+-------------------------+-----|
  100.  |     |          | 7. 10 sec timer elapsed | FSC001 protocol selected| exit|
  101.  |     |          +-------------------------+-------------------------+-----|
  102.  |     |          | 8. 20 sec timer elapsed | Not a mail session      | exit|
  103.  |-----+----------+-------------------------+-------------------------+-----|
  104.  | RS6 | TsyncTmr | 1. Timer not running    | Start 10 second timer   | RS5 |
  105.  |     |          |                         | Reset 20 sec timer      |     |
  106.  |     |          +-------------------------+-------------------------+-----|
  107.  |     |          | 2. Timer running        | Two TSYNCS = FTS-0001   | exit|
  108.  |-----+----------+-------------------------+-------------------------+-----|
  109.  | RS7 | ExtMail  | 1. String matched       | External mail selected  | exit|
  110.  |     |          +-------------------------+-------------------------+-----|
  111.  |     |          | 2. Checked all strings  | Get next input character| RS5 |
  112.  `-----+----------+-------------------------+-------------------------+-----'
  113.  
  114.  */
  115.  
  116.  
  117. /* Include this file before any other includes or defines! */
  118.  
  119. #include "includes.h"
  120.  
  121. /*
  122.  * Data structure used by all RecvSync state machine functions.
  123.  * Contains all data which needs to be passed between various states.
  124.  *
  125.  */
  126.  
  127. typedef struct {
  128.    long Idle_Timer;                     /* 5 second initial idle timer      */
  129.    long BBS_Timer;                      /* 20 second Timer for BBS user     */
  130.    char *ExtMailScan[16];               /* Scan pointer for External Mail   */
  131.    long TSYNC_Timer;                    /* 10 second TSYNC timer            */
  132.    int  result;                         /* Result we want to send out       */
  133. } RSARGS, *RSARGSP;
  134.  
  135. int RSSyncInit (RSARGSP, int);          /* Called by state machine at start */
  136. int RSExit     (RSARGSP, int);          /* Called by state machine at end   */
  137. int RSIdleWait (RSARGSP);               /* RS1 state processing function    */
  138. int RSSendBannr(RSARGSP);               /* RS2 state processing function    */
  139. int RSRecvInit (RSARGSP);               /* RS3 state processing function    */
  140. int RSSendSync (RSARGSP);               /* RS4 state processing function    */
  141. int RSWaitSync (RSARGSP);               /* RS5 state processing function    */
  142. int RSTsyncTmr (RSARGSP);               /* RS6 state processing function    */
  143. int RSExtMail  (RSARGSP);               /* RS7 state processing function    */
  144.  
  145.  
  146. #define RS0    0                        /* Reserved value of 0 for init     */
  147. #define RSexit 0                        /* Slot 1 is exit, but called by 0  */
  148. #define RS1    2                        /* First "user" slot is 2.          */
  149. #define RS2    3                        /* After that, it all maps n : n+1  */
  150. #define RS3    4
  151. #define RS4    5
  152. #define RS5    6
  153. #define RS6    7
  154. #define RS7    8
  155.  
  156.  
  157. STATES Recv_Sync [] = {                 /* Table used by state machine      */
  158.    { "RSSyncInit", RSSyncInit },        /* And referred to by 'SSn' defines */
  159.    { "RSExit",     RSExit },            /* listed above ...                 */
  160.    { "RSIdleWait", RSIdleWait },
  161.    { "RSSendBannr",RSSendBannr },
  162.    { "RSRecvInit", RSRecvInit },
  163.    { "RSSendSync", RSSendSync },
  164.    { "RSWaitSync", RSWaitSync },
  165.    { "RSTsyncTmr", RSTsyncTmr },
  166.    { "RSExtMail",  RSExtMail },
  167. };
  168.  
  169.  
  170. /*
  171.  * CalledRecvSync
  172.  * Determine whether the calling system is an FTS-0001 mailer, an FTS-0006
  173.  * mailer, an external mailer, or a human caller. Use the general state 
  174.  * machine driver.
  175.  *
  176.  * This is the only external entry point into this module.
  177.  *
  178.  */
  179.  
  180. int CalledRecvSync (void)
  181. {
  182.    RSARGS args;
  183.    int res;
  184.  
  185.    args.result = 0;
  186.  
  187.    res = state_machine (Recv_Sync, &args, 2);
  188.    return (res);
  189. }
  190.  
  191.  
  192.  
  193. /*
  194.  * This routine is called by the state machine when the 'RSexit'
  195.  * state is seen. Its return value is what the state machine
  196.  * will return to its caller as the result of the function.
  197.  *
  198.  */
  199.  
  200. int RSExit (RSARGSP args, int cur_state)
  201. {
  202.    happy_compiler = cur_state;  /* Makes compiler happy! */
  203.    return (args->result);
  204. }
  205.  
  206.  /*
  207.  
  208.  .-----+----------+-------------------------+-------------------------+-----.
  209.  | RS0 | SyncInit |                         | Start 5 second idle tmr | RS1 |
  210.  `-----+----------+-------------------------+-------------------------+-----'
  211.  
  212.  */
  213.  
  214.  
  215. int RSSyncInit (RSARGSP args, int start_state)
  216. {
  217.    args->Idle_Timer = timerset (500);
  218.    happy_compiler = start_state;  /* Makes compiler happy! */
  219.    return (RS1);
  220. }
  221.  
  222.  /*
  223.  
  224.  .-----+----------+-------------------------+-------------------------+-----.
  225.  | RS1 | IdleWait | 1. 5 sec tmr expired    | Take the initiative     | RS2 |
  226.  |     |          +-------------------------+-------------------------+-----|
  227.  |     |          | 2. Carrier lost         | Session aborted         | exit|
  228.  |     |          +-------------------------+-------------------------+-----|
  229.  |     |          | 3. Peek = YOOHOO        | Looks like a live WaZOO | RS3 |
  230.  |     |          +-------------------------+-------------------------+-----|
  231.  |     |          | 4. Peek = TSYNC         | Live FSC001, we think   | RS3 |
  232.  |     |          +-------------------------+-------------------------+-----|
  233.  |     |          | 5. Peek = CR, LF, space | He looks alive          | RS2 |
  234.  |     |          +-------------------------+-------------------------+-----|
  235.  |     |          | 6. Other character      | Eat it                  | RS1 |
  236.  `-----+----------+-------------------------+-------------------------+-----'
  237.  
  238.  */
  239.  
  240.  
  241. int RSIdleWait (RSARGSP args)
  242. {
  243.    unsigned int i;
  244.    int ret;
  245.  
  246.    while (!timeup (args->Idle_Timer)) {
  247.  
  248.       if (!CARRIER) {
  249.          args->result = 0;
  250.          return (RSexit);
  251.       }
  252.  
  253.       if ((i = (unsigned int) PEEKBYTE ()) == 0xffff)
  254.          continue;
  255.  
  256.       switch (i & 0xff) {
  257.  
  258.       case TSYNC:
  259.       case YOOHOO:
  260.          ret = RS3;                           /* Fast lane, no banner */
  261.          break;
  262.       
  263.       case CR:
  264.       case LV:
  265.       case ' ':
  266.       case ENQ:
  267.       case ESC:
  268.          ret =  RS2;                          /* He's alive, do banner*/
  269.          break;
  270.  
  271.       default:
  272.          (void) TIMED_READ (0);               /* eat it up            */
  273.          ret = RS1;
  274.          break;
  275.       }
  276.       return (ret);
  277.  
  278.    }
  279.    return (RS2);                                 /* Idle for 5 seconds   */
  280. }
  281.  
  282.  /*
  283.  
  284.  .-----+----------+-------------------------+-------------------------+-----.
  285.  | RS2 |SendBanner| 1. Error returned       | Session aborted         | exit|
  286.  |     |          +-------------------------+-------------------------+-----|
  287.  |     |          | 2. Banner sent OK       |                         | RS3 |
  288.  `-----+----------+-------------------------+-------------------------+-----'
  289.  
  290.  */
  291.  
  292.  
  293. int RSSendBannr (RSARGSP args)
  294. {
  295.    char buff[128];
  296.  
  297.    (void) sprintf (buff, MSG_TXT(M_ADDRESS), 
  298.                          Full_Addr_Str (&my_addr),
  299.                          ANNOUNCE,
  300.                          ((serial == -1) ? MSG_TXT(M_UNREGISTERED) : " "));
  301.  
  302.    if (!SendBanner ((char far *)buff)) {
  303.       args->result = 0;
  304.       return (RSexit);
  305.    }
  306.  
  307.    if (BBSbanner != NULL) {                      /* If BBS name spec'ed, */
  308.       SENDBYTE ('\r');                           /* make sure user gets  */
  309.       SENDBYTE ('\n');                           /* a full cr/lf...      */
  310.  
  311.       if (!SendBanner ((char far *)BBSbanner)) {
  312.          args->result = 0;
  313.          return (RSexit);
  314.       }
  315.  
  316.       SENDBYTE ('\r');                           /* make sure user gets  */
  317.       SENDBYTE ('\n');                           /* a full cr/lf...      */
  318.    }
  319.  
  320.    if (!mail_only) {
  321.       if (!SendBanner (BBSwelcome)) {
  322.          args->result = 0;
  323.          return (RSexit);
  324.       }
  325.    }
  326.  
  327.    return (RS3);
  328. }
  329.  
  330.  /*
  331.  
  332.  .-----+----------+-------------------------+-------------------------+-----.
  333.  | RS3 |RecvInit  |                         | Init ext-mail scan ptrs | RS4 |
  334.  |     |          |                         | Start 20 sec timer      |     |
  335.  |     |          |                         | Init 10 sec timer       |     |
  336.  `-----+----------+-------------------------+-------------------------+-----'
  337.  
  338.  */
  339.  
  340.  
  341. int RSRecvInit (RSARGSP args)
  342. {
  343.    register int k;
  344.  
  345.    for (k = 0; k < num_ext_mail; k++) {
  346.       args->ExtMailScan[k] = ext_mail_string[k]; /* UUCP handshake       */
  347.    }
  348.  
  349.    args->BBS_Timer = timerset (BBStimeout);      /* 20 second timeout    */
  350.    args->TSYNC_Timer = 0L;                       /* Initialize for test  */
  351.  
  352.    return (RS4);
  353. }
  354.  
  355.  /*
  356.  
  357.  .-----+----------+-------------------------+-------------------------+-----.
  358.  | RS4 |SendSync  | 1. Error returned       | Session aborted         | exit|
  359.  |     |(xmit sync+-------------------------+-------------------------+-----|
  360.  |     |string)   | 2. String sent OK       | Watch for sender sync   | RS5 |
  361.  `-----+----------+-------------------------+-------------------------+-----'
  362.  
  363.  */
  364.  
  365.  
  366. int RSSendSync (RSARGSP args)
  367. {
  368.    char far *c;
  369.  
  370.    if (mail_only) {                              /* If no BBS allowed,   */
  371.       c = (char far *) noBBS;                    /* tell human to git    */
  372.    } else {
  373.       c = (char far *) BBSesc;                   /* or hit ESC for BBS   */
  374.    }
  375.  
  376.    if (!SendBanner (c)) {
  377.       args->result = 0;
  378.       return (RSexit);
  379.    }
  380.  
  381.    return (RS5);
  382. }
  383.  
  384.  /*
  385.  
  386.  .-----+----------+-------------------------+-------------------------+-----.
  387.  | RS5 | WaitSync | 1. Carrier lost         | Session aborted         | exit|
  388.  |     |          +-------------------------+-------------------------+-----|
  389.  |     |          | 2. YOOHOO received      | WaZOO session selected  | exit|
  390.  |     |          +-------------------------+-------------------------+-----|
  391.  |     |          | 3. TSYNC received       | probable FSC001         | RS6 |
  392.  |     |          +-------------------------+-------------------------+-----|
  393.  |     |          | 4. CR received          | Still sync'ing          | RS4 |
  394.  |     |          +-------------------------+-------------------------+-----|
  395.  |     |          | 5. ESC received         | BBS session             | exit|
  396.  |     |          +-------------------------+-------------------------+-----|
  397.  |     |          | 6. Other character rcvd | check ext-mail string   | RS7 |
  398.  |     |          +-------------------------+-------------------------+-----|
  399.  |     |          | 7. 10 sec timer elapsed | FSC001 protocol selected| exit|
  400.  |     |          +-------------------------+-------------------------+-----|
  401.  |     |          | 8. 20 sec timer elapsed | Not a mail session      | exit|
  402.  `-----+----------+-------------------------+-------------------------+-----'
  403.  
  404.  */
  405.  
  406.  
  407. int RSWaitSync (RSARGSP args)
  408. {
  409.    unsigned int i;
  410.    int ret = -1;
  411.  
  412.    while (CARRIER) {
  413.  
  414.       if (got_ESC ()) {                          /* Manual abort?        */
  415.          LOWER_DTR ();                             /* Yes, drop carrier    */
  416.          timer (10);
  417.          args->result = 0;
  418.          return (RSexit);
  419.       }
  420.  
  421.       /* Note: the following line is a NON-DESTRUCTIVE READ! */
  422.  
  423.       switch ((i = (unsigned int) PEEKBYTE ()) & 0xff) {
  424.  
  425.       case YOOHOO:                            /* Looks like a WaZOO   */
  426.  
  427.          if (no_WaZOO_Session) {              /* If we're not WaZOO,  */
  428.             (void) TIMED_READ (0);            /* Eat the YooHoo       */
  429.             continue;
  430.          }
  431.  
  432.          CLEAR_OUTBOUND ();                   /* End noisy banner now */
  433.          args->result = 3;                    /* WaZOO session        */
  434.          ret = RSexit;
  435.          break;
  436.  
  437.       case TSYNC:                             /* Looks like an FTSC   */
  438.  
  439.          CLEAR_OUTBOUND ();                   /* End noisy banner now */
  440.          (void) TIMED_READ (0);               /* Eat the TSYNC        */
  441.  
  442.          if (!no_WaZOO_Session) {             /* If we support WaZOO, */
  443.             ret = RS6;                        /* Go set up the timer  */
  444.          } else {
  445.             args->result = 2;                 /* FTS-0001 session     */
  446.             ret = RSexit;
  447.          }
  448.  
  449.          break;
  450.  
  451.       case CR:                                /* Still sync'ing       */
  452.  
  453.          (void) TIMED_READ (0);               /* Eat the character    */
  454.          ret = RS4;                           /* Back to that stuff   */
  455.          break;
  456.  
  457.       case ESC:                               /* User wants the BBS   */
  458.  
  459.          (void) TIMED_READ (0);               /* Eat the character    */
  460.          if (!mail_only) {
  461.             args->result = 1;                 /* BBS session          */
  462.             ret = RSexit;
  463.          }
  464.          break;
  465.  
  466.       default:                                /* A less special char  */
  467.          if (i != 0xffff)
  468.             ret = RS7;                        /* Check ext mailers    */
  469.          break;
  470.  
  471.       }
  472.       if (ret >= 0)
  473.          return (ret);
  474.  
  475.       if ((args->TSYNC_Timer != 0L) && (timeup (args->TSYNC_Timer))) {
  476.          args->result = 2;                    /* FTS-0001 session     */
  477.          return (RSexit);
  478.       }
  479.  
  480.       if (timeup (args->BBS_Timer)) {         /* The big timeout      */
  481.          args->result = 1;                    /* BBS session          */
  482.          return (RSexit);
  483.       }
  484.  
  485.       time_release ();                        /* Give up the slice    */
  486.  
  487.    } /* End (while CARRIER) */
  488.  
  489.    args->result = 0;                             /* carrier loss = abort */
  490.    return (RSexit);
  491. }
  492.  
  493.  /*
  494.  
  495.  .-----+----------+-------------------------+-------------------------+-----.
  496.  | RS6 | TsyncTmr | 1. Timer not running    | Start 10 second timer   | RS5 |
  497.  |     |          |                         | Reset 20 sec timer      |     |
  498.  |     |          +-------------------------+-------------------------+-----|
  499.  |     |          | 2. Timer running        | Two TSYNCS = FTS-0001   | exit|
  500.  `-----+----------+-------------------------+-------------------------+-----'
  501.  
  502.  */
  503.  
  504.  
  505. int RSTsyncTmr (RSARGSP args)
  506. {
  507.    if (args->TSYNC_Timer == 0L) {                /* If not already set,  */
  508.       args->TSYNC_Timer = timerset (1000);       /* Then just set it     */
  509.       args->BBS_Timer = timerset (BBStimeout);   /* Reset 20 sec timer   */
  510.       return (RS5);
  511.    }
  512.    args->result = 2;                             /* FTS-0001 session     */
  513.    return (RSexit);
  514. }
  515.  
  516.  /*
  517.  
  518.  .-----+----------+-------------------------+-------------------------+-----.
  519.  | RS7 | ExtMail  | 1. String matched       | External mail selected  | exit|
  520.  |     |          +-------------------------+-------------------------+-----|
  521.  |     |          | 2. Checked all strings  | Get next input character| RS5 |
  522.  `-----+----------+-------------------------+-------------------------+-----'
  523.  
  524.  */
  525.  
  526.  
  527. int RSExtMail (RSARGSP args)
  528. {
  529.    int i, k;
  530.  
  531.    /* Note: last character was read in RS5 using a NON-DESTRUCTIVE READ.
  532.       We now will get it out with a DESTRUCTIVE READ.                     */
  533.  
  534.    i = TIMED_READ (0) & 0xff;                    /* Eat the character     */
  535.  
  536.    for (k = 0; k < num_ext_mail; k++) {
  537.       if (i != (int) *(args->ExtMailScan[k]++)) {    /* Does this match next? */
  538.          args->ExtMailScan[k] = ext_mail_string[k];  /* No, reset pointer */
  539.       } else {
  540.          if (!*args->ExtMailScan[k]) {           /* End of UUCP string?   */
  541.                 args->result = 5 + k;            /* 5 + n for external    */
  542.                 return (RSexit);
  543.          }
  544.       }
  545.    }
  546.  
  547.    return (RS5);
  548. }
  549.