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) |
218 |
|
{ |
219 |
|
if (size_ == 1) |
220 |
|
{ |
221 |
+ |
assert(*line == '\n'); |
222 |
+ |
|
223 |
|
header_ = false; |
224 |
|
|
225 |
|
goto crlf; |
257 |
|
printf(" %u message(s) delivered.\n", count); |
258 |
|
} |
259 |
|
|
260 |
+ |
void delete() |
261 |
+ |
{ |
262 |
+ |
char *mailboxes[] = { NEGATIVE, POSITIVE }; |
263 |
+ |
|
264 |
+ |
for (char **mailbox = mailboxes; mailbox != mailboxes + sizeof (mailboxes) / sizeof (*mailboxes); ++mailbox) |
265 |
+ |
{ |
266 |
+ |
printf("\nDeleting messages from the %s mailbox:\n", *mailbox); |
267 |
+ |
|
268 |
+ |
const unsigned long *messages_ = messages[mailbox - mailboxes]; |
269 |
+ |
const size_t count = counts[mailbox - mailboxes]; |
270 |
+ |
char *sequence = calloc(1, sizeof (char)); |
271 |
+ |
size_t size = 1; |
272 |
+ |
|
273 |
+ |
for (size_t index = 0; index != count; ++index) |
274 |
+ |
{ |
275 |
+ |
char *message; |
276 |
+ |
|
277 |
+ |
asprintf(&message, "%lu%s", messages_[index], index + 1 != count ? "," : ""); |
278 |
+ |
|
279 |
+ |
sequence = realloc(sequence, (size += strlen(message)) * sizeof (char)); |
280 |
+ |
|
281 |
+ |
strlcat(sequence, message, size); |
282 |
+ |
free(message); |
283 |
+ |
} |
284 |
+ |
|
285 |
+ |
mail_setflag_full(streams[mailbox - mailboxes], sequence, "\\Deleted", ST_UID | ST_SILENT); |
286 |
+ |
free(sequence); |
287 |
+ |
printf(" %u message(s) marked deleted.\n", count); |
288 |
+ |
} |
289 |
+ |
} |
290 |
+ |
|
291 |
|
int main(int argc, char *argv[]) |
292 |
|
{ |
293 |
|
int exception; |
317 |
|
else |
318 |
|
return printf("Usage: %s [-debug]\n", argv[0]), 1; |
319 |
|
|
320 |
< |
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"); |
320 |
> |
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"); |
321 |
> |
|
322 |
> |
check(sigemptyset(&set), environment); |
323 |
> |
check(sigaddset(&set, SIGHUP), environment); |
324 |
> |
check(sigaddset(&set, SIGINT), environment); |
325 |
|
|
326 |
|
# include <c-client/linkage.c> |
327 |
|
|
330 |
|
|
331 |
|
learn(); |
332 |
|
deliver(); |
333 |
+ |
delete(); |
334 |
|
mail_close(streams[0]); |
335 |
|
mail_close(streams[1]); |
336 |
|
free(messages[0]); |
352 |
|
assert(messages[index]); |
353 |
|
|
354 |
|
messages[index][counts[index] - 1] = number; |
355 |
+ |
|
356 |
+ |
break; |
357 |
|
} |
358 |
|
} |
359 |
|
|
378 |
|
|
379 |
|
void mm_critical(MAILSTREAM *stream) |
380 |
|
{ |
381 |
< |
fprintf(stderr, "critical\n"); |
381 |
> |
if (!critical++) |
382 |
> |
check(sigprocmask(SIG_BLOCK, &set, NULL), environment); |
383 |
|
} |
384 |
|
|
385 |
|
void mm_nocritical(MAILSTREAM *stream) |
386 |
|
{ |
387 |
< |
fprintf(stderr, "nocritical\n"); |
387 |
> |
if (!--critical) |
388 |
> |
check(sigprocmask(SIG_UNBLOCK, &set, NULL), environment); |
389 |
|
} |
390 |
|
|
391 |
|
long mm_diskerror(MAILSTREAM *stream, long errcode, long serious) |