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