summaryrefslogtreecommitdiffstats
path: root/lib/App/ImapNotify.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/App/ImapNotify.pm')
-rw-r--r--lib/App/ImapNotify.pm44
1 files changed, 40 insertions, 4 deletions
diff --git a/lib/App/ImapNotify.pm b/lib/App/ImapNotify.pm
index e7aba6f..b7b1cf7 100644
--- a/lib/App/ImapNotify.pm
+++ b/lib/App/ImapNotify.pm
@@ -11,6 +11,7 @@ use App::ImapNotify::Notifier;
use Carp;
use Function::Parameters;
use Log::Any qw($log);
+use Syntax::Keyword::Try;
=encoding utf-8
@@ -55,10 +56,47 @@ method new_no_defaults($class: $config, $deps = {}) {
return $self;
}
+=head3 loop_reconnect
+
+ $app->loop_reconnect();
+
+Same as loop(), but automatcally calls loop() again if the connection is lost.
+
+=cut
+
+method loop_reconnect() {
+ my $holdoff_time = 10;
+ my $holdoff_repeat = 0;
+ my $holdoff_limit = 300;
+
+ while (1) {
+ my $last_time = time;
+ try {
+ $self->loop();
+ } catch {
+ die $@ unless $@ =~ m/^Lost connection while .*/;
+ }
+ $holdoff_repeat = 0 if (time - $last_time) > $holdoff_limit;
+ sleep($holdoff_time * $holdoff_repeat++);
+ }
+}
+
+=head3 loop
+
+ $app->loop();
+
+Open a connection and wait for NOTIFY notifications. When a NOTIFY notification
+arrives, show a notification to the user.
+
+This method throws an exception when the connection to the server is lost. If
+you want to continue waiting for new notifications, you may call this method
+again. Also look at loop_reconnect().
+
+=cut
+
method loop() {
- #my $imap = $self->{deps}->{imap_client}->connect($self->{config}->@{qw(host port log_id)});
- #$imap->login($self->{config}->@{qw(username password)});
my $imap = $self->{deps}->{imap_client};
+ $imap->reconnect();
$imap->select($self->{config}->{mailboxes}->@[0]);
$imap->send_command("notify set (selected (MessageExpunge MessageNew (uid body.peek[header.fields (from to subject)]))) (mailboxes (".join(' ', $self->{config}->{mailboxes}->@*).") (MessageNew MessageExpunge MailboxName))");
@@ -75,8 +113,6 @@ method loop() {
my $mailbox = $+{mailbox};
my $uid = $+{uidnext} - 1;
$log->debugf("Got status change: '%s'", $line =~ s/\r\n$//r);
- #$imap2->select($mailbox);
- #my $message = $imap2->send_command("uid fetch $uid (body.peek[header.fields (from to subject)])");
$imap->select($mailbox);
my $message = $imap->send_command("uid fetch $uid (body.peek[header.fields (from to subject)])");
pop @{$message};