second cumulative gtk2 patch
[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 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
343 {
344         return matcherlist_match(filtering->matchers, info);
345 }
346
347 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
348     gboolean * final)
349 {
350         gboolean result;
351         gchar    buf[50];
352         GSList * tmp;
353         
354         * final = FALSE;
355         for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
356                 FilteringAction * action;
357                 
358                 action = tmp->data;
359                 
360                 if (FALSE == (result = filteringaction_apply(action, info))) {
361                         g_warning("action %s could not be applied", 
362                             filteringaction_to_string(buf, sizeof buf, action));
363                 }
364                 
365                 if (filtering_is_final_action(action)) {
366                         * final = TRUE;
367                         break;
368                 }
369         }
370         return result;
371 }
372
373 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
374 {
375         switch(filtering_action->type) {
376         case MATCHACTION_MOVE:
377         case MATCHACTION_DELETE:
378                 return TRUE; /* MsgInfo invalid for message */
379         case MATCHACTION_EXECUTE:
380         case MATCHACTION_COPY:
381         case MATCHACTION_MARK:
382         case MATCHACTION_UNMARK:
383         case MATCHACTION_LOCK:
384         case MATCHACTION_UNLOCK:
385         case MATCHACTION_MARK_AS_READ:
386         case MATCHACTION_MARK_AS_UNREAD:
387         case MATCHACTION_FORWARD:
388         case MATCHACTION_FORWARD_AS_ATTACHMENT:
389         case MATCHACTION_REDIRECT:
390                 return FALSE; /* MsgInfo still valid for message */
391         default:
392                 return FALSE;
393         }
394 }
395
396 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
397 {
398         GSList  *l;
399         gboolean final;
400         gboolean applied;
401         
402         g_return_val_if_fail(info != NULL, TRUE);
403         
404         for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
405                 FilteringProp * filtering = (FilteringProp *) l->data;
406
407                 if (filtering_match_condition(filtering, info)) {
408                         applied = filtering_apply_rule(filtering, info, &final);
409 #if 0
410                         if (TRUE == (final = filtering_is_final_action(filtering)))
411                                 break;
412 #endif
413                         if (final)
414                                 break;
415                 }               
416         }
417
418         /* put in inbox if a final rule could not be applied, or
419          * the last rule was not a final one. */
420         if ((final && !applied) || !final) {
421                 return FALSE;
422         }
423
424         return TRUE;
425 }
426
427 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
428 {
429         return filter_msginfo(flist, info);
430 }
431
432 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
433 {
434         const gchar *command_str;
435
436         command_str = get_matchparser_tab_str(action->type);
437
438         if (command_str == NULL)
439                 return NULL;
440
441         switch(action->type) {
442         case MATCHACTION_MOVE:
443         case MATCHACTION_COPY:
444         case MATCHACTION_EXECUTE:
445                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
446                 return dest;
447
448         case MATCHACTION_DELETE:
449         case MATCHACTION_MARK:
450         case MATCHACTION_UNMARK:
451         case MATCHACTION_LOCK:
452         case MATCHACTION_UNLOCK:
453         case MATCHACTION_MARK_AS_READ:
454         case MATCHACTION_MARK_AS_UNREAD:
455                 g_snprintf(dest, destlen, "%s", command_str);
456                 return dest;
457
458         case MATCHACTION_REDIRECT:
459         case MATCHACTION_FORWARD:
460         case MATCHACTION_FORWARD_AS_ATTACHMENT:
461                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
462                 return dest; 
463
464         case MATCHACTION_COLOR:
465                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
466                 return dest;  
467         default:
468                 return NULL;
469         }
470 }
471
472 gchar * filteringaction_list_to_string(GSList * action_list)
473 {
474         gchar *action_list_str;
475         gchar  buf[256];
476         GSList * tmp;
477         gchar *list_str;
478
479         action_list_str = NULL;
480         for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
481                 gchar *action_str;
482                 FilteringAction * action;
483                 
484                 action = tmp->data;
485                 
486                 action_str = filteringaction_to_string(buf,
487                     sizeof buf, action);
488                 
489                 if (action_list_str != NULL) {
490                         list_str = g_strconcat(action_list_str, " ", action_str, NULL);
491                         g_free(action_list_str);
492                 }
493                 else {
494                         list_str = g_strdup(action_str);
495                 }
496                 action_list_str = list_str;
497         }
498
499         return action_list_str;
500 }
501
502 gchar * filteringprop_to_string(FilteringProp * prop)
503 {
504         gchar *list_str;
505         gchar *action_list_str;
506         gchar *filtering_str;
507         GSList * tmp;
508
509         action_list_str = filteringaction_list_to_string(prop->action_list);
510
511         if (action_list_str == NULL)
512                 return NULL;
513
514         list_str = matcherlist_to_string(prop->matchers);
515
516         if (list_str == NULL) {
517                 g_free(action_list_str);
518                 return NULL;
519         }
520
521         filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
522         g_free(action_list_str);
523         g_free(list_str);
524
525         return filtering_str;
526 }
527
528 void prefs_filtering_free(GSList * prefs_filtering)
529 {
530         while (prefs_filtering != NULL) {
531                 FilteringProp * filtering = (FilteringProp *)
532                         prefs_filtering->data;
533                 filteringprop_free(filtering);
534                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
535         }
536 }
537
538 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
539 {
540         FolderItem *item = node->data;
541
542         g_return_val_if_fail(item, FALSE);
543         g_return_val_if_fail(item->prefs, FALSE);
544
545         prefs_filtering_free(item->prefs->processing);
546         item->prefs->processing = NULL;
547
548         return FALSE;
549 }
550
551 void prefs_filtering_clear(void)
552 {
553         GList * cur;
554
555         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
556                 Folder *folder;
557
558                 folder = (Folder *) cur->data;
559                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
560                                 prefs_filtering_free_func, NULL);
561         }
562
563         prefs_filtering_free(global_processing);
564         global_processing = NULL;
565 }
566
567 void prefs_filtering_clear_folder(Folder *folder)
568 {
569         g_return_if_fail(folder);
570         g_return_if_fail(folder->node);
571
572         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
573                         prefs_filtering_free_func, NULL);
574         /* FIXME: Note folder settings were changed, where the updates? */
575 }
576