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>
28 /* #define FROM_DETECTION */
40 * Like strncpy() but with terminated result.
42 char *stringcopy(char *dest, const char *src, size_t n)
44 strncpy(dest, src, n-1);
50 * Strips balanced quotes in the middle of the realname
52 char *strip_quotes(char *name)
54 static char realname[HDR_LEN];
58 if ((cpl = index(name, '"')) != NULL && (cpr = index(cpl+1, '"')) != NULL) {
59 for (cp=name,xp=realname; *cp && xp < realname+sizeof(realname)-1; cp++) {
60 if (cp != cpl && cp != cpr)
70 * Extract the realname from a mail address
72 * From: "Log Kristian Koehntopp" <joey@infodrom.org>
73 * From: "Jérôme" ATHIAS <jerome@athias.fr>
74 * From: frank@kuesterei.ch (=?iso-8859-1?q?Frank_K=FCster?=)
75 * From: root@luonnotar.infodrom.org (Cron Daemon)
76 * From: <JoMaBusch@web.de>
77 * From: root@luonnotar.infodrom.org
79 char *realname(char *from)
81 static char name[HDR_LEN];
86 /* From: REALNAME <login@host.domain> */
87 if ((cpr = index(from, '<')) != NULL && index(from, '>') != NULL) {
88 if (cpr > from) cpr--;
90 /* Strip trailing spaces */
91 while (cpr > from && isspace(*cpr)) cpr--;
93 /* Strip leading spaces */
94 cpl=from;while (*cpl && isspace(*cpl)) cpl++;
96 /* Strip balanced surrounding quotes */
97 if (*cpl == '"' && *cpr == '"') { cpl++;cpr--; }
100 stringcopy(name, cpl,
101 sizeof(name) < cpr-cpl+2?sizeof(name):cpr-cpl+2);
103 if (index(name, '"') != NULL)
104 stringcopy(name, strip_quotes(name), sizeof(name));
106 /* Apparently no realname included */
107 cpl = index(from, '<');
108 cpr = index(from, '>');
109 stringcopy(name, cpl+1,
110 sizeof(name) < cpr-cpl?sizeof(name):cpr-cpl);
113 /* From: login@host.domain (REALNAME) */
114 } else if ((cpl = index(from, '(')) != NULL && (cpr = index(from, ')')) != NULL) {
115 stringcopy(name, cpl+1,
116 sizeof(name) < cpr-cpl?sizeof(name):cpr-cpl);
118 /* From: login@host.domain */
120 /* Strip leading spaces */
121 cpl=from;while (*cpl && isspace(*cpl)) cpl++;
122 for (cpr=cpl; *cpr && !isspace(*cpr); cpr++);
123 stringcopy(name, cpl,
124 sizeof(name) < cpr-cpl+1?sizeof(name):cpr-cpl+1);
130 int inspect_mbox(char *path, char *prefix, off_t size, int opt_flags)
139 char from_[HDR_LEN] = "";
140 char from[HDR_LEN] = "";
141 char realfrom[HDR_LEN] = "";
142 #ifdef FROM_DETECTION
143 char to[HDR_LEN] = "";
145 char subject[HDR_LEN] = "";
148 if ((f = fopen(path, "r")) == NULL)
151 if (size > 0 && fseek(f, size, SEEK_SET) != 0)
155 if ((cp = fgets(buf, sizeof(buf), f)) == NULL)
157 if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n') {
158 buf[strlen(buf)-1] = '\0';
160 while (!feof(f) && fgets(tmp, sizeof(tmp), f) != NULL) {
161 if (strlen(tmp) > 0 && tmp[strlen(tmp)-1] == '\n')
167 if (strlen(buf) == 0) {
172 stringcopy(realfrom, realname(from), sizeof(realfrom));
175 stringcopy(realfrom, reduce_from_(from_), sizeof(realfrom));
178 emit(prefix, realfrom, subject, priority, opt_flags);
180 #ifdef FROM_DETECTION
181 from_[0] = from[0] = to[0] = subject[0] = '\0';
183 from_[0] = from[0] = subject[0] = '\0';
187 if (strncasecmp(buf, "From ", 5) == 0)
188 stringcopy(from_, buf+5, sizeof(from_));
189 else if (strncasecmp(buf, "From: ", 6) == 0)
190 stringcopy(from, buf+6, sizeof(from));
191 #ifdef FROM_DETECTION
192 else if (strncasecmp(buf, "To: ", 4) == 0)
193 stringcopy(to, buf+4, sizeof(to));
195 else if (strncasecmp(buf, "Subject: ", 9) == 0)
196 stringcopy(subject, buf+9, sizeof(subject));
197 else if (strncasecmp(buf, "Priority: urgent", 16) == 0 && buf[16] == '\0')
199 else if (strncasecmp(buf, "X-Priority: 1", 13) == 0 && buf[13] == '\0')
202 } else if (strncasecmp(buf, "From ", 5) == 0) {
204 stringcopy(from_, buf+5, sizeof(from_));
211 int watch_mbox(char *path, char *prefix, off_t *size, int opt_flags)
216 if (stat(path, &st) == 0) {
217 if (st.st_size > *size)
218 if (access(path, R_OK) == 0) {
219 newmail = inspect_mbox(path, prefix, *size, opt_flags);