ba7eca9859bfdf852940405be2109cc718982e05
[claws.git] / src / filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "defs.h"
21 #include <glib.h>
22 #include <glib/gi18n.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <gtk/gtk.h>
28 #include <stdio.h>
29
30 #include "utils.h"
31 #include "procheader.h"
32 #include "matcher.h"
33 #include "filtering.h"
34 #include "prefs_gtk.h"
35 #include "compose.h"
36 #include "prefs_common.h"
37
38 #define PREFSBUFSIZE            1024
39
40 GSList * pre_global_processing = NULL;
41 GSList * post_global_processing = NULL;
42 GSList * filtering_rules = NULL;
43
44 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
45
46 #define STRLEN_WITH_CHECK(expr) \
47         strlen_with_check(#expr, __LINE__, expr)
48                 
49 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
50 {
51         if (str) 
52                 return strlen(str);
53         else {
54                 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
55                 return 0;
56         }
57 }
58
59 FilteringAction * filteringaction_new(int type, int account_id,
60                                       gchar * destination,
61                                       gint labelcolor, gint score)
62 {
63         FilteringAction * action;
64
65         action = g_new0(FilteringAction, 1);
66
67         action->type = type;
68         action->account_id = account_id;
69         if (destination) {
70                 action->destination       = g_strdup(destination);
71         } else {
72                 action->destination       = NULL;
73         }
74         action->labelcolor = labelcolor;        
75         action->score = score;
76         return action;
77 }
78
79 void filteringaction_free(FilteringAction * action)
80 {
81         g_return_if_fail(action);
82         g_free(action->destination);
83         g_free(action);
84 }
85
86 FilteringProp * filteringprop_new(gboolean enabled,
87                                   const gchar *name,
88                                   gint account_id,
89                                   MatcherList * matchers,
90                                   GSList * action_list)
91 {
92         FilteringProp * filtering;
93
94         filtering = g_new0(FilteringProp, 1);
95         filtering->enabled = enabled;
96         filtering->name = name ? g_strdup(name): NULL;
97         filtering->account_id = account_id;
98         filtering->matchers = matchers;
99         filtering->action_list = action_list;
100
101         return filtering;
102 }
103
104 static FilteringAction * filteringaction_copy(FilteringAction * src)
105 {
106         FilteringAction * new;
107         
108         new = g_new0(FilteringAction, 1);
109         
110         new->type = src->type;
111         new->account_id = src->account_id;
112         if (src->destination)
113                 new->destination = g_strdup(src->destination);
114         else 
115                 new->destination = NULL;
116         new->labelcolor = src->labelcolor;
117         new->score = src->score;
118
119         return new;
120 }
121
122 FilteringProp * filteringprop_copy(FilteringProp *src)
123 {
124         FilteringProp * new;
125         GSList *tmp;
126         
127         new = g_new0(FilteringProp, 1);
128         new->matchers = g_new0(MatcherList, 1);
129
130         for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
131                 MatcherProp *matcher = (MatcherProp *)tmp->data;
132                 
133                 new->matchers->matchers = g_slist_append(new->matchers->matchers,
134                                                    matcherprop_copy(matcher));
135                 tmp = tmp->next;
136         }
137
138         new->matchers->bool_and = src->matchers->bool_and;
139
140         new->action_list = NULL;
141
142         for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
143                 FilteringAction *filtering_action;
144                 
145                 filtering_action = tmp->data;
146                 
147                 new->action_list = g_slist_append(new->action_list,
148                     filteringaction_copy(filtering_action));
149         }
150
151         new->enabled = src->enabled;
152         new->name = g_strdup(src->name);
153
154         return new;
155 }
156
157 void filteringprop_free(FilteringProp * prop)
158 {
159         GSList * tmp;
160
161         g_return_if_fail(prop);
162         matcherlist_free(prop->matchers);
163         
164         for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
165                 filteringaction_free(tmp->data);
166         }
167         g_free(prop->name);
168         g_free(prop);
169 }
170
171 void filtering_move_and_copy_msg(MsgInfo *msginfo)
172 {
173         GSList *list = g_slist_append(NULL, msginfo);
174         filtering_move_and_copy_msgs(list);
175         g_slist_free(list);
176 }
177
178 /* move and copy messages by batches to be faster on IMAP */
179 void filtering_move_and_copy_msgs(GSList *msgs)
180 {
181         GSList *messages = g_slist_copy(msgs);
182         FolderItem *last_item = NULL;
183         gboolean is_copy = FALSE, is_move = FALSE;
184         debug_print("checking %d messages\n", g_slist_length(msgs));
185         while (messages) {
186                 GSList *batch = NULL, *cur;
187                 gint found = 0;
188                 for (cur = messages; cur; cur = cur->next) {
189                         MsgInfo *info = (MsgInfo *)cur->data;
190                         if (last_item == NULL) {
191                                 last_item = info->to_filter_folder;
192                         }
193                         if (last_item == NULL)
194                                 continue;
195                         if (!is_copy && !is_move) {
196                                 if (info->is_copy)
197                                         is_copy = TRUE;
198                                 else if (info->is_move)
199                                         is_move = TRUE;
200                         }
201                         found++;
202                         if (info->to_filter_folder == last_item 
203                         &&  info->is_copy == is_copy
204                         &&  info->is_move == is_move) {
205                                 batch = g_slist_prepend(batch, info);
206                         }
207                 }
208                 if (found == 0) {
209                         debug_print("no more messages to move/copy\n");
210                         break;
211                 } else {
212                         debug_print("%d messages to %s in %s\n", found,
213                                 is_copy ? "copy":"move", last_item->name ? last_item->name:"(noname)");
214                 }
215                 for (cur = batch; cur; cur = cur->next) {
216                         MsgInfo *info = (MsgInfo *)cur->data;
217                         messages = g_slist_remove(messages, info);
218                 }
219                 batch = g_slist_reverse(batch);
220                 if (g_slist_length(batch)) {
221                         MsgInfo *info = (MsgInfo *)batch->data;
222                         if (is_copy && last_item != info->folder) {
223                                 folder_item_copy_msgs(last_item, batch);
224                         } else if (is_move && last_item != info->folder) {
225                                 if (folder_item_move_msgs(last_item, batch) < 0)
226                                         folder_item_move_msgs(
227                                                 folder_get_default_inbox(), 
228                                                 batch);
229                         }
230                         /* we don't reference the msginfos, because caller will do */
231                         g_slist_free(batch);
232                         batch = NULL;
233                 }
234                 last_item = NULL;
235                 is_copy = FALSE;
236                 is_move = FALSE;
237         }
238         /* we don't reference the msginfos, because caller will do */
239         g_slist_free(messages);
240 }
241
242 /*
243   fitleringaction_apply
244   runs the action on one MsgInfo
245   return value : return TRUE if the action could be applied
246 */
247
248 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
249 {
250         FolderItem * dest_folder;
251         gint val;
252         Compose * compose;
253         PrefsAccount * account;
254         gchar * cmd;
255
256         switch(action->type) {
257         case MATCHACTION_MOVE:
258                 dest_folder =
259                         folder_find_item_from_identifier(action->destination);
260                 if (!dest_folder) {
261                         debug_print("*** folder not found '%s'\n",
262                                 action->destination ?action->destination :"");
263                         return FALSE;
264                 }
265                 
266                 /* check if mail is set to copy already, 
267                  * in which case we have to do it */
268                 if (info->is_copy && info->to_filter_folder) {
269                         debug_print("should cp and mv !\n");
270                         folder_item_copy_msg(info->to_filter_folder, info);
271                         info->is_copy = FALSE;
272                 }
273                 /* mark message to be moved */          
274                 info->is_move = TRUE;
275                 info->to_filter_folder = dest_folder;
276                 return TRUE;
277
278         case MATCHACTION_COPY:
279                 dest_folder =
280                         folder_find_item_from_identifier(action->destination);
281
282                 if (!dest_folder) {
283                         debug_print("*** folder not found '%s'\n",
284                                 action->destination ?action->destination :"");
285                         return FALSE;
286                 }
287
288                 /* check if mail is set to copy already, 
289                  * in which case we have to do it */
290                 if (info->is_copy && info->to_filter_folder) {
291                         debug_print("should cp and mv !\n");
292                         folder_item_copy_msg(info->to_filter_folder, info);
293                         info->is_copy = FALSE;
294                 }
295                 /* mark message to be copied */         
296                 info->is_copy = TRUE;
297                 info->to_filter_folder = dest_folder;
298                 return TRUE;
299
300         case MATCHACTION_DELETE:
301                 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
302                         return FALSE;
303                 return TRUE;
304
305         case MATCHACTION_MARK:
306                 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
307                 return TRUE;
308
309         case MATCHACTION_UNMARK:
310                 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
311                 return TRUE;
312
313         case MATCHACTION_LOCK:
314                 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
315                 return TRUE;
316
317         case MATCHACTION_UNLOCK:
318                 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);       
319                 return TRUE;
320                 
321         case MATCHACTION_MARK_AS_READ:
322                 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
323                 return TRUE;
324
325         case MATCHACTION_MARK_AS_UNREAD:
326                 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
327                 return TRUE;
328         
329         case MATCHACTION_MARK_AS_SPAM:
330                 procmsg_spam_learner_learn(info, NULL, TRUE);
331                 procmsg_msginfo_change_flags(info, MSG_SPAM, 0, MSG_NEW|MSG_UNREAD, 0);
332                 if (procmsg_spam_get_folder(info)) {
333                         info->is_move = TRUE;
334                         info->to_filter_folder = procmsg_spam_get_folder(info);
335                 }
336                 return TRUE;
337
338         case MATCHACTION_MARK_AS_HAM:
339                 procmsg_spam_learner_learn(info, NULL, FALSE);
340                 return TRUE;
341         
342         case MATCHACTION_COLOR:
343                 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0); 
344                 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
345                 return TRUE;
346
347         case MATCHACTION_FORWARD:
348         case MATCHACTION_FORWARD_AS_ATTACHMENT:
349                 account = account_find_from_id(action->account_id);
350                 compose = compose_forward(account, info,
351                         action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
352                         NULL, TRUE, TRUE);
353                 compose_entry_append(compose, action->destination,
354                                      compose->account->protocol == A_NNTP
355                                             ? COMPOSE_NEWSGROUPS
356                                             : COMPOSE_TO);
357
358                 val = compose_send(compose);
359
360                 return val == 0 ? TRUE : FALSE;
361
362         case MATCHACTION_REDIRECT:
363                 account = account_find_from_id(action->account_id);
364                 compose = compose_redirect(account, info, TRUE);
365                 if (compose->account->protocol == A_NNTP)
366                         break;
367                 else
368                         compose_entry_append(compose, action->destination,
369                                              COMPOSE_TO);
370
371                 val = compose_send(compose);
372                 
373                 return val == 0 ? TRUE : FALSE;
374
375         case MATCHACTION_EXECUTE:
376                 cmd = matching_build_command(action->destination, info);
377                 if (cmd == NULL)
378                         return FALSE;
379                 else {
380                         system(cmd);
381                         g_free(cmd);
382                 }
383                 return TRUE;
384
385         case MATCHACTION_SET_SCORE:
386                 info->score = action->score;
387                 return TRUE;
388
389         case MATCHACTION_CHANGE_SCORE:
390                 info->score += action->score;
391                 return TRUE;
392
393         case MATCHACTION_STOP:
394                 return FALSE;
395
396         case MATCHACTION_HIDE:
397                 info->hidden = TRUE;
398                 return TRUE;
399
400         case MATCHACTION_IGNORE:
401                 procmsg_msginfo_set_flags(info, MSG_IGNORE_THREAD, 0);
402                 return TRUE;
403
404         default:
405                 break;
406         }
407         return FALSE;
408 }
409
410 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
411 {
412         GSList *p;
413         g_return_val_if_fail(action_list, FALSE);
414         g_return_val_if_fail(info, FALSE);
415         for (p = action_list; p && p->data; p = g_slist_next(p)) {
416                 FilteringAction *a = (FilteringAction *) p->data;
417                 if (filteringaction_apply(a, info)) {
418                         if (filtering_is_final_action(a))
419                                 break;
420                 } else
421                         return FALSE;
422                 
423         }
424         return TRUE;
425 }
426
427 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info,
428                                                         PrefsAccount *ac_prefs)
429 {
430         gboolean matches = FALSE;
431
432         if (ac_prefs != NULL) {
433                 matches = ((filtering->account_id == 0)
434                                         || (filtering->account_id == ac_prefs->account_id));
435         } else {
436                 switch (prefs_common.apply_per_account_filtering_rules) {
437                 case FILTERING_ACCOUNT_RULES_FORCE:
438                         /* apply filtering rules regardless to the account info */
439                         matches = TRUE;
440                         break;
441                 case FILTERING_ACCOUNT_RULES_SKIP:
442                         /* don't apply filtering rules that belong to an account */
443                         matches = (filtering->account_id == 0);
444                         break;
445                 case FILTERING_ACCOUNT_RULES_USE_CURRENT:
446                         matches = ((filtering->account_id == 0)
447                                         || (filtering->account_id == cur_account->account_id));
448                         break;
449                 }
450         }
451
452         return matches && matcherlist_match(filtering->matchers, info);
453 }
454
455 /*!
456  *\brief        Apply a rule on message.
457  *
458  *\param        filtering List of filtering rules.
459  *\param        info Message to apply rules on.
460  *\param        final Variable returning TRUE or FALSE if one of the
461  *              encountered actions was final. 
462  *              See also \ref filtering_is_final_action.
463  *
464  *\return       gboolean TRUE to continue applying rules.
465  */
466 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
467     gboolean * final)
468 {
469         gboolean result = TRUE;
470         gchar    buf[256];
471         GSList * tmp;
472         
473         * final = FALSE;
474         for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
475                 FilteringAction * action;
476                 
477                 action = tmp->data;
478                 
479                 if (FALSE == (result = filteringaction_apply(action, info))) {
480                         g_warning("No further processing after rule %s\n",
481                             filteringaction_to_string(buf, sizeof buf, action));
482                 }
483                 
484                 if (filtering_is_final_action(action)) {
485                         * final = TRUE;
486                         break;
487                 }
488         }
489         return result;
490 }
491
492 /*!
493  *\brief        Check if an action is "final", i.e. should break further
494  *              processing.
495  *
496  *\param        filtering_action Action to check.
497  *
498  *\return       gboolean TRUE if \a filtering_action is final.  
499  */
500 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
501 {
502         switch(filtering_action->type) {
503         case MATCHACTION_MOVE:
504         case MATCHACTION_DELETE:
505         case MATCHACTION_STOP:
506         case MATCHACTION_MARK_AS_SPAM:
507                 return TRUE; /* MsgInfo invalid for message */
508         default:
509                 return FALSE;
510         }
511 }
512
513 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info, PrefsAccount* ac_prefs)
514 {
515         GSList  *l;
516         gboolean final;
517         gboolean apply_next;
518         
519         g_return_val_if_fail(info != NULL, TRUE);
520         
521         for (l = filtering_list, final = FALSE, apply_next = FALSE; l != NULL; l = g_slist_next(l)) {
522                 FilteringProp * filtering = (FilteringProp *) l->data;
523
524                 if (filtering->enabled && filtering_match_condition(filtering, info, ac_prefs)) {
525                         apply_next = filtering_apply_rule(filtering, info, &final);
526                         if (final)
527                                 break;
528                 }               
529         }
530
531         /* put in inbox if a final rule could not be applied, or
532          * the last rule was not a final one. */
533         if ((final && !apply_next) || !final) {
534                 return FALSE;
535         }
536
537         return TRUE;
538 }
539
540 /*!
541  *\brief        Filter a message against a list of rules.
542  *
543  *\param        flist List of filter rules.
544  *\param        info Message.
545  *
546  *\return       gboolean TRUE if filter rules handled the message.
547  *
548  *\note         Returning FALSE means the message was not handled,
549  *              and that the calling code should do the default
550  *              processing. E.g. \ref inc.c::inc_start moves the 
551  *              message to the inbox.   
552  */
553 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info, PrefsAccount* ac_prefs)
554 {
555         return filter_msginfo(flist, info, ac_prefs);
556 }
557
558 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
559 {
560         const gchar *command_str;
561         gchar * quoted_dest;
562         
563         command_str = get_matchparser_tab_str(action->type);
564
565         if (command_str == NULL)
566                 return NULL;
567
568         switch(action->type) {
569         case MATCHACTION_MOVE:
570         case MATCHACTION_COPY:
571         case MATCHACTION_EXECUTE:
572                 quoted_dest = matcher_quote_str(action->destination);
573                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
574                 g_free(quoted_dest);
575                 return dest;
576
577         case MATCHACTION_DELETE:
578         case MATCHACTION_MARK:
579         case MATCHACTION_UNMARK:
580         case MATCHACTION_LOCK:
581         case MATCHACTION_UNLOCK:
582         case MATCHACTION_MARK_AS_READ:
583         case MATCHACTION_MARK_AS_UNREAD:
584         case MATCHACTION_MARK_AS_SPAM:
585         case MATCHACTION_MARK_AS_HAM:
586         case MATCHACTION_STOP:
587         case MATCHACTION_HIDE:
588         case MATCHACTION_IGNORE:
589                 g_snprintf(dest, destlen, "%s", command_str);
590                 return dest;
591
592         case MATCHACTION_REDIRECT:
593         case MATCHACTION_FORWARD:
594         case MATCHACTION_FORWARD_AS_ATTACHMENT:
595                 quoted_dest = matcher_quote_str(action->destination);
596                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
597                 g_free(quoted_dest);
598                 return dest; 
599
600         case MATCHACTION_COLOR:
601                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
602                 return dest;  
603
604         case MATCHACTION_CHANGE_SCORE:
605         case MATCHACTION_SET_SCORE:
606                 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
607                 return dest;  
608
609         default:
610                 return NULL;
611         }
612 }
613
614 gchar * filteringaction_list_to_string(GSList * action_list)
615 {
616         gchar *action_list_str;
617         gchar  buf[256];
618         GSList * tmp;
619         gchar *list_str;
620
621         action_list_str = NULL;
622         for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
623                 gchar *action_str;
624                 FilteringAction * action;
625                 
626                 action = tmp->data;
627                 
628                 action_str = filteringaction_to_string(buf,
629                     sizeof buf, action);
630                 
631                 if (action_list_str != NULL) {
632                         list_str = g_strconcat(action_list_str, " ", action_str, NULL);
633                         g_free(action_list_str);
634                 }
635                 else {
636                         list_str = g_strdup(action_str);
637                 }
638                 action_list_str = list_str;
639         }
640
641         return action_list_str;
642 }
643
644 gchar * filteringprop_to_string(FilteringProp * prop)
645 {
646         gchar *list_str;
647         gchar *action_list_str;
648         gchar *filtering_str;
649
650         action_list_str = filteringaction_list_to_string(prop->action_list);
651
652         if (action_list_str == NULL)
653                 return NULL;
654
655         list_str = matcherlist_to_string(prop->matchers);
656
657         if (list_str == NULL) {
658                 g_free(action_list_str);
659                 return NULL;
660         }
661
662         filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
663         g_free(action_list_str);
664         g_free(list_str);
665
666         return filtering_str;
667 }
668
669 void prefs_filtering_free(GSList * prefs_filtering)
670 {
671         while (prefs_filtering != NULL) {
672                 FilteringProp * filtering = (FilteringProp *)
673                         prefs_filtering->data;
674                 filteringprop_free(filtering);
675                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
676         }
677 }
678
679 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
680 {
681         FolderItem *item = node->data;
682
683         g_return_val_if_fail(item, FALSE);
684         g_return_val_if_fail(item->prefs, FALSE);
685
686         prefs_filtering_free(item->prefs->processing);
687         item->prefs->processing = NULL;
688
689         return FALSE;
690 }
691
692 void prefs_filtering_clear(void)
693 {
694         GList * cur;
695
696         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
697                 Folder *folder;
698
699                 folder = (Folder *) cur->data;
700                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
701                                 prefs_filtering_free_func, NULL);
702         }
703
704         prefs_filtering_free(filtering_rules);
705         filtering_rules = NULL;
706         prefs_filtering_free(pre_global_processing);
707         pre_global_processing = NULL;
708         prefs_filtering_free(post_global_processing);
709         post_global_processing = NULL;
710 }
711
712 void prefs_filtering_clear_folder(Folder *folder)
713 {
714         g_return_if_fail(folder);
715         g_return_if_fail(folder->node);
716
717         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
718                         prefs_filtering_free_func, NULL);
719         /* FIXME: Note folder settings were changed, where the updates? */
720 }
721
722 gboolean filtering_peek_per_account_rules(GSList *filtering_list)
723 /* return TRUE if there's at least one per-account filtering rule */
724 {
725         GSList *l;
726
727         for (l = filtering_list; l != NULL; l = g_slist_next(l)) {
728                 FilteringProp * filtering = (FilteringProp *) l->data;
729
730                 if (filtering->enabled && (filtering->account_id != 0)) {
731                         return TRUE;
732                 }               
733         }
734
735         return FALSE;
736 }