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 */
41 * Like strncpy() but with terminated result.
43 char *stringcopy(char *dest, const char *src, size_t n)
45 strncpy(dest, src, n-1);
51 * Strips balanced quotes in the middle of the realname
53 char *strip_quotes(char *name)
55 static char realname[HDR_LEN];
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)
71 * Extract the realname from a mail address
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
80 char *realname(char *from)
82 static char name[HDR_LEN];
87 /* From: REALNAME <login@host.domain> */
88 if ((cpr = index(from, '<')) != NULL && index(from, '>') != NULL) {
89 if (cpr > from) cpr--;
91 /* Strip trailing spaces */
92 while (cpr > from && isspace(*cpr)) cpr--;
94 /* Strip leading spaces */
95 cpl=from;while (*cpl && isspace(*cpl)) cpl++;
97 /* Strip balanced surrounding quotes */
98 if (*cpl == '"' && *cpr == '"') { cpl++;cpr--; }
101 stringcopy(name, cpl,
102 sizeof(name) < cpr-cpl+2?sizeof(name):cpr-cpl+2);
104 if (index(name, '"') != NULL)
105 stringcopy(name, strip_quotes(name), sizeof(name));
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);
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);
119 /* From: login@host.domain */
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);
132 * Tries to extract useful content from the From_ line
134 char *reduce_from_(char *from_)
136 static char name[HDR_LEN];
139 for (cpl=from_; *cpl && isspace(*cpl); cpl++);
140 for (cpr=cpl; *cpr && !isspace(*cpr); cpr++);
143 stringcopy(name, cpl,
144 sizeof(name) < cpr-cpl+1?sizeof(name):cpr-cpl+1);
148 int inspect_mbox(char *path, char *prefix, off_t size, int opt_flags)
158 char from_[HDR_LEN] = "";
159 char from[HDR_LEN] = "";
160 char realfrom[HDR_LEN] = "";
161 #ifdef FROM_DETECTION
162 char to[HDR_LEN] = "";
164 char subject[HDR_LEN] = "";
167 if ((f = fopen(path, "r")) == NULL)
170 if (size > 0 && fseek(f, size, SEEK_SET) != 0)
174 if ((cp = fgets(buf, sizeof(buf), f)) == NULL)
176 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n') {
177 buf[strlen(buf)-1] = '\0';
178 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\r')
179 buf[strlen(buf)-1] = '\0';
181 while (!feof(f) && fgets(tmp, sizeof(tmp), f) != NULL) {
182 if (strlen(tmp) > 0 && tmp[strlen(tmp)-1] == '\n')
188 if (strlen(buf) == 0) {
193 stringcopy(realfrom, realname(from), sizeof(realfrom));
195 stringcopy(realfrom, reduce_from_(from_), sizeof(realfrom));
197 emit(prefix, realfrom, subject, priority, opt_flags);
199 #ifdef FROM_DETECTION
200 from_[0] = from[0] = to[0] = subject[0] = '\0';
202 from_[0] = from[0] = subject[0] = '\0';
206 if (strncasecmp(buf, "From ", 5) == 0)
207 stringcopy(from_, buf+5, sizeof(from_));
208 else if (strncasecmp(buf, "From: ", 6) == 0)
209 stringcopy(from, buf+6, sizeof(from));
210 #ifdef FROM_DETECTION
211 else if (strncasecmp(buf, "To: ", 4) == 0)
212 stringcopy(to, buf+4, sizeof(to));
214 else if (strncasecmp(buf, "Subject: ", 9) == 0)
215 stringcopy(subject, buf+9, sizeof(subject));
216 else if (strncasecmp(buf, "Priority: urgent", 16) == 0 && buf[16] == '\0')
218 else if (strncasecmp(buf, "X-Priority: 1", 13) == 0 && buf[13] == '\0')
221 } else if (strlen(buf) == 0) {
223 } else if (readnewline && strncasecmp(buf, "From ", 5) == 0) {
226 stringcopy(from_, buf+5, sizeof(from_));
236 int watch_mbox(char *path, char *prefix, off_t *size, int opt_flags)
239 struct utimbuf timbuf;
242 if (stat(path, &st) == 0) {
243 if (st.st_size > *size)
244 if (access(path, R_OK) == 0) {
245 timbuf.actime = st.st_atime;
246 timbuf.modtime = st.st_mtime;
248 newmail = inspect_mbox(path, prefix, *size, opt_flags);
250 utime(path, &timbuf);