home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / elm.lzh / ELM / UTILS / READMSG.C < prev   
C/C++ Source or Header  |  1993-03-18  |  13KB  |  468 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: readmsg.c,v 4.1.1.2 90/06/21 22:40:12 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.2 $   $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:    readmsg.c,v $
  17.  * Revision 4.1.1.2  90/06/21  22:40:12  syd
  18.  * Reduce occurrences of unprotected "From " confusing message count
  19.  * From: Marius Olafsson
  20.  * 
  21.  * Revision 4.1.1.1  90/06/21  22:33:51  syd
  22.  * Fix error message in readmsg and clear variable for use
  23.  * From: Hans Buurman
  24.  * 
  25.  * Revision 4.1  90/04/28  22:44:52  syd
  26.  * checkin of Elm 2.3 as of Release PL0
  27.  * 
  28.  *
  29.  ******************************************************************************/
  30.  
  31. /** This routine adds the functionality of the "~r" command to the Elm mail 
  32.     system while still allowing the user to use the editor of their choice.
  33.  
  34.     The program, without any arguments, tries to read a file in the users home 
  35.     directory called ".readmsg" (actually defined in the sysdefs.h system 
  36.     defines file) and if it finds it reads the current message.  If it doesn't 
  37.     find it, it will return a usage error.
  38.  
  39.     The program can also be called with an explicit message number, list of 
  40.     message numbers, or a string to match in the message (including the header).
  41.     NOTE that when you use the string matching option it will match the first 
  42.     message containing that EXACT (case sensitive) string and then exit.
  43. **/
  44.  
  45. #include <stdio.h>
  46. #include <ctype.h>
  47.  
  48. #include "defs.h"
  49.  
  50. /** three defines for what level of headers to display **/
  51.  
  52. #define ALL        1
  53. #define WEED        2
  54. #define NONE        3
  55.  
  56. #define metachar(c)    (c == '=' || c == '+' || c == '%')
  57.  
  58. static char ident[] = { WHAT_STRING };
  59.  
  60. #define  MAX_LIST    25        /* largest single list of arguments */
  61.  
  62. #define  LAST_MESSAGE    9999        /* last message in list ('$' char)  */
  63. #define  LAST_CHAR    '$'        /* char to delimit last message..   */
  64. #define  STAR        '*'        /* char to delimit all messages...  */
  65.  
  66. int read_message[MAX_LIST];         /* list of messages to read        */
  67. int messages = 0;            /* index into list of messages      */
  68.  
  69. int numcmp();                /* strcmp, but for numbers          */
  70. char *words();                /* function defined below...        */
  71.  
  72.  
  73. extern char *optarg;        /* for parsing the ...             */
  74. extern int   optind;            /*  .. starting arguments           */
  75.  
  76. char *getenv();                /* keep lint happy */
  77.  
  78. main(argc, argv)
  79. int argc;
  80. char *argv[];
  81. {
  82.     FILE *file;                    /* generic file descriptor! */
  83.     char filename[SLEN],             /* filename buffer          */
  84.          infile[SLEN],            /* input filename        */
  85.          buffer[SLEN],             /* file reading buffer      */
  86.          string[SLEN],            /* string match buffer      */
  87.          *cp;
  88.  
  89.     int current_in_queue = 0,         /* these are used for...     */
  90.         current = 0,            /* ...going through msgs     */
  91.         list_all_messages = 0,        /* just list 'em all??       */
  92.         num,                 /* for argument parsing      */
  93.         page_breaks = 0,            /* use "^L" breaks??         */
  94.             total,                /* number of msgs current    */
  95.         include_headers = WEED,         /* flag: include msg header? */
  96.         last_message = 0,             /* flag: read last message?  */
  97.         not_in_header = 0,            /* flag: in msg header?      */
  98. #ifdef MMDF
  99.         newheader = 0,            /* flag: hit ^A^A^A^A line   */
  100. #endif /* MMDF */
  101.         string_match = 0;            /* flag: using string match?  */
  102.         string[0] = '\0';            /* init match string to empty */
  103.         infile[0] = '\0';            /* init mail file to empty    */
  104.  
  105.     /**** start of the actual program ****/
  106.  
  107.     while ((num = getopt(argc, argv, "nhf:p")) != EOF) {
  108.       switch (num) {
  109.         case 'n' : include_headers = NONE;        break;
  110.         case 'h' : include_headers = ALL;        break;
  111.         case 'f' : strcpy(infile, optarg);    
  112.                if (metachar(infile[0]))
  113.                      if (expand(infile) == 0)
  114.                        printf("%s: couldn't expand filename %s!\n", 
  115.                       argv[0], infile);
  116.                break;
  117.         case 'p' : page_breaks++;            break;
  118.         case '?' : printf(
  119.             "Usage: %s [-n|-h] [-f filename] [-p] <message list>\n",
  120.              argv[0]);
  121.                  exit(1);
  122.       }
  123.     }
  124.     
  125.     /** whip past the starting arguments so that we're pointing
  126.         to the right stuff... **/
  127.  
  128.     *argv++;    /* past the program name... */
  129.  
  130.     while (optind-- > 1) {
  131.       *argv++;
  132.       argc--;
  133.     }
  134.  
  135.     /** now let's figure out the parameters to the program... **/
  136.  
  137.     if (argc == 1) {    /* no arguments... called from 'Elm'? */
  138.       sprintf(filename, "%s/%s", getenv("HOME"), readmsg_file);
  139.       if ((file = fopen(filename, "r")) != NULL) {
  140.         fscanf(file, "%d", &(read_message[messages++]));
  141.         fclose(file);
  142.       }
  143.       else {    /* no arguments AND no .readmsg file!! */
  144.         fprintf(stderr,
  145.             "Usage: readmsg [-n|-h] [-f filename] [-p] <message list>\n");
  146.         exit(1);
  147.       }
  148.     }
  149.     else if (! isdigit(*argv[0]) && *argv[0] != LAST_CHAR && 
  150.              *argv[0] != STAR) {  
  151.       string_match++;
  152.      
  153.       while (*argv)
  154.         sprintf(string, "%s%s%s", string, string[0] == '\0'? "" : " ",
  155.             *argv++);
  156.     }
  157.     else if (*argv[0] == STAR)         /* all messages....   */
  158.       list_all_messages++;
  159.     else {                       /* list of nums   */
  160.  
  161.       while (--argc > 0) {
  162.         num = -1;
  163.  
  164.         sscanf(*argv,"%d", &num);
  165.  
  166.         if (num < 0) {
  167.           if (*argv[0] == LAST_CHAR) {
  168.             last_message++;
  169.         num = LAST_MESSAGE;
  170.           }
  171.           else {
  172.             fprintf(stderr,"I don't understand what '%s' means...\n",
  173.             *argv); 
  174.                exit(1); 
  175.           }
  176.         }
  177.         else if (num == 0) {    /* another way to say "last" */
  178.           last_message++;
  179.           num = LAST_MESSAGE;
  180.         }
  181.  
  182.         *argv++; 
  183.  
  184.         read_message[messages++] = num;
  185.       }
  186.  
  187.       /** and now sort 'em to ensure they're in a reasonable order... **/
  188.  
  189.       qsort(read_message, messages, sizeof(int), numcmp);
  190.     }
  191.  
  192.     /** Now let's get to the mail file... **/
  193.  
  194.     if (strlen(infile) == 0) {
  195.       if ((cp = getenv("MAIL")) == NULL) {
  196.         if ((cp = getenv("LOGNAME")) == NULL)
  197.           sprintf(infile, "%s%s", mailhome, getenv("USER"));
  198.         else
  199.           sprintf(infile, "%s%s", mailhome, cp);
  200.       }
  201.       else
  202.         strcpy(infile, cp);
  203.     }
  204.  
  205.     if ((file = fopen(infile, "r")) == NULL) {
  206.       printf("But you have no mail! [ file = %s ]\n", infile);
  207.       exit(0);
  208.     }
  209.  
  210.     /** Now it's open, let's display some 'ole messages!! **/
  211.  
  212.     if (string_match || last_message) {   /* pass through it once */
  213.  
  214.       if (last_message) {
  215.         total = count_messages(file);    /* instantiate count */
  216.         for (num=0; num < messages; num++)
  217.           if (read_message[num] == LAST_MESSAGE)
  218.         read_message[num] = total;
  219.       }
  220.       else if (string_match)
  221.         match_string(file, string);        /* stick msg# in list */
  222.  
  223.       if (total == 0 && ! string_match) {
  224.         printf("There aren't any messages to read!\n");
  225.         exit(0);
  226.       }
  227.     }
  228.  
  229.      /** now let's have some fun! **/
  230. #ifdef MMDF
  231.     newheader = 0;
  232. #endif /* MMDF */
  233.     
  234.     while (fgets(buffer, SLEN, file) != NULL) {
  235. #ifdef MMDF
  236.       if (strcmp(buffer, MSG_SEPERATOR) == 0)
  237.         newheader = !newheader;
  238.       if (newheader && buffer[0] == '\001') {
  239. #else
  240.       if (real_from(buffer)) {
  241. #endif /* MMDF */
  242.         if (! list_all_messages) {
  243.           if (current == read_message[current_in_queue])
  244.             current_in_queue++;
  245.           if (current_in_queue >= messages) 
  246.             exit(0);
  247.         }
  248.         current++;
  249.         not_in_header = 0;    /* we're in the header! */
  250.       }
  251.       if (current == read_message[current_in_queue] || list_all_messages)
  252. #ifdef MMDF
  253.         if ((include_headers==ALL || not_in_header)
  254.         && strcmp(buffer, MSG_SEPERATOR) != 0)
  255. #else
  256.         if (include_headers==ALL || not_in_header)
  257. #endif /* MMDF */
  258.           printf("%s", buffer);
  259.         else if (strlen(buffer) < 2) {
  260.           not_in_header++;
  261.           if (include_headers==WEED) 
  262.         list_saved_headers(page_breaks);
  263.         }
  264.         else if (include_headers==WEED)
  265.           possibly_save(buffer);     /* check to see if we want this */ 
  266.     }
  267.     
  268.     exit(0);
  269. }
  270.  
  271. int
  272. count_messages(file)
  273. FILE *file;
  274. {
  275.     /** Returns the number of messages in the file **/
  276.  
  277.     char buffer[SLEN];
  278.     int  count = 0;
  279. #ifdef MMDF
  280.     int  newheader = 0;
  281. #endif /* MMDF */
  282.  
  283.     while (fgets(buffer, SLEN, file) != NULL)
  284. #ifdef MMDF
  285.       if ((strcmp(buffer, MSG_SEPERATOR) == 0)
  286.         && (++newheader % 2))
  287. #else
  288.       if (real_from(buffer))
  289. #endif /* MMDF */
  290.         count++;
  291.  
  292.     rewind( file );
  293.     return( count );
  294. }
  295.  
  296. match_string(mailfile, string)
  297. FILE *mailfile;
  298. char *string;
  299. {
  300.     /** Increment "messages" and put the number of the message
  301.         in the message_count[] buffer until we match the specified 
  302.         string... **/
  303.  
  304.     char buffer[SLEN];
  305.     int  message_count = 0;
  306. #ifdef MMDF
  307.     int  newheader = 0;
  308. #endif /* MMDF */
  309.  
  310.     while (fgets(buffer, SLEN, mailfile) != NULL) {
  311. #ifdef MMDF
  312.       if ((strcmp(buffer, MSG_SEPERATOR) == 0)
  313.         && (++newheader % 2))
  314. #else
  315.       if (real_from(buffer))
  316. #endif /* MMDF */
  317.         message_count++;
  318.  
  319.       if (in_string(buffer, string)) {
  320.         read_message[messages++] = message_count;
  321.         rewind(mailfile);    
  322.         return;
  323.       }
  324.     }
  325.  
  326.     fprintf(stderr,"Couldn't find message containing '%s'\n", string);
  327.     exit(1);
  328. }
  329.  
  330. int 
  331. numcmp(a, b)
  332. int *a, *b;
  333. {
  334.     /** compare 'a' to 'b' returning less than, equal, or greater
  335.         than, accordingly.
  336.      **/
  337.  
  338.     return(*a - *b);
  339. }
  340.  
  341. static char from[SLEN], subject[SLEN], date[SLEN], to[SLEN];
  342.  
  343. possibly_save(buffer)
  344. char *buffer;
  345. {
  346.     /** Check to see what "buffer" is...save it if it looks 
  347.         interesting... We'll always try to get SOMETHING
  348.         by tearing apart the "From " line...  **/
  349.  
  350.     if (strncmp(buffer, "Date:", 5) == 0)
  351.       strcpy(date, buffer);
  352.     else if (strncmp(buffer, "Subject:", 8) == 0)
  353.       strcpy(subject,buffer);
  354.     else if (strncmp(buffer,"From:", 5) == 0)
  355.       strcpy(from, buffer);
  356.     else if (strncmp(buffer,"To: ", 3) == 0)
  357.       strncpy(to, buffer, SLEN);
  358.     else if (strncmp(buffer,"From ", 5) == 0) {
  359.       sprintf(from, "From: %s\n", words(2,1, buffer));    
  360.       sprintf(date,"Date: %s",    words(3,7, buffer));
  361.       to[0] = '\0';
  362.       subject[0] = '\0';
  363.     }
  364. }
  365.  
  366. list_saved_headers(page_break)
  367. int page_break;
  368. {
  369.     /** This routine will display the information saved from the
  370.         message being listed...If it displays anything it'll end
  371.         with a blank line... **/
  372.  
  373.     register int displayed_line = FALSE;
  374.     static int messages_listed = 0;
  375.  
  376.     if (messages_listed++) 
  377.       if (page_break)
  378.         putchar(FORMFEED);    
  379.       else
  380.         printf(
  381. "\n--------------------------------------------------------------------\n\n\n");
  382.  
  383.     if (strlen(from)    > 0) { printf("%s", from);    displayed_line++;}
  384.     if (strlen(subject) > 0) { printf("%s", subject); displayed_line++;}
  385.     if (strlen(to)      > 0) { printf("%s", to);      displayed_line++;}
  386.     if (strlen(date)    > 0) { printf("%s", date);    displayed_line++;}
  387.     
  388.     if (displayed_line)
  389.        putchar('\n');
  390. }
  391.  
  392. char *words(word, num_words, buffer)
  393. int word, num_words;
  394. char *buffer;
  395. {
  396.     /** Return a buffer starting at 'word' and containing 'num_words'
  397.         words from buffer.  Assume white space will delimit each word.
  398.     **/
  399.  
  400.     static char internal_buffer[SLEN];
  401.     char   *wordptr, *bufptr, mybuffer[SLEN], *strtok();
  402.     int    wordnumber = 0, copying_words = 0;
  403.  
  404.     internal_buffer[0] = '\0';    /* initialize */
  405.  
  406.     strcpy(mybuffer, buffer);
  407.     bufptr = (char *) mybuffer;    /* and setup */
  408.  
  409.     while ((wordptr = strtok(bufptr, " \t")) != NULL) {
  410.       if (++wordnumber == word) {
  411.         strcpy(internal_buffer, wordptr);
  412.         copying_words++;
  413.         num_words--;
  414.       }
  415.       else if (copying_words) {
  416.         strcat(internal_buffer, " ");
  417.         strcat(internal_buffer, wordptr);
  418.         num_words--;
  419.       }
  420.  
  421.       if (num_words < 1) 
  422.         return((char *) internal_buffer);
  423.  
  424.       bufptr = NULL;
  425.     }
  426.  
  427.     return( (char *) internal_buffer);
  428. }
  429.  
  430. int
  431. real_from(buffer)
  432. char *buffer;
  433. {
  434.     /***** Returns true iff 's' has the seven 'from' fields, (or
  435.            8 - some machines include the TIME ZONE!!!) *****/
  436.  
  437.     char sixthword[STRING], seventhword[STRING],
  438.          eighthword[STRING], ninthword[STRING];
  439.  
  440.     /* From <user> <day> <month> <day> <hr:min:sec> <year> */
  441.  
  442.     if(strncmp(buffer, "From ", 5) != 0)
  443.       return(FALSE);
  444.  
  445.     /* Extract 6th, 7th, 8th, and 9th words */
  446.     seventhword[0] = eighthword[0] = ninthword[0] = '\0';
  447.     sscanf(buffer, "%*s %*s %*s %*s %*s %s %s %s %s",
  448.       sixthword, seventhword, eighthword, ninthword);
  449.  
  450.     /* Not a from line if 6th word doesn't have colons for time field */
  451.     if(strlen(sixthword) < 3)
  452.       return(FALSE);
  453.     if (sixthword[1] != ':' && sixthword[2] != ':')
  454.       return(FALSE);        
  455.  
  456.     /* Not a from line if there is no seventh word */
  457.     if(seventhword[0] == '\0')
  458.       return(FALSE);
  459.  
  460.     /* Not a from line if there is a ninthword */
  461.     if (eighthword[0] != '\0') {
  462.       if(ninthword[0] != '\0')
  463.         return(FALSE);    
  464.     }
  465.  
  466.     return(TRUE);
  467. }
  468.