2005-03-04 [paul] 1.0.1cvs21
[claws.git] / src / filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto & The Sylpheed Claws Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21 #include <ctype.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <gtk/gtk.h>
26 #include <stdio.h>
27 #include "intl.h"
28 #include "utils.h"
29 #include "procheader.h"
30 #include "matcher.h"
31 #include "filtering.h"
32 #include "prefs_gtk.h"
33 #include "compose.h"
34
35 #define PREFSBUFSIZE            1024
36
37 GSList * pre_global_processing = NULL;
38 GSList * post_global_processing = NULL;
39 GSList * filtering_rules = NULL;
40
41 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
42
43 #define STRLEN_WITH_CHECK(expr) \
44         strlen_with_check(#expr, __LINE__, expr)
45                 
46 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
47 {
48         if (str) 
49                 return strlen(str);
50         else {
51                 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
52                 return 0;
53         }
54 }
55
56 FilteringAction * filteringaction_new(int type, int account_id,
57                                       gchar * destination,
58                                       gint labelcolor, gint score)
59 {
60         FilteringAction * action;
61
62         action = g_new0(FilteringAction, 1);
63
64         action->type = type;
65         action->account_id = account_id;
66         if (destination) {
67                 action->destination       = g_strdup(destination);
68         } else {
69                 action->destination       = NULL;
70         }
71         action->labelcolor = labelcolor;        
72         action->score = score;
73         return action;
74 }
75
76 void filteringaction_free(FilteringAction * action)
77 {
78         g_return_if_fail(action);
79         if (action->destination)
80                 g_free(action->destination);
81         g_free(action);
82 }
83
84 FilteringProp * filteringprop_new(MatcherList * matchers,
85                                   GSList * action_list)
86 {
87         FilteringProp * filtering;
88
89         filtering = g_new0(FilteringProp, 1);
90         filtering->matchers = matchers;
91         filtering->action_list = action_list;
92
93         return filtering;
94 }
95
96 static FilteringAction * filteringaction_copy(FilteringAction * src)
97 {
98         FilteringAction * new;
99         
100         new = g_new0(FilteringAction, 1);
101         
102         new->type = src->type;
103         new->account_id = src->account_id;
104         if (src->destination)
105                 new->destination = g_strdup(src->destination);
106         else 
107                 new->destination = NULL;
108         new->labelcolor = src->labelcolor;
109
110         return new;
111 }
112
113 FilteringProp * filteringprop_copy(FilteringProp *src)
114 {
115         FilteringProp * new;
116         GSList *tmp;
117         
118         new = g_new0(FilteringProp, 1);
119         new->matchers = g_new0(MatcherList, 1);
120
121         for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
122                 MatcherProp *matcher = (MatcherProp *)tmp->data;
123                 
124                 new->matchers->matchers = g_slist_append(new->matchers->matchers,
125                                                    matcherprop_copy(matcher));
126                 tmp = tmp->next;
127         }
128
129         new->matchers->bool_and = src->matchers->bool_and;
130
131         new->action_list = NULL;
132
133         for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
134                 FilteringAction *filtering_action;
135                 
136                 filtering_action = tmp->data;
137                 
138                 new->action_list = g_slist_append(new->action_list,
139                     filteringaction_copy(filtering_action));
140         }
141
142         return new;
143 }
144
145 void filteringprop_free(FilteringProp * prop)
146 {
147         GSList * tmp;
148
149         g_return_if_fail(prop);
150         matcherlist_free(prop->matchers);
151         
152         for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
153                 filteringaction_free(tmp->data);
154         }
155         g_free(prop);
156 }
157
158 /*
159   fitleringaction_apply
160   runs the action on one MsgInfo
161   return value : return TRUE if the action could be applied
162 */
163
164 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
165 {
166         FolderItem * dest_folder;
167         gint val;
168         Compose * compose;
169         PrefsAccount * account;
170         gchar * cmd;
171
172         switch(action->type) {
173         case MATCHACTION_MOVE:
174                 dest_folder =
175                         folder_find_item_from_identifier(action->destination);
176                 if (!dest_folder) {
177                         debug_print("*** folder not found '%s'\n",
178                                 action->destination ?action->destination :"");
179                         return FALSE;
180                 }
181                 
182                 if (folder_item_move_msg(dest_folder, info) == -1) {
183                         debug_print("*** could not move message\n");
184                         return FALSE;
185                 }       
186
187                 return TRUE;
188
189         case MATCHACTION_COPY:
190                 dest_folder =
191                         folder_find_item_from_identifier(action->destination);
192
193                 if (!dest_folder) {
194                         debug_print("*** folder not found '%s'\n",
195                                 action->destination ?action->destination :"");
196                         return FALSE;
197                 }
198
199                 if (folder_item_copy_msg(dest_folder, info) == -1)
200                         return FALSE;
201
202                 return TRUE;
203
204         case MATCHACTION_DELETE:
205                 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
206                         return FALSE;
207                 return TRUE;
208
209         case MATCHACTION_MARK:
210                 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
211                 return TRUE;
212
213         case MATCHACTION_UNMARK:
214                 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
215                 return TRUE;
216
217         case MATCHACTION_LOCK:
218                 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
219                 return TRUE;
220
221         case MATCHACTION_UNLOCK:
222                 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);       
223                 return TRUE;
224                 
225         case MATCHACTION_MARK_AS_READ:
226                 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
227                 return TRUE;
228
229         case MATCHACTION_MARK_AS_UNREAD:
230                 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
231                 return TRUE;
232         
233         case MATCHACTION_COLOR:
234                 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0); 
235                 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
236                 return TRUE;
237
238         case MATCHACTION_FORWARD:
239         case MATCHACTION_FORWARD_AS_ATTACHMENT:
240                 account = account_find_from_id(action->account_id);
241                 compose = compose_forward(account, info,
242                         action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
243                         NULL, TRUE);
244                 compose_entry_append(compose, action->destination,
245                                      compose->account->protocol == A_NNTP
246                                             ? COMPOSE_NEWSGROUPS
247                                             : COMPOSE_TO);
248
249                 val = compose_send(compose);
250
251                 return val == 0 ? TRUE : FALSE;
252
253         case MATCHACTION_REDIRECT:
254                 account = account_find_from_id(action->account_id);
255                 compose = compose_redirect(account, info);
256                 if (compose->account->protocol == A_NNTP)
257                         break;
258                 else
259                         compose_entry_append(compose, action->destination,
260                                              COMPOSE_TO);
261
262                 val = compose_send(compose);
263                 
264                 return val == 0 ? TRUE : FALSE;
265
266         case MATCHACTION_EXECUTE:
267                 cmd = matching_build_command(action->destination, info);
268                 if (cmd == NULL)
269                         return FALSE;
270                 else {
271                         system(cmd);
272                         g_free(cmd);
273                 }
274                 return TRUE;
275
276         case MATCHACTION_SET_SCORE:
277                 info->score = action->score;
278                 return TRUE;
279
280         case MATCHACTION_CHANGE_SCORE:
281                 info->score += action->score;
282                 return TRUE;
283
284         case MATCHACTION_STOP:
285                 return FALSE;
286
287         case MATCHACTION_HIDE:
288                 info->hidden = TRUE;
289                 return TRUE;
290
291         default:
292                 break;
293         }
294         return FALSE;
295 }
296
297 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
298 {
299         GSList *p;
300         g_return_val_if_fail(action_list, FALSE);
301         g_return_val_if_fail(info, FALSE);
302         for (p = action_list; p && p->data; p = g_slist_next(p)) {
303                 FilteringAction *a = (FilteringAction *) p->data;
304                 if (filteringaction_apply(a, info)) {
305                         if (filtering_is_final_action(a))
306                                 break;
307                 } else
308                         return FALSE;
309                 
310         }
311         return TRUE;
312 }
313
314 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
315 {
316         return matcherlist_match(filtering->matchers, info);
317 }
318
319 /*!
320  *\brief        Apply a rule on message.
321  *
322  *\param        filtering List of filtering rules.
323  *\param        info Message to apply rules on.
324  *\param        final Variable returning TRUE or FALSE if one of the
325  *              encountered actions was final. 
326  *              See also \ref filtering_is_final_action.
327  *
328  *\return       gboolean TRUE to continue applying rules.
329  */
330 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
331     gboolean * final)
332 {
333         gboolean result = TRUE;
334         gchar    buf[50];
335         GSList * tmp;
336         
337         * final = FALSE;
338         for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
339                 FilteringAction * action;
340                 
341                 action = tmp->data;
342                 
343                 if (FALSE == (result = filteringaction_apply(action, info))) {
344                         g_warning("No further processing after rule %s\n",
345                             filteringaction_to_string(buf, sizeof buf, action));
346                 }
347                 
348                 if (filtering_is_final_action(action)) {
349                         * final = TRUE;
350                         break;
351                 }
352         }
353         return result;
354 }
355
356 /*!
357  *\brief        Check if an action is "final", i.e. should break further
358  *              processing.
359  *
360  *\param        filtering_action Action to check.
361  *
362  *\return       gboolean TRUE if \a filtering_action is final.  
363  */
364 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
365 {
366         switch(filtering_action->type) {
367         case MATCHACTION_MOVE:
368         case MATCHACTION_DELETE:
369         case MATCHACTION_STOP:
370                 return TRUE; /* MsgInfo invalid for message */
371         default:
372                 return FALSE;
373         }
374 }
375
376 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
377 {
378         GSList  *l;
379         gboolean final;
380         gboolean apply_next;
381         
382         g_return_val_if_fail(info != NULL, TRUE);
383         
384         for (l = filtering_list, final = FALSE, apply_next = FALSE; l != NULL; l = g_slist_next(l)) {
385                 FilteringProp * filtering = (FilteringProp *) l->data;
386
387                 if (filtering_match_condition(filtering, info)) {
388                         apply_next = filtering_apply_rule(filtering, info, &final);
389                         if (final)
390                                 break;
391                 }               
392         }
393
394         /* put in inbox if a final rule could not be applied, or
395          * the last rule was not a final one. */
396         if ((final && !apply_next) || !final) {
397                 return FALSE;
398         }
399
400         return TRUE;
401 }
402
403 /*!
404  *\brief        Filter a message against a list of rules.
405  *
406  *\param        flist List of filter rules.
407  *\param        info Message.
408  *
409  *\return       gboolean TRUE if filter rules handled the message.
410  *
411  *\note         Returning FALSE means the message was not handled,
412  *              and that the calling code should do the default
413  *              processing. E.g. \ref inc.c::inc_start moves the 
414  *              message to the inbox.   
415  */
416 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
417 {
418         return filter_msginfo(flist, info);
419 }
420
421 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
422 {
423         const gchar *command_str;
424         gchar * quoted_dest;
425         
426         command_str = get_matchparser_tab_str(action->type);
427
428         if (command_str == NULL)
429                 return NULL;
430
431         switch(action->type) {
432         case MATCHACTION_MOVE:
433         case MATCHACTION_COPY:
434         case MATCHACTION_EXECUTE:
435                 quoted_dest = matcher_quote_str(action->destination);
436                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
437                 g_free(quoted_dest);
438                 return dest;
439
440         case MATCHACTION_DELETE:
441         case MATCHACTION_MARK:
442         case MATCHACTION_UNMARK:
443         case MATCHACTION_LOCK:
444         case MATCHACTION_UNLOCK:
445         case MATCHACTION_MARK_AS_READ:
446         case MATCHACTION_MARK_AS_UNREAD:
447         case MATCHACTION_STOP:
448         case MATCHACTION_HIDE:
449                 g_snprintf(dest, destlen, "%s", command_str);
450                 return dest;
451
452         case MATCHACTION_REDIRECT:
453         case MATCHACTION_FORWARD:
454         case MATCHACTION_FORWARD_AS_ATTACHMENT:
455                 quoted_dest = matcher_quote_str(action->destination);
456                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
457                 g_free(quoted_dest);
458                 return dest; 
459
460         case MATCHACTION_COLOR:
461                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
462                 return dest;  
463
464         case MATCHACTION_CHANGE_SCORE:
465         case MATCHACTION_SET_SCORE:
466                 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
467                 return dest;  
468
469         default:
470                 return NULL;
471         }
472 }
473
474 gchar * filteringaction_list_to_string(GSList * action_list)
475 {
476         gchar *action_list_str;
477         gchar  buf[256];
478         GSList * tmp;
479         gchar *list_str;
480
481         action_list_str = NULL;
482         for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
483                 gchar *action_str;
484                 FilteringAction * action;
485                 
486                 action = tmp->data;
487                 
488                 action_str = filteringaction_to_string(buf,
489                     sizeof buf, action);
490                 
491                 if (action_list_str != NULL) {
492                         list_str = g_strconcat(action_list_str, " ", action_str, NULL);
493                         g_free(action_list_str);
494                 }
495                 else {
496                         list_str = g_strdup(action_str);
497                 }
498                 action_list_str = list_str;
499         }
500
501         return action_list_str;
502 }
503
504 gchar * filteringprop_to_string(FilteringProp * prop)
505 {
506         gchar *list_str;
507         gchar *action_list_str;
508         gchar *filtering_str;
509
510         action_list_str = filteringaction_list_to_string(prop->action_list);
511
512         if (action_list_str == NULL)
513                 return NULL;
514
515         list_str = matcherlist_to_string(prop->matchers);
516
517         if (list_str == NULL) {
518                 g_free(action_list_str);
519                 return NULL;
520         }
521
522         filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
523         g_free(action_list_str);
524         g_free(list_str);
525
526         return filtering_str;
527 }
528
529 void prefs_filtering_free(GSList * prefs_filtering)
530 {
531         while (prefs_filtering != NULL) {
532                 FilteringProp * filtering = (FilteringProp *)
533                         prefs_filtering->data;
534                 filteringprop_free(filtering);
535                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
536         }
537 }
538
539 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
540 {
541         FolderItem *item = node->data;
542
543         g_return_val_if_fail(item, FALSE);
544         g_return_val_if_fail(item->prefs, FALSE);
545
546         prefs_filtering_free(item->prefs->processing);
547         item->prefs->processing = NULL;
548
549         return FALSE;
550 }
551
552 void prefs_filtering_clear(void)
553 {
554         GList * cur;
555
556         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
557                 Folder *folder;
558
559                 folder = (Folder *) cur->data;
560                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
561                                 prefs_filtering_free_func, NULL);
562         }
563
564         prefs_filtering_free(filtering_rules);
565         filtering_rules = NULL;
566         prefs_filtering_free(pre_global_processing);
567         pre_global_processing = NULL;
568         prefs_filtering_free(post_global_processing);
569         post_global_processing = NULL;
570 }
571
572 void prefs_filtering_clear_folder(Folder *folder)
573 {
574         g_return_if_fail(folder);
575         g_return_if_fail(folder->node);
576
577         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
578                         prefs_filtering_free_func, NULL);
579         /* FIXME: Note folder settings were changed, where the updates? */
580 }
581