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