2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto & The Sylpheed Claws Team
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.
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.
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.
20 /* (alfons) - Just a quick note of how this filtering module works on
21 * new (arriving) messages.
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).
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.
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).
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.
58 #include "procheader.h"
60 #include "filtering.h"
64 #define PREFSBUFSIZE 1024
66 GSList * global_processing = NULL;
68 #define STRLEN_WITH_CHECK(expr) \
69 strlen_with_check(#expr, __LINE__, expr)
71 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
76 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
81 FilteringAction * filteringaction_new(int type, int account_id,
85 FilteringAction * action;
87 action = g_new0(FilteringAction, 1);
90 action->account_id = account_id;
92 action->destination = g_strdup(destination);
93 action->labelcolor = labelcolor;
97 void filteringaction_free(FilteringAction * action)
99 if (action->destination)
100 g_free(action->destination);
104 FilteringProp * filteringprop_new(MatcherList * matchers,
105 FilteringAction * action)
107 FilteringProp * filtering;
109 filtering = g_new0(FilteringProp, 1);
110 filtering->matchers = matchers;
111 filtering->action = action;
116 void filteringprop_free(FilteringProp * prop)
118 matcherlist_free(prop->matchers);
119 filteringaction_free(prop->action);
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.
127 static gboolean filteringaction_update_mark(MsgInfo * info)
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);
137 if (dest_path == NULL) {
138 g_warning(_("Can't open mark file.\n"));
142 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
144 g_warning(_("Can't open mark file.\n"));
148 procmsg_write_flags(info, fp);
155 static gchar * filteringaction_execute_command(gchar * cmd, MsgInfo * info)
158 gchar * filename = NULL;
159 gchar * processed_cmd;
163 matcher_unescape_str(cmd);
165 size = strlen(cmd) + 1;
173 case 's': /* subject */
174 size += STRLEN_WITH_CHECK(info->subject) - 2;
177 size += STRLEN_WITH_CHECK(info->from) - 2;
180 size += STRLEN_WITH_CHECK(info->to) - 2;
183 size += STRLEN_WITH_CHECK(info->cc) - 2;
186 size += STRLEN_WITH_CHECK(info->date) - 2;
188 case 'i': /* message-id */
189 size += STRLEN_WITH_CHECK(info->msgid) - 2;
191 case 'n': /* newsgroups */
192 size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
194 case 'r': /* references */
195 size += STRLEN_WITH_CHECK(info->references) - 2;
198 filename = folder_item_fetch_msg(info->folder, info->msgnum);
199 if (filename == NULL) {
200 g_warning(_("filename is not set"));
204 size += strlen(filename) - 2;
213 processed_cmd = g_new0(gchar, size);
225 case 's': /* subject */
226 if (info->subject != NULL)
227 strcpy(p, info->subject);
233 if (info->from != NULL)
234 strcpy(p, info->from);
240 if (info->to != NULL)
247 if (info->cc != NULL)
254 if (info->date != NULL)
255 strcpy(p, info->date);
260 case 'i': /* message-id */
261 if (info->msgid != NULL)
262 strcpy(p, info->msgid);
267 case 'n': /* newsgroups */
268 if (info->newsgroups != NULL)
269 strcpy(p, info->newsgroups);
274 case 'r': /* references */
275 if (info->references != NULL)
276 strcpy(p, info->references);
300 return processed_cmd;
304 fitleringaction_apply
305 runs the action on one MsgInfo
306 return value : return TRUE if the action could be applied
309 #define CHANGE_FLAGS(msginfo) \
311 if (msginfo->folder->folder->change_flags != NULL) \
312 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
317 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
318 GHashTable *folder_table)
320 FolderItem * dest_folder;
323 PrefsAccount * account;
326 switch(action->type) {
327 case MATCHACTION_MOVE:
329 folder_find_item_from_identifier(action->destination);
333 if (folder_item_move_msg(dest_folder, info) == -1) {
337 /* WRONG: can not update the mark, because the message has
338 * been moved. info pertains to original location.
339 * folder_item_move_msg() already updated the mark for the
340 * destination folder.
342 filteringaction_update_mark(info);
345 val = GPOINTER_TO_INT(g_hash_table_lookup
346 (folder_table, dest_folder));
348 folder_item_scan(dest_folder);
349 g_hash_table_insert(folder_table, dest_folder,
352 val = GPOINTER_TO_INT(g_hash_table_lookup
353 (folder_table, info->folder));
355 folder_item_scan(info->folder);
356 g_hash_table_insert(folder_table, info->folder,
362 case MATCHACTION_COPY:
364 folder_find_item_from_identifier(action->destination);
369 /* NOTE: the following call *will* update the mark file for
370 * the destination folder. but the original message will
371 * still be there in the inbox. */
373 if (folder_item_copy_msg(dest_folder, info) == -1)
377 val = GPOINTER_TO_INT(g_hash_table_lookup
378 (folder_table, dest_folder));
380 folder_item_scan(dest_folder);
381 g_hash_table_insert(folder_table, dest_folder,
387 case MATCHACTION_DELETE:
388 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
391 /* WRONG: can not update the mark. this would actually add
392 * a bogus record to the mark file for the message's original
395 filteringaction_update_mark(info);
400 case MATCHACTION_MARK:
401 MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
402 filteringaction_update_mark(info);
408 case MATCHACTION_UNMARK:
409 MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
410 filteringaction_update_mark(info);
416 case MATCHACTION_MARK_AS_READ:
417 MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
418 filteringaction_update_mark(info);
424 case MATCHACTION_MARK_AS_UNREAD:
425 MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
426 filteringaction_update_mark(info);
432 case MATCHACTION_FORWARD:
434 account = account_find_from_id(action->account_id);
435 compose = compose_forward(account, info, FALSE);
436 if (compose->account->protocol == A_NNTP)
437 compose_entry_append(compose, action->destination,
440 compose_entry_append(compose, action->destination,
443 val = compose_send(compose);
445 gtk_widget_destroy(compose->window);
449 gtk_widget_destroy(compose->window);
452 case MATCHACTION_FORWARD_AS_ATTACHMENT:
454 account = account_find_from_id(action->account_id);
455 compose = compose_forward(account, info, TRUE);
456 if (compose->account->protocol == A_NNTP)
457 compose_entry_append(compose, action->destination,
460 compose_entry_append(compose, action->destination,
463 val = compose_send(compose);
465 gtk_widget_destroy(compose->window);
469 gtk_widget_destroy(compose->window);
472 case MATCHACTION_BOUNCE:
474 account = account_find_from_id(action->account_id);
475 compose = compose_bounce(account, info);
476 if (compose->account->protocol == A_NNTP)
479 compose_entry_append(compose, action->destination,
482 val = compose_send(compose);
484 gtk_widget_destroy(compose->window);
488 gtk_widget_destroy(compose->window);
491 case MATCHACTION_EXECUTE:
493 cmd = matching_build_command(action->destination, info);
508 /* filteringprop_apply() - runs the action on one MsgInfo if it matches the
509 * criterium. certain actions can be followed by other actions. in this
510 * case the function returns FALSE. if an action can not be followed
511 * by others, the function returns TRUE.
513 * remember that this is because of the fact that msg flags are always
514 * _appended_ to mark files. currently sylpheed does not insert messages
515 * at a certain index.
516 * now, after having performed a certain action, the MsgInfo is still
517 * valid for the message. in *this* case the function returns FALSE.
519 static gboolean filteringprop_apply(FilteringProp * filtering, MsgInfo * info,
520 GHashTable *folder_table)
522 if (matcherlist_match(filtering->matchers, info)) {
527 if (FALSE == (result = filteringaction_apply(filtering->action, info,
529 action_str = filteringaction_to_string(buf, sizeof buf, filtering->action);
530 g_warning(_("action %s could not be applied"), action_str);
533 switch(filtering->action->type) {
534 case MATCHACTION_MOVE:
535 case MATCHACTION_DELETE:
536 return TRUE; /* MsgInfo invalid for message */
537 case MATCHACTION_EXECUTE:
538 case MATCHACTION_COPY:
539 case MATCHACTION_MARK:
540 case MATCHACTION_MARK_AS_READ:
541 case MATCHACTION_UNMARK:
542 case MATCHACTION_MARK_AS_UNREAD:
543 case MATCHACTION_FORWARD:
544 case MATCHACTION_FORWARD_AS_ATTACHMENT:
545 case MATCHACTION_BOUNCE:
546 return FALSE; /* MsgInfo still valid for message */
555 void filter_msginfo(GSList * filtering_list, MsgInfo * info,
556 GHashTable *folder_table)
561 g_warning(_("msginfo is not set"));
565 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
566 FilteringProp * filtering = (FilteringProp *) l->data;
568 if (filteringprop_apply(filtering, info, folder_table))
573 void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
574 GHashTable *folder_table)
579 g_warning(_("msginfo is not set"));
583 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
584 FilteringProp * filtering = (FilteringProp *) l->data;
586 switch (filtering->action->type) {
587 case MATCHACTION_MOVE:
588 case MATCHACTION_DELETE:
589 if (filteringprop_apply(filtering, info, folder_table))
595 void filter_message(GSList * filtering_list, FolderItem * item,
596 gint msgnum, GHashTable *folder_table)
600 MsgFlags msgflags = { 0, 0 };
603 g_warning(_("folderitem not set"));
607 filename = folder_item_fetch_msg(item, msgnum);
609 if (filename == NULL) {
610 g_warning(_("filename is not set"));
614 msginfo = procheader_parse(filename, msgflags, TRUE);
618 if (msginfo == NULL) {
619 g_warning(_("could not get info for %s"), filename);
623 msginfo->folder = item;
624 msginfo->msgnum = msgnum;
626 filter_msginfo(filtering_list, msginfo, folder_table);
629 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
633 command_str = get_matchparser_tab_str(action->type);
635 if (command_str == NULL)
638 switch(action->type) {
639 case MATCHACTION_MOVE:
640 case MATCHACTION_COPY:
641 case MATCHACTION_EXECUTE:
642 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
645 case MATCHACTION_DELETE:
646 case MATCHACTION_MARK:
647 case MATCHACTION_UNMARK:
648 case MATCHACTION_MARK_AS_READ:
649 case MATCHACTION_MARK_AS_UNREAD:
650 g_snprintf(dest, destlen, "%s", command_str);
653 case MATCHACTION_BOUNCE:
654 case MATCHACTION_FORWARD:
655 case MATCHACTION_FORWARD_AS_ATTACHMENT:
656 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
659 case MATCHACTION_COLOR:
660 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
668 gchar * filteringprop_to_string(FilteringProp * prop)
672 gchar *filtering_str;
675 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
677 if (action_str == NULL)
680 list_str = matcherlist_to_string(prop->matchers);
682 if (list_str == NULL)
685 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
688 return filtering_str;
691 void prefs_filtering_free(GSList * prefs_filtering)
693 while (prefs_filtering != NULL) {
694 FilteringProp * filtering = (FilteringProp *)
695 prefs_filtering->data;
696 filteringprop_free(filtering);
697 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
701 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
703 FolderItem *item = node->data;
708 prefs_filtering_free(item->prefs->processing);
709 item->prefs->processing = NULL;
714 void prefs_filtering_clear()
718 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
721 folder = (Folder *) cur->data;
722 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
723 prefs_filtering_free_func, NULL);
726 prefs_filtering_free(global_processing);
727 global_processing = NULL;