fix a segfault when move fails
[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.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         action->type = type;
61         action->account_id = account_id;
62         if (destination) {
63                 action->destination       = g_strdup(destination);
64                 action->unesc_destination = matcher_unescape_str(g_strdup(destination));
65         } else {
66                 action->destination       = NULL;
67                 action->unesc_destination = NULL;
68         }
69         action->labelcolor = labelcolor;        
70         return action;
71 }
72
73 void filteringaction_free(FilteringAction * action)
74 {
75         g_return_if_fail(action);
76         if (action->destination)
77                 g_free(action->destination);
78         if (action->unesc_destination)
79                 g_free(action->unesc_destination);
80         g_free(action);
81 }
82
83 FilteringProp * filteringprop_new(MatcherList * matchers,
84                                   FilteringAction * action)
85 {
86         FilteringProp * filtering;
87
88         filtering = g_new0(FilteringProp, 1);
89         filtering->matchers = matchers;
90         filtering->action = action;
91
92         return filtering;
93 }
94
95 FilteringProp * filteringprop_copy(FilteringProp *src)
96 {
97         FilteringProp * new;
98         GSList *tmp;
99         
100         new = g_new0(FilteringProp, 1);
101         new->matchers = g_new0(MatcherList, 1);
102         new->action = g_new0(FilteringAction, 1);
103         for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
104                 MatcherProp *matcher = (MatcherProp *)tmp->data;
105                 
106                 new->matchers->matchers = g_slist_append(new->matchers->matchers,
107                                                    matcherprop_copy(matcher));
108                 tmp = tmp->next;
109         }
110         new->matchers->bool_and = src->matchers->bool_and;
111         new->action->type = src->action->type;
112         new->action->account_id = src->action->account_id;
113         if (src->action->destination)
114                 new->action->destination = g_strdup(src->action->destination);
115         else 
116                 new->action->destination = NULL;
117         if (src->action->unesc_destination)
118                 new->action->unesc_destination = g_strdup(src->action->unesc_destination);
119         else
120                 new->action->unesc_destination = NULL;
121         new->action->labelcolor = src->action->labelcolor;
122         return new;
123 }
124
125 void filteringprop_free(FilteringProp * prop)
126 {
127         matcherlist_free(prop->matchers);
128         filteringaction_free(prop->action);
129         g_free(prop);
130 }
131
132 /*
133   fitleringaction_apply
134   runs the action on one MsgInfo
135   return value : return TRUE if the action could be applied
136 */
137
138 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
139 {
140         FolderItem * dest_folder;
141         gint val;
142         Compose * compose;
143         PrefsAccount * account;
144         gchar * cmd;
145
146         switch(action->type) {
147         case MATCHACTION_MOVE:
148                 dest_folder =
149                         folder_find_item_from_identifier(action->destination);
150                 if (!dest_folder)
151                         return FALSE;
152                 
153                 if (folder_item_move_msg(dest_folder, info) == -1) {
154                         debug_print("*** could not move message\n");
155                         return FALSE;
156                 }       
157
158                 return TRUE;
159
160         case MATCHACTION_COPY:
161                 dest_folder =
162                         folder_find_item_from_identifier(action->destination);
163
164                 if (!dest_folder)
165                         return FALSE;
166
167                 if (folder_item_copy_msg(dest_folder, info) == -1)
168                         return FALSE;
169
170                 return TRUE;
171
172         case MATCHACTION_DELETE:
173                 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
174                         return FALSE;
175                 return TRUE;
176
177         case MATCHACTION_MARK:
178                 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
179                 return TRUE;
180
181         case MATCHACTION_UNMARK:
182                 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
183                 return TRUE;
184                 
185         case MATCHACTION_MARK_AS_READ:
186                 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
187                 return TRUE;
188
189         case MATCHACTION_MARK_AS_UNREAD:
190                 debug_print("*** setting unread flags\n");
191                 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
192                 return TRUE;
193         
194         case MATCHACTION_COLOR:
195                 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0); 
196                 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
197                 return TRUE;
198
199         case MATCHACTION_FORWARD:
200                 account = account_find_from_id(action->account_id);
201                 compose = compose_forward(account, info, FALSE, NULL);
202                 if (compose->account->protocol == A_NNTP)
203                         compose_entry_append(compose, action->destination,
204                                              COMPOSE_NEWSGROUPS);
205                 else
206                         compose_entry_append(compose, action->destination,
207                                              COMPOSE_TO);
208
209                 val = compose_send(compose);
210                 if (val == 0) {
211                         gtk_widget_destroy(compose->window);
212                         return TRUE;
213                 }
214
215                 gtk_widget_destroy(compose->window);
216                 return FALSE;
217
218         case MATCHACTION_FORWARD_AS_ATTACHMENT:
219
220                 account = account_find_from_id(action->account_id);
221                 compose = compose_forward(account, info, TRUE, NULL);
222                 if (compose->account->protocol == A_NNTP)
223                         compose_entry_append(compose, action->destination,
224                                              COMPOSE_NEWSGROUPS);
225                 else
226                         compose_entry_append(compose, action->destination,
227                                              COMPOSE_TO);
228
229                 val = compose_send(compose);
230                 if (val == 0) {
231                         gtk_widget_destroy(compose->window);
232                         return TRUE;
233                 }
234                 gtk_widget_destroy(compose->window);
235                 return FALSE;
236
237         case MATCHACTION_REDIRECT:
238                 account = account_find_from_id(action->account_id);
239                 compose = compose_redirect(account, info);
240                 if (compose->account->protocol == A_NNTP)
241                         break;
242                 else
243                         compose_entry_append(compose, action->destination,
244                                              COMPOSE_TO);
245
246                 val = compose_send(compose);
247                 if (val == 0) {
248                         gtk_widget_destroy(compose->window);
249                         return TRUE;
250                 }
251
252                 gtk_widget_destroy(compose->window);
253                 return FALSE;
254
255         case MATCHACTION_EXECUTE:
256                 cmd = matching_build_command(action->unesc_destination, info);
257                 if (cmd == NULL)
258                         return FALSE;
259                 else {
260                         system(cmd);
261                         g_free(cmd);
262                 }
263                 return TRUE;
264
265         default:
266                 break;
267         }
268         return FALSE;
269 }
270
271 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
272 {
273         return matcherlist_match(filtering->matchers, info);
274 }
275
276 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
277 {
278         gboolean result;
279         gchar    buf[50];
280
281         if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
282                 g_warning(_("action %s could not be applied"), 
283                 filteringaction_to_string(buf, sizeof buf, filtering->action));
284         }
285         return result;
286 }
287
288 static gboolean filtering_is_final_action(FilteringProp *filtering)
289 {
290         switch(filtering->action->type) {
291         case MATCHACTION_MOVE:
292         case MATCHACTION_DELETE:
293                 return TRUE; /* MsgInfo invalid for message */
294         case MATCHACTION_EXECUTE:
295         case MATCHACTION_COPY:
296         case MATCHACTION_MARK:
297         case MATCHACTION_MARK_AS_READ:
298         case MATCHACTION_UNMARK:
299         case MATCHACTION_MARK_AS_UNREAD:
300         case MATCHACTION_FORWARD:
301         case MATCHACTION_FORWARD_AS_ATTACHMENT:
302         case MATCHACTION_REDIRECT:
303                 return FALSE; /* MsgInfo still valid for message */
304         default:
305                 return FALSE;
306         }
307 }
308
309 static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
310                            MsgInfo * info)
311 {
312         GSList  *l;
313         gboolean final;
314         gboolean applied;
315         gint val;
316         
317         if (info == NULL) {
318                 g_warning(_("msginfo is not set"));
319                 return;
320         }
321         
322         for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
323                 FilteringProp * filtering = (FilteringProp *) l->data;
324
325                 if (filtering_match_condition(filtering, info)) {
326                         applied = filtering_apply_rule(filtering, info);
327                         if (TRUE == (final = filtering_is_final_action(filtering)))
328                                 break;
329                 }               
330         }
331
332         /* put in inbox if a final rule could not be applied, or
333          * the last rule was not a final one. */
334         if ((final && !applied) || !final) {
335                 if (inbox != info->folder) {
336                         if (folder_item_move_msg(inbox, info) == -1) {
337                                 debug_print("*** Could not drop message in inbox; check .processing\n");
338                                 return;
339                         }       
340                 }       
341         }
342 }
343
344 /*!
345  *\brief        filters a message based on its message info data
346  *
347  *\param        flist filter and actions list
348  *\param        info message
349  */
350 void filter_message_by_msginfo_with_inbox(GSList *flist, MsgInfo *info, FolderItem *def_inbox)
351 {
352         FolderItem *inbox;
353
354         if ((def_inbox == NULL)) {
355                 debug_print("using default inbox as final destination!\n");
356                 inbox = folder_get_default_inbox();
357         } else
358                 inbox = def_inbox;
359
360         /*
361          * message is already in a folder. the filtering code will
362          * handle duplicate moves and copies.
363          */
364         filter_msginfo(flist, inbox, info);
365 }
366
367 void filter_message_by_msginfo(GSList *flist, MsgInfo *info)
368 {
369         filter_message_by_msginfo_with_inbox(flist, info, info->folder);
370 }
371
372 /*!
373  *\brief        filters a message waiting to be processed in the
374  *              .processing folder. 
375  *
376   *\param       filtering_list list of filters and actions
377   *\param       inbox default inbox when no filter could be applied
378   *\param       msgnum message number in processing folder
379   *             changed after the call to this function
380   */
381 void filter_message(GSList *filtering_list, FolderItem *inbox,
382                     gint msgnum)
383 {
384         MsgInfo *msginfo;
385         gchar *filename;
386         MsgFlags  msgflags = { 0, 0 };
387         FolderItem *item = folder_get_default_processing();
388
389         if (item == NULL) {
390                 g_warning(_("folderitem not set"));
391                 return;
392         }
393
394         filename = folder_item_fetch_msg(item, msgnum);
395
396         if (filename == NULL) {
397                 g_warning(_("filename is not set"));
398                 return;
399         }
400
401         msginfo = procheader_parse_file(filename, msgflags, TRUE, FALSE);
402         
403         g_free(filename);
404
405         if (msginfo == NULL) {
406                 g_warning(_("could not get info for %s"), filename);
407                 return;
408         }
409
410         msginfo->folder = item;
411         msginfo->msgnum = msgnum;
412
413         filter_msginfo(filtering_list, inbox, msginfo);
414
415         procmsg_msginfo_free(msginfo);
416 }
417
418 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
419 {
420         gchar *command_str;
421
422         command_str = get_matchparser_tab_str(action->type);
423
424         if (command_str == NULL)
425                 return NULL;
426
427         switch(action->type) {
428         case MATCHACTION_MOVE:
429         case MATCHACTION_COPY:
430         case MATCHACTION_EXECUTE:
431                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
432                 return dest;
433
434         case MATCHACTION_DELETE:
435         case MATCHACTION_MARK:
436         case MATCHACTION_UNMARK:
437         case MATCHACTION_MARK_AS_READ:
438         case MATCHACTION_MARK_AS_UNREAD:
439                 g_snprintf(dest, destlen, "%s", command_str);
440                 return dest;
441
442         case MATCHACTION_REDIRECT:
443         case MATCHACTION_FORWARD:
444         case MATCHACTION_FORWARD_AS_ATTACHMENT:
445                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
446                 return dest; 
447
448         case MATCHACTION_COLOR:
449                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
450                 return dest;  
451         case MATCHACTION_DELETE_ON_SERVER:
452                 g_snprintf(dest, destlen, "%s", command_str);
453                 return dest;
454         default:
455                 return NULL;
456         }
457 }
458
459 gchar * filteringprop_to_string(FilteringProp * prop)
460 {
461         gchar *list_str;
462         gchar *action_str;
463         gchar *filtering_str;
464         gchar  buf[256];
465
466         action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
467
468         if (action_str == NULL)
469                 return NULL;
470
471         list_str = matcherlist_to_string(prop->matchers);
472
473         if (list_str == NULL)
474                 return NULL;
475
476         filtering_str = g_strconcat(list_str, " ", action_str, NULL);
477         g_free(list_str);
478
479         return filtering_str;
480 }
481
482 void prefs_filtering_free(GSList * prefs_filtering)
483 {
484         while (prefs_filtering != NULL) {
485                 FilteringProp * filtering = (FilteringProp *)
486                         prefs_filtering->data;
487                 filteringprop_free(filtering);
488                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
489         }
490 }
491
492 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
493 {
494         FolderItem *item = node->data;
495
496         if(!item->prefs)
497                 return FALSE;
498
499         prefs_filtering_free(item->prefs->processing);
500         item->prefs->processing = NULL;
501
502         return FALSE;
503 }
504
505 void prefs_filtering_clear()
506 {
507         GList * cur;
508
509         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
510                 Folder *folder;
511
512                 folder = (Folder *) cur->data;
513                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
514                                 prefs_filtering_free_func, NULL);
515         }
516
517         prefs_filtering_free(global_processing);
518         global_processing = NULL;
519 }