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