make sure that we use the inbox set per account when filtering
[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 /* (alfons) - Just a quick note of how this filtering module works on 
21  * new (arriving) messages.
22  * 
23  * 1) as an initialization step, code in inc.c and mbox.c set up the 
24  *    drop folder to the inbox (see inc.c and mbox.c).
25  *
26  * 2) the message is actually being copied to the drop folder using
27  *    folder_item_add_msg(dropfolder, file, TRUE). this function
28  *    eventually calls mh->add_msg(). however, the important thing
29  *    about this function is, is that the folder is not yet updated
30  *    to reflect the copy. i don't know about the validity of this
31  *    assumption, however, the filtering code assumes this and
32  *    updates the marks itself.
33  *
34  * 3) technically there's nothing wrong with the matcher (the 
35  *    piece of code which matches search strings). there's
36  *    one gotcha in procmsg.c:procmsg_get_message_file(): it
37  *    only reads a message file based on a MsgInfo. for design
38  *    reasons the filtering system should read directly from
39  *    a file (based on the file's name).
40  *
41  * 4) after the matcher sorts out any matches, it looks at the
42  *    action. this part again pushes the folder system design
43  *    to its limits. based on the assumption in 2), the matcher
44  *    knows the message has not been added to the folder system yet.
45  *    it can happily update mark files, and in fact it does.
46  * 
47  */ 
48
49 #include "defs.h"
50 #include <ctype.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <errno.h>
54 #include <gtk/gtk.h>
55 #include <stdio.h>
56 #include "intl.h"
57 #include "utils.h"
58 #include "procheader.h"
59 #include "matcher.h"
60 #include "filtering.h"
61 #include "prefs.h"
62 #include "compose.h"
63
64 #define PREFSBUFSIZE            1024
65
66 GSList * global_processing = NULL;
67
68 #define STRLEN_WITH_CHECK(expr) \
69         strlen_with_check(#expr, __LINE__, expr)
70                 
71 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
72 {
73         if (str) 
74                 return strlen(str);
75         else {
76                 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
77                 return 0;
78         }
79 }
80
81 FilteringAction * filteringaction_new(int type, int account_id,
82                                       gchar * destination,
83                                       gint labelcolor)
84 {
85         FilteringAction * action;
86
87         action = g_new0(FilteringAction, 1);
88
89         action->type = type;
90         action->account_id = account_id;
91         if (destination)
92                 action->destination = g_strdup(destination);
93         action->labelcolor = labelcolor;        
94         return action;
95 }
96
97 void filteringaction_free(FilteringAction * action)
98 {
99         if (action->destination)
100                 g_free(action->destination);
101         g_free(action);
102 }
103
104 FilteringProp * filteringprop_new(MatcherList * matchers,
105                                   FilteringAction * action)
106 {
107         FilteringProp * filtering;
108
109         filtering = g_new0(FilteringProp, 1);
110         filtering->matchers = matchers;
111         filtering->action = action;
112
113         return filtering;
114 }
115
116 void filteringprop_free(FilteringProp * prop)
117 {
118         matcherlist_free(prop->matchers);
119         filteringaction_free(prop->action);
120         g_free(prop);
121 }
122
123 /* filteringaction_update_mark() - updates a mark for a message. note that
124  * the message should not have been moved or copied. remember that the
125  * procmsg_open_mark_file(PATH, TRUE) actually _appends_ a new record.
126  */
127 static gboolean filteringaction_update_mark(MsgInfo * info)
128 {
129         gchar * dest_path;
130         FILE * fp;
131
132         if (info->folder->folder->type == F_MH) {
133                 dest_path = folder_item_get_path(info->folder);
134                 if (!is_dir_exist(dest_path))
135                         make_dir_hier(dest_path);
136                 
137                 if (dest_path == NULL) {
138                         g_warning(_("Can't open mark file.\n"));
139                         return FALSE;
140                 }
141                 
142                 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
143                     == NULL) {
144                         g_warning(_("Can't open mark file.\n"));
145                         return FALSE;
146                 }
147                 
148                 procmsg_write_flags(info, fp);
149                 fclose(fp);
150                 return TRUE;
151         }
152         return FALSE;
153 }
154
155 static gchar * filteringaction_execute_command(gchar * cmd, MsgInfo * info)
156 {
157         gchar * s = cmd;
158         gchar * filename = NULL;
159         gchar * processed_cmd;
160         gchar * p;
161         gint size;
162
163         matcher_unescape_str(cmd);
164
165         size = strlen(cmd) + 1;
166         while (*s != '\0') {
167                 if (*s == '%') {
168                         s++;
169                         switch (*s) {
170                         case '%':
171                                 size -= 1;
172                                 break;
173                         case 's': /* subject */
174                                 size += STRLEN_WITH_CHECK(info->subject) - 2;
175                                 break;
176                         case 'f': /* from */
177                                 size += STRLEN_WITH_CHECK(info->from) - 2;
178                                 break;
179                         case 't': /* to */
180                                 size += STRLEN_WITH_CHECK(info->to) - 2;
181                                 break;
182                         case 'c': /* cc */
183                                 size += STRLEN_WITH_CHECK(info->cc) - 2;
184                                 break;
185                         case 'd': /* date */
186                                 size += STRLEN_WITH_CHECK(info->date) - 2;
187                                 break;
188                         case 'i': /* message-id */
189                                 size += STRLEN_WITH_CHECK(info->msgid) - 2;
190                                 break;
191                         case 'n': /* newsgroups */
192                                 size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
193                                 break;
194                         case 'r': /* references */
195                                 size += STRLEN_WITH_CHECK(info->references) - 2;
196                                 break;
197                         case 'F': /* file */
198                                 filename = folder_item_fetch_msg(info->folder, info->msgnum);
199                                 if (filename == NULL) {
200                                         g_warning(_("filename is not set"));
201                                         return NULL;
202                                 }
203                                 else
204                                         size += strlen(filename) - 2;
205                                 break;
206                         }
207                         s++;
208                 }
209                 else s++;
210         }
211
212
213         processed_cmd = g_new0(gchar, size);
214         s = cmd;
215         p = processed_cmd;
216
217         while (*s != '\0') {
218                 if (*s == '%') {
219                         s++;
220                         switch (*s) {
221                         case '%':
222                                 *p = '%';
223                                 p++;
224                                 break;
225                         case 's': /* subject */
226                                 if (info->subject != NULL)
227                                         strcpy(p, info->subject);
228                                 else
229                                         strcpy(p, "(none)");
230                                 p += strlen(p);
231                                 break;
232                         case 'f': /* from */
233                                 if (info->from != NULL)
234                                         strcpy(p, info->from);
235                                 else
236                                         strcpy(p, "(none)");
237                                 p += strlen(p);
238                                 break;
239                         case 't': /* to */
240                                 if (info->to != NULL)
241                                         strcpy(p, info->to);
242                                 else
243                                         strcpy(p, "(none)");
244                                 p += strlen(p);
245                                 break;
246                         case 'c': /* cc */
247                                 if (info->cc != NULL)
248                                         strcpy(p, info->cc);
249                                 else
250                                         strcpy(p, "(none)");
251                                 p += strlen(p);
252                                 break;
253                         case 'd': /* date */
254                                 if (info->date != NULL)
255                                         strcpy(p, info->date);
256                                 else
257                                         strcpy(p, "(none)");
258                                 p += strlen(p);
259                                 break;
260                         case 'i': /* message-id */
261                                 if (info->msgid != NULL)
262                                         strcpy(p, info->msgid);
263                                 else
264                                         strcpy(p, "(none)");
265                                 p += strlen(p);
266                                 break;
267                         case 'n': /* newsgroups */
268                                 if (info->newsgroups != NULL)
269                                         strcpy(p, info->newsgroups);
270                                 else
271                                         strcpy(p, "(none)");
272                                 p += strlen(p);
273                                 break;
274                         case 'r': /* references */
275                                 if (info->references != NULL)
276                                         strcpy(p, info->references);
277                                 else
278                                         strcpy(p, "(none)");
279                                 p += strlen(p);
280                                 break;
281                         case 'F': /* file */
282                                 strcpy(p, filename);
283                                 p += strlen(p);
284                                 break;
285                         default:
286                                 *p = '%';
287                                 p++;
288                                 *p = *s;
289                                 p++;
290                                 break;
291                         }
292                         s++;
293                 }
294                 else {
295                         *p = *s;
296                         p++;
297                         s++;
298                 }
299         }
300         return processed_cmd;
301 }
302
303 /*
304   fitleringaction_apply
305   runs the action on one MsgInfo
306   return value : return TRUE if the action could be applied
307 */
308
309 #define CHANGE_FLAGS(msginfo) \
310 { \
311 if (msginfo->folder->folder->change_flags != NULL) \
312 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
313                                       msginfo->folder, \
314                                       msginfo); \
315 }
316
317 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
318                                       GHashTable *folder_table)
319 {
320         FolderItem * dest_folder;
321         gint val;
322         Compose * compose;
323         PrefsAccount * account;
324         gchar * cmd;
325
326         switch(action->type) {
327         case MATCHACTION_MOVE:
328                 dest_folder =
329                         folder_find_item_from_identifier(action->destination);
330                 if (!dest_folder)
331                         return FALSE;
332                 
333                 if (folder_item_move_msg(dest_folder, info) == -1) {
334                         return FALSE;
335                 }       
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                 }
346                 return TRUE;
347
348         case MATCHACTION_COPY:
349                 dest_folder =
350                         folder_find_item_from_identifier(action->destination);
351
352                 if (!dest_folder)
353                         return FALSE;
354
355                 if (folder_item_copy_msg(dest_folder, info) == -1)
356                         return FALSE;
357
358                 if (folder_table) {
359                         val = GPOINTER_TO_INT(g_hash_table_lookup
360                                               (folder_table, dest_folder));
361                         if (val == 0) {
362                                 folder_item_scan(dest_folder);
363                                 g_hash_table_insert(folder_table, dest_folder,
364                                                     GINT_TO_POINTER(1));
365                         }
366                 }
367                 return TRUE;
368
369         case MATCHACTION_DELETE:
370                 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
371                         return FALSE;
372                 return TRUE;
373
374         case MATCHACTION_MARK:
375                 MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
376                 return TRUE;
377
378         case MATCHACTION_UNMARK:
379                 MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
380                 return TRUE;
381                 
382         case MATCHACTION_MARK_AS_READ:
383                 MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
384                 return TRUE;
385
386         case MATCHACTION_MARK_AS_UNREAD:
387                 MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
388                 return TRUE;
389         
390         case MATCHACTION_COLOR:
391                 MSG_SET_COLORLABEL_VALUE(info->flags, action->labelcolor);
392                 return TRUE;
393
394         case MATCHACTION_FORWARD:
395                 account = account_find_from_id(action->account_id);
396                 compose = compose_forward(account, info, FALSE);
397                 if (compose->account->protocol == A_NNTP)
398                         compose_entry_append(compose, action->destination,
399                                              COMPOSE_NEWSGROUPS);
400                 else
401                         compose_entry_append(compose, action->destination,
402                                              COMPOSE_TO);
403
404                 val = compose_send(compose);
405                 if (val == 0) {
406                         gtk_widget_destroy(compose->window);
407                         return TRUE;
408                 }
409
410                 gtk_widget_destroy(compose->window);
411                 return FALSE;
412
413         case MATCHACTION_FORWARD_AS_ATTACHMENT:
414
415                 account = account_find_from_id(action->account_id);
416                 compose = compose_forward(account, info, TRUE);
417                 if (compose->account->protocol == A_NNTP)
418                         compose_entry_append(compose, action->destination,
419                                              COMPOSE_NEWSGROUPS);
420                 else
421                         compose_entry_append(compose, action->destination,
422                                              COMPOSE_TO);
423
424                 val = compose_send(compose);
425                 if (val == 0) {
426                         gtk_widget_destroy(compose->window);
427                         return TRUE;
428                 }
429                 gtk_widget_destroy(compose->window);
430                 return FALSE;
431
432         case MATCHACTION_BOUNCE:
433                 account = account_find_from_id(action->account_id);
434                 compose = compose_bounce(account, info);
435                 if (compose->account->protocol == A_NNTP)
436                         break;
437                 else
438                         compose_entry_append(compose, action->destination,
439                                              COMPOSE_TO);
440
441                 val = compose_send(compose);
442                 if (val == 0) {
443                         gtk_widget_destroy(compose->window);
444                         return TRUE;
445                 }
446
447                 gtk_widget_destroy(compose->window);
448                 return FALSE;
449
450         case MATCHACTION_EXECUTE:
451                 cmd = matching_build_command(action->destination, info);
452                 if (cmd == NULL)
453                         return TRUE;
454                 else {
455                         system(cmd);
456                         g_free(cmd);
457                 }
458                 return FALSE;
459
460         default:
461                 return FALSE;
462         }
463 }
464
465 /* filteringprop_apply() - runs the action on one MsgInfo if it matches the 
466  * criterium. certain actions can be followed by other actions. in this
467  * case the function returns FALSE. if an action can not be followed
468  * by others, the function returns TRUE.
469  *
470  * remember that this is because of the fact that msg flags are always
471  * _appended_ to mark files. currently sylpheed does not insert messages 
472  * at a certain index. 
473  * now, after having performed a certain action, the MsgInfo is still
474  * valid for the message. in *this* case the function returns FALSE.
475  */
476 static gboolean filteringprop_apply(FilteringProp * filtering, MsgInfo * info,
477                                     GHashTable *folder_table)
478 {
479         if (matcherlist_match(filtering->matchers, info)) {
480                 gboolean result;
481                 gchar   *action_str;
482                 gchar    buf[256]; 
483
484                 if (FALSE == (result = filteringaction_apply(filtering->action, info,
485                                                folder_table))) {
486                         action_str = filteringaction_to_string(buf, sizeof buf, filtering->action);
487                         g_warning(_("action %s could not be applied"), action_str);
488                 }
489
490                 switch(filtering->action->type) {
491                 case MATCHACTION_MOVE:
492                 case MATCHACTION_DELETE:
493                         return TRUE; /* MsgInfo invalid for message */
494                 case MATCHACTION_EXECUTE:
495                 case MATCHACTION_COPY:
496                 case MATCHACTION_MARK:
497                 case MATCHACTION_MARK_AS_READ:
498                 case MATCHACTION_UNMARK:
499                 case MATCHACTION_MARK_AS_UNREAD:
500                 case MATCHACTION_FORWARD:
501                 case MATCHACTION_FORWARD_AS_ATTACHMENT:
502                 case MATCHACTION_BOUNCE:
503                         return FALSE; /* MsgInfo still valid for message */
504                 default:
505                         return FALSE;
506                 }
507         }
508         else
509                 return FALSE;
510 }
511
512 static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
513                            MsgInfo * info, GHashTable *folder_table)
514 {
515         GSList          *l;
516         gboolean         result;
517         
518         if (info == NULL) {
519                 g_warning(_("msginfo is not set"));
520                 return;
521         }
522         
523         for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
524                 FilteringProp * filtering = (FilteringProp *) l->data;
525                 if (TRUE == (result = filteringprop_apply(filtering, info, folder_table))) 
526                         break;
527         }
528
529         /* drop in inbox too */
530         if (!result) {
531                 gint val;
532
533                 if (folder_item_move_msg(inbox, info) == -1) {
534                         debug_print(_("*** Could not drop message in inbox; still in .processing\n"));
535                         return;
536                 }       
537
538                 if (folder_table) {
539                         val = GPOINTER_TO_INT(g_hash_table_lookup
540                                               (folder_table, inbox));
541                         if (val == 0) {
542                                 folder_item_scan(inbox);
543                                 g_hash_table_insert(folder_table, inbox,
544                                                     GINT_TO_POINTER(1));
545                         }
546                 }
547         }
548 }
549
550 void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
551                                    GHashTable *folder_table)
552 {
553         GSList * l;
554
555         if (info == NULL) {
556                 g_warning(_("msginfo is not set"));
557                 return;
558         }
559         
560         for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
561                 FilteringProp * filtering = (FilteringProp *) l->data;
562
563                 switch (filtering->action->type) {
564                 case MATCHACTION_MOVE:
565                 case MATCHACTION_DELETE:
566                         if (filteringprop_apply(filtering, info, folder_table))
567                                 return;
568                 }
569         }
570 }
571
572 void filter_message(GSList *filtering_list, FolderItem *inbox,
573                     gint msgnum, GHashTable *folder_table)
574 {
575         MsgInfo *msginfo;
576         gchar *filename;
577         MsgFlags  msgflags = { 0, 0 };
578         FolderItem *item = folder_get_default_processing();
579
580         if (item == NULL) {
581                 g_warning(_("folderitem not set"));
582                 return;
583         }
584
585         filename = folder_item_fetch_msg(item, msgnum);
586
587         if (filename == NULL) {
588                 g_warning(_("filename is not set"));
589                 return;
590         }
591
592         msginfo = procheader_parse(filename, msgflags, TRUE);
593         
594         g_free(filename);
595
596         if (msginfo == NULL) {
597                 g_warning(_("could not get info for %s"), filename);
598                 return;
599         }
600
601         msginfo->folder = item;
602         msginfo->msgnum = msgnum;
603
604         filter_msginfo(filtering_list, inbox, msginfo, folder_table);
605 }
606
607 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
608 {
609         gchar *command_str;
610
611         command_str = get_matchparser_tab_str(action->type);
612
613         if (command_str == NULL)
614                 return NULL;
615
616         switch(action->type) {
617         case MATCHACTION_MOVE:
618         case MATCHACTION_COPY:
619         case MATCHACTION_EXECUTE:
620                 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
621                 return dest;
622
623         case MATCHACTION_DELETE:
624         case MATCHACTION_MARK:
625         case MATCHACTION_UNMARK:
626         case MATCHACTION_MARK_AS_READ:
627         case MATCHACTION_MARK_AS_UNREAD:
628                 g_snprintf(dest, destlen, "%s", command_str);
629                 return dest;
630
631         case MATCHACTION_BOUNCE:
632         case MATCHACTION_FORWARD:
633         case MATCHACTION_FORWARD_AS_ATTACHMENT:
634                 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
635                 return dest; 
636
637         case MATCHACTION_COLOR:
638                 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
639                 return dest;  
640
641         default:
642                 return NULL;
643         }
644 }
645
646 gchar * filteringprop_to_string(FilteringProp * prop)
647 {
648         gchar *list_str;
649         gchar *action_str;
650         gchar *filtering_str;
651         gchar  buf[256];
652
653         action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
654
655         if (action_str == NULL)
656                 return NULL;
657
658         list_str = matcherlist_to_string(prop->matchers);
659
660         if (list_str == NULL)
661                 return NULL;
662
663         filtering_str = g_strconcat(list_str, " ", action_str, NULL);
664         g_free(list_str);
665
666         return filtering_str;
667 }
668
669 void prefs_filtering_free(GSList * prefs_filtering)
670 {
671         while (prefs_filtering != NULL) {
672                 FilteringProp * filtering = (FilteringProp *)
673                         prefs_filtering->data;
674                 filteringprop_free(filtering);
675                 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
676         }
677 }
678
679 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
680 {
681         FolderItem *item = node->data;
682
683         if(!item->prefs)
684                 return FALSE;
685
686         prefs_filtering_free(item->prefs->processing);
687         item->prefs->processing = NULL;
688
689         return FALSE;
690 }
691
692 void prefs_filtering_clear()
693 {
694         GList * cur;
695
696         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
697                 Folder *folder;
698
699                 folder = (Folder *) cur->data;
700                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
701                                 prefs_filtering_free_func, NULL);
702         }
703
704         prefs_filtering_free(global_processing);
705         global_processing = NULL;
706 }