home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / DOOR / BD2K260A.ZIP / SRC.ZIP / SRC / B_RSPFIL.C next >
C/C++ Source or Header  |  1998-10-03  |  13KB  |  443 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. #ifdef Y2K_FIXES
  180.         /* [Y2K] Fixed, forced 2-digit year to be printed.
  181.          */
  182.         (void) sprintf (dummy_text, "%02d %3.3s %02d %02d:%02d:02d",
  183.             tp->tm_mday, mtext[tp->tm_mon], TWO_DIGIT_YEAR(tp->tm_year),
  184.             tp->tm_hour, tp->tm_min, tp->tm_sec);
  185. #else
  186.         (void) sprintf (dummy_text, "%02d %3.3s %02d %02d:%02d:02d",
  187.             tp->tm_mday, mtext[tp->tm_mon], tp->tm_year,
  188.             tp->tm_hour, tp->tm_min, tp->tm_sec);
  189. #endif
  190.         (void) fwrite (dummy_text, strlen (dummy_text) + 1, 1, Response);
  191.  
  192.         /* write sysop */
  193.  
  194.         (void) fwrite ("Sysop", 6, 1, Response);
  195.  
  196.         /* write your name */
  197.  
  198.         (void) fwrite (sysop, strlen (sysop) + 1, 1, Response);
  199.  
  200.         /* write subject */
  201.  
  202.         (void) fwrite ("Response to File Request", 25, 1, Response);
  203.  
  204.         /* write extended flags */
  205.  
  206.         if (alias[assumed].Zone != remote_addr.Zone)
  207.         {
  208.             sprintf (dummy_text, "\001INTL %d:%d/%d %d:%d/%d\r",
  209.                 real_addr.Zone, real_addr.Net, real_addr.Node,
  210.                 alias[assumed].Zone, alias[assumed].Net,
  211.                 alias[assumed].Node);
  212.             (void) fwrite (dummy_text, strlen (dummy_text), 1, Response);
  213.         }
  214.         if (alias[assumed].Point > 0)
  215.         {
  216.             sprintf (dummy_text, "\001FMPT %d\r", alias[assumed].Point);
  217.             (void) fwrite (dummy_text, strlen (dummy_text), 1, Response);
  218.         }
  219.         if (real_addr.Point > 0)
  220.         {
  221.             sprintf (dummy_text, "\001TOPT %d\r", real_addr.Point);
  222.             (void) fwrite (dummy_text, strlen (dummy_text), 1, Response);
  223.         }
  224.         (void) fwrite ("\r\n\r\n", 4, 1, Response);
  225.     }
  226.  
  227.     while (!feof (Template))
  228.     {
  229.  
  230. read_line:
  231.  
  232.         e_input[0] = '\0';
  233.         if (fgets (text, 254, Template) == NULL)
  234.             break;
  235.  
  236.         if (text[0] == '%' && text[1] == ';')
  237.             continue;                        /* Comment at start, no output  */
  238.  
  239.         p = text;
  240.         q = e_input;
  241.  
  242.         while (*p)
  243.         {
  244.  
  245.             if (*p == '\n')                    /* All done if newline seen    */
  246.                 break;
  247.  
  248.             if (*p != '%')                    /* Copy until(unless) we see % */
  249.             {
  250.                 *q++ = *p++;
  251.                 continue;
  252.             }
  253.  
  254.             if (*++p == ';')                /* If followed by ; just skip  */
  255.                 break;
  256.  
  257.             if ((i = parse (p, rspverbs)) == -1) /* Check against arg list */
  258.             {
  259.                 *q++ = '%';                    /* No match, use the % literal */
  260.                 continue;
  261.             }
  262.  
  263.             switch (i)
  264.             {
  265.  
  266.             case 1:            /* "text"       */
  267.  
  268.                 if (((i = atoi (p = skip_blanks (&p[4]))) < 1) || (i > 9))
  269.                     goto read_line;
  270.  
  271.                 if (!*(p = skip_to_blank (p)) || !*++p)
  272.                     goto read_line;
  273.  
  274.                 if (failure_text[--i] != NULL)
  275.                 {
  276.                     free (failure_text[i]);
  277.                     failure_text[i] = NULL;
  278.                 }
  279.  
  280.                 failure_text[i] = s = calloc (1, 1 + strlen (p));
  281.                 if (s == NULL)
  282.                     goto read_line;
  283.                 while ((*p) && (*p != '\n'))
  284.                     *s++ = *p++;
  285.                 *s++ = '\0';
  286.  
  287.                 goto read_line;
  288.  
  289.             case 2:            /* "date"       */
  290.                 p += 4;
  291. #ifdef Y2K_FIXES
  292.                 /* [Y2K] - Fixed, forced two-digit year to be printed.
  293.                  */
  294.                 (void) sprintf (dummy_text, "%2d-%3s-%02d", tp->tm_mday, mtext[tp->tm_mon], TWO_DIGIT_YEAR(tp->tm_year));
  295. #else
  296.                 (void) sprintf (dummy_text, "%2d:%02d", tp->tm_hour, tp->tm_min);
  297. #endif
  298.               scopy:
  299.                 s = dummy_text;
  300.               ccopy:
  301.                 while (*s)
  302.                     *q++ = *s++;
  303.  
  304.                 break;
  305.  
  306.             case 3:            /* "time"       */
  307.                 p += 4;
  308.                 (void) sprintf (dummy_text, "%2d:%02d", tp->tm_hour, tp->tm_min);
  309.                 goto scopy;
  310.  
  311.             case 4:            /* "bink"       */
  312.                 p += 4;
  313.                 s = ANNOUNCE;
  314.                 goto ccopy;
  315.  
  316.             case 5:            /* "mynode"     */
  317.                 p += 6;
  318.                 (void) sprintf (dummy_text, "%s", Full_Addr_Str (&(alias[assumed])));
  319.                 goto scopy;
  320.  
  321.             case 6:            /* "system"     */
  322.                 p += 6;
  323.                 s = system_name;
  324.                 goto ccopy;
  325.  
  326.             case 7:            /* "sysop"      */
  327.                 p += 5;
  328.                 s = sysop;
  329.                 goto ccopy;
  330.  
  331.             case 8:            /* "yrnode"     */
  332.                 p += 6;
  333.                 (void) sprintf (dummy_text, "%s", Full_Addr_Str (&remote_addr));
  334.                 goto scopy;
  335.  
  336.             case 9:            /* "request"    */
  337.                 p += 7;
  338.                 s = data;
  339.                 goto ccopy;
  340.  
  341.             case 10:        /* "status"     */
  342.                 p += 6;
  343.                 (void) strcpy (dummy_text, p);
  344.                 if (failure_text[failure - 1] == NULL)
  345.                     text[0] = '\0';
  346.                 else
  347.                     (void) strcpy (text, failure_text[failure - 1]);
  348.                 (void) strcat (text, dummy_text);
  349.                 p = text;
  350.                 break;
  351.  
  352.             case 11:        /* "abort"      */
  353.  
  354.                 if (*(p = skip_blanks (&p[5])))    /* If there's an argument, */
  355.                 {
  356.                     if (failure != atoi (p))        /* See if it matches failure   */
  357.                         goto read_line;                /* No, keep going.             */
  358.                 }
  359.  
  360.                 (void) fclose (Response);            /* Abort things: Close file,   */
  361.                 Response = NULL;                        /* Keep track of closed file   */
  362.                 (void) unlink (resp_filename);    /* Then delete it              */
  363.                 goto resp_failed;                        /* And take the failure exit   */
  364.  
  365.             case 12:        /* "exit"       */
  366.  
  367.                 if (*(p = skip_blanks (&p[4])))    /* If there's an argument, */
  368.                 {
  369.                     if (failure != atoi (p))        /* See if it matches failure   */
  370.                         goto read_line;                /* No, keep going.             */
  371.                 }
  372.                 goto resp_done;                        /* A match, close the file     */
  373.  
  374.             case 13:        /* "line"       */
  375.  
  376.                 if ((!(*(p = skip_blanks (&p[4]))))
  377.                     || (failure != atoi (p)))        /* If argument doesn't match,  */
  378.                     goto read_line;                    /* throw out line, keep going. */
  379.  
  380.                 if (*(p = skip_to_blank (p)))        /* If there's any text,        */
  381.                 {
  382.                     (void) strcpy (text, ++p);        /* Copy rest of line down      */
  383.                     p = text;                            /* Move pointer to front       */
  384.                 }
  385.                 break;
  386.  
  387.             }                /* End switch   */
  388.         }                    /* End while *p */
  389.  
  390.         *q++ = '\r';
  391.         *q++ = '\n';
  392.         *q = '\0';
  393.  
  394.         i = (int) (q - e_input);
  395.         (void) fwrite (e_input, (unsigned int) i, 1, Response);
  396.     }                        /* End while !feof (Template) */
  397.  
  398. resp_done:
  399.  
  400.     (void) strcpy (data, resp_filename);
  401.  
  402.     (void) fclose (Template);
  403.     Template = NULL;
  404.  
  405.     /* write the final null *//* MB 93-12-12  PKTRSP */
  406.     if (pktrsp)
  407.         (void) fwrite ("\r\n\r\n\0\0\0", 7, 1, Response);
  408.  
  409.     (void) fclose (Response);
  410.     Response = NULL;
  411.     goto cleanup;
  412.  
  413. resp_failed:
  414.  
  415.     *data = '\0';
  416.  
  417.     if (Response != NULL)
  418.         (void) fclose (Response);
  419.  
  420.     if (Template != NULL)
  421.         (void) fclose (Template);
  422.  
  423. cleanup:
  424.  
  425.     for (i = 0; i < 9; i++)
  426.     {
  427.         if (failure_text[i] != NULL)
  428.         {
  429.             free (failure_text[i]);
  430.             failure_text[i] = NULL;
  431.         }
  432.     }
  433.  
  434.     if (text != NULL)
  435.         free (text);
  436.  
  437.     if (dummy_text != NULL)
  438.         free (dummy_text);
  439.  
  440.     return;
  441. }
  442.  
  443.