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