imap.pl untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 1 Podpod n/a totaltotal 0.0% 0 / 122 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/perl
2 require 'lib/Net/IMAP/Simple.pm';
3 print "Square brackets: [] indicate optional arguments\n\n";
4 print "IMAP Server[:port] [localhost]: ";
5  
6 while(<>){
7 chomp;
8 $_ ||= 'localhost';
9 $imap = Net::IMAP::Simple->new($_, port => 143, timeout => 90) || die "$Net::IMAP::Simple::errstr\n";
10 if($imap){
11 print "Connected.\n";
12 last;
13 } else {
14 print "Connection to $_ failed: $Net::IMAP::Simple::errstr\n";
15 print "IMAP Server[:port]: ";
16 }
17 }
18  
19 print "User: ";
20 while(<>){
21 chomp;
22 $user = $_;
23 if(!$user){
24 print "Blank user not allowed\n";
25 print "User: ";
26 } else {
27 last;
28 }
29 }
30  
31 print "Password: ";
32 system("stty -echo");
33 while(<>){
34 chomp;
35 if(!$imap->login($user, $_)){
36 print "Login failed: " . $imap->errstr . "\n";
37 } else {
38 my $msgs = $imap->select("INBOX");
39 print "Messages in INBOX: $msgs\n";
40 last;
41 }
42 }
43  
44 system("stty echo");
45 print "\n";
46  
47 my $ptc = qq{
48 Please enter a command:
49  
50 help - This help screen
51 list - List all folders / mail boxes accessable by this account
52 folders - List all folders within <box>
53 select box <box> - Select a mail box
54 select folder <folder> - Select a folder within <box>, format: Some.Folder.I.Own
55 which looks like: Some/Folder/I/Own
56 exit - Disconnect and close
57  
58 };
59  
60 print $ptc . "[root] ";
61  
62 my %o;
63 while(<>){
64 chomp;
65 my (@folders, %boxes);
66 my @folders = $imap->mailboxes;
67 for(@folders){
68 $boxes{ (split(/\./))[0] } = 1;
69 }
70  
71 my @io = split(/\s+/, $_);
72  
73 if($io[0] eq 'select'){
74 if($io[1] eq 'box'){
75 if(!$boxes{ $io[2] }){
76 print $ptc . "Invalid mail box: $io\n\n";
77 } else {
78 print "\n-- Mail box successfully selected --\n $io[2]\n\n";
79 $o{box} = $io[2];
80 }
81 } elsif($io[1] eq 'folder'){
82 my $c = $imap->select($io[2]);
83 if(!defined $c){
84 print $ptc . "Select error: " . $imap->errstr . "\n\n";
85 } else {
86 print "-- Folder information: $io[2] --\n";
87 print " Messages: " . $c . "\n";
88 print " Recent: " . $imap->recent . "\n";
89 print " Flags: " . $imap->flags . "\n";
90 print "Flag List: " . join(" ", $imap->flags) . "\n\n";
91 # $o{folder} = $io[2];
92 }
93 } else {
94 print $ptc . "Invalid select option\n\n";
95 }
96 } elsif($io[0] eq 'list'){
97 print "-- Avaliable mail folders/boxes --\n";
98 for(keys %boxes){
99 print "Mail box: $_\n";
100 }
101 print "\n";
102 } elsif($io[0] eq 'folders' && $o{box}){
103 print "-- Listing folders in: $o{box} --\n";
104 my $x = $o{box};
105 $x =~ s/(\W)/\\$1/g;
106 for(@folders){
107 if(/^$x/){
108 my $msgs = $imap->select($_);
109 if(!defined $msgs){
110 print "Failed to read: $o{box} -> $_: " . $imap->errstr . "\n";
111 } else {
112 printf("$o{box} -> $_ " . (" " x (30 - length($_))) . "[%06d]\n", $msgs);
113 }
114 }
115 }
116 print "\n";
117 } elsif($io[0] eq 'exit' || $io[0] eq 'quit'){
118 print "Good bye!\n\n";
119 $imap->quit;
120 exit;
121 } elsif($io[0] eq 'help'){
122 print $ptc;
123 } else {
124 print $ptc . "Invalid command: $io[0]\n\n";
125 }
126  
127 print "[" . ($o{box} ? $o{box} : 'root') . ($o{folder} ? " -> $o{folder}" : '') . "] ";
128 }