home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / BMUTIL.C < prev    next >
C/C++ Source or Header  |  1993-03-07  |  24KB  |  1,019 lines

  1. /*
  2.  *    Simple mail user interface for KA9Q IP/TCP package.
  3.  *    A.D. Barksdale Garbee II, aka Bdale, N3EUA
  4.  *    Copyright 1986 Bdale Garbee, All Rights Reserved.
  5.  *    Permission granted for non-commercial copying and use, provided
  6.  *    this notice is retained.
  7.  *    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  8.  *    Permission granted for non-commercial copying and use, provided
  9.  *    this notice is retained.
  10.  *
  11.  *    Ported to NOS at 900120 by Anders Klemets SM0RGV.
  12.  */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <time.h>
  17. #include "global.h"
  18. #include "ftpserv.h"
  19. #include "smtp.h"
  20. #include "proc.h"
  21. #include "usock.h"
  22. #include "socket.h"
  23. #include "telnet.h"
  24. #include "timer.h"
  25. #include "session.h"
  26. #include "files.h"
  27.  
  28. #define        SETVBUF
  29. #if    defined(UNIX) || defined(MICROSOFT)
  30. #include    <sys/types.h>
  31. #endif
  32. /*
  33. #if    defined(UNIX) || defined(MICROSOFT) || defined(__TURBOC__)
  34. #include    <sys/stat.h>
  35. #endif
  36. #ifdef AZTEC
  37. #include <stat.h>
  38. #endif
  39. */
  40. #include <fcntl.h>
  41. #include "bm.h"
  42. #include "mailbox.h"
  43.  
  44. #ifdef SETVBUF
  45. #define        MYBUF    1024
  46. #endif
  47.  
  48. void scanmail(struct mbx *m);
  49.  
  50. extern long ftell();
  51. static char Badmsg[] = "Invalid Message number %d\n";
  52. static char Nomail[] = "No messages\n";
  53. static char Noaccess[] = "Unable to access %s\n";
  54. static int readnotes __ARGS((struct mbx *m,FILE *ifile,int update));
  55. static long isnewmail __ARGS((struct mbx *m));
  56. static int initnotes __ARGS((struct mbx *m));
  57. static int lockit __ARGS((struct mbx *m));
  58. static long fsize __ARGS((char *name));
  59. static void mfclose __ARGS((struct mbx *m));
  60. static int tkeywait __ARGS((char *prompt,int flush));
  61.  
  62. static int initnotes(struct mbx *m)
  63. {
  64.     FILE    *tmpfile();
  65.     FILE    *ifile;
  66.     register struct    let *cmsg;
  67.     char buf[256];
  68.     int     i, ret;
  69.  
  70.     sprintf(buf,"%s/%s.txt",Mailspool,m->area);
  71.     if ((ifile = fopen(buf,READ_TEXT)) == NULLFILE)
  72.         return 0;
  73.     fseek(ifile,0L,2);     /* go to end of file */
  74.     m->mboxsize = ftell(ifile);
  75.     rewind(ifile);
  76.     if(!stricmp(m->area,m->name)) /* our private mail area */
  77.         m->mysize = m->mboxsize;
  78.     if ((m->mfile = tmpfile()) == NULLFILE) {
  79.         (void) fclose(ifile);
  80.         return -1;
  81.     }
  82. #ifdef    SETVBUF
  83.     if (m->stdinbuf == NULLCHAR)
  84.         m->stdinbuf = mallocw(MYBUF);
  85.     setvbuf(ifile, m->stdinbuf, _IOFBF, MYBUF);
  86.     if (m->stdoutbuf == NULLCHAR)
  87.         m->stdoutbuf = mallocw(MYBUF);
  88.     setvbuf(m->mfile, m->stdoutbuf, _IOFBF, MYBUF);
  89. #endif
  90.     m->nmsgs = 0;
  91.     m->current = 0;
  92.     m->change = 0;
  93.     m->newmsgs = 0;
  94.     m->anyread = 0;
  95.     /* Allocate space for reading messages */
  96.     free((char *)m->mbox);
  97.     m->mbox = (struct let *)callocw(Maxlet+1,sizeof(struct let));
  98.     ret = readnotes(m,ifile,0);
  99.     (void) fclose(ifile);
  100. #ifdef SETVBUF
  101.     free(m->stdinbuf);
  102.     m->stdinbuf = NULLCHAR;
  103. #endif
  104.     if (ret != 0)
  105.         return -1;
  106.     for (cmsg = &m->mbox[1],i = 1; i <= m->nmsgs; i++, cmsg++)  
  107.         if ((cmsg->status & BM_READ) == 0) {
  108.             m->newmsgs++;
  109.             if (m->current == 0)
  110.                 m->current = i;
  111.         }
  112.     /* start at one if no new messages */
  113.     if (m->current == 0)
  114.         m->current++;
  115.  
  116.     return 0;
  117. }
  118.  
  119. /* readnotes assumes that ifile is pointing to the first
  120.  * message that needs to be read.  For initial reads of a
  121.  * notesfile, this will be the beginning of the file.  For
  122.  * rereads when new mail arrives, it will be the first new
  123.  * message.
  124.  */
  125. static int readnotes(struct mbx *m,FILE *ifile,int update)
  126. {
  127.     char     tstring[LINELEN];
  128.     long    cpos;
  129.     register struct    let *cmsg;
  130.     register char *line;
  131.  
  132.     cmsg = (struct let *)NULL;
  133.     line = tstring;
  134.     while(fgets(line,LINELEN,ifile) != NULLCHAR) {
  135.         /* scan for begining of a message */
  136.         if(strncmp(line,"From ",5) == 0) {
  137.             pwait(NULL);
  138.             cpos = ftell(m->mfile);
  139.             fputs(line,m->mfile);
  140.             if (m->nmsgs == Maxlet) {
  141.                 tprintf("Mail box full: > %d messages\n",Maxlet);
  142.                 mfclose(m);
  143.                 return -1;
  144.             }
  145.             m->nmsgs++;
  146.             cmsg = &m->mbox[m->nmsgs];
  147.             cmsg->start = cpos;
  148.             if(!update)
  149.                 cmsg->status = 0;
  150.             cmsg->size = strlen(line);
  151.             while (fgets(line,LINELEN,ifile) != NULLCHAR) {
  152.                 if (*line == '\n') { /* done header part */
  153.                     cmsg->size++;
  154.                     putc(*line, m->mfile);
  155.                     break;
  156.                 }
  157.                 if (htype(line) == STATUS) {
  158.                     if (line[8] == 'R') 
  159.                         cmsg->status |= BM_READ;
  160.                     continue;
  161.                 }
  162.                 cmsg->size += strlen(line);
  163.                 if (fputs(line,m->mfile) == EOF) {
  164.                     tprintf("tmp file: %s",sys_errlist[errno]);
  165.                     mfclose(m);
  166.                     return -1;
  167.                 }
  168.  
  169.             }
  170.         } else if (cmsg) {
  171.             cmsg->size += strlen(line);
  172.             fputs(line,m->mfile);
  173.         }
  174.     }
  175.     return 0;
  176. }
  177.  
  178. /* list headers of a notesfile a message */
  179. int
  180. dolistnotes(argc,argv,p)
  181. int argc;
  182. char *argv[];
  183. void *p;
  184. {
  185.     struct mbx *m;
  186.     register struct    let *cmsg;
  187.     register char    *cp, *s;
  188.     char    smtp_date[SLINELEN], smtp_from[SLINELEN];
  189.     char    smtp_subject[SLINELEN], tstring[LINELEN], type;
  190.     int    start, stop;
  191.     long    size;
  192.     char    *area;
  193.  
  194.     m = (struct mbx *) p;
  195.     if (m->mfile == NULLFILE) {
  196.         tprintf(Nomail);
  197.         return 0;
  198.     }
  199.  
  200.     area = strdup(m->area);
  201.     while((cp = strchr(area,'/')) != NULLCHAR)
  202.         *cp = '.';
  203.     tprintf("Mail area: %s  %d message%s -  %d new\n\n",area,m->nmsgs,
  204.         m->nmsgs == 1 ? " " : "s ", m->newmsgs);
  205.     free(area);
  206.  
  207.     stop = m->nmsgs;
  208.     if(m->stype == 'L') {        /* LL (List Latest) command */
  209.          if(argc > 1)
  210.           start = stop - atoi(argv[1]) + 1;
  211.          else
  212.           start = stop;
  213.     }
  214.     else {
  215.          if(argc > 1)
  216.           start = atoi(argv[1]);
  217.          else
  218.           start = 1;
  219.          if(argc > 2)
  220.           stop = atoi(argv[2]);
  221.     }
  222.     if(stop > m->nmsgs)
  223.         stop = m->nmsgs;
  224.     if(start < 1 || start > stop) {
  225.         tprintf("Invalid range.\n");
  226.         return 0;
  227.     }
  228.     for (cmsg = &m->mbox[start]; start <= stop; start++, cmsg++) {
  229.         *smtp_date = '\0';
  230.         *smtp_from = '\0';
  231.         *smtp_subject = '\0';
  232.         type = ' ';
  233.         fseek(m->mfile,cmsg->start,0);
  234.         size = cmsg->size;
  235.         while (size > 0 && fgets(tstring,sizeof(tstring),m->mfile)
  236.                != NULLCHAR) {
  237.             if (*tstring == '\n')    /* end of header */
  238.                 break;
  239.             size -= strlen(tstring);
  240.             rip(tstring);
  241.             /* handle continuation later */
  242.             if (*tstring == ' '|| *tstring == '\t')
  243.                 continue;
  244.             switch(htype(tstring)) {
  245.             case FROM:
  246.                 cp = getaddress(tstring,0);
  247.                 sprintf(smtp_from,"%.30s",
  248.                     cp != NULLCHAR ? cp : "");
  249.                 break;
  250.             case SUBJECT:
  251.                 sprintf(smtp_subject,"%.34s",&tstring[9]);
  252.                 break;
  253.             case DATE:
  254.                 if ((cp = strchr(tstring,',')) == NULLCHAR)
  255.                     cp = &tstring[6];
  256.                 else
  257.                     cp++;
  258.                 /* skip spaces */
  259.                 while (*cp == ' ') cp++;
  260.                 if(strlen(cp) < 17)
  261.                     break;     /* not a valid length */
  262.                 s = smtp_date;
  263.                 /* copy day */
  264.                 if (atoi(cp) < 10 && *cp != '0') {
  265.                     *s++ = ' ';
  266.                 } else
  267.                     *s++ = *cp++;
  268.                 *s++ = *cp++;
  269.  
  270.                 *s++ = ' ';
  271.                 *s = '\0';
  272.                 while (*cp == ' ')
  273.                     cp++;
  274.                 strncat(s,cp,3);    /* copy month */
  275.                 cp += 3;
  276.                 while (*cp == ' ')
  277.                     cp++;
  278.                 /* skip year */
  279.                 while (isdigit(*cp))
  280.                     cp++;
  281.                 /* copy time */
  282.                 strncat(s,cp,6); /* space hour : min */
  283.                 break;
  284.             case BBSTYPE:
  285.                 type = tstring[16];
  286.                 break;
  287.             case NOHEADER:
  288.                 break;
  289.             }
  290.         }
  291.         if((type == m->stype && m->stype != ' ') || m->stype == ' '
  292.            || m->stype == 'L')
  293.              tprintf("%c%c%c%3d %-27.27s %-12.12s %5ld %.25s\n",
  294.                  (start == m->current ? '>' : ' '),
  295.                  (cmsg->status & BM_DELETE ? 'D' : ' '),
  296.                  (cmsg->status & BM_READ ? 'Y' : 'N'),
  297.                  start, smtp_from, smtp_date,
  298.                  cmsg->size, smtp_subject);
  299.     }
  300.     return 0;
  301. }
  302.  
  303. /*  save msg on stream - if noheader set don't output the header */
  304. int msgtofile(struct mbx *m,int msg,FILE *tfile,int noheader)
  305. {
  306.     char    tstring[LINELEN];
  307.     long     size;
  308.  
  309.     if (m->mfile == NULLFILE) {
  310.         tprintf(Nomail);
  311.         return -1;
  312.     }
  313.     fseek(m->mfile,m->mbox[msg].start,0);
  314.     size = m->mbox[msg].size;
  315.  
  316.     if (noheader) {
  317.         /* skip header */
  318.         while (size > 0 && fgets(tstring,sizeof(tstring),m->mfile)
  319.                != NULLCHAR) {
  320.             size -= strlen(tstring);
  321.             if (*tstring == '\n')
  322.                 break;
  323.         }
  324.     }
  325.     while (size > 0 && fgets(tstring,sizeof(tstring),m->mfile)
  326.            != NULLCHAR) {
  327.         size -= strlen(tstring);
  328.         fputs(tstring,tfile);
  329.         if (ferror(tfile)) {
  330.             tprintf("Error writing mail file\n");
  331.             return -1;
  332.         }
  333.     }
  334.     return 0;
  335. }
  336.  
  337. /*  dodelmsg - delete message in current notesfile */
  338. int
  339. dodelmsg(argc,argv,p)
  340. int argc;
  341. char *argv[];
  342. void *p;
  343. {
  344.     struct mbx *m;
  345.     int msg,i;
  346.     m = (struct mbx *) p;
  347.     if (m->mfile == NULLFILE) {
  348.         tprintf(Nomail);
  349.         return 0;
  350.     }
  351.     for(i = 1; i < argc; ++i) {
  352.         msg = atoi(argv[i]);
  353.         if(msg < 0 || msg > m->nmsgs) {
  354.             tprintf(Badmsg,msg);
  355.             continue;
  356.         }
  357.         /* Check if we have permission to delete others mail */
  358.         if(!(m->privs & FTP_WRITE) && stricmp(m->area,m->name)) {
  359.             tprintf(Noperm);
  360.             return 0;
  361.         }
  362.         m->mbox[msg].status |= BM_DELETE;
  363.         tprintf("Msg %d Killed.\n", msg);
  364.         m->change = 1;
  365.     }
  366.     return 0;
  367. }
  368. /* close the temp file while coping mail back to the mailbox */
  369. int closenotes(struct mbx *m)
  370. {
  371.     register struct    let *cmsg;
  372.     register char *line;
  373.     char tstring[LINELEN], buf[256];
  374.     long size;
  375.     int i, nostatus = 0, nodelete;
  376.     FILE    *nfile;
  377.  
  378.     if (m->mfile == NULLFILE)
  379.         return 0;
  380.  
  381.     if(!m->change) {        /* no changes were made */
  382.         mfclose(m);
  383.         m->mboxsize = 0;
  384.         return 0;
  385.     }
  386.     /* If this area is a public message area, then we will not add a
  387.      * Status line to indicate that the message has been read.
  388.      */
  389.     nostatus = isarea(m->area);
  390.  
  391.     /* Don't delete messages from public message areas unless you are
  392.      * a BBS.
  393.      */
  394.     if(nostatus)
  395.         nodelete = !(m->privs & SYSOP_CMD);
  396.     else
  397.         nodelete = 0;
  398.  
  399.     /* See if any messages have been forwarded, otherwise just close
  400.      * the file and return since there is nothing to write back.
  401.      */
  402.     if(nostatus && nodelete) {
  403.         for(i=1; i <= m->nmsgs; ++i)
  404.             if(m->mbox[i].status & BM_FORWARDED)
  405.                 break;
  406.         if(i > m->nmsgs) {
  407.             mfclose(m);
  408.             m->mboxsize = 0;
  409.             return 0;
  410.         }
  411.     }
  412.     line = tstring;
  413.     scanmail(m);
  414.     if(lockit(m))
  415.         return -1;
  416.     sprintf(buf,"%s/%s.txt",Mailspool,m->area);
  417.     if ((nfile = fopen(buf,WRITE_TEXT)) == NULLFILE) {
  418.         tprintf(Noaccess,buf);
  419.         mfclose(m);
  420.         m->mboxsize = 0;
  421.         rmlock(Mailspool,m->area);
  422.         return -1;
  423.     }
  424.     /* copy tmp file back to notes file */
  425.     for (cmsg = &m->mbox[1],i = 1; i <= m->nmsgs; i++, cmsg++) {
  426.         fseek(m->mfile,cmsg->start,0);
  427.         size = cmsg->size;
  428.         /* It is not possible to delete messages if nodelete is set */
  429.         if ((cmsg->status & BM_DELETE) && !nodelete)
  430.             continue;
  431.         /* copy the header */
  432.         while (size > 0 && fgets(line,LINELEN,m->mfile) != NULLCHAR) {
  433.             size -= strlen(line);
  434.             if (*line == '\n') {
  435.                 if (cmsg->status & BM_FORWARDED)
  436.                     fprintf(nfile,"%s%s\n",Hdrs[XFORWARD],
  437.                         m->name);
  438.                 if ((cmsg->status & BM_READ) != 0 && !nostatus)
  439.                     fprintf(nfile,"%sR\n",Hdrs[STATUS]);
  440.                 fprintf(nfile,"\n");
  441.                 break;
  442.             }
  443.             fputs(line,nfile);
  444.             /* pwait(NULL);  can cause problems if exiting NOS */
  445.         }
  446.         while (size > 0 && fgets(line,LINELEN,m->mfile) != NULLCHAR) {
  447.             fputs(line,nfile);
  448.             size -= strlen(line);
  449.             /* pwait(NULL);   dont want no damaged files */
  450.             if (ferror(nfile)) {
  451.                 tprintf("Error writing mail file\n");
  452.                 (void) fclose(nfile);
  453.                 mfclose(m);
  454.                 m->mboxsize = 0;
  455.                 rmlock(Mailspool,m->area);
  456.                 return -1;
  457.             }
  458.         }
  459.     }
  460.     m->nmsgs = 0;
  461.     if (!stricmp(m->name,m->area))
  462.         m->mysize = ftell(nfile); /* Update the size of our mailbox */
  463.     /* remove a zero length file */
  464.     if (ftell(nfile) == 0L)
  465.         (void) unlink(buf);
  466.     (void) fclose(nfile);
  467.     mfclose(m);
  468.     m->mboxsize = 0;
  469.     rmlock(Mailspool,m->area);
  470.     pwait(NULL);
  471.     return 0;
  472. }
  473.  
  474. /* Returns 1 if name is a public message Area, 0 otherwise */
  475. int
  476. isarea(name)
  477. char *name;
  478. {
  479.     char buf[LINELEN], *cp;
  480.     FILE *fp;
  481.     if((fp = fopen(Arealist,READ_TEXT)) == NULLFILE)
  482.         return 0;
  483.     while(fgets(buf,sizeof(buf),fp) != NULLCHAR) {
  484.         /* The first word on each line is all that matters */
  485.         if((cp = strchr(buf,' ')) == NULLCHAR)
  486.             if((cp = strchr(buf,'\t')) == NULLCHAR)
  487.                 continue;
  488.         *cp = '\0';
  489.         if((cp = strchr(buf,'\t')) != NULLCHAR)
  490.             *cp = '\0';
  491.         if(stricmp(name,buf) == 0) {    /* found it */
  492.             fclose(fp);
  493.             return 1;
  494.         }
  495.     }
  496.     fclose(fp);
  497.     return 0;
  498. }
  499.  
  500. static int lockit(struct mbx *m)
  501. {
  502.     int c, cnt = 0;
  503.  
  504.     while(mlock(Mailspool,m->area)) {
  505.         pause(1000L/MSPTICK);    /* Wait one second */
  506.         if(++cnt == 10) {
  507.             cnt = 0;
  508.             c = tkeywait("Mail file is busy, Abort or Retry ? ",1);
  509.             if (c == 'A' || c == 'a' || c == EOF) {
  510.                 mfclose(m);
  511.                 return 1;
  512.             }
  513.         }
  514.     }
  515.     return 0;
  516. }
  517.  
  518. /* read the next message or the current one if new */
  519. int
  520. doreadnext(argc,argv,p)
  521. int argc;
  522. char *argv[];
  523. void *p;
  524. {
  525.     struct mbx *m;
  526.     char buf[10], *newargv[2];
  527.     m = (struct mbx *) p;
  528.     if (m->mfile == NULLFILE)
  529.         return 0;
  530.     if ((m->mbox[m->current].status & BM_READ) != 0) {
  531.         if (m->current == 1 && m->anyread == 0)
  532.             ;
  533.         else if (m->current < m->nmsgs) {
  534.             m->current++;
  535.         } else {
  536.             tprintf("Last message\n");
  537.             return 0;
  538.         }
  539.     }
  540.     sprintf(buf,"%d",m->current);
  541.     newargv[0] = "read";
  542.     newargv[1] = buf;
  543.     m->anyread = 1;
  544.     return doreadmsg(2,newargv,p);
  545. }
  546.  
  547. /*  display message on the crt given msg number */
  548. int
  549. doreadmsg(argc,argv,p)
  550. int argc;
  551. char *argv[];
  552. void *p;
  553. {
  554.     struct mbx *m;
  555.     register int c, col, lin;
  556.     char    buf[MAXCOL+2], *cp, *cp2;
  557.     int    msg, cnt, i, usemore, verbose, mbxheader, pathcol;
  558.     int    header, lastheader;
  559.     long     size;
  560.  
  561.     m = (struct mbx *) p;
  562.     if (m->mfile == NULLFILE) {
  563.         tprintf(Nomail);
  564.         return 0;
  565.     }
  566.     if(m->type == TELNET || m->type == TIP)
  567.         usemore = 1;    /* Display More prompt */
  568.     else
  569.         usemore = 0;
  570.     lin = MAXLIN-1;
  571.     for(i = 1; i < argc; ++i) {
  572.         msg = atoi(argv[i]);
  573.         if( msg < 1 || msg > m->nmsgs) {
  574.             tprintf(Badmsg,msg);
  575.             return 0;
  576.         }
  577.         fseek(m->mfile,m->mbox[msg].start,0);
  578.         size = m->mbox[msg].size;
  579.         m->current = msg;
  580.         header = NOHEADER;
  581.         mbxheader = 0;
  582.         if(*argv[0] == 'v')
  583.             verbose = 1;    /* display all header lines */
  584.         else
  585.             verbose = 0;
  586.  
  587.         tprintf("Message #%d %s\n", msg,
  588.             m->mbox[msg].status & BM_DELETE ? "[Deleted]" : "");
  589.         if ((m->mbox[msg].status & BM_READ) == 0) {
  590.             m->mbox[msg].status |= BM_READ;
  591.             m->change = 1;
  592.             m->newmsgs--;
  593.         }
  594.         --lin;
  595.         col = 0;
  596.         while (!feof(m->mfile) && size > 0) {
  597.             for (col = 0;  col < MAXCOL;) {
  598.                 c = getc(m->mfile);
  599.                 size--;
  600.                 if (feof(m->mfile) || size == 0) /* end this line */
  601.                     break;
  602.                 if (c == '\t') {
  603.                     cnt = col + 8 - (col & 7);
  604.                     if (cnt >= MAXCOL) /* end this line */
  605.                         break;
  606.                     while (col < cnt)
  607.                         buf[col++] = ' ';
  608.                 } else {
  609.                     if (c == '\n')
  610.                         break;
  611.                     buf[col++] = c;
  612.                 }
  613.             }
  614.             if(col < MAXCOL)
  615.                 buf[col++] = '\n';
  616.             buf[col] = '\0';
  617.             if(mbxheader > 0) {
  618.                  /* Digest R: lines and display as a Path: line */
  619.                  if(strncmp(buf,"R:",2) != 0 ||
  620.                 (cp = strchr(buf,'@')) == NULLCHAR) {
  621.                   tputc('\n');
  622.                   mbxheader = -1; /* don't get here again */
  623.                   verbose = 1;
  624.                  }
  625.                  else {
  626.                   if(*(++cp) == ':')
  627.                        ++cp;
  628.                   for(cp2 = cp; isalnum(*cp2); ++cp2)  ;
  629.                   *cp2 = '\0';
  630.                   if(mbxheader++ == 1) {
  631.                        tputs("Path: ");
  632.                        pathcol = 5;
  633.                        --lin;
  634.                   }
  635.                   else {
  636.                        tputc('!');
  637.                        if(++pathcol + strlen(cp) > MAXCOL-3){
  638.                         tputs("\n      ");
  639.                         pathcol = 5;
  640.                         --lin;
  641.                        }
  642.                   }
  643.                   tputs(cp);
  644.                   pathcol += strlen(cp);
  645.                   ++lin;    /* to allow for not printing it later */
  646.                  }
  647.             }
  648.             if(col == 1 && !verbose && !mbxheader)
  649.                  /* last header line reached */
  650.                  mbxheader = 1;
  651.             if(verbose)
  652.                 tputs(buf);
  653.             if(!verbose && !mbxheader){
  654.                 lastheader = header;
  655.                 if(!isspace(*buf))
  656.                     header = htype(buf);
  657.                 else
  658.                     header = lastheader;
  659.                 switch(header) {
  660.                 case TO:
  661.                 case CC:
  662.                 case FROM:
  663.                 case DATE:
  664.                 case SUBJECT:
  665.                 case APPARTO:
  666.                 case ORGANIZATION:
  667.                     tputs(buf);
  668.                     break;
  669.                 default:
  670.                     ++lin;
  671.                 }
  672.             }
  673.             col = 0;
  674.             if(usemore && --lin == 0){
  675.                 c = tkeywait("--More--",0);
  676.                 lin = MAXLIN-1;
  677.                 if(c == -1 || c == 'q' || c == 'Q')
  678.                     break;
  679.                 if(c == '\n' || c == '\r')
  680.                     lin = 1;
  681.             }
  682.         }
  683.     }
  684.     return 0;
  685. }
  686.  
  687. /* Set up m->to when replying to a message. The subject is returned in
  688.  * m->line.
  689.  */
  690. int mbx_reply(int argc,char *argv[],struct mbx *m,struct list **cclist,char **rhdr)
  691. {
  692.     char subject[MBXLINE], *msgid = NULLCHAR, *date = NULLCHAR;
  693.     char *cp;
  694.     int msg, lastheader, header = NOHEADER;
  695.     long size;
  696.  
  697.     /* Free anything that might be allocated
  698.      * since the last call to mbx_to() or mbx_reply()
  699.      */
  700.     free(m->to);
  701.     m->to = NULLCHAR;
  702.     free(m->tofrom);
  703.     m->tofrom = NULLCHAR;
  704.     free(m->tomsgid);
  705.     m->tomsgid = NULLCHAR;
  706.     free(m->origto);
  707.     m->origto = NULLCHAR;
  708.     subject[0] = '\0';
  709.  
  710.     if(argc == 1)
  711.          msg = m->current;
  712.     else
  713.          msg = atoi(argv[1]);
  714.     if (m->mfile == NULLFILE) {
  715.          if(m->sid & MBX_SID)
  716.           tputs("NO - ");
  717.         tputs(Nomail);
  718.         return 0;
  719.     }
  720.     if(msg < 1 || msg > m->nmsgs) {
  721.          if(m->sid & MBX_SID)
  722.           tputs("NO - ");
  723.          tputs(Badmsg);
  724.          return -1;
  725.     }
  726.     fseek(m->mfile,m->mbox[msg].start,0);
  727.     size = m->mbox[msg].size;
  728.     m->current = msg;
  729.     while(size > 0 && fgets(m->line,MBXLINE-1,m->mfile) != NULLCHAR) {
  730.          size -= strlen(m->line);
  731.          if(m->line[0] == '\n')    /* end of header */
  732.           break;
  733.          rip(m->line);
  734.          lastheader = header;
  735.          if(!isspace(m->line[0])) {
  736.           header = htype(m->line);
  737.           lastheader = NOHEADER;
  738.          }
  739.          switch(header) {
  740.          case SUBJECT:
  741.           if(strlen(m->line) > 11 && !strnicmp(&m->line[9],"Re:",3))
  742.                strcpy(subject,&m->line[9]);
  743.           else
  744.                sprintf(subject,"Re: %s",&m->line[9]);
  745.           break;
  746.          case FROM:
  747.           if(m->to == NULLCHAR && (cp = getaddress(m->line,0)) !=
  748.              NULLCHAR)
  749.                m->to = strdup(cp);
  750.           break;
  751.          case REPLYTO:
  752.           if((cp = getaddress(m->line,0)) != NULLCHAR) {
  753.                free(m->to);
  754.                m->to = strdup(cp);
  755.           }
  756.           break;
  757.          case MSGID:
  758.           free(msgid);
  759.           msgid = strdup(&m->line[12]);
  760.           break;
  761.          case DATE:
  762.           free(date);
  763.           date = strdup(&m->line[6]);
  764.           break;
  765.          case TO:
  766.          case CC:
  767.          case APPARTO:
  768.           /* Get addresses on To, Cc and Apparently-To lines */
  769.           cp = m->line;
  770.           m->line[strlen(cp)+1] = '\0';    /* add extra null at end */
  771.           for(;;) {
  772.                if((cp = getaddress(cp,lastheader == header ||
  773.                        cp != m->line)) == NULLCHAR)
  774.                 break;
  775.                addlist(cclist,cp,0);
  776.                /* skip to next address, if any */
  777.                cp += strlen(cp) + 1;
  778.           }
  779.           break;
  780.          }
  781.     }
  782.     if(msgid != NULLCHAR || date != NULLCHAR) {
  783.          *rhdr = mallocw(LINELEN);
  784.          sprintf(*rhdr,"In-Reply-To: your message ");
  785.          if(date != NULLCHAR) {
  786.           sprintf(m->line,"of %s.\n",date);
  787.           strcat(*rhdr,m->line);
  788.           if(msgid != NULLCHAR)
  789.                strcat(*rhdr,"             ");
  790.          }
  791.          if(msgid != NULLCHAR) {
  792.           sprintf(m->line,"%s\n",msgid);
  793.           strcat(*rhdr,m->line);
  794.          }
  795.          free(msgid);
  796.          free(date);
  797.     }
  798.     strcpy(m->line,subject);
  799.     return 0;
  800. }
  801.  
  802. void scanmail(struct mbx *m)         /* Get any new mail */
  803. {
  804.     FILE *nfile;
  805.     int ret, cnt;
  806.     char buf[256];
  807.     long diff;
  808.  
  809.     if ((diff = isnewmail(m)) == 0L)
  810.         return;
  811.     if(lockit(m))
  812.         return;
  813.     if(m->mfile == NULLFILE || diff < 0L) {
  814.         /* This is the first time scanmail is called, or the
  815.          * mail file size has decreased. In the latter case,
  816.          * any changes we did to this area will be lost, but this
  817.          * is not fatal.
  818.          */
  819.         initnotes(m);
  820.         rmlock(Mailspool,m->area);
  821.         return;
  822.     }
  823.     sprintf(buf,"%s/%s.txt",Mailspool,m->area);
  824.     if ((nfile = fopen(buf,READ_TEXT)) == NULLFILE)
  825.         tprintf(Noaccess,buf);
  826.     else {
  827.         /* rewind tempfile */
  828.         fseek(m->mfile,0L,0);
  829.         cnt = m->nmsgs;
  830.         /* Reread all messages since size they may have changed
  831.          * in size after a X-Forwarded-To line was added.
  832.          */
  833.         m->nmsgs = 0;
  834.         ret = readnotes(m,nfile,1);   /* get the mail */
  835.         m->newmsgs += m->nmsgs - cnt;
  836.         m->mboxsize = ftell(nfile);
  837.         if(!stricmp(m->name,m->area))
  838.             m->mysize = m->mboxsize;
  839.         (void) fclose(nfile);
  840.         if (ret != 0)
  841.             tprintf("Error updating mail file\n");
  842.     }
  843.     rmlock(Mailspool,m->area);
  844. }
  845.  
  846. /* Check the current mailbox to see if new mail has arrived.
  847.  * Returns the difference in size.
  848.  */
  849. static long isnewmail(struct mbx *m)
  850. {
  851.     char buf[256];
  852.     sprintf(buf,"%s/%s.txt",Mailspool,m->area);
  853.     return fsize(buf) - m->mboxsize;
  854. }
  855.  
  856. /* Check if the private mail area has changed */
  857. long isnewprivmail(struct mbx *m)
  858. {
  859.     long cnt;
  860.     char buf[256];
  861.     sprintf(buf,"%s/%s.txt",Mailspool,m->name);
  862.     cnt = m->mysize;
  863.     m->mysize = fsize(buf);
  864.     return m->mysize - cnt; /* != 0 not more than once */
  865. }
  866.  
  867. char *Hdrs[] = {
  868.     "Approved: ",
  869.     "From: ",
  870.     "To: ",
  871.     "Date: ",
  872.     "Message-Id: ",
  873.     "Subject: ",
  874.     "Received: ",
  875.     "Sender: ",
  876.     "Reply-To: ",
  877.     "Status: ",
  878.     "X-BBS-Msg-Type: ",
  879.     "X-Forwarded-To: ",
  880.     "Cc: ",
  881.     "Return-Receipt-To: ",
  882.     "Apparently-To: ",
  883.     "Errors-To: ",
  884.     "Organization: ",
  885.     NULLCHAR
  886. };
  887.  
  888. /* return the header type */
  889. int
  890. htype(s)
  891. char *s;
  892. {
  893.     register char *p;
  894.     register int i;
  895.  
  896.     p = s;
  897.     /* check to see if there is a ':' before and white space */
  898.     while (*p != '\0' && *p != ' ' && *p != ':')
  899.         p++;
  900.     if (*p != ':')
  901.         return NOHEADER;
  902.  
  903.     for (i = 0; Hdrs[i] != NULLCHAR; i++) {
  904.         if (strnicmp(Hdrs[i],s,strlen(Hdrs[i])) == 0)
  905.             return i;
  906.     }
  907.     return UNKNOWN;
  908. }
  909.  
  910. /* This function returns the length of a file. The proper thing would be
  911.  * to use stat(), but it fails when using DesqView together with Turbo-C
  912.  * code.
  913.  */
  914. static long
  915. fsize(name)
  916. char *name;
  917. {
  918.     long cnt;
  919.     FILE *fp;
  920.     if((fp = fopen(name,READ_TEXT)) == NULLFILE)
  921.         return -1L;
  922.     fseek(fp,0L,2);
  923.     cnt = ftell(fp);
  924.     fclose(fp);
  925.     return cnt;
  926. }
  927.  
  928. /* close the temporary mail file */
  929. static void mfclose(struct mbx *m)
  930. {
  931.     if(m->mfile != NULLFILE)
  932.         fclose(m->mfile);
  933.     m->mfile = NULLFILE;
  934. #ifdef SETVBUF
  935.     free(m->stdoutbuf);
  936.     m->stdoutbuf = NULLCHAR;
  937. #endif
  938. }
  939.  
  940. /* Parse a string in the "Text: <user@host>" or "Text: user@host (Text)"
  941.  * format for the address user@host.
  942.  */
  943. char *
  944. getaddress(string,cont)
  945. char *string;
  946. int cont;        /* true if string is a continued header line */
  947. {
  948.     char *cp, *ap = NULLCHAR;
  949.     int par = 0;
  950.     if((cp = getname(string)) != NULLCHAR) /* Look for <> style address */
  951.          return cp;
  952.     cp = string;
  953.     if(!cont)
  954.          if((cp = strchr(string,':')) == NULLCHAR)    /* Skip the token */
  955.           return NULLCHAR;
  956.          else
  957.           ++cp;
  958.     for(; *cp != '\0'; ++cp) {
  959.          if(par && *cp == ')') {
  960.           --par;
  961.           continue;
  962.          }
  963.          if(*cp == '(')        /* Ignore text within parenthesis */
  964.           ++par;
  965.          if(par)
  966.           continue;
  967.          if(*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == ',') {
  968.           if(ap != NULLCHAR)
  969.                break;
  970.           continue;
  971.          }
  972.          if(ap == NULLCHAR)
  973.           ap = cp;
  974.     }
  975.     *cp = '\0';
  976.     return ap;
  977. }
  978.  
  979. /* Print prompt and read one character, telnet version */
  980. static int
  981. tkeywait(prompt,flush)
  982. char *prompt;    /* Optional prompt */
  983. int flush;    /* Flush queued input? */
  984. {
  985.     int c, i, oldimode,oldomode;
  986.  
  987.     if(flush && socklen(Curproc->input,0) != 0)
  988.         recv_mbuf(Curproc->input,NULL,0,NULLCHAR,0); /* flush */
  989.     if(prompt == NULLCHAR)
  990.         prompt = "Hit enter to continue"; 
  991.     tprintf("%s%c%c%c",prompt,IAC,WILL,TN_ECHO);
  992.     usflush(Curproc->output);
  993.  
  994.     /* discard the response */
  995.  
  996.     oldimode = sockmode(Curproc->input,SOCK_BINARY);
  997.     oldomode = sockmode(Curproc->output,SOCK_BINARY);
  998.  
  999.     while((c = rrecvchar(Curproc->input)) == IAC){
  1000.         c = rrecvchar(Curproc->input);
  1001.         if(c > 250 && c < 255)
  1002.             rrecvchar(Curproc->input);
  1003.     }
  1004.  
  1005.     sockmode(Curproc->output,oldomode);
  1006.     sockmode(Curproc->input,oldimode);
  1007.  
  1008.     /* Get rid of the prompt */
  1009.     for(i=strlen(prompt);i != 0;i--)
  1010.         tputc('\b');
  1011.     for(i=strlen(prompt);i != 0;i--)
  1012.         tputc(' ');
  1013.     for(i=strlen(prompt);i != 0;i--)
  1014.         tputc('\b');
  1015.     tprintf("%c%c%c",IAC,WONT,TN_ECHO);
  1016.     usflush(Curproc->output);
  1017.     return c;
  1018. }
  1019.