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, 0);
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 CtdlLogPrintf(CTDL_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
133 if (config.c_spam_flag_only) {
134 CtdlLogPrintf(CTDL_DEBUG, "flag spam code used");
144 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
145 extract_token(sascore, buf, 3, ' ', sizeof sascore);
146 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
148 sprintf(buf,"X-Spam-Level: ");
149 char *cur = buf + 14;
150 for (numscore = atoi(sascore); numscore>0; numscore--)
154 sprintf(cur,"\r\nX-Spam-Status: %s, score=%s required=%s\r\n", sastatus, sascore, saoutof);
155 headerlen = strlen(buf);
156 oldmsgsize = strlen(msg->cm_fields['M']) + 1;
157 newmsgsize = headerlen + oldmsgsize;
159 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], newmsgsize);
161 memmove(msg->cm_fields['M']+headerlen,msg->cm_fields['M'],oldmsgsize);
162 memcpy(msg->cm_fields['M'],buf,headerlen);
165 CtdlLogPrintf(CTDL_DEBUG, "reject spam code used");
166 if (!strncasecmp(buf, "Spam: True", 10)) {
171 if (msg->cm_fields['0'] != NULL) {
172 free(msg->cm_fields['0']);
174 msg->cm_fields['0'] = strdup("message rejected by spam filter");
184 CTDL_MODULE_INIT(spam)
188 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
191 /* return our Subversion id for the Log */