17 |
|
#define NEGATIVE "Spam/False Negative" |
18 |
|
#define POSITIVE "Spam/False Positive" |
19 |
|
|
20 |
– |
/*#define SED "/usr/bin/sed" |
21 |
– |
#define ASSASSIN "/usr/local/bin/spamassassin"*/ |
20 |
|
#define SPAMC "/usr/local/bin/spamc" |
21 |
|
#define SPAMD "/var/run/spamd/spamd.sock" |
22 |
|
|
25 |
– |
/*#define INDENT "s/^/ /" |
26 |
– |
#define PERIOD "s/[^.]$/&./"*/ |
27 |
– |
|
23 |
|
#define TEMP "/tmp/SpamUpdate.XXXXXX" |
24 |
|
|
25 |
|
static jmp_buf environment; |
39 |
|
return value; |
40 |
|
} |
41 |
|
|
42 |
< |
/*static char *fcheck(char *value, FILE *stream, jmp_buf environment) |
42 |
> |
static void *mcheck(void *value, jmp_buf environment) |
43 |
> |
{ |
44 |
> |
if (value == NIL) |
45 |
> |
longjmp(environment, 2); |
46 |
> |
|
47 |
> |
return value; |
48 |
> |
} |
49 |
> |
|
50 |
> |
static char *fcheck(char *value, FILE *stream, jmp_buf environment) |
51 |
|
{ |
52 |
|
if (!value) |
53 |
|
{ |
65 |
|
} |
66 |
|
|
67 |
|
return value; |
65 |
– |
}*/ |
66 |
– |
|
67 |
– |
static void *mcheck(void *value, jmp_buf environment) |
68 |
– |
{ |
69 |
– |
if (value == NIL) |
70 |
– |
longjmp(environment, 2); |
71 |
– |
|
72 |
– |
return value; |
68 |
|
} |
69 |
|
|
70 |
|
static void learn() |
83 |
|
|
84 |
|
const unsigned long *messages_ = messages[job - jobs]; |
85 |
|
const size_t count = counts[job - jobs]; |
86 |
< |
size_t learned = 0; |
86 |
> |
size_t learned = 0, collabed = 0; |
87 |
|
|
88 |
|
for (size_t index = 0; index != count; ++index) |
89 |
|
{ |
90 |
|
unsigned long size; |
91 |
< |
char *header = mcheck(mail_fetchheader_full(streams[job - jobs], messages_[index], NIL, &size, FT_UID), environment); |
91 |
> |
char *message = mcheck(mail_fetch_message(streams[job - jobs], messages_[index], &size, FT_UID), environment); |
92 |
|
char file[sizeof (TEMP) / sizeof (char)] = TEMP; |
93 |
|
int file_ = check(mkstemp(file), environment); |
94 |
|
|
95 |
< |
check(write(file_, header, size), environment); |
101 |
< |
|
102 |
< |
char *text = mcheck(mail_fetchtext_full(streams[job - jobs], messages_[index], &size, FT_UID), environment); |
103 |
< |
|
104 |
< |
check(write(file_, text, size), environment); |
95 |
> |
check(write(file_, message, size * sizeof (char)), environment); |
96 |
|
check(lseek(file_, 0, SEEK_SET), environment); |
97 |
|
|
98 |
|
pid_t learn; |
99 |
+ |
int pipe_[2][2]; |
100 |
+ |
|
101 |
+ |
check(pipe(pipe_[0]), environment); |
102 |
|
|
103 |
|
if (!(learn = check(fork(), environment))) |
104 |
|
{ |
105 |
|
check(dup2(file_, 0), environment); |
106 |
+ |
check(dup2(pipe_[0][1], 1), environment); |
107 |
+ |
check(close(pipe_[0][0]), environment); |
108 |
|
check(execl(SPAMC, "spamc", "-L", job->learn, "-U", SPAMD, NULL), environment); |
109 |
|
} |
110 |
|
|
111 |
+ |
check(close(pipe_[0][1]), environment); |
112 |
+ |
|
113 |
|
pid_t collab; |
114 |
|
|
115 |
+ |
check(pipe(pipe_[1]), environment); |
116 |
+ |
|
117 |
|
if (!(collab = check(fork(), environment))) |
118 |
|
{ |
119 |
|
check(dup2(file_, 0), environment); |
120 |
+ |
check(dup2(pipe_[1][1], 1), environment); |
121 |
+ |
check(close(pipe_[1][0]), environment); |
122 |
|
check(execl(SPAMC, "spamc", "-L", job->collab, "-U", SPAMD, NULL), environment); |
123 |
|
} |
124 |
|
|
125 |
|
check(close(file_), environment); |
126 |
|
check(unlink(file), environment); |
127 |
+ |
check(close(pipe_[1][1]), environment); |
128 |
|
|
129 |
< |
int status; |
130 |
< |
|
131 |
< |
check(waitpid(learn, &status, 0), environment); |
129 |
> |
FILE *learn_ = fdopen(pipe_[0][0], "r"); |
130 |
> |
size_t size_; |
131 |
> |
char *line = fcheck(fgetln(learn_, &size_), learn_, environment); |
132 |
|
|
133 |
< |
if (WEXITSTATUS(status) == 5) |
133 |
> |
if (strncmp(line, "Message successfully un/learned\n", size_) == 0) |
134 |
|
++learned; |
135 |
|
|
136 |
< |
check(waitpid(collab, &status, 0), environment); |
134 |
< |
} |
135 |
< |
|
136 |
< |
printf(" %u message(s) learned and %u message(s) %sed.\n", learned, count, job->verb); |
137 |
< |
} |
138 |
< |
} |
139 |
< |
|
140 |
< |
// XXX: this is going to be moved to a deliver daemon that will run more often |
141 |
< |
/*static void deliver() |
142 |
< |
{ |
143 |
< |
printf("\nDelivering messages from the " POSITIVE " mailbox:\n"); |
144 |
< |
|
145 |
< |
const unsigned long *messages_ = messages[1]; |
146 |
< |
const size_t count = counts[1]; |
147 |
< |
|
148 |
< |
streams[0] = mcheck(mail_open(streams[0], "INBOX", (debug ? OP_DEBUG : 0) | OP_SHORTCACHE), environment); |
149 |
< |
|
150 |
< |
for (int index = 0; index != count; ++index) |
151 |
< |
{ |
152 |
< |
int out[2], in[2]; |
153 |
< |
|
154 |
< |
check(pipe(out), environment); |
155 |
< |
check(pipe(in), environment); |
156 |
< |
|
157 |
< |
pid_t assassin; |
158 |
< |
|
159 |
< |
if (!(assassin = check(fork(), environment))) |
160 |
< |
{ |
161 |
< |
check(dup2(out[0], 0), environment); |
162 |
< |
check(close(out[1]), environment); |
163 |
< |
check(dup2(in[1], 1), environment); |
164 |
< |
check(close(in[0]), environment); |
165 |
< |
check(execl(ASSASSIN, "spamassassin", "-d", NULL), environment); |
166 |
< |
} |
167 |
< |
|
168 |
< |
check(close(out[0]), environment); |
169 |
< |
check(close(in[1]), environment); |
136 |
> |
fclose(learn_); |
137 |
|
|
138 |
< |
unsigned long size; |
172 |
< |
char *header = mcheck(mail_fetchheader_full(streams[1], messages_[index], NIL, &size, FT_UID), environment); |
138 |
> |
FILE *collab_ = fdopen(pipe_[1][0], "r"); |
139 |
|
|
140 |
< |
check(write(out[1], header, size), environment); |
140 |
> |
line = fcheck(fgetln(collab_, &size_), collab_, environment); |
141 |
|
|
142 |
< |
char *text = mcheck(mail_fetchtext_full(streams[1], messages_[index], &size, FT_UID), environment); |
142 |
> |
if (strncmp(line, "Message successfully reported/revoked\n", size_) == 0) |
143 |
> |
++collabed; |
144 |
|
|
145 |
< |
check(write(out[1], text, size), environment); |
179 |
< |
check(close(out[1]), environment); |
145 |
> |
fclose(collab_); |
146 |
|
|
147 |
< |
FILE *assassin_ = fdopen(in[0], "r"); |
182 |
< |
size_t size_; |
183 |
< |
bool header_ = true; |
184 |
< |
char *message = NULL; |
185 |
< |
|
186 |
< |
size = 0; |
187 |
< |
|
188 |
< |
do |
189 |
< |
{ |
190 |
< |
char *line = fcheck(fgetln(assassin_, &size_), assassin_, environment); |
191 |
< |
|
192 |
< |
message = message ? realloc(message, (size + size_) * sizeof (char)) : malloc((size + size_) * sizeof (char)); |
193 |
< |
|
194 |
< |
memcpy(message + size, line, size_); |
195 |
< |
|
196 |
< |
size += size_; |
197 |
< |
|
198 |
< |
if (header_) |
199 |
< |
{ |
200 |
< |
if (size_ == 1) |
201 |
< |
{ |
202 |
< |
assert(*line == '\n'); |
203 |
< |
|
204 |
< |
header_ = false; |
205 |
< |
|
206 |
< |
goto crlf; |
207 |
< |
} |
208 |
< |
else if (size_ == 2 && !memcmp(line, "\r\n", 2)) |
209 |
< |
header_ = false; |
210 |
< |
else if (!(size_ > 2 && !memcmp(line + size_ - 2, "\r\n", 2))) |
211 |
< |
{ |
212 |
< |
crlf: assert(message); |
213 |
< |
|
214 |
< |
message = realloc(message, ++size * sizeof (char)); |
147 |
> |
int status; |
148 |
|
|
149 |
< |
memcpy(message + size - 2, "\r\n", 2); |
150 |
< |
} |
218 |
< |
} |
149 |
> |
check(waitpid(learn, &status, 0), environment); |
150 |
> |
check(waitpid(collab, &status, 0), environment); |
151 |
|
} |
220 |
– |
while (size_ != 0); |
221 |
– |
|
222 |
– |
if (fclose(assassin_)) |
223 |
– |
longjmp(environment, 1); |
152 |
|
|
153 |
< |
int status; |
226 |
< |
|
227 |
< |
check(waitpid(assassin, &status, 0), environment); |
228 |
< |
|
229 |
< |
STRING message_; |
230 |
< |
|
231 |
< |
INIT(&message_, mail_string, message, size); |
232 |
< |
mcheck((void *)mail_append_full(streams[0], "INBOX", "\\Seen", NIL, &message_), environment); |
233 |
< |
free(message); |
153 |
> |
printf(" %u message(s) learned and %u message(s) %sed.\n", learned, collabed, job->verb); |
154 |
|
} |
155 |
< |
|
236 |
< |
streams[0] = mcheck(mail_open(streams[0], NEGATIVE, debug ? OP_DEBUG : 0), environment); |
237 |
< |
|
238 |
< |
printf(" %u message(s) delivered.\n", count); |
239 |
< |
}*/ |
155 |
> |
} |
156 |
|
|
157 |
|
void delete() |
158 |
|
{ |
226 |
|
streams[1] = mcheck(mail_open(NIL, POSITIVE, debug ? OP_DEBUG : 0), environment); |
227 |
|
|
228 |
|
learn(); |
313 |
– |
//deliver(); |
229 |
|
delete(); |
230 |
|
mail_close(streams[0]); |
231 |
|
mail_close(streams[1]); |
235 |
|
return 0; |
236 |
|
} |
237 |
|
|
238 |
< |
void mm_flags(MAILSTREAM *stream, unsigned long number) {} |
239 |
< |
void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status) {} |
238 |
> |
void mm_flags(MAILSTREAM *stream, unsigned long number) |
239 |
> |
{ |
240 |
> |
if (debug) |
241 |
> |
fprintf(stderr, "flags: %lu\n", number); |
242 |
> |
} |
243 |
> |
|
244 |
> |
void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status) |
245 |
> |
{ |
246 |
> |
if (debug) |
247 |
> |
{ |
248 |
> |
fprintf(stderr, "status: %s\n", mailbox); |
249 |
> |
|
250 |
> |
if (status->flags & SA_MESSAGES) |
251 |
> |
fprintf(stderr, " messages: %lu\n", status->messages); |
252 |
> |
|
253 |
> |
if (status->flags & SA_RECENT) |
254 |
> |
fprintf(stderr, " recent: %lu\n", status->recent); |
255 |
> |
|
256 |
> |
if (status->flags & SA_UNSEEN) |
257 |
> |
fprintf(stderr, " unseen: %lu\n", status->unseen); |
258 |
> |
|
259 |
> |
if (status->flags & SA_UIDNEXT) |
260 |
> |
fprintf(stderr, " uid next: %lu\n", status->uidnext); |
261 |
> |
|
262 |
> |
if (status->flags & SA_UIDVALIDITY) |
263 |
> |
fprintf(stderr, " uid validity: %lu\n", status->uidvalidity); |
264 |
> |
} |
265 |
> |
} |
266 |
|
|
267 |
|
void mm_searched(MAILSTREAM *stream, unsigned long number) |
268 |
|
{ |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
< |
void mm_exists(MAILSTREAM *stream, unsigned long number) {} |
282 |
> |
void mm_exists(MAILSTREAM *stream, unsigned long number) |
283 |
> |
{ |
284 |
> |
if (debug) |
285 |
> |
fprintf(stderr, "exists: %lu\n", number); |
286 |
> |
} |
287 |
> |
|
288 |
|
void mm_expunged(MAILSTREAM *stream, unsigned long number) {} |
289 |
|
void mm_list(MAILSTREAM *stream, int delim, char *name, long attrib) {} |
290 |
|
void mm_lsub(MAILSTREAM *stream, int delim, char *name, long attrib) {} |
291 |
< |
void mm_notify(MAILSTREAM *stream, char *string, long errflg) {} |
291 |
> |
|
292 |
> |
void mm_notify(MAILSTREAM *stream, char *string, long errflg) |
293 |
> |
{ |
294 |
> |
if (debug) |
295 |
> |
fprintf(stderr, "%li: %s\n", errflg, string); |
296 |
> |
} |
297 |
|
|
298 |
|
void mm_log(char *string, long errflg) |
299 |
|
{ |
300 |
+ |
if (debug) |
301 |
+ |
fprintf(stderr, "%li: %s\n", errflg, string); |
302 |
+ |
|
303 |
|
if (errflg == ERROR) |
304 |
|
asprintf(&error, "%s", string); |
305 |
|
} |
325 |
|
|
326 |
|
long mm_diskerror(MAILSTREAM *stream, long errcode, long serious) |
327 |
|
{ |
328 |
< |
fprintf(stderr, "diskerror:\n errcode = %li\n serious = %li\n", errcode, serious); |
328 |
> |
fprintf(stderr, "diskerror: %li%s\n", errcode, serious ? " serious" : ""); |
329 |
|
|
330 |
|
return 1; |
331 |
|
} |
332 |
|
|
333 |
|
void mm_fatal(char *string) |
334 |
|
{ |
335 |
< |
fprintf(stderr, "fatal:\n string = %s\n", string); |
335 |
> |
fprintf(stderr, "fatal: %s\n", string); |
336 |
|
} |