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-2015 by the citadel.org team
9 * This program is open source software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #define SPAMASSASSIN_PORT "783"
28 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <libcitadel.h>
37 #include "citserver.h"
44 #include "internet_addressing.h"
46 #include "clientsocket.h"
49 #include "ctdl_module.h"
54 * Connect to the SpamAssassin server and scan a message.
56 int spam_assassin(struct CtdlMessage *msg, struct recptypes *recp) {
66 /* For users who have authenticated to this server we never want to
67 * apply spam filtering, because presumably they're trustworthy.
69 if (CC->logged_in) return(0);
71 /* See if we have any SpamAssassin hosts configured */
72 num_sahosts = get_hosts(sahosts, "spamassassin");
73 if (num_sahosts < 1) return(0);
75 /* Try them one by one until we get a working one */
76 for (sa=0; sa<num_sahosts; ++sa) {
77 extract_token(buf, sahosts, sa, '|', sizeof buf);
78 syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
79 sock = sock_connect(buf, SPAMASSASSIN_PORT);
80 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
84 /* If the service isn't running, just pass the mail
85 * through. Potentially throwing away mails isn't good.
90 CCC->SBuf.Buf = NewStrBuf();
91 CCC->sMigrateBuf = NewStrBuf();
92 CCC->SBuf.ReadWritePointer = NULL;
95 syslog(LOG_DEBUG, "Transmitting command\n");
96 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
97 sock_write(&sock, buf, strlen(buf));
100 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
101 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
102 msgtext = CC->redirect_buffer;
103 CC->redirect_buffer = NULL;
105 sock_write(&sock, SKEY(msgtext));
106 FreeStrBuf(&msgtext);
108 /* Close one end of the socket connection; this tells SpamAssassin
112 sock_shutdown(sock, SHUT_WR);
115 syslog(LOG_DEBUG, "Awaiting response\n");
116 if (sock_getln(&sock, buf, sizeof buf) < 0) {
119 syslog(LOG_DEBUG, "<%s\n", buf);
120 if (strncasecmp(buf, "SPAMD", 5)) {
123 if (sock_getln(&sock, buf, sizeof buf) < 0) {
126 syslog(LOG_DEBUG, "<%s\n", buf);
127 syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", CtdlGetConfigInt("c_spam_flag_only"));
128 if (CtdlGetConfigInt("c_spam_flag_only")) {
136 syslog(LOG_DEBUG, "flag spam code used");
138 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
139 extract_token(sascore, buf, 3, ' ', sizeof sascore);
140 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
142 memcpy(buf, HKEY("X-Spam-Level: "));
144 for (numscore = atoi(sascore); numscore>0; numscore--)
148 headerlen = cur - buf;
149 headerlen += snprintf(cur, (sizeof(buf) - headerlen),
150 "\r\nX-Spam-Status: %s, score=%s required=%s\r\n",
151 sastatus, sascore, saoutof);
153 CM_PrependToField(msg, eMesageText, buf, headerlen);
156 syslog(LOG_DEBUG, "reject spam code used");
157 if (!strncasecmp(buf, "Spam: True", 10)) {
162 CM_SetField(msg, eErrorMsg, HKEY("message rejected by spam filter"));
167 FreeStrBuf(&CCC->SBuf.Buf);
168 FreeStrBuf(&CCC->sMigrateBuf);
174 CTDL_MODULE_INIT(spam)
178 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
181 /* return our module name for the log */