0.9.3claws64
[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_LOCK:
192                 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
193                 return TRUE;
194
195         case MATCHACTION_UNLOCK:
196                 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);       
197                 return TRUE;
198                 
199         case MATCHACTION_MARK_AS_READ:
200                 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
201                 return TRUE;
202
203         case MATCHACTION_MARK_AS_UNREAD:
204                 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
205                 return TRUE;
206         
207         case MATCHACTION_COLOR:
208                 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0); 
209                 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
210                 return TRUE;
211
212         case MATCHACTION_FORWARD:
213                 account = account_find_from_id(action->account_id);
214                 compose = compose_forward(account, info, FALSE, NULL);
215                 if (compose->account->protocol == A_NNTP)
216                         compose_entry_append(compose, action->destination,
217                                              COMPOSE_NEWSGROUPS);
218                 else
219                         compose_entry_append(compose, action->destination,
220                                              COMPOSE_TO);
221
222                 val = compose_send(compose);
223                 if (val == 0) {
224                         gtk_widget_destroy(compose->window);
225                         return TRUE;
226                 }
227
228                 gtk_widget_destroy(compose->window);
229                 return FALSE;
230
231         case MATCHACTION_FORWARD_AS_ATTACHMENT:
232
233                 account = account_find_from_id(action->account_id);
234                 compose = compose_forward(account, info, TRUE, NULL);
235                 if (compose->account->protocol == A_NNTP)
236                         compose_entry_append(compose, action->destination,
237                                              COMPOSE_NEWSGROUPS);
238                 else
239                         compose_entry_append(compose, action->destination,
240                                              COMPOSE_TO);
241
242                 val = compose_send(compose);
243                 if (val == 0) {
244                         gtk_widget_destroy(compose->window);
245                         return TRUE;
246                 }
247                 gtk_widget_destroy(compose->window);
248                 return FALSE;
249
250         case MATCHACTION_REDIRECT:
251                 account = account_find_from_id(action->account_id);
252                 compose = compose_redirect(account, info);
253                 if (compose->account->protocol == A_NNTP)
254                         break;
255                 else
256                         compose_entry_append(compose, action->destination,
257                                              COMPOSE_TO);
258
259                 val = compose_send(compose);
260                 if (val == 0) {
261                         gtk_widget_destroy(compose->window);
262                         return TRUE;
263                 }
264
265                 gtk_widget_destroy(compose->window);
266                 return FALSE;
267
268         case MATCHACTION_EXECUTE:
269                 cmd = matching_build_command(action->unesc_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_CHANGE_SCORE:
279                 /* NOTE:
280                  * action->account_id is 0 if just assignment, -1 if decrement
281                  * and 1 if increment by action->labelcolor 
282                  * action->labelcolor has the score value change
283                  */
284                 info->score = action->account_id ==  1 ? info->score + action->labelcolor
285                             : action->account_id == -1 ? info->score - action->labelcolor
286                             : action->labelcolor; 
287                 return TRUE;
288
289         default:
290                 break;
291         }
292         return FALSE;
293 }
294
295 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
296 {
297         return matcherlist_match(filtering->matchers, info);
298 }
299
300 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
301 {
302         gboolean result;
303         gchar    buf[50];
304
305         if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
306                 g_warning("action %s could not be applied", 
307                 filteringaction_to_string(buf, sizeof buf, filtering->action));
308         }
309         return result;
310 }
311
312 static gboolean filtering_is_final_action(FilteringProp *filtering)
313 {
314         switch(filtering->action->type) {
315         case MATCHACTION_MOVE:
316         case MATCHACTION_DELETE:
317                 return TRUE; /* MsgInfo invalid for message */
318         case MATCHACTION_EXECUTE:
319         case MATCHACTION_COPY:
320         case MATCHACTION_MARK:
321         case MATCHACTION_UNMARK:
322         case MATCHACTION_LOCK:
323         case MATCHACTION_UNLOCK:
324         case MATCHACTION_MARK_AS_READ:
325         case MATCHACTION_MARK_AS_UNREAD:
326         case MATCHACTION_FORWARD:
327         case MATCHACTION_FORWARD_AS_ATTACHMENT:
328         case MATCHACTION_REDIRECT:
329                 return FALSE; /* MsgInfo still valid for message */
330         default:
331                 return FALSE;
332         }
333 }
334
335 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
336 {
337         GSList  *l;
338         gboolean final;
339         gboolean applied;
340         
341         g_return_val_if_fail(info != NULL, TRUE);
342         
343         for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
344                 FilteringProp * filtering = (FilteringProp *) l->data;
345
346                 if (filtering_match_condition(filtering, info)) {
347                         applied = filtering_apply_rule(filtering, info);
348                         if (TRUE == (final = filtering_is_final_action(filtering)))
349                                 break;
350                 }               
351         }
352
353         /* put in inbox if a final rule could not be applied, or
354          * the last rule was not a final one. */
355         if ((final && !applied) || !final) {
356                 return FALSE;
357         }
358
359         return TRUE;
360 }
361
362 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
363 {
364         return filter_msginfo(flist, info);
365 }
366
367 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
368 {
369         const gchar *command_str;
370
371         command_str = get_matchparser_tab_str(action->type);
372
373         if (command_str == NULL)
374                 return NULL;
375
376         switch(action->type) {
377         case MATCHACTION_MOVE:
378         case MATCHACTION_COPY:
379         case MATCHACTION_EXECUTE:
380                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
381                 return dest;
382
383         case MATCHACTION_DELETE:
384         case MATCHACTION_MARK:
385         case MATCHACTION_UNMARK:
386         case MATCHACTION_LOCK:
387         case MATCHACTION_UNLOCK:
388         case MATCHACTION_MARK_AS_READ:
389         case MATCHACTION_MARK_AS_UNREAD:
390                 g_snprintf(dest, destlen, "%s", command_str);
391                 return dest;
392
393         case MATCHACTION_REDIRECT:
394         case MATCHACTION_FORWARD:
395         case MATCHACTION_FORWARD_AS_ATTACHMENT:
396                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
397                 return dest; 
398
399         case MATCHACTION_COLOR:
400                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
401                 return dest;  
402         default:
403                 return NULL;
404         }
405 }
406
407 gchar * filteringprop_to_string(FilteringProp * prop)
408 {
409         gchar *list_str;
410         gchar *action_str;
411         gchar *filtering_str;
412         gchar  buf[256];
413
414         action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
415
416         if (action_str == NULL)
417                 return NULL;
418
419         list_str = matcherlist_to_string(prop->matchers);
420
421         if (list_str == NULL)
422                 return NULL;
423
424         filtering_str = g_strconcat(list_str, " ", action_str, NULL);
425         g_free(list_str);
426
427         return filtering_str;
428 }
429
430 void prefs_filtering_free(GSList * prefs_filtering)
431 {
432         while (prefs_filtering != NULL) {
433                 FilteringProp * filtering = (FilteringProp *)
434                         prefs_filtering->data;
435                 filteringprop_free(filtering);
436                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
437         }
438 }
439
440 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
441 {
442         FolderItem *item = node->data;
443
444         g_return_val_if_fail(item, FALSE);
445         g_return_val_if_fail(item->prefs, FALSE);
446
447         prefs_filtering_free(item->prefs->processing);
448         item->prefs->processing = NULL;
449
450         return FALSE;
451 }
452
453 void prefs_filtering_clear(void)
454 {
455         GList * cur;
456
457         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
458                 Folder *folder;
459
460                 folder = (Folder *) cur->data;
461                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
462                                 prefs_filtering_free_func, NULL);
463         }
464
465         prefs_filtering_free(global_processing);
466         global_processing = NULL;
467 }
468
469 void prefs_filtering_clear_folder(Folder *folder)
470 {
471         g_return_if_fail(folder);
472         g_return_if_fail(folder->node);
473
474         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
475                         prefs_filtering_free_func, NULL);
476         /* FIXME: Note folder settings were changed, where the updates? */
477 }
478