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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#!/usr/bin/perl -w
use strict;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use Curses::UI;
use Curses;
use Net::POP3;
use Mail::Header;
use Mail::Address;
my $cui = new Curses::UI ( -clear_on_exit => 1 );
my $pop3 = undef;
my $connection = undef;
# We do not want STDERR to clutter our screen.
open STDERR, ">/dev/null";
# ----------------------------------------------------------------------
# setup(): Setup the connection
# ----------------------------------------------------------------------
sub check_connection($;)
{
my $buttons = shift;
my $conwin = $buttons->parent;
my $cui = $conwin->root;
foreach my $key ('username','password','host','port')
{
my $obj = $conwin->getobj($key);
my $value = $obj->get;
$connection->{$key} = $value;
# TODO: focus back to entry does not seem to
# move the cursor with it.
if ($value =~ /^\s*$/) {
$cui->error("Missing value for $key field");
$obj->focus;
return;
}
}
return 1;
}
sub setup_connection()
{
my $conwin = $cui->add(
'connection_window', 'Window',
-border => 1,
-ipad => 2,
-height => 15,
-width => 60,
-centered => 1,
-title => "POP3 connection",
);
$conwin->add(
'host_label', 'Label',
-x => 0, -y => 0, -width => 13,
-textalignment => 'right',
-text => 'POP3 host :',
);
$conwin->add(
'host', 'TextEntry',
-x => 14, -y => 0,
-text => 'pop',
);
$conwin->add(
'port_label', 'Label',
-x => 0, -y => 2, -width => 13,
-textalignment => 'right',
-text => 'POP3 port :',
);
$conwin->add(
'port', 'TextEntry',
-x => 14, -y => 2,
-regexp => '/^\d*$/',
-text => '110',
);
$conwin->add(
'username_label', 'Label',
-x => 0, -y => 4, -width => 13,
-textalignment => 'right',
-text => 'Username :',
);
$conwin->add(
'username', 'TextEntry',
-x => 14, -y => 4,
-text => getpwuid($>),
);
$conwin->add(
'password_label', 'Label',
-x => 0, -y => 6, -width => 13,
-textalignment => 'right',
-text => 'Password :',
);
$conwin->add(
'password', 'TextEntry',
-x => 14, -y => 6,
-password => '*',
-text => '',
)->focus;
my $buttons = $conwin->add(
'buttons', 'Buttonbox',
-x => 14, -y => 8,
-buttons => [
{ -label => '< Connect >',
-onpress => sub {
my $this = shift;
if (check_connection($this)) {
if (pop3_connect()) {
$this->parent->loose_focus;
}
}
},
},
{ -label => '< Quit >',
-onpress => sub {exit} },
],
);
$conwin->modalfocus;
$cui->delete('connection_window')
}
# ----------------------------------------------------------------------
# pop3_connect(): Connect to the POP3 server and exit if it fails
# ----------------------------------------------------------------------
sub pop3_connect()
{
$cui->progress(
-message => "Connecting to the POP3 server...",
-max => 4,
-pos => 1,
);
my $error = 0;
$pop3 = Net::POP3->new(
$connection->{host},
Port => $connection->{port},
Timeout => 0,
);
if (not $pop3) {
$error++;
$cui->error("Could not connect to "
."$connection->{host}:$connection->{port}");
}
$cui->setprogress(2, "Sending username...");
if (not $error and not defined $pop3->user($connection->{username})) {
$error++;
my $err = $pop3->message(); chomp $err;
$cui->error("Sending USER failed:\n$err");
}
$cui->setprogress(3, "Sending password...");
if (not $error and not defined $pop3->pass($connection->{password})) {
$error++;
my $err = $pop3->message(); chomp $err;
$cui->error("Sending PASS failed:\n$err");
}
if (not $error) {
$cui->setprogress(4, "Connection successful!");
sleep 1;
}
$cui->noprogress;
return !$error;
}
# ----------------------------------------------------------------------
# The inbox screen
# ----------------------------------------------------------------------
sub build_inbox()
{
my $list = $pop3->list();
my @ids = sort {$a<=>$b} keys %$list;
my $msg = "Retrieving headers";
$cui->progress(
-max => scalar(@ids),
-message => $msg,
);
my @values = ();
my %labels = ();
my $progress_pos = 0;
foreach my $n (@ids)
{
my $lines = $pop3->top($n, 0);
my $header = new Mail::Header($lines);
# Add value
push @values, $n;
# Add label
my $subject = $header->get('Subject');
my $from = $header->get('From');
my $addr = new Mail::Address($from);
my $name = substr($addr->name, 0, 15);
$labels{$n} = sprintf("%4d", $n)
. " | "
. sprintf("%15s", $name)
. " | "
. $header->get('Subject');
$cui->setprogress(
++$progress_pos,
$msg . ": message $progress_pos of " . scalar(@ids)
);
}
$cui->noprogress;
my $listwin = $cui->add('list_window', 'Window');
my $ml = $listwin->add(
'message_list', 'Listbox',
-values => \@values,
-labels => \%labels,
-vscrollbar => 1,
-border => 1,
-ipad => 1,
-title => '<ENTER> view message <CTR+Q> Quit from program',
);
$ml->set_binding(sub{exit(0)}, "\cC", "\cQ");
$ml->set_routine('option-select', \&view_message);
}
# ----------------------------------------------------------------------
# view_message(): callback routine for the inbox list
# ----------------------------------------------------------------------
sub view_message()
{
my $this = shift;
# Get the selected message id.
$this->{-selected} = $this->{-ypos};
my $id = $this->get;
$this->{-selected} = undef;
# Retrieve the message from the POP3 server.
$cui->status("Retrieving message $id from the POP3 server...");
my $lines = $pop3->get($id);
unless (ref $lines)
{
# Maybe the connection went away. Reconnect and try again.
$pop3->close;
unless (pop3_connect())
{
$cui->error("Fatal error: Could not reconnect\n"
. "to the POP3 server.");
exit(1);
}
$lines = $pop3->get($id);
}
unless (ref $lines)
{
$cui->error("Failed to retrieve message $id\n"
."from the POP3 server.\n"
."Even after reconnecting");
exit_program();
}
$cui->nostatus;
# Create the viewer window.
my $viewwin = $cui->add('view_window', 'Window');
my $tv = $viewwin->add(
'textviewer', 'TextViewer',
-text => join("", @$lines),
-vscrollbar => 1,
-wrapping => 1,
-border => 1,
-ipad => 1,
-title => '<ENTER> return to inbox <CTRL+Q>: Quit from program',
);
$viewwin->set_binding(sub{ shift()->loose_focus }, KEY_ENTER());
$viewwin->set_binding(sub{exit(0)}, "\cC", "\cQ");
$viewwin->modalfocus;
$cui->delete('view_window');
$cui->draw;
}
# ----------------------------------------------------------------------
# Clean exit
# ----------------------------------------------------------------------
END { $pop3->quit if defined $pop3 }
# ----------------------------------------------------------------------
# The main program
# ----------------------------------------------------------------------
setup_connection();
build_inbox();
$cui->mainloop;
|