home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BSRC_250.LZH / RECVBARK.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  10KB  |  388 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 Bob Hartman                  */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*               BinkleyTerm "BARK" File request state machine              */
  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. /* Include this file before any other includes or defines! */
  47.  
  48. #include "includes.h"
  49.  
  50. int  get_req_str (char *);
  51. void gen_req_name (char *);
  52. int  cdog_callback (char *);
  53. int  cdog_time (long);
  54.  
  55. int RBInit (BARKARGSP, int);
  56. int RBEnd (BARKARGSP, int);
  57. int RBHonorReq (BARKARGSP);
  58. int RBWaitBark (BARKARGSP);
  59. int RBAckBark (BARKARGSP);
  60. int RBWaitStrt (BARKARGSP);
  61. int RBSendFile (BARKARGSP);
  62.  
  63. STATES Bark_Receiver[] = {
  64.    { "RBInit", RBInit },
  65.    { "RBEnd", RBEnd },
  66.    { "RB0", RBHonorReq },
  67.    { "RB1", RBWaitBark },
  68.    { "RB2", RBAckBark },
  69.    { "RB3", RBWaitStrt },
  70.    { "RB4", RBSendFile }
  71. };
  72.  
  73. int Receive_Bark_Packet (BARKARGSP args)
  74. {
  75.    if (get_req_str (args->barkpacket))
  76.       {
  77.       gen_req_name (args->barkpacket);
  78.       args->barkok = 1;
  79.       return (0);
  80.       }
  81.  
  82.    return (1);
  83. }
  84.  
  85. int RBInit (BARKARGSP args, int start_state)
  86. {
  87.    XON_DISABLE ();
  88.    args->barkok = 0;
  89.    return (start_state);
  90. }
  91.  
  92. int RBEnd (BARKARGSP args, int cur_state)
  93. {
  94.    if (!no_requests)
  95.       {
  96.       status_line (":%s %s %s", MSG_TXT(M_END_OF), MSG_TXT(M_INBOUND), MSG_TXT(M_FILE_REQUESTS));
  97.       }
  98.    happy_compiler = args->nfiles; /* This makes compilers happy! */
  99.    return (cur_state);
  100. }
  101.  
  102. int RBHonorReq (BARKARGSP args)
  103. {
  104.    args->nfiles = 0;
  105.  
  106.    if (!no_requests)
  107.       {
  108.       status_line (":%s %s", MSG_TXT(M_INBOUND), MSG_TXT(M_FILE_REQUESTS));
  109.       SENDBYTE (ENQ);
  110.       args->T1 = timerset (200);
  111.       return (RB1);
  112.       }
  113.    else
  114.       {
  115.       SENDBYTE (CAN);
  116.       status_line (MSG_TXT(M_REFUSING_IN_FREQ));
  117.       return (SUCCESS);
  118.       }
  119. }
  120.  
  121. int RBWaitBark (BARKARGSP args)
  122. {
  123.    int c, ret;
  124.    long RB1Timer;
  125.  
  126.    ret = -1;
  127.    RB1Timer = timerset (2000);
  128.    while (ret < 0)
  129.       {
  130.       while ((c = PEEKBYTE ()) < 0)
  131.          {
  132.          if (!CARRIER)
  133.             return (CARRIER_ERR);
  134.          time_release ();
  135.  
  136.          if (timeup (args->T1))
  137.             {
  138.             break;
  139.             }
  140.          }
  141.  
  142.       if (timeup (RB1Timer))
  143.          {
  144. /* Report error */
  145.          return (SENDBLOCK_ERR);
  146.          }
  147.  
  148.       if (((unsigned int)c == 0xffff) && timeup (args->T1))
  149.          {
  150.          CLEAR_INBOUND ();
  151.          SENDBYTE (ENQ);
  152.          args->T1 = timerset (200);
  153.          continue;
  154.          }
  155.  
  156.       c = TIMED_READ (0);
  157.  
  158.       switch (c)
  159.          {
  160.          case ACK:
  161.             if (Receive_Bark_Packet (args) == 0)
  162.                ret = RB2;
  163.             else
  164.                ret = RB0;
  165.             break;
  166.  
  167.          case ETB:
  168.             ret = SUCCESS;
  169.             break;
  170.  
  171.          case ENQ:
  172.             SENDBYTE (ETB);
  173.             break;
  174.          }
  175.       }
  176.    return (ret);
  177. }
  178.  
  179. int RBAckBark (BARKARGSP args)
  180. {
  181.    if (args->barkok)
  182.       {
  183.       SENDBYTE (ACK);
  184.       return (RB3);
  185.       }
  186.    else
  187.       {
  188.       SENDBYTE (NAK);
  189.       return (RB1);
  190.       }
  191. }
  192.  
  193. int RBWaitStrt (BARKARGSP args)
  194. {
  195.    int c = 0;
  196.    long RB3Timer;
  197.    long RB3Timer1;
  198.  
  199.    RB3Timer = timerset (1500);
  200.    while (CARRIER && (!timeup (RB3Timer)))
  201.       {
  202.       RB3Timer1 = timerset (300);
  203.       while (!timeup (RB3Timer1))
  204.          {
  205.          if ((c = PEEKBYTE ()) >= 0)
  206.             break;
  207.  
  208.          time_release ();
  209.          }
  210.  
  211.       if ((unsigned int)c == 0xffff)
  212.          SENDBYTE (ACK);
  213.       else if ((c == 'C') || (c == NAK))
  214.          return (RB4);
  215.       }
  216.  
  217.    happy_compiler = args->nfiles; /* This makes compilers happy! */
  218.  
  219. /* Return error */
  220.    return (SENDBLOCK_ERR);
  221. }
  222.  
  223. int RBSendFile (BARKARGSP args)
  224. {
  225.    int nfiles1;
  226.  
  227.    nfiles1 = args->nfiles;
  228.    if (((args->nfiles = n_frproc (args->barkpacket, args->nfiles, cdog_callback, cdog_time)) < 0)
  229.       || nfiles1 == args->nfiles)
  230.       {
  231.       (void) Batch_Send (NULL);
  232.       }
  233.    else
  234.       {
  235.       (void) Batch_Send (NULL);
  236.       status_line (MSG_TXT(M_MATCHING_FILES), args->nfiles - nfiles1);
  237.       }
  238.  
  239.    return (RB0);
  240. }
  241.  
  242. int SEA_recvreq ()
  243. {
  244.    BARKARGS bark;
  245.  
  246.    Netmail_Session = 2;
  247.    CLEAR_INBOUND ();
  248.    return (state_machine (Bark_Receiver, &bark, RB0));
  249. }
  250.  
  251. int get_req_str (char *req)
  252. {
  253.    unsigned int crc, crc1, crc2, crc3;
  254.    int i, j;
  255.  
  256.    crc = i = 0;
  257.    while (CARRIER)
  258.       {
  259.       j = TIMED_READ (2);
  260.       if (j < 0)
  261.          return (0);
  262.  
  263.       if ((j == ACK) && (i == 0))
  264.          {
  265.          /* Just skip the extra ACK */
  266.          continue;
  267.          }
  268.  
  269.       if (i >= 100)
  270.          {
  271.          /* Too long of a string */
  272.          status_line (MSG_TXT(M_BAD_BARK));
  273.          CLEAR_INBOUND ();
  274.          return (0);
  275.          }
  276.  
  277.       if (j == ETX)
  278.          {
  279.          crc1 = (unsigned) TIMED_READ (2);
  280.          crc2 = (unsigned) TIMED_READ (2);
  281.          crc3 = (crc2 << 8) + crc1;
  282.          if (crc3 != crc)
  283.             {
  284.             status_line (MSG_TXT(M_BAD_CRC));
  285.             return (0);
  286.             }
  287.          req[i] = '\0';
  288.          return (1);
  289.          }
  290.       else if (j == SUB)
  291.          {
  292.          return (0);
  293.          }
  294.       else
  295.          {
  296.          req[i++] = (char) (j & 0xff);
  297.          crc = xcrc (crc, (j & 0xff));
  298.          }
  299.       }
  300.    return (0);
  301. }
  302.  
  303. /*
  304.  * gen_req_name -- take the name [time] [password] fields from
  305.  *                 the BARK file request format and reformat to
  306.  *                 name [!password] [+time] WaZOO format for use
  307.  *                 by the WaZOO file request routines.
  308.  *
  309.  * Input:          *req = pointer to character array with Bark string
  310.  * Output:         *req array contents reformatted
  311.  *
  312.  */
  313.  
  314.  
  315. void gen_req_name (char *req)
  316. {
  317.    char *q, *q1;
  318.    char buf[48];
  319.    char *fsecs = NULL;
  320.  
  321.    q = req;
  322.    q1 = buf;
  323.  
  324.    /* Get the filename */
  325.  
  326.    while ((*q) && (!isspace (*q)))
  327.       {
  328.       *q1++ = *q++;
  329.       }
  330.    *q1 = '\0';
  331.  
  332.    /* If we have more characters, go on */
  333.  
  334.    if (*q)
  335.       {
  336.       /* Skip the space */
  337.       fsecs = q++;
  338.       *fsecs = '+';
  339.  
  340.       /* Skip the digits */
  341.  
  342.       while ((*q) && (!isspace (*q)))
  343.          q++;
  344.  
  345.       /* If we have more, get the password */
  346.  
  347.       if (*q)
  348.          {
  349.          *q++ = '\0';                   /* Skip space, terminate the time */
  350.  
  351.          *q1++ = ' ';
  352.          *q1++ = '!';
  353.          while (*q)
  354.             {
  355.             *q1++ = *q++;
  356.             }
  357.          *q1 = '\0';
  358.          }
  359.  
  360.       /* If we got an update time          */
  361.       if (fsecs != NULL)
  362.          {
  363.          *q1++ = ' ';
  364.          while (*fsecs)
  365.             {
  366.             *q1++ = *fsecs++;
  367.             }
  368.          *q1 = '\0';
  369.          }
  370.       }
  371.  
  372.    (void) strcpy (req, buf);
  373.    return;
  374. }
  375.  
  376. int cdog_callback (char *reqs)
  377. {
  378.    return (!Batch_Send (reqs));
  379. }
  380.  
  381. int cdog_time (long filesize)
  382. {
  383.    int i;
  384.    i = (int) (filesize * 10 / cur_baud.rate_value * 100 / 94);
  385.    return (i);
  386. }
  387.  
  388.