]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/Fetchmail.pm
fix #1881: increase limit for ids in spam quarantine
[pmg-api.git] / PMG / Fetchmail.pm
index 67a83798b0889d09b7be4ec5601d0156c5ad2aa9..7e16c7967767a3a7b45198e0f5226bded569d82e 100644 (file)
@@ -7,10 +7,69 @@ use Data::Dumper;
 use PVE::Tools;
 use PVE::INotify;
 
+use PMG::Utils;
 use PMG::Config;
+use PMG::ClusterConfig;
 
 my $inotify_file_id = 'fetchmailrc';
 my $config_filename = '/etc/pmg/fetchmailrc';
+my $config_link_filename = '/etc/fetchmailrc';
+
+my $fetchmail_default_id = 'fetchmail_default';
+my $fetchmail_default_filename = '/etc/default/fetchmail';
+
+sub read_fetchmail_default {
+    my ($filename, $fh) = @_;
+
+    if (defined($fh)) {
+       while (defined(my $line = <$fh>)) {
+           if ($line =~ m/^START_DAEMON=yes\s*$/) {
+               return 1;
+           }
+       }
+    }
+
+    return 0;
+}
+
+sub write_fetchmail_default {
+    my ($filename, $fh, $enable) = @_;
+
+    open (my $orgfh, "<", $filename);
+
+    my $wrote_start_daemon = 0;
+
+    my $write_start_daemon_line = sub {
+
+       return if $wrote_start_daemon;  # only once
+       $wrote_start_daemon = 1;
+
+       if ($enable) {
+           print $fh "START_DAEMON=yes\n";
+       } else {
+           print $fh "START_DAEMON=no\n";
+       }
+    };
+
+    if (defined($orgfh)) {
+       while (defined(my $line = <$orgfh>)) {
+           if ($line =~ m/^#?START_DAEMON=.*$/) {
+               $write_start_daemon_line->();
+           } else {
+               print $fh $line;
+           }
+       }
+    } else {
+       $write_start_daemon_line->();
+    }
+}
+
+PVE::INotify::register_file(
+    $fetchmail_default_id, $fetchmail_default_filename,
+    \&read_fetchmail_default,
+    \&write_fetchmail_default,
+    undef,
+    always_call_parser => 1);
 
 my $set_fetchmail_defaults = sub {
     my ($item) = @_;
@@ -59,9 +118,12 @@ sub read_fetchmail_conf {
        my $get_next_token = sub {
 
            do {
-               while ($data =~ /\G('([^']*)'|\S+|)(?:\s|$)/g) {
+               while ($data =~ /\G("([^"]*)"|\S+|)(?:\s|$)/g) {
+                   my ($token, $string) = ($1, $2);
                    if ($1 ne '') {
-                       return wantarray ? ($1, $2) : $1;
+                       $string =~ s/\\x([0-9A-Fa-f]{2})/chr(hex($1))/eg
+                           if defined($string);
+                       return wantarray ? ($token, $string) : $token;
                    }
                }
                $data = <$fh>;
@@ -89,7 +151,7 @@ sub read_fetchmail_conf {
                $finalize_item->($item) if defined($item);
                my $id = $get_token_argument->();
                $item = { id => $id };
-               $item->{enable} = $token eq 'pass' ? 1 : 0;
+               $item->{enable} = $token eq 'poll' ? 1 : 0;
                next;
            }
 
@@ -126,22 +188,34 @@ sub read_fetchmail_conf {
 sub write_fetchmail_conf {
     my ($filename, $fh, $fmcfg) = @_;
 
+    my $data = {};
+
+    # Note: we correctly quote data here to make fetchmailrc.tt simpler
+
+    my $entry_count = 0;
+
     foreach my $id (keys %$fmcfg) {
-       my $item = $fmcfg->{$id};
-       $item->{id} = $id;
+       my $org = $fmcfg->{$id};
+       my $item = { id => $id };
+       $entry_count++;
+       foreach my $k (keys %$org) {
+           my $v = $org->{$k};
+           $v =~ s/([^A-Za-z0-9\:\@\-\._~])/sprintf "\\x%02x",ord($1)/eg;
+           $item->{$k} = $v;
+       }
        $set_fetchmail_defaults->($item);
        my $options = [ 'dropdelivered' ];
        push @$options, 'ssl' if $item->{ssl};
        push @$options, 'keep' if $item->{keep};
        $item->{options} = join(' ', @$options);
+       $data->{$id} = $item;
     }
 
     my $raw = '';
 
-
     my $pmgcfg = PMG::Config->new();
     my $vars = $pmgcfg->get_template_vars();
-    $vars->{fetchmail_users} = $fmcfg;
+    $vars->{fetchmail_users} = $data;
 
     my $tt = PMG::Config::get_template_toolkit();
     $tt->process('fetchmailrc.tt', $vars, \$raw) ||
@@ -152,6 +226,33 @@ sub write_fetchmail_conf {
     chmod(0600, $fh);
 
     PVE::Tools::safe_print($filename, $fh, $raw);
+
+    update_fetchmail_default($entry_count);
+}
+
+sub update_fetchmail_default {
+    my ($enable) = @_;
+
+    my $cinfo = PMG::ClusterConfig->new();
+
+    my $is_enabled = PVE::INotify::read_file('fetchmail_default');
+    my $role = $cinfo->{local}->{type} // '-';
+    if (($role eq '-') || ($role eq 'master')) {
+       if (!!$enable != !!$is_enabled) {
+           PVE::INotify::write_file('fetchmail_default', $enable);
+           PMG::Utils::service_cmd('fetchmail', 'restart');
+       }
+       if (! -e $config_link_filename) {
+           symlink ($config_filename, $config_link_filename);
+       }
+    } else {
+       if ($is_enabled) {
+           PVE::INotify::write_file('fetchmail_default', 0);
+       }
+       if (-e $config_link_filename) {
+           unlink $config_link_filename;
+       }
+    }
 }
 
 PVE::INotify::register_file(