2 * $Id: serv_dspam.c 5876 2007-12-10 23:22:03Z dothebart $
4 * This module glues libDSpam to the Citadel server in order to implement
7 * This code is released under the terms of the GNU General Public License.
18 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
34 #include <libcitadel.h>
37 #include "citserver.h"
44 #include "internet_addressing.h"
47 #include "ctdl_module.h"
53 #include <dspam/libdspam.h>
56 * Citadel protocol to manage sieve scripts.
57 * This is basically a simplified (read: doesn't resemble IMAP) version
58 * of the 'managesieve' protocol.
60 void cmd_tspam(char *argbuf) {
63 /* tag mails as spam or Ham */
64 /* probably do: dspam_init(ctdl_dspam_dir); dspam_process dspam_addattribute; dspam_destroy*/
65 // extract DSS_ERROR or DSS_CORPUS from the commandline. error->ham; corpus -> spam?
70 void ctdl_dspam_init(void) {
72 libdspam_init("bdb");/* <which database backend do we prefer? */
76 void dspam_do_msg(long msgnum, void *userdata)
78 DSPAM_CTX *CTX; /* DSPAM Context */
79 struct CtdlMessage *msg;
80 struct _ds_spam_signature SIG; /* signature */
82 CTX = *(DSPAM_CTX**) userdata;
83 msg = CtdlFetchMessage(msgnum, 0);
84 if (msg == NULL) return;
85 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
87 /* Call DSPAM's processor with the message text */
88 if (dspam_process (CTX, msg->cm_fields['A']) != 0)
90 lprintf(CTDL_CRIT, "ERROR: dspam_process failed");
93 if (CTX->signature == NULL)
95 lprintf(CTDL_CRIT,"No signature provided\n");
99 /* Copy to a safe place */
101 SIG.data = malloc (CTX->signature->length);
102 if (SIG.data != NULL)
103 memcpy (SIG.data, CTX->signature->data, CTX->signature->length);
105 SIG.length = CTX->signature->length;
106 /* Print processing results */
107 printf ("Probability: %2.4f Confidence: %2.4f, Result: %s\n",
110 (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent");
112 //// todo: put signature into the citadel message
113 //// todo: save message; destroy message.
116 int serv_dspam_room(struct ctdlroom *room)
118 DSPAM_CTX *CTX; /* DSPAM Context */
120 /* scan for spam; do */
121 /* probably do: dspam_init; dspam_process dspam_addattribute; dspam_destroy*/
123 //#define DSR_ISSPAM 0x01
124 //#define DSR_ISINNOCENT 0x02
125 // dspam_init (cc->username, NULL, ctdl_dspam_home, DSM_PROCESS,
126 // DSF_SIGNATURE | DSF_NOISE);
127 /// todo: if roomname = spam / ham -> learn!
128 if (room->QRflags & QR_PRIVATE) /* Are we sending to a private mailbox? */
131 // maybe we should better get our realname here?
132 snprintf(User, 64, "%ld", room->QRroomaide);
134 CTX = dspam_init(User,
138 DSF_SIGNATURE | DSF_NOISE);
140 /// else -> todo: global user for public rooms etc.
143 lprintf(CTDL_CRIT, "ERROR: dspam_init failed!\n");
144 return ERROR + INTERNAL_ERROR;
146 /* Use graham and robinson algorithms, graham's p-values */
147 CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;
149 /* Use CHAIN tokenizer */
150 CTX->tokenizer = DSZ_CHAIN;
152 CtdlForEachMessage(MSGS_GT, NULL, NULL, NULL, NULL,
159 void serv_dspam_shutdown (void)
161 libdspam_shutdown ();
163 #endif /* HAVE_LIBDSPAM */
165 CTDL_MODULE_INIT(dspam)
172 CtdlRegisterCleanupHook(serv_dspam_shutdown);
173 CtdlRegisterProtoHook(cmd_tspam, "SPAM", "Tag Message as Spam/Ham to learn DSPAM");
175 CtdlRegisterRoomHook(serv_dspam_room);
177 ///CtdlRegisterSessionHook(perform_dspam_processing, EVT_HOUSE);
179 #else /* HAVE_LIBDSPAM */
181 lprintf(CTDL_INFO, "This server is missing libdspam Spam filtering will be disabled.\n");
183 #endif /* HAVE_LIBDSPAM */
186 /* return our Subversion id for the Log */
187 return "$Id: serv_dspam.c 5876 2007-12-10 23:22:03Z dothebart $";