Err... should close the mbox
[infodrom/newmail] / mbox.c
1 /*
2     Copyright (c) 2004  Joey Schulze <joey@infodrom.org>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <utime.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <ctype.h>
27 #include "output.h"
28
29 /* #define FROM_DETECTION */
30
31 struct mail {
32   char from[128];
33   char subject[128];
34   int priority;
35   off_t size;
36 };
37
38 #define HDR_LEN 128
39
40 /*
41  * Like strncpy() but with terminated result.
42  */
43 char *stringcopy(char *dest, const char *src, size_t n)
44 {
45   strncpy(dest, src, n-1);
46   dest[n-1] = '\0';
47   return dest;
48 }
49
50 /*
51  * Strips balanced quotes in the middle of the realname
52  */
53 char *strip_quotes(char *name)
54 {
55   static char realname[HDR_LEN];
56   char *cpl, *cpr;
57   char *cp, *xp;
58
59   if ((cpl = index(name, '"')) != NULL && (cpr = index(cpl+1, '"')) != NULL) {
60     for (cp=name,xp=realname; *cp && xp < realname+sizeof(realname)-1; cp++) {
61       if (cp != cpl && cp != cpr)
62         *xp++ = *cp;
63     }
64     *xp = '\0';
65     return realname;
66   } else
67     return name;
68 }
69
70 /*
71  * Extract the realname from a mail address
72  *
73  * From: "Log Kristian Koehntopp" <joey@infodrom.org>
74  * From: "Jérôme" ATHIAS <jerome@athias.fr>
75  * From: frank@kuesterei.ch (=?iso-8859-1?q?Frank_K=FCster?=)
76  * From: root@luonnotar.infodrom.org (Cron Daemon)
77  * From: <JoMaBusch@web.de>
78  * From: root@luonnotar.infodrom.org
79 */
80 char *realname(char *from)
81 {
82   static char name[HDR_LEN];
83   char *cpl, *cpr;
84
85   name[0] = '\0';
86
87   /* From: REALNAME <login@host.domain> */
88   if ((cpr = index(from, '<')) != NULL && index(from, '>') != NULL) {
89     if (cpr > from) cpr--;
90
91     /* Strip trailing spaces */
92     while (cpr > from && isspace(*cpr)) cpr--;
93
94     /* Strip leading spaces */
95     cpl=from;while (*cpl && isspace(*cpl)) cpl++;
96
97     /* Strip balanced surrounding quotes */
98     if (*cpl == '"' && *cpr == '"') { cpl++;cpr--; }
99
100     if (cpr > cpl) {
101       stringcopy(name, cpl,
102                  sizeof(name) < cpr-cpl+2?sizeof(name):cpr-cpl+2);
103
104       if (index(name, '"') != NULL)
105         stringcopy(name, strip_quotes(name), sizeof(name));
106     } else {
107       /* Apparently no realname included */
108       cpl = index(from, '<');
109       cpr = index(from, '>');
110       stringcopy(name, cpl+1,
111                  sizeof(name) < cpr-cpl?sizeof(name):cpr-cpl);
112     }
113
114   /* From: login@host.domain (REALNAME) */
115   } else if ((cpl = index(from, '(')) != NULL && (cpr = index(from, ')')) != NULL) {
116     stringcopy(name, cpl+1,
117                sizeof(name) < cpr-cpl?sizeof(name):cpr-cpl);
118
119   /* From: login@host.domain */
120   } else {
121     /* Strip leading spaces */
122     cpl=from;while (*cpl && isspace(*cpl)) cpl++;
123     for (cpr=cpl; *cpr && !isspace(*cpr); cpr++);
124     stringcopy(name, cpl,
125                sizeof(name) < cpr-cpl+1?sizeof(name):cpr-cpl+1);
126   }
127
128   return name;
129 }
130
131 int inspect_mbox(char *path, char *prefix, off_t size, int opt_flags)
132 {
133   FILE *f;
134   char buf[HDR_LEN];
135   char tmp[512];
136   char *cp;
137   int inheader = 1;
138   int newmail = 0;
139
140   char from_[HDR_LEN] = "";
141   char from[HDR_LEN] = "";
142   char realfrom[HDR_LEN] = "";
143 #ifdef FROM_DETECTION
144   char to[HDR_LEN] = "";
145 #endif
146   char subject[HDR_LEN] = "";
147   int priority = 0;
148
149   if ((f = fopen(path, "r")) == NULL)
150     return 0;
151
152   if (size > 0 && fseek(f, size, SEEK_SET) != 0)
153     return 0;
154
155   while (!feof(f)) {
156     if ((cp = fgets(buf, sizeof(buf), f)) == NULL)
157       continue;
158     if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n') {
159       buf[strlen(buf)-1] = '\0';
160     } else {
161       while (!feof(f) && fgets(tmp, sizeof(tmp), f) != NULL) {
162         if (strlen(tmp) > 0 && tmp[strlen(tmp)-1] == '\n')
163           break;
164       }
165     }
166
167     if (inheader) {
168       if (strlen(buf) == 0) {
169         inheader = 0;
170         newmail = 1;
171
172         if (strlen(from))
173           stringcopy(realfrom, realname(from), sizeof(realfrom));
174 #ifdef todo
175         else
176           stringcopy(realfrom, reduce_from_(from_), sizeof(realfrom));
177 #endif
178
179         emit(prefix, realfrom, subject, priority, opt_flags);
180
181 #ifdef FROM_DETECTION
182         from_[0] = from[0] = to[0] = subject[0] = '\0';
183 #else
184         from_[0] = from[0] = subject[0] = '\0';
185 #endif
186         priority = 0;
187       } else {
188         if (strncasecmp(buf, "From ", 5) == 0)
189           stringcopy(from_, buf+5, sizeof(from_));
190         else if (strncasecmp(buf, "From: ", 6) == 0)
191           stringcopy(from, buf+6, sizeof(from));
192 #ifdef FROM_DETECTION
193         else if (strncasecmp(buf, "To: ", 4) == 0)
194           stringcopy(to, buf+4, sizeof(to));
195 #endif
196         else if (strncasecmp(buf, "Subject: ", 9) == 0)
197           stringcopy(subject, buf+9, sizeof(subject));
198         else if (strncasecmp(buf, "Priority: urgent", 16) == 0 && buf[16] == '\0')
199           priority = 1;
200         else if (strncasecmp(buf, "X-Priority: 1", 13) == 0 && buf[13] == '\0')
201           priority = 1;
202       }
203     } else if (strncasecmp(buf, "From ", 5) == 0) {
204       inheader = 1;
205       stringcopy(from_, buf+5, sizeof(from_));
206     }
207   }
208
209   fclose(f);
210
211   return newmail;
212 }
213
214 int watch_mbox(char *path, char *prefix, off_t *size, int opt_flags)
215 {
216   struct stat st;
217   struct utimbuf timbuf;
218   int newmail = 0;
219
220   if (stat(path, &st) == 0) {
221     if (st.st_size > *size)
222       if (access(path, R_OK) == 0) {
223         timbuf.actime = st.st_atime;
224         timbuf.modtime = st.st_mtime;
225
226         newmail = inspect_mbox(path, prefix, *size, opt_flags);
227
228         utime(path, &timbuf);
229       }
230     *size = st.st_size;
231     return newmail;
232   } else {
233     *size = 0;
234   }
235
236   return 0;
237 }