Add block address matching pattern feature
authorRicardo Mones <ricardo@mones.org>
Wed, 27 Mar 2013 17:36:23 +0000 (18:36 +0100)
committerRicardo Mones <ricardo@mones.org>
Sun, 31 Mar 2013 01:37:47 +0000 (03:37 +0200)
Should fix bug #2340 'Create block list'.

src/plugins/address_keeper/address_keeper.c
src/plugins/address_keeper/address_keeper_prefs.c
src/plugins/address_keeper/address_keeper_prefs.h

index 085f8b6190f26c9218a0cbc81eeda8218cbac311..be4f05338deb4d82400b51d0b93fa5aa8f9d0bce 100644 (file)
@@ -87,19 +87,43 @@ gchar *get_comment_from_addr(const gchar *addr)
        return NULL;
 }
 
+/**
+ * Checks an address for matching a blocked address pattern.
+ *
+ * @param addr The full address.
+ * @param blocked The regexp matching blocked addresses.
+ *
+ * @return TRUE if given address matches any of the patterns, FALSE otherwise.
+ */
+gboolean matches_blocked_address(const gchar *addr, MatcherList *blocked)
+{
+       if (blocked != NULL) {
+               MsgInfo info;
+
+               info.subject = addr;
+               return matcherlist_match(blocked, &info);
+       }
+       return FALSE;
+}
+
 /**
  * Saves an address to the configured addressbook folder if not known.
  *
  * @param abf The address book file containing target folder.
  * @param folder The address book folder where addresses are added.
  * @param addr The address to be added.
+ * @param blocked The regexp matching blocked addresses.
  */
-void keep_if_unknown(AddressBookFile * abf, ItemFolder * folder, gchar *addr)
+void keep_if_unknown(AddressBookFile * abf, ItemFolder * folder, gchar *addr, MatcherList *blocked)
 {
        gchar *clean_addr = NULL;
        gchar *keepto = addkeeperprefs.addressbook_folder;
 
        debug_print("checking addr '%s'\n", addr);
+       if (matches_blocked_address(addr, blocked)) {
+               debug_print("addr '%s' is blocked by regexp\n", addr);
+               return;
+       }
        clean_addr = g_strdup(addr);
        extract_address(clean_addr);
        start_address_completion(NULL);
@@ -146,6 +170,7 @@ static gboolean addrk_before_send_hook(gpointer source, gpointer data)
        const gchar *to_hdr;
        const gchar *cc_hdr;
        const gchar *bcc_hdr;
+       MatcherList *blocked = NULL;
 
        debug_print("address_keeper invoked!\n");
        if (compose->batch)
@@ -170,6 +195,12 @@ static gboolean addrk_before_send_hook(gpointer source, gpointer data)
        cc_hdr = prefs_common_translated_header_name("Cc:");
        bcc_hdr = prefs_common_translated_header_name("Bcc:");
 
+       if (addkeeperprefs.block_matching_addrs != NULL
+                       && addkeeperprefs.block_matching_addrs[0] != '\0') {
+               blocked = matcherlist_new_from_lines(addkeeperprefs.block_matching_addrs, FALSE);
+               if (blocked == NULL)
+                       g_warning("couldn't allocate matcher");
+       }
        for (cur = compose->header_list; cur != NULL; cur = cur->next) {
                gchar *header;
                gchar *entry;
@@ -183,20 +214,22 @@ static gboolean addrk_before_send_hook(gpointer source, gpointer data)
                if (*entry != '\0') {
                        if (!g_ascii_strcasecmp(header, to_hdr)
                                && addkeeperprefs.keep_to_addrs == TRUE) {
-                               keep_if_unknown(abf, folder, entry);
+                               keep_if_unknown(abf, folder, entry, blocked);
                        }
                        if (!g_ascii_strcasecmp(header, cc_hdr)
                                && addkeeperprefs.keep_cc_addrs == TRUE) {
-                               keep_if_unknown(abf, folder, entry);
+                               keep_if_unknown(abf, folder, entry, blocked);
                        }
                        if (!g_ascii_strcasecmp(header, bcc_hdr)
                                && addkeeperprefs.keep_bcc_addrs == TRUE) {
-                               keep_if_unknown(abf, folder, entry);
+                               keep_if_unknown(abf, folder, entry, blocked);
                        }
                }
                g_free(header);
                g_free(entry);
-       }       
+       }
+       if (blocked != NULL)    
+               matcherlist_free(blocked);
 
        return FALSE;   /* continue sending */
 }
index 3e322d0a8e99120bd0c43b6fd9acb3d02e4a49b6..3ecf7f591237e7278d4d7922b71b6baea1f8e519 100644 (file)
@@ -47,6 +47,7 @@ struct AddressKeeperPrefsPage
        GtkWidget *keep_to_addrs_check;
        GtkWidget *keep_cc_addrs_check;
        GtkWidget *keep_bcc_addrs_check;
+       GtkWidget *block_matching_addrs;
 };
 
 struct AddressKeeperPrefsPage addkeeperprefs_page;
@@ -60,6 +61,8 @@ static PrefParam param[] = {
          P_BOOL, NULL, NULL, NULL},
        {"keep_bcc_addrs", "FALSE", &addkeeperprefs.keep_bcc_addrs,
          P_BOOL, NULL, NULL, NULL},
