Implemented a progress bar that counts the number of completed tasks when the action...
[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 * 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         /* NOTE:
61          * if type is MATCHACTION_CHANGE_SCORE, account_id = (-1, 0, 1) and
62          * labelcolor = the score value change
63          */
64
65         action->type = type;
66         action->account_id = account_id;
67         if (destination) {
68                 action->destination       = g_strdup(destination);
69                 action->unesc_destination = matcher_unescape_str(g_strdup(destination));
70         } else {
71                 action->destination       = NULL;
72                 action->unesc_destination = NULL;
73         }
74         action->labelcolor = labelcolor;        
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         if (action->unesc_destination)
84                 g_free(action->unesc_destination);
85         g_free(action);
86 }
87
88 FilteringProp * filteringprop_new(MatcherList * matchers,
89                                   FilteringAction * action)
90 {
91         FilteringProp * filtering;
92
93         filtering = g_new0(FilteringProp, 1);
94         filtering->matchers = matchers;
95         filtering->action = action;
96
97         return filtering;
98 }
99
100 FilteringProp * filteringprop_copy(FilteringProp *src)
101 {
102         FilteringProp * new;
103         GSList *tmp;
104         
105         new = g_new0(FilteringProp, 1);
106         new->matchers = g_new0(MatcherList, 1);
107         new->action = g_new0(FilteringAction, 1);
108         for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
109                 MatcherProp *matcher = (MatcherProp *)tmp->data;
110                 
111                 new->matchers->matchers = g_slist_append(new->matchers->matchers,
112                                                    matcherprop_copy(matcher));
113                 tmp = tmp->next;
114         }
115         new->matchers->bool_and = src->matchers->bool_and;
116         new->action->type = src->action->type;
117         new->action->account_id = src->action->account_id;
118         if (src->action->destination)
119                 new->action->destination = g_strdup(src->action->destination);
120         else 
121                 new->action->destination = NULL;
122         if (src->action->unesc_destination)
123                 new->action->unesc_destination = g_strdup(src->action->unesc_destination);
124         else
125                 new->action->unesc_destination = NULL;
126         new->action->labelcolor = src->action->labelcolor;
127         return new;
128 }
129
130 void filteringprop_free(FilteringProp * prop)
131 {
132         g_return_if_fail(prop);
133         matcherlist_free(prop->matchers);
134         filteringaction_free(prop->action);
135         g_free(prop);
136 }
137
138 /*
139   fitleringaction_apply
140   runs the action on one MsgInfo
141   return value : return TRUE if the action could be applied
142 */
143
144 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
145 {
146         FolderItem * dest_folder;
147         gint val;
148         Compose * compose;
149         PrefsAccount * account;
150         gchar * cmd;
151
152         switch(action->type) {
153         case MATCHACTION_MOVE:
154                 dest_folder =
155                         folder_find_item_from_identifier(action->destination);
156                 if (!dest_folder)
157                         return FALSE;
158                 
159                 if (folder_item_move_msg(dest_folder, info) == -1) {
160                         debug_print("*** could not move message\n");
161                         return FALSE;
162                 }       
163
164                 return TRUE;
165
166         case MATCHACTION_COPY:
167                 dest_folder =
168                         folder_find_item_from_identifier(action->destination);
169
170                 if (!dest_folder)
171                         return FALSE;
172
173                 if (folder_item_copy_msg(dest_folder, info) == -1)
174                         return FALSE;
175
176                 return TRUE;
177
178         case MATCHACTION_DELETE:
179                 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
180                         return FALSE;
181                 return TRUE;
182
183         case MATCHACTION_MARK:
184                 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
185                 return TRUE;
186
187         case MATCHACTION_UNMARK:
188                 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
189                 return TRUE;
190                 
191         case MATCHACTION_MARK_AS_READ:
192                 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
193                 return TRUE;
194
195         case MATCHACTION_MARK_AS_UNREAD:
196                 debug_print("*** setting unread flags\n");
197                 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
198                 return TRUE;
199         
200         case MATCHACTION_COLOR:
201                 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0); 
202                 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
203                 return TRUE;
204
205         case MATCHACTION_FORWARD:
206                 account = account_find_from_id(action->account_id);
207                 compose = compose_forward(account, info, FALSE, NULL);
208                 if (compose->account->protocol == A_NNTP)
209                         compose_entry_append(compose, action->destination,
210                                              COMPOSE_NEWSGROUPS);
211                 else
212                         compose_entry_append(compose, action->destination,
213                                              COMPOSE_TO);
214
215                 val = compose_send(compose);
216                 if (val == 0) {
217                         gtk_widget_destroy(compose->window);
218                         return TRUE;
219                 }
220
221                 gtk_widget_destroy(compose->window);
222                 return FALSE;
223
224         case MATCHACTION_FORWARD_AS_ATTACHMENT:
225
226                 account = account_find_from_id(action->account_id);
227                 compose = compose_forward(account, info, TRUE, NULL);
228                 if (compose->account->protocol == A_NNTP)
229                         compose_entry_append(compose, action->destination,
230                                              COMPOSE_NEWSGROUPS);
231                 else
232                         compose_entry_append(compose, action->destination,
233                                              COMPOSE_TO);
234
235                 val = compose_send(compose);
236                 if (val == 0) {
237                         gtk_widget_destroy(compose->window);
238                         return TRUE;
239                 }
240                 gtk_widget_destroy(compose->window);
241                 return FALSE;
242
243         case MATCHACTION_REDIRECT:
244                 account = account_find_from_id(action->account_id);
245                 compose = compose_redirect(account, info);
246                 if (compose->account->protocol == A_NNTP)
247                         break;
248                 else
249                         compose_entry_append(compose, action->destination,
250                                              COMPOSE_TO);
251
252                 val = compose_send(compose);
253                 if (val == 0) {
254                         gtk_widget_destroy(compose->window);
255                         return TRUE;
256                 }
257
258                 gtk_widget_destroy(compose->window);
259                 return FALSE;
260
261         case MATCHACTION_EXECUTE:
262                 cmd = matching_build_command(action->unesc_destination, info);
263                 if (cmd == NULL)
264                         return FALSE;
265                 else {
266                         system(cmd);
267                         g_free(cmd);
268                 }
269                 return TRUE;
270
271         case MATCHACTION_CHANGE_SCORE:
272                 /* NOTE:
273                  * action->account_id is 0 if just assignment, -1 if decrement
274                  * and 1 if increment by action->labelcolor 
275                  * action->labelcolor has the score value change
276                  */
277                 info->score = action->account_id ==  1 ? info->score + action->labelcolor
278                             : action->account_id == -1 ? info->score - action->labelcolor
279                             : action->labelcolor; 
280                 return TRUE;
281
282         default:
283                 break;
284         }
285         return FALSE;
286 }
287
288 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
289 {
290         return matcherlist_match(filtering->matchers, info);
291 }
292
293 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
294 {
295         gboolean result;
296         gchar    buf[50];
297
298         if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
299                 g_warning("action %s could not be applied", 
300                 filteringaction_to_string(buf, sizeof buf, filtering->action));
301         }
302         return result;
303 }
304
305 static gboolean filtering_is_final_action(FilteringProp *filtering)
306 {
307         switch(filtering->action->type) {
308         case MATCHACTION_MOVE:
309         case MATCHACTION_DELETE:
310                 return TRUE; /* MsgInfo invalid for message */
311         case MATCHACTION_EXECUTE:
312         case MATCHACTION_COPY:
313         case MATCHACTION_MARK:
314         case MATCHACTION_MARK_AS_READ:
315         case MATCHACTION_UNMARK:
316         case MATCHACTION_MARK_AS_UNREAD:
317         case MATCHACTION_FORWARD:
318         case MATCHACTION_FORWARD_AS_ATTACHMENT:
319         case MATCHACTION_REDIRECT:
320                 return FALSE; /* MsgInfo still valid for message */
321         default:
322                 return FALSE;
323         }
324 }
325
326 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
327 {
328         GSList  *l;
329         gboolean final;
330         gboolean applied;
331         
332         g_return_val_if_fail(info != NULL, TRUE);
333         
334         for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
335                 FilteringProp * filtering = (FilteringProp *) l->data;
336
337                 if (filtering_match_condition(filtering, info)) {
338                         applied = filtering_apply_rule(filtering, info);
339                         if (TRUE == (final = filtering_is_final_action(filtering)))
340                                 break;
341                 }               
342         }
343
344         /* put in inbox if a final rule could not be applied, or
345          * the last rule was not a final one. */
346         if ((final && !applied) || !final) {
347                 return FALSE;
348         }
349
350         return TRUE;
351 }
352
353 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
354 {
355         return filter_msginfo(flist, info);
356 }
357
358 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
359 {
360         const gchar *command_str;
361
362         command_str = get_matchparser_tab_str(action->type);
363
364         if (command_str == NULL)
365                 return NULL;
366
367         switch(action->type) {
368         case MATCHACTION_MOVE:
369         case MATCHACTION_COPY:
370         case MATCHACTION_EXECUTE:
371                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
372                 return dest;
373
374         case MATCHACTION_DELETE:
375         case MATCHACTION_MARK:
376         case MATCHACTION_UNMARK:
377         case MATCHACTION_MARK_AS_READ:
378         case MATCHACTION_MARK_AS_UNREAD:
379                 g_snprintf(dest, destlen, "%s", command_str);
380                 return dest;
381
382         case MATCHACTION_REDIRECT:
383         case MATCHACTION_FORWARD:
384         case MATCHACTION_FORWARD_AS_ATTACHMENT:
385                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
386                 return dest; 
387
388         case MATCHACTION_COLOR:
389                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
390                 return dest;  
391         default:
392                 return NULL;
393         }
394 }
395
396 gchar * filteringprop_to_string(FilteringProp * prop)
397 {
398         gchar *list_str;
399         gchar *action_str;
400         gchar *filtering_str;
401         gchar  buf[256];
402
403         action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
404
405         if (action_str == NULL)
406                 return NULL;
407
408         list_str = matcherlist_to_string(prop->matchers);
409
410         if (list_str == NULL)
411                 return NULL;
412
413         filtering_str = g_strconcat(list_str, " ", action_str, NULL);
414         g_free(list_str);
415
416         return filtering_str;
417 }
418
419 void prefs_filtering_free(GSList * prefs_filtering)
420 {
421         while (prefs_filtering != NULL) {
422                 FilteringProp * filtering = (FilteringProp *)
423                         prefs_filtering->data;
424                 filteringprop_free(filtering);
425                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
426         }
427 }
428
429 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
430 {
431         FolderItem *item = node->data;
432
433         g_return_val_if_fail(item, FALSE);
434         g_return_val_if_fail(item->prefs, FALSE);
435
436         prefs_filtering_free(item->prefs->processing);
437         item->prefs->processing = NULL;
438
439         return FALSE;
440 }
441
442 void prefs_filtering_clear(void)
443 {
444         GList * cur;
445
446         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
447                 Folder *folder;
448
449                 folder = (Folder *) cur->data;
450                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
451                                 prefs_filtering_free_func, NULL);
452         }
453
454         prefs_filtering_free(global_processing);
455         global_processing = NULL;
456 }
457
458 void prefs_filtering_clear_folder(Folder *folder)
459 {
460         g_return_if_fail(folder);
461         g_return_if_fail(folder->node);
462
463         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
464                         prefs_filtering_free_func, NULL);
465         /* FIXME: Note folder settings were changed, where the updates? */
466 }
467