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 typedef struct stringlist stringlist;
66 * Citadel protocol to manage sieve scripts.
67 * This is basically a simplified (read: doesn't resemble IMAP) version
68 * of the 'managesieve' protocol.
70 void cmd_tspam(char *argbuf) {
80 if (CtdlAccessCheck(ac_room_aide)) return;
81 if (atoi(argbuf) == 0) {
82 cprintf("%d Ok.\n", CIT_OK);
85 cprintf("%d Send info...\n", SEND_LISTING);
88 len = client_getln(buf, sizeof buf);
89 if (strcmp(buf, "000")) {
90 if (Messages == NULL) {
91 Messages = malloc (sizeof (stringlist));
95 Messages->Next = malloc (sizeof (stringlist));
96 NextMsg = NextMsg->Next;
99 NextMsg->Str = malloc (len+1);
101 memcpy (NextMsg->Str, buf, len + 1);/// maybe split spam /ham per line?
104 } while (strcmp(buf, "000"));
105 /// is there a way to filter foreachmessage by a list?
106 /* tag mails as spam or Ham */
107 /* probably do: dspam_init(ctdl_dspam_dir); dspam_process dspam_addattribute; dspam_destroy*/
108 // extract DSS_ERROR or DSS_CORPUS from the commandline. error->ham; corpus -> spam?
109 /// todo: send answer listing...
114 void ctdl_dspam_init(void) {
116 /// libdspam_init("bdb");/* <which database backend do we prefer? */
120 void dspam_do_msg(long msgnum, void *userdata)
123 DSPAM_CTX *CTX; /* DSPAM Context */
124 struct CtdlMessage *msg;
125 struct _ds_spam_signature SIG; /* signature */
127 CTX = *(DSPAM_CTX**) userdata;
128 msg = CtdlFetchMessage(msgnum, 0);
129 if (msg == NULL) return;
133 CC->redirect_buffer = malloc(SIZ);
134 CC->redirect_len = 0;
135 CC->redirect_alloc = SIZ;
136 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
137 msgtext = CC->redirect_buffer;
138 // don't need? msglen = CC->redirect_len;
139 CC->redirect_buffer = NULL;
140 CC->redirect_len = 0;
141 CC->redirect_alloc = 0;
143 /* Call DSPAM's processor with the message text */
144 if (dspam_process (CTX, msgtext) != 0)
147 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_process failed");
150 if (CTX->signature == NULL)
152 CtdlLogPrintf(CTDL_CRIT,"No signature provided\n");
156 /* Copy to a safe place */
158 SIG.data = malloc (CTX->signature->length);
159 if (SIG.data != NULL)
160 memcpy (SIG.data, CTX->signature->data, CTX->signature->length);
164 SIG.length = CTX->signature->length;
165 /* Print processing results */
166 CtdlLogPrintf (CTDL_DEBUG, "Probability: %2.4f Confidence: %2.4f, Result: %s\n",
169 (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent");
171 //// todo: put signature into the citadel message
172 //// todo: save message; destroy message.
175 int serv_dspam_room(struct ctdlroom *room)
177 DSPAM_CTX *CTX; /* DSPAM Context */
179 /* scan for spam; do */
180 /* probably do: dspam_init; dspam_process dspam_addattribute; dspam_destroy*/
182 //#define DSR_ISSPAM 0x01
183 //#define DSR_ISINNOCENT 0x02
184 // dspam_init (cc->username, NULL, ctdl_dspam_home, DSM_PROCESS,
185 // DSF_SIGNATURE | DSF_NOISE);
186 /// todo: if roomname = spam / ham -> learn!
187 if (room->QRflags & QR_PRIVATE) /* Are we sending to a private mailbox? */
190 // maybe we should better get our realname here?
191 snprintf(User, 64, "%ld", room->QRroomaide);
193 CTX = dspam_init(User,
197 DSF_SIGNATURE | DSF_NOISE);
199 /// else -> todo: global user for public rooms etc.
202 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_init failed!\n");
203 return ERROR + INTERNAL_ERROR;
205 /* Use graham and robinson algorithms, graham's p-values */
206 CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;
208 /* Use CHAIN tokenizer */
209 CTX->tokenizer = DSZ_CHAIN;
211 CtdlForEachMessage(MSGS_GT, 1, NULL, NULL, NULL,
218 void serv_dspam_shutdown (void)
220 libdspam_shutdown ();
222 #endif /* HAVE_LIBDSPAM */
224 CTDL_MODULE_INIT(dspam)
226 return "$Id: serv_dspam.c 5876 2007-12-10 23:22:03Z dothebart $" "disabled.";
232 CtdlRegisterCleanupHook(serv_dspam_shutdown);
233 CtdlRegisterProtoHook(cmd_tspam, "SPAM", "Tag Message as Spam/Ham to learn DSPAM");
235 CtdlRegisterRoomHook(serv_dspam_room);
237 ///CtdlRegisterSessionHook(perform_dspam_processing, EVT_HOUSE);
239 #else /* HAVE_LIBDSPAM */
241 CtdlLogPrintf(CTDL_INFO, "This server is missing libdspam Spam filtering will be disabled.\n");
243 #endif /* HAVE_LIBDSPAM */
246 /* return our Subversion id for the Log */
247 return "$Id: serv_dspam.c 5876 2007-12-10 23:22:03Z dothebart $";