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