diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2018-04-18 23:48:47 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2018-04-19 00:08:04 +0200 |
commit | 5d5499739cbab53c423722692be1bda9ab93e7fb (patch) | |
tree | 2cdd5a0d2b3168aaa20b7fb627a4cc4af29b3408 | |
parent | 55622ec37a56ccf0a905ee2a1ec0cd4d1dbaea67 (diff) | |
download | imapArch-5d5499739cbab53c423722692be1bda9ab93e7fb.tar.gz |
Remove corrupted files
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | archiveIMAP.pl | 144 | ||||
-rw-r--r-- | expireIMAP.pl | 35 | ||||
-rwxr-xr-x | listFolders.py | 14 | ||||
-rwxr-xr-x | playWithImap.py | 67 | ||||
-rw-r--r-- | previousAttempts/jython/pydoctest.py | 63 | ||||
-rw-r--r-- | previousAttempts/perl/archiveIMAP.pl | bin | 4986 -> 0 bytes | |||
-rw-r--r-- | previousAttempts/perl/expireIMAP.pl | bin | 862 -> 0 bytes | |||
-rw-r--r-- | previousAttempts/perl/listFolders.pl | 23 | ||||
-rw-r--r-- | previousAttempts/perl/playWithImap.pl | bin | 2203 -> 0 bytes |
10 files changed, 260 insertions, 87 deletions
diff --git a/.gitignore b/.gitignore deleted file mode 100644 index c3d94fa..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.ÆððÙ~{»þ$¿vØY[x&¬˜ò²-Ïu¶ëOLšš)pøÙ™JJ‡
\ No newline at end of file diff --git a/archiveIMAP.pl b/archiveIMAP.pl new file mode 100644 index 0000000..a1833de --- /dev/null +++ b/archiveIMAP.pl @@ -0,0 +1,144 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Mail::IMAPClient; +use IO::Socket::SSL; +use Data::Dumper; +use DateTime; +use DateTime::Format::Strptime; +use Config::IniFiles; + +# possible values are currently -- zimbra, localhost, pobox +my $account = "localhost"; + +# How many months before today the cut date should be? +my $howManyMonths = 3; +my $debug = 0; + +# get configuration for the account +my $conf = Config::IniFiles->new( -file => "/home/matej/.bugzillarc"); +die "No configuration for account $account" unless $conf->SectionExists($account); +my $hostname = $conf->val($account,'host'); +my $login = $conf->val($account,'name'); +my $password = $conf->val($account,'password'); +my $ssl= $conf->val($account,'ssl'); + +sub getTargetFolder { + my $source = shift; + my $year = shift; + + $source =~ s/^\/*(.*)\/*$/$1/; + return "/INBOX/Archiv/" . $year . '/' . $source; +} + +# makes sure that the folder including its parents +# RFC2060 says in 6.3.3 that server SHOULD create +# parents, so just to be sure if it doesn't. +sub assureFolder { + my $imaphandle = shift; + my $fullfoldername = shift; + my $sep = $imap->separator($fullfoldername); + + if ($imaphandle->exists($fullfoldername)) { + return 1; + } + my $parentpath = join ($sep, + (split /$sep/,$fullfoldername)[0,-1] + ); + assureFolder($imaphandle,$parentpath); + $imaphandle->create($fullfoldername) or + die "Unable to create folder $fullfoldername"; +} + +our $Strp = new DateTime::Format::Strptime( + pattern => '%a, %d %b %Y %H:%M:%S %z' +); +our $StrpNoTZ = new DateTime::Format::Strptime( + pattern => '%a, %d %b %Y %H:%M:%S' +); + +sub getMessageYear { + my $msgStr = shift; + my $msgDt = $Strp->parse_datetime($msgStr); + + if (!$msgDt) { + $msgDt = $StrpNoTZ->parse_datetime($msgStr); + } + if (!$msgDt) { + print "Date EMPTY.\n"; + return ""; # TODO: message without Date: + # not sure what to do about it + # Currently just do nothing and + # return empty string. + } + my $year = $msgDt->year; + if ($debug) { + print "\$msgStr = $msgStr, \$msgDt = $msgDt, year = $year\n"; + } + + return $year; +} + +my $imap = Mail::IMAPClient->new(); +if ($ssl) { + my $sslSocket=IO::Socket::SSL->new("$hostname:imaps"); + die ("Error connecting - $@") unless defined $sslSocket; + $sslSocket->autoflush(1); + + $imap = Mail::IMAPClient->new( + Server => $hostname, + Socket => $sslSocket, + User => $login, + Debug => $debug, + Password => $password, + UID => 1 + ) or die "Cannot connect to localhost as matej: $@"; +} else { + $imap = Mail::IMAPClient->new( + Server => $hostname, + User => $login, + Debug => $debug, + Password => $password, + UID => 1 + ) or die "Cannot connect to localhost as matej: $@"; +} + +my $cutDate = DateTime->now(); +$cutDate->add( months => -$howManyMonths ); + +my @sourceFolders = grep(!/^INBOX\/Archiv/,$imap->folders()); +my %targetedMessages; +my ($msgYear,$msgDateStr,$targetFolder); + +foreach my $folder (@sourceFolders) { + $imap->select($folder); + die "Cannot select folder $folder\n" if $@; + my @msgsProc = $imap->search(" UNDELETED BEFORE " . $cutDate->strftime("%d-%b-%Y")); + if ($#msgsProc > 0) { + print "Move $#msgsProc in $folder.\n"; + foreach my $msg (@msgsProc) { + $msgYear = getMessageYear($imap->date($msg)); + if ($msgYear !~ /^\s*$/) { + $targetFolder = getTargetFolder($folder,$msgYear); + if ($debug) { + print "Move message $msg from the folder $folder to $targetFolder.\n"; + } + push ( @{ $targetedMessages{$folder} } , $msg); + } + } + } + foreach my $tFolder (keys %targetedMessages) { + if (!($imap->exists($tFolder))) { + # Not sure how would following deal with non-existent + # parent folder, so rather recursively create + # parents. + # + #$imap->create($tFolder) + # or die "Could not create $tFolder: $@\n"; + assureFolder($imap,$tFolder); + } + $imap->move($tFolder,$targetedMessages{$tFolder}); + } +} +$imap->close();
\ No newline at end of file diff --git a/expireIMAP.pl b/expireIMAP.pl new file mode 100644 index 0000000..5f00651 --- /dev/null +++ b/expireIMAP.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Mail::IMAPClient; +use Data::Dumper; +use DateTime; + +my $hostname = "localhost"; +my $login = "matej"; +my $passwd = "lubdkc"; + +die "Just one name of the folder, please." unless $#ARGV == 0; + +# How many months before today the cut date should be? +my $howManyMonths = 3; +my $lengthOfScreen = 65; + +my $imap = Mail::IMAPClient->new(); +$imap = Mail::IMAPClient->new( + Server => $hostname, + User => $login, + Password => $passwd, + UID => 1 +) or die "Cannot connect to localhost as matej: $@"; + +my $cutDate = DateTime->now(); +$cutDate->add( months => -$howManyMonths ); + +# Do the stuff! +$imap->select($ARGV[0]); +my @msgsProc = $imap->search(" UNDELETED BEFORE " . $cutDate->strftime("%d-%b-%Y")); +print "\$#msgsProc = $#msgsProc\n"; +$imap->delete_message(@msgsProc) or die "Cannot delete $#msgsProc messages."; +$imap->close();
\ No newline at end of file diff --git a/listFolders.py b/listFolders.py new file mode 100755 index 0000000..db75596 --- /dev/null +++ b/listFolders.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3.6 + +import configparser +import os.path + +import imapy + +config = configparser.ConfigParser() +config.read(os.path.expanduser('~/.config/imap_archiver.cfg')) +cfg = dict(config.items(config['general']['account'])) +box = imapy.connect(**cfg) + +for fold in box.folders(): + print(fold) diff --git a/playWithImap.py b/playWithImap.py new file mode 100755 index 0000000..dd68fb6 --- /dev/null +++ b/playWithImap.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3.6 + +import configparser +import os.path + +import imapy + +config = configparser.ConfigParser() +config.read(os.path.expanduser('~/.config/imap_archiver.cfg')) +cfg = dict(config.items(config['general']['account'])) +print(cfg) + + +# my $msg; +# +# my $imap = Mail::IMAPClient->new(); +# $imap = Mail::IMAPClient->new( +# Server => "localhost", +# User => "matej", +# Password => "lubdkc", +# UID => 1 +# ) or die "Cannot connect to localhost as matej: $@"; +# +# $imap->select('INBOX'); +# my $hash = $imap->fetch_hash("RFC822.SIZE","INTERNALDATE","FLAGS","ENVELOPE"); +# #print Data::Dumper->Dumpxs([$hash],['$hash']); +# +# #print "\n===============================\n"; +# my @folders = $imap->folders; +# #print join("\n",@folders),".\n"; +# +# #print "\n===============================\n"; +# # or s/folders/subscribed/ +# @folders = $imap->folders("INBOX" . $imap->separator . "Pratele" . $imap->separator); +# print join("\n",@folders),".\n"; +# +# print "\n===============================\n"; +# my $msgID; +# my $headers; +# foreach $msg (keys(%$hash)) { +# $msgID = $imap->get_header($msg,"Message-Id"); +# print "\$msg ID = $msg\nMESSAGE-ID = $msgID\n"; +# $headers = $imap->parse_headers($msg,"Date","Received","Subject","To"); +# print "Headers = " . Data::Dumper->Dumpxs([$headers],['$headers']) . "\n"; +# } +# +# ## Get a list of messages in the current folder: +# #my @msgs = $imap->messages or die "Could not messages: $@\n"; +# ## Get a reference to an array of messages in the current folder: +# #my $msgs = $imap->messages or die "Could not messages: $@\n"; +# +# #use Mail::IMAPClient; +# # my $imap = Mail::IMAPClient->new( Server => $imaphost, +# # User => $login, +# # Password=> $pass, +# # Uid => 1, # optional +# # ); +# # +# # $imap->select("World Domination"); +# # # get the flags for every message in my 'World Domination' folder +# # $flaghash = $imap->flags( scalar($imap->search("ALL"))) ; +# # +# # # pump through sorted hash keys to print results: +# # for my $k (sort { $flaghash->{$a} <=> $flaghash->{$b} } keys %$flaghash) { +# # # print: Message 1: \Flag1, \Flag2, \Flag3 +# # print "Message $k:\t",join(", ",@{$flaghash->{$k}}),"\n"; +# # } diff --git a/previousAttempts/jython/pydoctest.py b/previousAttempts/jython/pydoctest.py deleted file mode 100644 index 863be80..0000000 --- a/previousAttempts/jython/pydoctest.py +++ /dev/null @@ -1,63 +0,0 @@ -""" -This is the "example" module. - -The example module supplies one function, factorial(). For example, - ->>> factorial(5) -120 -""" - -def factorial(n): - """Return the factorial of n, an exact integer >= 0. - - If the result is small enough to fit in an int, return an int. - Else return a long. - - >>> [factorial(n) for n in range(6)] - [1, 1, 2, 6, 24, 120] - >>> [factorial(long(n)) for n in range(6)] - [1, 1, 2, 6, 24, 120] - >>> factorial(30) - 265252859812191058636308480000000L - >>> factorial(30L) - 265252859812191058636308480000000L - >>> factorial(-1) - Traceback (most recent call last): - ... - ValueError: n must be >= 0 - - Factorials of floats are OK, but the float must be an exact integer: - >>> factorial(30.1) - Traceback (most recent call last): - ... - ValueError: n must be exact integer - >>> factorial(30.0) - 265252859812191058636308480000000L - - It must also not be ridiculously large: - >>> factorial(1e100) - Traceback (most recent call last): - ... - OverflowError: n too large - """ - - import math - if not n >= 0: - raise ValueError("n must be >= 0") - if math.floor(n) != n: - raise ValueError("n must be exact integer") - if n+1 == n: # catch a value like 1e300 - raise OverflowError("n too large") - result = 1 - factor = 2 - while factor <= n: - result *= factor - factor += 1 - return result - -def _test(): - import doctest - doctest.testmod() - -if __name__ == "__main__": - _test() diff --git a/previousAttempts/perl/archiveIMAP.pl b/previousAttempts/perl/archiveIMAP.pl Binary files differdeleted file mode 100644 index 818dad9..0000000 --- a/previousAttempts/perl/archiveIMAP.pl +++ /dev/null diff --git a/previousAttempts/perl/expireIMAP.pl b/previousAttempts/perl/expireIMAP.pl Binary files differdeleted file mode 100644 index 873c275..0000000 --- a/previousAttempts/perl/expireIMAP.pl +++ /dev/null diff --git a/previousAttempts/perl/listFolders.pl b/previousAttempts/perl/listFolders.pl deleted file mode 100644 index 5be4bc6..0000000 --- a/previousAttempts/perl/listFolders.pl +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use Mail::IMAPClient; -use Data::Dumper; - -my $msg; -my $hostname = "localhost"; -my $login = "matej"; -my $passwd = "lubdkc"; - -my $imap = Mail::IMAPClient->new(); -$imap = Mail::IMAPClient->new( - Server => $hostname, - User => $login, - Password => $passwd, - UID => 1 -) or die "Cannot connect to localhost as matej: $@"; - -print "\n===============================\n"; -my @folders = $imap->folders; -print join("\n",@folders),"\n";
\ No newline at end of file diff --git a/previousAttempts/perl/playWithImap.pl b/previousAttempts/perl/playWithImap.pl Binary files differdeleted file mode 100644 index f062d73..0000000 --- a/previousAttempts/perl/playWithImap.pl +++ /dev/null |