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> |
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" |
22 |
< |
|
23 |
< |
#define INDENT "s/^/ /" |
24 |
< |
#define PERIOD "s/[^.]$/&./" |
20 |
> |
#define SPAMC "/usr/local/bin/spamc" |
21 |
> |
#define SPAMD "/var/run/spamd/spamd.sock" |
22 |
|
|
23 |
|
#define TEMP "/tmp/SpamUpdate.XXXXXX" |
24 |
|
|
25 |
|
static jmp_buf environment; |
26 |
|
static char *error = NULL; |
27 |
|
static bool debug = false; |
28 |
+ |
static sigset_t set; |
29 |
|
static MAILSTREAM *streams[2]; |
30 |
+ |
static uint8_t critical = 0; |
31 |
|
static unsigned long *messages[] = { NULL, NULL }; |
32 |
|
static size_t counts[] = { 0, 0 }; |
33 |
|
|
34 |
< |
/*static*/ int check(int value, jmp_buf environment) |
34 |
> |
static int check(int value, jmp_buf environment) |
35 |
|
{ |
36 |
|
if (value == -1) |
37 |
|
longjmp(environment, 1); |
39 |
|
return value; |
40 |
|
} |
41 |
|
|
43 |
– |
/*static*/ char *fcheck(char *value, FILE *stream, jmp_buf environment) |
44 |
– |
{ |
45 |
– |
if (!value) |
46 |
– |
{ |
47 |
– |
if (ferror(stream)) |
48 |
– |
longjmp(environment, 1); |
49 |
– |
else |
50 |
– |
{ |
51 |
– |
clearerr(stream); |
52 |
– |
|
53 |
– |
if (stream == stdin) |
54 |
– |
printf("\n"); |
55 |
– |
|
56 |
– |
return ""; |
57 |
– |
} |
58 |
– |
} |
59 |
– |
|
60 |
– |
return value; |
61 |
– |
} |
62 |
– |
|
42 |
|
static void *mcheck(void *value, jmp_buf environment) |
43 |
|
{ |
44 |
|
if (value == NIL) |
47 |
|
return value; |
48 |
|
} |
49 |
|
|
50 |
< |
/*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 |
< |
|
84 |
< |
static void learn(jmp_buf environment) |
50 |
> |
static void learn() |
51 |
|
{ |
52 |
< |
typedef struct { const char *verb, *args, *mailbox; } Job; |
52 |
> |
typedef struct { const char *verb, *learn, *collab, *mailbox; } Job; |
53 |
|
|
54 |
< |
Job jobs[] = { { .verb = "reporting", .args = "-rR", .mailbox = NEGATIVE }, { .verb = "revoking", .args = "-kW", .mailbox = POSITIVE } }; |
54 |
> |
Job jobs[] = { { .verb = "report", .learn = "spam", .collab = "report", .mailbox = NEGATIVE }, { .verb = "revok", .learn = "ham", .collab = "revoke", .mailbox = POSITIVE } }; |
55 |
|
|
56 |
|
for (Job *job = jobs; job != jobs + sizeof (jobs) / sizeof (*jobs); ++job) |
57 |
|
{ |
58 |
< |
printf("\nLearning and %s from the %s mailbox:\n", job->verb, job->mailbox); |
58 |
> |
printf("\nLearning and %sing from the %s mailbox:\n", job->verb, job->mailbox); |
59 |
|
|
60 |
|
SEARCHPGM search = { .undeleted = 1 }; |
61 |
|
|
62 |
|
mail_search_full(streams[job - jobs], NIL, &search, SE_UID); |
63 |
|
|
64 |
< |
unsigned long *messages_ = messages[job - jobs]; |
65 |
< |
size_t count = counts[job - jobs]; |
66 |
< |
char files[count][sizeof (TEMP) / sizeof (char)]; |
64 |
> |
const unsigned long *messages_ = messages[job - jobs]; |
65 |
> |
const size_t count = counts[job - jobs]; |
66 |
> |
size_t learned = 0; |
67 |
|
|
68 |
|
for (size_t index = 0; index != count; ++index) |
69 |
|
{ |
70 |
|
unsigned long size; |
71 |
< |
char *header = mcheck(mail_fetchheader_full(streams[job - jobs], messages_[index], NIL, &size, FT_UID), environment); |
71 |
> |
char *message = mcheck(mail_fetch_message(streams[job - jobs], messages_[index], &size, FT_UID), environment); |
72 |
> |
char file[sizeof (TEMP) / sizeof (char)] = TEMP; |
73 |
> |
int file_ = check(mkstemp(file), environment); |
74 |
|
|
75 |
< |
strcpy(files[index], TEMP); |
75 |
> |
check(write(file_, message, size * sizeof (char)), environment); |
76 |
> |
check(lseek(file_, 0, SEEK_SET), environment); |
77 |
|
|
78 |
< |
int file = check(mkstemp(files[index]), environment); |
78 |
> |
pid_t learn; |
79 |
|
|
80 |
< |
check(write(file, header, size), environment); |
80 |
> |
if (!(learn = check(fork(), environment))) |
81 |
> |
{ |
82 |
> |
check(dup2(file_, 0), environment); |
83 |
> |
check(execl(SPAMC, "spamc", "-L", job->learn, "-U", SPAMD, NULL), environment); |
84 |
> |
} |
85 |
|
|
86 |
< |
char *text = mcheck(mail_fetchtext_full(streams[job - jobs], messages_[index], &size, FT_UID), environment); |
86 |
> |
pid_t collab; |
87 |
|
|
88 |
< |
check(write(file, text, size), environment); |
89 |
< |
check(close(file), environment); |
90 |
< |
} |
88 |
> |
if (!(collab = check(fork(), environment))) |
89 |
> |
{ |
90 |
> |
check(dup2(file_, 0), environment); |
91 |
> |
check(execl(SPAMC, "spamc", "-L", job->collab, "-U", SPAMD, NULL), environment); |
92 |
> |
} |
93 |
|
|
94 |
< |
int pipe_[2]; |
94 |
> |
check(close(file_), environment); |
95 |
> |
check(unlink(file), environment); |
96 |
|
|
97 |
< |
check(pipe(pipe_), environment); |
97 |
> |
int status; |
98 |
|
|
99 |
< |
pid_t assassin; |
99 |
> |
check(waitpid(learn, &status, 0), environment); |
100 |
|
|
101 |
< |
if (!(assassin = check(fork(), environment))) |
102 |
< |
{ |
127 |
< |
check(dup2(pipe_[1], 1), environment); |
128 |
< |
check(close(pipe_[0]), environment); |
101 |
> |
if (WEXITSTATUS(status) == 5) |
102 |
> |
++learned; |
103 |
|
|
104 |
< |
const char *args[count ? count + 3 : 5]; |
105 |
< |
|
132 |
< |
args[0] = "spamassassin"; |
133 |
< |
args[1] = job->args; |
134 |
< |
|
135 |
< |
if (count) |
136 |
< |
for (size_t index = 0; index != count; ++index) |
137 |
< |
args[index + 2] = files[index]; |
138 |
< |
else |
139 |
< |
{ |
140 |
< |
args[2] = "--mbox"; |
141 |
< |
args[3] = "/dev/null"; |
142 |
< |
} |
104 |
> |
check(waitpid(collab, &status, 0), environment); |
105 |
> |
} |
106 |
|
|
107 |
< |
args[count ? count + 2 : 4] = NULL; |
107 |
> |
printf(" %u message(s) learned and %u message(s) %sed.\n", learned, count, job->verb); |
108 |
> |
} |
109 |
> |
} |
110 |
|
|
111 |
< |
check(execv(ASSASSIN, (char *const *)args), environment); |
112 |
< |
} |
111 |
> |
void delete() |
112 |
> |
{ |
113 |
> |
char *mailboxes[] = { NEGATIVE, POSITIVE }; |
114 |
|
|
115 |
< |
check(close(pipe_[1]), environment); |
115 |
> |
for (char **mailbox = mailboxes; mailbox != mailboxes + sizeof (mailboxes) / sizeof (*mailboxes); ++mailbox) |
116 |
> |
{ |
117 |
> |
printf("\nDeleting messages from the %s mailbox:\n", *mailbox); |
118 |
|
|
119 |
< |
pid_t sed; |
119 |
> |
const unsigned long *messages_ = messages[mailbox - mailboxes]; |
120 |
> |
const size_t count = counts[mailbox - mailboxes]; |
121 |
> |
char *sequence = calloc(1, sizeof (char)); |
122 |
> |
size_t size = 1; |
123 |
|
|
124 |
< |
if (!(sed = check(fork(), environment))) |
124 |
> |
for (size_t index = 0; index != count; ++index) |
125 |
|
{ |
126 |
< |
check(dup2(pipe_[0], 0), environment); |
156 |
< |
check(execl(SED, "sed", "-e", INDENT, "-e", PERIOD, NULL), environment); |
157 |
< |
} |
126 |
> |
char *message; |
127 |
|
|
128 |
< |
check(close(pipe_[0]), environment); |
128 |
> |
asprintf(&message, "%lu%s", messages_[index], index + 1 != count ? "," : ""); |
129 |
|
|
130 |
< |
int status; |
130 |
> |
sequence = realloc(sequence, (size += strlen(message)) * sizeof (char)); |
131 |
|
|
132 |
< |
check(waitpid(assassin, &status, 0), environment); |
133 |
< |
check(waitpid(sed, &status, 0), environment); |
132 |
> |
strlcat(sequence, message, size); |
133 |
> |
free(message); |
134 |
> |
} |
135 |
|
|
136 |
< |
for (size_t index = 0; index != count; ++index) |
137 |
< |
check(unlink(files[index]), environment); |
136 |
> |
mail_setflag_full(streams[mailbox - mailboxes], sequence, "\\Deleted", ST_UID | ST_SILENT); |
137 |
> |
free(sequence); |
138 |
> |
printf(" %u message(s) marked deleted.\n", count); |
139 |
|
} |
140 |
|
} |
141 |
|
|
168 |
|
else |
169 |
|
return printf("Usage: %s [-debug]\n", argv[0]), 1; |
170 |
|
|
171 |
< |
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"); |
171 |
> |
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"); |
172 |
> |
|
173 |
> |
check(sigemptyset(&set), environment); |
174 |
> |
check(sigaddset(&set, SIGHUP), environment); |
175 |
> |
check(sigaddset(&set, SIGINT), environment); |
176 |
|
|
177 |
|
# include <c-client/linkage.c> |
178 |
|
|
179 |
|
streams[0] = mcheck(mail_open(NIL, NEGATIVE, debug ? OP_DEBUG : 0), environment); |
180 |
|
streams[1] = mcheck(mail_open(NIL, POSITIVE, debug ? OP_DEBUG : 0), environment); |
181 |
|
|
182 |
< |
learn(environment); |
182 |
> |
learn(); |
183 |
> |
delete(); |
184 |
|
mail_close(streams[0]); |
185 |
|
mail_close(streams[1]); |
186 |
+ |
free(messages[0]); |
187 |
+ |
free(messages[1]); |
188 |
|
|
189 |
|
return 0; |
190 |
|
} |
191 |
|
|
192 |
< |
void mm_flags(MAILSTREAM *stream, unsigned long number) {} |
193 |
< |
void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status) {} |
192 |
> |
void mm_flags(MAILSTREAM *stream, unsigned long number) |
193 |
> |
{ |
194 |
> |
if (debug) |
195 |
> |
fprintf(stderr, "flags: %lu\n", number); |
196 |
> |
} |
197 |
> |
|
198 |
> |
void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status) |
199 |
> |
{ |
200 |
> |
if (debug) |
201 |
> |
{ |
202 |
> |
fprintf(stderr, "status: %s\n", mailbox); |
203 |
> |
|
204 |
> |
if (status->flags & SA_MESSAGES) |
205 |
> |
fprintf(stderr, " messages: %lu\n", status->messages); |
206 |
> |
|
207 |
> |
if (status->flags & SA_RECENT) |
208 |
> |
fprintf(stderr, " recent: %lu\n", status->recent); |
209 |
> |
|
210 |
> |
if (status->flags & SA_UNSEEN) |
211 |
> |
fprintf(stderr, " unseen: %lu\n", status->unseen); |
212 |
> |
|
213 |
> |
if (status->flags & SA_UIDNEXT) |
214 |
> |
fprintf(stderr, " uid next: %lu\n", status->uidnext); |
215 |
> |
|
216 |
> |
if (status->flags & SA_UIDVALIDITY) |
217 |
> |
fprintf(stderr, " uid validity: %lu\n", status->uidvalidity); |
218 |
> |
} |
219 |
> |
} |
220 |
|
|
221 |
|
void mm_searched(MAILSTREAM *stream, unsigned long number) |
222 |
|
{ |
228 |
|
assert(messages[index]); |
229 |
|
|
230 |
|
messages[index][counts[index] - 1] = number; |
231 |
+ |
|
232 |
+ |
break; |
233 |
|
} |
234 |
|
} |
235 |
|
|
236 |
< |
void mm_exists(MAILSTREAM *stream, unsigned long number) {} |
236 |
> |
void mm_exists(MAILSTREAM *stream, unsigned long number) |
237 |
> |
{ |
238 |
> |
if (debug) |
239 |
> |
fprintf(stderr, "exists: %lu\n", number); |
240 |
> |
} |
241 |
> |
|
242 |
|
void mm_expunged(MAILSTREAM *stream, unsigned long number) {} |
243 |
|
void mm_list(MAILSTREAM *stream, int delim, char *name, long attrib) {} |
244 |
|
void mm_lsub(MAILSTREAM *stream, int delim, char *name, long attrib) {} |
245 |
< |
void mm_notify(MAILSTREAM *stream, char *string, long errflg) {} |
245 |
> |
|
246 |
> |
void mm_notify(MAILSTREAM *stream, char *string, long errflg) |
247 |
> |
{ |
248 |
> |
if (debug) |
249 |
> |
fprintf(stderr, "%li: %s\n", errflg, string); |
250 |
> |
} |
251 |
|
|
252 |
|
void mm_log(char *string, long errflg) |
253 |
|
{ |
254 |
+ |
if (debug) |
255 |
+ |
fprintf(stderr, "%li: %s\n", errflg, string); |
256 |
+ |
|
257 |
|
if (errflg == ERROR) |
258 |
|
asprintf(&error, "%s", string); |
259 |
|
} |
267 |
|
|
268 |
|
void mm_critical(MAILSTREAM *stream) |
269 |
|
{ |
270 |
< |
fprintf(stderr, "critical\n"); |
270 |
> |
if (!critical++) |
271 |
> |
check(sigprocmask(SIG_BLOCK, &set, NULL), environment); |
272 |
|
} |
273 |
|
|
274 |
|
void mm_nocritical(MAILSTREAM *stream) |
275 |
|
{ |
276 |
< |
fprintf(stderr, "nocritical\n"); |
276 |
> |
if (!--critical) |
277 |
> |
check(sigprocmask(SIG_UNBLOCK, &set, NULL), environment); |
278 |
|
} |
279 |
|
|
280 |
|
long mm_diskerror(MAILSTREAM *stream, long errcode, long serious) |
281 |
|
{ |
282 |
< |
fprintf(stderr, "diskerror:\n errcode = %li\n serious = %li\n", errcode, serious); |
282 |
> |
fprintf(stderr, "diskerror: %li%s\n", errcode, serious ? " serious" : ""); |
283 |
|
|
284 |
|
return 1; |
285 |
|
} |
286 |
|
|
287 |
|
void mm_fatal(char *string) |
288 |
|
{ |
289 |
< |
fprintf(stderr, "fatal:\n string = %s\n", string); |
289 |
> |
fprintf(stderr, "fatal: %s\n", string); |
290 |
|
} |