0.8.10claws100
[claws.git] / src / plugins / spamassassin / spamassassin.c
index a782e4c3a910b404e11c88e47db0c8cafe9fbf59..99ed7cff21ee230d0bf8c35f0d410228dc9420ca 100644 (file)
 
 static gint hook_id;
 static int flags = SPAMC_RAW_MODE | SPAMC_SAFE_FALLBACK | SPAMC_CHECK_ONLY;
+static gchar *username = NULL;
 
-gboolean spamassassin_enable;
-gchar *spamassassin_hostname;
-int spamassassin_port;
-int spamassassin_max_size;
-gboolean spamassassin_receive_spam;
-gchar *spamassassin_save_folder;
+static SpamAssassinConfig config;
 
 static PrefParam param[] = {
-       {"enable", "FALSE", &spamassassin_enable, P_BOOL,
+       {"enable", "FALSE", &config.enable, P_BOOL,
         NULL, NULL, NULL},
-       {"hostname", "localhost", &spamassassin_hostname, P_STRING,
+       {"hostname", "localhost", &config.hostname, P_STRING,
         NULL, NULL, NULL},
-       {"port", "783", &spamassassin_port, P_USHORT,
+       {"port", "783", &config.port, P_USHORT,
         NULL, NULL, NULL},
-       {"max_size", "250", &spamassassin_max_size, P_USHORT,
+       {"max_size", "250", &config.max_size, P_USHORT,
         NULL, NULL, NULL},
-       {"receive_spam", "TRUE", &spamassassin_receive_spam, P_BOOL,
+       {"receive_spam", "TRUE", &config.receive_spam, P_BOOL,
         NULL, NULL, NULL},
-       {"save_folder", NULL, &spamassassin_save_folder, P_STRING,
+       {"save_folder", NULL, &config.save_folder, P_STRING,
         NULL, NULL, NULL},
 
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
@@ -97,72 +93,54 @@ static gboolean mail_filtering_hook(gpointer source, gpointer data)
        gboolean is_spam = FALSE;
        FILE *fp = NULL;
        struct message m;
-       int ret;
-       gchar *username = NULL, *oldlocale = NULL;
+       gchar *oldlocale = NULL;
        struct sockaddr addr;
 
-       if (!spamassassin_enable)
+       if (!config.enable)
                return FALSE;
 
        debug_print("Filtering message %d\n", msginfo->msgnum);
 
-       oldlocale = g_strdup(setlocale(LC_ALL, NULL));
-       if (oldlocale == NULL) {
-               debug_print("failed to get locale");
-               goto CATCH;
-       }
-
+       /* remember old locale and set it to C */
+       Xstrdup_a(oldlocale, setlocale(LC_ALL, NULL), return FALSE);
        setlocale(LC_ALL, "C");
 
-       ret = lookup_host(spamassassin_hostname, spamassassin_port, &addr);
-       if (ret != EX_OK) {
+       if (lookup_host(config.hostname, config.port, &addr) != EX_OK) {
                debug_print("failed to look up spamd host");
-               goto CATCH;
+               return FALSE;
        }
 
        m.type = MESSAGE_NONE;
-       m.max_len = spamassassin_max_size * 1024;
-
-       username = g_get_user_name();
-       if (username == NULL) {
-               debug_print("failed to get username");
-               goto CATCH;
-       }
+       m.max_len = config.max_size * 1024;
 
-       fp = procmsg_open_message(msginfo);
-       if (fp == NULL) {
+       if ((fp = procmsg_open_message(msginfo)) == NULL) {
                debug_print("failed to open message file");
-               goto CATCH;
+               return FALSE;
        }
 
-       ret = message_read(fileno(fp), flags, &m);
-       if (ret != EX_OK) {
+       if (message_read(fileno(fp), flags, &m) != EX_OK) {
                debug_print("failed to read message");
-               goto CATCH;
+               fclose(fp);
+               message_cleanup(&m);
+               return FALSE;
        }
 
-       ret = message_filter(&addr, username, flags, &m);
-       if ((ret == EX_OK) && (m.is_spam == EX_ISSPAM))
+       if ((message_filter(&addr, username, flags, &m) == EX_OK) && (m.is_spam == EX_ISSPAM))
                is_spam = TRUE;
 
-CATCH:
-       if (fp != NULL)
-               fclose(fp);
        message_cleanup(&m);
-       if (oldlocale != NULL) {
-               setlocale(LC_ALL, oldlocale);
-               g_free(oldlocale);
-       }
+       fclose(fp);
+       setlocale(LC_ALL, oldlocale);
 
        if (is_spam) {
-               if (spamassassin_receive_spam) {
+               if (config.receive_spam) {
                        FolderItem *save_folder;
 
                        debug_print("message is spam\n");
                            
-                       if ((!spamassassin_save_folder) ||
-                           (spamassassin_save_folder[0] == '\0') ||
-                           ((save_folder = folder_find_item_from_identifier(spamassassin_save_folder)) == NULL))
+                       if ((!config.save_folder) ||
+                           (config.save_folder[0] == '\0') ||
+                           ((save_folder = folder_find_item_from_identifier(config.save_folder)) == NULL))
                                save_folder = folder_get_default_trash();
 
                        procmsg_msginfo_unset_flags(msginfo, ~0, 0);
@@ -177,6 +155,11 @@ CATCH:
        return FALSE;
 }
 
+SpamAssassinConfig *spamassassin_get_config()
+{
+       return &config;
+}
+
 void spamassassin_save_config()
 {
        PrefFile *pfile;
@@ -208,6 +191,12 @@ gint plugin_init(gchar **error)
                return -1;
        }
 
+       username = g_get_user_name();
+       if (username == NULL) {
+               *error = g_strdup("Failed to get username");
+               return -1;
+       }
+
        prefs_set_default(param);
        prefs_read_config(param, "SpamAssassin", COMMON_RC);
 
@@ -220,7 +209,8 @@ gint plugin_init(gchar **error)
 void plugin_done()
 {
        hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
-       g_free(spamassassin_hostname);
+       g_free(config.hostname);
+       g_free(config.save_folder);
 
        debug_print("Spamassassin plugin unloaded\n");
 }