Decouple getting version info from printing
[clawsker.git] / clawsker
index 8e5aade36bf5faf922c888d3d4068d11b48e2be8..27ea596e7135364d2a8a0cc33625546a06372b26 100755 (executable)
--- a/clawsker
+++ b/clawsker
@@ -213,6 +213,8 @@ sub _ {
     l_acc_gtls_pri => _('GnuTLS priority'),
     h_acc_gtls_pri => _('Value to use as GnuTLS priority string if custom priority check is enabled. Otherwise this value is ignored.'),
 
+    l_plu_gpg_alimit => _('Autocompletion limit'),
+    h_plu_gpg_alimit => _('Limits the number of addresses obtained from from keyring through autocompletion. Use 0 to get all matches.'),
     l_plu_lav_burl => _('Base URL'),
     h_plu_lav_burl => _('This is the URL where avatar requests are sent. You can use the one of your own libravatar server, if available.'),
     l_plu_prl_flvb => _('Log level'),
@@ -227,17 +229,27 @@ sub _ {
     e_running => _('seems Claws Mail is currently running, close it first.'),
 );
 
+# data and metadata of resource files
+my $CONFIGDATA;
+my $CONFIGMETA;
+my $ACCOUNTDATA;
+my $ACCOUNTMETA;
 # all preferences read by load_preferences
 my %PREFS = ();
 my %ACPREFS = ();
+my %PLPREFS = ();
 # values of all preferences handled by clawsker
 my %HPVALUE = ();
 my %ACHPVALUE = ();
+my %PLHPVALUE = ();
 # default config dir and file name
 my $ALTCONFIGDIR = FALSE;
 my $CONFIGDIR = $ENV{HOME} . '/.claws-mail/';
 my $CONFIGRC = 'clawsrc';
 my $ACCOUNTRC = 'accountrc';
+# supported and available plugins lists
+my @PLUGINS = qw(AttRemover GPG ManageSieve Libravatar PerlPlugin);
+my @AVPLUGINS = ();
 
 # index constants for preference arrays
 use constant NAME  => 0; # the name on the rc file
@@ -246,6 +258,7 @@ use constant DESC  => 2; # the description for the hint/help
 use constant TYPE  => 3; # data type: bool, int, float, string, color
 use constant CMVER => 4; # lowest[,highest] Claws Mail version(s) the feature exists
 use constant CMDEF => 5; # default value for the preference in Claws Mail
+use constant PLUGIN => 6; # plugin section (only in plugin preferences)
 
 # constants for GUI spacing
 use constant HBOX_SPC => 5;
@@ -1857,6 +1870,7 @@ sub new_accounts_page() {
         'int,0,3000', # 0 pixels - 3000 pixels
         '3.9.0.74',
         '-1',
+        'AttRemover',
     ],
     arm_winh => [
         'win_height',
@@ -1865,6 +1879,36 @@ sub new_accounts_page() {
         'int,0,3000', # 0 pixels - 3000 pixels
         '3.9.0.74',
         '-1',
+        'AttRemover',
+    ],
+    # GPG
+    gpg_alimit => [
+        'autocompletion_limit',
+        $xl::s{l_plu_gpg_alimit},
+        $xl::s{h_plu_gpg_alimit},
+        'int,0,100',
+        '3.12.0.75',
+        '0',
+        'GPG',
+    ],
+    # managesieve
+    msv_winw => [
+        'manager_win_width',
+        $xl::s{l_win_w},
+        $xl::s{h_win_w},
+        'int,0,3000', # 0 pixels - 3000 pixels
+        '3.11.1.210',
+        '-1',
+        'ManageSieve',
+    ],
+    msv_winh => [
+        'manager_win_height',
+        $xl::s{l_win_h},
+        $xl::s{h_win_h},
+        'int,0,3000', # 0 pixels - 3000 pixels
+        '3.11.1.210',
+        '-1',
+        'ManageSieve',
     ],
     # libravatar
     lav_burl => [
@@ -1874,6 +1918,7 @@ sub new_accounts_page() {
         'char,0,1024,32',
         '3.9.3.32',
         'http://cdn.libravatar.org/avatar',
+        'Libravatar',
     ],
     # perl
     prl_flvb => [
@@ -1883,45 +1928,80 @@ sub new_accounts_page() {
         '0=l_plu_prl_none;1=l_plu_prl_manual;2=l_plu_prl_action;3=l_plu_prl_match',
         '3.9.0.75',
         '2',
+        'PerlPlugin',
     ],
 );
 
 sub new_plugins_page() {
-    return new_vbox_pack (
+    my %frame = ();
+    $frame{'AttRemover'} =
                 new_subpage_frame (
                      new_hbox_pack (
-                          new_text_box_for_int (\%pr::plu, 'arm_winw', \%HPVALUE),
-                          new_text_box_for_int (\%pr::plu, 'arm_winh', \%HPVALUE)),
-                     _('Attachment remover'), 'not-packed'),
+                          new_text_box_for_int (\%pr::plu, 'arm_winw', $PLHPVALUE{'AttRemover'}),
+                          new_text_box_for_int (\%pr::plu, 'arm_winh', $PLHPVALUE{'AttRemover'})),
+                     _('Attachment remover'), 'not-packed');
+    $frame{'GPG'} =
                 new_subpage_frame (
                      new_hbox_pack (
-                          new_text_box_for_nchar (\%pr::plu, 'lav_burl', \%HPVALUE)),
-                     _('Libravatar'), 'not-packed'),
+                          new_text_box_for_int (\%pr::plu, 'gpg_alimit', $PLHPVALUE{'GPG'})),
+                     _('GPG'), 'not-packed');
+    $frame{'ManageSieve'} =
                 new_subpage_frame (
                      new_hbox_pack (
-                          new_selection_box_for (\%pr::plu, 'prl_flvb', \%HPVALUE)),
-                     _('Perl'), 'not-packed')
-           );
+                          new_text_box_for_int (\%pr::plu, 'msv_winw', $PLHPVALUE{'ManageSieve'}),
+                          new_text_box_for_int (\%pr::plu, 'msv_winh', $PLHPVALUE{'ManageSieve'})),
+                     _('Sieve manager'), 'not-packed');
+    $frame{'Libravatar'} =
+                new_subpage_frame (
+                     new_hbox_pack (
+                          new_text_box_for_nchar (\%pr::plu, 'lav_burl', $PLHPVALUE{'Libravatar'})),
+                     _('Libravatar'), 'not-packed');
+    $frame{'PerlPlugin'} =
+                new_subpage_frame (
+                     new_hbox_pack (
+                          new_selection_box_for (\%pr::plu, 'prl_flvb', $PLHPVALUE{'PerlPlugin'})),
+                     _('Perl'), 'not-packed');
+    foreach (@PLUGINS) {
+        $frame{$_}->set_sensitive (defined $PLHPVALUE{$_});
+    }
+    return new_vbox_pack (
+                $frame{'AttRemover'},
+                $frame{'GPG'},
+                $frame{'ManageSieve'},
+                $frame{'Libravatar'},
+                $frame{'PerlPlugin'});
 }
 
 # version info
