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