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"
58 #include "internet_addressing.h"
61 #include "ctdl_module.h"
65 #define CONFIG_DEFAULT file_dpsam_conf
66 #define LOGDIR file_dspam_log
69 //#define HAVE_CONFIG_H
70 #include <dspam/libdspam.h>
71 //#define HAVE_CONFIG_H
73 typedef struct stringlist stringlist;
83 * Citadel protocol to manage sieve scripts.
84 * This is basically a simplified (read: doesn't resemble IMAP) version
85 * of the 'managesieve' protocol.
87 void cmd_tspam(char *argbuf) {
97 if (CtdlAccessCheck(ac_room_aide)) return;
98 if (atoi(argbuf) == 0) {
99 cprintf("%d Ok.\n", CIT_OK);
102 cprintf("%d Send info...\n", SEND_LISTING);
105 len = client_getln(buf, sizeof buf);
106 if (strcmp(buf, "000")) {
107 if (Messages == NULL) {
108 Messages = malloc (sizeof (stringlist));
112 Messages->Next = malloc (sizeof (stringlist));
113 NextMsg = NextMsg->Next;
115 NextMsg->Next = NULL;
116 NextMsg->Str = malloc (len+1);
118 memcpy (NextMsg->Str, buf, len + 1);/// maybe split spam /ham per line?
121 } while (strcmp(buf, "000"));
122 /// is there a way to filter foreachmessage by a list?
123 /* tag mails as spam or Ham */
124 /* probably do: dspam_init(ctdl_dspam_dir); dspam_process dspam_addattribute; dspam_destroy*/
125 // extract DSS_ERROR or DSS_CORPUS from the commandline. error->ham; corpus -> spam?
126 /// todo: send answer listing...
131 void ctdl_dspam_init(void) {
133 /// libdspam_init("bdb");/* <which database backend do we prefer? */
137 void dspam_do_msg(long msgnum, void *userdata)
140 DSPAM_CTX *CTX; /* DSPAM Context */
141 struct CtdlMessage *msg;
142 struct _ds_spam_signature SIG; /* signature */
144 CTX = *(DSPAM_CTX**) userdata;
145 msg = CtdlFetchMessage(msgnum, 0);
146 if (msg == NULL) return;
150 CC->redirect_buffer = malloc(SIZ);
151 CC->redirect_len = 0;
152 CC->redirect_alloc = SIZ;
153 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
154 msgtext = CC->redirect_buffer;
155 // don't need? msglen = CC->redirect_len;
156 CC->redirect_buffer = NULL;
157 CC->redirect_len = 0;
158 CC->redirect_alloc = 0;
160 /* Call DSPAM's processor with the message text */
161 if (dspam_process (CTX, msgtext) != 0)
164 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_process failed");
167 if (CTX->signature == NULL)
169 CtdlLogPrintf(CTDL_CRIT,"No signature provided\n");
173 /* Copy to a safe place */
175 msg->cm_fields['G'] = malloc (CTX->signature->length * 2);
176 CtdlEncodeBase64(msg->cm_fields['G'], CTX->signature->data, CTX->signature->length, 0);
180 SIG.length = CTX->signature->length;
181 /* Print processing results */
182 CtdlLogPrintf (CTDL_DEBUG, "Probability: %2.4f Confidence: %2.4f, Result: %s\n",
185 (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent");
187 //// todo: put signature into the citadel message
188 //// todo: save message; destroy message.
191 int serv_dspam_room(struct ctdlroom *room)
193 DSPAM_CTX *CTX; /* DSPAM Context */
195 /* scan for spam; do */
196 /* probably do: dspam_init; dspam_process dspam_addattribute; dspam_destroy*/
198 //#define DSR_ISSPAM 0x01
199 //#define DSR_ISINNOCENT 0x02
200 // dspam_init (cc->username, NULL, ctdl_dspam_home, DSM_PROCESS,
201 // DSF_SIGNATURE | DSF_NOISE);
202 /// todo: if roomname = spam / ham -> learn!
203 if ((room->QRflags & QR_PRIVATE) &&/* Are we sending to a private mailbox? */
204 (strstr(room->QRname, ".Mail")!=NULL))
208 // maybe we should better get our realname here?
209 snprintf(User, 64, "%ld", room->QRroomaide);
210 extract_token(User, room->QRname, 0, '.', sizeof(User));
211 CTX = dspam_init(User,
215 DSF_SIGNATURE | DSF_NOISE);
218 /// else -> todo: global user for public rooms etc.
221 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_init failed!\n");
222 return ERROR + INTERNAL_ERROR;
224 /* Use graham and robinson algorithms, graham's p-values */
225 CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;
227 /* Use CHAIN tokenizer */
228 CTX->tokenizer = DSZ_CHAIN;
230 CtdlForEachMessage(MSGS_GT, 1, NULL, NULL, NULL,
237 void serv_dspam_shutdown (void)
239 libdspam_shutdown ();
241 #endif /* HAVE_LIBDSPAM */
243 CTDL_MODULE_INIT(dspam)
245 return "$Id$" "disabled.";
251 CtdlRegisterCleanupHook(serv_dspam_shutdown);
252 CtdlRegisterProtoHook(cmd_tspam, "SPAM", "Tag Message as Spam/Ham to learn DSPAM");
254 CtdlRegisterRoomHook(serv_dspam_room);
256 ///CtdlRegisterSessionHook(perform_dspam_processing, EVT_HOUSE);
258 #else /* HAVE_LIBDSPAM */
260 CtdlLogPrintf(CTDL_INFO, "This server is missing libdspam Spam filtering will be disabled.\n");
262 #endif /* HAVE_LIBDSPAM */
265 /* return our Subversion id for the Log */