home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / msgd196s.lzh / READMAIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-02  |  10.2 KB  |  451 lines

  1. /*
  2.   
  3. Title:    MsgEd
  4.   
  5. File:    readmail.c
  6.   
  7. Author:    Jim Nutt
  8.     
  9. Copr:    1987 by Jim Nutt
  10.   
  11. Description:
  12.     
  13.     handles all disk i/o for msged
  14.     
  15. Revision History:
  16.   
  17.     0.00    12 December 1987
  18.     
  19. Support Files:
  20.   
  21.     msged.h
  22.     dos.h
  23.   
  24. */
  25.  
  26. #include "msged.h"
  27. #ifdef __MSC__
  28. #include <sys\types.h>
  29. #include <fcntl.h>
  30. #endif
  31. #include <sys\stat.h>
  32. #ifdef __TURBOC__
  33. #include <fcntl.h>
  34. #include <dir.h>
  35. #endif
  36.  
  37. static void checkrecvd(MSG * m);
  38.  
  39. int     scanmail(char *mp)
  40.  
  41. {
  42. #ifdef __ZTC__
  43.     struct FIND *fileinfo = NULL;
  44. #endif
  45. #ifdef __TURBOC__
  46.     struct ffblk fileinfo;
  47. #endif
  48. #ifdef __MSC__
  49.     struct find_t fileinfo;
  50. #endif
  51.     char    path[PATHLEN];
  52.     int     cnt = 0;
  53.     int     msgnum;
  54.     FILE   *fp = NULL;
  55.     unsigned int status;
  56.  
  57.     if (messages != NULL)
  58.         free(messages);
  59.  
  60.         checkmem(messages = (char *) calloc(2, 1));     /* allocate for 1
  61.                              * message */
  62.     messages[1] = FALSE;
  63.     arealist[area].last = 1;
  64.     strcpy(path, mp);
  65.     strcat(path, "\\*.msg");
  66.  
  67. #ifdef __ZTC__
  68.     fileinfo = findfirst(path, 0);
  69.     while (fileinfo != NULL) {
  70.         msgnum = atoi(fileinfo->name);
  71.                 if ((msgnum != 0) && (fileinfo->size >= (long) sizeof(MSGHEADER))) {
  72.             cnt++;
  73.             if (msgnum > arealist[area].last) {
  74.                                 checkmem(messages = realloc(messages,msgnum + 1));
  75.                 arealist[area].last++;
  76.                 while (arealist[area].last < msgnum)
  77.                     messages[arealist[area].last++] = FALSE;
  78.             }
  79.             messages[msgnum] = TRUE;
  80.         }
  81.         fileinfo = findnext();
  82.     }
  83. #else
  84. #ifdef __TURBOC__
  85.     status = findfirst(path, &fileinfo, 0);
  86.     while (status == 0) {
  87.         msgnum = atoi(fileinfo.ff_name);
  88.                 if ((msgnum != 0) && (fileinfo.ff_fsize >= (long) sizeof(MSGHEADER))) {
  89.             cnt++;
  90.             if (msgnum > arealist[area].last) {
  91.                 checkmem(messages = realloc(messages,
  92.                                 msgnum + 1));
  93.                 arealist[area].last++;
  94.                 while (arealist[area].last < msgnum)
  95.                     messages[arealist[area].last++] = FALSE;
  96.             }
  97.             messages[msgnum] = TRUE;
  98.         }
  99.         status = findnext(&fileinfo);
  100.     }
  101. #else
  102. #ifdef __MSC__
  103.     status = _dos_findfirst(path, 0, &fileinfo);
  104.     while (status == 0) {
  105.         msgnum = atoi(fileinfo.name);
  106.                 if ((msgnum != 0) && (fileinfo.size >= (long) sizeof(MSGHEADER))) {
  107.             cnt++;
  108.             if (msgnum > arealist[area].last) {
  109.                 checkmem(messages = realloc(messages,
  110.                                 msgnum + 1));
  111.                 arealist[area].last++;
  112.                 while (arealist[area].last < msgnum)
  113.                     messages[arealist[area].last++] = FALSE;
  114.             }
  115.             messages[msgnum] = TRUE;
  116.         }
  117.         status = _dos_findnext(&fileinfo);
  118.     }
  119. #else
  120. #error scanmail() requires zortech, quick or turbo c to compile
  121. #endif
  122. #endif
  123. #endif
  124.  
  125.     strcpy(path, mp);
  126.     strcat(path, "\\lastread");
  127.     fp = fopen(path, "rb");
  128.     if (fp != NULL) {
  129.                 fread(&arealist[area].lastread, 2, 1, fp);
  130.                 if (fread(&arealist[area].current,2,1,fp) != 1)
  131.                         arealist[area].current = arealist[area].lastread;
  132.         fclose(fp);
  133.     }
  134.     else {
  135.         fp = fopen(path, "wb");
  136.         fputc(0, fp);
  137.                 fputc(0, fp);
  138.                 fputc(0, fp);
  139.                 fputc(0, fp);
  140.         fclose(fp);
  141.     }
  142.  
  143.     for (arealist[area].first = 1;
  144.          (arealist[area].first < arealist[area].last) &&
  145.          !messages[arealist[area].first];
  146.          arealist[area].first++)
  147.         ;
  148.  
  149.     if (!messages[arealist[area].lastread])
  150.         arealist[area].lastread = arealist[area].last;
  151.  
  152.         if ((arealist[area].current) == 0)
  153.         arealist[area].current = arealist[area].lastread =
  154.             arealist[area].last;
  155.  
  156.     if (cnt == 0)
  157.         arealist[area].current = arealist[area].lastread =
  158.                         arealist[area].first = arealist[area].last = 0;
  159.  
  160.         arealist[area].current = min(arealist[area].current, arealist[area].last);
  161.         arealist[area].lastread= min(arealist[area].lastread,arealist[area].last);
  162.  
  163.     return (cnt);
  164. }
  165.  
  166. int     readmsg(MSG * m, int n)
  167. {
  168.     struct stat buf;
  169.     char    path[PATHLEN];
  170.     char   *text = NULL;
  171.     int     fd = 0;
  172.         unsigned int     s;
  173.     LINE   *t = NULL;
  174.     int     i = 0, j = 0, flag = 0;
  175.     char    dst[30], src[30],ddom[30], sdom[30];
  176.  
  177.     if (!messages[n])
  178.         return (FALSE);
  179.  
  180.     sprintf(path, "%s\\%d.MSG", arealist[area].path, n);
  181.  
  182.         stat(path, &buf);
  183.  
  184.         if ((long) buf.st_size < (long) sizeof(MSGHEADER)) {
  185.                 messages[n] = FALSE;
  186.                 return(FALSE);
  187.         }
  188.  
  189.         if ((s = (unsigned int)(buf.st_size - sizeof(MSGHEADER))) < 0) {
  190.                 messages[n] = FALSE;
  191.                 return(FALSE);
  192.         }
  193.  
  194.         if ((fd = open(path, 0)) == -1) {
  195.                 messages[n] = FALSE;
  196.                 return (FALSE);
  197.         }
  198.  
  199.     if (m->to.domain != NULL)
  200.         free(m->to.domain);
  201.  
  202.     if (m->from.domain != NULL)
  203.         free(m->from.domain);
  204.  
  205.         m->msgnum = n;
  206.  
  207.         if (s > 0) {
  208.                 if ((text = (char *) calloc(1, (s + 1))) == NULL) {
  209.                         close(fd);
  210.                         messages[n] = FALSE;
  211.                         return(FALSE);
  212.                 }
  213.         }
  214.         else
  215.                 text = NULL;
  216.  
  217.         read(fd, (char *) &(m->header), (int) sizeof(MSGHEADER));
  218.  
  219.         if ((m->from.net = m->header.orig_net) < -1) {
  220.                 free(text);
  221.                 close(fd);
  222.                 messages[n] = FALSE;
  223.                 return(FALSE);
  224.         }
  225.         if ((m->from.node = m->header.orig) < -1) {
  226.                 free(text);
  227.                 close(fd);
  228.                 messages[n] = FALSE;
  229.                 return(FALSE);
  230.         }
  231.         if ((m->to.net = m->header.dest_net) < -1) {
  232.                 free(text);
  233.                 close(fd);
  234.                 messages[n] = FALSE;
  235.                 return(FALSE);
  236.         }
  237.         if ((m->to.node = m->header.dest) < -1) {
  238.                 free(text);
  239.                 close(fd);
  240.                 messages[n] = FALSE;
  241.                 return(FALSE);
  242.         }
  243.  
  244.         if (m->header.recvd && !arealist[area].netmail && readthread) {
  245.                 free(text);
  246.                 close(fd);
  247.                 messages[n] = FALSE;
  248.                 return(FALSE);
  249.         }
  250.  
  251.         if (text == NULL) {
  252.                 msgbuf.first = msgbuf.last = NULL;
  253.                 close(fd);
  254.                 return(TRUE);
  255.         }
  256.  
  257.         i = read(fd, text, s);
  258.  
  259.         if ((t = msgbuf.first = buffer(&text)) == NULL) {
  260.                 if (text != NULL)
  261.                         free(text);
  262.                 close(fd);
  263.                 return(TRUE);
  264.     }
  265.  
  266.     free(text);
  267.     close(fd);
  268.  
  269.     checkrecvd(m);
  270.  
  271.     *dst = '\0';
  272.     *src = '\0';
  273.  
  274.     i = j = 0;
  275.     m->from.point = m->to.point = 0;
  276.     m->from.zone = m->to.zone = thisnode.zone;
  277.     memset(path,0,PATHLEN);
  278.     memset(ddom,0,30);
  279.     memset(sdom,0,30);
  280.     memset(dst,0,30);
  281.     memset(src,0,30);
  282.  
  283.     while (t != NULL) {
  284.  
  285.         msgbuf.last = t;
  286.  
  287.         flag = 0;
  288.  
  289.         if ((t->text != NULL) && (*(t->text) == '\01')) {
  290.             flag += sscanf(t->text, "\01FMPT %i\n", &(i));
  291.             flag += sscanf(t->text, "\01TOPT %i\n", &(j));
  292.             flag += sscanf(t->text, "\01INTL %s %s\n", dst, src);
  293.             flag += sscanf(t->text, "\01DOMAIN %s %s %s %s\n",ddom,dst,sdom,src);
  294.             flag += sscanf(t->text, "\01---%s", path);
  295.         }
  296.  
  297.         if ((seenbys != YES) && (strncmp("SEEN-BY: ", t->text, 8) == 0))
  298.             flag = TRUE;
  299.  
  300.         if (flag && !kludgelines) {
  301.             LINE   *t2;
  302.  
  303.                         if (t->prev != NULL)
  304.                                 t->prev->next = t->next;
  305.                         else
  306.                                 msgbuf.first = t->next;
  307.  
  308.                         if (t->next) {
  309.                                 msgbuf.last = t->next;
  310.                                 t->next->prev = t->prev;
  311.                         }
  312.                         else
  313.                                 msgbuf.last = t->prev;
  314.  
  315.                         msgbuf.first->prev = NULL;
  316.  
  317.                         free(t->text);
  318.  
  319.             t2 = t;
  320.             t = t->next;
  321.             free(t2);
  322.         }
  323.         else
  324.             t = t->next;
  325.     }
  326.  
  327.     if (*src != '\0')
  328.         m->from = parsenode(src);
  329.  
  330.     if (*dst != '\0')
  331.         m->to = parsenode(dst);
  332.  
  333.     if (i)
  334.         m->from.point = i;
  335.  
  336.     if (j)
  337.         m->to.point = j;
  338.  
  339.     m->to.domain = NULL;
  340.     m->from.domain = NULL;
  341.  
  342.         if (*ddom != '\0') {
  343.                 m->to.domain = strlwr(strdup(ddom));
  344.                 assert(m->to.domain);
  345.         }
  346.             
  347.         if (*sdom != '\0') {
  348.                 m->from.domain = strlwr(strdup(sdom));
  349.                 assert(m->from.domain);
  350.         }
  351.  
  352.     return (TRUE);
  353. }
  354.  
  355. MSGHEADER readheader(int n)
  356.  
  357. {
  358.     char    path[PATHLEN];
  359.     int     fd = 0;
  360.     MSGHEADER m;
  361.  
  362.     if (!messages[n]) {
  363.         m.dest = -1;
  364.         return (m);
  365.     }
  366.  
  367.     sprintf(path, "%s\\%d.MSG", arealist[area].path, n);
  368.  
  369.     if ((fd = open(path, 0)) == -1) {
  370.                 m.dest = -1;
  371.         return (m);
  372.     }
  373.  
  374.     read(fd, (char *) &m, sizeof(MSGHEADER));
  375.  
  376.     close(fd);
  377.     return (m);
  378. }
  379.  
  380. char *readtext(int n)
  381.  
  382. {
  383.     char    path[PATHLEN];
  384.     int     fd = 0;
  385.     char   *t;
  386.     struct stat buf;
  387.     unsigned int s;
  388.     MSGHEADER tmp;
  389.     
  390.     if (!messages[n]) 
  391.         return (NULL);
  392.  
  393.     sprintf(path, "%s\\%d.MSG", arealist[area].path, n);
  394.  
  395.     if (stat(path, &buf) == -1)
  396.         return(NULL);
  397.  
  398.     if ((s = (unsigned int)(buf.st_size - sizeof(MSGHEADER))) < 1)
  399.         return(NULL);
  400.  
  401.     if ((fd = open(path, 0)) == -1) 
  402.         return (NULL);
  403.  
  404.         if (read(fd, (char *) &tmp, sizeof(MSGHEADER)) == -1) {
  405.                 close(fd);
  406.                 return(NULL);
  407.         }
  408.  
  409.         if ((t = (char *) calloc(1,(s + 145))) == NULL) {
  410.                 close(fd);
  411.                 return(NULL);
  412.         }
  413.  
  414.     strncpy(t,tmp.subj,72);
  415.     strncat(t,tmp.from,36);
  416.     strncat(t,tmp.to,36);
  417.  
  418.     if (read(fd, (t + strlen(t)), s) == -1) {
  419.         if (t) 
  420.                         free(t);
  421.                 close(fd);
  422.         return(NULL);
  423.     }
  424.  
  425.     close(fd);
  426.         t = (char *) realloc(t,strlen(t)+1);
  427.     return (t);
  428. }
  429.  
  430. static void checkrecvd(MSG * m)
  431. {
  432.         char   *s, *t;
  433.  
  434.     if (m->header.recvd)
  435.         return;
  436.  
  437.         s = strlwr(strdup(username));
  438.         assert(s);
  439.         t = (char *) calloc(1, sizeof(m->header.to) + 1);
  440.         memset(t,0,sizeof(t));
  441.         memcpy(t,m->header.to,sizeof(m->header.to) + 1);
  442.         strlwr(t);
  443.  
  444.         if ((memcmp(s, t, strlen(s)) == 0) || (!arealist[area].netmail && readthread)) {
  445.                 m->header.recvd = 1;
  446.                 writeheader(m->header, m->msgnum);
  447.     }
  448.     free(s);
  449.     free(t);
  450. }
  451.