2 * This module allows Citadel to use SpamAssassin to filter incoming messages
3 * arriving via SMTP. For more information on SpamAssassin, visit
4 * http://www.spamassassin.org (the SpamAssassin project is not in any way
5 * affiliated with the Citadel project).
7 * Copyright (c) 1998-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define SPAMASSASSIN_PORT "783"
34 #include <sys/types.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
50 #include <sys/socket.h>
51 #include <libcitadel.h>
54 #include "citserver.h"
61 #include "internet_addressing.h"
63 #include "clientsocket.h"
66 #include "ctdl_module.h"
71 * Connect to the SpamAssassin server and scan a message.
73 int spam_assassin(struct CtdlMessage *msg) {
83 /* For users who have authenticated to this server we never want to
84 * apply spam filtering, because presumably they're trustworthy.
86 if (CC->logged_in) return(0);
88 /* See if we have any SpamAssassin hosts configured */
89 num_sahosts = get_hosts(sahosts, "spamassassin");
90 if (num_sahosts < 1) return(0);
92 /* Try them one by one until we get a working one */
93 for (sa=0; sa<num_sahosts; ++sa) {
94 extract_token(buf, sahosts, sa, '|', sizeof buf);
95 syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
96 sock = sock_connect(buf, SPAMASSASSIN_PORT);
97 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
101 /* If the service isn't running, just pass the mail
102 * through. Potentially throwing away mails isn't good.
107 CCC->sReadBuf = NewStrBuf();
108 CCC->sMigrateBuf = NewStrBuf();
112 syslog(LOG_DEBUG, "Transmitting command\n");
113 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
114 sock_write(&sock, buf, strlen(buf));
117 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
118 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
119 msgtext = CC->redirect_buffer;
120 CC->redirect_buffer = NULL;
122 sock_write(&sock, SKEY(msgtext));
123 FreeStrBuf(&msgtext);
125 /* Close one end of the socket connection; this tells SpamAssassin
129 sock_shutdown(sock, SHUT_WR);
132 syslog(LOG_DEBUG, "Awaiting response\n");
133 if (sock_getln(&sock, buf, sizeof buf) < 0) {
136 syslog(LOG_DEBUG, "<%s\n", buf);
137 if (strncasecmp(buf, "SPAMD", 5)) {
140 if (sock_getln(&sock, buf, sizeof buf) < 0) {
143 syslog(LOG_DEBUG, "<%s\n", buf);
144 syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
145 if (config.c_spam_flag_only) {
146 syslog(LOG_DEBUG, "flag spam code used");
156 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
157 extract_token(sascore, buf, 3, ' ', sizeof sascore);
158 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
160 sprintf(buf,"X-Spam-Level: ");
161 char *cur = buf + 14;
162 for (numscore = atoi(sascore); numscore>0; numscore--)
166 sprintf(cur,"\r\nX-Spam-Status: %s, score=%s required=%s\r\n", sastatus, sascore, saoutof);
167 headerlen = strlen(buf);
168 oldmsgsize = strlen(msg->cm_fields['M']) + 1;
169 newmsgsize = headerlen + oldmsgsize;
171 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], newmsgsize);
173 memmove(msg->cm_fields['M']+headerlen,msg->cm_fields['M'],oldmsgsize);
174 memcpy(msg->cm_fields['M'],buf,headerlen);
177 syslog(LOG_DEBUG, "reject spam code used");
178 if (!strncasecmp(buf, "Spam: True", 10)) {
183 if (msg->cm_fields['0'] != NULL) {
184 free(msg->cm_fields['0']);
186 msg->cm_fields['0'] = strdup("message rejected by spam filter");
191 FreeStrBuf(&CCC->sReadBuf);
192 FreeStrBuf(&CCC->sMigrateBuf);
198 CTDL_MODULE_INIT(spam)
202 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
205 /* return our Subversion id for the Log */