40290ad63c2fcee641b5cf84ee21f28a67927f21
[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 <ctype.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <gtk/gtk.h>
26 #include <stdio.h>
27 #include "intl.h"
28 #include "utils.h"
29 #include "procheader.h"
30 #include "matcher.h"
31 #include "filtering.h"
32 #include "prefs_gtk.h"
33 #include "compose.h"
34
35 #define PREFSBUFSIZE            1024
36
37 GSList * pre_global_processing = NULL;
38 GSList * post_global_processing = NULL;
39 GSList * filtering_rules = NULL;
40
41 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
42
43 #define STRLEN_WITH_CHECK(expr) \
44         strlen_with_check(#expr, __LINE__, expr)
45                 
46 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
47 {
48         if (str) 
49                 return strlen(str);
50         else {
51                 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
52                 return 0;
53         }
54 }
55
56 FilteringAction * filteringaction_new(int type, int account_id,
57                                       gchar * destination,
58                                       gint labelcolor, gint score)
59 {
60         FilteringAction * action;
61
62         action = g_new0(FilteringAction, 1);
63
64         action->type = type;
65         action->account_id = account_id;
66         if (destination) {
67                 action->destination       = g_strdup(destination);
68         } else {
69                 action->destination       = NULL;
70         }
71         action->labelcolor = labelcolor;        
72         action->score = score;
73         return action;
74 }
75
76 void filteringaction_free(FilteringAction * action)
77 {
78         g_return_if_fail(action);
79         if (action->destination)
80                 g_free(action->destination);
81         g_free(action);
82 }
83
84 FilteringProp * filteringprop_new(MatcherList * matchers,
85                                   GSList * action_list)
86 {
87         FilteringProp * filtering;
88
89         filtering = g_new0(FilteringProp, 1);
90         filtering->matchers = matchers;
91         filtering->action_list = action_list;
92
93         return filtering;
94 }
95
96 static FilteringAction * filteringaction_copy(FilteringAction * src)
97 {
98         FilteringAction * new;
99         
100         new = g_new0(FilteringAction, 1);
101         
102         new->type = src->type;
103         new->account_id = src->account_id;
104         if (src->destination)
105                 new->destination = g_strdup(src->destination);
106         else 
107                 new->destination = NULL;
108         new->labelcolor = src->labelcolor;
109
110         return new;
111 }
112
113 FilteringProp * filteringprop_copy(FilteringProp *src)
114 {
115         FilteringProp * new;
116         GSList *tmp;
117         
118         new = g_new0(FilteringProp, 1);
119         new->matchers = g_new0(MatcherList, 1);
120
121         for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
122                 MatcherProp *matcher = (MatcherProp *)tmp->data;
123                 
124                 new->matchers->matchers = g_slist_append(new->matchers->matchers,
125                                                    matcherprop_copy(matcher));
126                 tmp = tmp->next;
127         }
128
129         new->matchers->bool_and = src->matchers->bool_and;
130
131         new->action_list = NULL;
132
133         for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
134                 FilteringAction *filtering_action;
135                 
136                 filtering_action = tmp->data;
137                 
138                 new->action_list = g_slist_append(new->action_list,
139                     filteringaction_copy(filtering_action));
140         }
141
142         return new;
143 }
144
145 void filteringprop_free(FilteringProp * prop)
146 {
147         GSList * tmp;
148
149         g_return_if_fail(prop);
150         matcherlist_free(prop->matchers);
151         
152         for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
153                 filteringaction_free(tmp->data);
154         }
155         g_free(prop);
156 }
157
158 /*
159   fitleringaction_apply
160   runs the action on one MsgInfo
161   return value : return TRUE if the action could be applied
162 */
163
164 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
165 {
166         FolderItem * dest_folder;
167         gint val;
168         Compose * compose;
169         PrefsAccount * account;
170         gchar * cmd;
171
172         switch(action->type) {
173         case MATCHACTION_MOVE:
174                 dest_folder =
175                         folder_find_item_from_identifier(action->destination);
176                 if (!dest_folder)
177                         return FALSE;
178                 
179                 if (folder_item_move_msg(dest_folder, info) == -1) {
180                         debug_print("*** could not move message\n");
181                         return FALSE;
182                 }       
183
184                 return TRUE;
185
186         case MATCHACTION_COPY:
187                 dest_folder =
188                         folder_find_item_from_identifier(action->destination);
189
190                 if (!dest_folder)
191                         return FALSE;
192
193                 if (folder_item_copy_msg(dest_folder, info) == -1)
194                         return FALSE;
195
196                 return TRUE;
197
198         case MATCHACTION_DELETE:
199                 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
200                         return FALSE;
201                 return TRUE;
202
203         case MATCHACTION_MARK:
204                 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
205                 return TRUE;
206
207         case MATCHACTION_UNMARK:
208                 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
209                 return TRUE;
210
211         case MATCHACTION_LOCK:
212                 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
213                 return TRUE;
214
215         case MATCHACTION_UNLOCK:
216                 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);       
217                 return TRUE;
218                 
219         case MATCHACTION_MARK_AS_READ:
220                 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
221                 return TRUE;
222
223         case MATCHACTION_MARK_AS_UNREAD:
224                 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
225                 return TRUE;
226         
227         case MATCHACTION_COLOR:
228                 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0); 
229                 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
230                 return TRUE;
231
232         case MATCHACTION_FORWARD:
233         case MATCHACTION_FORWARD_AS_ATTACHMENT:
234                 account = account_find_from_id(action->account_id);
235                 compose = compose_forward(account, info,
236                         action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
237                         NULL, TRUE);
238                 compose_entry_append(compose, action->destination,
239                                      compose->account->protocol == A_NNTP
240                                             ? COMPOSE_NEWSGROUPS
241                                             : COMPOSE_TO);
242
243                 val = compose_send(compose);
244                 gtk_widget_destroy(compose->window);
245
246                 return val == 0 ? TRUE : FALSE;
247
248         case MATCHACTION_REDIRECT:
249                 account = account_find_from_id(action->account_id);
250                 compose = compose_redirect(account, info);
251                 if (compose->account->protocol == A_NNTP)
252                         break;
253                 else
254                         compose_entry_append(compose, action->destination,
255                                              COMPOSE_TO);
256
257                 val = compose_send(compose);
258                 gtk_widget_destroy(compose->window);
259                 
260                 return val == 0 ? TRUE : FALSE;
261
262         case MATCHACTION_EXECUTE:
263                 cmd = matching_build_command(action->destination, info);
264                 if (cmd == NULL)
265                         return FALSE;
266                 else {
267                         system(cmd);
268                         g_free(cmd);
269                 }
270                 return TRUE;
271
272         case MATCHACTION_SET_SCORE:
273                 info->score = action->score;
274                 return TRUE;
275
276         case MATCHACTION_ADD_SCORE:
277                 info->score += action->score;
278                 return TRUE;
279
280         case MATCHACTION_STOP:
281                 return TRUE;
282
283         case MATCHACTION_HIDE:
284                 info->hidden = TRUE;
285                 return TRUE;
286
287         default:
288                 break;
289         }
290         return FALSE;
291 }
292
293 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
294 {
295         GSList *p;
296         g_return_val_if_fail(action_list, FALSE);
297         g_return_val_if_fail(info, FALSE);
298         for (p = action_list; p && p->data; p = g_slist_next(p)) {
299                 FilteringAction *a = (FilteringAction *) p->data;
300                 if (filteringaction_apply(a, info)) {
301                         if (filtering_is_final_action(a))
302                                 break;
303                 } else
304                         return FALSE;
305                 
306         }
307         return TRUE;
308 }
309
310 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
311 {
312         return matcherlist_match(filtering->matchers, info);
313 }
314
315 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
316     gboolean * final)
317 {
318         gboolean result;
319         gchar    buf[50];
320         GSList * tmp;
321         
322         * final = FALSE;
323         for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
324                 FilteringAction * action;
325                 
326                 action = tmp->data;
327                 
328                 if (FALSE == (result = filteringaction_apply(action, info))) {
329                         g_warning("action %s could not be applied", 
330                             filteringaction_to_string(buf, sizeof buf, action));
331                 }
332                 
333                 if (filtering_is_final_action(action)) {
334                         * final = TRUE;
335                         break;
336                 }
337         }
338         return result;
339 }
340
341 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
342 {
343         switch(filtering_action->type) {
344         case MATCHACTION_MOVE:
345         case MATCHACTION_DELETE:
346         case MATCHACTION_STOP:
347                 return TRUE; /* MsgInfo invalid for message */
348         default:
349                 return FALSE;
350         }
351 }
352
353 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
354 {
355         GSList  *l;
356         gboolean final;
357         gboolean applied;
358         
359         g_return_val_if_fail(info != NULL, TRUE);
360         
361         for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
362                 FilteringProp * filtering = (FilteringProp *) l->data;
363
364                 if (filtering_match_condition(filtering, info)) {
365                         applied = filtering_apply_rule(filtering, info, &final);
366                         if (final)
367                                 break;
368                 }               
369         }
370
371         /* put in inbox if a final rule could not be applied, or
372          * the last rule was not a final one. */
373         if ((final && !applied) || !final) {
374                 return FALSE;
375         }
376
377         return TRUE;
378 }
379
380 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
381 {
382         return filter_msginfo(flist, info);
383 }
384
385 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
386 {
387         const gchar *command_str;
388         gchar * quoted_dest;
389         
390         command_str = get_matchparser_tab_str(action->type);
391
392         if (command_str == NULL)
393                 return NULL;
394
395         switch(action->type) {
396         case MATCHACTION_MOVE:
397         case MATCHACTION_COPY:
398         case MATCHACTION_EXECUTE:
399                 quoted_dest = matcher_quote_str(action->destination);
400                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
401                 g_free(quoted_dest);
402                 return dest;
403
404         case MATCHACTION_DELETE:
405         case MATCHACTION_MARK:
406         case MATCHACTION_UNMARK:
407         case MATCHACTION_LOCK:
408         case MATCHACTION_UNLOCK:
409         case MATCHACTION_MARK_AS_READ:
410         case MATCHACTION_MARK_AS_UNREAD:
411         case MATCHACTION_STOP:
412         case MATCHACTION_HIDE:
413                 g_snprintf(dest, destlen, "%s", command_str);
414                 return dest;
415
416         case MATCHACTION_REDIRECT:
417         case MATCHACTION_FORWARD:
418         case MATCHACTION_FORWARD_AS_ATTACHMENT:
419                 quoted_dest = matcher_quote_str(action->destination);
420                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
421                 g_free(quoted_dest);
422                 return dest; 
423
424         case MATCHACTION_COLOR:
425                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
426                 return dest;  
427
428         case MATCHACTION_ADD_SCORE:
429         case MATCHACTION_SET_SCORE:
430                 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
431                 return dest;  
432
433         default:
434                 return NULL;
435         }
436 }
437
438 gchar * filteringaction_list_to_string(GSList * action_list)
439 {
440         gchar *action_list_str;
441         gchar  buf[256];
442         GSList * tmp;
443         gchar *list_str;
444
445         action_list_str = NULL;
446         for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
447                 gchar *action_str;
448                 FilteringAction * action;
449                 
450                 action = tmp->data;
451                 
452                 action_str = filteringaction_to_string(buf,
453                     sizeof buf, action);
454                 
455                 if (action_list_str != NULL) {
456                         list_str = g_strconcat(action_list_str, " ", action_str, NULL);
457                         g_free(action_list_str);
458                 }
459                 else {
460                         list_str = g_strdup(action_str);
461                 }
462                 action_list_str = list_str;
463         }
464
465         return action_list_str;
466 }
467
468 gchar * filteringprop_to_string(FilteringProp * prop)
469 {
470         gchar *list_str;
471         gchar *action_list_str;
472         gchar *filtering_str;
473
474         action_list_str = filteringaction_list_to_string(prop->action_list);
475
476         if (action_list_str == NULL)
477                 return NULL;
478
479         list_str = matcherlist_to_string(prop->matchers);
480
481         if (list_str == NULL) {
482                 g_free(action_list_str);
483                 return NULL;
484         }
485
486         filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
487         g_free(action_list_str);
488         g_free(list_str);
489
490         return filtering_str;
491 }
492
493 void prefs_filtering_free(GSList * prefs_filtering)
494 {
495         while (prefs_filtering != NULL) {
496                 FilteringProp * filtering = (FilteringProp *)
497                         prefs_filtering->data;
498                 filteringprop_free(filtering);
499                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
500         }
501 }
502
503 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
504 {
505         FolderItem *item = node->data;
506
507         g_return_val_if_fail(item, FALSE);
508         g_return_val_if_fail(item->prefs, FALSE);
509
510         prefs_filtering_free(item->prefs->processing);
511         item->prefs->processing = NULL;
512
513         return FALSE;
514 }
515
516 void prefs_filtering_clear(void)
517 {
518         GList * cur;
519
520         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
521                 Folder *folder;
522
523                 folder = (Folder *) cur->data;
524                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
525                                 prefs_filtering_free_func, NULL);
526         }
527
528         prefs_filtering_free(filtering_rules);
529         filtering_rules = NULL;
530         prefs_filtering_free(pre_global_processing);
531         pre_global_processing = NULL;
532         prefs_filtering_free(post_global_processing);
533         post_global_processing = NULL;
534 }
535
536 void prefs_filtering_clear_folder(Folder *folder)
537 {
538         g_return_if_fail(folder);
539         g_return_if_fail(folder->node);
540
541         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
542                         prefs_filtering_free_func, NULL);
543         /* FIXME: Note folder settings were changed, where the updates? */
544 }
545