home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / elm2.3 / part26 < prev    next >
Encoding:
Internet Message Format  |  1990-06-07  |  8.1 KB

  1. Subject:  v22i084:  ELM mail syste, release 2.3, Part26/26
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 8b74e56d 39f4171f c99d08c1 78be427d
  5.  
  6. Submitted-by: Syd Weinstein <syd@dsinc.dsi.com>
  7. Posting-number: Volume 22, Issue 84
  8. Archive-name: elm2.3/part26
  9.  
  10. ---- Cut Here and unpack ----
  11. #!/bin/sh
  12. # this is part 26 of a multipart archive
  13. # do not concatenate these parts, unpack them in order with /bin/sh
  14. # file utils/readmsg.c continued
  15. #
  16. CurArch=26
  17. if test ! -r s2_seq_.tmp
  18. then echo "Please unpack part 1 first!"
  19.      exit 1; fi
  20. ( read Scheck
  21.   if test "$Scheck" != $CurArch
  22.   then echo "Please unpack part $Scheck next!"
  23.        exit 1;
  24.   else exit 0; fi
  25. ) < s2_seq_.tmp || exit 1
  26. echo "x - Continuing file utils/readmsg.c"
  27. sed 's/^X//' << 'SHAR_EOF' >> utils/readmsg.c
  28. X          last_message++;
  29. X          num = LAST_MESSAGE;
  30. X        }
  31. X
  32. X        *argv++; 
  33. X
  34. X        read_message[messages++] = num;
  35. X      }
  36. X
  37. X      /** and now sort 'em to ensure they're in a reasonable order... **/
  38. X
  39. X      qsort(read_message, messages, sizeof(int), numcmp);
  40. X    }
  41. X
  42. X    /** Now let's get to the mail file... **/
  43. X
  44. X    if (strlen(infile) == 0) {
  45. X      if ((cp = getenv("MAIL")) == NULL) {
  46. X        if ((cp = getenv("LOGNAME")) == NULL)
  47. X          sprintf(infile, "%s/%s", mailhome, getenv("USER"));
  48. X        else
  49. X          sprintf(infile, "%s/%s", mailhome, cp);
  50. X      }
  51. X      else
  52. X        strcpy(infile, cp);
  53. X    }
  54. X
  55. X    if ((file = fopen(infile, "r")) == NULL) {
  56. X      printf("But you have no mail! [ file = %d ]\n", infile);
  57. X      exit(0);
  58. X    }
  59. X
  60. X    /** Now it's open, let's display some 'ole messages!! **/
  61. X
  62. X    if (string_match || last_message) {   /* pass through it once */
  63. X
  64. X      if (last_message) {
  65. X        total = count_messages(file);    /* instantiate count */
  66. X        for (num=0; num < messages; num++)
  67. X          if (read_message[num] == LAST_MESSAGE)
  68. X        read_message[num] = total;
  69. X      }
  70. X      else if (string_match)
  71. X        match_string(file, string);        /* stick msg# in list */
  72. X
  73. X      if (total == 0 && ! string_match) {
  74. X        printf("There aren't any messages to read!\n");
  75. X        exit(0);
  76. X      }
  77. X    }
  78. X
  79. X     /** now let's have some fun! **/
  80. X#ifdef MMDF
  81. X    newheader = 0;
  82. X#endif /* MMDF */
  83. X    
  84. X    while (fgets(buffer, SLEN, file) != NULL) {
  85. X#ifdef MMDF
  86. X      if (strcmp(buffer, MSG_SEPERATOR) == 0)
  87. X        newheader = !newheader;
  88. X      if (newheader && buffer[0] == '\001') {
  89. X#else
  90. X      if (real_from(buffer)) {
  91. X#endif /* MMDF */
  92. X        if (! list_all_messages) {
  93. X          if (current == read_message[current_in_queue])
  94. X            current_in_queue++;
  95. X          if (current_in_queue >= messages) 
  96. X            exit(0);
  97. X        }
  98. X        current++;
  99. X        not_in_header = 0;    /* we're in the header! */
  100. X      }
  101. X      if (current == read_message[current_in_queue] || list_all_messages)
  102. X#ifdef MMDF
  103. X        if ((include_headers==ALL || not_in_header)
  104. X        && strcmp(buffer, MSG_SEPERATOR) != 0)
  105. X#else
  106. X        if (include_headers==ALL || not_in_header)
  107. X#endif /* MMDF */
  108. X          printf("%s", buffer);
  109. X        else if (strlen(buffer) < 2) {
  110. X          not_in_header++;
  111. X          if (include_headers==WEED) 
  112. X        list_saved_headers(page_breaks);
  113. X        }
  114. X        else if (include_headers==WEED)
  115. X          possibly_save(buffer);     /* check to see if we want this */ 
  116. X    }
  117. X    
  118. X    exit(0);
  119. X}
  120. X
  121. Xint
  122. Xcount_messages(file)
  123. XFILE *file;
  124. X{
  125. X    /** Returns the number of messages in the file **/
  126. X
  127. X    char buffer[SLEN];
  128. X    int  count = 0;
  129. X#ifdef MMDF
  130. X    int  newheader = 0;
  131. X#endif /* MMDF */
  132. X
  133. X    while (fgets(buffer, SLEN, file) != NULL)
  134. X#ifdef MMDF
  135. X      if ((strcmp(buffer, MSG_SEPERATOR) == 0)
  136. X        && (++newheader % 2)) count++;
  137. X#else
  138. X      if (strncmp(buffer, "From ", 5) == 0)
  139. X        count++;
  140. X#endif /* MMDF */
  141. X
  142. X    rewind( file );
  143. X    return( count );
  144. X}
  145. X
  146. Xmatch_string(mailfile, string)
  147. XFILE *mailfile;
  148. Xchar *string;
  149. X{
  150. X    /** Increment "messages" and put the number of the message
  151. X        in the message_count[] buffer until we match the specified 
  152. X        string... **/
  153. X
  154. X    char buffer[SLEN];
  155. X    int  message_count = 0;
  156. X#ifdef MMDF
  157. X    int  newheader = 0;
  158. X#endif /* MMDF */
  159. X
  160. X    while (fgets(buffer, SLEN, mailfile) != NULL) {
  161. X#ifdef MMDF
  162. X      if ((strcmp(buffer, MSG_SEPERATOR) == 0)
  163. X        && (++newheader % 2)) message_count++;
  164. X#else
  165. X      if (strncmp(buffer, "From ", 5) == 0)
  166. X        message_count++;
  167. X#endif /* MMDF */
  168. X
  169. X      if (in_string(buffer, string)) {
  170. X        read_message[messages++] = message_count;
  171. X        rewind(mailfile);    
  172. X        return;
  173. X      }
  174. X    }
  175. X
  176. X    fprintf(stderr,"Couldn't find message containing '%s'\n", string);
  177. X    exit(1);
  178. X}
  179. X
  180. Xint 
  181. Xnumcmp(a, b)
  182. Xint *a, *b;
  183. X{
  184. X    /** compare 'a' to 'b' returning less than, equal, or greater
  185. X        than, accordingly.
  186. X     **/
  187. X
  188. X    return(*a - *b);
  189. X}
  190. X
  191. Xstatic char from[SLEN], subject[SLEN], date[SLEN], to[SLEN];
  192. X
  193. Xpossibly_save(buffer)
  194. Xchar *buffer;
  195. X{
  196. X    /** Check to see what "buffer" is...save it if it looks 
  197. X        interesting... We'll always try to get SOMETHING
  198. X        by tearing apart the "From " line...  **/
  199. X
  200. X    if (strncmp(buffer, "Date:", 5) == 0)
  201. X      strcpy(date, buffer);
  202. X    else if (strncmp(buffer, "Subject:", 8) == 0)
  203. X      strcpy(subject,buffer);
  204. X    else if (strncmp(buffer,"From:", 5) == 0)
  205. X      strcpy(from, buffer);
  206. X    else if (strncmp(buffer,"To: ", 3) == 0)
  207. X      strncpy(to, buffer, SLEN);
  208. X    else if (strncmp(buffer,"From ", 5) == 0) {
  209. X      sprintf(from, "From: %s\n", words(2,1, buffer));    
  210. X      sprintf(date,"Date: %s",    words(3,7, buffer));
  211. X      to[0] = '\0';
  212. X      subject[0] = '\0';
  213. X    }
  214. X}
  215. X
  216. Xlist_saved_headers(page_break)
  217. Xint page_break;
  218. X{
  219. X    /** This routine will display the information saved from the
  220. X        message being listed...If it displays anything it'll end
  221. X        with a blank line... **/
  222. X
  223. X    register int displayed_line = FALSE;
  224. X    static int messages_listed = 0;
  225. X
  226. X    if (messages_listed++) 
  227. X      if (page_break)
  228. X        putchar(FORMFEED);    
  229. X      else
  230. X        printf(
  231. X"\n--------------------------------------------------------------------\n\n\n");
  232. X
  233. X    if (strlen(from)    > 0) { printf("%s", from);    displayed_line++;}
  234. X    if (strlen(subject) > 0) { printf("%s", subject); displayed_line++;}
  235. X    if (strlen(to)      > 0) { printf("%s", to);      displayed_line++;}
  236. X    if (strlen(date)    > 0) { printf("%s", date);    displayed_line++;}
  237. X    
  238. X    if (displayed_line)
  239. X       putchar('\n');
  240. X}
  241. X
  242. Xchar *words(word, num_words, buffer)
  243. Xint word, num_words;
  244. Xchar *buffer;
  245. X{
  246. X    /** Return a buffer starting at 'word' and containing 'num_words'
  247. X        words from buffer.  Assume white space will delimit each word.
  248. X    **/
  249. X
  250. X    static char internal_buffer[SLEN];
  251. X    char   *wordptr, *bufptr, mybuffer[SLEN], *strtok();
  252. X    int    wordnumber = 0, copying_words = 0;
  253. X
  254. X    internal_buffer[0] = '\0';    /* initialize */
  255. X
  256. X    strcpy(mybuffer, buffer);
  257. X    bufptr = (char *) mybuffer;    /* and setup */
  258. X
  259. X    while ((wordptr = strtok(bufptr, " \t")) != NULL) {
  260. X      if (++wordnumber == word) {
  261. X        strcpy(internal_buffer, wordptr);
  262. X        copying_words++;
  263. X        num_words--;
  264. X      }
  265. X      else if (copying_words) {
  266. X        strcat(internal_buffer, " ");
  267. X        strcat(internal_buffer, wordptr);
  268. X        num_words--;
  269. X      }
  270. X
  271. X      if (num_words < 1) 
  272. X        return((char *) internal_buffer);
  273. X
  274. X      bufptr = NULL;
  275. X    }
  276. X
  277. X    return( (char *) internal_buffer);
  278. X}
  279. X
  280. Xint
  281. Xreal_from(buffer)
  282. Xchar *buffer;
  283. X{
  284. X    /***** Returns true iff 's' has the seven 'from' fields, (or
  285. X           8 - some machines include the TIME ZONE!!!) *****/
  286. X
  287. X    char sixthword[STRING], seventhword[STRING],
  288. X         eighthword[STRING], ninthword[STRING];
  289. X
  290. X    /* From <user> <day> <month> <day> <hr:min:sec> <year> */
  291. X
  292. X    if(strncmp(buffer, "From ", 5) != 0)
  293. X      return(FALSE);
  294. X
  295. X    /* Extract 6th, 7th, 8th, and 9th words */
  296. X    seventhword[0] = eighthword[0] = ninthword[0] = '\0';
  297. X    sscanf(buffer, "%*s %*s %*s %*s %*s %s %s %s %s",
  298. X      sixthword, seventhword, eighthword, ninthword);
  299. X
  300. X    /* Not a from line if 6th word doesn't have colons for time field */
  301. X    if(strlen(sixthword) < 3)
  302. X      return(FALSE);
  303. X    if (sixthword[1] != ':' && sixthword[2] != ':')
  304. X      return(FALSE);        
  305. X
  306. X    /* Not a from line if there is no seventh word */
  307. X    if(seventhword[0] == '\0')
  308. X      return(FALSE);
  309. X
  310. X    /* Not a from line if there is a ninthword */
  311. X    if (eighthword[0] != '\0') {
  312. X      if(ninthword[0] != '\0')
  313. X        return(FALSE);    
  314. X    }
  315. X
  316. X    return(TRUE);
  317. X}
  318. SHAR_EOF
  319. echo "File utils/readmsg.c is complete"
  320. chmod 0444 utils/readmsg.c || echo "restore of utils/readmsg.c fails"
  321. rm -f s2_seq_.tmp
  322. echo "You have unpacked the last part"
  323. exit 0
  324.  
  325. exit 0 # Just in case...
  326.