#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <utime.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
};
#define HDR_LEN 128
+#define TAB 0x09
/*
* Like strncpy() but with terminated result.
return name;
}
+/*
+ * Tries to extract useful content from the From_ line
+ */
+char *reduce_from_(char *from_)
+{
+ static char name[HDR_LEN];
+ char *cpl, *cpr;
+
+ for (cpl=from_; *cpl && isspace(*cpl); cpl++);
+ for (cpr=cpl; *cpr && !isspace(*cpr); cpr++);
+
+ if (cpr > cpl)
+ stringcopy(name, cpl,
+ sizeof(name) < cpr-cpl+1?sizeof(name):cpr-cpl+1);
+ return name;
+}
+
int inspect_mbox(char *path, char *prefix, off_t size, int opt_flags)
{
FILE *f;
char tmp[512];
char *cp;
int inheader = 1;
+ int readnewline = 0;
int newmail = 0;
+ int lookahead;
char from_[HDR_LEN] = "";
char from[HDR_LEN] = "";
continue;
if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n') {
buf[strlen(buf)-1] = '\0';
+ if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\r')
+ buf[strlen(buf)-1] = '\0';
+
+ if (inheader && !feof(f)) {
+ lookahead = fgetc(f);
+ if (lookahead == TAB) {
+ if ((cp = fgets(tmp, sizeof(tmp), f)) != NULL) {
+ strncat(buf, tmp, sizeof(buf)-strlen(buf)-1);
+ if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+ buf[strlen(buf)-1] = '\0';
+ if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\r')
+ buf[strlen(buf)-1] = '\0';
+
+ if (strlen(tmp) > 0 && buf[strlen(tmp)-1] == '\n') {
+ /* Read the remainder */
+ while (!feof(f) && fgets(tmp, sizeof(tmp), f) != NULL) {
+ if (strlen(tmp) > 0 && tmp[strlen(tmp)-1] == '\n')
+ break;
+ }
+ }
+ }
+ } else
+ /* Rewind by one character for next read */
+ if (lookahead != EOF)
+ fseek(f, -1, SEEK_CUR);
+ }
} else {
+ /* Read the remainder */
while (!feof(f) && fgets(tmp, sizeof(tmp), f) != NULL) {
if (strlen(tmp) > 0 && tmp[strlen(tmp)-1] == '\n')
break;
if (strlen(from))
stringcopy(realfrom, realname(from), sizeof(realfrom));
-#ifdef todo
else
stringcopy(realfrom, reduce_from_(from_), sizeof(realfrom));
-#endif
emit(prefix, realfrom, subject, priority, opt_flags);
else if (strncasecmp(buf, "X-Priority: 1", 13) == 0 && buf[13] == '\0')
priority = 1;
}
- } else if (strncasecmp(buf, "From ", 5) == 0) {
+ } else if (strlen(buf) == 0) {
+ readnewline = 1;
+ } else if (readnewline && strncasecmp(buf, "From ", 5) == 0) {
inheader = 1;
+ readnewline = 0;
stringcopy(from_, buf+5, sizeof(from_));
- }
+ } else
+ readnewline = 0;
}
+ fclose(f);
+
return newmail;
}
int watch_mbox(char *path, char *prefix, off_t *size, int opt_flags)
{
struct stat st;
+ struct utimbuf timbuf;
int newmail = 0;
if (stat(path, &st) == 0) {
if (st.st_size > *size)
if (access(path, R_OK) == 0) {
+ timbuf.actime = st.st_atime;
+ timbuf.modtime = st.st_mtime;
+
newmail = inspect_mbox(path, prefix, *size, opt_flags);
+
+ utime(path, &timbuf);
}
*size = st.st_size;
return newmail;