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"
56 #include "internet_addressing.h"
59 #include "ctdl_module.h"
63 #define CONFIG_DEFAULT file_dpsam_conf
64 #define LOGDIR file_dspam_log
67 //#define HAVE_CONFIG_H
68 #include <dspam/libdspam.h>
69 //#define HAVE_CONFIG_H
71 typedef struct stringlist stringlist;
81 * Citadel protocol to manage sieve scripts.
82 * This is basically a simplified (read: doesn't resemble IMAP) version
83 * of the 'managesieve' protocol.
85 void cmd_tspam(char *argbuf) {
95 if (CtdlAccessCheck(ac_room_aide)) return;
96 if (atoi(argbuf) == 0) {
97 cprintf("%d Ok.\n", CIT_OK);
100 cprintf("%d Send info...\n", SEND_LISTING);
103 len = client_getln(buf, sizeof buf);
104 if (strcmp(buf, "000")) {
105 if (Messages == NULL) {
106 Messages = malloc (sizeof (stringlist));
110 Messages->Next = malloc (sizeof (stringlist));
111 NextMsg = NextMsg->Next;
113 NextMsg->Next = NULL;
114 NextMsg->Str = malloc (len+1);
116 memcpy (NextMsg->Str, buf, len + 1);/// maybe split spam /ham per line?
119 } while (strcmp(buf, "000"));
120 /// is there a way to filter foreachmessage by a list?
121 /* tag mails as spam or Ham */
122 /* probably do: dspam_init(ctdl_dspam_dir); dspam_process dspam_addattribute; dspam_destroy*/
123 // extract DSS_ERROR or DSS_CORPUS from the commandline. error->ham; corpus -> spam?
124 /// todo: send answer listing...
129 void ctdl_dspam_init(void) {
131 /// libdspam_init("bdb");/* <which database backend do we prefer? */
135 void dspam_do_msg(long msgnum, void *userdata)
138 DSPAM_CTX *CTX; /* DSPAM Context */
139 struct CtdlMessage *msg;
140 struct _ds_spam_signature SIG; /* signature */
142 CTX = *(DSPAM_CTX**) userdata;
143 msg = CtdlFetchMessage(msgnum, 0);
144 if (msg == NULL) return;
148 CC->redirect_buffer = malloc(SIZ);
149 CC->redirect_len = 0;
150 CC->redirect_alloc = SIZ;
151 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
152 msgtext = CC->redirect_buffer;
153 // don't need? msglen = CC->redirect_len;
154 CC->redirect_buffer = NULL;
155 CC->redirect_len = 0;
156 CC->redirect_alloc = 0;
158 /* Call DSPAM's processor with the message text */
159 if (dspam_process (CTX, msgtext) != 0)
162 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_process failed");
165 if (CTX->signature == NULL)
167 CtdlLogPrintf(CTDL_CRIT,"No signature provided\n");
171 /* Copy to a safe place */
173 msg->cm_fields['G'] = malloc (CTX->signature->length * 2);
174 CtdlEncodeBase64(msg->cm_fields['G'], CTX->signature->data, CTX->signature->length, 0);
178 SIG.length = CTX->signature->length;
179 /* Print processing results */
180 CtdlLogPrintf (CTDL_DEBUG, "Probability: %2.4f Confidence: %2.4f, Result: %s\n",
183 (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent");
185 //// todo: put signature into the citadel message
186 //// todo: save message; destroy message.
189 int serv_dspam_room(struct ctdlroom *room)
191 DSPAM_CTX *CTX; /* DSPAM Context */
193 /* scan for spam; do */
194 /* probably do: dspam_init; dspam_process dspam_addattribute; dspam_destroy*/
196 //#define DSR_ISSPAM 0x01
197 //#define DSR_ISINNOCENT 0x02
198 // dspam_init (cc->username, NULL, ctdl_dspam_home, DSM_PROCESS,
199 // DSF_SIGNATURE | DSF_NOISE);
200 /// todo: if roomname = spam / ham -> learn!
201 if ((room->QRflags & QR_PRIVATE) &&/* Are we sending to a private mailbox? */
202 (strstr(room->QRname, ".Mail")!=NULL))
206 // maybe we should better get our realname here?
207 snprintf(User, 64, "%ld", room->QRroomaide);
208 extract_token(User, room->QRname, 0, '.', sizeof(User));
209 CTX = dspam_init(User,
213 DSF_SIGNATURE | DSF_NOISE);
216 /// else -> todo: global user for public rooms etc.
219 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_init failed!\n");
220 return ERROR + INTERNAL_ERROR;
222 /* Use graham and robinson algorithms, graham's p-values */
223 CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;
225 /* Use CHAIN tokenizer */
226 CTX->tokenizer = DSZ_CHAIN;
228 CtdlForEachMessage(MSGS_GT, 1, NULL, NULL, NULL,
235 void serv_dspam_shutdown (void)
237 libdspam_shutdown ();
239 #endif /* HAVE_LIBDSPAM */
241 CTDL_MODULE_INIT(dspam)
243 return "$Id$" "disabled.";
249 CtdlRegisterCleanupHook(serv_dspam_shutdown);
250 CtdlRegisterProtoHook(cmd_tspam, "SPAM", "Tag Message as Spam/Ham to learn DSPAM");
252 CtdlRegisterRoomHook(serv_dspam_room);
254 ///CtdlRegisterSessionHook(perform_dspam_processing, EVT_HOUSE);
256 #else /* HAVE_LIBDSPAM */
258 CtdlLogPrintf(CTDL_INFO, "This server is missing libdspam Spam filtering will be disabled.\n");
260 #endif /* HAVE_LIBDSPAM */
263 /* return our Subversion id for the Log */