cfdc07797abc418fa4bbce4da8ab535ec2c99fc2
[claws.git] / src / filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 /* 
21  * initital     Hoa             initial
22  * 07/16/01     Alfons          fix bugs
23  * 07/18/01     Alfons          rewrite things
24  */
25
26 /* 07/18/01 (alfons) 
27  * 
28  * filter_message() is no longer the entry point for filtering. it's
29  * replaced with filter_incoming_message() which does not trust
30  * on MsgInfo's from a folder. instead it directly works with
31  * the temporary file.  
32  * updating the mark file is a lot easier now, because we can do that
33  * right after a call to folder_item_add_msg(). 
34  */ 
35
36 #include "defs.h"
37 #include <ctype.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <gtk/gtk.h>
42 #include <stdio.h>
43 #include "intl.h"
44 #include "utils.h"
45 #include "procheader.h"
46 #include "matcher.h"
47 #include "filtering.h"
48 #include "prefs.h"
49 #include "compose.h"
50
51 #define PREFSBUFSIZE            1024
52
53 GSList * global_processing = NULL;
54
55 FilteringAction * filteringaction_new(int type, int account_id,
56                                       gchar * destination,
57                                       gint labelcolor)
58 {
59         FilteringAction * action;
60
61         action = g_new0(FilteringAction, 1);
62
63         action->type = type;
64         action->account_id = account_id;
65         if (destination)
66                 action->destination = g_strdup(destination);
67         action->labelcolor = labelcolor;        
68         return action;
69 }
70
71 void filteringaction_free(FilteringAction * action)
72 {
73         if (action->destination)
74                 g_free(action->destination);
75         g_free(action);
76 }
77
78 FilteringProp * filteringprop_new(MatcherList * matchers,
79                                   FilteringAction * action)
80 {
81         FilteringProp * filtering;
82
83         filtering = g_new0(FilteringProp, 1);
84         filtering->matchers = matchers;
85         filtering->action = action;
86
87         return filtering;
88 }
89
90 void filteringprop_free(FilteringProp * prop)
91 {
92         matcherlist_free(prop->matchers);
93         filteringaction_free(prop->action);
94         g_free(prop);
95 }
96
97 /* filteringaction_update_mark() - updates a mark for a message. note that
98  * the message should not have been moved or copied. remember that the
99  * procmsg_open_mark_file(PATH, TRUE) actually _appends_ a new record.
100  */
101 static gboolean filteringaction_update_mark(MsgInfo * info)
102 {
103         gchar * dest_path;
104         FILE * fp;
105
106         if (info->folder->folder->type == F_MH) {
107                 dest_path = folder_item_get_path(info->folder);
108                 if (!is_dir_exist(dest_path))
109                         make_dir_hier(dest_path);
110                 
111                 if (dest_path == NULL) {
112                         g_warning(_("Can't open mark file.\n"));
113                         return FALSE;
114                 }
115                 
116                 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
117                     == NULL) {
118                         g_warning(_("Can't open mark file.\n"));
119                         return FALSE;
120                 }
121                 
122                 procmsg_write_flags(info, fp);
123                 fclose(fp);
124                 return TRUE;
125         }
126         return FALSE;
127 }
128
129 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
130 {
131         if (str) 
132                 return strlen(str);
133         else {
134                 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
135                 return 0;
136         }
137 }
138
139 #define STRLEN_WITH_CHECK(expr) \
140         strlen_with_check(#expr, __LINE__, expr)
141         
142 static gchar * filteringaction_execute_command(gchar * cmd, MsgInfo * info)
143 {
144         gchar * s = cmd;
145         gchar * filename = NULL;
146         gchar * processed_cmd;
147         gchar * p;
148         gint size;
149
150         matcher_unescape_str(cmd);
151
152         size = strlen(cmd) + 1;
153         while (*s != '\0') {
154                 if (*s == '%') {
155                         s++;
156                         switch (*s) {
157                         case '%':
158                                 size -= 1;
159                                 break;
160                         case 's': /* subject */
161                                 size += STRLEN_WITH_CHECK(info->subject) - 2;
162                                 break;
163                         case 'f': /* from */
164                                 size += STRLEN_WITH_CHECK(info->from) - 2;
165                                 break;
166                         case 't': /* to */
167                                 size += STRLEN_WITH_CHECK(info->to) - 2;
168                                 break;
169                         case 'c': /* cc */
170                                 size += STRLEN_WITH_CHECK(info->cc) - 2;
171                                 break;
172                         case 'd': /* date */
173                                 size += STRLEN_WITH_CHECK(info->date) - 2;
174                                 break;
175                         case 'i': /* message-id */
176                                 size += STRLEN_WITH_CHECK(info->msgid) - 2;
177                                 break;
178                         case 'n': /* newsgroups */
179                                 size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
180                                 break;
181                         case 'r': /* references */
182                                 size += STRLEN_WITH_CHECK(info->references) - 2;
183                                 break;
184                         case 'F': /* file */
185                                 if (MSG_IS_FILTERING(info->flags))
186                                         filename = g_strdup((gchar *)info->folder);
187                                 else
188                                         filename = folder_item_fetch_msg(info->folder, info->msgnum);
189                                 
190                                 if (filename == NULL) {
191                                         g_warning(_("filename is not set"));
192                                         return NULL;
193                                 }
194                                 else
195                                         size += strlen(filename) - 2;
196                                 break;
197                         }
198                         s++;
199                 }
200                 else s++;
201         }
202
203
204         processed_cmd = g_new0(gchar, size);
205         s = cmd;
206         p = processed_cmd;
207
208         while (*s != '\0') {
209                 if (*s == '%') {
210                         s++;
211                         switch (*s) {
212                         case '%':
213                                 *p = '%';
214                                 p++;
215                                 break;
216                         case 's': /* subject */
217                                 if (info->subject != NULL)
218                                         strcpy(p, info->subject);
219                                 else
220                                         strcpy(p, "(none)");
221                                 p += strlen(p);
222                                 break;
223                         case 'f': /* from */
224                                 if (info->from != NULL)
225                                         strcpy(p, info->from);
226                                 else
227                                         strcpy(p, "(none)");
228                                 p += strlen(p);
229                                 break;
230                         case 't': /* to */
231                                 if (info->to != NULL)
232                                         strcpy(p, info->to);
233                                 else
234                                         strcpy(p, "(none)");
235                                 p += strlen(p);
236                                 break;
237                         case 'c': /* cc */
238                                 if (info->cc != NULL)
239                                         strcpy(p, info->cc);
240                                 else
241                                         strcpy(p, "(none)");
242                                 p += strlen(p);
243                                 break;
244                         case 'd': /* date */
245                                 if (info->date != NULL)
246                                         strcpy(p, info->date);
247                                 else
248                                         strcpy(p, "(none)");
249                                 p += strlen(p);
250                                 break;
251                         case 'i': /* message-id */
252                                 if (info->msgid != NULL)
253                                         strcpy(p, info->msgid);
254                                 else
255                                         strcpy(p, "(none)");
256                                 p += strlen(p);
257                                 break;
258                         case 'n': /* newsgroups */
259                                 if (info->newsgroups != NULL)
260                                         strcpy(p, info->newsgroups);
261                                 else
262                                         strcpy(p, "(none)");
263                                 p += strlen(p);
264                                 break;
265                         case 'r': /* references */
266                                 if (info->references != NULL)
267                                         strcpy(p, info->references);
268                                 else
269                                         strcpy(p, "(none)");
270                                 p += strlen(p);
271                                 break;
272                         case 'F': /* file */
273                                 strcpy(p, filename);
274                                 p += strlen(p);
275                                 break;
276                         default:
277                                 *p = '%';
278                                 p++;
279                                 *p = *s;
280                                 p++;
281                                 break;
282                         }
283                         s++;
284                 }
285                 else {
286                         *p = *s;
287                         p++;
288                         s++;
289                 }
290         }
291
292         g_free(filename);
293         return processed_cmd;
294 }
295
296 /*
297   fitleringaction_apply
298   runs the action on one MsgInfo
299   return value : return TRUE if the action could be applied
300 */
301
302 #define CHANGE_FLAGS(msginfo) \
303 { \
304 if (msginfo->folder->folder->change_flags != NULL) \
305 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
306                                       msginfo->folder, \
307                                       msginfo); \
308 }
309
310 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
311                                       GHashTable *folder_table)
312 {
313         FolderItem * dest_folder;
314         gint val;
315         Compose * compose;
316         PrefsAccount * account;
317         gchar * cmd;
318
319         switch(action->type) {
320         case MATCHACTION_MOVE:
321                 dest_folder =
322                         folder_find_item_from_identifier(action->destination);
323                 if (!dest_folder)
324                         return FALSE;
325                 
326                 if (folder_item_move_msg(dest_folder, info) == -1) {
327                         return FALSE;
328                 }       
329
330                 /* WRONG: can not update the mark, because the message has 
331                  * been moved. info pertains to original location. 
332                  * folder_item_move_msg() already updated the mark for the
333                  * destination folder.
334                 info->flags = 0;
335                 filteringaction_update_mark(info);
336                  */
337                 if (folder_table) {
338                         val = GPOINTER_TO_INT(g_hash_table_lookup
339                                               (folder_table, dest_folder));
340                         if (val == 0) {
341                                 folder_item_scan(dest_folder);
342                                 g_hash_table_insert(folder_table, dest_folder,
343                                                     GINT_TO_POINTER(1));
344                         }
345                         val = GPOINTER_TO_INT(g_hash_table_lookup
346                                               (folder_table, info->folder));
347                         if (val == 0) {
348                                 folder_item_scan(info->folder);
349                                 g_hash_table_insert(folder_table, info->folder,
350                                                     GINT_TO_POINTER(1));
351                         }
352                 }
353                 return TRUE;
354
355         case MATCHACTION_COPY:
356                 dest_folder =
357                         folder_find_item_from_identifier(action->destination);
358
359                 if (!dest_folder)
360                         return FALSE;
361
362                 /* NOTE: the following call *will* update the mark file for
363                  * the destination folder. but the original message will
364                  * still be there in the inbox. */
365
366                 if (folder_item_copy_msg(dest_folder, info) == -1)
367                         return FALSE;
368
369                 if (folder_table) {
370                         val = GPOINTER_TO_INT(g_hash_table_lookup
371                                               (folder_table, dest_folder));
372                         if (val == 0) {
373                                 folder_item_scan(dest_folder);
374                                 g_hash_table_insert(folder_table, dest_folder,
375                                                     GINT_TO_POINTER(1));
376                         }
377                 }
378                 return TRUE;
379
380         case MATCHACTION_DELETE:
381                 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
382                         return FALSE;
383
384                 /* WRONG: can not update the mark. this would actually add
385                  * a bogus record to the mark file for the message's original 
386                  * folder. 
387                 info->flags = 0;
388                 filteringaction_update_mark(info);
389                  */
390
391                 return TRUE;
392
393         case MATCHACTION_MARK:
394                 MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
395                 debug_print("*** MARKING message %d, in folder %s, \"%s\"\n",
396                             info->msgnum, info->folder->name,
397                             info->subject ? info->subject : "<none>");
398                 filteringaction_update_mark(info);
399
400                 CHANGE_FLAGS(info);
401
402                 return TRUE;
403
404         case MATCHACTION_UNMARK:
405                 MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
406                 filteringaction_update_mark(info);
407
408                 CHANGE_FLAGS(info);
409
410                 return TRUE;
411                 
412         case MATCHACTION_MARK_AS_READ:
413                 MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
414                 filteringaction_update_mark(info);
415
416                 CHANGE_FLAGS(info);
417
418                 return TRUE;
419
420         case MATCHACTION_MARK_AS_UNREAD:
421                 MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
422                 filteringaction_update_mark(info);
423
424                 CHANGE_FLAGS(info);
425                 
426                 return TRUE;
427
428         case MATCHACTION_FORWARD:
429
430                 account = account_find_from_id(action->account_id);
431                 compose = compose_forward(account, info, FALSE);
432                 if (compose->account->protocol == A_NNTP)
433                         compose_entry_append(compose, action->destination,
434                                              COMPOSE_NEWSGROUPS);
435                 else
436                         compose_entry_append(compose, action->destination,
437                                              COMPOSE_TO);
438
439                 val = compose_send(compose);
440                 if (val == 0) {
441                         gtk_widget_destroy(compose->window);
442                         return TRUE;
443                 }
444
445                 gtk_widget_destroy(compose->window);
446                 return FALSE;
447
448         case MATCHACTION_FORWARD_AS_ATTACHMENT:
449
450                 account = account_find_from_id(action->account_id);
451                 compose = compose_forward(account, info, TRUE);
452                 if (compose->account->protocol == A_NNTP)
453                         compose_entry_append(compose, action->destination,
454                                              COMPOSE_NEWSGROUPS);
455                 else
456                         compose_entry_append(compose, action->destination,
457                                              COMPOSE_TO);
458
459                 val = compose_send(compose);
460                 if (val == 0) {
461                         gtk_widget_destroy(compose->window);
462                         return TRUE;
463                 }
464
465                 gtk_widget_destroy(compose->window);
466                 return FALSE;
467
468         case MATCHACTION_BOUNCE:
469
470                 account = account_find_from_id(action->account_id);
471                 compose = compose_bounce(account, info);
472                 if (compose->account->protocol == A_NNTP)
473                         break;
474                 else
475                         compose_entry_append(compose, action->destination,
476                                              COMPOSE_TO);
477
478                 val = compose_send(compose);
479                 if (val == 0) {
480                         gtk_widget_destroy(compose->window);
481                         return TRUE;
482                 }
483
484                 gtk_widget_destroy(compose->window);
485                 return FALSE;
486
487         case MATCHACTION_EXECUTE:
488
489                 cmd = matching_build_command(action->destination, info);
490                 if (cmd == NULL)
491                         return TRUE;
492                 else {
493                         system(cmd);
494                         g_free(cmd);
495                 }
496
497                 return TRUE;
498
499         default:
500                 return FALSE;
501         }
502 }
503
504 /* filteringprop_apply() - runs the action on one MsgInfo if it matches the 
505  * criterium. certain actions can be followed by other actions. in this
506  * case the function returns FALSE. if an action can not be followed
507  * by others, the function returns TRUE.
508  *
509  * remember that this is because of the fact that msg flags are always
510  * _appended_ to mark files. currently sylpheed does not insert messages 
511  * at a certain index. 
512  * now, after having performed a certain action, the MsgInfo is still
513  * valid for the message. in *this* case the function returns FALSE.
514  */
515 static gboolean filteringprop_apply(FilteringProp * filtering, MsgInfo * info,
516                                     GHashTable *folder_table)
517 {
518         if (matcherlist_match(filtering->matchers, info)) {
519                 gboolean result;
520                 gchar   *action_str;
521                 gchar    buf[256]; 
522
523                 if (FALSE == (result = filteringaction_apply(filtering->action, info,
524                                                folder_table))) {
525                         action_str = filteringaction_to_string(buf, sizeof buf, filtering->action);
526                         g_warning(_("action %s could not be applied"), action_str);
527                 }
528
529                 switch(filtering->action->type) {
530                 case MATCHACTION_MOVE:
531                 case MATCHACTION_DELETE:
532                         return TRUE; /* MsgInfo invalid for message */
533                 case MATCHACTION_EXECUTE:
534                 case MATCHACTION_COPY:
535                 case MATCHACTION_MARK:
536                 case MATCHACTION_MARK_AS_READ:
537                 case MATCHACTION_UNMARK:
538                 case MATCHACTION_MARK_AS_UNREAD:
539                 case MATCHACTION_FORWARD:
540                 case MATCHACTION_FORWARD_AS_ATTACHMENT:
541                 case MATCHACTION_BOUNCE:
542                         return FALSE; /* MsgInfo still valid for message */
543                 default:
544                         return FALSE;
545                 }
546         }
547         else
548                 return FALSE;
549 }
550
551 void filter_msginfo(GSList * filtering_list, MsgInfo * info,
552                     GHashTable *folder_table)
553 {
554         GSList * l;
555
556         if (info == NULL) {
557                 g_warning(_("msginfo is not set"));
558                 return;
559         }
560         
561         for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
562                 FilteringProp * filtering = (FilteringProp *) l->data;
563                 
564                 if (filteringprop_apply(filtering, info, folder_table))
565                         break;
566         }
567 }
568
569 void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
570                                    GHashTable *folder_table)
571 {
572         GSList * l;
573
574         if (info == NULL) {
575                 g_warning(_("msginfo is not set"));
576                 return;
577         }
578         
579         for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
580                 FilteringProp * filtering = (FilteringProp *) l->data;
581
582                 switch (filtering->action->type) {
583                 case MATCHACTION_MOVE:
584                 case MATCHACTION_DELETE:
585                         if (filteringprop_apply(filtering, info, folder_table))
586                                 return;
587                 }
588         }
589 }
590
591 void filter_message(GSList * filtering_list, FolderItem * item,
592                     gint msgnum, GHashTable *folder_table)
593 {
594         MsgInfo * msginfo;
595         gchar * filename;
596         MsgFlags  msgflags = { 0, 0 };
597
598         if (item == NULL) {
599                 g_warning(_("folderitem not set"));
600                 return;
601         }
602
603         filename = folder_item_fetch_msg(item, msgnum);
604
605         if (filename == NULL) {
606                 g_warning(_("filename is not set"));
607                 return;
608         }
609
610         msginfo = procheader_parse(filename, msgflags, TRUE);
611         
612         g_free(filename);
613
614         if (msginfo == NULL) {
615                 g_warning(_("could not get info for %s"), filename);
616                 return;
617         }
618
619         msginfo->folder = item;
620         msginfo->msgnum = msgnum;
621
622         filter_msginfo(filtering_list, msginfo, folder_table);
623 }
624
625
626 /******************************************************************************/
627
628 /* revised filtering system.  
629  * 
630  * 07/18/01     alfons          initial revisement
631  */
632
633 /* (alfons)
634  * 
635  * the revised filtering system does not rely on a message being "registered"
636  * in one of sylpheed's folders. it now uses the file sylpheed created on
637  * incorporation of mail. 
638  * i do use MsgInfo data; that's needed by the matcher, but for the rest
639  * the MsgInfo is not really used. */
640  
641
642 /* add_mark() - adds a mark for a file */
643 static void add_mark(FolderItem *folder, gint msgnum, MsgPermFlags flags)
644 {
645         gchar * dest_path;
646         FILE * fp;
647
648         if (folder->folder->type == F_MH) {
649                 dest_path = folder_item_get_path(folder);
650                 if (!is_dir_exist(dest_path))
651                         make_dir_hier(dest_path);
652                 
653                 if (dest_path == NULL) {
654                         g_warning(_("Can't open mark file.\n"));
655                         return;
656                 }
657                 
658                 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
659                     == NULL) {
660                         g_warning(_("Can't open mark file.\n"));
661                         return;
662                 }
663
664                 /* TODO: straight from procmsg.c:procmsg_write_flags()
665                  * should update this when mark file version changes */
666 #if MARK_VERSION == 2            
667                 WRITE_CACHE_DATA_INT(msgnum, fp);
668                 WRITE_CACHE_DATA_INT(flags, fp);
669 #else
670 #error should rewrite the above for new  mark version   
671                 /* paste the source code of procmsg.c:procmsg_write_flags() */
672 #endif
673                 fclose(fp);
674                 return;
675         }
676         return;
677 }
678
679 /* prepare_destination() - prepares destination folder by registering it 
680  * in the global folder table (if not already there). it returns TRUE if
681  * successful. it also returns the FolderItem for the destination */
682 static gboolean prepare_destination(const gchar *destination, FolderItem **item, 
683                                     GHashTable *folder_table)
684 {
685         gint val;
686         FolderItem *result;
687
688         result = folder_find_item_from_identifier(destination);
689         if (!result)
690                 return FALSE;
691         if (folder_table) {
692                 *item = result;
693                 val = GPOINTER_TO_INT(g_hash_table_lookup(folder_table, *item));
694                 if (val == 0) {
695                         folder_item_scan(*item);
696                         g_hash_table_insert(folder_table, *item, GINT_TO_POINTER(1));
697                 }
698         }
699         return TRUE;
700 }
701
702 /* filter_incoming_perform_actions() - performs actions on incoming
703  * message. this function handles updating marks a little bit smarter;
704  * remember that marks can only be appended. */
705 static gboolean filter_incoming_perform_actions(FolderItem *default_folder, 
706                                                 MsgInfo    *msginfo,
707                                                 GHashTable *folder_table)
708 {
709         /* matching actions for msginfo */
710         struct matching_action {
711                 FilteringAction         *action;
712                 struct matching_action  *next;
713         };
714
715         struct matching_action  ma_head = { NULL, NULL };
716         struct matching_action *ma_tail = &ma_head;
717
718         /* need this for the forwarding stuff */
719         PrefsAccount *account;
720         Compose      *compose;
721         MsgFlags      tmp;
722         gchar        *fwd_msg_name;
723         gint          val;
724         MsgInfo      *fwd_msg;
725
726         MsgFlags     markflags = { 0, 0 };
727
728         /* use the global prefs_filtering list */
729         GSList *     list = global_processing;
730
731         /* things we get after having added a message to a folder */
732         FolderItem  *dest_folder;
733         gint         msgnum;
734         
735         /* after performing certain action, we may have to copy 
736          * the message to inbox too */
737         gboolean     copy_to_inbox_too = TRUE;
738         
739         /* filename is only put in msginfo->folder if MSG_FILTERING is set. */
740         const gchar *filename = (gchar *) msginfo->folder;
741         
742         MsgPermFlags flags;
743         gboolean     stop;
744         gchar        *cmd;
745
746         /* build list of matching actions */
747         for (; list != NULL; list = g_slist_next(list)) {
748                 FilteringProp *prop = (FilteringProp *) list->data;
749                 
750                 if (!matcherlist_match(prop->matchers, msginfo))
751                         continue;
752
753                 if (NULL == (ma_tail->next = alloca(sizeof(struct matching_action)))) {
754                         debug_print(_("action allocation error\n"));
755                         break;
756                 }
757
758                 debug_print("*** action %d\n", prop->action->type);
759
760                 ma_tail->action = prop->action;
761                 ma_tail->next->action = NULL;
762                 ma_tail->next->next = NULL;
763                 ma_tail = ma_tail->next;
764         }
765
766         debug_print("*** collecting marks\n");          
767         
768         /* collect all marks */
769         for (ma_tail = &ma_head, stop = FALSE; ma_tail->action && !stop; ma_tail = ma_tail->next) {
770                 switch (ma_tail->action->type) {
771                 
772                 case MATCHACTION_MARK:
773                         MSG_SET_PERM_FLAGS(markflags, MSG_MARKED);
774                         break;
775                         
776                 case MATCHACTION_UNMARK:
777                         MSG_UNSET_PERM_FLAGS(markflags, MSG_MARKED);
778                         break;
779                 
780                 case MATCHACTION_MARK_AS_READ:
781                         MSG_UNSET_PERM_FLAGS(markflags, MSG_UNREAD);
782                         break;
783
784                 case MATCHACTION_MARK_AS_UNREAD:                        
785                         MSG_SET_PERM_FLAGS(markflags, MSG_UNREAD);
786                         break;
787
788                 case MATCHACTION_COLOR:
789                         MSG_SET_COLORLABEL_VALUE(markflags, ma_tail->action->labelcolor);
790                         break;
791
792                 /* UNCONTINUABLE */
793                 case MATCHACTION_FORWARD:
794                 case MATCHACTION_FORWARD_AS_ATTACHMENT:
795                 case MATCHACTION_BOUNCE:
796                 case MATCHACTION_MOVE:
797                 case MATCHACTION_DELETE:
798                         stop = TRUE;
799                         break;
800                 
801                 default:
802                         break;
803                 }
804         }
805
806
807         /* msgnum & dest_folder should have valid values after a succesful
808          * drop; in this case there is a MsgInfo available */
809         msgnum      = -1;
810         dest_folder = NULL;
811
812         debug_print("*** performing actions\n"); 
813         
814         for (ma_tail = &ma_head ; ma_tail->action; ma_tail = ma_tail->next) {
815
816                 /* there are variables you have to use when defining new actions:
817                  *
818                  * copy_to_inbox_too - if the original message should be copied to the inbox 
819                  *                     (default_folder) too.
820                  * 
821                  * also note that after dropping it to a folder (folder_item_add_msg()) you have
822                  * to mark the message, just to make sure any defined mark actions are applied. */
823                  
824 #define ACTION  (ma_tail->action->type) 
825
826                 /* C O N T I N U A B L E */
827
828                 if (MATCHACTION_COPY == ACTION) {
829                         debug_print("*** performing copy\n");
830                         copy_to_inbox_too = TRUE;
831                         if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
832                                 debug_print("Rule failed: unknown destination %s\n", ma_tail->action->destination);
833                                 continue; /* try next action */
834                         }
835                         if (0 > (msgnum = folder_item_add_msg(dest_folder, filename, FALSE))) {
836                                 debug_print(_("Rule failed: could not copy to folder %s\n"),
837                                             ma_tail->action->destination);
838                                 continue; /* try next action */     
839                         }   
840                         flags = msginfo->flags.perm_flags | markflags.perm_flags;
841                         add_mark(dest_folder, msgnum, flags);
842                 }               
843                 else if (MATCHACTION_EXECUTE == ACTION) {
844                         debug_print("*** performing exec\n");
845                         copy_to_inbox_too = TRUE;
846                         
847                         /* matching_build_command() knows about filtering */
848                         cmd = matching_build_command(ma_tail->action->destination, msginfo);
849                         if (cmd) { 
850                                 system(cmd);
851                                 g_free(cmd);
852                         }
853                         else
854                                 debug_print(_("Rule failed: no command line\n"));
855                 } 
856
857                 /* U N C O N T I N U A B L E */
858                 
859                 else if (MATCHACTION_FORWARD == ACTION
860                          || MATCHACTION_FORWARD_AS_ATTACHMENT == ACTION) {
861                         debug_print("*** performing forward\n");
862
863                         /* forwarding messages is complicated because there's currently no 
864                          * way to forward a message using "filenames"; you can only forward
865                          * a message if you have its MsgInfo. this means we have to drop
866                          * the message first */ 
867                         if (0 > (msgnum = folder_item_add_msg(default_folder, filename, FALSE))) {
868                                 debug_print(_("Rule failed: could not forward\n"));
869                                 copy_to_inbox_too = TRUE;
870                                 continue;
871                         }
872                                 
873                         flags = msginfo->flags.perm_flags | markflags.perm_flags;
874                         flags |= MSG_FORWARDED;
875                         add_mark(default_folder, msgnum, flags);
876
877                         /* grab the dropped message */
878                         fwd_msg_name = folder_item_fetch_msg(default_folder, msgnum);
879
880                         tmp.perm_flags = tmp.tmp_flags = 0;
881                         fwd_msg = procheader_parse(fwd_msg_name, tmp, TRUE);
882
883                         fwd_msg->folder = default_folder;
884                         fwd_msg->msgnum = msgnum;
885
886                         /* do the compose_XXX stuff */
887                         account = account_find_from_id(ma_tail->action->account_id);
888                         compose = compose_forward(account, fwd_msg, ACTION == MATCHACTION_FORWARD ? FALSE : TRUE);
889                         if (compose->account->protocol == A_NNTP)
890                                 compose_entry_append(compose, ma_tail->action->destination,
891                                                      COMPOSE_NEWSGROUPS);
892                         else
893                                 compose_entry_append(compose, ma_tail->action->destination,
894                                                      COMPOSE_TO);
895
896                         compose_send(compose);
897
898                         procmsg_msginfo_free(fwd_msg);
899                         
900                         gtk_widget_destroy(compose->window);
901                         break;
902                 }                       
903                 else if (MATCHACTION_BOUNCE == ACTION) {
904                         if (0 > (msgnum = folder_item_add_msg(default_folder, filename, FALSE))) {
905                                 debug_print(_("Rule failed: could not forward\n"));
906                                 copy_to_inbox_too = TRUE;
907                                 continue;
908                         }
909                                 
910                         /* grab the dropped message */
911                         fwd_msg_name = folder_item_fetch_msg(default_folder, msgnum);
912
913                         fwd_msg = procheader_parse(fwd_msg_name, tmp, TRUE);
914
915                         fwd_msg->folder = default_folder;
916                         fwd_msg->msgnum = msgnum;
917
918                         /* do the compose_XXX stuff */
919                         account = account_find_from_id(ma_tail->action->account_id);
920                         compose = compose_bounce(account, fwd_msg);
921                         if (compose->account->protocol == A_NNTP)
922                                 break;
923                         else
924                                 compose_entry_append(compose, ma_tail->action->destination,
925                                                      COMPOSE_TO);
926
927                         compose_send(compose);
928
929                         procmsg_msginfo_free(fwd_msg);
930                         
931                         gtk_widget_destroy(compose->window);
932                         break;
933                 }                       
934                 else if (MATCHACTION_DELETE == ACTION) {
935                         debug_print("*** performing delete\n");
936                         copy_to_inbox_too = FALSE;
937
938                         /* drop to Trash */
939                         dest_folder = folder_get_default_trash();
940                         msgnum = folder_item_add_msg(dest_folder, filename, FALSE);
941                         if (msgnum < 0) {
942                                 debug_print(_("Rule failed: could not move to trash"));
943                                 copy_to_inbox_too = TRUE;           
944                         }
945                         else {
946                                 flags = msginfo->flags.perm_flags | markflags.perm_flags;
947                                 add_mark(dest_folder, msgnum, flags);
948                         }
949                         break;                          
950                 }
951                 else if (MATCHACTION_MOVE == ACTION) {
952                         debug_print("*** performing move\n");
953                         copy_to_inbox_too = FALSE;
954                         
955                         if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
956                                 copy_to_inbox_too = TRUE;
957                                 break;
958                         }
959                                 
960                         msgnum = folder_item_add_msg(dest_folder, filename, FALSE);
961                         if (msgnum < 0) {
962                                 debug_print(_("Rule failed: could not move to folder %s\n"),
963                                             ma_tail->action->destination);
964                                 copy_to_inbox_too = TRUE;                                          
965                                 break;
966                         }   
967                         
968                         flags = msginfo->flags.perm_flags | markflags.perm_flags;
969                         add_mark(dest_folder, msgnum, flags);
970                         break;
971                 }
972         }
973
974         /* may need to copy it to inbox too */
975         if (copy_to_inbox_too) {
976                 debug_print("*** performing inbox copy\n");
977                 msgnum = folder_item_add_msg(default_folder, filename, TRUE);
978                 if (msgnum < 0) {
979                         debug_print(_("error copying incoming file %s to inbox\n"), 
980                                     filename);
981                         return FALSE;                               
982                 }
983                 flags = msginfo->flags.perm_flags | markflags.perm_flags;
984                 add_mark(default_folder, msgnum, flags);
985         }
986         else {
987                 debug_print("*** unlinking\n");
988                 if (unlink(filename) < 0) 
989                         debug_print(_("error deleting incoming message file\n"));
990         }
991
992 #undef ACTION
993
994         return TRUE;
995 }
996
997 static void filter_incoming_msginfo(FolderItem *default_folder, MsgInfo *msginfo, 
998                                     GHashTable *folder_table)
999 {
1000         filter_incoming_perform_actions(default_folder, msginfo, folder_table); 
1001 }
1002
1003 /* filter_incoming_message() - tries to apply a filter on one incoming message.
1004  * it also handles the case when there's no filter match */
1005 void filter_incoming_message(FolderItem *default_folder, const gchar *file_name, 
1006                              GHashTable *folder_table)
1007 {
1008         MsgInfo *msginfo;
1009         MsgFlags msgflags = { 0, 0 };
1010         
1011         /* make an "uncomplete" msginfo. it's incomplete because it doesn't
1012          * have a message number / folder yet. */
1013         if (NULL == (msginfo = procheader_parse(file_name, msgflags, TRUE))) {
1014                 g_warning(_("error filtering incoming message %s\n"), 
1015                           file_name);
1016                 return;         
1017         }
1018
1019         /* let matcher know that this is a message that has no
1020          * valid body data yet. */
1021         MSG_SET_TMP_FLAGS(msginfo->flags, MSG_FILTERING);
1022         msginfo->folder = (FolderItem *) g_strdup(file_name); /* actually storing a pointer to a string */
1023         msginfo->msgnum = 0;
1024
1025         filter_incoming_msginfo(default_folder, msginfo, folder_table);
1026
1027         g_free(msginfo->folder);
1028         procmsg_msginfo_free(msginfo);
1029 }
1030
1031 /****************************************************************************/
1032
1033
1034
1035 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
1036 {
1037         gchar *command_str;
1038
1039         command_str = get_matchparser_tab_str(action->type);
1040
1041         if (command_str == NULL)
1042                 return NULL;
1043
1044         switch(action->type) {
1045         case MATCHACTION_MOVE:
1046         case MATCHACTION_COPY:
1047         case MATCHACTION_EXECUTE:
1048                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
1049                 return dest;
1050
1051         case MATCHACTION_DELETE:
1052         case MATCHACTION_MARK:
1053         case MATCHACTION_UNMARK:
1054         case MATCHACTION_MARK_AS_READ:
1055         case MATCHACTION_MARK_AS_UNREAD:
1056                 g_snprintf(dest, destlen, "%s", command_str);
1057                 return dest;
1058
1059         case MATCHACTION_BOUNCE:
1060         case MATCHACTION_FORWARD:
1061         case MATCHACTION_FORWARD_AS_ATTACHMENT:
1062                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
1063                 return dest; 
1064
1065         case MATCHACTION_COLOR:
1066                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
1067                 return dest;  
1068
1069         default:
1070                 return NULL;
1071         }
1072 }
1073
1074 gchar * filteringprop_to_string(FilteringProp * prop)
1075 {
1076         gchar *list_str;
1077         gchar *action_str;
1078         gchar *filtering_str;
1079         gchar  buf[256];
1080
1081         action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
1082
1083         if (action_str == NULL)
1084                 return NULL;
1085
1086         list_str = matcherlist_to_string(prop->matchers);
1087
1088         if (list_str == NULL)
1089                 return NULL;
1090
1091         filtering_str = g_strconcat(list_str, " ", action_str, NULL);
1092         g_free(list_str);
1093
1094         return filtering_str;
1095 }
1096
1097 void prefs_filtering_free(GSList * prefs_filtering)
1098 {
1099         while (prefs_filtering != NULL) {
1100                 FilteringProp * filtering = (FilteringProp *)
1101                         prefs_filtering->data;
1102                 filteringprop_free(filtering);
1103                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
1104         }
1105 }
1106
1107 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
1108 {
1109         FolderItem *item = node->data;
1110
1111         if(!item->prefs)
1112                 return FALSE;
1113
1114         prefs_filtering_free(item->prefs->processing);
1115         item->prefs->processing = NULL;
1116
1117         return FALSE;
1118 }
1119
1120 void prefs_filtering_clear()
1121 {
1122         GList * cur;
1123
1124         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
1125                 Folder *folder;
1126
1127                 folder = (Folder *) cur->data;
1128                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1129                                 prefs_filtering_free_func, NULL);
1130         }
1131
1132         prefs_filtering_free(global_processing);
1133         global_processing = NULL;
1134 }