[comp.sources.unix] v22i084: ELM mail syste, release 2.3, Part26/26

rsalz@uunet.uu.net (Rich Salz) (06/06/90)

Submitted-by: Syd Weinstein <syd@dsinc.dsi.com>
Posting-number: Volume 22, Issue 84
Archive-name: elm2.3/part26

---- Cut Here and unpack ----
#!/bin/sh
# this is part 26 of a multipart archive
# do not concatenate these parts, unpack them in order with /bin/sh
# file utils/readmsg.c continued
#
CurArch=26
if test ! -r s2_seq_.tmp
then echo "Please unpack part 1 first!"
     exit 1; fi
( read Scheck
  if test "$Scheck" != $CurArch
  then echo "Please unpack part $Scheck next!"
       exit 1;
  else exit 0; fi
) < s2_seq_.tmp || exit 1
echo "x - Continuing file utils/readmsg.c"
sed 's/^X//' << 'SHAR_EOF' >> utils/readmsg.c
X	      last_message++;
X	      num = LAST_MESSAGE;
X	    }
X
X	    *argv++; 
X
X	    read_message[messages++] = num;
X	  }
X
X	  /** and now sort 'em to ensure they're in a reasonable order... **/
X
X	  qsort(read_message, messages, sizeof(int), numcmp);
X	}
X
X	/** Now let's get to the mail file... **/
X
X	if (strlen(infile) == 0) {
X	  if ((cp = getenv("MAIL")) == NULL) {
X	    if ((cp = getenv("LOGNAME")) == NULL)
X	      sprintf(infile, "%s/%s", mailhome, getenv("USER"));
X	    else
X	      sprintf(infile, "%s/%s", mailhome, cp);
X	  }
X	  else
X	    strcpy(infile, cp);
X	}
X
X	if ((file = fopen(infile, "r")) == NULL) {
X	  printf("But you have no mail! [ file = %d ]\n", infile);
X	  exit(0);
X	}
X
X	/** Now it's open, let's display some 'ole messages!! **/
X
X	if (string_match || last_message) {   /* pass through it once */
X
X	  if (last_message) {
X	    total = count_messages(file);	/* instantiate count */
X	    for (num=0; num < messages; num++)
X	      if (read_message[num] == LAST_MESSAGE)
X		read_message[num] = total;
X	  }
X	  else if (string_match)
X	    match_string(file, string);		/* stick msg# in list */
X
X	  if (total == 0 && ! string_match) {
X	    printf("There aren't any messages to read!\n");
X	    exit(0);
X	  }
X	}
X
X 	/** now let's have some fun! **/
X#ifdef MMDF
X	newheader = 0;
X#endif /* MMDF */
X	
X	while (fgets(buffer, SLEN, file) != NULL) {
X#ifdef MMDF
X	  if (strcmp(buffer, MSG_SEPERATOR) == 0)
X	    newheader = !newheader;
X	  if (newheader && buffer[0] == '\001') {
X#else
X	  if (real_from(buffer)) {
X#endif /* MMDF */
X	    if (! list_all_messages) {
X	      if (current == read_message[current_in_queue])
X	        current_in_queue++;
X	      if (current_in_queue >= messages) 
X	        exit(0);
X	    }
X	    current++;
X	    not_in_header = 0;	/* we're in the header! */
X	  }
X	  if (current == read_message[current_in_queue] || list_all_messages)
X#ifdef MMDF
X	    if ((include_headers==ALL || not_in_header)
X		&& strcmp(buffer, MSG_SEPERATOR) != 0)
X#else
X	    if (include_headers==ALL || not_in_header)
X#endif /* MMDF */
X	      printf("%s", buffer);
X	    else if (strlen(buffer) < 2) {
X	      not_in_header++;
X	      if (include_headers==WEED) 
X		list_saved_headers(page_breaks);
X	    }
X	    else if (include_headers==WEED)
X	      possibly_save(buffer); 	/* check to see if we want this */ 
X	}
X	
X	exit(0);
X}
X
Xint
Xcount_messages(file)
XFILE *file;
X{
X	/** Returns the number of messages in the file **/
X
X	char buffer[SLEN];
X	int  count = 0;
X#ifdef MMDF
X	int  newheader = 0;
X#endif /* MMDF */
X
X	while (fgets(buffer, SLEN, file) != NULL)
X#ifdef MMDF
X	  if ((strcmp(buffer, MSG_SEPERATOR) == 0)
X	    && (++newheader % 2)) count++;
X#else
X	  if (strncmp(buffer, "From ", 5) == 0)
X	    count++;
X#endif /* MMDF */
X
X	rewind( file );
X	return( count );
X}
X
Xmatch_string(mailfile, string)
XFILE *mailfile;
Xchar *string;
X{
X	/** Increment "messages" and put the number of the message
X	    in the message_count[] buffer until we match the specified 
X	    string... **/
X
X	char buffer[SLEN];
X	int  message_count = 0;
X#ifdef MMDF
X	int  newheader = 0;
X#endif /* MMDF */
X
X	while (fgets(buffer, SLEN, mailfile) != NULL) {
X#ifdef MMDF
X	  if ((strcmp(buffer, MSG_SEPERATOR) == 0)
X	    && (++newheader % 2)) message_count++;
X#else
X	  if (strncmp(buffer, "From ", 5) == 0)
X	    message_count++;
X#endif /* MMDF */
X
X	  if (in_string(buffer, string)) {
X	    read_message[messages++] = message_count;
X	    rewind(mailfile);	
X	    return;
X	  }
X	}
X
X	fprintf(stderr,"Couldn't find message containing '%s'\n", string);
X	exit(1);
X}
X
Xint 
Xnumcmp(a, b)
Xint *a, *b;
X{
X	/** compare 'a' to 'b' returning less than, equal, or greater
X	    than, accordingly.
X	 **/
X
X	return(*a - *b);
X}
X
Xstatic char from[SLEN], subject[SLEN], date[SLEN], to[SLEN];
X
Xpossibly_save(buffer)
Xchar *buffer;
X{
X	/** Check to see what "buffer" is...save it if it looks 
X	    interesting... We'll always try to get SOMETHING
X	    by tearing apart the "From " line...  **/
X
X	if (strncmp(buffer, "Date:", 5) == 0)
X	  strcpy(date, buffer);
X	else if (strncmp(buffer, "Subject:", 8) == 0)
X	  strcpy(subject,buffer);
X	else if (strncmp(buffer,"From:", 5) == 0)
X	  strcpy(from, buffer);
X	else if (strncmp(buffer,"To: ", 3) == 0)
X	  strncpy(to, buffer, SLEN);
X	else if (strncmp(buffer,"From ", 5) == 0) {
X	  sprintf(from, "From: %s\n", words(2,1, buffer));	
X	  sprintf(date,"Date: %s",    words(3,7, buffer));
X	  to[0] = '\0';
X	  subject[0] = '\0';
X	}
X}
X
Xlist_saved_headers(page_break)
Xint page_break;
X{
X	/** This routine will display the information saved from the
X	    message being listed...If it displays anything it'll end
X	    with a blank line... **/
X
X	register int displayed_line = FALSE;
X	static int messages_listed = 0;
X
X	if (messages_listed++) 
X	  if (page_break)
X	    putchar(FORMFEED);	
X	  else
X	    printf(
X"\n--------------------------------------------------------------------\n\n\n");
X
X	if (strlen(from)    > 0) { printf("%s", from);    displayed_line++;}
X	if (strlen(subject) > 0) { printf("%s", subject); displayed_line++;}
X	if (strlen(to)      > 0) { printf("%s", to);      displayed_line++;}
X	if (strlen(date)    > 0) { printf("%s", date);    displayed_line++;}
X	
X	if (displayed_line)
X	   putchar('\n');
X}
X
Xchar *words(word, num_words, buffer)
Xint word, num_words;
Xchar *buffer;
X{
X	/** Return a buffer starting at 'word' and containing 'num_words'
X	    words from buffer.  Assume white space will delimit each word.
X	**/
X
X	static char internal_buffer[SLEN];
X	char   *wordptr, *bufptr, mybuffer[SLEN], *strtok();
X	int    wordnumber = 0, copying_words = 0;
X
X	internal_buffer[0] = '\0';	/* initialize */
X
X	strcpy(mybuffer, buffer);
X	bufptr = (char *) mybuffer;	/* and setup */
X
X	while ((wordptr = strtok(bufptr, " \t")) != NULL) {
X	  if (++wordnumber == word) {
X	    strcpy(internal_buffer, wordptr);
X	    copying_words++;
X	    num_words--;
X	  }
X	  else if (copying_words) {
X	    strcat(internal_buffer, " ");
X	    strcat(internal_buffer, wordptr);
X	    num_words--;
X	  }
X
X	  if (num_words < 1) 
X	    return((char *) internal_buffer);
X
X	  bufptr = NULL;
X	}
X
X	return( (char *) internal_buffer);
X}
X
Xint
Xreal_from(buffer)
Xchar *buffer;
X{
X	/***** Returns true iff 's' has the seven 'from' fields, (or
X	       8 - some machines include the TIME ZONE!!!) *****/
X
X	char sixthword[STRING], seventhword[STRING],
X	     eighthword[STRING], ninthword[STRING];
X
X	/* From <user> <day> <month> <day> <hr:min:sec> <year> */
X
X	if(strncmp(buffer, "From ", 5) != 0)
X	  return(FALSE);
X
X	/* Extract 6th, 7th, 8th, and 9th words */
X	seventhword[0] = eighthword[0] = ninthword[0] = '\0';
X	sscanf(buffer, "%*s %*s %*s %*s %*s %s %s %s %s",
X	  sixthword, seventhword, eighthword, ninthword);
X
X	/* Not a from line if 6th word doesn't have colons for time field */
X	if(strlen(sixthword) < 3)
X	  return(FALSE);
X	if (sixthword[1] != ':' && sixthword[2] != ':')
X	  return(FALSE);		
X
X	/* Not a from line if there is no seventh word */
X	if(seventhword[0] == '\0')
X	  return(FALSE);
X
X	/* Not a from line if there is a ninthword */
X	if (eighthword[0] != '\0') {
X	  if(ninthword[0] != '\0')
X	    return(FALSE);	
X	}
X
X	return(TRUE);
X}
SHAR_EOF
echo "File utils/readmsg.c is complete"
chmod 0444 utils/readmsg.c || echo "restore of utils/readmsg.c fails"
rm -f s2_seq_.tmp
echo "You have unpacked the last part"
exit 0

exit 0 # Just in case...
-- 
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
Use a domain-based address or give alternate paths, or you may lose out.