home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / forms.c < prev    next >
C/C++ Source or Header  |  1992-03-15  |  10KB  |  363 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: forms.c,v 4.1 90/04/28 22:43:08 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    forms.c,v $
  17.  * Revision 4.1  90/04/28  22:43:08  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  *
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** This set of files supports the 'forms' options (AT&T Mail Forms) to
  24.     the mail system.  The specs are drawn from a document from AT&T entitled
  25.     "Standard for Exchanging Forms on AT&T Mail", version 1.9.
  26.  
  27. **/
  28.  
  29. /** Some notes on the format of a FORM;
  30.  
  31.     First off, in AT&T Mail parlance, this program only supports SIMPLE
  32.     forms, currently.  This means that while each form must have three
  33.      sections;
  34.  
  35.         [options-section]
  36.         ***
  37.         [form-image]
  38.         ***
  39.         [rules-section]
  40.  
  41.     this program will ignore the first and third sections completely.  The
  42.     program will assume that the user merely enteres the form-image section,
  43.     and will append and prepend the triple asterisk sequences that *MUST*
  44.     be part of the message.  The messages are also expected to have a
  45.     specific header - "Content-Type: mailform" - which will be added on all
  46.     outbound mail and checked on inbound...
  47. **/
  48.  
  49. #include "headers.h"
  50. #include <errno.h>
  51.  
  52. #ifndef OS2
  53. extern int errno;
  54. #endif
  55.  
  56. char *error_name(), *strcat(), *strcpy();
  57.  
  58. check_form_file(filename)
  59. char *filename;
  60. {
  61.     /** This routine returns the number of fields in the specified file,
  62.         or -1 if an error is encountered. **/
  63.  
  64.     FILE *form;
  65.     char buffer[SLEN];
  66.     register int field_count = 0;
  67.  
  68.     if ((form = fopen(filename, "r")) == NULL) {
  69.       error2("Error %s trying to open %s to check fields!",
  70.           error_name(errno), filename);
  71.       return(-1);
  72.     }
  73.  
  74.     while (fgets(buffer, SLEN, form) != NULL) {
  75.       field_count += occurances_of(COLON, buffer);
  76.     }
  77.  
  78.     fclose(form);
  79.  
  80.     return(field_count);
  81. }
  82.  
  83. format_form(filename)
  84. char *filename;
  85. {
  86.     /** This routine accepts a validated file that is the middle
  87.         section of a form message and prepends and appends the appropriate
  88.         instructions.  It's pretty simple.
  89.         This returns the number of forms in the file, or -1 on errors
  90.     **/
  91.  
  92.     FILE *form, *newform;
  93.     char  newfname[SLEN], buffer[SLEN];
  94.     register form_count = 0;
  95.  
  96.     dprint(4, (debugfile, "Formatting form file '%s'\n", filename));
  97.  
  98.     /** first off, let's open the files... **/
  99.  
  100.     if ((form = fopen(filename, "r")) == NULL) {
  101.       error("Can't read the message to validate the form!");
  102.       dprint(1, (debugfile,
  103.               "** Error encountered opening file \"%s\" - %s (check_form) **\n",
  104.           filename, error_name(errno)));
  105.       return(-1);
  106.     }
  107.  
  108.     sprintf(newfname, "%s%d%s", temp_dir, getpid(), temp_form_file);
  109.  
  110.     if ((newform = fopen(newfname, "w")) == NULL) {
  111.       error("Couldn't open newform file for form output!");
  112.       dprint(1, (debugfile,
  113.               "** Error encountered opening file \"%s\" - %s (check_form) **\n",
  114.           newfname, error_name(errno)));
  115.       return(-1);
  116.     }
  117.  
  118.     /** the required header... **/
  119.  
  120.     /* these are actually the defaults, but let's be sure, okay? */
  121.  
  122.     fprintf(newform, "WIDTH=78\nTYPE=SIMPLE\nOUTPUT=TEXT\n***\n");
  123.  
  124.     /** and let's have some fun transfering the stuff across... **/
  125.  
  126.     while (fgets(buffer, SLEN, form) != NULL) {
  127.       fputs(buffer, newform);
  128.       form_count += occurances_of(COLON, buffer);
  129.     }
  130.  
  131.     fprintf(newform, "***\n");    /* that closing bit! */
  132.  
  133.     fclose(form);
  134.     fclose(newform);
  135.  
  136.     if (form_count > 0) {
  137.       if (unlink(filename) != 0) {
  138.         error2("Error %s unlinking file %s.", error_name(errno), filename);
  139.         return(-1);
  140.       }
  141.       if (rename(newfname, filename)) {
  142.         error3("Error %s renaming %s to %s.", error_name(errno),
  143.             newfname, filename);
  144.         return(-1);
  145.       }
  146.     }
  147.         else if (unlink(newfname)) {
  148.       error2("Error %s unlinking file %s.", error_name(errno), newfname);
  149.       return(-1);
  150.     }
  151.  
  152.     return(form_count);
  153. }
  154.  
  155. int
  156. mail_filled_in_form(address, subject)
  157. char *address, *subject;
  158. {
  159.     /** This is the interesting routine.  This one will read the
  160.         message and prompt the user, line by line, for each of
  161.         the fields...returns non-zero if it succeeds
  162.     **/
  163.  
  164.     FILE           *fd;
  165.     register int lines = 0, count;
  166.     char         buffer[SLEN], *ptr;
  167.  
  168.     dprint(4, (debugfile,
  169.         "replying to form with;\n\taddress=%s and\n\t subject=%s\n",
  170.          address, subject));
  171.  
  172.         if (fseek(mailfile, headers[current-1]->offset, 0) == -1) {
  173.       dprint(1, (debugfile,
  174.            "Error: seek %ld resulted in errno %s (%s)\n",
  175.            headers[current-1]->offset, error_name(errno),
  176.            "mail_filled_in_form"));
  177.       error2("ELM [seek] couldn't read %d bytes into file (%s).",
  178.              headers[current-1]->offset, error_name(errno));
  179.       return(0);
  180.         }
  181.  
  182.     /* now we can fly along and get to the message body... */
  183.  
  184.     while ((ptr = fgets(buffer, SLEN, mailfile)) != NULL) {
  185.       if (strlen(buffer) == 1)    /* <return> only */
  186.         break;
  187.       else if (strncmp(buffer,"From ", 5) == 0 && lines++ > 0) {
  188.         error("No form in this message!?");
  189.         return(0);
  190.       }
  191.     }
  192.  
  193.     if (ptr == NULL) {
  194.       error("No form in this message!?");
  195.       return(0);
  196.     }
  197.  
  198.     dprint(6, (debugfile, "- past header of form message -\n"));
  199.  
  200.     /* at this point we're at the beginning of the body of the message */
  201.  
  202.     /* now we can skip to the FORM-IMAGE section by reading through a
  203.        line with a triple asterisk... */
  204.  
  205.     while ((ptr = fgets(buffer, SLEN, mailfile)) != NULL) {
  206.       if (strcmp(buffer, "***\n") == 0)
  207.         break;    /* we GOT it!  It's a miracle! */
  208.       else if (strncmp(buffer, "From ",5) == 0) {
  209.         error("Badly constructed form.  Can't reply!");
  210.         return(0);
  211.       }
  212.     }
  213.  
  214.     if (ptr == NULL) {
  215.       error("Badly constructed form.  Can't reply!");
  216.       return(0);
  217.     }
  218.  
  219.     dprint(6, (debugfile, "- skipped the non-forms-image stuff -\n"));
  220.  
  221.     /* one last thing - let's open the tempfile for output... */
  222.  
  223.     sprintf(buffer, "%s%s%d", temp_dir, temp_form_file, getpid());
  224.  
  225.     dprint(2, (debugfile,"-- forms sending using file %s --\n", buffer));
  226.  
  227.     if ((fd = fopen(buffer,"w")) == NULL) {
  228.       error2("Can't open \"%s\" as output file! (%s).", buffer,
  229.          error_name(errno));
  230.       dprint(1, (debugfile,
  231.           "** Error %s encountered trying to open temp file %s;\n",
  232.           error_name(errno), buffer));
  233.       return(0);
  234.     }
  235.  
  236.     /* NOW we're ready to read the form image in and start prompting... */
  237.  
  238.     Raw(OFF);
  239.     ClearScreen();
  240.  
  241.     while ((ptr = fgets(buffer, SLEN, mailfile)) != NULL) {
  242.       dprint(9, (debugfile, "- read %s", buffer));
  243.       if (strcmp(buffer, "***\n") == 0) /* end of form! */
  244.         break;
  245.  
  246.       switch ((count = occurances_of(COLON, buffer))) {
  247.         case 0 : printf("%s", buffer);        /* output line */
  248.              fprintf(fd, "%s", buffer);
  249.              break;
  250.             case 1 : if (buffer[0] == COLON) {
  251.                    printf(
  252. "(Enter as many lines as needed, ending with a '.' by itself on a line)\n");
  253.                        while (fgets(buffer, SLEN, stdin) != NULL) {
  254.                  no_ret(buffer);
  255.                      if (strcmp(buffer, ".") == 0)
  256.                        break;
  257.                      else
  258.                fprintf(fd,"%s\n", buffer);
  259.                }
  260.                  }
  261.                  else
  262.                prompt_for_entries(buffer, fd, count);
  263.                  break;
  264.             default: prompt_for_entries(buffer, fd, count);
  265.       }
  266.     }
  267.  
  268.     Raw(ON);
  269.     fclose(fd);
  270.  
  271.     /** let's just mail this off now... **/
  272.  
  273.     mail_form(address, subject);
  274.  
  275.     return(1);
  276. }
  277.  
  278. prompt_for_entries(buffer, fd, entries)
  279. char *buffer;
  280. FILE *fd;
  281. int  entries;
  282. {
  283.     /** deals with lines that have multiple colons on them.  It must first
  284.         figure out how many spaces to allocate for each field then prompts
  285.         the user, line by line, for the entries...
  286.     **/
  287.  
  288.     char mybuffer[SLEN], prompt[SLEN], spaces[SLEN];
  289.     register int  field_size, i, j, offset = 0, extra_tabs = 0;
  290.  
  291.     dprint(7, (debugfile,
  292.         "prompt-for-multiple [%d] -entries \"%s\"\n", entries,
  293.         buffer));
  294.  
  295.     strcpy(prompt, "No Prompt Available:");
  296.  
  297.     while (entries--) {
  298.       j=0;
  299.       i = chloc((char *) buffer + offset, COLON) + 1;
  300.       while (j < i - 1) {
  301.         prompt[j] = buffer[j+offset];
  302.         j++;
  303.       }
  304.       prompt[j] = '\0';
  305.  
  306.       field_size = 0;
  307.  
  308.       while (whitespace(buffer[i+offset])) {
  309.         if (buffer[i+offset] == TAB) {
  310.           field_size += 8 - (i % 8);
  311.           extra_tabs += (8 - (i % 8)) - 1;
  312.         }
  313.         else
  314.           field_size += 1;
  315.         i++;
  316.       }
  317.  
  318.       offset += i;
  319.  
  320.       if (field_size == 0)     /* probably last prompt in line... */
  321.         field_size = 78 - (offset + extra_tabs);
  322.  
  323.       prompt_for_sized_entry(prompt, mybuffer, field_size);
  324.  
  325.       spaces[0] = ' ';    /* always at least ONE trailing space... */
  326.       spaces[1] = '\0';
  327.  
  328.       /*  field_size-1 for the space spaces[] starts with  */
  329.       for (j = strlen(mybuffer); j < field_size-1; j++)
  330.         strcat(spaces, " ");
  331.  
  332.       fprintf(fd, "%s: %s%s", prompt, mybuffer, spaces);
  333.       fflush(fd);
  334.     }
  335.  
  336.     fprintf(fd, "\n");
  337. }
  338.  
  339. prompt_for_sized_entry(prompt, buffer, field_size)
  340. char *prompt, *buffer;
  341. int   field_size;
  342. {
  343.     /* This routine prompts for an entry of the size specified. */
  344.  
  345.     register int i;
  346.  
  347.     dprint(7, (debugfile, "prompt-for-sized-entry \"%s\" %d chars\n",
  348.         prompt, field_size));
  349.  
  350.     printf("%s: ", prompt);
  351.  
  352.     for (i=0;i<field_size; i++)
  353.       putchar('_');
  354.     for (i=0;i<field_size; i++)
  355.       putchar(BACKSPACE);
  356.     fflush(stdout);
  357.  
  358.     fgets(buffer, SLEN, stdin);
  359.     no_ret(buffer);
  360.  
  361.     if (strlen(buffer) > field_size) buffer[field_size-1] = '\0';
  362. }
  363.