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