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) {
338 val = GPOINTER_TO_INT(g_hash_table_lookup
339 (folder_table, dest_folder));
341 folder_item_scan(dest_folder);
342 g_hash_table_insert(folder_table, dest_folder,
348 case MATCHACTION_COPY:
350 folder_find_item_from_identifier(action->destination);
355 if (folder_item_copy_msg(dest_folder, info) == -1)
359 val = GPOINTER_TO_INT(g_hash_table_lookup
360 (folder_table, dest_folder));
362 folder_item_scan(dest_folder);
363 g_hash_table_insert(folder_table, dest_folder,
369 case MATCHACTION_DELETE:
370 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
374 case MATCHACTION_MARK:
375 MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
378 case MATCHACTION_UNMARK:
379 MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
382 case MATCHACTION_MARK_AS_READ:
383 MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
386 case MATCHACTION_MARK_AS_UNREAD:
387 MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
390 case MATCHACTION_COLOR:
391 MSG_SET_COLORLABEL_VALUE(info->flags, action->labelcolor);
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,
401 compose_entry_append(compose, action->destination,
404 val = compose_send(compose);
406 gtk_widget_destroy(compose->window);
410 gtk_widget_destroy(compose->window);
413 case MATCHACTION_FORWARD_AS_ATTACHMENT:
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,
421 compose_entry_append(compose, action->destination,
424 val = compose_send(compose);
426 gtk_widget_destroy(compose->window);
429 gtk_widget_destroy(compose->window);
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)
438 compose_entry_append(compose, action->destination,
441 val = compose_send(compose);
443 gtk_widget_destroy(compose->window);
447 gtk_widget_destroy(compose->window);
450 case MATCHACTION_EXECUTE:
451 cmd = matching_build_command(action->destination, info);
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.
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.
476 static gboolean filteringprop_apply(FilteringProp * filtering, MsgInfo * info,
477 GHashTable *folder_table)
479 if (matcherlist_match(filtering->matchers, info)) {
484 if (FALSE == (result = filteringaction_apply(filtering->action, info,
486 action_str = filteringaction_to_string(buf, sizeof buf, filtering->action);
487 g_warning(_("action %s could not be applied"), action_str);
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 */
512 void filter_msginfo(GSList * filtering_list, MsgInfo * info,
513 GHashTable *folder_table)
520 g_warning(_("msginfo is not set"));
524 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
525 FilteringProp * filtering = (FilteringProp *) l->data;
526 if (TRUE == (result = filteringprop_apply(filtering, info, folder_table)))
530 /* drop in inbox too */
534 inbox = folder_get_default_inbox();
537 if (folder_item_move_msg(inbox, info) == -1) {
538 debug_print(_("*** Could not drop message in inbox; still in .processing\n"));
543 val = GPOINTER_TO_INT(g_hash_table_lookup
544 (folder_table, inbox));
546 folder_item_scan(inbox);
547 g_hash_table_insert(folder_table, inbox,
554 void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
555 GHashTable *folder_table)
560 g_warning(_("msginfo is not set"));
564 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
565 FilteringProp * filtering = (FilteringProp *) l->data;
567 switch (filtering->action->type) {
568 case MATCHACTION_MOVE:
569 case MATCHACTION_DELETE:
570 if (filteringprop_apply(filtering, info, folder_table))
576 void filter_message(GSList * filtering_list, FolderItem * item,
577 gint msgnum, GHashTable *folder_table)
581 MsgFlags msgflags = { 0, 0 };
584 g_warning(_("folderitem not set"));
588 filename = folder_item_fetch_msg(item, msgnum);
590 if (filename == NULL) {
591 g_warning(_("filename is not set"));
595 msginfo = procheader_parse(filename, msgflags, TRUE);
599 if (msginfo == NULL) {
600 g_warning(_("could not get info for %s"), filename);
604 msginfo->folder = item;
605 msginfo->msgnum = msgnum;
607 filter_msginfo(filtering_list, msginfo, folder_table);
610 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
614 command_str = get_matchparser_tab_str(action->type);
616 if (command_str == NULL)
619 switch(action->type) {
620 case MATCHACTION_MOVE:
621 case MATCHACTION_COPY:
622 case MATCHACTION_EXECUTE:
623 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
626 case MATCHACTION_DELETE:
627 case MATCHACTION_MARK:
628 case MATCHACTION_UNMARK:
629 case MATCHACTION_MARK_AS_READ:
630 case MATCHACTION_MARK_AS_UNREAD:
631 g_snprintf(dest, destlen, "%s", command_str);
634 case MATCHACTION_BOUNCE:
635 case MATCHACTION_FORWARD:
636 case MATCHACTION_FORWARD_AS_ATTACHMENT:
637 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
640 case MATCHACTION_COLOR:
641 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
649 gchar * filteringprop_to_string(FilteringProp * prop)
653 gchar *filtering_str;
656 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
658 if (action_str == NULL)
661 list_str = matcherlist_to_string(prop->matchers);
663 if (list_str == NULL)
666 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
669 return filtering_str;
672 void prefs_filtering_free(GSList * prefs_filtering)
674 while (prefs_filtering != NULL) {
675 FilteringProp * filtering = (FilteringProp *)
676 prefs_filtering->data;
677 filteringprop_free(filtering);
678 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
682 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
684 FolderItem *item = node->data;
689 prefs_filtering_free(item->prefs->processing);
690 item->prefs->processing = NULL;
695 void prefs_filtering_clear()
699 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
702 folder = (Folder *) cur->data;
703 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
704 prefs_filtering_free_func, NULL);
707 prefs_filtering_free(global_processing);
708 global_processing = NULL;