Add option to read from Maildir.
authorThomas Hochstein <thh@inter.net>
Sat, 15 Feb 2014 16:41:54 +0000 (17:41 +0100)
committerThomas Hochstein <thh@inter.net>
Sat, 15 Feb 2014 16:41:54 +0000 (17:41 +0100)
Parse a mailbox by virtue of Mail::Box and
add "mailboxtype" to usevote.cfg.

Picked from upstream.

Signed-off-by: Thomas Hochstein <thh@inter.net>
UVreadmail.pm
usevote.cfg
uvvote.pl

index e1e599c..01d062f 100644 (file)
@@ -9,6 +9,7 @@ use UVmessage;
 use MIME::QuotedPrint;
 use MIME::Base64;
 use MIME::Parser;
 use MIME::QuotedPrint;
 use MIME::Base64;
 use MIME::Parser;
+use Mail::Box::Manager;
 use POSIX qw(strftime);
 
 use vars qw($VERSION);
 use POSIX qw(strftime);
 
 use vars qw($VERSION);
@@ -118,37 +119,52 @@ sub process {
       close (UIDLCACHE) or print STDERR UVmessage::get("READMAIL_UIDL_CLOSE") . "\n";
     }
 
       close (UIDLCACHE) or print STDERR UVmessage::get("READMAIL_UIDL_CLOSE") . "\n";
     }
 
-    # make archive of all mails
-    my $fileproblem = 0;
-    open (VOTES, ">$filename") or $fileproblem = 1;
-    if ($fileproblem) {
-      print STDERR UVmessage::get("READMAIL_ARCHIVE_PROBLEM",
-                   (FILE => $filename)) . "\n";
-    } else {
-      print VOTES join ("\n", @mails);
-      close (VOTES)
-        or print STDERR UVmessage::get("READMAIL_ARCHIVE_CLOSE",
-                        (FILE => $filename)) . "\n";
-    }
-
     $pop->quit();
 
     $pop->quit();
 
+  # Mailbox / Maildir
   } else {
   } else {
-    # open mail file
-    open(VOTES, "<$filename")
-        or die UVmessage::get("READMAIL_NOMAILFILE", (FILE => $filename)) . "\n\n";
-
-    # read all mails
-    my $i = 0;
-    while (<VOTES>) {
-      if (/$config{mailstart}/) {
-        $i++;
+
+    my $mgr = Mail::Box::Manager->new;
+    my $folder;
+
+    eval{
+      $folder = $mgr->open( folder => $config{votefile},
+                create => 0,
+                access => 'rw',
+                type   => $config{mailboxtype},
+                expand => 'LAZY',
+              );
+    };
+    die UVmessage::get("READMAIL_NOMAILFILE", (FILE => $config{votefile})) . "\n\n" if $@;
+
+    # Iterate over the messages.
+    foreach (@$folder) {
+      my $mail = $_->string;
+      $_->delete();
+      my $fromline = 'From ';
+      if ($mail =~ /From: .*?<(.+?)>/) {
+        $fromline .= $1;
+      } elsif ($mail =~ /From:\s+?(\S+?\@\S+?)\s/) {
+        $fromline .= $1;
+      } else {
+        $fromline .= 'foo@bar.invalid';
       }
       }
-      $mails[$i] = ($mails[$i] || "") . $_;
+      $fromline .= ' ' . localtime() . "\n";  #strftime ('%a %b %d %H:%M:%S %Y', localtime) . "\n";
+      push (@mails, $fromline . $mail);
     }
     }
+  }
 
 
-    # close mail file
-    close(VOTES);
+  # make archive of all mails
+  my $fileproblem = 0;
+  open (VOTES, ">$filename") or $fileproblem = 1;
+  if ($fileproblem) {
+    print STDERR UVmessage::get("READMAIL_ARCHIVE_PROBLEM",
+                 (FILE => $filename)) . "\n";
+  } else {
+    print VOTES join ("\n", @mails);
+    close (VOTES)
+      or print STDERR UVmessage::get("READMAIL_ARCHIVE_CLOSE",
+                      (FILE => $filename)) . "\n";
   }
 
   foreach my $mail (@mails) {
   }
 
   foreach my $mail (@mails) {
index aee1fc1..c472b45 100644 (file)
@@ -90,6 +90,8 @@ pop3 = 1
 # Mailbox, in der die zu verarbeitenden Mails liegen (falls pop3=0)
 votefile = votes
 
 # Mailbox, in der die zu verarbeitenden Mails liegen (falls pop3=0)
 votefile = votes
 
+# Typ der Mailbox (mbox, maildir)
+mailboxtype = mbox
 # POP3-Einstellungen fuer Abruf der eingehenden Wahlscheine:
 # Server, Port, Benutzername, Passwort
 pop3server = 127.0.0.1
 # POP3-Einstellungen fuer Abruf der eingehenden Wahlscheine:
 # Server, Port, Benutzername, Passwort
 pop3server = 127.0.0.1
index 99988c0..5cff375 100755 (executable)
--- a/uvvote.pl
+++ b/uvvote.pl
@@ -135,16 +135,6 @@ if ($clean) {
   my $thisresult = "ergebnis-" . $ext;
   my $thisvotes = "stimmen-" . $ext;
   
   my $thisresult = "ergebnis-" . $ext;
   my $thisvotes = "stimmen-" . $ext;
   
-  # POP3 not activated: rename votes file
-  unless ($config{pop3}) {
-    print UVmessage::get("VOTE_RENAMING_MAILBOX"), "\n";
-    rename ($config{votefile}, "$config{tmpdir}/$thisvotes")
-       or die UVmessage::get("ERR_RENAME_MAILFILE") . "$!\n\n";
-  
-    #  wait, so that current mail deliveries can finalize
-    sleep 2;
-  }
-
   # open results file
   open (RESULT, ">>$config{tmpdir}/$thisresult")
      or die UVmessage::get("VOTE_WRITE_RESULTS", (FILE=>$thisresult)) . "\n\n";
   # open results file
   open (RESULT, ">>$config{tmpdir}/$thisresult")
      or die UVmessage::get("VOTE_WRITE_RESULTS", (FILE=>$thisresult)) . "\n\n";
This page took 0.014907 seconds and 4 git commands to generate.