home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1996 October / PCO_10.ISO / filesbbs / bsrc_260.arj / SRC.ZIP / recvbark.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-20  |  9.2 KB  |  412 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-96, Bit Bucket Software Co.              */
  11. /*                                                                          */
  12. /*                  This module was written by Bob Hartman                  */
  13. /*                                                                          */
  14. /*               BinkleyTerm "BARK" File request state machine              */
  15. /*                                                                          */
  16. /*                                                                          */
  17. /*    For complete  details  of the licensing restrictions, please refer    */
  18. /*    to the License  agreement,  which  is published in its entirety in    */
  19. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.260.    */
  20. /*                                                                          */
  21. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  22. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  23. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  24. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  25. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  26. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  27. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  28. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  29. /*                                                                          */
  30. /*                                                                          */
  31. /* You can contact Bit Bucket Software Co. at any one of the following      */
  32. /* addresses:                                                               */
  33. /*                                                                          */
  34. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  35. /* P.O. Box 460398                AlterNet 7:42/1491                        */
  36. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  37. /*                                Internet f491.n343.z1.fidonet.org         */
  38. /*                                                                          */
  39. /* Please feel free to contact us at any time to share your comments about  */
  40. /* our software and/or licensing policies.                                  */
  41. /*                                                                          */
  42. /*--------------------------------------------------------------------------*/
  43.  
  44. /* Include this file before any other includes or defines! */
  45.  
  46. #include "includes.h"
  47.  
  48. int get_req_str (char *);
  49. void gen_req_name (char *);
  50. int cdog_callback (char *);
  51. int cdog_time (long);
  52.  
  53. int RBInit (BARKARGSP);
  54. int RBEnd (BARKARGSP);
  55. int RBHonorReq (BARKARGSP);
  56. int RBWaitBark (BARKARGSP);
  57. int RBAckBark (BARKARGSP);
  58. int RBWaitStrt (BARKARGSP);
  59. int RBSendFile (BARKARGSP);
  60.  
  61. typedef struct
  62. {
  63.     char *state_name;
  64.     int (*state_func) (BARKARGSP);
  65. } RSTATES, *RSTATEP;
  66.  
  67. RSTATES Bark_Receiver[] =
  68. {
  69.     {"RBInit", RBInit},
  70.     {"RBEnd", RBEnd},
  71.     {"RB0", RBHonorReq},
  72.     {"RB1", RBWaitBark},
  73.     {"RB2", RBAckBark},
  74.     {"RB3", RBWaitStrt},
  75.     {"RB4", RBSendFile}
  76. };
  77.  
  78. int 
  79. Receive_Bark_Packet (BARKARGSP args)
  80. {
  81.     if (get_req_str (args->barkpacket))
  82.     {
  83.         gen_req_name (args->barkpacket);
  84.         args->barkok = 1;
  85.         return (0);
  86.     }
  87.  
  88.     return (1);
  89. }
  90.  
  91. int 
  92. RBInit (BARKARGSP args)
  93. {
  94.     XON_DISABLE ();
  95.     args->barkok = 0;
  96.     return ((int) args->control);
  97. }
  98.  
  99. int 
  100. RBEnd (BARKARGSP args)
  101. {
  102.     if (!no_requests)
  103.     {
  104.         status_line (":%s %s %s", MSG_TXT (M_END_OF), MSG_TXT (M_INBOUND), MSG_TXT (M_FILE_REQUESTS));
  105.     }
  106.     happy_compiler = args->nfiles;    /* This makes compilers happy! */
  107.     return ((int) args->control);
  108. }
  109.  
  110. int 
  111. RBHonorReq (BARKARGSP args)
  112. {
  113.     args->nfiles = 0;
  114.  
  115.     if (!no_requests)
  116.     {
  117.         status_line (":%s %s", MSG_TXT (M_INBOUND), MSG_TXT (M_FILE_REQUESTS));
  118.         SENDBYTE (ENQ);
  119.         args->T1 = timerset (200);
  120.         return (RB1);
  121.     }
  122.     else
  123.     {
  124.         SENDBYTE (CAN);
  125.         status_line (MSG_TXT (M_REFUSING_IN_FREQ));
  126.         return (SUCCESS);
  127.     }
  128. }
  129.  
  130. int 
  131. RBWaitBark (BARKARGSP args)
  132. {
  133.     unsigned short c;
  134.    int ret;
  135.     long RB1Timer;
  136.  
  137.     ret = -1;
  138.     RB1Timer = timerset (2000);
  139.     while (ret < 0)
  140.     {
  141.         while ((c = PEEKBYTE ()) == 0xffff)
  142.         {
  143.             if (!CARRIER)
  144.                 return (CARRIER_ERR);
  145.             time_release ();
  146.  
  147.             if (timeup (args->T1))
  148.             {
  149.                 break;
  150.             }
  151.         }
  152.  
  153.         if (timeup (RB1Timer))
  154.         {
  155.             /* Report error */
  156.             return (SENDBLOCK_ERR);
  157.         }
  158.  
  159.         if (((unsigned short) c == 0xffff) && timeup (args->T1))
  160.         {
  161.             CLEAR_INBOUND ();
  162.             SENDBYTE (ENQ);
  163.             args->T1 = timerset (200);
  164.             continue;
  165.         }
  166.  
  167.         c = TIMED_READ (0);
  168.  
  169.         switch (c)
  170.         {
  171.         case ACK:
  172.             if (Receive_Bark_Packet (args) == 0)
  173.                 ret = RB2;
  174.             else
  175.                 ret = RB0;
  176.             break;
  177.  
  178.         case ETB:
  179.             ret = SUCCESS;
  180.             break;
  181.  
  182.         case ENQ:
  183.             SENDBYTE (ETB);
  184.             break;
  185.  
  186.         default:
  187.             time_release ();    /*PLF Sun  12-01-1991  05:29:25 */
  188.         }
  189.     }
  190.     return (ret);
  191. }
  192.  
  193. int 
  194. RBAckBark (BARKARGSP args)
  195. {
  196.     if (args->barkok)
  197.     {
  198.         SENDBYTE (ACK);
  199.         return (RB3);
  200.     }
  201.     else
  202.     {
  203.         SENDBYTE (NAK);
  204.         return (RB1);
  205.     }
  206. }
  207.  
  208. int 
  209. RBWaitStrt (BARKARGSP args)
  210. {
  211.     short c = 0;
  212.     long RB3Timer;
  213.     long RB3Timer1;
  214.  
  215.     RB3Timer = timerset (1500);
  216.     while (CARRIER && (!timeup (RB3Timer)))
  217.     {
  218.         RB3Timer1 = timerset (300);
  219.         while (!timeup (RB3Timer1))
  220.         {
  221.             if ((c = PEEKBYTE ()) >= 0)
  222.                 break;
  223.  
  224.             time_release ();
  225.         }
  226.  
  227.         if ((unsigned short) c == 0xffff)
  228.             SENDBYTE (ACK);
  229.         else if ((c == 'C') || (c == NAK))
  230.             return (RB4);
  231.     }
  232.  
  233.     happy_compiler = args->nfiles;    /* This makes compilers happy! */
  234.  
  235.     /* Return error */
  236.     return (SENDBLOCK_ERR);
  237. }
  238.  
  239. int 
  240. RBSendFile (BARKARGSP args)
  241. {
  242.     int nfiles1;
  243.  
  244.     nfiles1 = args->nfiles;
  245.     if (((args->nfiles = n_frproc (args->barkpacket, args->nfiles, cdog_callback, cdog_time)) < 0)
  246.         || nfiles1 == args->nfiles)
  247.     {
  248.         (void) Batch_Send (NULL);
  249.     }
  250.     else
  251.     {
  252.         (void) Batch_Send (NULL);
  253.         status_line (MSG_TXT (M_MATCHING_FILES), args->nfiles - nfiles1);
  254.     }
  255.  
  256.     return (RB0);
  257. }
  258.  
  259. int 
  260. SEA_recvreq ()
  261. {
  262.     BARKARGS bark;
  263.  
  264.     Netmail_Session = 2;
  265.     CLEAR_INBOUND ();
  266.     return (state_machine ((STATEP) Bark_Receiver, &bark, RB0));
  267. }
  268.  
  269. int 
  270. get_req_str (char *req)
  271. {
  272.     word crc, crc1, crc2, crc3;
  273.     int i, j;
  274.  
  275.     crc = i = 0;
  276.     while (CARRIER)
  277.     {
  278.         j = TIMED_READ (2);
  279.         if (j < 0)
  280.         {
  281.             time_release ();    /*PLF Sun  12-01-1991  05:29:49 */
  282.             return (0);
  283.         }
  284.  
  285.         if ((j == ACK) && (i == 0))
  286.         {
  287.             /* Just skip the extra ACK */
  288.             continue;
  289.         }
  290.  
  291.         if (i >= 100)
  292.         {
  293.             /* Too long of a string */
  294.             status_line (MSG_TXT (M_BAD_BARK));
  295.             CLEAR_INBOUND ();
  296.             return (0);
  297.         }
  298.  
  299.         if (j == ETX)
  300.         {
  301.             crc1 = (word) TIMED_READ (2);
  302.             crc2 = (word) TIMED_READ (2);
  303.             crc3 = (crc2 << 8) + crc1;
  304.             if (crc3 != crc)
  305.             {
  306.                 status_line (MSG_TXT (M_BAD_CRC));
  307.                 return (0);
  308.             }
  309.             req[i] = '\0';
  310.             return (1);
  311.         }
  312.         else if (j == SUB)
  313.         {
  314.             return (0);
  315.         }
  316.         else
  317.         {
  318.             req[i++] = (char) (j & 0xff);
  319.             crc = xcrc (crc, (j & 0xff));
  320.         }
  321.     }
  322.     return (0);
  323. }
  324.  
  325. /*
  326.  * gen_req_name -- take the name [time] [password] fields from
  327.  *                 the BARK file request format and reformat to
  328.  *                 name [!password] [+time] WaZOO format for use
  329.  *                 by the WaZOO file request routines.
  330.  *
  331.  * Input:          *req = pointer to character array with Bark string
  332.  * Output:         *req array contents reformatted
  333.  *
  334.  */
  335.  
  336. void 
  337. gen_req_name (char *req)
  338. {
  339.     char *q, *q1;
  340.     char buf[48];
  341.     char *fsecs = NULL;
  342.  
  343.     q = req;
  344.     q1 = buf;
  345.  
  346.     /* Get the filename */
  347.  
  348.     while ((*q) && (!isspace (*q)))
  349.     {
  350.         *q1++ = *q++;
  351.     }
  352.     *q1 = '\0';
  353.  
  354.     /* If we have more characters, go on */
  355.  
  356.     if (*q)
  357.     {
  358.         /* Skip the space */
  359.         fsecs = q++;
  360.         *fsecs = '+';
  361.  
  362.         /* Skip the digits */
  363.  
  364.         while ((*q) && (!isspace (*q)))
  365.             q++;
  366.  
  367.         /* If we have more, get the password */
  368.  
  369.         if (*q)
  370.         {
  371.             *q++ = '\0';        /* Skip space, terminate the time */
  372.  
  373.             *q1++ = ' ';
  374.             *q1++ = '!';
  375.             while (*q)
  376.             {
  377.                 *q1++ = *q++;
  378.             }
  379.             *q1 = '\0';
  380.         }
  381.  
  382.         /* If we got an update time          */
  383.         if (fsecs != NULL)
  384.         {
  385.             *q1++ = ' ';
  386.             while (*fsecs)
  387.             {
  388.                 *q1++ = *fsecs++;
  389.             }
  390.             *q1 = '\0';
  391.         }
  392.     }
  393.  
  394.     (void) strcpy (req, buf);
  395.     return;
  396. }
  397.  
  398. int 
  399. cdog_callback (char *reqs)
  400. {
  401.     return (!Batch_Send (reqs));
  402. }
  403.  
  404. int 
  405. cdog_time (long filesize)
  406. {
  407.     long ltemp;
  408.  
  409.     ltemp = (filesize * 10L / cur_baud.rate_value * 100L / 94L);
  410.     return (ltemp < 20L) ? 20 : (int) ltemp;
  411. }
  412.