home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BSRC_250.LZH / B_SESSIO.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  16KB  |  490 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. /*                   BinkleyTerm Mail Session Initiator                     */
  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. extern int CallerSendSync (void);
  51. extern int CalledRecvSync (void);
  52.  
  53. void b_session (int was_initiator)
  54. {
  55.    int i;
  56.    ADDR tmp;
  57.  
  58.    CURRENT = DEFAULT;
  59.  
  60.    mail_finished = 0;
  61.  
  62.    (void) time (&etm);
  63.  
  64.    freq_accum.bytes = 0L;
  65.    freq_accum.files = 0;
  66.    freq_accum.time  = 0L;
  67.  
  68.    if (un_attended && fullscreen)
  69.       do_ready (MSG_TXT(M_READY_CONNECT));
  70.  
  71.    /* Remove the old .REQ and .RSP files */
  72.  
  73.    for (i = 0; i < 10; i++)
  74.       {
  75.       if (alias[i].Net == 0)
  76.          break;
  77.  
  78.       tmp = alias[i];
  79.  
  80.    /* For a point, massage the address to get the right .REQ filename */
  81.  
  82.       if (tmp.Point != 0)
  83.          {
  84.          tmp.Node  = tmp.Point;
  85.          tmp.Point = 0;
  86.          tmp.Net   = (pvtnet > 0) ? (unsigned int) pvtnet : 0;
  87.          }        
  88.  
  89.       (void) sprintf (junk, "%s%s.REQ", CURRENT.sc_Inbound, Hex_Addr_Str (&tmp));
  90.       (void) unlink (junk);
  91.  
  92.       (void) sprintf (junk, "%s.RSP", Hex_Addr_Str (&tmp));
  93.       (void) unlink (junk);
  94.       }
  95.  
  96.    tmp.Zone = 0;
  97.    tmp.Net = 0;
  98.    tmp.Node = 0;
  99.    tmp.Point = 0;
  100.    tmp.Domain = NULL;
  101.  
  102.    remote_capabilities = 0;
  103.  
  104.    my_capabilities = (no_zapzed) ? 0 : (ZED_ZAPPER | ZED_ZIPPER);
  105.    my_capabilities |= (no_dietifna) ? 0 : Y_DIETIFNA;
  106.    if ((janus_baud >= cur_baud.rate_value) || (janus_OK))
  107.       my_capabilities |= DOES_IANUS;
  108.  
  109.    (void) flag_file (SET_SESSION_FLAG, &called_addr, 0);
  110.    if (was_initiator)
  111.       {
  112.  
  113. /*
  114.  *    OUTBOUND -- meaning call was made by us.
  115.  */
  116.  
  117.       last_type (1, &remote_addr);
  118.  
  119.       if (Whack_CR () == 0)
  120.          {
  121.  
  122. retry_outbound:
  123.  
  124.          switch (i = CallerSendSync())
  125.             {
  126.             case 1:
  127.             FTSC_sender (0);
  128.             break;
  129.  
  130.             case 2:
  131.             if (YooHoo_Sender ())
  132.                {
  133.                if ((remote_capabilities & my_capabilities) == 0)
  134.                   {
  135.                   no_WaZOO_Session = 1;
  136.                   status_line (MSG_TXT(M_NO_COMMON_PROTO));
  137.                   goto retry_outbound;
  138.                   }
  139.                WaZOO (1);
  140.                }
  141.             break;
  142.  
  143.             default:
  144.             break;
  145.  
  146.             } /* end switch */
  147.          }
  148.  
  149.       }
  150.    else
  151.       {
  152.  
  153. /*
  154.  *    INBOUND -- meaning we were connected to by another system.
  155.  */
  156.  
  157. retry_inbound:
  158.  
  159.       b_init ();
  160.  
  161.       switch (i = CalledRecvSync())
  162.          {   
  163.          case 1:                                 /* ESCape or Timeout    */
  164.          if (CARRIER && !mail_only)
  165.             {
  166.             last_type (3, &tmp);
  167.             (void) flag_file (CLEAR_SESSION_FLAG, &called_addr, 0);
  168.             BBSexit ();
  169.             }
  170.          break;
  171.  
  172.          case 2:                                 /* FTSC Netmail Session */
  173.          if (CARRIER)
  174.             {
  175.             tmp.Zone = (unsigned int) -1000;
  176.             last_type (2, &tmp);
  177.             tmp.Zone = 0;
  178.             ++hist.mail_calls;
  179.             if (un_attended && fullscreen)
  180.                {
  181.                (void) sprintf (junk, "%d/%d", hist.bbs_calls, hist.mail_calls);
  182.                sb_move (history_hWnd, HIST_MAIL_ROW, HIST_COL);
  183.                sb_puts (history_BBS, junk);
  184.                sb_show ();
  185.                }
  186.            (void) FTSC_receiver (0);
  187.             }
  188.          break;
  189.  
  190.          case 3:                                 /* WaZOO Netmail Session*/
  191.          if (YooHoo_Receiver ())
  192.             {
  193.             if ((remote_capabilities & my_capabilities) == 0)
  194.                {
  195.                no_WaZOO_Session = 1;
  196.                status_line (MSG_TXT(M_NO_COMMON_PROTO));
  197.                goto retry_inbound;
  198.                }
  199.             last_type (1, &remote_addr);
  200.             ++hist.mail_calls;
  201.             if (un_attended && fullscreen)
  202.                {
  203.                (void) sprintf (junk, "%d/%d", hist.bbs_calls, hist.mail_calls);
  204.                sb_move (history_hWnd, HIST_MAIL_ROW, HIST_COL);
  205.                sb_puts (history_BBS, junk);
  206.                sb_show ();
  207.                }
  208.             WaZOO (0);                           /* do WaZOO!!!          */
  209.             }
  210.          break;
  211.  
  212.          default:
  213.          if ((i >= 5) && (i < (5 + num_ext_mail)))    /* See if ext mail */
  214.             {
  215.             if (CARRIER)
  216.                {
  217.                last_type (4, &tmp);
  218.                (void) flag_file (CLEAR_SESSION_FLAG, &called_addr, 0);
  219.                UUCPexit (lev_ext_mail[i-5]);
  220.                }
  221.             }
  222.          break;
  223.  
  224.          } /* end switch */
  225.  
  226.       } /* end if (was_initiator) */
  227.  
  228.    no_WaZOO_Session = no_WaZOO;
  229.    (void) flag_file (CLEAR_FLAG, &called_addr, 1); /* ensure flag's gone */
  230.    (void) flag_file (CLEAR_SESSION_FLAG, &called_addr, 0);
  231.    return;
  232. }
  233.  
  234.  
  235. void b_init ()
  236. {
  237.    got_arcmail = got_packet = got_mail = sent_mail = 0;
  238.    remote_addr.Zone = remote_addr.Net = remote_addr.Node = remote_addr.Point = 0;
  239.    remote_addr.Domain = NULL;
  240.    assumed = 0;
  241.    Netmail_Session = remote_capabilities = mail_finished = 0;
  242.    CURRENT = DEFAULT;
  243. }
  244.  
  245.  
  246. /*
  247.  * Send banner-type lines to remote. Since strange modems like Courier HST
  248.  * have a propensity for thinking they are connected when talking to a dial
  249.  * tone (but do leave CTS down just to screw us up) we have to use a timeout
  250.  * to figure out if we are in trouble, and if so, we drop DTR to make the
  251.  * bogus carrier go away.
  252.  *
  253.  * This routine is used in modules B_BBS.C and RECVSYNC.C, both of which
  254.  * are called from this module only.
  255.  *
  256.  */
  257.  
  258. int SendBanner (char far *string)
  259. {
  260.    long t1;
  261.    register char c;
  262.  
  263.    t1 = timerset (600);                         /* 60 seconds max to put out
  264.                                                  * banner */
  265.  
  266.    while (!timeup (t1) && CARRIER)
  267.       {
  268.       if (got_ESC ())                           /* Manual abort?     */
  269.             break;                              /* Use failure logic */
  270.  
  271.       if (!OUT_FULL ())
  272.          {
  273.          c = *string++;
  274.          if (!c || c == 0x1a)                   /* Test for end      */
  275.             return (1);                         /* Yes, success      */
  276.          SENDBYTE ((unsigned char) c);
  277.          }
  278.       else                                      /* If output was full*/
  279.          time_release ();                       /* give up timeslice */
  280.       }
  281.  
  282.    /*
  283.     * If we get here we had trouble. Drop DTR now to hopefully get out of
  284.     * this trouble. Flush outbound. Pause for 1 second.
  285.     */
  286.  
  287.    LOWER_DTR ();
  288.    CLEAR_OUTBOUND ();
  289.  
  290.    timer (10);
  291.  
  292.    CLEAR_INBOUND ();
  293.  
  294.    return (0);
  295. }
  296.  
  297.  
  298. int flag_file (int function, ADDRP address, int do_stat)
  299. {
  300.  
  301.    FILE *fptr;
  302.    char *HoldName;
  303.    static ADDR last_set;
  304.    char flagname[128];
  305.    char tmpname[128];
  306.    char BSYname[15];
  307.    static int had_to_punt = 0;
  308.  
  309.    if (!TaskNumber)
  310.       return (FALSE);
  311.  
  312.    HoldName = HoldAreaNameMunge (address);
  313.  
  314.    switch (function)
  315.       {
  316.       case INITIALIZE:
  317.       last_set.Zone = (unsigned int) -1;
  318.       had_to_punt = 0;
  319.  
  320.       case CLEAR_SESSION_FLAG:
  321.       /* At the end of a session, delete the task file */
  322.       if (flag_dir)
  323.          {
  324.          (void) sprintf (flagname, "%sTask.%02x",
  325.                                flag_dir, TaskNumber);
  326.          (void) unlink (flagname);
  327.          }
  328.       return (FALSE);
  329.  
  330.       case SET_SESSION_FLAG:
  331.    /* At the start of a session, set up the task number */
  332.       if (flag_dir)
  333.          {
  334.          (void) sprintf (flagname, "%sTask.%02x",
  335.                                flag_dir, TaskNumber);
  336.          fptr = fopen (flagname, "wb");
  337.          (void) fclose (fptr);
  338.          }
  339.       return (FALSE);
  340.  
  341.       case TEST_AND_SET:
  342.  
  343.    /*
  344.     * First see if we already HAVE this lock! If so, return now.
  345.     *
  346.     */
  347.  
  348.       if (memcmp (&last_set, address, sizeof (ADDR)) == 0)
  349.          return (FALSE);
  350.  
  351.    /*
  352.     * Next determine the directory in which we will create the flagfile.
  353.     * Also, the name of the file.
  354.     *
  355.     */
  356.  
  357.       if (address->Point != 0)
  358.          {
  359.          (void) sprintf (flagname, "%s%04x%04x.PNT\\",
  360.                      HoldName, address->Net, address->Node);
  361.          (void) sprintf (BSYname, "%08x.BSY",
  362.                      address->Point);
  363.          }
  364.       else
  365.          {
  366.          (void) strcpy (flagname, HoldName);
  367.          (void) sprintf (BSYname, "%04x%04x.BSY",
  368.                             address->Net, address->Node);
  369.          }
  370.    /*
  371.     * File opens are destructive by nature. Therefore use a file name
  372.     * that's unique to us. Create it in the chosen target. If we can't
  373.     * do that, try to use the flag directory.
  374.     *
  375.     */
  376.  
  377.       had_to_punt = 0;
  378.       (void) sprintf (tmpname, "%sBINKBSY.%02x",flagname,TaskNumber);
  379.       fptr = fopen (tmpname, "wb");
  380.       if ((fptr == NULL) && (flag_dir))
  381.          {
  382.          had_to_punt = 1;
  383.          (void) strcpy (flagname, flag_dir);
  384.          (void) sprintf (tmpname, "%sBINKBSY.%02x",flagname,TaskNumber);
  385.          fptr = fopen (tmpname, "wb");
  386.          }         
  387.    /*
  388.     * Now we've done all we can. The file is either open in the 
  389.     * appropriate outbound or it's in the flag directory.
  390.     * If neither option worked out, go away. There's nothing to do.
  391.     *
  392.     */
  393.  
  394.       if (fptr == NULL)
  395.          {
  396.          if (do_stat)
  397.             status_line (MSG_TXT(M_FAILED_CREATE_FLAG),tmpname);
  398.          last_set.Zone = (unsigned int) -1;
  399.          return (TRUE);
  400.          }
  401.       (void) fclose (fptr);
  402.  
  403.    /*
  404.     * Now the test&set. Attempt to rename the file to a value specific
  405.     * to the remote node's address. If we succeed, we have the lock. 
  406.     * If we do not, delete the temp file.
  407.     *
  408.     */
  409.       (void) strcat (flagname, BSYname);   /* Add the .BSY file name */
  410.       if (!rename (tmpname, flagname))
  411.          {
  412.          if (do_stat)
  413.             status_line (MSG_TXT(M_CREATED_FLAGFILE),flagname);
  414.          (void) memcpy (&last_set, address, sizeof (ADDR));
  415.          return (FALSE);
  416.          }
  417.  
  418.       if (do_stat)
  419.          status_line (MSG_TXT(M_THIS_ADDRESS_LOCKED), Full_Addr_Str (address));
  420.       (void) unlink (tmpname);
  421.       last_set.Zone = (unsigned int) -1;
  422.       return (TRUE);
  423.  
  424.       case CLEAR_FLAG:
  425.  
  426.    /*
  427.     * Make sure we need to clear something.
  428.     * Zone should be something other than -1 if that's the case.
  429.     *
  430.     */
  431.       if (last_set.Zone == (unsigned int) -1)
  432.          return (TRUE);
  433.  
  434.    /*
  435.     * Next compare what we want to clear with what we think we have.
  436.     *
  437.     */
  438.  
  439.       if (memcmp (&last_set, address, sizeof (ADDR)) != 0)
  440.          {
  441.          if (do_stat)
  442.             status_line (MSG_TXT(M_BAD_CLEAR_FLAGFILE),
  443.                             Full_Addr_Str (address));
  444.          last_set.Zone = (unsigned int) -1;
  445.          return (TRUE);
  446.          }             
  447.    /*
  448.     * We match. Recalculate the directory. Yeah, that's redundant
  449.     * code, but it saves static space.
  450.     *
  451.     */
  452.       if (address->Point != 0)
  453.          {
  454.          (void) sprintf (flagname, "%s%04x%04x.PNT\\",
  455.                      HoldName, address->Net, address->Node);
  456.          (void) sprintf (BSYname, "%08x.BSY",
  457.                                   address->Point);
  458.          }
  459.       else
  460.          {
  461.          (void) strcpy (flagname, HoldName);
  462.          (void) sprintf (BSYname, "%04x%04x.BSY",
  463.                                   address->Net, address->Node);
  464.          }
  465.  
  466.       if (had_to_punt)
  467.          (void) strcpy (flagname, flag_dir);
  468.       (void) strcat (flagname, BSYname);
  469.  
  470.       had_to_punt = 0;
  471.       last_set.Zone = (unsigned int) -1;
  472.       if (!unlink (flagname))
  473.          {
  474.          if (do_stat)
  475.             status_line (MSG_TXT(M_CLEARED_FLAGFILE), flagname);
  476.          return (TRUE);
  477.          }
  478.  
  479.       if (do_stat)
  480.          status_line (MSG_TXT(M_FAILED_CLEAR_FLAG),flagname);
  481.       return (FALSE);
  482.  
  483.       default:
  484.       break;
  485.       }
  486.  
  487.    return (TRUE);
  488. }
  489.  
  490.