6 |
|
|
7 |
|
#include <assert.h> |
8 |
|
#include <sys/types.h> |
9 |
– |
#include <regex.h> |
9 |
|
#include <setjmp.h> |
10 |
+ |
#include <signal.h> |
11 |
|
#include <stdbool.h> |
12 |
|
#include <stdio.h> |
13 |
|
#include <sys/wait.h> |
28 |
|
static jmp_buf environment; |
29 |
|
static char *error = NULL; |
30 |
|
static bool debug = false; |
31 |
+ |
static sigset_t set; |
32 |
|
static MAILSTREAM *streams[2]; |
33 |
+ |
static uint8_t critical = 0; |
34 |
|
static unsigned long *messages[] = { NULL, NULL }; |
35 |
|
static size_t counts[] = { 0, 0 }; |
36 |
|
|
70 |
|
return value; |
71 |
|
} |
72 |
|
|
71 |
– |
/*static*/ int regcheck(int value, const regex_t *regex, jmp_buf environment) |
72 |
– |
{ |
73 |
– |
if (value && value != REG_NOMATCH) |
74 |
– |
{ |
75 |
– |
char exception[regerror(value, regex, NULL, 0)]; |
76 |
– |
|
77 |
– |
regerror(value, regex, exception, sizeof (exception)); |
78 |
– |
longjmp(environment, (int)exception); |
79 |
– |
} |
80 |
– |
|
81 |
– |
return value; |
82 |
– |
} |
83 |
– |
|
73 |
|
static void learn() |
74 |
|
{ |
75 |
|
typedef struct { const char *verb, *args, *mailbox; } Job; |
84 |
|
|
85 |
|
mail_search_full(streams[job - jobs], NIL, &search, SE_UID); |
86 |
|
|
87 |
< |
/*unsigned long *messages_ = messages[job - jobs]; |
88 |
< |
size_t count = counts[job - jobs]; |
87 |
> |
const unsigned long *messages_ = messages[job - jobs]; |
88 |
> |
const size_t count = counts[job - jobs]; |
89 |
|
char files[count][sizeof (TEMP) / sizeof (char)]; |
90 |
|
|
91 |
|
for (size_t index = 0; index != count; ++index) |
153 |
|
check(waitpid(sed, &status, 0), environment); |
154 |
|
|
155 |
|
for (size_t index = 0; index != count; ++index) |
156 |
< |
check(unlink(files[index]), environment);*/ |
156 |
> |
check(unlink(files[index]), environment); |
157 |
|
} |
158 |
|
} |
159 |
|
|
161 |
|
{ |
162 |
|
printf("\nDelivering messages from the " POSITIVE " mailbox:\n"); |
163 |
|
|
164 |
< |
unsigned long *messages_ = messages[1]; |
165 |
< |
size_t count = counts[1]; |
164 |
> |
const unsigned long *messages_ = messages[1]; |
165 |
> |
const size_t count = counts[1]; |
166 |
> |
|
167 |
|
streams[0] = mcheck(mail_open(streams[0], "INBOX", (debug ? OP_DEBUG : 0) | OP_SHORTCACHE), environment); |
168 |
|
|
169 |
|
for (int index = 0; index != count; ++index) |
255 |
|
printf(" %u message(s) delivered.\n", count); |
256 |
|
} |
257 |
|
|
258 |
+ |
void delete() |
259 |
+ |
{ |
260 |
+ |
char *mailboxes[] = { NEGATIVE, POSITIVE }; |
261 |
+ |
|
262 |
+ |
for (char **mailbox = mailboxes; mailbox != mailboxes + sizeof (mailboxes) / sizeof (*mailboxes); ++mailbox) |
263 |
+ |
{ |
264 |
+ |
printf("\nDeleting messages from the %s mailbox:\n", *mailbox); |
265 |
+ |
|
266 |
+ |
const unsigned long *messages_ = messages[mailbox - mailboxes]; |
267 |
+ |
const size_t count = counts[mailbox - mailboxes]; |
268 |
+ |
char *sequence = calloc(1, sizeof (char)); |
269 |
+ |
size_t size = 1; |
270 |
+ |
|
271 |
+ |
for (size_t index = 0; index != count; ++index) |
272 |
+ |
{ |
273 |
+ |
char *message; |
274 |
+ |
|
275 |
+ |
asprintf(&message, "%lu%s", messages_[index], index + 1 != count ? "," : ""); |
276 |
+ |
|
277 |
+ |
sequence = realloc(sequence, (size += strlen(message)) * sizeof (char)); |
278 |
+ |
|
279 |
+ |
strlcat(sequence, message, size); |
280 |
+ |
free(message); |
281 |
+ |
} |
282 |
+ |
|
283 |
+ |
mail_setflag_full(streams[mailbox - mailboxes], sequence, "\\Deleted", ST_UID | ST_SILENT); |
284 |
+ |
free(sequence); |
285 |
+ |
printf(" %u message(s) marked deleted.\n", count); |
286 |
+ |
} |
287 |
+ |
} |
288 |
+ |
|
289 |
|
int main(int argc, char *argv[]) |
290 |
|
{ |
291 |
|
int exception; |
315 |
|
else |
316 |
|
return printf("Usage: %s [-debug]\n", argv[0]), 1; |
317 |
|
|
318 |
< |
printf("Information:\n If you receive Spam in your Inbox mailbox, copy it to the " NEGATIVE " mailbox.\n\n If you receive mail that is not Spam in your Assassin mailbox, copy it to the " POSITIVE " mailbox.\n"); |
318 |
> |
printf("Information:\n If you receive spam in your Inbox mailbox, copy it to the " NEGATIVE " mailbox.\n\n If you receive mail that is not spam in your Spam/Assassin mailbox, copy it to the " POSITIVE " mailbox.\n"); |
319 |
> |
|
320 |
> |
check(sigemptyset(&set), environment); |
321 |
> |
check(sigaddset(&set, SIGHUP), environment); |
322 |
> |
check(sigaddset(&set, SIGINT), environment); |
323 |
|
|
324 |
|
# include <c-client/linkage.c> |
325 |
|
|
328 |
|
|
329 |
|
learn(); |
330 |
|
deliver(); |
331 |
+ |
delete(); |
332 |
|
mail_close(streams[0]); |
333 |
|
mail_close(streams[1]); |
334 |
|
free(messages[0]); |
350 |
|
assert(messages[index]); |
351 |
|
|
352 |
|
messages[index][counts[index] - 1] = number; |
353 |
+ |
|
354 |
+ |
break; |
355 |
|
} |
356 |
|
} |
357 |
|
|
376 |
|
|
377 |
|
void mm_critical(MAILSTREAM *stream) |
378 |
|
{ |
379 |
< |
fprintf(stderr, "critical\n"); |
379 |
> |
if (!critical++) |
380 |
> |
check(sigprocmask(SIG_BLOCK, &set, NULL), environment); |
381 |
|
} |
382 |
|
|
383 |
|
void mm_nocritical(MAILSTREAM *stream) |
384 |
|
{ |
385 |
< |
fprintf(stderr, "nocritical\n"); |
385 |
> |
if (!--critical) |
386 |
> |
check(sigprocmask(SIG_UNBLOCK, &set, NULL), environment); |
387 |
|
} |
388 |
|
|
389 |
|
long mm_diskerror(MAILSTREAM *stream, long errcode, long serious) |