4 * This module allows Citadel to use SpamAssassin to filter incoming messages
5 * arriving via SMTP. For more information on SpamAssassin, visit
6 * http://www.spamassassin.org (the SpamAssassin project is not in any way
7 * affiliated with the Citadel project).
10 #define SPAMASSASSIN_PORT "783"
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
36 #include <sys/socket.h>
37 #include <libcitadel.h>
40 #include "citserver.h"
49 #include "internet_addressing.h"
51 #include "clientsocket.h"
54 #include "ctdl_module.h"
59 * Connect to the SpamAssassin server and scan a message.
61 int spam_assassin(struct CtdlMessage *msg) {
71 /* For users who have authenticated to this server we never want to
72 * apply spam filtering, because presumably they're trustworthy.
74 if (CC->logged_in) return(0);
76 /* See if we have any SpamAssassin hosts configured */
77 num_sahosts = get_hosts(sahosts, "spamassassin");
78 if (num_sahosts < 1) return(0);
80 /* Try them one by one until we get a working one */
81 for (sa=0; sa<num_sahosts; ++sa) {
82 extract_token(buf, sahosts, sa, '|', sizeof buf);
83 CtdlLogPrintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
84 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
85 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
89 /* If the service isn't running, just pass the mail
90 * through. Potentially throwing away mails isn't good.
96 CtdlLogPrintf(CTDL_DEBUG, "Transmitting command\n");
97 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
98 sock_write(sock, buf, strlen(buf));
101 CC->redirect_buffer = malloc(SIZ);
102 CC->redirect_len = 0;
103 CC->redirect_alloc = SIZ;
104 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
105 msgtext = CC->redirect_buffer;
106 msglen = CC->redirect_len;
107 CC->redirect_buffer = NULL;
108 CC->redirect_len = 0;
109 CC->redirect_alloc = 0;
111 sock_write(sock, msgtext, msglen);
114 /* Close one end of the socket connection; this tells SpamAssassin
117 sock_shutdown(sock, SHUT_WR);
120 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
121 if (sock_getln(sock, buf, sizeof buf) < 0) {
124 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
125 if (strncasecmp(buf, "SPAMD", 5)) {
128 if (sock_getln(sock, buf, sizeof buf) < 0) {
131 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
132 if (!strncasecmp(buf, "Spam: True", 10)) {
137 if (msg->cm_fields['0'] != NULL) {
138 free(msg->cm_fields['0']);
140 msg->cm_fields['0'] = strdup("message rejected by spam filter");
149 CTDL_MODULE_INIT(spam)
153 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
156 /* return our Subversion id for the Log */