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