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
[8];
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
));
501 /* The command header, finalized with an empty line.
502 * This needs to be written in a single write(2)! */
504 # define _X(X) do {memcpy(lp, X, sizeof(X) -1); lp += sizeof(X) -1;} while (0)
506 i
= ((cp
= ssdp
->d_user
.s
) != NULL
) ? ssdp
->d_user
.l
: 0;
507 size
= sizeof(NETLINE("A_VERY_LONG_COMMAND " SPAMD_IDENT
)) +
508 sizeof(NETLINE("Content-length: 9223372036854775807")) +
509 ((cp
!= NULL
) ? sizeof("User: ") + i
+ sizeof(NETNL
) : 0) +
510 sizeof(NETLINE("Message-class: spam")) +
511 sizeof(NETLINE("Set: local")) +
512 sizeof(NETLINE("Remove: local")) +
513 sizeof(NETNL
) /*+1*/;
514 lp
= headbuf
= n_lofi_alloc(size
);
516 switch (vcp
->vc_action
) {
518 _X(NETLINE("CHECK " SPAMD_IDENT
));
523 _X(NETLINE("TELL " SPAMD_IDENT
));
527 lp
+= snprintf(lp
, size
, NETLINE("Content-length: %" PRIuZ
),
528 (size_t)vcp
->vc_mp
->m_size
);
537 switch (vcp
->vc_action
) {
542 _X(NETLINE("Message-class: ham")
543 NETLINE("Set: local")
547 _X(NETLINE("Message-class: spam")
548 NETLINE("Set: local")
552 if (vcp
->vc_mp
->m_flag
& MSPAM
)
553 _X(NETLINE("Message-class: spam"));
555 _X(NETLINE("Message-class: ham"));
556 _X(NETLINE("Remove: local")
562 i
= PTR2SIZE(lp
- headbuf
);
563 if (n_poption
& n_PO_VERBVERB
)
564 n_err(">>> %.*s <<<\n", (int)i
, headbuf
);
565 if (i
!= (size_t)write(dsfd
, headbuf
, i
))
568 /* Then simply pass through the message "as-is" */
569 for (size
= vcp
->vc_mp
->m_size
; size
> 0;) {
570 i
= fread(vcp
->vc_buffer
, sizeof *vcp
->vc_buffer
,
571 n_MIN(size
, BUFFER_SIZE
), vcp
->vc_ifp
);
573 if (ferror(vcp
->vc_ifp
))
579 if (i
!= (size_t)write(dsfd
, vcp
->vc_buffer
, i
)) {
581 n_err(_("%s`%s': I/O on *spamd-socket* failed: %s\n"),
582 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
587 /* We are finished, say so */
588 shutdown(dsfd
, SHUT_WR
);
590 /* Be aware on goto: i will be a line counter after this loop! */
591 for (size
= 0, i
= BUFFER_SIZE
-1;;) {
592 ssize_t j
= read(dsfd
, vcp
->vc_buffer
+ size
, i
);
598 /* For the current way of doing things a single read will suffice.
599 * Note we'll be "penaltized" when awaiting EOF on the socket, at least
600 * in blocking mode, so do avoid that and break off */
604 vcp
->vc_buffer
[size
] = '\0';
606 if (size
== 0 || size
== BUFFER_SIZE
) {
608 n_err(_("%s`%s': bogus spamd(1) I/O interaction (%lu)\n"),
609 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], (ul_i
)i
);
611 if (n_poption
& n_PO_VERBVERB
)
612 n_err(">>> BUFFER: %s <<<\n", vcp
->vc_buffer
);
617 /* From the response, read those lines that interest us */
618 for (lp
= vcp
->vc_buffer
; size
> 0; ++i
) {
620 lp
= strchr(lp
, NETNL
[0]);
624 if (lp
[1] != NETNL
[1])
627 size
-= PTR2SIZE(lp
- cp
);
630 if (!strncmp(cp
, "SPAMD/1.1 0 EX_OK", sizeof("SPAMD/1.1 0 EX_OK") -1))
632 if (vcp
->vc_action
!= _SPAM_RATE
||
633 strstr(cp
, "Service Unavailable") == NULL
)
636 /* Unfortunately a missing --allow-tell drops connection.. */
637 n_err(_("%s`%s': service not available in spamd(1) instance\n"),
638 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
]);
642 switch (vcp
->vc_action
) {
644 if (strncmp(cp
, "Spam: ", sizeof("Spam: ") -1))
646 cp
+= sizeof("Spam: ") -1;
648 if (!strncmp(cp
, "False", sizeof("False") -1)) {
649 cp
+= sizeof("False") -1;
650 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
651 } else if (!strncmp(cp
, "True", sizeof("True") -1)) {
652 cp
+= sizeof("True") -1;
653 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
654 vcp
->vc_mp
->m_flag
|= MSPAM
;
658 while (blankspacechar(*cp
))
664 char *xcp
= strchr(cp
, '/');
666 size
= PTR2SIZE(xcp
- cp
);
669 _spam_rate2score(vcp
, cp
);
675 /* Empty response means ok but "did nothing" */
677 strncmp(cp
, "DidSet: local", sizeof("DidSet: local") -1))
679 if (*cp
== '\0' && vcp
->vc_verbose
)
680 n_err(_("\tBut spamd(1) \"did nothing\" for message\n"));
681 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
682 if (vcp
->vc_action
== _SPAM_SPAM
)
683 vcp
->vc_mp
->m_flag
|= MSPAM
;
688 strncmp(cp
, "DidRemove: local", sizeof("DidSet: local") -1))
690 if (*cp
== '\0' && vcp
->vc_verbose
)
691 n_err(_("\tBut spamd(1) \"did nothing\" for message\n"));
692 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
706 safe_signal(SIGQUIT
, ssdp
->d_oquit
);
707 safe_signal(SIGINT
, ssdp
->d_oint
);
708 safe_signal(SIGHUP
, ssdp
->d_ohup
);
709 safe_signal(SIGPIPE
, ssdp
->d_opipe
);
710 safe_signal(SIGTSTP
, ssdp
->d_otstp
);
711 safe_signal(SIGTTIN
, ssdp
->d_ottin
);
712 safe_signal(SIGTTOU
, ssdp
->d_ottou
);
715 if (__spamd_sig
!= 0) {
718 sigaddset(&cset
, __spamd_sig
);
719 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
720 n_raise(__spamd_sig
);
725 #endif /* HAVE_SPAM_SPAMD */
727 #ifdef HAVE_SPAM_FILTER
729 _spamfilter_setup(struct spam_vc
*vcp
)
731 struct spam_filter
*sfp
;
732 char const *cp
, *var
;
736 sfp
= &vcp
->vc_t
.filter
;
738 switch (vcp
->vc_action
) {
740 cp
= ok_vlook(spamfilter_rate
);
741 var
= "spam-filter-rate";
744 cp
= ok_vlook(spamfilter_ham
);
745 var
= "spam-filter-ham";
748 cp
= ok_vlook(spamfilter_spam
);
749 var
= "spam-filter-spam";
753 n_err(_("`%s': *%s* is not set\n"), _spam_cmds
[vcp
->vc_action
], var
);
756 sfp
->f_super
.cf_cmd
= savestr(cp
);
759 var
= "spam-filter-nospam";
760 if ((cp
= ok_vlook(spamfilter_nospam
)) == NULL
)
762 sfp
->f_cmd_nospam
= savestr(cp
);
763 if ((cp
= ok_vlook(spamfilter_noham
)) == NULL
)
765 sfp
->f_cmd_noham
= savestr(cp
);
770 if (vcp
->vc_action
== _SPAM_RATE
&&
771 (cp
= ok_vlook(spamfilter_rate_scanscore
)) != NULL
) {
775 var
= strchr(cp
, ';');
777 n_err(_("`%s': *spamfilter-rate-scanscore*: missing semicolon;: %s\n"),
778 _spam_cmds
[vcp
->vc_action
], cp
);
783 if((n_idec_buf(&sfp
->f_score_grpno
, cp
, PTR2SIZE(var
- cp
), 0,
784 n_IDEC_MODE_LIMIT_32BIT
, NULL
785 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
786 ) != n_IDEC_STATE_CONSUMED
){
787 n_err(_("`%s': *spamfilter-rate-scanscore*: bad group: %s\n"),
788 _spam_cmds
[vcp
->vc_action
], cp
);
791 if (sfp
->f_score_grpno
>= SPAM_FILTER_MATCHES
) {
792 n_err(_("`%s': *spamfilter-rate-scanscore*: "
793 "group %u excesses limit %u\n"),
794 _spam_cmds
[vcp
->vc_action
], sfp
->f_score_grpno
,
795 SPAM_FILTER_MATCHES
);
799 if ((s
= regcomp(&sfp
->f_score_regex
, bp
, REG_EXTENDED
| REG_ICASE
))
801 n_err(_("`%s': invalid *spamfilter-rate-scanscore* regex: %s: %s\n"),
802 _spam_cmds
[vcp
->vc_action
], n_shexp_quote_cp(cp
, FAL0
),
803 n_regex_err_to_doc(&sfp
->f_score_regex
, s
));
806 if (sfp
->f_score_grpno
> sfp
->f_score_regex
.re_nsub
) {
807 regfree(&sfp
->f_score_regex
);
808 n_err(_("`%s': *spamfilter-rate-scanscore*: no group %u: %s\n"),
809 _spam_cmds
[vcp
->vc_action
], sfp
->f_score_grpno
, cp
);
813 # endif /* HAVE_REGEX */
815 _spam_cf_setup(vcp
, TRU1
);
817 vcp
->vc_act
= &_spamfilter_interact
;
818 vcp
->vc_dtor
= &_spamfilter_dtor
;
826 _spamfilter_interact(struct spam_vc
*vcp
)
829 regmatch_t rem
[SPAM_FILTER_MATCHES
], *remp
;
830 struct spam_filter
*sfp
;
836 if (vcp
->vc_action
== _SPAM_FORGET
)
837 vcp
->vc_t
.cf
.cf_cmd
= (vcp
->vc_mp
->m_flag
& MSPAM
)
838 ? vcp
->vc_t
.filter
.f_cmd_nospam
: vcp
->vc_t
.filter
.f_cmd_noham
;
840 if (!(rv
= _spam_cf_interact(vcp
)))
843 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
844 if (vcp
->vc_action
!= _SPAM_RATE
) {
845 if (vcp
->vc_action
== _SPAM_SPAM
)
846 vcp
->vc_mp
->m_flag
|= MSPAM
;
848 } else switch (WEXITSTATUS(vcp
->vc_t
.filter
.f_super
.cf_waitstat
)) {
850 vcp
->vc_mp
->m_flag
|= MSPAMUNSURE
;
855 vcp
->vc_mp
->m_flag
|= MSPAM
;
863 sfp
= &vcp
->vc_t
.filter
;
865 if (sfp
->f_score_grpno
== 0)
868 assert(sfp
->f_super
.cf_result
!= NULL
);
869 remp
= rem
+ sfp
->f_score_grpno
;
871 if (regexec(&sfp
->f_score_regex
, sfp
->f_super
.cf_result
, n_NELEM(rem
), rem
,
872 0) == REG_NOMATCH
|| (remp
->rm_so
| remp
->rm_eo
) < 0) {
873 n_err(_("`%s': *spamfilter-rate-scanscore* "
874 "doesn't match filter output!\n"),
875 _spam_cmds
[vcp
->vc_action
]);
876 sfp
->f_score_grpno
= 0;
880 cp
= sfp
->f_super
.cf_result
;
881 cp
[remp
->rm_eo
] = '\0';
883 _spam_rate2score(vcp
, cp
);
884 # endif /* HAVE_REGEX */
892 _spamfilter_dtor(struct spam_vc
*vcp
)
894 struct spam_filter
*sfp
;
897 sfp
= &vcp
->vc_t
.filter
;
899 if (sfp
->f_super
.cf_result
!= NULL
)
900 free(sfp
->f_super
.cf_result
);
902 if (sfp
->f_score_grpno
> 0)
903 regfree(&sfp
->f_score_regex
);
907 #endif /* HAVE_SPAM_FILTER */
909 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
911 _spam_cf_setup(struct spam_vc
*vcp
, bool_t useshell
)
915 struct spam_cf
*scfp
;
917 n_LCTA(2 < n_NELEM(scfp
->cf_env
), "Preallocated buffer too small");
919 scfp
= &vcp
->vc_t
.cf
;
921 if ((scfp
->cf_useshell
= useshell
)) {
922 scfp
->cf_acmd
= ok_vlook(SHELL
);
926 /* MAILX_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
927 * TODO a file wherever he wants! *Do* create a zero-size temporary file
928 * TODO and give *that* path as MAILX_FILENAME_TEMPORARY, clean it up once
929 * TODO the pipe returns? Like this we *can* verify path/name issues! */
930 cp
= n_random_create_cp(n_MIN(NAME_MAX
/ 4, 16), NULL
);
931 scfp
->cf_env
[0] = str_concat_csvl(&s
,
932 n_PIPEENV_FILENAME_GENERATED
, "=", cp
, NULL
)->s
;
933 /* v15 compat NAIL_ environments vanish! */
934 scfp
->cf_env
[1] = str_concat_csvl(&s
,
935 "NAIL_FILENAME_GENERATED", "=", cp
, NULL
)->s
;
936 scfp
->cf_env
[2] = NULL
;
940 static sigjmp_buf __spam_cf_actjmp
; /* TODO someday, we won't need it */
941 static int volatile __spam_cf_sig
; /* TODO someday, we won't need it */
943 __spam_cf_onsig(int sig
) /* TODO someday, we won't need it no more */
945 NYD_X
; /* Signal handler */
947 siglongjmp(__spam_cf_actjmp
, 1);
951 _spam_cf_interact(struct spam_vc
*vcp
)
953 struct spam_cf
*scfp
;
964 _P2C
= _P2C_0
| _P2C_1
,
967 _C2P
= _C2P_0
| _C2P_1
,
972 } volatile state
= _NONE
;
975 scfp
= &vcp
->vc_t
.cf
;
976 if (scfp
->cf_result
!= NULL
) {
977 free(scfp
->cf_result
);
978 scfp
->cf_result
= NULL
;
981 /* TODO Avoid that we jump away; yet necessary signal mess */
982 /*__spam_cf_sig = 0;*/
985 scfp
->cf_otstp
= safe_signal(SIGTSTP
, SIG_DFL
);
986 scfp
->cf_ottin
= safe_signal(SIGTTIN
, SIG_DFL
);
987 scfp
->cf_ottou
= safe_signal(SIGTTOU
, SIG_DFL
);
988 scfp
->cf_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
989 scfp
->cf_ohup
= safe_signal(SIGHUP
, &__spam_cf_onsig
);
990 scfp
->cf_oint
= safe_signal(SIGINT
, &__spam_cf_onsig
);
991 scfp
->cf_oquit
= safe_signal(SIGQUIT
, &__spam_cf_onsig
);
992 /* Keep sigs blocked */
993 pid
= 0; /* cc uninit */
995 if (!pipe_cloexec(p2c
)) {
996 n_err(_("%s`%s': cannot create parent communication pipe: %s\n"),
997 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
1002 if (!pipe_cloexec(c2p
)) {
1003 n_err(_("%s`%s': cannot create child pipe: %s\n"),
1004 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], n_err_to_doc(n_err_no
));
1009 if (sigsetjmp(__spam_cf_actjmp
, 1)) {
1010 if (*vcp
->vc_esep
!= '\0')
1011 n_err(vcp
->vc_esep
);
1018 /* Start our command as requested */
1020 if ((pid
= n_child_start(
1021 (scfp
->cf_acmd
!= NULL
? scfp
->cf_acmd
: scfp
->cf_cmd
),
1022 &cset
, p2c
[0], c2p
[1],
1023 scfp
->cf_a0
, (scfp
->cf_acmd
!= NULL
? scfp
->cf_cmd
: NULL
), NULL
,
1024 scfp
->cf_env
)) < 0) {
1032 /* Yes, we could sendmp(SEND_MBOX), but simply passing through the MBOX
1033 * content does the same in effect, however much more efficiently.
1034 * XXX NOTE: this may mean we pass a message without From_ line! */
1035 for (size
= vcp
->vc_mp
->m_size
; size
> 0;) {
1038 i
= fread(vcp
->vc_buffer
, 1, n_MIN(size
, BUFFER_SIZE
), vcp
->vc_ifp
);
1040 if (ferror(vcp
->vc_ifp
))
1045 if (i
!= (size_t)write(p2c
[1], vcp
->vc_buffer
, i
)) {
1052 /* TODO Quite racy -- block anything for a while? */
1053 if (state
& _SIGHOLD
) {
1058 if (state
& _P2C_0
) {
1062 if (state
& _C2P_1
) {
1066 /* And cause EOF for the reader */
1067 if (state
& _P2C_1
) {
1072 if (state
& _RUNNING
) {
1073 if (!(state
& _ERRORS
) &&
1074 vcp
->vc_action
== _SPAM_RATE
&& !(state
& (_JUMPED
| _ERRORS
))) {
1075 ssize_t i
= read(c2p
[0], vcp
->vc_buffer
, BUFFER_SIZE
- 1);
1077 vcp
->vc_buffer
[i
] = '\0';
1078 if ((cp
= strchr(vcp
->vc_buffer
, NETNL
[0])) == NULL
&&
1079 (cp
= strchr(vcp
->vc_buffer
, NETNL
[1])) == NULL
) {
1080 n_err(_("%s`%s': program generates too much output: %s\n"),
1081 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
],
1082 n_shexp_quote_cp(scfp
->cf_cmd
, FAL0
));
1085 scfp
->cf_result
= sbufdup(vcp
->vc_buffer
,
1086 PTR2SIZE(cp
- vcp
->vc_buffer
));
1087 /* FIXME consume child output until EOF??? */
1094 n_child_wait(pid
, &scfp
->cf_waitstat
);
1095 if (WIFEXITED(scfp
->cf_waitstat
))
1099 if (state
& _C2P_0
) {
1104 safe_signal(SIGQUIT
, scfp
->cf_oquit
);
1105 safe_signal(SIGINT
, scfp
->cf_oint
);
1106 safe_signal(SIGHUP
, scfp
->cf_ohup
);
1107 safe_signal(SIGPIPE
, scfp
->cf_opipe
);
1108 safe_signal(SIGTSTP
, scfp
->cf_otstp
);
1109 safe_signal(SIGTTIN
, scfp
->cf_ottin
);
1110 safe_signal(SIGTTOU
, scfp
->cf_ottou
);
1113 if (state
& _JUMPED
) {
1114 assert(vcp
->vc_dtor
!= NULL
);
1115 (*vcp
->vc_dtor
)(vcp
);
1118 sigaddset(&cset
, __spam_cf_sig
);
1119 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
1120 n_raise(__spam_cf_sig
);
1122 return !(state
& (_JUMPED
| _ERRORS
));
1124 #endif /* HAVE_SPAM_SPAMC || HAVE_SPAM_FILTER */
1126 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_SPAMD ||\
1127 (defined HAVE_SPAM_FILTER && defined HAVE_REGEX)
1129 _spam_rate2score(struct spam_vc
*vcp
, char *buf
){
1131 enum n_idec_state ids
;
1134 /* C99 */{ /* Overcome ISO C / compiler weirdness */
1138 ids
= n_idec_ui32_cp(&m
, buf
, 10, &cp
);
1139 if((ids
& n_IDEC_STATE_EMASK
) & ~n_IDEC_STATE_EBASE
)
1141 buf
= n_UNCONST(cp
);
1145 if(!(ids
& n_IDEC_STATE_CONSUMED
)){
1146 /* Floating-point rounding for non-mathematicians */
1150 if((c1
= buf
[0]) != '\0' && (c2
= buf
[1]) != '\0' &&
1151 (c3
= buf
[2]) != '\0'){
1167 ids
= n_idec_ui32_cp(&s
, buf
, 10, NULL
);
1168 if((ids
& (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1169 ) != n_IDEC_STATE_CONSUMED
)
1174 vcp
->vc_mp
->m_spamscore
= (m
<< 8) | s
;
1178 #endif /* _SPAM_SPAMC || _SPAM_SPAMD || (_SPAM_FILTER && HAVE_REGEX) */
1181 c_spam_clear(void *v
)
1186 for (ip
= v
; *ip
!= 0; ++ip
)
1187 message
[(size_t)*ip
- 1].m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
1198 for (ip
= v
; *ip
!= 0; ++ip
) {
1199 message
[(size_t)*ip
- 1].m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
1200 message
[(size_t)*ip
- 1].m_flag
|= MSPAM
;
1207 c_spam_forget(void *v
)
1212 rv
= _spam_action(_SPAM_FORGET
, (int*)v
) ? OKAY
: STOP
;
1223 rv
= _spam_action(_SPAM_HAM
, (int*)v
) ? OKAY
: STOP
;
1229 c_spam_rate(void *v
)
1234 rv
= _spam_action(_SPAM_RATE
, (int*)v
) ? OKAY
: STOP
;
1240 c_spam_spam(void *v
)
1245 rv
= _spam_action(_SPAM_SPAM
, (int*)v
) ? OKAY
: STOP
;
1249 #endif /* HAVE_SPAM */