+       {"block_matching_addrs", "", &addkeeperprefs.block_matching_addrs,
+        P_STRING, NULL, NULL, NULL},
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
 };
 
@@ -88,8 +91,12 @@ static void addkeeper_prefs_create_widget_func(PrefsPage * _page,
        GtkWidget *keep_to_checkbox;
        GtkWidget *keep_cc_checkbox;
        GtkWidget *keep_bcc_checkbox;
+       GtkWidget *blocked_frame;
+       GtkWidget *blocked_vbox;
+       GtkWidget *blocked_scrolledwin;
        GtkWidget *hbox;
        GtkWidget *vbox;
+       GtkTextBuffer *buffer;
 
        vbox = gtk_vbox_new(FALSE, 6);
        hbox = gtk_hbox_new(FALSE, 6);
@@ -149,6 +156,24 @@ static void addkeeper_prefs_create_widget_func(PrefsPage * _page,
        gtk_widget_show(keep_bcc_checkbox);
 
        page->keep_bcc_addrs_check = keep_bcc_checkbox;
+
+       blocked_vbox = gtkut_get_options_frame(vbox, &blocked_frame, _("Exclude addresses matching the following regular expressions (one per line):"));
+       gtk_container_set_border_width(GTK_CONTAINER(blocked_frame), 6);
+
+       page->block_matching_addrs = gtk_text_view_new();
+       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
+       gtk_text_buffer_set_text(buffer, addkeeperprefs.block_matching_addrs, -1);
+       
+       blocked_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
+       gtk_scrolled_window_set_policy
+               (GTK_SCROLLED_WINDOW (blocked_scrolledwin),
+                GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+       gtk_scrolled_window_set_shadow_type
+               (GTK_SCROLLED_WINDOW (blocked_scrolledwin), GTK_SHADOW_IN);
+
+       gtk_container_add(GTK_CONTAINER(blocked_scrolledwin), page->block_matching_addrs);
+       gtk_widget_set_size_request(page->block_matching_addrs, -1, 72);
+       gtk_box_pack_start(GTK_BOX(blocked_vbox), blocked_scrolledwin, FALSE, FALSE, 0);
        
        gtk_widget_show_all(vbox);
 
@@ -189,6 +214,9 @@ static void addkeeper_prefs_save_func(PrefsPage * _page)
 {
        struct AddressKeeperPrefsPage *page = (struct AddressKeeperPrefsPage *) _page;
        const gchar *text;
+       GtkTextBuffer *buffer;
+       GtkTextIter start, end;
+
        text = gtk_entry_get_text(GTK_ENTRY(page->addressbook_folder));
        addkeeperprefs.addressbook_folder = g_strdup(text);
        addkeeperprefs.keep_to_addrs = gtk_toggle_button_get_active(
@@ -197,13 +225,25 @@ static void addkeeper_prefs_save_func(PrefsPage * _page)
                GTK_TOGGLE_BUTTON(page->keep_cc_addrs_check));
        addkeeperprefs.keep_bcc_addrs = gtk_toggle_button_get_active(
                GTK_TOGGLE_BUTTON(page->keep_bcc_addrs_check));
+
+       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->block_matching_addrs));
+       gtk_text_buffer_get_start_iter(buffer, &start);
+       gtk_text_buffer_get_end_iter(buffer, &end);
+       text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+       g_free(addkeeperprefs.block_matching_addrs);
+       addkeeperprefs.block_matching_addrs = g_malloc(2 * strlen(text) + 1);
+       pref_get_escaped_pref(addkeeperprefs.block_matching_addrs, text);
+
        addkeeper_save_config();
+       g_free(addkeeperprefs.block_matching_addrs);
+       addkeeperprefs.block_matching_addrs = text;
 }
 
 void address_keeper_prefs_init(void)
 {
        static gchar *path[3];
        gchar *rcpath;
+       gchar *tmp;
        
        path[0] = _("Plugins");
        path[1] = _("Address Keeper");
@@ -214,6 +254,11 @@ void address_keeper_prefs_init(void)
        prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
        g_free(rcpath);
 
+       tmp = g_malloc(strlen(addkeeperprefs.block_matching_addrs) + 1);
+       pref_get_unescaped_pref(tmp, addkeeperprefs.block_matching_addrs);
+       g_free(addkeeperprefs.block_matching_addrs);
+       addkeeperprefs.block_matching_addrs = tmp;
+
        addkeeperprefs_page.page.path = path;
        addkeeperprefs_page.page.create_widget = addkeeper_prefs_create_widget_func;
        addkeeperprefs_page.page.destroy_widget = addkeeper_prefs_destroy_widget_func;
index 30541c03dafdfe9fd66779c0fb45b0f02e659ca3..9f1ea8c625fe391746dc8fc4f2834754ea2970bd 100644 (file)
@@ -27,10 +27,11 @@ typedef struct _AddressKeeperPrefs AddressKeeperPrefs;
 
 struct _AddressKeeperPrefs
 {
-       gchar            *addressbook_folder;
+       gchar           *addressbook_folder;
        gboolean        keep_to_addrs;
        gboolean        keep_cc_addrs;
        gboolean        keep_bcc_addrs;
+       gchar           *block_matching_addrs;
 };
 
 extern AddressKeeperPrefs addkeeperprefs;