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