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