fix crash when using Forward in Filtering when external editor option is set
[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         case MATCHACTION_FORWARD_AS_ATTACHMENT:
257                 account = account_find_from_id(action->account_id);
258                 compose = compose_forward(account, info,
259                         action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
260                         NULL, TRUE);
261                 compose_entry_append(compose, action->destination,
262                                      compose->account->protocol == A_NNTP
263                                             ? COMPOSE_NEWSGROUPS
264                                             : COMPOSE_TO);
265
266                 val = compose_send(compose);
267                 gtk_widget_destroy(compose->window);
268
269                 return val == 0 ? TRUE : FALSE;
270
271         case MATCHACTION_REDIRECT:
272                 account = account_find_from_id(action->account_id);
273                 compose = compose_redirect(account, info);
274                 if (compose->account->protocol == A_NNTP)
275                         break;
276                 else
277                         compose_entry_append(compose, action->destination,
278                                              COMPOSE_TO);
279
280                 val = compose_send(compose);
281                 gtk_widget_destroy(compose->window);
282                 
283                 return val == 0 ? TRUE : FALSE;
284
285         case MATCHACTION_EXECUTE:
286                 cmd = matching_build_command(action->unesc_destination, info);
287                 if (cmd == NULL)
288                         return FALSE;
289                 else {
290                         system(cmd);
291                         g_free(cmd);
292                 }
293                 return TRUE;
294
295         case MATCHACTION_SET_SCORE:
296                 info->score = action->score;
297                 return TRUE;
298
299         case MATCHACTION_ADD_SCORE:
300                 info->score += action->score;
301                 return TRUE;
302
303         case MATCHACTION_STOP:
304                 break;
305
306         default:
307                 break;
308         }
309         return FALSE;
310 }
311
312 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
313 {
314         GSList *p;
315         g_return_val_if_fail(action_list, FALSE);
316         g_return_val_if_fail(info, FALSE);
317         for (p = action_list; p && p->data; p = g_slist_next(p)) {
318                 FilteringAction *a = (FilteringAction *) p->data;
319                 if (filteringaction_apply(a, info)) {
320                         if (filtering_is_final_action(a))
321                                 break;
322                 } else
323                         return FALSE;
324                 
325         }
326         return TRUE;
327 }
328
329 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
330 {
331         return matcherlist_match(filtering->matchers, info);
332 }
333
334 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
335     gboolean * final)
336 {
337         gboolean result;
338         gchar    buf[50];
339         GSList * tmp;
340         
341         * final = FALSE;
342         for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
343                 FilteringAction * action;
344                 
345                 action = tmp->data;
346                 
347                 if (FALSE == (result = filteringaction_apply(action, info))) {
348                         g_warning("action %s could not be applied", 
349                             filteringaction_to_string(buf, sizeof buf, action));
350                 }
351                 
352                 if (filtering_is_final_action(action)) {
353                         * final = TRUE;
354                         break;
355                 }
356         }
357         return result;
358 }
359
360 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
361 {
362         switch(filtering_action->type) {
363         case MATCHACTION_MOVE:
364         case MATCHACTION_DELETE:
365         case MATCHACTION_STOP:
366                 return TRUE; /* MsgInfo invalid for message */
367         default:
368                 return FALSE;
369         }
370 }
371
372 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
373 {
374         GSList  *l;
375         gboolean final;
376         gboolean applied;
377         
378         g_return_val_if_fail(info != NULL, TRUE);
379         
380         for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
381                 FilteringProp * filtering = (FilteringProp *) l->data;
382
383                 if (filtering_match_condition(filtering, info)) {
384                         applied = filtering_apply_rule(filtering, info, &final);
385 #if 0
386                         if (TRUE == (final = filtering_is_final_action(filtering)))
387                                 break;
388 #endif
389                         if (final)
390                                 break;
391                 }               
392         }
393
394         /* put in inbox if a final rule could not be applied, or
395          * the last rule was not a final one. */
396         if ((final && !applied) || !final) {
397                 return FALSE;
398         }
399
400         return TRUE;
401 }
402
403 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
404 {
405         return filter_msginfo(flist, info);
406 }
407
408 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
409 {
410         const gchar *command_str;
411
412         command_str = get_matchparser_tab_str(action->type);
413
414         if (command_str == NULL)
415                 return NULL;
416
417         switch(action->type) {
418         case MATCHACTION_MOVE:
419         case MATCHACTION_COPY:
420         case MATCHACTION_EXECUTE:
421                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
422                 return dest;
423
424         case MATCHACTION_DELETE:
425         case MATCHACTION_MARK:
426         case MATCHACTION_UNMARK:
427         case MATCHACTION_LOCK:
428         case MATCHACTION_UNLOCK:
429         case MATCHACTION_MARK_AS_READ:
430         case MATCHACTION_MARK_AS_UNREAD:
431         case MATCHACTION_STOP:
432                 g_snprintf(dest, destlen, "%s", command_str);
433                 return dest;
434
435         case MATCHACTION_REDIRECT:
436         case MATCHACTION_FORWARD:
437         case MATCHACTION_FORWARD_AS_ATTACHMENT:
438                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
439                 return dest; 
440
441         case MATCHACTION_COLOR:
442                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
443                 return dest;  
444
445         case MATCHACTION_ADD_SCORE:
446         case MATCHACTION_SET_SCORE:
447                 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
448                 return dest;  
449
450         default:
451                 return NULL;
452         }
453 }
454
455 gchar * filteringaction_list_to_string(GSList * action_list)
456 {
457         gchar *action_list_str;
458         gchar  buf[256];
459         GSList * tmp;
460         gchar *list_str;
461
462         action_list_str = NULL;
463         for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
464                 gchar *action_str;
465                 FilteringAction * action;
466                 
467                 action = tmp->data;
468                 
469                 action_str = filteringaction_to_string(buf,
470                     sizeof buf, action);
471                 
472                 if (action_list_str != NULL) {
473                         list_str = g_strconcat(action_list_str, " ", action_str, NULL);
474                         g_free(action_list_str);
475                 }
476                 else {
477                         list_str = g_strdup(action_str);
478                 }
479                 action_list_str = list_str;
480         }
481
482         return action_list_str;
483 }
484
485 gchar * filteringprop_to_string(FilteringProp * prop)
486 {
487         gchar *list_str;
488         gchar *action_list_str;
489         gchar *filtering_str;
490         GSList * tmp;
491
492         action_list_str = filteringaction_list_to_string(prop->action_list);
493
494         if (action_list_str == NULL)
495                 return NULL;
496
497         list_str = matcherlist_to_string(prop->matchers);
498
499         if (list_str == NULL) {
500                 g_free(action_list_str);
501                 return NULL;
502         }
503
504         filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
505         g_free(action_list_str);
506         g_free(list_str);
507
508         return filtering_str;
509 }
510
511 void prefs_filtering_free(GSList * prefs_filtering)
512 {
513         while (prefs_filtering != NULL) {
514                 FilteringProp * filtering = (FilteringProp *)
515                         prefs_filtering->data;
516                 filteringprop_free(filtering);
517                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
518         }
519 }
520
521 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
522 {
523         FolderItem *item = node->data;
524
525         g_return_val_if_fail(item, FALSE);
526         g_return_val_if_fail(item->prefs, FALSE);
527
528         prefs_filtering_free(item->prefs->processing);
529         item->prefs->processing = NULL;
530
531         return FALSE;
532 }
533
534 void prefs_filtering_clear(void)
535 {
536         GList * cur;
537
538         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
539                 Folder *folder;
540
541                 folder = (Folder *) cur->data;
542                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
543                                 prefs_filtering_free_func, NULL);
544         }
545
546         prefs_filtering_free(global_processing);
547         global_processing = NULL;
548 }
549
550 void prefs_filtering_clear_folder(Folder *folder)
551 {
552         g_return_if_fail(folder);
553         g_return_if_fail(folder->node);
554
555         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
556                         prefs_filtering_free_func, NULL);
557         /* FIXME: Note folder settings were changed, where the updates? */
558 }
559