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).
9 * Copyright (c) 1998-2009 by the citadel.org team
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define SPAMASSASSIN_PORT "783"
36 #include <sys/types.h>
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
52 #include <sys/socket.h>
53 #include <libcitadel.h>
56 #include "citserver.h"
63 #include "internet_addressing.h"
65 #include "clientsocket.h"
68 #include "ctdl_module.h"
73 * Connect to the SpamAssassin server and scan a message.
75 int spam_assassin(struct CtdlMessage *msg) {
85 /* For users who have authenticated to this server we never want to
86 * apply spam filtering, because presumably they're trustworthy.
88 if (CC->logged_in) return(0);
90 /* See if we have any SpamAssassin hosts configured */
91 num_sahosts = get_hosts(sahosts, "spamassassin");
92 if (num_sahosts < 1) return(0);
94 /* Try them one by one until we get a working one */
95 for (sa=0; sa<num_sahosts; ++sa) {
96 extract_token(buf, sahosts, sa, '|', sizeof buf);
97 CtdlLogPrintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
98 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
99 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
103 /* If the service isn't running, just pass the mail
104 * through. Potentially throwing away mails isn't good.
109 CCC->sReadBuf = NewStrBuf();
110 CCC->sMigrateBuf = NewStrBuf();
114 CtdlLogPrintf(CTDL_DEBUG, "Transmitting command\n");
115 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
116 sock_write(&sock, buf, strlen(buf));
119 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
120 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
121 msgtext = CC->redirect_buffer;
122 CC->redirect_buffer = NULL;
124 sock_write(&sock, SKEY(msgtext));
125 FreeStrBuf(&msgtext);
127 /* Close one end of the socket connection; this tells SpamAssassin
131 sock_shutdown(sock, SHUT_WR);
134 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
135 if (sock_getln(&sock, buf, sizeof buf) < 0) {
138 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
139 if (strncasecmp(buf, "SPAMD", 5)) {
142 if (sock_getln(&sock, buf, sizeof buf) < 0) {
145 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
146 CtdlLogPrintf(CTDL_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
147 if (config.c_spam_flag_only) {
148 CtdlLogPrintf(CTDL_DEBUG, "flag spam code used");
158 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
159 extract_token(sascore, buf, 3, ' ', sizeof sascore);
160 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
162 sprintf(buf,"X-Spam-Level: ");
163 char *cur = buf + 14;
164 for (numscore = atoi(sascore); numscore>0; numscore--)
168 sprintf(cur,"\r\nX-Spam-Status: %s, score=%s required=%s\r\n", sastatus, sascore, saoutof);
169 headerlen = strlen(buf);
170 oldmsgsize = strlen(msg->cm_fields['M']) + 1;
171 newmsgsize = headerlen + oldmsgsize;
173 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], newmsgsize);
175 memmove(msg->cm_fields['M']+headerlen,msg->cm_fields['M'],oldmsgsize);
176 memcpy(msg->cm_fields['M'],buf,headerlen);
179 CtdlLogPrintf(CTDL_DEBUG, "reject spam code used");
180 if (!strncasecmp(buf, "Spam: True", 10)) {
185 if (msg->cm_fields['0'] != NULL) {
186 free(msg->cm_fields['0']);
188 msg->cm_fields['0'] = strdup("message rejected by spam filter");
193 FreeStrBuf(&CCC->sReadBuf);
194 FreeStrBuf(&CCC->sMigrateBuf);
200 CTDL_MODULE_INIT(spam)
204 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
207 /* return our Subversion id for the Log */