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