-sub print_version() {
-    print $xl::s{about_title} . "\n";
-    print $xl::s{about_version} . " $VERSION\n";
-    print "Perl-GLib " . $Glib::VERSION;
+sub get_toolkit_versions {
+    my %versions = ();
+    $versions{'glib'} = $Glib::VERSION;
     # version info stuff appeared in 1.040
     if ($Glib::VERSION >= 1.040) {
-        print _(", built for ") . join(".", Glib->GET_VERSION_INFO)
-              . _(", running with ") . join(".", &Glib::major_version,
-              &Glib::minor_version, &Glib::micro_version);
+        $versions{'glib-b'} = join('.', Glib->GET_VERSION_INFO);
+        $versions{'glib-r'} = join('.',
+            &Glib::major_version, &Glib::minor_version, &Glib::micro_version);
     }
-    print "\n";
-    print "Perl-GTK2 " . $Gtk2::VERSION;
+    $versions{'gtk2'} = $Gtk2::VERSION;
     if ($Gtk2::VERSION >= 1.040) {
-        print _(", built for ") . join(".", Gtk2->GET_VERSION_INFO)
-              . _(", running with ") . join(".", &Gtk2::major_version,
-              &Gtk2::minor_version, &Gtk2::micro_version);
+        $versions{'gtk2-b'} = join('.', Gtk2->GET_VERSION_INFO);
+        $versions{'gtk2-r'} = join('.',
+            &Gtk2::major_version, &Gtk2::minor_version, &Gtk2::micro_version);
     }
+    return \%versions;
+}
+
+sub print_version() {
+    print $xl::s{about_title} . "\n";
+    print $xl::s{about_version} . " $VERSION\n";
+    my $v = get_toolkit_versions ();
+    print "Perl-GLib " . $v->{'glib'};
+    print _(", built for ") . $v->{'glib-b'}
+        . _(", running with ") . $v->{'glib-r'} if $v->{'glib-b'};
+    print "\n";
+    print "Perl-GTK2 " . $v->{'gtk2'};
+    print _(", built for ") . $v->{'gtk2-b'}
+        . _(", running with ") . $v->{'gtk2-r'} if $v->{'gtk2-b'};
     print "\n";
     my $clawsver = ($CLAWSV eq "") ?
                 _("was not found!") :
@@ -1938,11 +2018,12 @@ sub print_help() {
     print _("Syntax:\n");
     print _("  clawsker [options]\n");
     print _("Options:\n");
-    print _("  -h|--help                        Prints this help screen.\n");
-    print _("  -v|--version                     Prints version infos.\n");
-    print _("  -b|--verbose                     More messages on standard output.\n");
     print _("  -a|--alternate-config-dir <dir>  Uses <dir> as Claws Mail config dir.\n");
+    print _("  -b|--verbose                     More messages on standard output.\n");
     print _("  -c|--clawsrc <file>              Uses <file> as full resource name.\n");
+    print _("  -h|--help                        Prints this help screen.\n");
+    print _("  -r|--read-only                   Disables writing changes to disk.\n");
+    print _("  -v|--version                     Prints version infos.\n");
 }
 
 sub parse_command_line {
@@ -2012,6 +2093,17 @@ sub init_ac_hidden_preferences {
     return TRUE;
 }
 
+sub init_plu_hidden_preferences {
+    foreach my $key (keys %pr::plu) {
+        my $plugin = $pr::plu{$key}[PLUGIN];
+        my $pname = $pr::plu{$key}[NAME];
+        if (defined $PLPREFS{$plugin}) {
+            $PLHPVALUE{$plugin}{$pname} = $PLPREFS{$plugin}{$pname};
+        }
+    }
+    return TRUE;
+}
+
 # generic load/save resource files
 sub load_resource {
     my $rc = shift;
@@ -2083,14 +2175,18 @@ sub load_preferences {
     log_message ("Loading preferences from $rc\n");
     return FALSE unless check_rc_file ($rc);
     return FALSE unless check_claws_not_running ();
-    open (RCF, "<$rc");
-    while (<RCF>) {
-        chomp;
-        if (/^([8a-z_]+)=(.*)$/) {
-            $PREFS{$1} = decode('UTF-8', $2);
+    ($CONFIGDATA, $CONFIGMETA) = load_resource ($rc);
+    foreach (keys %{$CONFIGDATA->{'Common'}}) {
+        $PREFS{$_} = $CONFIGDATA->{'Common'}{$_};
+    }
+    foreach my $plugin (@PLUGINS) {
+        if (defined $CONFIGDATA->{$plugin}) {
+            push (@AVPLUGINS, $plugin);
+            foreach (keys %{$CONFIGDATA->{$plugin}}) {
+                $PLPREFS{$plugin}{$_} = $CONFIGDATA->{$plugin}{$_};
+            }
         }
     }
-    close (RCF);
     return TRUE;
 }
 
@@ -2099,19 +2195,14 @@ sub load_ac_preferences {
     log_message ("Loading account preferences from $rc\n");
     return FALSE unless check_rc_file ($rc);
     return FALSE unless check_claws_not_running ();
-    open (RCF, "<$rc");
-    my $akey;
-    while (<RCF>) {
-        chomp;
-        if (/^\[Account: (\d+)\]$/) {
-          $akey = $1;
-          next;
-        }
-        if (/^([8a-z_]+)=(.*)$/) {
-            $ACPREFS{$akey}{$1} = decode('UTF-8', $2);
+    ($ACCOUNTDATA, $ACCOUNTMETA) = load_resource ($rc);
+    foreach my $asect (keys %$ACCOUNTDATA) {
+        if ($asect =~ /^Account: (\d+)$/) {
+            foreach (keys %{$ACCOUNTDATA->{$asect}}) {
+                $ACPREFS{$1}{$_} = $ACCOUNTDATA->{$asect}{$_};
+            }
         }
     }
-    close (RCF);
     return TRUE;
 }
 
@@ -2123,24 +2214,19 @@ sub save_preferences {
     return FALSE unless check_claws_not_running ();
     my $rcbak = "$rc.backup";
     rename ($rc, $rcbak);
-    open (RCF, ">$rc");
-    open (RCB, "<$rcbak");
-    while (<RCB>) {
-        chomp;
-        if (/^([8a-z_]+)=(.*)$/) {
-            if (defined($HPVALUE{$1})) {
-                print RCF $1 . "=" . $HPVALUE{$1} . "\n";
-            }
-            else {
-                print RCF $_ . "\n";
-            }
+    foreach (keys %PREFS) {
+        if (defined $HPVALUE{$_}) {
+            $CONFIGDATA->{'Common'}{$_} = $HPVALUE{$_};
         }
-        else {
-            print RCF $_ . "\n";
+    }
+    foreach my $plugin (@AVPLUGINS) {
+        foreach (keys %{$CONFIGDATA->{$plugin}}) {
+            if (defined $PLHPVALUE{$plugin}{$_}) {
+                $CONFIGDATA->{$plugin}{$_} = $PLHPVALUE{$plugin}{$_};
+            }
         }
     }
-    close (RCB);
-    close (RCF);
+    save_resource ($rc, $CONFIGDATA, $CONFIGMETA);
     return TRUE;
 }
 
@@ -2151,30 +2237,16 @@ sub save_ac_preferences {
     return FALSE unless check_claws_not_running ();
     my $rcbak = "$rc.backup";
     rename ($rc, $rcbak);
-    open (RCF, ">$rc");
-    open (RCB, "<$rcbak");
-    my $akey;
-    while (<RCB>) {
-        chomp;
-        if (/^\[Account: (\d+)\]$/) {
-          $akey = $1;
-          print RCF $_ . "\n";
-          next;
-        }
-        if (/^([8a-z_]+)=(.*)$/) {
-            if (defined($ACHPVALUE{$akey}{$1})) {
-                print RCF $1 . "=" . $ACHPVALUE{$akey}{$1} . "\n";
+    foreach my $asect (keys %$ACCOUNTDATA) {
+        if ($asect =~ /^Account: (\d+)$/) {
+            foreach (keys %{$ACCOUNTDATA->{$asect}}) {
+                if (defined $ACHPVALUE{$1}{$_}) {
+                    $ACCOUNTDATA->{$asect}{$_} = $ACHPVALUE{$1}{$_};
+                }
             }
-            else {
-                print RCF $_ . "\n";
-            }
-        }
-        else {
-            print RCF $_ . "\n";
         }
     }
-    close (RCB);
-    close (RCF);
+    save_resource ($rc, $ACCOUNTDATA, $ACCOUNTMETA);
     return TRUE;
 }
 
@@ -2279,6 +2351,7 @@ exit unless load_preferences ();
 exit unless load_ac_preferences ();
 exit unless init_hidden_preferences ();
 exit unless init_ac_hidden_preferences ();
+exit unless init_plu_hidden_preferences ();
 # create main GUI
 my $box = Gtk2::VBox->new (FALSE, 5);
 $box->set_border_width(3);