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