1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Spam related facilities.
4 * Copyright (c) 2013 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #ifndef HAVE_AMALGAMATION
28 #ifdef HAVE_SPAM_SPAMD
29 # include <sys/socket.h>
33 /* This is chosen rather arbitrarily.
34 * It must be able to swallow the first line of a rate response,
35 * and an entire CHECK/TELL spamd(1) response */
36 #if BUFFER_SIZE < 1024
37 # error *spam-interface* BUFFER_SIZE constraints are not matched
40 #ifdef HAVE_SPAM_SPAMD
41 # define SPAMD_IDENT "SPAMC/1.5"
43 # define SUN_LEN(SUP) \
44 (sizeof(*(SUP)) - sizeof((SUP)->sun_path) + strlen((SUP)->sun_path))
48 #ifdef HAVE_SPAM_FILTER
49 /* n_NELEM() of regmatch_t groups */
50 # define SPAM_FILTER_MATCHES 32u
60 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
63 char *cf_result
; /* _SPAM_RATE: first response line */
67 /* .cf_cmd may be adjusted for each call (`spamforget')... */
70 char const *cf_env
[4];
71 sighandler_type cf_otstp
;
72 sighandler_type cf_ottin
;
73 sighandler_type cf_ottou
;
74 sighandler_type cf_ohup
;
75 sighandler_type cf_opipe
;
76 sighandler_type cf_oint
;
77 sighandler_type cf_oquit
;
81 #ifdef HAVE_SPAM_SPAMC
83 struct spam_cf c_super
;
84 char const *c_cmd_arr
[9];
88 #ifdef HAVE_SPAM_SPAMD
91 sighandler_type d_otstp
;
92 sighandler_type d_ottin
;
93 sighandler_type d_ottou
;
94 sighandler_type d_ohup
;
95 sighandler_type d_opipe
;
96 sighandler_type d_oint
;
97 sighandler_type d_oquit
;
98 struct sockaddr_un d_sun
;
102 #ifdef HAVE_SPAM_FILTER
104 struct spam_cf f_super
;
105 char const *f_cmd_nospam
; /* Working relative to current message.. */
106 char const *f_cmd_noham
;
109 ui32_t f_score_grpno
; /* 0 for not set */
110 regex_t f_score_regex
;
116 enum spam_action vc_action
;
117 bool_t vc_verbose
; /* Verbose output */
118 bool_t vc_progress
; /* "Progress meter" (mutual verbose) */
120 bool_t (*vc_act
)(struct spam_vc
*);
121 void (*vc_dtor
)(struct spam_vc
*);
122 char *vc_buffer
; /* I/O buffer, BUFFER_SIZE bytes */
123 size_t vc_mno
; /* Current message number */
124 struct message
*vc_mp
; /* Current message */
125 FILE *vc_ifp
; /* Input stream on .vc_mp */
127 #ifdef HAVE_SPAM_SPAMC
128 struct spam_spamc spamc
;
130 #ifdef HAVE_SPAM_SPAMD
131 struct spam_spamd spamd
;
133 #ifdef HAVE_SPAM_FILTER
134 struct spam_filter filter
;
136 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
140 char const *vc_esep
; /* Error separator for progress mode */
143 /* Indices according to enum spam_action */
144 static char const _spam_cmds
[][16] = {
145 "spamrate", "spamham", "spamspam", "spamforget"
148 /* Shared action setup */
149 static bool_t
_spam_action(enum spam_action sa
, int *ip
);
151 /* *spam-interface*=spamc: initialize, communicate */
152 #ifdef HAVE_SPAM_SPAMC
153 static bool_t
_spamc_setup(struct spam_vc
*vcp
);
154 static bool_t
_spamc_interact(struct spam_vc
*vcp
);
155 static void _spamc_dtor(struct spam_vc
*vcp
);
158 /* *spam-interface*=spamd: initialize, communicate */
159 #ifdef HAVE_SPAM_SPAMD
160 static bool_t
_spamd_setup(struct spam_vc
*vcp
);
161 static bool_t
_spamd_interact(struct spam_vc
*vcp
);
164 /* *spam-interface*=filter: initialize, communicate */
165 #ifdef HAVE_SPAM_FILTER
166 static bool_t
_spamfilter_setup(struct spam_vc
*vcp
);
167 static bool_t
_spamfilter_interact(struct spam_vc
*vcp
);
168 static void _spamfilter_dtor(struct spam_vc
*vcp
);
171 /* *spam-interface*=(spamc|filter): create child + communication */
172 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
173 static void _spam_cf_setup(struct spam_vc
*vcp
, bool_t useshell
);
174 static bool_t
_spam_cf_interact(struct spam_vc
*vcp
);
177 /* Convert a floating-point spam rate into message.m_spamscore */
178 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_SPAMD ||\
179 (defined HAVE_SPAM_FILTER && defined HAVE_REGEX)
180 static void _spam_rate2score(struct spam_vc
*vcp
, char *buf
);
184 _spam_action(enum spam_action sa
, int *ip
)
187 size_t maxsize
, skipped
, cnt
, curr
;
192 memset(&vc
, 0, sizeof vc
);
194 vc
.vc_verbose
= ((n_poption
& n_PO_VERB
) != 0);
195 vc
.vc_progress
= (!vc
.vc_verbose
&& ((n_psonce
& n_PSO_INTERACTIVE
) != 0));
196 vc
.vc_esep
= vc
.vc_progress
? "\n" : n_empty
;
198 /* Check and setup the desired spam interface */
199 if ((cp
= ok_vlook(spam_interface
)) == NULL
) {
200 n_err(_("`%s': no *spam-interface* set\n"), _spam_cmds
[sa
]);
202 #ifdef HAVE_SPAM_SPAMC
203 } else if (!asccasecmp(cp
, "spamc")) {
204 if (!_spamc_setup(&vc
))
207 #ifdef HAVE_SPAM_SPAMD
208 } else if (!asccasecmp(cp
, "spamd")) { /* TODO v15: remove */
209 n_OBSOLETE(_("*spam-interface*=spamd is obsolete, please use =spamc"));
210 if (!_spamd_setup(&vc
))
213 #ifdef HAVE_SPAM_FILTER
214 } else if (!asccasecmp(cp
, "filter")) {
215 if (!_spamfilter_setup(&vc
))
219 n_err(_("`%s': unknown / unsupported *spam-interface*: %s\n"),
224 /* *spam-maxsize* we do handle ourselfs instead */
225 if ((cp
= ok_vlook(spam_maxsize
)) == NULL
||
226 (n_idec_ui32_cp(&maxsize
, cp
, 0, NULL
), maxsize
) == 0)
227 maxsize
= SPAM_MAXSIZE
;
229 /* Finally get an I/O buffer */
230 vc
.vc_buffer
= salloc(BUFFER_SIZE
);
233 if (vc
.vc_progress
) {
237 for (curr
= 0, ok
= TRU1
; *ip
!= 0; --cnt
, ++curr
, ++ip
) {
238 vc
.vc_mno
= (size_t)*ip
;
239 vc
.vc_mp
= message
+ vc
.vc_mno
- 1;
240 if (sa
== _SPAM_RATE
)
241 vc
.vc_mp
->m_spamscore
= 0;
243 if (vc
.vc_mp
->m_size
> maxsize
) {
245 n_err(_("`%s': message %lu exceeds maxsize (%lu > %lu), skip\n"),
246 _spam_cmds
[sa
], (ul_i
)vc
.vc_mno
, (ul_i
)(size_t)vc
.vc_mp
->m_size
,
248 else if (vc
.vc_progress
) {
249 fprintf(n_stdout
, "\r%s: !%-6" PRIuZ
" %6" PRIuZ
"/%-6" PRIuZ
,
250 _spam_cmds
[sa
], vc
.vc_mno
, cnt
, curr
);
256 n_err(_("`%s': message %lu\n"), _spam_cmds
[sa
], (ul_i
)vc
.vc_mno
);
257 else if (vc
.vc_progress
) {
258 fprintf(n_stdout
, "\r%s: .%-6" PRIuZ
" %6" PRIuZ
"/%-6" PRIuZ
,
259 _spam_cmds
[sa
], vc
.vc_mno
, cnt
, curr
);
264 if ((vc
.vc_ifp
= setinput(&mb
, vc
.vc_mp
, NEED_BODY
)) == NULL
) {
265 n_err(_("%s`%s': cannot load message %lu: %s\n"),
266 vc
.vc_esep
, _spam_cmds
[sa
], (ul_i
)vc
.vc_mno
,
267 n_err_to_doc(n_err_no
));
272 if (!(ok
= (*vc
.vc_act
)(&vc
)))
276 if (vc
.vc_progress
) {
278 fprintf(n_stdout
, _(" %s (%" PRIuZ
"/%" PRIuZ
" all/skipped)\n"),
279 (ok
? _("done") : V_(n_error
)), curr
, skipped
);
283 if (vc
.vc_dtor
!= NULL
)
290 #ifdef HAVE_SPAM_SPAMC
292 _spamc_setup(struct spam_vc
*vcp
)
294 struct spam_spamc
*sscp
;
296 char const **args
, *cp
;
300 sscp
= &vcp
->vc_t
.spamc
;
301 args
= sscp
->c_cmd_arr
;
303 if ((cp
= ok_vlook(spamc_command
)) == NULL
) {
304 # ifdef SPAM_SPAMC_PATH
305 cp
= SPAM_SPAMC_PATH
;
307 n_err(_("`%s': *spamc-command* is not set\n"),
308 _spam_cmds
[vcp
->vc_action
]);
314 switch (vcp
->vc_action
) {
333 *args
++ = "-l"; /* --log-to-stderr */
334 *args
++ = "-x"; /* No "safe callback", we need to react on errors! */
336 if ((cp
= ok_vlook(spamc_arguments
)) != NULL
)
339 if ((cp
= ok_vlook(spamc_user
)) != NULL
) {
341 cp
= ok_vlook(LOGNAME
);
345 assert(PTR2SIZE(args
- sscp
->c_cmd_arr
) <= n_NELEM(sscp
->c_cmd_arr
));
348 sscp
->c_super
.cf_cmd
= str_concat_cpa(&str
, sscp
->c_cmd_arr
, " ")->s
;
350 n_err(_("spamc(1) via %s\n"),
351 n_shexp_quote_cp(sscp
->c_super
.cf_cmd
, FAL0
));
353 _spam_cf_setup(vcp
, FAL0
);
355 vcp
->vc_act
= &_spamc_interact
;
356 vcp
->vc_dtor
= &_spamc_dtor
;
358 # ifndef SPAM_SPAMC_PATH
366 _spamc_interact(struct spam_vc
*vcp
)
371 if (!(rv
= _spam_cf_interact(vcp
)))
374 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
375 if (vcp
->vc_action
!= _SPAM_RATE
) {
376 if (vcp
->vc_action
== _SPAM_SPAM
)
377 vcp
->vc_mp
->m_flag
|= MSPAM
;
381 switch (WEXITSTATUS(vcp
->vc_t
.spamc
.c_super
.cf_waitstat
)) {
383 vcp
->vc_mp
->m_flag
|= MSPAM
;
392 if ((cp
= strchr(buf
= vcp
->vc_t
.spamc
.c_super
.cf_result
, '/')) != NULL
)
393 buf
[PTR2SIZE(cp
- buf
)] = '\0';
394 _spam_rate2score(vcp
, buf
);
402 _spamc_dtor(struct spam_vc
*vcp
)
405 if (vcp
->vc_t
.spamc
.c_super
.cf_result
!= NULL
)
406 free(vcp
->vc_t
.spamc
.c_super
.cf_result
);
409 #endif /* HAVE_SPAM_SPAMC */
411 #ifdef HAVE_SPAM_SPAMD
413 _spamd_setup(struct spam_vc
*vcp
)
415 struct spam_spamd
*ssdp
;
421 ssdp
= &vcp
->vc_t
.spamd
;
423 if ((cp
= ok_vlook(spamd_user
)) != NULL
) {
425 cp
= ok_vlook(LOGNAME
);
426 ssdp
->d_user
.l
= strlen(ssdp
->d_user
.s
= n_UNCONST(cp
));
429 if ((cp
= ok_vlook(spamd_socket
)) == NULL
) {
430 n_err(_("`%s': required *spamd-socket* is not set\n"),
431 _spam_cmds
[vcp
->vc_action
]);
434 if ((l
= strlen(cp
) +1) >= sizeof(ssdp
->d_sun
.sun_path
)) {
435 n_err(_("`%s': *spamd-socket* too long: %s\n"),
436 _spam_cmds
[vcp
->vc_action
], n_shexp_quote_cp(cp
, FAL0
));
439 ssdp
->d_sun
.sun_family
= AF_UNIX
;
440 memcpy(ssdp
->d_sun
.sun_path
, cp
, l
);
442 vcp
->vc_act
= &_spamd_interact
;
449 static sigjmp_buf __spamd_actjmp
; /* TODO oneday, we won't need it no more */
450 static int volatile __spamd_sig
; /* TODO oneday, we won't need it no more */
452 __spamd_onsig(int sig
) /* TODO someday, we won't need it no more */
454 NYD_X
; /* Signal handler */
456 siglongjmp(__spamd_actjmp
, 1);
460 _spamd_interact(struct spam_vc
*vcp
)
462 struct spam_spamd
*ssdp
;
464 char *lp
, *cp
, * volatile headbuf
= NULL
;
465 int volatile dsfd
= -1;
466 bool_t
volatile rv
= FAL0
;
469 ssdp
= &vcp
->vc_t
.spamd
;
473 ssdp
->d_otstp
= safe_signal(SIGTSTP
, SIG_DFL
);
474 ssdp
->d_ottin
= safe_signal(SIGTTIN
, SIG_DFL
);
475 ssdp
->d_ottou
= safe_signal(SIGTTOU
, SIG_DFL
);
476 ssdp
->d_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
477 ssdp
->d_ohup
= safe_signal(SIGHUP
, &__spamd_onsig
);
478 ssdp
->d_oint
= safe_signal(SIGINT
, &__spamd_onsig
);
479 ssdp
->d_oquit
= safe_signal(SIGQUIT
, &__spamd_onsig
);
480 if (sigsetjmp(__spamd_actjmp
, 1)) {
481 if (*vcp
->vc_esep
!= '\0')
487 if ((dsfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
488 n_err(_("%s`%s': can't create unix(4) socket: %s\n"),
489 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
493 if (connect(dsfd
, (struct sockaddr
*)&ssdp
->d_sun
, SUN_LEN(&ssdp
->d_sun
)) ==
495 n_err(_("%s`%s': can't connect to *spam-socket*: %s\n"),
496 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
502 /* The command header, finalized with an empty line.
503 * This needs to be written in a single write(2)! */
505 # define _X(X) do {memcpy(lp, X, sizeof(X) -1); lp += sizeof(X) -1;} while (0)
507 i
= ((cp
= ssdp
->d_user
.s
) != NULL
) ? ssdp
->d_user
.l
: 0;
508 size
= sizeof(NETLINE("A_VERY_LONG_COMMAND " SPAMD_IDENT
)) +
509 sizeof(NETLINE("Content-length: 9223372036854775807")) +
510 ((cp
!= NULL
) ? sizeof("User: ") + i
+ sizeof(NETNL
) : 0) +
511 sizeof(NETLINE("Message-class: spam")) +
512 sizeof(NETLINE("Set: local")) +
513 sizeof(NETLINE("Remove: local")) +
514 sizeof(NETNL
) /*+1*/;
515 lp
= headbuf
= n_lofi_alloc(size
);
517 switch (vcp
->vc_action
) {
519 _X(NETLINE("CHECK " SPAMD_IDENT
));
524 _X(NETLINE("TELL " SPAMD_IDENT
));
528 lp
+= snprintf(lp
, size
, NETLINE("Content-length: %" PRIuZ
),
529 (size_t)vcp
->vc_mp
->m_size
);
538 switch (vcp
->vc_action
) {
543 _X(NETLINE("Message-class: ham")
544 NETLINE("Set: local")
548 _X(NETLINE("Message-class: spam")
549 NETLINE("Set: local")
553 if (vcp
->vc_mp
->m_flag
& MSPAM
)
554 _X(NETLINE("Message-class: spam"));
556 _X(NETLINE("Message-class: ham"));
557 _X(NETLINE("Remove: local")
563 i
= PTR2SIZE(lp
- headbuf
);
564 if (n_poption
& n_PO_VERBVERB
)
565 n_err(">>> %.*s <<<\n", (int)i
, headbuf
);
566 if (i
!= (size_t)write(dsfd
, headbuf
, i
))
569 /* Then simply pass through the message "as-is" */
570 for (size
= vcp
->vc_mp
->m_size
; size
> 0;) {
571 i
= fread(vcp
->vc_buffer
, sizeof *vcp
->vc_buffer
,
572 n_MIN(size
, BUFFER_SIZE
), vcp
->vc_ifp
);
574 if (ferror(vcp
->vc_ifp
))
580 if (i
!= (size_t)write(dsfd
, vcp
->vc_buffer
, i
)) {
582 n_err(_("%s`%s': I/O on *spamd-socket* failed: %s\n"),
583 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
588 /* We are finished, say so */
589 shutdown(dsfd
, SHUT_WR
);
591 /* Be aware on goto: i will be a line counter after this loop! */
592 for (size
= 0, i
= BUFFER_SIZE
-1;;) {
593 ssize_t j
= read(dsfd
, vcp
->vc_buffer
+ size
, i
);
599 /* For the current way of doing things a single read will suffice.
600 * Note we'll be "penaltized" when awaiting EOF on the socket, at least
601 * in blocking mode, so do avoid that and break off */
605 vcp
->vc_buffer
[size
] = '\0';
607 if (size
== 0 || size
== BUFFER_SIZE
) {
609 n_err(_("%s`%s': bogus spamd(1) I/O interaction (%lu)\n"),
610 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], (ul_i
)i
);
612 if (n_poption
& n_PO_VERBVERB
)
613 n_err(">>> BUFFER: %s <<<\n", vcp
->vc_buffer
);
618 /* From the response, read those lines that interest us */
619 for (lp
= vcp
->vc_buffer
; size
> 0; ++i
) {
621 lp
= strchr(lp
, NETNL
[0]);
625 if (lp
[1] != NETNL
[1])
628 size
-= PTR2SIZE(lp
- cp
);
631 if (!strncmp(cp
, "SPAMD/1.1 0 EX_OK", sizeof("SPAMD/1.1 0 EX_OK") -1))
633 if (vcp
->vc_action
!= _SPAM_RATE
||
634 strstr(cp
, "Service Unavailable") == NULL
)
637 /* Unfortunately a missing --allow-tell drops connection.. */
638 n_err(_("%s`%s': service not available in spamd(1) instance\n"),
639 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
]);
643 switch (vcp
->vc_action
) {
645 if (strncmp(cp
, "Spam: ", sizeof("Spam: ") -1))
647 cp
+= sizeof("Spam: ") -1;
649 if (!strncmp(cp
, "False", sizeof("False") -1)) {
650 cp
+= sizeof("False") -1;
651 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
652 } else if (!strncmp(cp
, "True", sizeof("True") -1)) {
653 cp
+= sizeof("True") -1;
654 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
655 vcp
->vc_mp
->m_flag
|= MSPAM
;
659 while (blankspacechar(*cp
))
665 char *xcp
= strchr(cp
, '/');
667 size
= PTR2SIZE(xcp
- cp
);
670 _spam_rate2score(vcp
, cp
);
676 /* Empty response means ok but "did nothing" */
678 strncmp(cp
, "DidSet: local", sizeof("DidSet: local") -1))
680 if (*cp
== '\0' && vcp
->vc_verbose
)
681 n_err(_("\tBut spamd(1) \"did nothing\" for message\n"));
682 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
683 if (vcp
->vc_action
== _SPAM_SPAM
)
684 vcp
->vc_mp
->m_flag
|= MSPAM
;
689 strncmp(cp
, "DidRemove: local", sizeof("DidSet: local") -1))
691 if (*cp
== '\0' && vcp
->vc_verbose
)
692 n_err(_("\tBut spamd(1) \"did nothing\" for message\n"));
693 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
707 safe_signal(SIGQUIT
, ssdp
->d_oquit
);
708 safe_signal(SIGINT
, ssdp
->d_oint
);
709 safe_signal(SIGHUP
, ssdp
->d_ohup
);
710 safe_signal(SIGPIPE
, ssdp
->d_opipe
);
711 safe_signal(SIGTSTP
, ssdp
->d_otstp
);
712 safe_signal(SIGTTIN
, ssdp
->d_ottin
);
713 safe_signal(SIGTTOU
, ssdp
->d_ottou
);
716 if (__spamd_sig
!= 0) {
719 sigaddset(&cset
, __spamd_sig
);
720 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
721 n_raise(__spamd_sig
);
726 #endif /* HAVE_SPAM_SPAMD */
728 #ifdef HAVE_SPAM_FILTER
730 _spamfilter_setup(struct spam_vc
*vcp
)
732 struct spam_filter
*sfp
;
733 char const *cp
, *var
;
737 sfp
= &vcp
->vc_t
.filter
;
739 switch (vcp
->vc_action
) {
741 cp
= ok_vlook(spamfilter_rate
);
742 var
= "spam-filter-rate";
745 cp
= ok_vlook(spamfilter_ham
);
746 var
= "spam-filter-ham";
749 cp
= ok_vlook(spamfilter_spam
);
750 var
= "spam-filter-spam";
754 n_err(_("`%s': *%s* is not set\n"), _spam_cmds
[vcp
->vc_action
], var
);
757 sfp
->f_super
.cf_cmd
= savestr(cp
);
760 var
= "spam-filter-nospam";
761 if ((cp
= ok_vlook(spamfilter_nospam
)) == NULL
)
763 sfp
->f_cmd_nospam
= savestr(cp
);
764 if ((cp
= ok_vlook(spamfilter_noham
)) == NULL
)
766 sfp
->f_cmd_noham
= savestr(cp
);
771 if (vcp
->vc_action
== _SPAM_RATE
&&
772 (cp
= ok_vlook(spamfilter_rate_scanscore
)) != NULL
) {
776 var
= strchr(cp
, ';');
778 n_err(_("`%s': *spamfilter-rate-scanscore*: missing semicolon;: %s\n"),
779 _spam_cmds
[vcp
->vc_action
], cp
);
784 if((n_idec_buf(&sfp
->f_score_grpno
, cp
, PTR2SIZE(var
- cp
), 0,
785 n_IDEC_MODE_LIMIT_32BIT
, NULL
786 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
787 ) != n_IDEC_STATE_CONSUMED
){
788 n_err(_("`%s': *spamfilter-rate-scanscore*: bad group: %s\n"),
789 _spam_cmds
[vcp
->vc_action
], cp
);
792 if (sfp
->f_score_grpno
>= SPAM_FILTER_MATCHES
) {
793 n_err(_("`%s': *spamfilter-rate-scanscore*: "
794 "group %u excesses limit %u\n"),
795 _spam_cmds
[vcp
->vc_action
], sfp
->f_score_grpno
,
796 SPAM_FILTER_MATCHES
);
800 if ((s
= regcomp(&sfp
->f_score_regex
, bp
, REG_EXTENDED
| REG_ICASE
))
802 n_err(_("`%s': invalid *spamfilter-rate-scanscore* regex: %s: %s\n"),
803 _spam_cmds
[vcp
->vc_action
], n_shexp_quote_cp(cp
, FAL0
),
804 n_regex_err_to_doc(&sfp
->f_score_regex
, s
));
807 if (sfp
->f_score_grpno
> sfp
->f_score_regex
.re_nsub
) {
808 regfree(&sfp
->f_score_regex
);
809 n_err(_("`%s': *spamfilter-rate-scanscore*: no group %u: %s\n"),
810 _spam_cmds
[vcp
->vc_action
], sfp
->f_score_grpno
, cp
);
814 # endif /* HAVE_REGEX */
816 _spam_cf_setup(vcp
, TRU1
);
818 vcp
->vc_act
= &_spamfilter_interact
;
819 vcp
->vc_dtor
= &_spamfilter_dtor
;
827 _spamfilter_interact(struct spam_vc
*vcp
)
830 regmatch_t rem
[SPAM_FILTER_MATCHES
], *remp
;
831 struct spam_filter
*sfp
;
837 if (vcp
->vc_action
== _SPAM_FORGET
)
838 vcp
->vc_t
.cf
.cf_cmd
= (vcp
->vc_mp
->m_flag
& MSPAM
)
839 ? vcp
->vc_t
.filter
.f_cmd_nospam
: vcp
->vc_t
.filter
.f_cmd_noham
;
841 if (!(rv
= _spam_cf_interact(vcp
)))
844 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
845 if (vcp
->vc_action
!= _SPAM_RATE
) {
846 if (vcp
->vc_action
== _SPAM_SPAM
)
847 vcp
->vc_mp
->m_flag
|= MSPAM
;
849 } else switch (WEXITSTATUS(vcp
->vc_t
.filter
.f_super
.cf_waitstat
)) {
851 vcp
->vc_mp
->m_flag
|= MSPAMUNSURE
;
856 vcp
->vc_mp
->m_flag
|= MSPAM
;
864 sfp
= &vcp
->vc_t
.filter
;
866 if (sfp
->f_score_grpno
== 0)
869 assert(sfp
->f_super
.cf_result
!= NULL
);
870 remp
= rem
+ sfp
->f_score_grpno
;
872 if (regexec(&sfp
->f_score_regex
, sfp
->f_super
.cf_result
, n_NELEM(rem
), rem
,
873 0) == REG_NOMATCH
|| (remp
->rm_so
| remp
->rm_eo
) < 0) {
874 n_err(_("`%s': *spamfilter-rate-scanscore* "
875 "doesn't match filter output!\n"),
876 _spam_cmds
[vcp
->vc_action
]);
877 sfp
->f_score_grpno
= 0;
881 cp
= sfp
->f_super
.cf_result
;
882 cp
[remp
->rm_eo
] = '\0';
884 _spam_rate2score(vcp
, cp
);
885 # endif /* HAVE_REGEX */
893 _spamfilter_dtor(struct spam_vc
*vcp
)
895 struct spam_filter
*sfp
;
898 sfp
= &vcp
->vc_t
.filter
;
900 if (sfp
->f_super
.cf_result
!= NULL
)
901 free(sfp
->f_super
.cf_result
);
903 if (sfp
->f_score_grpno
> 0)
904 regfree(&sfp
->f_score_regex
);
908 #endif /* HAVE_SPAM_FILTER */
910 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
912 _spam_cf_setup(struct spam_vc
*vcp
, bool_t useshell
)
916 struct spam_cf
*scfp
;
918 n_LCTA(2 < n_NELEM(scfp
->cf_env
), "Preallocated buffer too small");
920 scfp
= &vcp
->vc_t
.cf
;
922 if ((scfp
->cf_useshell
= useshell
)) {
923 scfp
->cf_acmd
= ok_vlook(SHELL
);
927 /* MAILX_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
928 * TODO a file wherever he wants! *Do* create a zero-size temporary file
929 * TODO and give *that* path as MAILX_FILENAME_TEMPORARY, clean it up once
930 * TODO the pipe returns? Like this we *can* verify path/name issues! */
931 cp
= n_random_create_cp(n_MIN(NAME_MAX
/ 4, 16), NULL
);
932 scfp
->cf_env
[0] = str_concat_csvl(&s
,
933 n_PIPEENV_FILENAME_GENERATED
, "=", cp
, NULL
)->s
;
934 /* v15 compat NAIL_ environments vanish! */
935 scfp
->cf_env
[1] = str_concat_csvl(&s
,
936 "NAIL_FILENAME_GENERATED", "=", cp
, NULL
)->s
;
937 scfp
->cf_env
[2] = NULL
;
941 static sigjmp_buf __spam_cf_actjmp
; /* TODO someday, we won't need it */
942 static int volatile __spam_cf_sig
; /* TODO someday, we won't need it */
944 __spam_cf_onsig(int sig
) /* TODO someday, we won't need it no more */
946 NYD_X
; /* Signal handler */
948 siglongjmp(__spam_cf_actjmp
, 1);
952 _spam_cf_interact(struct spam_vc
*vcp
)
954 struct spam_cf
*scfp
;
965 _P2C
= _P2C_0
| _P2C_1
,
968 _C2P
= _C2P_0
| _C2P_1
,
973 } volatile state
= _NONE
;
976 scfp
= &vcp
->vc_t
.cf
;
977 if (scfp
->cf_result
!= NULL
) {
978 free(scfp
->cf_result
);
979 scfp
->cf_result
= NULL
;
982 /* TODO Avoid that we jump away; yet necessary signal mess */
983 /*__spam_cf_sig = 0;*/
986 scfp
->cf_otstp
= safe_signal(SIGTSTP
, SIG_DFL
);
987 scfp
->cf_ottin
= safe_signal(SIGTTIN
, SIG_DFL
);
988 scfp
->cf_ottou
= safe_signal(SIGTTOU
, SIG_DFL
);
989 scfp
->cf_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
990 scfp
->cf_ohup
= safe_signal(SIGHUP
, &__spam_cf_onsig
);
991 scfp
->cf_oint
= safe_signal(SIGINT
, &__spam_cf_onsig
);
992 scfp
->cf_oquit
= safe_signal(SIGQUIT
, &__spam_cf_onsig
);
993 /* Keep sigs blocked */
994 pid
= 0; /* cc uninit */
996 if (!pipe_cloexec(p2c
)) {
997 n_err(_("%s`%s': cannot create parent communication pipe: %s\n"),
998 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
1003 if (!pipe_cloexec(c2p
)) {
1004 n_err(_("%s`%s': cannot create child pipe: %s\n"),
1005 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
1010 if (sigsetjmp(__spam_cf_actjmp
, 1)) {
1011 if (*vcp
->vc_esep
!= '\0')
1012 n_err(vcp
->vc_esep
);
1019 /* Start our command as requested */
1021 if ((pid
= n_child_start(
1022 (scfp
->cf_acmd
!= NULL
? scfp
->cf_acmd
: scfp
->cf_cmd
),
1023 &cset
, p2c
[0], c2p
[1],
1024 scfp
->cf_a0
, (scfp
->cf_acmd
!= NULL
? scfp
->cf_cmd
: NULL
), NULL
,
1025 scfp
->cf_env
)) < 0) {
1033 /* Yes, we could sendmp(SEND_MBOX), but simply passing through the MBOX
1034 * content does the same in effect, however much more efficiently.
1035 * XXX NOTE: this may mean we pass a message without From_ line! */
1036 for (size
= vcp
->vc_mp
->m_size
; size
> 0;) {
1039 i
= fread(vcp
->vc_buffer
, 1, n_MIN(size
, BUFFER_SIZE
), vcp
->vc_ifp
);
1041 if (ferror(vcp
->vc_ifp
))
1046 if (i
!= (size_t)write(p2c
[1], vcp
->vc_buffer
, i
)) {
1053 /* TODO Quite racy -- block anything for a while? */
1054 if (state
& _SIGHOLD
) {
1059 if (state
& _P2C_0
) {
1063 if (state
& _C2P_1
) {
1067 /* And cause EOF for the reader */
1068 if (state
& _P2C_1
) {
1073 if (state
& _RUNNING
) {
1074 if (!(state
& _ERRORS
) &&
1075 vcp
->vc_action
== _SPAM_RATE
&& !(state
& (_JUMPED
| _ERRORS
))) {
1076 ssize_t i
= read(c2p
[0], vcp
->vc_buffer
, BUFFER_SIZE
- 1);
1078 vcp
->vc_buffer
[i
] = '\0';
1079 if ((cp
= strchr(vcp
->vc_buffer
, NETNL
[0])) == NULL
&&
1080 (cp
= strchr(vcp
->vc_buffer
, NETNL
[1])) == NULL
) {
1081 n_err(_("%s`%s': program generates too much output: %s\n"),
1082 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
],
1083 n_shexp_quote_cp(scfp
->cf_cmd
, FAL0
));
1086 scfp
->cf_result
= sbufdup(vcp
->vc_buffer
,
1087 PTR2SIZE(cp
- vcp
->vc_buffer
));
1088 /* FIXME consume child output until EOF??? */
1095 n_child_wait(pid
, &scfp
->cf_waitstat
);
1096 if (WIFEXITED(scfp
->cf_waitstat
))
1100 if (state
& _C2P_0
) {
1105 safe_signal(SIGQUIT
, scfp
->cf_oquit
);
1106 safe_signal(SIGINT
, scfp
->cf_oint
);
1107 safe_signal(SIGHUP
, scfp
->cf_ohup
);
1108 safe_signal(SIGPIPE
, scfp
->cf_opipe
);
1109 safe_signal(SIGTSTP
, scfp
->cf_otstp
);
1110 safe_signal(SIGTTIN
, scfp
->cf_ottin
);
1111 safe_signal(SIGTTOU
, scfp
->cf_ottou
);
1114 if (state
& _JUMPED
) {
1115 assert(vcp
->vc_dtor
!= NULL
);
1116 (*vcp
->vc_dtor
)(vcp
);
1119 sigaddset(&cset
, __spam_cf_sig
);
1120 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
1121 n_raise(__spam_cf_sig
);
1123 return !(state
& (_JUMPED
| _ERRORS
));
1125 #endif /* HAVE_SPAM_SPAMC || HAVE_SPAM_FILTER */
1127 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_SPAMD ||\
1128 (defined HAVE_SPAM_FILTER && defined HAVE_REGEX)
1130 _spam_rate2score(struct spam_vc
*vcp
, char *buf
){
1132 enum n_idec_state ids
;
1135 /* C99 */{ /* Overcome ISO C / compiler weirdness */
1139 ids
= n_idec_ui32_cp(&m
, buf
, 10, &cp
);
1140 if((ids
& n_IDEC_STATE_EMASK
) & ~n_IDEC_STATE_EBASE
)
1142 buf
= n_UNCONST(cp
);
1146 if(!(ids
& n_IDEC_STATE_CONSUMED
)){
1147 /* Floating-point rounding for non-mathematicians */
1151 if((c1
= buf
[0]) != '\0' && (c2
= buf
[1]) != '\0' &&
1152 (c3
= buf
[2]) != '\0'){
1168 ids
= n_idec_ui32_cp(&s
, buf
, 10, NULL
);
1169 if((ids
& (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1170 ) != n_IDEC_STATE_CONSUMED
)
1175 vcp
->vc_mp
->m_spamscore
= (m
<< 8) | s
;
1179 #endif /* _SPAM_SPAMC || _SPAM_SPAMD || (_SPAM_FILTER && HAVE_REGEX) */
1182 c_spam_clear(void *v
)
1187 for (ip
= v
; *ip
!= 0; ++ip
)
1188 message
[(size_t)*ip
- 1].m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
1199 for (ip
= v
; *ip
!= 0; ++ip
) {
1200 message
[(size_t)*ip
- 1].m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
1201 message
[(size_t)*ip
- 1].m_flag
|= MSPAM
;
1208 c_spam_forget(void *v
)
1213 rv
= _spam_action(_SPAM_FORGET
, (int*)v
) ? OKAY
: STOP
;
1224 rv
= _spam_action(_SPAM_HAM
, (int*)v
) ? OKAY
: STOP
;
1230 c_spam_rate(void *v
)
1235 rv
= _spam_action(_SPAM_RATE
, (int*)v
) ? OKAY
: STOP
;
1241 c_spam_spam(void *v
)
1246 rv
= _spam_action(_SPAM_SPAM
, (int*)v
) ? OKAY
: STOP
;
1250 #endif /* HAVE_SPAM */