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>
39 #include "sysdep_decls.h"
40 #include "citserver.h"
44 #include "serv_extensions.h"
51 #include "internet_addressing.h"
53 #include "clientsocket.h"
58 * Connect to the SpamAssassin server and scan a message.
60 int spam_assassin(struct CtdlMessage *msg) {
70 /* For users who have authenticated to this server we never want to
71 * apply spam filtering, because presumably they're trustworthy.
73 if (CC->logged_in) return(0);
75 /* See if we have any SpamAssassin hosts configured */
76 num_sahosts = get_hosts(sahosts, "spamassassin");
77 if (num_sahosts < 1) return(0);
79 /* Try them one by one until we get a working one */
80 for (sa=0; sa<num_sahosts; ++sa) {
81 extract_token(buf, sahosts, sa, '|', sizeof buf);
82 lprintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
83 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
84 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
88 /* If the service isn't running, just pass the mail
89 * through. Potentially throwing away mails isn't good.
95 lprintf(CTDL_DEBUG, "Transmitting command\n");
96 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
97 sock_write(sock, buf, strlen(buf));
100 CC->redirect_buffer = malloc(SIZ);
101 CC->redirect_len = 0;
102 CC->redirect_alloc = SIZ;
103 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
104 msgtext = CC->redirect_buffer;
105 msglen = CC->redirect_len;
106 CC->redirect_buffer = NULL;
107 CC->redirect_len = 0;
108 CC->redirect_alloc = 0;
110 sock_write(sock, msgtext, msglen);
113 /* Close one end of the socket connection; this tells SpamAssassin
116 sock_shutdown(sock, SHUT_WR);
119 lprintf(CTDL_DEBUG, "Awaiting response\n");
120 if (sock_gets(sock, buf) < 0) {
123 lprintf(CTDL_DEBUG, "<%s\n", buf);
124 if (strncasecmp(buf, "SPAMD", 5)) {
127 if (sock_gets(sock, buf) < 0) {
130 lprintf(CTDL_DEBUG, "<%s\n", buf);
131 if (!strncasecmp(buf, "Spam: True", 10)) {
136 if (msg->cm_fields['0'] != NULL) {
137 free(msg->cm_fields['0']);
139 msg->cm_fields['0'] = strdup("5.7.1 message rejected by spam filter");
148 char *serv_spam_init(void)
150 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
152 /* return our Subversion id for the Log */