summaryrefslogtreecommitdiffstats
path: root/lib/App/ImapNotify
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2018-07-19 16:29:32 +0200
committerFlorian Pritz <bluewind@xinu.at>2018-07-19 16:29:32 +0200
commitc66032630f0e4fc3f4b59413a12f9ab35be958be (patch)
tree267ad62e364f141a6c40ac33e8c03794befdd2ff /lib/App/ImapNotify
parent12e45f6e461696131b8950245ac26f1dc7fb5573 (diff)
downloadApp-ImapNotify-c66032630f0e4fc3f4b59413a12f9ab35be958be.tar.gz
App-ImapNotify-c66032630f0e4fc3f4b59413a12f9ab35be958be.tar.xz
Reconnect when connection is lost
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'lib/App/ImapNotify')
-rw-r--r--lib/App/ImapNotify/ImapClient.pm31
-rw-r--r--lib/App/ImapNotify/Socket/SSL.pm20
2 files changed, 28 insertions, 23 deletions
diff --git a/lib/App/ImapNotify/ImapClient.pm b/lib/App/ImapNotify/ImapClient.pm
index 6e5e21b..ae91c78 100644
--- a/lib/App/ImapNotify/ImapClient.pm
+++ b/lib/App/ImapNotify/ImapClient.pm
@@ -53,27 +53,33 @@ method new_no_defaults($class: $config, $deps = {}) {
$self->{keepalive_timeout} = $config->{keepalive_timeout};
$self->{line_buffer} = [];
$self->{deps} = $deps;
- $self->_connect();
- $self->_login($config->@{qw(username password)});
+ $self->{config} = $config;
return $self;
}
-method _connect() {
- # wait for server greeting
+method reconnect() {
+ $self->{command_counter} = 0;
+ $self->{deps}->{sock}->reconnect();
my $response = $self->readline_block();
+ croak("Lost connection while waiting for server greeting") if not defined $response;
+
if ($response !~ m/^\* OK (.*)/) {
- confess "Invalid server greeting. Got reply: $response";
+ confess "Invalid server greeting. Got reply: '$response'";
}
$self->{capabilities} = $1;
+
+ $self->_login();
}
-method _login($username, $password) {
+method _login() {
if ($self->{capabilities} !~ m/\bAUTH=PLAIN\b/) {
croak "Server doesn't support AUTH=PLAIN";
}
- my $response = $self->send_command("login $username $password");
+ my $response = $self->send_command("login $self->{config}->{username} $self->{config}->{password}");
+ return if not defined $response;
+
confess "No capabilities found in response: '".$response->[0]."'" unless $response->[0] =~ m/^OK \[(.*)\].*$/;
$self->{capabilities} = $1;
@@ -92,15 +98,10 @@ method select($mailbox) {
method send_command($command) {
- state $counter = 0;
-
- my $id = "CMD-".$counter++;
+ my $id = "CMD-".$self->{command_counter}++;
chomp($command);
- #print "Sending $command\n";
- #sleep (5) if $command eq "noop";
-
$self->writeline("$id $command\r\n");
my @lines;
@@ -125,8 +126,10 @@ method send_command($command) {
if ($line =~ /^(.*\r\n)$/) {
push @lines, $1;
}
-
}
+
+ croak("Lost connection while waiting for reply to command '$command'");
+ return;
}
method get_uids($mailbox) {
diff --git a/lib/App/ImapNotify/Socket/SSL.pm b/lib/App/ImapNotify/Socket/SSL.pm
index 18d7ffa..2654cdc 100644
--- a/lib/App/ImapNotify/Socket/SSL.pm
+++ b/lib/App/ImapNotify/Socket/SSL.pm
@@ -8,24 +8,26 @@ use Function::Parameters;
use IO::Socket::SSL;
use Log::Any qw($log);
-method new($class: $host, $port, $deps = {}) {
- $deps->{sock} //= IO::Socket::SSL->new("$host:$port");
- return $class->new_no_defaults($deps);
-}
-
-method new_no_defaults($class: $deps = {}) {
+method new($class: $host, $port) {
my $self = {};
+ $self->{config} = {
+ host => $host,
+ port => $port,
+ };
bless $self, $class;
- $self->{deps} = $deps;
return $self;
}
+method reconnect() {
+ $self->{sock} = IO::Socket::SSL->new("$self->{config}->{host}:$self->{config}->{port}");
+}
+
method readline() {
- return CORE::readline $self->{deps}->{sock};
+ return CORE::readline $self->{sock};
}
method writeline($line) {
- print {$self->{deps}->{sock}} $line;
+ print {$self->{sock}} $line;
}