Citadel API clean up.
[citadel.git] / citadel / modules / dspam / serv_dspam.c
1 /*
2  * $Id$
3  *
4  * This module glues libDSpam to the Citadel server in order to implement
5  * DSPAM Spamchecking 
6  *
7  * Copyright (c) 2009 by the citadel.org team
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "policy.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "internet_addressing.h"
58
59
60 #include "ctdl_module.h"
61
62
63 #ifdef HAVE_LIBDSPAM
64 #define CONFIG_DEFAULT file_dpsam_conf
65 #define LOGDIR file_dspam_log
66
67
68 //#define HAVE_CONFIG_H
69 #include <dspam/libdspam.h>
70 //#define HAVE_CONFIG_H
71
72 typedef struct stringlist stringlist;
73
74 struct stringlist {
75         char *Str;
76         long len;
77         stringlist *Next;
78 };
79         
80
81 /*
82  * Citadel protocol to manage sieve scripts.
83  * This is basically a simplified (read: doesn't resemble IMAP) version
84  * of the 'managesieve' protocol.
85  */
86 void cmd_tspam(char *argbuf) {
87         char buf[SIZ];
88         long len;
89         long count;
90         stringlist *Messages; 
91         stringlist *NextMsg; 
92
93         Messages = NULL;
94         NextMsg = NULL;
95         count = 0;
96         if (CtdlAccessCheck(ac_room_aide)) return;
97         if (atoi(argbuf) == 0) {
98                 cprintf("%d Ok.\n", CIT_OK);
99                 return;
100         }
101         cprintf("%d Send info...\n", SEND_LISTING);
102
103         do {
104                 len = client_getln(buf, sizeof buf);
105                 if (strcmp(buf, "000")) {
106                         if (Messages == NULL) {
107                                 Messages = malloc (sizeof (stringlist));
108                                 NextMsg = Messages;
109                         }
110                         else {
111                                 Messages->Next = malloc (sizeof (stringlist));
112                                 NextMsg = NextMsg->Next;
113                         }
114                         NextMsg->Next = NULL;
115                         NextMsg->Str = malloc (len+1);
116                         NextMsg->len = len;
117                         memcpy (NextMsg->Str, buf, len + 1);/// maybe split spam /ham per line?
118                         count++;
119                 }
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...
126 }
127
128
129
130 void ctdl_dspam_init(void) {
131
132 ///     libdspam_init("bdb");/* <which database backend do we prefer? */
133
134 }
135
136 void dspam_do_msg(long msgnum, void *userdata) 
137 {
138         char *msgtext;
139         DSPAM_CTX *CTX;                 /* DSPAM Context */
140         struct CtdlMessage *msg;
141         struct _ds_spam_signature SIG;        /* signature */
142
143         CTX = *(DSPAM_CTX**) userdata;
144         msg = CtdlFetchMessage(msgnum, 0);
145         if (msg == NULL) return;
146
147
148         /* Message */
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;
158
159         /* Call DSPAM's processor with the message text */
160         if (dspam_process (CTX, msgtext) != 0)
161         {
162                 free(msgtext);
163                 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_process failed");
164                 return;
165         }
166         if (CTX->signature == NULL)
167         {
168                 CtdlLogPrintf(CTDL_CRIT,"No signature provided\n");
169         }
170         else
171         {
172 /* Copy to a safe place */
173
174                 msg->cm_fields['G'] = malloc (CTX->signature->length * 2);
175                 CtdlEncodeBase64(msg->cm_fields['G'], CTX->signature->data, CTX->signature->length, 0);
176         }
177         free(msgtext);
178
179         SIG.length = CTX->signature->length;
180         /* Print processing results */
181         CtdlLogPrintf (CTDL_DEBUG, "Probability: %2.4f Confidence: %2.4f, Result: %s\n",
182                 CTX->probability,
183                 CTX->confidence,
184                 (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent");
185
186         //// todo: put signature into the citadel message
187         //// todo: save message; destroy message.
188 }
189
190 int serv_dspam_room(struct ctdlroom *room)
191 {
192         DSPAM_CTX *CTX;                 /* DSPAM Context */
193
194         /* scan for spam; do */
195         /* probably do: dspam_init; dspam_process dspam_addattribute; dspam_destroy*/
196 //DSS_NONE
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))
204
205         {
206                 char User[64];
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, 
211                                  NULL,
212                                  ctdl_dspam_dir, 
213                                  DSM_PROCESS, 
214                                  DSF_SIGNATURE | DSF_NOISE);
215         }
216         else return 0;//// 
217         /// else -> todo: global user for public rooms etc.
218         if (CTX == NULL)
219         {
220                 CtdlLogPrintf(CTDL_CRIT, "ERROR: dspam_init failed!\n");
221                 return ERROR + INTERNAL_ERROR;
222         }
223         /* Use graham and robinson algorithms, graham's p-values */
224         CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;
225
226         /* Use CHAIN tokenizer */
227         CTX->tokenizer = DSZ_CHAIN;
228
229         CtdlForEachMessage(MSGS_GT, 1, NULL, NULL, NULL,
230                            dspam_do_msg,
231                            (void *) &CTX);
232
233         return 0;
234 }
235
236 void serv_dspam_shutdown (void)
237 {
238         libdspam_shutdown ();
239 }
240 #endif  /* HAVE_LIBDSPAM */
241
242 CTDL_MODULE_INIT(dspam)
243 {
244         return "$Id$" "disabled.";
245         if (!threading)
246         {
247 #ifdef HAVE_LIBDSPAM
248
249                 ctdl_dspam_init();
250                 CtdlRegisterCleanupHook(serv_dspam_shutdown);
251                 CtdlRegisterProtoHook(cmd_tspam, "SPAM", "Tag Message as Spam/Ham to learn DSPAM");
252
253                 CtdlRegisterRoomHook(serv_dspam_room);
254
255                 ///CtdlRegisterSessionHook(perform_dspam_processing, EVT_HOUSE);
256
257 #else   /* HAVE_LIBDSPAM */
258
259                 CtdlLogPrintf(CTDL_INFO, "This server is missing libdspam Spam filtering will be disabled.\n");
260
261 #endif  /* HAVE_LIBDSPAM */
262         }
263         
264         /* return our Subversion id for the Log */
265         return "$Id$";
266 }
267