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