home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / MAILROOT.C < prev    next >
C/C++ Source or Header  |  1991-08-15  |  16KB  |  602 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 Mail Control Routines                      */
  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. #include <stdio.h>
  47. #include <signal.h>
  48. #include <ctype.h>
  49. #include <conio.h>
  50. #include <sys/types.h>
  51. #include <sys/stat.h>
  52. #include <string.h>
  53. #include <fcntl.h>
  54. #include <time.h>
  55. #include <dos.h>
  56. #include <process.h>
  57. #include <stdlib.h>
  58. #include <io.h>
  59.  
  60. #include "com.h"
  61. #include "xfer.h"
  62. #include "zmodem.h"
  63. #include "keybd.h"
  64. #include "sbuf.h"
  65. #include "sched.h"
  66. #include "externs.h"
  67. #include "prototyp.h"
  68. #include "defines.h"
  69. #include "vfossil.h"
  70.  
  71. static char *estring(int, int);
  72.  
  73. int do_mail(baddr, manual)
  74. ADDR *baddr;
  75. int manual;
  76.     {
  77.     long callstart, callend;
  78.  
  79.     caller = 1;
  80.     got_packet = 0;
  81.     got_arcmail = 0;
  82.     got_mail = 0;
  83.     sent_mail = 0;
  84.     no_WaZOO_Session = 0;
  85.     sprintf(junk, "%s", Full_Addr_Str (baddr));
  86.     remote_addr.Zone = baddr->Zone;
  87.     remote_addr.Net = baddr->Net;
  88.     remote_addr.Node = baddr->Node;
  89.     remote_addr.Point = baddr->Point;
  90.     remote_addr.Domain = baddr->Domain;
  91.     called_addr.Zone = baddr->Zone;
  92.     called_addr.Net = baddr->Net;
  93.     called_addr.Node = baddr->Node;
  94.     called_addr.Point = baddr->Point;
  95.     called_addr.Domain = baddr->Domain;
  96.  
  97.     if (!net_params)
  98.         {
  99.         status_line(msgtxt[M_INSUFFICIENT_DATA]);
  100.         set_xy("");
  101.         return(0);
  102.         }
  103.  
  104.     if (!nodeproc(junk))
  105.         return(0);
  106.  
  107.     if (manual)
  108.         {
  109.         if (flag_file(TEST_AND_SET, &called_addr, 1))
  110.             {
  111.             if(CARRIER)
  112.                 mdm_hangup();
  113.             return(0);
  114.             }
  115.  
  116.         if (CARRIER)                               /* called manually maybe? */
  117.             goto process_the_damned_mail;           /* yup, just do some mail */
  118.  
  119.  
  120.         do_dial_strings();
  121.         try_2_connect((char *) (newnodedes.PhoneNumber));    /* try to connect         */
  122.         }
  123.     else
  124.         {
  125.         /* If this is supposed to be only local, then get out if it isn't */
  126.         if (e_ptrs[cur_event]->behavior & MAT_LOCAL)
  127.             {
  128.             if (e_ptrs[cur_event]->node_cost < 0)
  129.                 {
  130.                 if ((int) newnodedes.RealCost < -e_ptrs[cur_event]->node_cost)
  131.                     {
  132.                     return(0);
  133.                     }
  134.                 }
  135.             else
  136.                 {
  137.                 if ((int) newnodedes.RealCost > e_ptrs[cur_event]->node_cost)
  138.                     {
  139.                     return(0);
  140.                     }
  141.                 }
  142.             }
  143.         /* If it is supposed to be 24 hour mail only, get out if it isn't */
  144.         if (newnodelist && (!(e_ptrs[cur_event]->behavior & MAT_NOMAIL24)) && (!(newnodedes.NodeFlags & B_CM)))
  145.             return(0);
  146.       /* If we aren't supposed to send to CM's now, get out */
  147.         if (newnodelist && (e_ptrs[cur_event]->behavior & MAT_NOCM) && (newnodedes.NodeFlags & B_CM))
  148.             return(0);
  149.         /* Try to connect */
  150.  
  151.         if (flag_file(TEST_AND_SET, &called_addr, 1))
  152.             {
  153.             if (CARRIER)
  154.                 mdm_hangup();
  155.             return(0);
  156.             }
  157.  
  158.         do_dial_strings();
  159.         if (un_attended && fullscreen)
  160.             {
  161.             sb_move(holdwin, 2, 1);
  162.             sb_wa(holdwin, colors.calling, 31);
  163.             }
  164.  
  165.         callstart = time(NULL);
  166.  
  167.         if (!blank_on_key)
  168.             screen_blank = 0;
  169.  
  170.         if (try_1_connect ((char *) (newnodedes.PhoneNumber)) == -1)
  171.             {
  172.             if (un_attended && fullscreen)
  173.                 {
  174.                 sb_move(holdwin, 2, 1);
  175.                 sb_wa(holdwin, colors.hold, 31);
  176.                 }
  177.             flag_file(CLEAR_FLAG, &called_addr, 1);
  178.             return(-1);
  179.             }
  180.         }
  181.  
  182. process_the_damned_mail:
  183.  
  184.     if (CARRIER)                                  /* if we did,             */
  185.         {
  186.         if (manual)
  187.             callstart = time(NULL);
  188.  
  189.         b_session(1);                             /* do a mail session      */
  190.         flag_file(CLEAR_FLAG, &called_addr, 1);      /* make sure flag is gone */
  191.  
  192.         callend = time(NULL);
  193.         hist.callcost += cost_of_call(callstart, callend);
  194.         mdm_hangup();
  195.  
  196.         ++hist.connects;
  197.         if (un_attended && fullscreen)
  198.             {
  199.             sb_move(historywin, HIST_CONN_ROW, HIST_COL);
  200.             sprintf(junk, "%d/%ld", hist.connects, hist.callcost);
  201.             sb_puts(historywin, (unsigned char *) junk);
  202.             }
  203.  
  204.         write_stats();
  205.  
  206.         if (un_attended && (got_arcmail || got_packet || got_mail))
  207.             {
  208.             bad_call(baddr, -1);
  209.             receive_exit();
  210.             }
  211.         if (un_attended && fullscreen)
  212.             {
  213.             sb_move(holdwin, 2, 1);
  214.             sb_wa(holdwin, colors.hold, 31);
  215.             }
  216.         return(1);
  217.         }
  218.     else
  219.         {
  220.         status_line(msgtxt[M_END_OF_ATTEMPT]);
  221.         flag_file(CLEAR_FLAG, &called_addr, 1);
  222.         }
  223.     if (un_attended && fullscreen)
  224.         {
  225.         sb_move(holdwin, 2, 1);
  226.         sb_wa(holdwin, colors.hold, 31);
  227.         }
  228.     write_stats();
  229.     return(2);
  230.     }
  231.  
  232. int handle_inbound_mail()
  233.     {
  234.     long t;                                /* used for the timeouts  */
  235.     int mr;                                                /* Modem response */
  236.  
  237.  
  238.     caller = 0;
  239.     remote_addr.Zone = 0;
  240.     remote_addr.Net = 0;
  241.     remote_addr.Node = 0;
  242.     remote_addr.Point = 0;
  243.     remote_addr.Domain = NULL;
  244.  
  245. inloop:
  246.  
  247.     if (!(server_mode && CARRIER) && !CHAR_AVAIL())           /* Any action from modem? */
  248.         {
  249.         time_release();
  250.         return(0);                                /* No, nothing to do      */
  251.         }
  252.  
  253. #ifdef NEVER /* should not need this any more */
  254.  
  255.     /* if outbound only, then return */
  256.     if ((cur_event >= 0) && (e_ptrs[cur_event]->behavior & MAT_OUTONLY))
  257.         {
  258.         time_release();
  259.         return(0);
  260.         }
  261.  
  262. #endif
  263.  
  264.     mail_only = 1;
  265.     if ((cur_event >= 0) && (e_ptrs[cur_event]->behavior & MAT_BBS))
  266.         mail_only = 0;
  267.  
  268. should_answer:
  269.  
  270.     if (!blank_on_key)
  271.         screen_blank = 0;
  272.     if (un_attended && fullscreen)
  273.         sb_show();
  274.  
  275.     if (server_mode && CARRIER)
  276.         goto got_carrier;
  277.  
  278.     if (((mr = modem_response(500)) == 3) && (ans_str != NULL))      /* RING detected */
  279.         {
  280.         /*
  281.          * Try to make sure we don't send out the answer string while stuff is
  282.          * still coming from the modem.  Most modems don't like that kind of
  283.          * sequence (including HST's!). 
  284.          */
  285.         t = timerset (100);
  286.         while (CHAR_AVAIL() && (!timeup(t)))
  287.             {
  288.             t = timerset(100);
  289.             MODEM_IN();
  290. //            time_release();            /* CML -- stop hogging my CPU! */
  291.             }
  292.         CLEAR_INBOUND();
  293.         mdm_cmd_string(ans_str, 0);               /* transmit the answer string */
  294.                                                                 
  295.         goto should_answer;
  296.         }
  297.     else if (mr != 2)
  298.         {
  299.         t = timerset(6000);                       /* 1 minute               */
  300.         while ((!timeup(t)) && (!CHAR_AVAIL()) && (!KEYPRESS()))
  301.             time_release();                        /* wait for another result */
  302.         if (KEYPRESS())                           /* If aborted by user,    */
  303.             {
  304.             /* FOSSIL_CHAR();  */                  /* eat the character      */
  305.             return(1);                             /* and get out,           */
  306.             }
  307.         goto inloop;                              /* else proceed along     */
  308.         }
  309.  
  310. got_carrier:
  311.  
  312.     if (CARRIER)                                  /* if we have a carrier,  */
  313.         {
  314.         b_session(0);                             /* do a mail session      */
  315.         mdm_hangup();                             /* Make sure to hang up   */
  316.  
  317.         /* We got inbound mail */
  318.         if (got_arcmail || got_packet || got_mail)
  319.             {
  320.             receive_exit();
  321.             }
  322.         }
  323.     return(1);
  324.     }
  325.  
  326. void receive_exit()
  327.     {
  328.     char junk1[150];
  329.     int i;
  330.  
  331.     if (got_arcmail && (cur_event >= 0) && (e_ptrs[cur_event]->errlevel[2]))
  332.         {
  333.         status_line(msgtxt[M_EXIT_COMPRESSED],e_ptrs[cur_event]->errlevel[2]);
  334.  
  335.         errl_exit(e_ptrs[cur_event]->errlevel[2]);
  336.         }
  337.  
  338.     if (cur_event >= 0)
  339.         {
  340.         for (i = 0; i < 6; i++)
  341.             {
  342.             if (user_exits[i])
  343.                 {
  344.                 status_line(msgtxt[M_EXIT_AFTER_EXTENT],&(e_ptrs[cur_event]->err_extent[i][0]),e_ptrs[cur_event]->errlevel[i + 3]);
  345.                 errl_exit(e_ptrs[cur_event]->errlevel[i + 3]);
  346.                 }
  347.             }
  348.         }
  349.  
  350.     if ((got_mail || got_packet) && (cur_event >= 0) && (e_ptrs[cur_event]->errlevel[1]))
  351.         {
  352.         status_line(msgtxt[M_EXIT_AFTER_MAIL],e_ptrs[cur_event]->errlevel[1]);
  353.  
  354.         errl_exit(e_ptrs[cur_event]->errlevel[1]);
  355.         }
  356.  
  357.     if ((aftermail != NULL) && (got_mail || got_packet || got_arcmail))
  358.         {
  359.         status_line(msgtxt[M_AFTERMAIL]);
  360.         mdm_init(modem_busy);
  361.         exit_DTR();
  362.         screen_clear();
  363.         vfossil_cursor(1);
  364.         strcpy(junk1, aftermail);
  365.         if (cur_event >= 0)
  366.             strcat(junk1, e_ptrs[cur_event]->cmd);
  367.         close_up();
  368.         b_spawn(junk1);
  369.         come_back();     /* CML */
  370.         DTR_ON();
  371.         status_line(msgtxt[M_OK_AFTERMAIL]);
  372.         mdm_init(modem_init);
  373.         xmit_reset();
  374.         }
  375.  
  376.     got_arcmail = 0;
  377.     got_packet = 0;
  378.     got_mail = 0;
  379.     }
  380.  
  381. void errl_exit(n)
  382. int n;
  383.     {
  384.     write_sched();
  385.  
  386.     status_line("+end, %s", xfer_id);
  387.     mdm_init(modem_busy);                        /* Reinitialize the modem  */
  388.     exit_DTR();
  389.     /*SCB*/ if (fullscreen)
  390.         gotoxy(0, SB_ROWS);
  391.  
  392.     if (vfossil_installed)
  393.         vfossil_close();
  394.  
  395.     if (!share)
  396.         MDM_DISABLE();
  397.     exit(n);
  398.     }
  399.  
  400. long random_time(x)
  401. int x;
  402.     {
  403.     int i;
  404.  
  405.     if (x == 0)
  406.         {
  407.         return(0L);
  408.         }
  409.  
  410.     /* Number of seconds to delay is random based on x +/- 50% */
  411.     i = (rand() % (x + 1)) + (x / 2);
  412.  
  413.     return(timerset ((unsigned int) (i * 100)));
  414.     }
  415.  
  416.  
  417. char *HoldAreaNameMunge(maddr)
  418. ADDR *maddr;
  419.     {
  420.     static char munged[80];
  421.     register char *p, *q;
  422.     int i;
  423.  
  424.     if ((maddr->Domain != my_addr.Domain) && (maddr->Domain != NULL))
  425.         {
  426.         *domain_loc = '\0';
  427.         strcpy(munged, domain_area);
  428.         q = &(munged[strlen(munged)]);
  429.         for (i = 0; domain_name[i] != NULL; i++)
  430.             {
  431.             if (domain_name[i] == maddr->Domain)
  432.                 {
  433.                 if (domain_abbrev[i] != NULL)
  434.                     {
  435.                     p = domain_abbrev[i];
  436.                     while (*p)
  437.                         *q++ = *p++;
  438.                     if (no_zones)
  439.                         sprintf(q, "\\");
  440.                     else
  441.                         sprintf(q, ".%03x\\", maddr->Zone);
  442.                     }
  443.                 break;
  444.                 }
  445.             }
  446.         }
  447.     else
  448.         {
  449.         p = hold_area;
  450.         if ((maddr->Zone == (int) alias[0].Zone) || (no_zones))
  451.             return(p);
  452.         q = munged;
  453.         while (*p)
  454.             *q++ = *p++;
  455.         --q;
  456.         sprintf(q,".%03x\\",maddr->Zone);
  457.         }
  458.     return(munged);
  459.     }
  460.  
  461.  
  462. void mailer_banner()
  463.     {
  464.     if (fullscreen && un_attended)
  465.         {
  466.         vfossil_cursor(0);
  467.         sb_move(settingswin, SET_EVNT_ROW, SET_COL);
  468.         sprintf(junk, "%-2d", cur_event + 1);
  469.         sb_puts(settingswin, (unsigned char *) junk);
  470.         sprintf(junk, "%-5u Com%d", cur_baud, port_ptr + 1);
  471.         sb_move(settingswin, SET_PORT_ROW, SET_COL);
  472.         sb_puts(settingswin, (unsigned char *) junk);
  473.         clear_filetransfer();
  474.         }
  475.  
  476.     set_baud(max_baud.rate_value, 0);
  477.     }
  478.  
  479. void clear_filetransfer()
  480.     {
  481.     if (fullscreen && un_attended)
  482.         sb_fillc(filewin, ' ');
  483.     }
  484.  
  485. static char ebuf[50];
  486. static char *estring(e, how_big)
  487. int e;
  488. int how_big;
  489.     {
  490.     char j[10];
  491.  
  492.     ebuf[0] = '\0';
  493.     if (e >= 0)
  494.         {
  495.         if (e_ptrs[e]->behavior & MAT_BBS)
  496.             {
  497.             strcat(ebuf, "B");
  498.             if (how_big)
  499.                 strcat(ebuf, " ");
  500.             }
  501.         if (e_ptrs[e]->behavior & MAT_CM)
  502.             {
  503.             strcat(ebuf, "C");
  504.             if (how_big)
  505.                 strcat(ebuf, " ");
  506.             }
  507.         if (e_ptrs[e]->behavior & MAT_DYNAM)
  508.             {
  509.             strcat(ebuf, "D");
  510.             if (how_big)
  511.                 strcat(ebuf, " ");
  512.             }
  513.         if (how_big && (e_ptrs[e]->behavior & MAT_FORCED))
  514.             strcat(ebuf, "F ");
  515.         if (e_ptrs[e]->behavior & MAT_NOCM)
  516.             {
  517.             strcat(ebuf, "K");
  518.             if (how_big)
  519.                 strcat(ebuf, " ");
  520.             }
  521.         if (e_ptrs[e]->behavior & MAT_LOCAL)
  522.             {
  523.             strcat(ebuf, "L");
  524.             if (how_big)
  525.                 {
  526.                 if (e_ptrs[e]->node_cost > 0)
  527.                     sprintf(j, "<%d ", e_ptrs[e]->node_cost + 1);
  528.                 else
  529.                     sprintf(j, ">%d ", -e_ptrs[e]->node_cost - 1);
  530.                 strcat(ebuf, j);
  531.                 }
  532.             }
  533.         if (how_big && (e_ptrs[e]->behavior & MAT_NOMAIL24))
  534.             strcat(ebuf, "M ");
  535.         if (e_ptrs[e]->behavior & MAT_NOREQ)
  536.             {
  537.             strcat(ebuf, "N");
  538.             if (how_big)
  539.                 strcat(ebuf, " ");
  540.             }
  541.         if (e_ptrs[e]->behavior & MAT_OUTONLY)
  542.             {
  543.             strcat(ebuf, "S");
  544.             if (how_big)
  545.                 strcat(ebuf, " ");
  546.             }
  547.         if (e_ptrs[e]->behavior & MAT_NOOUT)
  548.             {
  549.             strcat(ebuf, "R");
  550.             if (how_big)
  551.                 strcat(ebuf, " ");
  552.             }
  553.         if (how_big && (e_ptrs[e]->behavior & MAT_NOOUTREQ))
  554.             strcat(ebuf, "X");
  555.         }
  556.     return(ebuf);
  557.     }
  558.  
  559. void do_ready(str)
  560. char *str;
  561.     {
  562.     if (fullscreen && un_attended)
  563.         {
  564.         if (!doing_poll)
  565.             {
  566.             clear_filetransfer();
  567.             }
  568.  
  569.         sb_move(settingswin, SET_EVNT_ROW, SET_COL);
  570.         sprintf(junk, "%-2d/%-6.6s", cur_event + 1, estring (cur_event, 0));
  571.         sb_puts(settingswin, (unsigned char *) junk);
  572.         sb_move(settingswin, SET_STAT_ROW, SET_COL);
  573.         sb_puts(settingswin, (unsigned char *) str);
  574.         sb_show();
  575.         }
  576.     }
  577.  
  578. void list_next_event()
  579.     {
  580.     int i;
  581.     char *p;
  582.     char j[100];
  583.  
  584.     i = time_to_next(0);
  585.  
  586.     if ((next_event >= 0) && fullscreen)
  587.         {
  588.         clear_filetransfer();
  589.         sb_move(filewin, 1, 2);
  590.         sprintf(j, msgtxt[M_NEXT_EVENT], next_event + 1, i);
  591.         sb_puts(filewin, (unsigned char *) j);
  592.         p = estring(next_event, 1);
  593.         if (*p != '\0')
  594.             {
  595.             sb_move(filewin, 2, 2);
  596.             sprintf(j, msgtxt[M_EVENT_FLAGS], p);
  597.             sb_puts(filewin, (unsigned char *) j);
  598.             }
  599.         sb_show();
  600.         }
  601.     }
  602.