home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / forms.c < prev    next >
C/C++ Source or Header  |  1991-11-26  |  10KB  |  362 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. extern int errno;
  53.  
  54. char *error_name();/* *strcat(), *strcpy(); */
  55.  
  56. check_form_file(filename)
  57. char *filename;
  58. {
  59.     /** This routine returns the number of fields in the specified file,
  60.         or -1 if an error is encountered. **/
  61.  
  62.     FILE *form;
  63.     char buffer[SLEN];
  64.     register int field_count = 0;
  65.  
  66.     if ((form = fopen(filename, "r")) == NULL) {
  67.       error2("Error %s trying to open %s to check fields!",
  68.           error_name(errno), filename);
  69.       return(-1);
  70.     }
  71.     
  72.     while (fgets(buffer, SLEN, form) != NULL) {
  73.       field_count += occurances_of(COLON, buffer);
  74.     }
  75.  
  76.     fclose(form);
  77.  
  78.     return(field_count);
  79. }
  80.  
  81. format_form(filename)
  82. char *filename;
  83. {
  84.     /** This routine accepts a validated file that is the middle 
  85.         section of a form message and prepends and appends the appropriate 
  86.         instructions.  It's pretty simple. 
  87.         This returns the number of forms in the file, or -1 on errors
  88.     **/
  89.     
  90.     FILE *form, *newform;
  91.     char  newfname[SLEN], buffer[SLEN];
  92.     register form_count = 0;
  93.  
  94.     dprint(4, (debugfile, "Formatting form file '%s'\n", filename));
  95.  
  96.     /** first off, let's open the files... **/
  97.  
  98.     if ((form = fopen(filename, "r")) == NULL) {
  99.       error("Can't read the message to validate the form!");
  100.       dprint(1, (debugfile,
  101.               "** Error encountered opening file \"%s\" - %s (check_form) **\n",
  102.           filename, error_name(errno)));
  103.       return(-1);
  104.     }
  105.  
  106.     sprintf(newfname, "%s%s%d", temp_dir, temp_form_file, getpid());
  107.  
  108.     if ((newform = fopen(newfname, "w")) == NULL) {
  109.       error("Couldn't open newform file for form output!");
  110.       dprint(1, (debugfile, 
  111.               "** Error encountered opening file \"%s\" - %s (check_form) **\n",
  112.           newfname, error_name(errno)));
  113.       return(-1);
  114.     }
  115.  
  116.     /** the required header... **/
  117.  
  118.     /* these are actually the defaults, but let's be sure, okay? */
  119.  
  120.     fprintf(newform, "WIDTH=78\nTYPE=SIMPLE\nOUTPUT=TEXT\n***\n");
  121.  
  122.     /** and let's have some fun transfering the stuff across... **/
  123.  
  124.     while (fgets(buffer, SLEN, form) != NULL) {
  125.       fputs(buffer, newform);
  126.       form_count += occurances_of(COLON, buffer);
  127.     }
  128.  
  129.     fprintf(newform, "***\n");    /* that closing bit! */
  130.  
  131.     fclose(form);
  132.     fclose(newform);
  133.  
  134.     if (form_count > 0) {
  135.       if (unlink(filename) != 0) {
  136.         error2("Error %s unlinking file %s.", error_name(errno), filename);
  137.         return(-1);
  138.       }
  139.       if (link(newfname, filename)) {
  140.         error3("Error %s linking %s to %s.", error_name(errno), 
  141.             newfname, filename);
  142.         return(-1);
  143.       }
  144.     }
  145.  
  146.     if (unlink(newfname)) {
  147.       error2("Error %s unlinking file %s.", error_name(errno), newfname);
  148.       return(-1);    
  149.     }
  150.  
  151.     return(form_count);
  152. }
  153.  
  154. int
  155. mail_filled_in_form(address, subject)
  156. char *address, *subject;
  157. {
  158.     /** This is the interesting routine.  This one will read the
  159.         message and prompt the user, line by line, for each of
  160.         the fields...returns non-zero if it succeeds
  161.     **/
  162.  
  163.     FILE           *fd;
  164.     register int lines = 0, count;
  165.     char         buffer[SLEN], *ptr;
  166.  
  167.     dprint(4, (debugfile, 
  168.         "replying to form with;\n\taddress=%s and\n\t subject=%s\n",
  169.          address, subject));
  170.  
  171.         if (fseek(mailfile, headers[current-1]->offset, 0) == -1) {
  172.       dprint(1, (debugfile,
  173.            "Error: seek %ld resulted in errno %s (%s)\n", 
  174.            headers[current-1]->offset, error_name(errno), 
  175.            "mail_filled_in_form"));
  176.       error2("ELM [seek] couldn't read %d bytes into file (%s).",
  177.              headers[current-1]->offset, error_name(errno));
  178.       return(0);
  179.         }
  180.  
  181.     /* now we can fly along and get to the message body... */
  182.  
  183.     while ((ptr = fgets(buffer, SLEN, mailfile)) != NULL) {
  184.       if (strlen(buffer) == 1)    /* <return> only */
  185.         break;
  186.       else if (strncmp(buffer,"From ", 5) == 0 && lines++ > 0) {
  187.         error("No form in this message!?");
  188.         return(0);
  189.       }
  190.     }
  191.  
  192.     if (ptr == NULL) {
  193.       error("No form in this message!?");
  194.       return(0);
  195.     }
  196.  
  197.     dprint(6, (debugfile, "- past header of form message -\n"));
  198.     
  199.     /* at this point we're at the beginning of the body of the message */
  200.  
  201.     /* now we can skip to the FORM-IMAGE section by reading through a 
  202.        line with a triple asterisk... */
  203.  
  204.     while ((ptr = fgets(buffer, SLEN, mailfile)) != NULL) {
  205.       if (strcmp(buffer, "***\n") == 0)
  206.         break;    /* we GOT it!  It's a miracle! */    
  207.       else if (strncmp(buffer, "From ",5) == 0) {
  208.         error("Badly constructed form.  Can't reply!");
  209.         return(0);
  210.       }
  211.     }
  212.  
  213.     if (ptr == NULL) {
  214.       error("Badly constructed form.  Can't reply!");
  215.       return(0);
  216.     }
  217.  
  218.     dprint(6, (debugfile, "- skipped the non-forms-image stuff -\n"));
  219.     
  220.     /* one last thing - let's open the tempfile for output... */
  221.     
  222.     sprintf(buffer, "%s%s%d", temp_dir, temp_form_file, getpid());
  223.  
  224.     dprint(2, (debugfile,"-- forms sending using file %s --\n", buffer));
  225.  
  226.     if ((fd = fopen(buffer,"w")) == NULL) {
  227.       error2("Can't open \"%s\" as output file! (%s).", buffer,
  228.          error_name(errno));
  229.       dprint(1, (debugfile,
  230.           "** Error %s encountered trying to open temp file %s;\n",
  231.           error_name(errno), buffer));
  232.       return(0);
  233.     }
  234.  
  235.     /* NOW we're ready to read the form image in and start prompting... */
  236.  
  237.     Raw(OFF);
  238.     ClearScreen();
  239.  
  240.     while ((ptr = fgets(buffer, SLEN, mailfile)) != NULL) {
  241.       dprint(9, (debugfile, "- read %s", buffer));
  242.       if (strcmp(buffer, "***\n") == 0) /* end of form! */
  243.         break;
  244.      
  245.       switch ((count = occurances_of(COLON, buffer))) {
  246.         case 0 : printf("%s", buffer);        /* output line */
  247.              fprintf(fd, "%s", buffer);     
  248.              break;
  249.             case 1 : if (buffer[0] == COLON) {
  250.                    printf(
  251. "(Enter as many lines as needed, ending with a '.' by itself on a line)\n");
  252.                        while (fgets(buffer, SLEN, stdin) != NULL) {
  253.                  no_ret(buffer);
  254.                      if (strcmp(buffer, ".") == 0)
  255.                        break;
  256.                      else 
  257.                fprintf(fd,"%s\n", buffer);
  258.                }
  259.                  }
  260.                  else 
  261.                prompt_for_entries(buffer, fd, count);
  262.                  break;
  263.             default: prompt_for_entries(buffer, fd, count);
  264.       }
  265.     }
  266.  
  267.     Raw(ON);
  268.     fclose(fd);
  269.  
  270.     /** let's just mail this off now... **/
  271.  
  272.     mail_form(address, subject);
  273.  
  274.     return(1);
  275. }
  276.  
  277. prompt_for_entries(buffer, fd, entries)
  278. char *buffer;
  279. FILE *fd;
  280. int  entries;
  281. {
  282.     /** deals with lines that have multiple colons on them.  It must first
  283.         figure out how many spaces to allocate for each field then prompts
  284.         the user, line by line, for the entries...
  285.     **/
  286.  
  287.     char mybuffer[SLEN], prompt[SLEN], spaces[SLEN];
  288.     register int  field_size, i, j, offset = 0, extra_tabs = 0;
  289.  
  290.     dprint(7, (debugfile, 
  291.         "prompt-for-multiple [%d] -entries \"%s\"\n", entries,
  292.         buffer));
  293.  
  294.     strcpy(prompt, "No Prompt Available:");
  295.  
  296.     while (entries--) {
  297.       j=0; 
  298.       i = chloc((char *) buffer + offset, COLON) + 1;
  299.       while (j < i - 1) {
  300.         prompt[j] = buffer[j+offset];
  301.         j++;
  302.       }
  303.       prompt[j] = '\0';
  304.  
  305.       field_size = 0;
  306.  
  307.       while (whitespace(buffer[i+offset])) {
  308.         if (buffer[i+offset] == TAB) {
  309.           field_size += 8 - (i % 8);
  310.           extra_tabs += (8 - (i % 8)) - 1;
  311.         }
  312.         else
  313.           field_size += 1;
  314.         i++;
  315.       }
  316.  
  317.       offset += i;
  318.     
  319.       if (field_size == 0)     /* probably last prompt in line... */
  320.         field_size = 78 - (offset + extra_tabs);
  321.  
  322.       prompt_for_sized_entry(prompt, mybuffer, field_size);
  323.  
  324.       spaces[0] = ' ';    /* always at least ONE trailing space... */
  325.       spaces[1] = '\0';
  326.  
  327.       /*  field_size-1 for the space spaces[] starts with  */
  328.       for (j = strlen(mybuffer); j < field_size-1; j++)
  329.         strcat(spaces, " ");
  330.  
  331.       fprintf(fd, "%s: %s%s", prompt, mybuffer, spaces);
  332.       fflush(fd);
  333.     }
  334.  
  335.     fprintf(fd, "\n");
  336. }
  337.  
  338. prompt_for_sized_entry(prompt, buffer, field_size)
  339. char *prompt, *buffer;
  340. int   field_size;
  341. {
  342.     /* This routine prompts for an entry of the size specified. */
  343.  
  344.     register int i;
  345.  
  346.     dprint(7, (debugfile, "prompt-for-sized-entry \"%s\" %d chars\n", 
  347.         prompt, field_size));
  348.  
  349.     printf("%s: ", prompt);
  350.     
  351.     for (i=0;i<field_size; i++)
  352.       putchar('_');
  353.     for (i=0;i<field_size; i++)
  354.       putchar(BACKSPACE);
  355.     fflush(stdout);
  356.  
  357.     fgets(buffer, SLEN, stdin);
  358.     no_ret(buffer);
  359.  
  360.     if (strlen(buffer) > field_size) buffer[field_size-1] = '\0';
  361. }
  362.