home *** CD-ROM | disk | FTP | other *** search
- //=========================================================
- //
- // loadix.c
- //
- // int loadix(char *user)
- //
- // user gives the name of the mailbox WITHOUT the .txt
- //
- // returns:
- // 0 index loaded ok
- // 1 user's mailbox is empty
- // 2 user's mailbox does not exist
- // 3 other error
- //
- //
- // Will scan the default mailbox file and construct
- // an index of mail items.
- //
- // A mail item has the format:
- //
- // Flag line: From ...
- // Header lines: Received: (this line can be 1 or more in length)
- // Date: Ddd, dd Mmm yy hh:mm:ss TZ
- // Message-Id: <xxxx@domain>
- // From: user@domain (name)
- // Reply-To: use@domain
- // To: recipient
- // Subject: text
- // (Other header lines)
- // Text flag line: CRLF (a blank line)
- // Text 0 or more text lines
- //
- //=========================================================
-
- // $Id: loadix.c,v 1.3 1994/02/08 23:32:02 gbj Exp user $
-
- /*
- $Log: loadix.c,v $
- * Revision 1.3 1994/02/08 23:32:02 gbj
- * First public release.
- *
- * Revision 1.2 1994/02/08 03:17:50 gbj
- * loadix() was processing quoted headers which did not have an initial
- * quote character as pukka headers. Added a flag to indicate whether
- * I am really in a header and only processed such header lines.
- *
- * Revision 1.1 1994/02/08 03:15:10 gbj
- * Initial revision
- *
- */
-
- #include "mailer.h"
- #include "utils.h"
-
- int loadix(char *xuser)
- {
- struct stat m;
- char ubox[256];
- int mno;
- long cpos, lpos;
- char *buf, *xbuf;
- char *f1, *f2, *f3, *f4, *f5, *f6;
- char *bp;
- int HDR=FALSE;
-
- maxmsgno=-1;
- buf=(char*)malloc(4096);
- if (buf == NULL)
- {
- fprintf(stderr, "\nloadix: Can't allocate 4096 bytes for buffer\n");
- return 3;
- }
- xbuf=(char*)malloc(4096);
- if (xbuf == NULL)
- {
- fprintf(stderr, "\nloadix: Can't allocate 4096 bytes for xbuffer\n");
- return 3;
- }
- // First, see if the mbox exists
- strcpy(ubox, mailpath);
- strcat(ubox, "\\");
- strcat(ubox, xuser);
- strcat(ubox, ".txt");
- fprintf(stderr, "loadix: processing %s\n", ubox);
- strcpy(user, xuser);
-
- if (access(ubox, 0))
- {
- free(buf);
- return 2; // mailbox does not exists
- }
-
- // See if there is anything in it
- if (stat(ubox, &m))
- {
- free(buf);
- return 1; // can't stat, assume empty
- }
- if (m.st_size <= 0)
- {
- free(buf);
- return 1; // empty
- }
-
- // Ok, the mailbox exists and is not empty, so let's open it
- // and load the MAILIX structure
-
- mno=-1;
- maxmsgno=-1;
- lpos=0;
- cpos=0;
- mfd=fopen(ubox, "rb");
- if (mfd == NULL)
- {
- free(buf);
- fprintf(stderr, "\nloadix: Can't open %s\n", ubox);
- return 3;
- }
-
- bp=fgets(buf, 4095, mfd);
- if (bp == NULL) // EOF with no data or IO error
- {
- free(buf);
- fprintf(stderr, "\nloadix: No data or IO error\n");
- fclose(mfd);
- return 3;
- }
- cpos=ftell(mfd);
- if (cpos == -1)
- {
- free(buf);
- fprintf(stderr, "\nloadix: ftell() error, lpos=%ld", lpos);
- fclose(mfd);
- return 3;
- }
-
- if (bp != NULL)
- {
- scaneol(buf);
- strcat(buf, "\n");
- if (strncmp(buf, "From ", 5) != 0)
- {
- fprintf(stderr, "\nloadix: no initial 'From' flag line\n");
- free(buf);
- fclose(mfd);
- return 3;
- }
- }
-
- while (bp != NULL) // Process the mailbox
- {
- strcpy(xbuf, buf);
- if (*buf == '\n' || *buf == '\r' || *buf == '\0')
- HDR=FALSE; // Mark end of header
- else if (strncmp(buf, "From ", 5) == 0 && strchr(buf, '@'))
- {
- HDR=TRUE; // Start of header
- mno++;
- maxmsgno=mno;
- mailix[mno].msgno=mno;
- mailix[mno].rflag=mailix[mno].dflag=0;
- strcpy(mailix[mno].sender, "**NO SENDER**");
- strcpy(mailix[mno].msgdate, "**NO DATE**");
- strcpy(mailix[mno].subject, "**NO SUBJECT**");
- strcpy(mailix[mno].replyto, "**NO REPLYTO**");
- mailix[mno].fpos=lpos;
- }
- else if (HDR)
- {
- f1=strtok(buf, " \r\n");
- if (f1 != NULL)
- {
-
- if (strncmp(f1, "Date:", 5) == 0)
- {
- if (strncmp(mailix[mno].msgdate, "**", 2) == 0)
- {
- f2=strtok(NULL, " \r\n"); // Ddd,
- f3=strtok(NULL, " \n"); // dd
- f4=strtok(NULL, " \r\n"); // Mmm
- f5=strtok(NULL, " \r\n"); // yy
- f6=strtok(NULL, " \r\n"); // hh:mm:ss
- if (f3 && f4 && f6)
- {
- sprintf(mailix[mno].msgdate,
- "%-2.2s %-3.3s %-5.5s",f3, f4, f6);
- }
- }
- }
- else if (strncmp(f1, "From:", 5) == 0)
- {
- f2=strtok(NULL, "\r\n");
- sprintf(mailix[mno].sender, "%s", f2);
- if (strncmp(mailix[mno].replyto, "**", 2) == 0)
- sprintf(mailix[mno].replyto, "%s", f2);
- }
- else if (strncmp(f1, "Reply-To:", 9) == 0)
- {
- f2=strtok(NULL, "\r\n");
- sprintf(mailix[mno].replyto, "%s", f2);
- if (strncmp(mailix[mno].sender, "**", 2) == 0)
- sprintf(mailix[mno].sender, "%s", f2);
- }
- else if (strncmp(f1, "Subject:", 8) == 0)
- {
- f2=strtok(NULL, "\r\n");
- sprintf(mailix[mno].subject, "%.28s", f2);
- }
- else if (strncmp(f1, "X-Status:", 9) == 0)
- {
- if (xbuf[10] == 'Y')
- mailix[mno].rflag=1;
- if (xbuf[11] == 'D')
- mailix[mno].dflag=1;
- }
- }
- }
- bp=fgets(buf, 4095, mfd);
- if (bp == NULL) // EOF with no data or IO error
- {
- break;
- }
- lpos=cpos;
- cpos=ftell(mfd);
- if (cpos == -1)
- {
- free(buf);
- fprintf(stderr, "\nloadix: ftell() error, lpos=%ld", lpos);
- fclose(mfd);
- return 3;
- }
- }
-
- free(buf);
- fclose(mfd);
- strcpy(mbox, ubox); // Set global var with mbox path
- parse_replyto();
-
- return 0;
- }
-
- // Ensure that replyto contains <...> without the <>
- void parse_replyto(void)
- {
- int i;
- char *f1, *f2;
- char buf[128];
-
- for (i=0; i <= maxmsgno; i++)
- {
- // printf("Reply To: before: %s\n", mailix[i].replyto);
- strcpy(buf, mailix[i].replyto);
- f1=strtok(buf, "<\r\n");
- f2=strtok(NULL, ">\r\n");
- if (f2)
- strcpy(mailix[i].replyto, f2);
- // printf("Reply To: after: %s\n", mailix[i].replyto);
- }
- }
-