2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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.
22 #include <glib/gi18n.h>
31 #include "procheader.h"
33 #include "filtering.h"
34 #include "prefs_gtk.h"
37 #define PREFSBUFSIZE 1024
39 GSList * pre_global_processing = NULL;
40 GSList * post_global_processing = NULL;
41 GSList * filtering_rules = NULL;
43 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
45 #define STRLEN_WITH_CHECK(expr) \
46 strlen_with_check(#expr, __LINE__, expr)
48 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
53 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
58 FilteringAction * filteringaction_new(int type, int account_id,
60 gint labelcolor, gint score)
62 FilteringAction * action;
64 action = g_new0(FilteringAction, 1);
67 action->account_id = account_id;
69 action->destination = g_strdup(destination);
71 action->destination = NULL;
73 action->labelcolor = labelcolor;
74 action->score = score;
78 void filteringaction_free(FilteringAction * action)
80 g_return_if_fail(action);
81 if (action->destination)
82 g_free(action->destination);
86 FilteringProp * filteringprop_new(MatcherList * matchers,
89 FilteringProp * filtering;
91 filtering = g_new0(FilteringProp, 1);
92 filtering->matchers = matchers;
93 filtering->action_list = action_list;
98 static FilteringAction * filteringaction_copy(FilteringAction * src)
100 FilteringAction * new;
102 new = g_new0(FilteringAction, 1);
104 new->type = src->type;
105 new->account_id = src->account_id;
106 if (src->destination)
107 new->destination = g_strdup(src->destination);
109 new->destination = NULL;
110 new->labelcolor = src->labelcolor;
115 FilteringProp * filteringprop_copy(FilteringProp *src)
120 new = g_new0(FilteringProp, 1);
121 new->matchers = g_new0(MatcherList, 1);
123 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
124 MatcherProp *matcher = (MatcherProp *)tmp->data;
126 new->matchers->matchers = g_slist_append(new->matchers->matchers,
127 matcherprop_copy(matcher));
131 new->matchers->bool_and = src->matchers->bool_and;
133 new->action_list = NULL;
135 for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
136 FilteringAction *filtering_action;
138 filtering_action = tmp->data;
140 new->action_list = g_slist_append(new->action_list,
141 filteringaction_copy(filtering_action));
147 void filteringprop_free(FilteringProp * prop)
151 g_return_if_fail(prop);
152 matcherlist_free(prop->matchers);
154 for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
155 filteringaction_free(tmp->data);
161 fitleringaction_apply
162 runs the action on one MsgInfo
163 return value : return TRUE if the action could be applied
166 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
168 FolderItem * dest_folder;
171 PrefsAccount * account;
174 switch(action->type) {
175 case MATCHACTION_MOVE:
177 folder_find_item_from_identifier(action->destination);
179 debug_print("*** folder not found '%s'\n",
180 action->destination ?action->destination :"");
184 if (folder_item_move_msg(dest_folder, info) == -1) {
185 debug_print("*** could not move message\n");
191 case MATCHACTION_COPY:
193 folder_find_item_from_identifier(action->destination);
196 debug_print("*** folder not found '%s'\n",
197 action->destination ?action->destination :"");
201 if (folder_item_copy_msg(dest_folder, info) == -1)
206 case MATCHACTION_DELETE:
207 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
211 case MATCHACTION_MARK:
212 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
215 case MATCHACTION_UNMARK:
216 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
219 case MATCHACTION_LOCK:
220 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
223 case MATCHACTION_UNLOCK:
224 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);
227 case MATCHACTION_MARK_AS_READ:
228 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
231 case MATCHACTION_MARK_AS_UNREAD:
232 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
235 case MATCHACTION_COLOR:
236 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
237 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
240 case MATCHACTION_FORWARD:
241 case MATCHACTION_FORWARD_AS_ATTACHMENT:
242 account = account_find_from_id(action->account_id);
243 compose = compose_forward(account, info,
244 action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
246 compose_entry_append(compose, action->destination,
247 compose->account->protocol == A_NNTP
251 val = compose_send(compose);
253 return val == 0 ? TRUE : FALSE;
255 case MATCHACTION_REDIRECT:
256 account = account_find_from_id(action->account_id);
257 compose = compose_redirect(account, info);
258 if (compose->account->protocol == A_NNTP)
261 compose_entry_append(compose, action->destination,
264 val = compose_send(compose);
266 return val == 0 ? TRUE : FALSE;
268 case MATCHACTION_EXECUTE:
269 cmd = matching_build_command(action->destination, info);
278 case MATCHACTION_SET_SCORE:
279 info->score = action->score;
282 case MATCHACTION_CHANGE_SCORE:
283 info->score += action->score;
286 case MATCHACTION_STOP:
289 case MATCHACTION_HIDE:
299 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
302 g_return_val_if_fail(action_list, FALSE);
303 g_return_val_if_fail(info, FALSE);
304 for (p = action_list; p && p->data; p = g_slist_next(p)) {
305 FilteringAction *a = (FilteringAction *) p->data;
306 if (filteringaction_apply(a, info)) {
307 if (filtering_is_final_action(a))
316 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
318 return matcherlist_match(filtering->matchers, info);
322 *\brief Apply a rule on message.
324 *\param filtering List of filtering rules.
325 *\param info Message to apply rules on.
326 *\param final Variable returning TRUE or FALSE if one of the
327 * encountered actions was final.
328 * See also \ref filtering_is_final_action.
330 *\return gboolean TRUE to continue applying rules.
332 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
335 gboolean result = TRUE;
340 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
341 FilteringAction * action;
345 if (FALSE == (result = filteringaction_apply(action, info))) {
346 g_warning("No further processing after rule %s\n",
347 filteringaction_to_string(buf, sizeof buf, action));
350 if (filtering_is_final_action(action)) {
359 *\brief Check if an action is "final", i.e. should break further
362 *\param filtering_action Action to check.
364 *\return gboolean TRUE if \a filtering_action is final.
366 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
368 switch(filtering_action->type) {
369 case MATCHACTION_MOVE:
370 case MATCHACTION_DELETE:
371 case MATCHACTION_STOP:
372 return TRUE; /* MsgInfo invalid for message */
378 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
384 g_return_val_if_fail(info != NULL, TRUE);
386 for (l = filtering_list, final = FALSE, apply_next = FALSE; l != NULL; l = g_slist_next(l)) {
387 FilteringProp * filtering = (FilteringProp *) l->data;
389 if (filtering_match_condition(filtering, info)) {
390 apply_next = filtering_apply_rule(filtering, info, &final);
396 /* put in inbox if a final rule could not be applied, or
397 * the last rule was not a final one. */
398 if ((final && !apply_next) || !final) {
406 *\brief Filter a message against a list of rules.
408 *\param flist List of filter rules.
409 *\param info Message.
411 *\return gboolean TRUE if filter rules handled the message.
413 *\note Returning FALSE means the message was not handled,
414 * and that the calling code should do the default
415 * processing. E.g. \ref inc.c::inc_start moves the
416 * message to the inbox.
418 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
420 return filter_msginfo(flist, info);
423 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
425 const gchar *command_str;
428 command_str = get_matchparser_tab_str(action->type);
430 if (command_str == NULL)
433 switch(action->type) {
434 case MATCHACTION_MOVE:
435 case MATCHACTION_COPY:
436 case MATCHACTION_EXECUTE:
437 quoted_dest = matcher_quote_str(action->destination);
438 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
442 case MATCHACTION_DELETE:
443 case MATCHACTION_MARK:
444 case MATCHACTION_UNMARK:
445 case MATCHACTION_LOCK:
446 case MATCHACTION_UNLOCK:
447 case MATCHACTION_MARK_AS_READ:
448 case MATCHACTION_MARK_AS_UNREAD:
449 case MATCHACTION_STOP:
450 case MATCHACTION_HIDE:
451 g_snprintf(dest, destlen, "%s", command_str);
454 case MATCHACTION_REDIRECT:
455 case MATCHACTION_FORWARD:
456 case MATCHACTION_FORWARD_AS_ATTACHMENT:
457 quoted_dest = matcher_quote_str(action->destination);
458 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
462 case MATCHACTION_COLOR:
463 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
466 case MATCHACTION_CHANGE_SCORE:
467 case MATCHACTION_SET_SCORE:
468 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
476 gchar * filteringaction_list_to_string(GSList * action_list)
478 gchar *action_list_str;
483 action_list_str = NULL;
484 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
486 FilteringAction * action;
490 action_str = filteringaction_to_string(buf,
493 if (action_list_str != NULL) {
494 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
495 g_free(action_list_str);
498 list_str = g_strdup(action_str);
500 action_list_str = list_str;
503 return action_list_str;
506 gchar * filteringprop_to_string(FilteringProp * prop)
509 gchar *action_list_str;
510 gchar *filtering_str;
512 action_list_str = filteringaction_list_to_string(prop->action_list);
514 if (action_list_str == NULL)
517 list_str = matcherlist_to_string(prop->matchers);
519 if (list_str == NULL) {
520 g_free(action_list_str);
524 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
525 g_free(action_list_str);
528 return filtering_str;
531 void prefs_filtering_free(GSList * prefs_filtering)
533 while (prefs_filtering != NULL) {
534 FilteringProp * filtering = (FilteringProp *)
535 prefs_filtering->data;
536 filteringprop_free(filtering);
537 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
541 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
543 FolderItem *item = node->data;
545 g_return_val_if_fail(item, FALSE);
546 g_return_val_if_fail(item->prefs, FALSE);
548 prefs_filtering_free(item->prefs->processing);
549 item->prefs->processing = NULL;
554 void prefs_filtering_clear(void)
558 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
561 folder = (Folder *) cur->data;
562 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
563 prefs_filtering_free_func, NULL);
566 prefs_filtering_free(filtering_rules);
567 filtering_rules = NULL;
568 prefs_filtering_free(pre_global_processing);
569 pre_global_processing = NULL;
570 prefs_filtering_free(post_global_processing);
571 post_global_processing = NULL;
574 void prefs_filtering_clear_folder(Folder *folder)
576 g_return_if_fail(folder);
577 g_return_if_fail(folder->node);
579 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
580 prefs_filtering_free_func, NULL);
581 /* FIXME: Note folder settings were changed, where the updates? */