home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / bts314b4 / b_rspfil.c < prev    next >
C/C++ Source or Header  |  1994-01-08  |  15KB  |  581 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 Vince Perriello                 */
  14. /*                                                                            */
  15. /*                                                                            */
  16. /*               BinkleyTerm File Request Failure Message Module                */
  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. #undef RSP    /* Define this for old style RSP files */
  47.  
  48.  
  49. #include <stdio.h>
  50. #include <time.h>
  51. #include <ctype.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54.  
  55. #ifdef __TURBOC__
  56. #ifndef __TOS__
  57. #include <mem.h>
  58. #include <alloc.h>
  59. #endif
  60. #else
  61. #ifndef LATTICE
  62. #include <memory.h>
  63. #include <malloc.h>
  64. #endif
  65. #endif
  66.  
  67. #include "bink.h"
  68. #include "msgs.h"
  69. #include "session.h"
  70.  
  71. #ifndef RSP
  72. #include "password.h"
  73. #include "fsc.h"        /* FSC-0048 packet header */
  74. #endif
  75.  
  76.  
  77.  
  78. static struct parse_list rspverbs [] = {
  79.     {4, "text"},
  80.     {4, "date"},
  81.     {4, "time"},
  82.     {4, "bink"},
  83.     {6, "mynode"},
  84.     {6, "system"},
  85.     {5, "sysop"},
  86.     {6, "yrnode"},
  87.     {7, "request"},
  88.     {6, "status"},
  89.     {5, "abort"},
  90.     {4, "exit"},
  91.     {4, "line"},
  92.     {0, NULL}
  93. };
  94.  
  95.  
  96. /*
  97.  * Build a Response packet (used to be .RSP file)
  98.  *
  99.  * data   : input = response, output = filename
  100.  * failure: failure code from file req module
  101.  */
  102.  
  103. void Make_Response( char *data, int failure )
  104. {
  105.     FILE *Template, *Response;
  106.     char *dummy_text;
  107.     char *text;
  108.     char *failure_text[10];
  109.     char resp_filename[13];
  110.     char *p, *q, *s;
  111.     struct tm *tp;
  112.     time_t ltime;
  113.     int i;
  114.  
  115. #ifdef DEBUG
  116.     status_line(">Make_Response(%s,%d)", data, failure);
  117. #endif
  118.  
  119.     if (Netmail_Session == 2)
  120.       {
  121.       text = strchr (data, ' ');
  122.       if (text != NULL)
  123.          *text = '\0';
  124.       }
  125.  
  126.     Template = Response = NULL;
  127.  
  128.     for (i = 0; i < 9; i++)
  129.        failure_text[i] = NULL;
  130.  
  131.     if (((text = malloc (256)) == NULL)
  132.     || ((dummy_text = malloc (256)) == NULL))
  133.        goto resp_failed;
  134.  
  135.     (void) time (<ime);
  136.     tp = localtime (<ime);
  137.     
  138.     if ((Template = fopen (CurrentReqTemplate, read_ascii)) == NULL)
  139.       {
  140.       (void) got_error (msgtxt[M_OPEN_MSG], CurrentReqTemplate);
  141.       goto resp_failed;
  142.       }
  143.  
  144.  
  145. #ifndef RSP
  146.     if(!SendRSP)
  147.         invent_pkt_name(resp_filename);
  148.     else
  149. #endif
  150.         (void) sprintf (resp_filename, "%s.RSP", Hex_Addr_Str (&alias[assumed].ad));
  151.  
  152.  
  153.     if ((Response = fopen (resp_filename, write_binary)) == NULL)
  154.       {
  155.       (void) got_error (msgtxt[M_OPEN_MSG], resp_filename);
  156.       goto resp_failed;
  157.       }
  158.     
  159. #ifndef RSP
  160.         /* Build a packet header... mostly copied from ftsc.c */
  161.         
  162.         if(!SendRSP)
  163.         {
  164. #ifdef PKT_2_2
  165.             struct _pkthdr packet;
  166.             
  167. #ifdef DEBUG
  168.             status_line(">Writing 2.2 Response NetMail packet %s", resp_filename);
  169. #endif
  170.  
  171.             /* Make it a type 2.2 packet */
  172.  
  173.             memset(&packet, 0, sizeof(packet));
  174.  
  175.             packet.product = isBITBRAIN;
  176.             packet.ver.w = PUTWORD(PKTVER);
  177.             if (n_getpassword (&remote_addr))
  178.             {
  179.                 if (remote_password != NULL)
  180.                 {
  181.                     strupr (remote_password);
  182.                     strncpy (packet.password, remote_password, 8);
  183.                 }
  184.             }
  185.             packet.orig_zone.w = PUTWORD(alias[assumed].ad.Zone);
  186.             packet.dest_zone.w = PUTWORD(remote_addr.Zone);
  187.  
  188.             packet.orig_node.w = PUTWORD(alias[assumed].ad.Node);
  189.             packet.dest_node.w = PUTWORD(remote_addr.Node);
  190.             packet.dest_net.w = PUTWORD(remote_addr.Net);
  191.             packet.orig_net.w = PUTWORD(alias[assumed].ad.Net);
  192.             packet.year.w = PUTWORD(alias[assumed].ad.Point);
  193.             packet.month.w = PUTWORD(remote_addr.Point);
  194.             packet.day.w = PUTWORD(0);
  195.             packet.hour.w = PUTWORD(0);
  196.             packet.minute.w = PUTWORD(0);
  197.             packet.second.w = PUTWORD(0);
  198.             packet.rate.w = PUTWORD(2);
  199.             if(alias[assumed].ad.Domain)
  200.             {
  201.                 for(i = 0; domain_name[i]; i++)
  202.                 {
  203.                     if(domain_name[i] == alias[assumed].ad.Domain)
  204.                         break;
  205.                 }
  206.                 if(domain_abbrev[i])
  207.                     strncpy (packet.B_fill2, domain_abbrev[i], 8);
  208.             }
  209.             if(remote_addr.Domain)
  210.             {
  211.                 for(i = 0; domain_name[i]; i++)
  212.                 {
  213.                     if (domain_name[i] == remote_addr.Domain)
  214.                         break;
  215.                 }
  216.                 if(domain_abbrev[i])
  217.                     strncpy (&(packet.B_fill2[8]), domain_abbrev[i], 8);
  218.             }
  219. #else
  220.         /* Generate FSC-0048 packet */
  221.         
  222.             PKT_HEADER packet;
  223.             time_t thetime;
  224.             struct tm *t;
  225.             
  226. #ifdef DEBUG
  227.             status_line(">Writing FSC-0048 Response NetMail packet %s", resp_filename);
  228. #endif
  229.             time(&thetime);
  230.             t = localtime(&thetime);
  231.  
  232.             memset(&packet, 0, sizeof(packet));
  233.  
  234.             packet.productCode = packet.ProductCodeCopy = isBITBRAIN;
  235.             packet.ver.w = PUTWORD(PKTVER);
  236.             if(n_getpassword (&remote_addr))
  237.             {
  238.                 if (remote_password != NULL)
  239.                 {
  240.                     strupr (remote_password);
  241.                     strncpy (packet.password, remote_password, 8);
  242.                 }
  243.             }
  244.             packet.origZone.w = packet.origZone2.w = PUTWORD(alias[assumed].ad.Zone);
  245.             packet.destZone.w = packet.destZone2.w = PUTWORD(remote_addr.Zone);
  246.  
  247.             packet.origNode.w = PUTWORD(alias[assumed].ad.Node);
  248.             packet.destNode.w = PUTWORD(remote_addr.Node);
  249.             packet.destNet.w = PUTWORD(remote_addr.Net);
  250.  
  251.             packet.AuxNet.w = PUTWORD(alias[assumed].ad.Net);
  252.             if(alias[assumed].ad.Point)
  253.                 packet.origNet.w = PUTWORD(-1);
  254.             else
  255.                 packet.origNet.w = PUTWORD(alias[assumed].ad.Net);
  256.             packet.origPoint.w = PUTWORD(alias[assumed].ad.Point);
  257.             packet.destPoint.w = PUTWORD(remote_addr.Point);
  258.  
  259.             packet.year.w = PUTWORD(t->tm_year+1900);
  260.             packet.month.w = PUTWORD(t->tm_mon);
  261.             packet.day.w = PUTWORD(t->tm_mday);
  262.             packet.hour.w = PUTWORD(t->tm_hour);
  263.             packet.minute.w = PUTWORD(t->tm_min);
  264.             packet.second.w = PUTWORD(t->tm_sec);
  265.             packet.baud.w = PUTWORD(cur_baud);
  266.         
  267.             packet.CapabilWord.w = PUTWORD(1);
  268.             packet.CWvalidationCopy.w = PUTWORD(0x100);
  269. #endif
  270.  
  271.             fwrite (&packet, sizeof (struct _pkthdr), 1, Response);
  272.         }
  273.  
  274.         if(!SendRSP)
  275.         {    /* Start a packed message */
  276.             struct {
  277.                 MWORD ver;
  278.                 MWORD origNode;
  279.                 MWORD destNode;
  280.                 MWORD origNet;
  281.                 MWORD destNet;
  282.                 MWORD Attribute;
  283.                 MWORD cost;
  284.             } msg;
  285.             char thetime[20];
  286.             time_t t;
  287.             struct tm *tptr;
  288.  
  289.             memset(&msg, 0, sizeof(msg));
  290.             msg.ver.w = PUTWORD(2);
  291.  
  292. #if 1    /* Use FakeNet? */
  293.             if(alias[assumed].ad.Point)
  294.             {
  295.                 msg.origNet.w = PUTWORD(alias[assumed].fakenet);
  296.                 msg.origNode.w = PUTWORD(alias[assumed].ad.Point);
  297.             }
  298.             else
  299. #endif
  300.             {
  301.                 msg.origNet.w = PUTWORD(alias[assumed].ad.Net);
  302.                 msg.origNode.w = PUTWORD(alias[assumed].ad.Node);
  303.             }
  304.  
  305. #if 1
  306.             if(remote_addr.Point &&
  307.                (remote_addr.Net == alias[assumed].ad.Net) &&
  308.                (remote_addr.Node == alias[assumed].ad.Node) )
  309.             {
  310.                 msg.destNet.w = PUTWORD(alias[assumed].fakenet);
  311.                 msg.destNode.w = PUTWORD(remote_addr.Point);
  312.  
  313.                 msg.origNet.w = PUTWORD(alias[assumed].fakenet);
  314.                 msg.origNode.w = PUTWORD(alias[assumed].ad.Point);
  315.  
  316.             }
  317.             else
  318. #endif
  319.             {
  320.                 msg.destNode.w = PUTWORD(remote_addr.Node);
  321.                 msg.destNet.w = PUTWORD(remote_addr.Net);
  322.             }
  323.  
  324.             msg.Attribute.w = PUTWORD(1);        /* Private */
  325.             msg.cost.w = PUTWORD(0);
  326.             fwrite(&msg, sizeof(msg), 1, Response);
  327.  
  328.             /* Make the DateTime */
  329.  
  330.             time(&t);
  331.             tptr = localtime(&t);
  332.             strftime(thetime, sizeof(thetime), "%d %b %y  %H:%M:%S", tptr);
  333.             fwrite(thetime, sizeof(thetime), 1, Response);
  334.  
  335.             /* To user name */
  336.             
  337.             fputs("SysOp", Response);
  338.             fputc(0, Response);
  339.             
  340.             /* From User name */
  341.             
  342.             fputs(xfer_id, Response);
  343.             fputc(0, Response);
  344.             
  345.             /* Subject */
  346.             
  347.             fputs("File Request", Response);
  348.             fputc(0, Response);
  349.             
  350.             /* Text */
  351.  
  352.             if(alias[assumed].ad.Point)
  353.                 fprintf(Response, "\001FMPT: %d\n", alias[assumed].ad.Point);
  354.             if(remote_addr.Point)
  355.                 fprintf(Response, "\001TOPT: %d\n", remote_addr.Point);
  356.             if(remote_addr.Zone != alias[assumed].ad.Zone)
  357.                 fprintf(Response, "\001INTL: %d:%d/%d %d:%d/%d\n",
  358.                     remote_addr.Zone, remote_addr.Net, remote_addr.Node,
  359.                     alias[assumed].ad.Zone, alias[assumed].ad.Net, alias[assumed].ad.Node);
  360.             /* Also do ^Domain */
  361.         }
  362. #endif
  363.  
  364.  
  365.     while (!feof (Template))
  366.        {
  367.  
  368. read_line:
  369.  
  370.        e_input[0] = '\0';
  371.        if (fgets (text, 254, Template) == NULL)
  372.           break;
  373.  
  374.        if (text[0] == '%' && text[1] == ';')
  375.           continue;                     /* Comment at start, no output */
  376.        
  377.        p = text;
  378.        q = e_input;
  379.  
  380.        while (*p)
  381.           {
  382.  
  383.           if (*p == '\n')                /* All done if newline seen    */
  384.              break;
  385.  
  386.           if (*p != '%')                /* Copy until(unless) we see % */
  387.              {
  388.              *q++ = *p++;
  389.              continue;
  390.              }
  391.              
  392.           if (*++p == ';')                /* If followed by ; just skip  */
  393.              break;
  394.  
  395.           if ((i = parse(p, rspverbs)) == -1)/* Check against arg list */
  396.              {
  397.              *q++ = '%';                /* No match, use the % literal */
  398.              continue;
  399.              }
  400.           
  401.           switch (i)
  402.           
  403.              {
  404.              
  405.              case 1:                    /* "text"                       */
  406.  
  407.              if (((i = atoi (p = skip_blanks (&p[4]))) < 1) || (i > 9))
  408.                 goto read_line; 
  409.  
  410.              if (!*(p = skip_to_blank (p)) || !*++p)
  411.                 goto read_line; 
  412.              
  413.              if (failure_text[--i] != NULL)
  414.                 free(failure_text[i]);
  415.  
  416.              failure_text[i] = s = malloc (1 + strlen (p));
  417.              if (s == NULL)
  418.                 goto read_line;
  419.              while ((*p) && (*p != '\n'))
  420.                 *s++ = *p++;
  421.              *s++ = '\0';
  422.  
  423.              goto read_line;
  424.              
  425.              case 2:                    /* "date"                       */
  426.              p += 4;
  427.              (void) sprintf (dummy_text,"%2d-%3s-%02d", tp->tm_mday, mtext[tp->tm_mon], tp->tm_year);
  428. scopy:
  429.              s = dummy_text;
  430. ccopy:
  431.              while (*s)
  432.                *q++ = *s++;
  433.              
  434.              break;
  435.              
  436.              case 3:                    /* "time"                       */
  437.              p += 4;
  438.              (void) sprintf (dummy_text, "%2d:%02d", tp->tm_hour, tp->tm_min);
  439.              goto scopy;
  440.              
  441.              case 4:                    /* "bink"                       */
  442.              p += 4;
  443.              s = ANNOUNCE;
  444.              goto ccopy;
  445.              
  446.              case 5:                    /* "mynode"                    */
  447.              p += 6;
  448.              (void) sprintf (dummy_text, "%s", Pretty_Addr_Str (&alias[assumed].ad));
  449.              goto scopy;
  450.              
  451.              case 6:                    /* "system"                    */
  452.              p += 6;
  453.              s = system_name;
  454.              goto ccopy;
  455.              
  456.              case 7:                    /* "sysop"                       */
  457.              p += 5;
  458.              s = sysop;
  459.              goto ccopy;
  460.              
  461.              case 8:                    /* "yrnode"                    */
  462.              p += 6;
  463.              (void) sprintf (dummy_text, "%s", Pretty_Addr_Str (&remote_addr));
  464.              goto scopy;
  465.              
  466.              case 9:                    /* "request"                   */
  467.              p += 7;
  468.              s = data;
  469.              goto ccopy;
  470.              
  471.              case 10:                    /* "status"                    */
  472.                  p += 6;
  473.                  strcpy (dummy_text, p);
  474.                 if(failure_text[failure-1])
  475.                     strcpy (text, failure_text [failure-1]);
  476.                  strcat (text, dummy_text);
  477.                  p = text;
  478.                  break;
  479.  
  480.              case 11:                    /* "abort"                       */
  481.  
  482.              if (*(p = skip_blanks (&p[5])))/* If there's an argument, */
  483.                 {
  484.                 if (failure != atoi (p))/* See if it matches failure   */
  485.                    goto read_line;        /* No, keep going.               */
  486.                 }
  487.  
  488.              fclose (Response);           /* Abort things: Close file,   */
  489.              Response = NULL;            /* Keep track of closed file   */
  490.              unlink (resp_filename);    /* Then delete it              */
  491.              goto resp_failed;            /* And take the failure exit   */
  492.  
  493.              case 12:                    /* "exit"                       */
  494.  
  495.              if (*(p = skip_blanks (&p[4])))/* If there's an argument, */
  496.                 {
  497.                 if (failure != atoi (p))/* See if it matches failure   */
  498.                    goto read_line;        /* No, keep going.               */
  499.                 }
  500.              goto resp_done;       /* A match, close the file      */
  501.  
  502.              case 13:                    /* "line"                       */
  503.  
  504.              if ((!(*(p = skip_blanks (&p[4]))))
  505.              || (failure != atoi (p)))    /* If argument doesn't match,  */
  506.                    goto read_line;        /* throw out line, keep going. */
  507.              if (*(p = skip_to_blank (p))) /* If there's any text,       */
  508.                 {
  509.                 (void) strcpy (text, ++p);       /* Copy rest of line down      */
  510.                 p = text;                /* Move pointer to front       */
  511.                 }
  512.              break;
  513.  
  514.              }            /* End switch                  */
  515.           }             /* End while *p               */
  516.  
  517.        *q++ = '\r';
  518.        *q++ = '\n';
  519.        *q = '\0';
  520.  
  521.        (void) fwrite (e_input, q - e_input, 1, Response);
  522.        }                /* End while !feof (Template) */
  523.  
  524. resp_done:
  525.  
  526.    strcpy (data, resp_filename);
  527.  
  528. #if 0
  529.    fclose (Template);
  530.    Template = NULL;
  531. #endif
  532.  
  533. #ifndef RSP
  534.     if(!SendRSP)
  535.     {
  536.  
  537.         /* Finish the packet */
  538.     
  539.         fputc(0, Response);    /* terminate text */
  540.         fputc(0, Response);    /* terminate packet */
  541.         fputc(0, Response);
  542.     }
  543.  
  544. #endif
  545.  
  546. #if 0
  547.    fclose (Response);
  548.    Response = NULL;
  549. #endif
  550.    goto cleanup;
  551.  
  552. resp_failed:
  553.  
  554.    *data = '\0';
  555.  
  556. cleanup:
  557.    if (Response != NULL)
  558.       fclose (Response);
  559.  
  560.    if (Template != NULL)
  561.       fclose (Template);
  562.  
  563.  
  564.    for (i = 0; i < 9; i++)
  565.        {
  566.        if (failure_text[i] != NULL)
  567.           {
  568.           free(failure_text[i]);
  569.           failure_text[i] = NULL;
  570.           }
  571.        }
  572.  
  573.    if (text != NULL)
  574.       free (text);
  575.  
  576.    if (dummy_text != NULL)
  577.       free (dummy_text);
  578.  
  579.    return;
  580. }
  581.