14 |
|
|
15 |
|
#include <c-client/c-client.h> |
16 |
|
|
17 |
< |
#define NEGATIVE "Spam/False Negative" |
18 |
< |
#define POSITIVE "Spam/False Positive" |
17 |
> |
#define NEGATIVE "Spam.False Negative" |
18 |
> |
#define POSITIVE "Spam.False Positive" |
19 |
> |
|
20 |
> |
#define SERVER "{mail.douglasthrift.net/tls/novalidate-cert}" |
21 |
|
|
22 |
|
#define SPAMC "/usr/local/bin/spamc" |
23 |
|
#define SPAMD "/var/run/spamd/spamd.sock" |
32 |
|
static uint8_t critical = 0; |
33 |
|
static unsigned long *messages[] = { NULL, NULL }; |
34 |
|
static size_t counts[] = { 0, 0 }; |
35 |
+ |
static char *pass; |
36 |
|
|
37 |
|
static int check(int value, jmp_buf environment) |
38 |
|
{ |
50 |
|
return value; |
51 |
|
} |
52 |
|
|
53 |
+ |
static char *fcheck(char *value, FILE *stream, jmp_buf environment) |
54 |
+ |
{ |
55 |
+ |
if (!value) |
56 |
+ |
{ |
57 |
+ |
if (ferror(stream)) |
58 |
+ |
longjmp(environment, 1); |
59 |
+ |
else |
60 |
+ |
{ |
61 |
+ |
clearerr(stream); |
62 |
+ |
|
63 |
+ |
if (stream == stdin) |
64 |
+ |
printf("\n"); |
65 |
+ |
|
66 |
+ |
return ""; |
67 |
+ |
} |
68 |
+ |
} |
69 |
+ |
|
70 |
+ |
return value; |
71 |
+ |
} |
72 |
+ |
|
73 |
|
static void learn() |
74 |
|
{ |
75 |
|
typedef struct { const char *verb, *learn, *collab, *mailbox; } Job; |
76 |
< |
|
76 |
> |
|
77 |
|
Job jobs[] = { { .verb = "report", .learn = "spam", .collab = "report", .mailbox = NEGATIVE }, { .verb = "revok", .learn = "ham", .collab = "revoke", .mailbox = POSITIVE } }; |
78 |
|
|
79 |
|
for (Job *job = jobs; job != jobs + sizeof (jobs) / sizeof (*jobs); ++job) |
81 |
|
printf("\nLearning and %sing from the %s mailbox:\n", job->verb, job->mailbox); |
82 |
|
|
83 |
|
SEARCHPGM search = { .undeleted = 1 }; |
84 |
< |
|
84 |
> |
|
85 |
|
mail_search_full(streams[job - jobs], NIL, &search, SE_UID); |
86 |
|
|
87 |
|
const unsigned long *messages_ = messages[job - jobs]; |
88 |
|
const size_t count = counts[job - jobs]; |
89 |
< |
size_t learned = 0; |
89 |
> |
size_t learned = 0, collabed = 0; |
90 |
|
|
91 |
|
for (size_t index = 0; index != count; ++index) |
92 |
|
{ |
99 |
|
check(lseek(file_, 0, SEEK_SET), environment); |
100 |
|
|
101 |
|
pid_t learn; |
102 |
+ |
int pipe_[2][2]; |
103 |
+ |
|
104 |
+ |
check(pipe(pipe_[0]), environment); |
105 |
|
|
106 |
|
if (!(learn = check(fork(), environment))) |
107 |
|
{ |
108 |
|
check(dup2(file_, 0), environment); |
109 |
+ |
check(dup2(pipe_[0][1], 1), environment); |
110 |
+ |
check(close(pipe_[0][0]), environment); |
111 |
|
check(execl(SPAMC, "spamc", "-L", job->learn, "-U", SPAMD, NULL), environment); |
112 |
|
} |
113 |
|
|
114 |
+ |
check(close(pipe_[0][1]), environment); |
115 |
+ |
|
116 |
|
pid_t collab; |
117 |
|
|
118 |
+ |
check(pipe(pipe_[1]), environment); |
119 |
+ |
|
120 |
|
if (!(collab = check(fork(), environment))) |
121 |
|
{ |
122 |
|
check(dup2(file_, 0), environment); |
123 |
+ |
check(dup2(pipe_[1][1], 1), environment); |
124 |
+ |
check(close(pipe_[1][0]), environment); |
125 |
|
check(execl(SPAMC, "spamc", "-L", job->collab, "-U", SPAMD, NULL), environment); |
126 |
|
} |
127 |
|
|
128 |
|
check(close(file_), environment); |
129 |
|
check(unlink(file), environment); |
130 |
+ |
check(close(pipe_[1][1]), environment); |
131 |
|
|
132 |
< |
int status; |
133 |
< |
|
134 |
< |
check(waitpid(learn, &status, 0), environment); |
132 |
> |
FILE *learn_ = fdopen(pipe_[0][0], "r"); |
133 |
> |
size_t size_; |
134 |
> |
char *line = fcheck(fgetln(learn_, &size_), learn_, environment); |
135 |
|
|
136 |
< |
if (WEXITSTATUS(status) == 5) |
136 |
> |
if (strncmp(line, "Message successfully un/learned\n", size_) == 0) |
137 |
|
++learned; |
138 |
|
|
139 |
+ |
fclose(learn_); |
140 |
+ |
|
141 |
+ |
FILE *collab_ = fdopen(pipe_[1][0], "r"); |
142 |
+ |
|
143 |
+ |
line = fcheck(fgetln(collab_, &size_), collab_, environment); |
144 |
+ |
|
145 |
+ |
if (strncmp(line, "Message successfully reported/revoked\n", size_) == 0) |
146 |
+ |
++collabed; |
147 |
+ |
|
148 |
+ |
fclose(collab_); |
149 |
+ |
|
150 |
+ |
int status; |
151 |
+ |
|
152 |
+ |
check(waitpid(learn, &status, 0), environment); |
153 |
|
check(waitpid(collab, &status, 0), environment); |
154 |
|
} |
155 |
|
|
156 |
< |
printf(" %u message(s) learned and %u message(s) %sed.\n", learned, count, job->verb); |
156 |
> |
printf(" %u message(s) learned and %u message(s) %sed.\n", learned, collabed, job->verb); |
157 |
|
} |
158 |
|
} |
159 |
|
|
167 |
|
|
168 |
|
const unsigned long *messages_ = messages[mailbox - mailboxes]; |
169 |
|
const size_t count = counts[mailbox - mailboxes]; |
121 |
– |
char *sequence = calloc(1, sizeof (char)); |
122 |
– |
size_t size = 1; |
170 |
|
|
171 |
< |
for (size_t index = 0; index != count; ++index) |
171 |
> |
if (count) |
172 |
|
{ |
173 |
< |
char *message; |
173 |
> |
char *sequence = calloc(1, sizeof (char)); |
174 |
> |
size_t size = 1; |
175 |
> |
|
176 |
> |
for (size_t index = 0; index != count; ++index) |
177 |
> |
{ |
178 |
> |
char *message; |
179 |
|
|
180 |
< |
asprintf(&message, "%lu%s", messages_[index], index + 1 != count ? "," : ""); |
180 |
> |
asprintf(&message, "%lu%s", messages_[index], index + 1 != count ? "," : ""); |
181 |
|
|
182 |
< |
sequence = realloc(sequence, (size += strlen(message)) * sizeof (char)); |
182 |
> |
sequence = realloc(sequence, (size += strlen(message)) * sizeof (char)); |
183 |
|
|
184 |
< |
strlcat(sequence, message, size); |
185 |
< |
free(message); |
184 |
> |
strlcat(sequence, message, size); |
185 |
> |
free(message); |
186 |
> |
} |
187 |
> |
|
188 |
> |
mail_setflag_full(streams[mailbox - mailboxes], sequence, "\\Deleted", ST_UID | ST_SILENT); |
189 |
> |
free(sequence); |
190 |
|
} |
191 |
|
|
136 |
– |
mail_setflag_full(streams[mailbox - mailboxes], sequence, "\\Deleted", ST_UID | ST_SILENT); |
137 |
– |
free(sequence); |
192 |
|
printf(" %u message(s) marked deleted.\n", count); |
193 |
|
} |
194 |
|
} |
222 |
|
else |
223 |
|
return printf("Usage: %s [-debug]\n", argv[0]), 1; |
224 |
|
|
225 |
< |
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"); |
225 |
> |
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 mailbox, copy it to the " POSITIVE " mailbox.\n"); |
226 |
|
|
227 |
+ |
check(asprintf(&pass, "%s/.CreditCardReminder.pass", getenv("HOME")), environment); |
228 |
|
check(sigemptyset(&set), environment); |
229 |
|
check(sigaddset(&set, SIGHUP), environment); |
230 |
|
check(sigaddset(&set, SIGINT), environment); |
231 |
|
|
232 |
|
# include <c-client/linkage.c> |
233 |
|
|
234 |
< |
streams[0] = mcheck(mail_open(NIL, NEGATIVE, debug ? OP_DEBUG : 0), environment); |
235 |
< |
streams[1] = mcheck(mail_open(NIL, POSITIVE, debug ? OP_DEBUG : 0), environment); |
234 |
> |
streams[0] = mcheck(mail_open(NIL, SERVER NEGATIVE, debug ? OP_DEBUG : 0), environment); |
235 |
> |
streams[1] = mcheck(mail_open(NIL, SERVER POSITIVE, debug ? OP_DEBUG : 0), environment); |
236 |
|
|
237 |
|
learn(); |
238 |
|
delete(); |
244 |
|
return 0; |
245 |
|
} |
246 |
|
|
247 |
< |
void mm_flags(MAILSTREAM *stream, unsigned long number) {} |
248 |
< |
void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status) {} |
247 |
> |
void mm_flags(MAILSTREAM *stream, unsigned long number) |
248 |
> |
{ |
249 |
> |
if (debug) |
250 |
> |
fprintf(stderr, "flags: %lu\n", number); |
251 |
> |
} |
252 |
> |
|
253 |
> |
void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status) |
254 |
> |
{ |
255 |
> |
if (debug) |
256 |
> |
{ |
257 |
> |
fprintf(stderr, "status: %s\n", mailbox); |
258 |
> |
|
259 |
> |
if (status->flags & SA_MESSAGES) |
260 |
> |
fprintf(stderr, " messages: %lu\n", status->messages); |
261 |
> |
|
262 |
> |
if (status->flags & SA_RECENT) |
263 |
> |
fprintf(stderr, " recent: %lu\n", status->recent); |
264 |
> |
|
265 |
> |
if (status->flags & SA_UNSEEN) |
266 |
> |
fprintf(stderr, " unseen: %lu\n", status->unseen); |
267 |
> |
|
268 |
> |
if (status->flags & SA_UIDNEXT) |
269 |
> |
fprintf(stderr, " uid next: %lu\n", status->uidnext); |
270 |
> |
|
271 |
> |
if (status->flags & SA_UIDVALIDITY) |
272 |
> |
fprintf(stderr, " uid validity: %lu\n", status->uidvalidity); |
273 |
> |
} |
274 |
> |
} |
275 |
|
|
276 |
|
void mm_searched(MAILSTREAM *stream, unsigned long number) |
277 |
|
{ |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
< |
void mm_exists(MAILSTREAM *stream, unsigned long number) {} |
291 |
> |
void mm_exists(MAILSTREAM *stream, unsigned long number) |
292 |
> |
{ |
293 |
> |
if (debug) |
294 |
> |
fprintf(stderr, "exists: %lu\n", number); |
295 |
> |
} |
296 |
> |
|
297 |
|
void mm_expunged(MAILSTREAM *stream, unsigned long number) {} |
298 |
|
void mm_list(MAILSTREAM *stream, int delim, char *name, long attrib) {} |
299 |
|
void mm_lsub(MAILSTREAM *stream, int delim, char *name, long attrib) {} |
300 |
|
|
301 |
< |
void mm_notify(MAILSTREAM *stream, char *string, long errflg) {} |
301 |
> |
void mm_notify(MAILSTREAM *stream, char *string, long errflg) |
302 |
> |
{ |
303 |
> |
if (debug) |
304 |
> |
fprintf(stderr, "%li: %s\n", errflg, string); |
305 |
> |
} |
306 |
|
|
307 |
|
void mm_log(char *string, long errflg) |
308 |
|
{ |
318 |
|
fprintf(stderr, "%s\n", string); |
319 |
|
} |
320 |
|
|
321 |
< |
void mm_login(NETMBX *mb, char *user, char *pwd, long trial) {} |
321 |
> |
void mm_login(NETMBX *mb, char *user, char *pwd, long trial) |
322 |
> |
{ |
323 |
> |
strcpy(user, "douglas"); |
324 |
> |
|
325 |
> |
FILE *file = fopen(pass, "r"); |
326 |
> |
|
327 |
> |
if (!file) |
328 |
> |
longjmp(environment, 1); |
329 |
> |
|
330 |
> |
size_t size; |
331 |
> |
char *line = fcheck(fgetln(file, &size), file, environment); |
332 |
> |
|
333 |
> |
strlcpy(pwd, line, size); |
334 |
> |
} |
335 |
|
|
336 |
|
void mm_critical(MAILSTREAM *stream) |
337 |
|
{ |
347 |
|
|
348 |
|
long mm_diskerror(MAILSTREAM *stream, long errcode, long serious) |
349 |
|
{ |
350 |
< |
fprintf(stderr, "diskerror:\n errcode = %li\n serious = %li\n", errcode, serious); |
350 |
> |
fprintf(stderr, "diskerror: %li%s\n", errcode, serious ? " serious" : ""); |
351 |
|
|
352 |
|
return 1; |
353 |
|
} |
354 |
|
|
355 |
|
void mm_fatal(char *string) |
356 |
|
{ |
357 |
< |
fprintf(stderr, "fatal:\n string = %s\n", string); |
357 |
> |
fprintf(stderr, "fatal: %s\n", string); |
358 |
|
} |