4 * This module glues libDSpam to the Citadel server in order to implement
7 * Copyright (c) 2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
48 #include <libcitadel.h>
51 #include "citserver.h"
57 #include "internet_addressing.h"
60 #include "ctdl_module.h"
64 #define CONFIG_DEFAULT file_dpsam_conf
65 #define LOGDIR file_dspam_log
68 //#define HAVE_CONFIG_H
69 #include <dspam/libdspam.h>
70 //#define HAVE_CONFIG_H
72 typedef struct stringlist stringlist;
82 * Citadel protocol to manage sieve scripts.
83 * This is basically a simplified (read: doesn't resemble IMAP) version
84 * of the 'managesieve' protocol.
86 void cmd_tspam(char *argbuf) {
96 if (CtdlAccessCheck(ac_room_aide)) return;
97 if (atoi(argbuf) == 0) {
98 cprintf("%d Ok.\n", CIT_OK);
101 cprintf("%d Send info...\n", SEND_LISTING);
104 len = client_getln(buf, sizeof buf);
105 if (strcmp(buf, "000")) {
106 if (Messages == NULL) {
107 Messages = malloc (sizeof (stringlist));
111 Messages->Next = malloc (sizeof (stringlist));
112 NextMsg = NextMsg->Next;
114 NextMsg->Next = NULL;
115 NextMsg->Str = malloc (len+1);
117 memcpy (NextMsg->Str, buf, len + 1);/// maybe split spam /ham per line?
120 } while (strcmp(buf, "000"));
121 /// is there a way to filter foreachmessage by a list?
122 /* tag mails as spam or Ham */
123 /* probably do: dspam_init(ctdl_dspam_dir); dspam_process dspam_addattribute; dspam_destroy*/
124 // extract DSS_ERROR or DSS_CORPUS from the commandline. error->ham; corpus -> spam?
125 /// todo: send answer listing...
130 void ctdl_dspam_init(void) {
132 /// libdspam_init("bdb");/* <which database backend do we prefer? */
136 void dspam_do_msg(long msgnum, void *userdata)
139 DSPAM_CTX *CTX; /* DSPAM Context */
140 struct CtdlMessage *msg;
141 struct _ds_spam_signature SIG; /* signature */
143 CTX = *(DSPAM_CTX**) userdata;
144 msg = CtdlFetchMessage(msgnum, 0);
145 if (msg == NULL) return;
149 CC->redirect_buffer = malloc(SIZ);
150 CC->redirect_len = 0;
151 CC->redirect_alloc = SIZ;
152 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
153 msgtext = CC->redirect_buffer;
154 // don't need? msglen = CC->redirect_len;
155 CC->redirect_buffer = NULL;
156 CC->redirect_len = 0;
157 CC->redirect_alloc = 0;
159 /* Call DSPAM's processor with the message text */
160 if (dspam_process (CTX, msgtext) != 0)
163 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_process failed");
166 if (CTX->signature == NULL)
168 CtdlLogPrintf(CTDL_CRIT,"No signature provided\n");
172 /* Copy to a safe place */
174 msg->cm_fields['G'] = malloc (CTX->signature->length * 2);
175 CtdlEncodeBase64(msg->cm_fields['G'], CTX->signature->data, CTX->signature->length, 0);
179 SIG.length = CTX->signature->length;
180 /* Print processing results */
181 CtdlLogPrintf (CTDL_DEBUG, "Probability: %2.4f Confidence: %2.4f, Result: %s\n",
184 (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent");
186 //// todo: put signature into the citadel message
187 //// todo: save message; destroy message.
190 int serv_dspam_room(struct ctdlroom *room)
192 DSPAM_CTX *CTX; /* DSPAM Context */
194 /* scan for spam; do */
195 /* probably do: dspam_init; dspam_process dspam_addattribute; dspam_destroy*/
197 //#define DSR_ISSPAM 0x01
198 //#define DSR_ISINNOCENT 0x02
199 // dspam_init (cc->username, NULL, ctdl_dspam_home, DSM_PROCESS,
200 // DSF_SIGNATURE | DSF_NOISE);
201 /// todo: if roomname = spam / ham -> learn!
202 if ((room->QRflags & QR_PRIVATE) &&/* Are we sending to a private mailbox? */
203 (strstr(room->QRname, ".Mail")!=NULL))
207 // maybe we should better get our realname here?
208 snprintf(User, 64, "%ld", room->QRroomaide);
209 extract_token(User, room->QRname, 0, '.', sizeof(User));
210 CTX = dspam_init(User,
214 DSF_SIGNATURE | DSF_NOISE);
217 /// else -> todo: global user for public rooms etc.
220 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_init failed!\n");
221 return ERROR + INTERNAL_ERROR;
223 /* Use graham and robinson algorithms, graham's p-values */
224 CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;
226 /* Use CHAIN tokenizer */
227 CTX->tokenizer = DSZ_CHAIN;
229 CtdlForEachMessage(MSGS_GT, 1, NULL, NULL, NULL,
236 void serv_dspam_shutdown (void)
238 libdspam_shutdown ();
240 #endif /* HAVE_LIBDSPAM */
242 CTDL_MODULE_INIT(dspam)
244 return "$Id$" "disabled.";
250 CtdlRegisterCleanupHook(serv_dspam_shutdown);
251 CtdlRegisterProtoHook(cmd_tspam, "SPAM", "Tag Message as Spam/Ham to learn DSPAM");
253 CtdlRegisterRoomHook(serv_dspam_room);
255 ///CtdlRegisterSessionHook(perform_dspam_processing, EVT_HOUSE);
257 #else /* HAVE_LIBDSPAM */
259 CtdlLogPrintf(CTDL_INFO, "This server is missing libdspam Spam filtering will be disabled.\n");
261 #endif /* HAVE_LIBDSPAM */
264 /* return our Subversion id for the Log */