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