home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1996 October / PCO_10.ISO / filesbbs / bsrc_260.arj / SRC.ZIP / b_rspfil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-20  |  12.2 KB  |  429 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-96, Bit Bucket Software Co.              */
  11. /*                                                                          */
  12. /*               This module was written by Vince Perriello                 */
  13. /*                                                                          */
  14. /*             BinkleyTerm File Request Failure Message Module              */
  15. /*                                                                          */
  16. /*                                                                          */
  17. /*    For complete  details  of the licensing restrictions, please refer    */
  18. /*    to the License  agreement,  which  is published in its entirety in    */
  19. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.260.    */
  20. /*                                                                          */
  21. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  22. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  23. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  24. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  25. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  26. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  27. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  28. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  29. /*                                                                          */
  30. /*                                                                          */
  31. /* You can contact Bit Bucket Software Co. at any one of the following      */
  32. /* addresses:                                                               */
  33. /*                                                                          */
  34. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  35. /* P.O. Box 460398                AlterNet 7:42/1491                        */
  36. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  37. /*                                Internet f491.n343.z1.fidonet.org         */
  38. /*                                                                          */
  39. /* Please feel free to contact us at any time to share your comments about  */
  40. /* our software and/or licensing policies.                                  */
  41. /*                                                                          */
  42. /*--------------------------------------------------------------------------*/
  43.  
  44. /* Include this file before any other includes or defines! */
  45.  
  46. #include "includes.h"
  47.  
  48. typedef struct
  49. {                                /* MB 93-12-12  PKTRSP */
  50.     short ver;
  51.     short orig_node;
  52.     short dest_node;
  53.     short orig_net;
  54.     short dest_net;
  55.     short attr;
  56.     short cost;
  57. } PACKED, *PACKED_PTR;
  58.  
  59. #define ATTRIB     129
  60.  
  61. void Make_Response (char *, int);    /* Build a response      */
  62.  
  63. void 
  64. Make_Response (char *data, int failure)
  65. {
  66.     FILE *Template, *Response;
  67.     char *dummy_text;
  68.     char *text;
  69.     char *failure_text[10];
  70.     char resp_filename[80];
  71.     char *p, *q, *s;
  72.     struct tm *tp;
  73.     time_t ltime;
  74.     int i;
  75.     struct _pkthdr45 pkt_hdr;
  76.     PACKED p_msg;
  77.     ADDR real_addr;
  78.  
  79.     if (Netmail_Session == 2)
  80.     {
  81.         text = strchr (data, ' ');
  82.         if (text != NULL)
  83.             *text = '\0';
  84.     }
  85.  
  86.     Template = Response = (FILE *) NULL;
  87.     dummy_text = NULL;
  88.  
  89.     for (i = 0; i < 9; i++)
  90.         failure_text[i] = NULL;
  91.  
  92.     if (((text = calloc (1, 256)) == NULL)
  93.         || ((dummy_text = calloc (1, 256)) == NULL))
  94.         goto resp_failed;
  95.  
  96.     (void) time (<ime);
  97.     tp = localtime (<ime);
  98.  
  99.     if ((Template = share_fopen (CURRENT.rq_Template, read_ascii, DENY_WRITE)) == NULL)
  100.     {
  101.         (void) got_error (MSG_TXT (M_OPEN_MSG), CURRENT.rq_Template);
  102.         goto resp_failed;
  103.     }
  104.  
  105.     if (pktrsp)                    /* MB 93-12-12  PKTRSP */
  106.     {
  107.         invent_pkt_name (resp_filename);    /* prepare unique response filename */
  108.         (void) sprintf (dummy_text, "%02x", TaskNumber);
  109.         (void) strncpy (resp_filename, dummy_text, 2);
  110.         if (failure == 10)
  111.         {
  112.             (void) sprintf (dummy_text, "%s%s", PROT.sc_Inbound, resp_filename);
  113.             assumed = 0;
  114.         }
  115.         else
  116.             (void) sprintf (dummy_text, "%s%s", flag_dir, resp_filename);
  117.         (void) strcpy (resp_filename, dummy_text);
  118.     }
  119.     else
  120.     {
  121.         if (alias[assumed].Point != 0)
  122.             (void) sprintf (resp_filename, "%08hx.RSP", alias[assumed].Point);
  123.         else
  124.             (void) sprintf (resp_filename, "%s.RSP", Hex_Addr_Str (&(alias[assumed])));
  125.     }
  126.  
  127.  
  128.     if ((Response = fopen (resp_filename, write_binary)) == NULL)
  129.     {
  130.         (void) got_error (MSG_TXT (M_OPEN_MSG), resp_filename);
  131.         goto resp_failed;
  132.     }
  133.  
  134.     if (pktrsp)                    /* MB 93-12-12  PKTRSP */
  135.     {
  136.         real_addr = remote_addr;
  137.         if ((pvtnet >= 0) && (remote_addr.Net == (word) pvtnet) &&
  138.             (boss_addr.Zone == remote_addr.Zone) &&
  139.             (remote_addr.Point == 0))
  140.         {
  141.             real_addr.Point = remote_addr.Node;
  142.             real_addr.Net = boss_addr.Net;
  143.             real_addr.Node = boss_addr.Node;
  144.         }
  145.         /* set up pkt and msg headers */
  146.  
  147.         memset (&pkt_hdr, 0, sizeof (pkt_hdr));
  148.  
  149.         pkt_hdr.orig_zone = alias[assumed].Zone;
  150.         pkt_hdr.orig_net = p_msg.orig_net = alias[assumed].Net;
  151.         pkt_hdr.orig_node = p_msg.orig_node = alias[assumed].Node;
  152.         pkt_hdr.orig_point = alias[assumed].Point;
  153.  
  154.         pkt_hdr.dest_zone = remote_addr.Zone;
  155.         pkt_hdr.dest_net = remote_addr.Net;
  156.         pkt_hdr.dest_point = remote_addr.Point;
  157.  
  158.         p_msg.dest_net = real_addr.Net;
  159.         p_msg.dest_node = real_addr.Node;
  160.  
  161.         pkt_hdr.ver = p_msg.ver = PKTVER;
  162.         pkt_hdr.subver = 2;
  163.  
  164.         pkt_hdr.product = PRDCT_CODE;
  165.         pkt_hdr.serial = 0;
  166.  
  167.         p_msg.attr = ATTRIB;
  168.  
  169.         /* write pkt header */
  170.  
  171.         (void) fwrite ((char *) &pkt_hdr, sizeof (struct _pkthdr), 1, Response);
  172.  
  173.         /* write msg header */
  174.  
  175.         (void) fwrite ((char *) &p_msg, sizeof (PACKED), 1, Response);
  176.  
  177.         /* write date */
  178.  
  179.         (void) sprintf (dummy_text, "%02d %3.3s %02d %02d:%02d:02d",
  180.             tp->tm_mday, mtext[tp->tm_mon], tp->tm_year,
  181.             tp->tm_hour, tp->tm_min, tp->tm_sec);
  182.         (void) fwrite (dummy_text, strlen (dummy_text) + 1, 1, Response);
  183.  
  184.         /* write sysop */
  185.  
  186.         (void) fwrite ("Sysop", 6, 1, Response);
  187.  
  188.         /* write your name */
  189.  
  190.         (void) fwrite (sysop, strlen (sysop) + 1, 1, Response);
  191.  
  192.         /* write subject */
  193.  
  194.         (void) fwrite ("Response to File Request", 25, 1, Response);
  195.  
  196.         /* write extended flags */
  197.  
  198.         if (alias[assumed].Zone != remote_addr.Zone)
  199.         {
  200.             sprintf (dummy_text, "\001INTL %d:%d/%d %d:%d/%d\r",
  201.                 real_addr.Zone, real_addr.Net, real_addr.Node,
  202.                 alias[assumed].Zone, alias[assumed].Net,
  203.                 alias[assumed].Node);
  204.             (void) fwrite (dummy_text, strlen (dummy_text), 1, Response);
  205.         }
  206.         if (alias[assumed].Point > 0)
  207.         {
  208.             sprintf (dummy_text, "\001FMPT %d\r", alias[assumed].Point);
  209.             (void) fwrite (dummy_text, strlen (dummy_text), 1, Response);
  210.         }
  211.         if (real_addr.Point > 0)
  212.         {
  213.             sprintf (dummy_text, "\001TOPT %d\r", real_addr.Point);
  214.             (void) fwrite (dummy_text, strlen (dummy_text), 1, Response);
  215.         }
  216.         (void) fwrite ("\r\n\r\n", 4, 1, Response);
  217.     }
  218.  
  219.     while (!feof (Template))
  220.     {
  221.  
  222. read_line:
  223.  
  224.         e_input[0] = '\0';
  225.         if (fgets (text, 254, Template) == NULL)
  226.             break;
  227.  
  228.         if (text[0] == '%' && text[1] == ';')
  229.             continue;                        /* Comment at start, no output  */
  230.  
  231.         p = text;
  232.         q = e_input;
  233.  
  234.         while (*p)
  235.         {
  236.  
  237.             if (*p == '\n')                    /* All done if newline seen    */
  238.                 break;
  239.  
  240.             if (*p != '%')                    /* Copy until(unless) we see % */
  241.             {
  242.                 *q++ = *p++;
  243.                 continue;
  244.             }
  245.  
  246.             if (*++p == ';')                /* If followed by ; just skip  */
  247.                 break;
  248.  
  249.             if ((i = parse (p, rspverbs)) == -1) /* Check against arg list */
  250.             {
  251.                 *q++ = '%';                    /* No match, use the % literal */
  252.                 continue;
  253.             }
  254.  
  255.             switch (i)
  256.             {
  257.  
  258.             case 1:            /* "text"       */
  259.  
  260.                 if (((i = atoi (p = skip_blanks (&p[4]))) < 1) || (i > 9))
  261.                     goto read_line;
  262.  
  263.                 if (!*(p = skip_to_blank (p)) || !*++p)
  264.                     goto read_line;
  265.  
  266.                 if (failure_text[--i] != NULL)
  267.                 {
  268.                     free (failure_text[i]);
  269.                     failure_text[i] = NULL;
  270.                 }
  271.  
  272.                 failure_text[i] = s = calloc (1, 1 + strlen (p));
  273.                 if (s == NULL)
  274.                     goto read_line;
  275.                 while ((*p) && (*p != '\n'))
  276.                     *s++ = *p++;
  277.                 *s++ = '\0';
  278.  
  279.                 goto read_line;
  280.  
  281.             case 2:            /* "date"       */
  282.                 p += 4;
  283.                 (void) sprintf (dummy_text, "%2d-%3s-%02d", tp->tm_mday, mtext[tp->tm_mon], tp->tm_year);
  284.               scopy:
  285.                 s = dummy_text;
  286.               ccopy:
  287.                 while (*s)
  288.                     *q++ = *s++;
  289.  
  290.                 break;
  291.  
  292.             case 3:            /* "time"       */
  293.                 p += 4;
  294.                 (void) sprintf (dummy_text, "%2d:%02d", tp->tm_hour, tp->tm_min);
  295.                 goto scopy;
  296.  
  297.             case 4:            /* "bink"       */
  298.                 p += 4;
  299.                 s = ANNOUNCE;
  300.                 goto ccopy;
  301.  
  302.             case 5:            /* "mynode"     */
  303.                 p += 6;
  304.                 (void) sprintf (dummy_text, "%s", Full_Addr_Str (&(alias[assumed])));
  305.                 goto scopy;
  306.  
  307.             case 6:            /* "system"     */
  308.                 p += 6;
  309.                 s = system_name;
  310.                 goto ccopy;
  311.  
  312.             case 7:            /* "sysop"      */
  313.                 p += 5;
  314.                 s = sysop;
  315.                 goto ccopy;
  316.  
  317.             case 8:            /* "yrnode"     */
  318.                 p += 6;
  319.                 (void) sprintf (dummy_text, "%s", Full_Addr_Str (&remote_addr));
  320.                 goto scopy;
  321.  
  322.             case 9:            /* "request"    */
  323.                 p += 7;
  324.                 s = data;
  325.                 goto ccopy;
  326.  
  327.             case 10:        /* "status"     */
  328.                 p += 6;
  329.                 (void) strcpy (dummy_text, p);
  330.                 if (failure_text[failure - 1] == NULL)
  331.                     text[0] = '\0';
  332.                 else
  333.                     (void) strcpy (text, failure_text[failure - 1]);
  334.                 (void) strcat (text, dummy_text);
  335.                 p = text;
  336.                 break;
  337.  
  338.             case 11:        /* "abort"      */
  339.  
  340.                 if (*(p = skip_blanks (&p[5])))    /* If there's an argument, */
  341.                 {
  342.                     if (failure != atoi (p))        /* See if it matches failure   */
  343.                         goto read_line;                /* No, keep going.             */
  344.                 }
  345.  
  346.                 (void) fclose (Response);            /* Abort things: Close file,   */
  347.                 Response = NULL;                        /* Keep track of closed file   */
  348.                 (void) unlink (resp_filename);    /* Then delete it              */
  349.                 goto resp_failed;                        /* And take the failure exit   */
  350.  
  351.             case 12:        /* "exit"       */
  352.  
  353.                 if (*(p = skip_blanks (&p[4])))    /* If there's an argument, */
  354.                 {
  355.                     if (failure != atoi (p))        /* See if it matches failure   */
  356.                         goto read_line;                /* No, keep going.             */
  357.                 }
  358.                 goto resp_done;                        /* A match, close the file     */
  359.  
  360.             case 13:        /* "line"       */
  361.  
  362.                 if ((!(*(p = skip_blanks (&p[4]))))
  363.                     || (failure != atoi (p)))        /* If argument doesn't match,  */
  364.                     goto read_line;                    /* throw out line, keep going. */
  365.  
  366.                 if (*(p = skip_to_blank (p)))        /* If there's any text,        */
  367.                 {
  368.                     (void) strcpy (text, ++p);        /* Copy rest of line down      */
  369.                     p = text;                            /* Move pointer to front       */
  370.                 }
  371.                 break;
  372.  
  373.             }                /* End switch   */
  374.         }                    /* End while *p */
  375.  
  376.         *q++ = '\r';
  377.         *q++ = '\n';
  378.         *q = '\0';
  379.  
  380.         i = (int) (q - e_input);
  381.         (void) fwrite (e_input, (unsigned int) i, 1, Response);
  382.     }                        /* End while !feof (Template) */
  383.  
  384. resp_done:
  385.  
  386.     (void) strcpy (data, resp_filename);
  387.  
  388.     (void) fclose (Template);
  389.     Template = NULL;
  390.  
  391.     /* write the final null *//* MB 93-12-12  PKTRSP */
  392.     if (pktrsp)
  393.         (void) fwrite ("\r\n\r\n\0\0\0", 7, 1, Response);
  394.  
  395.     (void) fclose (Response);
  396.     Response = NULL;
  397.     goto cleanup;
  398.  
  399. resp_failed:
  400.  
  401.     *data = '\0';
  402.  
  403.     if (Response != NULL)
  404.         (void) fclose (Response);
  405.  
  406.     if (Template != NULL)
  407.         (void) fclose (Template);
  408.  
  409. cleanup:
  410.  
  411.     for (i = 0; i < 9; i++)
  412.     {
  413.         if (failure_text[i] != NULL)
  414.         {
  415.             free (failure_text[i]);
  416.             failure_text[i] = NULL;
  417.         }
  418.     }
  419.  
  420.     if (text != NULL)
  421.         free (text);
  422.  
  423.     if (dummy_text != NULL)
  424.         free (dummy_text);
  425.  
  426.     return;
  427. }
  428.  
  429.