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 * This is a scanner I had started writing before deciding to just farm the
59 * job out to SpamAssassin. It *does* work but it's not in use. We've
60 * commented it out so it doesn't even compile.
62 #ifdef ___NOT_CURRENTLY_IN_USE___
63 /* Scan a message for spam */
64 int spam_filter(struct CtdlMessage *msg) {
65 int spam_strings_found = 0;
66 struct spamstrings_t *sptr;
69 /* Bail out if there's no message text */
70 if (msg->cm_fields['M'] == NULL) return(0);
74 ptr = msg->cm_fields['M'];
75 while (ptr++[0] != 0) {
76 for (sptr = spamstrings; sptr != NULL; sptr = sptr->next) {
77 if (!strncasecmp(ptr, sptr->string,
78 strlen(sptr->string))) {
84 if (spam_strings_found) {
85 if (msg->cm_fields['0'] != NULL) {
86 free(msg->cm_fields['0']);
88 msg->cm_fields['0'] = strdup("Unsolicited spam rejected");
89 return(spam_strings_found);
99 * Connect to the SpamAssassin server and scan a message.
101 int spam_assassin(struct CtdlMessage *msg) {
111 /* For users who have authenticated to this server we never want to
112 * apply spam filtering, because presumably they're trustworthy.
114 if (CC->logged_in) return(0);
116 /* See if we have any SpamAssassin hosts configured */
117 num_sahosts = get_hosts(sahosts, "spamassassin");
118 if (num_sahosts < 1) return(0);
120 /* Try them one by one until we get a working one */
121 for (sa=0; sa<num_sahosts; ++sa) {
122 extract_token(buf, sahosts, sa, '|', sizeof buf);
123 lprintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
124 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
125 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
129 /* If the service isn't running, just pass the mail
130 * through. Potentially throwing away mails isn't good.
136 lprintf(CTDL_DEBUG, "Transmitting command\n");
137 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
138 sock_write(sock, buf, strlen(buf));
141 CC->redirect_buffer = malloc(SIZ);
142 CC->redirect_len = 0;
143 CC->redirect_alloc = SIZ;
144 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
145 msgtext = CC->redirect_buffer;
146 msglen = CC->redirect_len;
147 CC->redirect_buffer = NULL;
148 CC->redirect_len = 0;
149 CC->redirect_alloc = 0;
151 sock_write(sock, msgtext, msglen);
154 /* Close one end of the socket connection; this tells SpamAssassin
157 sock_shutdown(sock, SHUT_WR);
160 lprintf(CTDL_DEBUG, "Awaiting response\n");
161 if (sock_gets(sock, buf) < 0) {
164 lprintf(CTDL_DEBUG, "<%s\n", buf);
165 if (strncasecmp(buf, "SPAMD", 5)) {
168 if (sock_gets(sock, buf) < 0) {
171 lprintf(CTDL_DEBUG, "<%s\n", buf);
172 if (!strncasecmp(buf, "Spam: True", 10)) {
177 if (msg->cm_fields['0'] != NULL) {
178 free(msg->cm_fields['0']);
180 msg->cm_fields['0'] = strdup(
181 "5.7.1 Message rejected by SpamAssassin");
190 char *serv_spam_init(void)
193 /* (disabled built-in scanner, see above)
194 CtdlRegisterMessageHook(spam_filter, EVT_SMTPSCAN);
197 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);