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 phree(msg->cm_fields['0']);
88 msg->cm_fields['0'] = strdoop("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) {
109 /* For users who have authenticated to this server we never want to
110 * apply spam filtering, because presumably they're trustworthy.
112 if (CC->logged_in) return(0);
114 /* See if we have any SpamAssassin hosts configured */
115 num_sahosts = get_hosts(sahosts, "spamassassin");
116 if (num_sahosts < 1) return(0);
118 /* Try them one by one until we get a working one */
119 for (sa=0; sa<num_sahosts; ++sa) {
120 extract(buf, sahosts, sa);
121 lprintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
122 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
123 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
127 /* If the service isn't running, just pass the mail
128 * through. Potentially throwing away mails isn't good.
134 lprintf(CTDL_DEBUG, "Transmitting command\n");
135 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
136 sock_write(sock, buf, strlen(buf));
139 CtdlRedirectOutput(NULL, sock);
140 CtdlOutputPreLoadedMsg(msg, 0L, MT_RFC822, HEADERS_ALL, 0, 1);
141 CtdlRedirectOutput(NULL, -1);
143 /* Close one end of the socket connection; this tells SpamAssassin
146 sock_shutdown(sock, SHUT_WR);
149 lprintf(CTDL_DEBUG, "Awaiting response\n");
150 if (sock_gets(sock, buf) < 0) {
153 lprintf(CTDL_DEBUG, "<%s\n", buf);
154 if (strncasecmp(buf, "SPAMD", 5)) {
157 if (sock_gets(sock, buf) < 0) {
160 lprintf(CTDL_DEBUG, "<%s\n", buf);
161 if (!strncasecmp(buf, "Spam: True", 10)) {
166 if (msg->cm_fields['0'] != NULL) {
167 phree(msg->cm_fields['0']);
169 msg->cm_fields['0'] = strdoop(
170 "5.7.1 Message rejected by SpamAssassin");
179 char *serv_spam_init(void)
182 /* (disabled built-in scanner, see above)
183 CtdlRegisterMessageHook(spam_filter, EVT_SMTPSCAN);
186 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);