home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / SCAN.C < prev    next >
C/C++ Source or Header  |  1992-08-19  |  5KB  |  220 lines

  1. /*
  2.    scan.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    911227 : Added this header
  5.    920620 : Version 0.1
  6.             - doesn't remember current folder
  7.             - doesn't support listing of a subrange
  8.    920706 : Added check to see if specified folder exists
  9.    920717 : Added ax25 pbbs R: line disposal
  10.    920721 : Fixed bug in listing short messages ("\nF" was not being found)
  11.    920819 : Catered for variation in the Date: header contents
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <io.h>
  18. #include "header.h"
  19. #include "buffer.h"
  20. #include "rc.h"
  21. #include "misc.h"
  22. #include "help.h"
  23. #include "pager.h"
  24. #include "current.h"
  25.  
  26. #ifdef BMH
  27. #define main scan_main
  28. #endif
  29.  
  30. static char *
  31. name(char *from)
  32. {
  33.    char *p, *q;
  34.  
  35.    if ( (p=strchr(from, '(')) != NULL) {
  36.       if ( (q=strchr(p, ')')) != NULL)
  37.          *q='\0';
  38.       else
  39.          return from;
  40.       return p+1;
  41.       }
  42.    if ( (p=strchr(from, '"')) != NULL) {
  43.       if ( (q=strchr(p+1, '"')) != NULL)
  44.          *q='\0';
  45.       else
  46.          return from;
  47.       return p+1;
  48.       }
  49.    return from;
  50. }
  51.  
  52. static char *
  53. topic(char **start, char **end, char *subject, char *line)
  54. {
  55.    int len = 0;
  56.    char *p = *start, *e = *end, *l = line;
  57.  
  58.    while ((*line++ = *subject++) > 0) { /* tcc doesn't like this line */
  59.       len++;
  60.       if (len>46) {
  61.          *line = '\0';
  62.          return l;
  63.          }
  64.       }
  65.    line--;
  66.    *line++ = '<';
  67.    *line++ = '<';
  68.    while (p<e) {
  69.       *line = *p++;
  70.       /*
  71.        * 920717 : Dispose of ax25 pbbs R: lines
  72.        */
  73.       if ((*(p-1)=='R') && (*(p-2)=='\n') && (*p==':')) {
  74.          char s[256];
  75.          (void) getstring(&p, &e, s, sizeof(s));
  76.          continue;
  77.          }
  78.       if ((*p == 'F') && ( *(p-1) == '\n') ) {
  79.          *line++ = '>';
  80.          *line++ = '>';
  81.          p--;
  82.          break;  /* can't risk calling refill, as that would mean missing the */
  83.          }       /* next message... */
  84.       if ((*line == '\n') || (*line == '\r') || (*line == 9) ) /* tab */
  85.          *line = ' ';
  86.       if (isspace(*line) && isspace(*(line-1)))
  87.          continue;
  88.       line++;
  89.       len++;
  90.       if (len>45)
  91.          break;
  92.       if ( (p==e) && (refill(&p, &e) != 0) )
  93.          break;
  94.       }
  95.       
  96.    if ((p==e) && (len<43)) {
  97.       *line++ = '>';
  98.       *line++ = '>';
  99.       }
  100.    *line++ = '\0';
  101.    *start = p;
  102.    *end = e;
  103.    return l;
  104. }
  105.  
  106. static int
  107. asc2month(char *s)
  108. {
  109.    char *result;
  110.    static char m[][4] = {
  111.          "Apr", "Aug", "Dec", "Feb", "Jan", "Jul",
  112.          "Jun", "Mar", "May", "Nov", "Oct", "Sep" };
  113.    static char m2num[] = {4, 8, 12, 2, 1, 7, 6, 3, 5, 11, 10, 9 };
  114.  
  115.    result = (char *) bsearch(s, m, 12, 4, strcmp);
  116.    return result == NULL ? 0 : (int) m2num[( (result - (char *) m) / 4 )];
  117. }
  118.  
  119. static void
  120. date2mh(char *date, int *day, int *month)
  121. {
  122.    if ( strlen(date) < 18) {
  123.       *day = 0;
  124.       *month = 0;
  125.       return ;
  126.       }
  127.    else 
  128.       date[17] = '\0';
  129.  
  130.    if ( (*day = atoi(&date[11])) == 0) {
  131.       /*
  132.        * bogon?
  133.        * Date: 06 Jan 91 17:30:20 UTC
  134.        * 0123456789
  135.        */
  136.       *day = atoi(&date[6]);
  137.       *month = asc2month(&date[9]);
  138.       }
  139.    else
  140.       /*
  141.        * Normal one
  142.        * Date: Sun, 06 Jan 91 17:30:20 UTC
  143.        * 012345678901234
  144.        */
  145.       *month = asc2month(&date[14]);
  146. }
  147.  
  148. static int
  149. dumpmsg(int i, char **start, char **end, int msg)
  150. {
  151.    char *argv[MAXHDR], line[256], buf[256];
  152.    int day = 0, month = 0;
  153.  
  154.    if ( parseheader(start, end, argv) == -1) {
  155.       fprintf(stderr, "dumpmsg: bad header on message %d\n", i);
  156.       return -1;
  157.       }
  158.    if (argv[DATE] != NULL)
  159.       date2mh(argv[DATE], &day, &month);
  160.    sprintf(buf, "%c%4d %2d/%02d %-18.18s %s",
  161.       i == msg ? '+' : ' ',
  162.       i, day, month,
  163.       argv[FROM] == NULL ? "" : name(argv[FROM]+6),
  164.       topic(start, end, argv[SUBJECT] == NULL ? "" : argv[SUBJECT]+9, line ) );
  165.    freeheader(argv);
  166.  
  167.    if ( page_puts(buf) == -1)
  168.       return -2;
  169.  
  170.    return 0;
  171. }
  172.  
  173. static void   
  174. listmsgs(char *p, char *end, int msg)
  175. {
  176.    int i = 0;
  177.  
  178.    page_setup();
  179.    while (1) {
  180.       while ( *p != 'F' )
  181.          p++;
  182.       if ( (p==end) && (refill(&p, &end) != 0) )
  183.          break;
  184.       else { /* may have found a From */
  185.          char s[256];
  186.  
  187.          if (*(p-1) != '\n') {
  188.             p++;
  189.             continue;
  190.             }
  191.          getstring(&p, &end, s, sizeof(s));
  192.          if (strncmp(s, "From ", 5) == 0)
  193.             if ( dumpmsg(++i, &p, &end, msg) == -2)
  194.                return ;
  195.          }
  196.       }
  197. }
  198.  
  199. int
  200. main(int argc, char *argv[])
  201. {
  202.    char *p, *end, *s;
  203.    int msg;
  204.  
  205.    dohelp(argc, argv, "scan [+folder]");
  206.  
  207.    if (setupbm()==-1)
  208.       return -1;
  209.  
  210.    if (getcurrent(argc, argv, &s, &msg) == -1)
  211.       return -1;
  212.  
  213.    if (loadmail("scan", s, &p, &end) == -1)
  214.       return -1;
  215.  
  216.    listmsgs(p, end, msg);
  217.  
  218.    return msg;
  219. }
  220.