2 Copyright (c) 2004 Joey Schulze <joey@infodrom.org>
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.
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.
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.
20 #include <sys/types.h>
29 /* #define FROM_DETECTION */
42 * Like strncpy() but with terminated result.
44 char *stringcopy(char *dest, const char *src, size_t n)
46 strncpy (dest, src, n-1);
52 * Strips balanced quotes in the middle of the realname
54 char *strip_quotes(char *name)
56 static char realname[HDR_LEN];
60 if ((cpl = index(name, '"')) != NULL && (cpr = index(cpl+1, '"')) != NULL) {
61 for (cp=name,xp=realname; *cp && xp < realname+sizeof(realname)-1; cp++) {
62 if (cp != cpl && cp != cpr)
72 * Extract the realname from a mail address
74 * From: "Log Kristian Koehntopp" <joey@infodrom.org>
75 * From: "Jérôme" ATHIAS <jerome@athias.fr>
76 * From: frank@kuesterei.ch (=?iso-8859-1?q?Frank_K=FCster?=)
77 * From: root@luonnotar.infodrom.org (Cron Daemon)
78 * From: <JoMaBusch@web.de>
79 * From: root@luonnotar.infodrom.org
81 char *realname(char *from)
83 static char name[HDR_LEN];
88 /* From: REALNAME <login@host.domain> */
89 if ((cpr = index(from, '<')) != NULL && index(from, '>') != NULL) {
90 if (cpr > from) cpr--;
92 /* Strip trailing spaces */
93 while (cpr > from && isspace(*cpr)) cpr--;
95 /* Strip leading spaces */
96 cpl=from;while (*cpl && isspace(*cpl)) cpl++;
98 /* Strip balanced surrounding quotes */
99 if (*cpl == '"' && *cpr == '"') { cpl++;cpr--; }
102 stringcopy (name, cpl,
103 sizeof(name) < cpr-cpl+2?sizeof(name):cpr-cpl+2);
105 if (index(name, '"') != NULL)
106 stringcopy (name, strip_quotes(name), sizeof(name));
108 /* Apparently no realname included */
109 cpl = index(from, '<');
110 cpr = index(from, '>');
111 stringcopy (name, cpl+1,
112 sizeof(name) < cpr-cpl?sizeof(name):cpr-cpl);
115 /* From: login@host.domain (REALNAME) */
116 } else if ((cpl = index(from, '(')) != NULL && (cpr = index(from, ')')) != NULL) {
117 stringcopy (name, cpl+1,
118 sizeof(name) < cpr-cpl?sizeof(name):cpr-cpl);
120 /* From: login@host.domain */
122 /* Strip leading spaces */
123 cpl=from;while (*cpl && isspace(*cpl)) cpl++;
124 for (cpr=cpl; *cpr && !isspace(*cpr); cpr++);
125 stringcopy (name, cpl,
126 sizeof(name) < cpr-cpl+1?sizeof(name):cpr-cpl+1);
133 * Tries to extract useful content from the From_ line
135 char *reduce_from_(char *from_)
137 static char name[HDR_LEN];
140 for (cpl=from_; *cpl && isspace(*cpl); cpl++);
141 for (cpr=cpl; *cpr && !isspace(*cpr); cpr++);
144 stringcopy (name, cpl,
145 sizeof(name) < cpr-cpl+1?sizeof(name):cpr-cpl+1);
149 int inspect_mbox(char *path, char *prefix, off_t size, int opt_flags)
160 char from_[HDR_LEN] = "";
161 char from[HDR_LEN] = "";
162 char realfrom[HDR_LEN] = "";
163 #ifdef FROM_DETECTION
164 char to[HDR_LEN] = "";
166 char subject[HDR_LEN] = "";
169 if ((f = fopen(path, "r")) == NULL)
172 if (size > 0 && fseek(f, size, SEEK_SET) != 0)
176 if ((cp = fgets(buf, sizeof(buf), f)) == NULL)
178 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n') {
179 buf[strlen(buf)-1] = '\0';
180 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\r')
181 buf[strlen(buf)-1] = '\0';
183 if (inheader && !feof(f)) {
184 lookahead = fgetc(f);
185 if (lookahead == TAB) {
186 if ((cp = fgets(tmp, sizeof(tmp), f)) != NULL) {
187 strncat (buf, tmp, sizeof(buf)-strlen(buf)-1);
188 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
189 buf[strlen(buf)-1] = '\0';
190 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\r')
191 buf[strlen(buf)-1] = '\0';
193 if (strlen(tmp) > 0 && buf[strlen(tmp)-1] == '\n') {
194 /* Read the remainder */
195 while (!feof(f) && fgets(tmp, sizeof(tmp), f) != NULL) {
196 if (strlen(tmp) > 0 && tmp[strlen(tmp)-1] == '\n')
202 /* Rewind by one character for next read */
203 if (lookahead != EOF)
204 fseek(f, -1, SEEK_CUR);
207 /* Read the remainder */
208 while (!feof(f) && fgets(tmp, sizeof(tmp), f) != NULL) {
209 if (strlen(tmp) > 0 && tmp[strlen(tmp)-1] == '\n')
215 if (strlen(buf) == 0) {
220 stringcopy (realfrom, realname(from), sizeof(realfrom));
222 stringcopy (realfrom, reduce_from_(from_), sizeof(realfrom));
224 emit (prefix, realfrom, subject, priority, opt_flags);
226 #ifdef FROM_DETECTION
227 from_[0] = from[0] = to[0] = subject[0] = '\0';
229 from_[0] = from[0] = subject[0] = '\0';
233 if (strncasecmp(buf, "From ", 5) == 0)
234 stringcopy (from_, buf+5, sizeof(from_));
235 else if (strncasecmp(buf, "From: ", 6) == 0)
236 stringcopy (from, buf+6, sizeof(from));
237 #ifdef FROM_DETECTION
238 else if (strncasecmp(buf, "To: ", 4) == 0)
239 stringcopy (to, buf+4, sizeof(to));
241 else if (strncasecmp(buf, "Subject: ", 9) == 0)
242 stringcopy (subject, buf+9, sizeof(subject));
243 else if (strncasecmp(buf, "Priority: urgent", 16) == 0 && buf[16] == '\0')
245 else if (strncasecmp(buf, "X-Priority: 1", 13) == 0 && buf[13] == '\0')
248 } else if (strlen(buf) == 0) {
250 } else if (readnewline && strncasecmp(buf, "From ", 5) == 0) {
253 stringcopy (from_, buf+5, sizeof(from_));
263 int watch_mbox(char *path, char *prefix, off_t *size, int opt_flags)
266 struct utimbuf timbuf;
269 if (stat(path, &st) == 0) {
270 if (st.st_size > *size)
271 if (access(path, R_OK) == 0) {
272 timbuf.actime = st.st_atime;
273 timbuf.modtime = st.st_mtime;
275 newmail = inspect_mbox(path, prefix, *size, opt_flags);
277 utime(path, &timbuf);