Allow Sieve config without userid without warning
[claws.git] / src / plugins / managesieve / sieve_prefs.c
index c0051220c77953cbaaad78584ec39e074d348d4a..2e2d0e9b8b475e01cbe22406e421fba59a4155c1 100644 (file)
@@ -49,7 +49,7 @@ static PrefParam prefs[] = {
                P_INT, NULL, NULL, NULL},
         {"manager_win_height", "-1", &sieve_config.manager_win_height,
                P_INT, NULL, NULL, NULL},
-        {0,0,0,0}
+        {0,0,0,0,0,0,0}
 };
 
 #define PACK_HBOX(hbox, vbox) \
@@ -154,8 +154,6 @@ static void sieve_prefs_account_create_widget_func(PrefsPage *_page,
 
        /* Server info */
        serv_vbox = gtkut_get_options_frame(sieve_vbox, &serv_frame, _("Server information"));
-       gtk_widget_show (serv_vbox);
-       gtk_box_pack_start (GTK_BOX (page_vbox), serv_vbox, FALSE, FALSE, 0);
 
        SET_TOGGLE_SENSITIVITY (enable_checkbtn, sieve_vbox);
        size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
@@ -187,8 +185,6 @@ static void sieve_prefs_account_create_widget_func(PrefsPage *_page,
        /* Encryption */
 
        tls_vbox = gtkut_get_options_frame(sieve_vbox, &tls_frame, _("Encryption"));
-       gtk_widget_show (tls_vbox);
-       gtk_box_pack_start (GTK_BOX (page_vbox), tls_vbox, FALSE, FALSE, 0);
 
        RADIO_ADD(tls_radio_no, tls_group, hbox, tls_vbox,
                        _("No encryption"));
@@ -480,8 +476,9 @@ struct SieveAccountConfig *sieve_prefs_account_get_config(
        gchar enable, use_host, use_port;
        guchar tls_type, auth, auth_type;
        gsize len;
+       gint num;
 #if defined(G_OS_WIN32) || defined(__OpenBSD__) || defined(__FreeBSD__)
-       /* Windows sscanf() does not understand the %ms format yet, so we
+       /* Non-GNU sscanf() does not understand the %ms format, so we
         * have to do the allocation of target buffer ourselves before
         * calling sscanf(), and copy the host string to config->host.
         */
@@ -507,9 +504,9 @@ struct SieveAccountConfig *sieve_prefs_account_get_config(
        enc_userid[0] = '\0';
        enc_passwd[0] = '\0';
 #if defined(G_OS_WIN32) || defined(__OpenBSD__) || defined(__FreeBSD__)
-       sscanf(confstr, "%c%c %255s %c%hu %hhu %hhu %hhu %255s %255s",
+       if ((num = sscanf(confstr, "%c%c %255s %c%hu %hhu %hhu %hhu %255s %255s",
 #else
-       sscanf(confstr, "%c%c %ms %c%hu %hhu %hhu %hhu %255s %255s",
+       if ((num = sscanf(confstr, "%c%c %ms %c%hu %hhu %hhu %hhu %255s %255s",
 #endif
                        &enable, &use_host,
 #if defined(G_OS_WIN32) || defined(__OpenBSD__) || defined(__FreeBSD__)
@@ -522,7 +519,20 @@ struct SieveAccountConfig *sieve_prefs_account_get_config(
                        &auth,
                        &auth_type,
                        enc_userid,
-                       enc_passwd);
+                       enc_passwd)) != 10) {
+                       /* This (10th element missing) will happen on any recent
+                        * configuration, where the password is already in
+                        * passwordstore, and not in this config string. We have
+                        * to read the 10th element in order not to break older
+                        * configurations, and to move the password to password
+                        * store.
+                        * The userid may be missing if it is unset.
+                        * If there are not 10, 9 or 8 elements, something is wrong. */
+               if (num != 9 && num != 8) {
+                       g_warning("failed reading Sieve config elements");
+               }
+       }
+       debug_print("Read %d Sieve config elements\n", num);
 
        /* Scan enums separately, for endian purposes */
        config->tls_type = tls_type;
@@ -543,8 +553,11 @@ struct SieveAccountConfig *sieve_prefs_account_get_config(
        }
 
        config->userid = g_base64_decode(enc_userid, &len);
-       if (enc_passwd[0]) {
-               // migrate password from passcrypt to passwordstore
+
+       /* migrate password from passcrypt to passwordstore, if
+        * it's not there yet */
+       if (enc_passwd[0] != '\0' &&
+                       !passwd_store_has_password_account(account->account_id, "sieve")) {
                gchar *pass = g_base64_decode(enc_passwd, &len);
                passcrypt_decrypt(pass, len);
                passwd_store_set_account(account->account_id, "sieve",
@@ -567,7 +580,7 @@ void sieve_prefs_account_set_config(
                enc_userid = g_base64_encode(config->userid, len);
        }
 
-       confstr = g_strdup_printf("%c%c %s %c%hu %hhu %hhu %hhu %s %s",
+       confstr = g_strdup_printf("%c%c %s %c%hu %hu %hu %hu %s",
                        config->enable ? 'y' : 'n',
                        config->use_host ? 'y' : 'n',
                        config->host && config->host[0] ? config->host : "!",
@@ -576,8 +589,7 @@ void sieve_prefs_account_set_config(
                        config->tls_type,
                        config->auth,
                        config->auth_type,
-                       enc_userid ? enc_userid : "",
-                       "");
+                       enc_userid ? enc_userid : "");
 
        if (enc_userid)
                g_free(enc_userid);