aboutsummaryrefslogtreecommitdiffstats
path: root/expireIMAP.pl
blob: 153a99ffd3a00acd24beaecacb65a6def2799d4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl

use strict;
use warnings;
use Mail::IMAPClient;
use Data::Dumper;
use DateTime::Format::Strptime;

my $msg;
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 $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 $Strp = new DateTime::Format::Strptime(
	pattern     => '%a, %d %b %Y %H:%M:%S %z'
);
my $StrpNoTZ = new DateTime::Format::Strptime(
	pattern     => '%a, %d %b %Y %H:%M:%S'
);

$imap->select($ARGV[0]);
my $hash = $imap->fetch_hash("INTERNALDATE","FLAGS");

my @flags;
my $isDeleted;
my $msgDateStr;
my $msgDate;

my $cutDate = DateTime->now();
$cutDate->add( months => -$howManyMonths );

# FIXME: Wrong -- USE $imap->before(RFC2060 date)
# SEARCH UNDELETED BEFORE 
foreach $msg (keys(%$hash)) {
	@flags = $imap->flags($msg);
	$isDeleted = grep(/\\Deleted/,@flags);
	if (!$isDeleted) {
		$msgDateStr = $imap->date($msg);
		$msgDate = $Strp->parse_datetime($msgDateStr);
		if (!$msgDate) {
			$msgDate = $StrpNoTZ->parse_datetime($msgDateStr); 
		}
		if (!$msgDate) {
			next; # TODO: message without Date:
			      # not sure what to do about it
			      # Currently just do nothing and let it rot there.
		}
		die "WARNING: \$msgDate = $msgDate" if $@;

		if ($msgDate < $cutDate) { 
			print "Msg $msg with date $msgDate is to be deleted.\n";
		}
	}
}

#	print "\@flags = @flags, \$isDeleted = $isDeleted\n";
#	$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";
#               }