summaryrefslogtreecommitdiffstats
path: root/script/imap-notify.pl
blob: 234c5e8a940518ce2d7811e7f7ae5a05e22a4d1c (plain)
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
#!/usr/bin/env perl

use v5.24;

use warnings;
use strict;

use App::ImapNotify;
use Function::Parameters;
use Log::Any::Adapter ('Stderr', log_level => "info");
use MCE::Hobo;
use Path::Tiny;
use TOML qw(from_toml);

=head1 NAME

imap-notify.pl - Simple notification script using IMAP NOTIFY

=head1 SYNOPSIS

  imap-notify.pl [options]

  Options:
    --debug

=head1 DESCRIPTION

Simple notification script using IMAP NOTIFY. Note that it is very simple and
implements a custom IMAP client with very limited features. Mostly a proof of
concept and personal script.

=head1 CONFIGURATION

To configure the script create a file called $XDG_CONFIG_HOME/imap-notify/config.toml.

You should create an array called "watches" with the following settings:

=over

=item log_id

String to identify a particular connection in the debug log.

=item host

Hostname or IP of the server to connect to.

=item port

Port to connect to.

=item username

Username to use to authenticate.

=item passwordeval

Command that is run by the script and that outputs the password that will be used to authenticate on stdout.

=item mailboxes

Array of mailbox names to watch.

=back

=head2 Example Configuration

 [[watches]]
 log_id = "first-server"
 host = "mail.first-server.localdomain"
 port = 993
 username = "my-username"
 passwordeval = "getpw-single my-username-password"
 mailboxes = ["INBOX", "INBOX.Postmaster", "Other.Mailbox"]
 
 [[watches]]
 log_id = "second-server"
 host = "imap.second.localdomain"
 port = 993
 username = "user@second.localdomain"
 passwordeval = "getpw-single user@second.localdomain"
 mailboxes = ["INBOX"]

=head1 SEE ALSO

L<App::ImapNotify>

=cut

my $config = from_toml(path(($ENV{XDG_CONFIG_HOME} // $ENV{HOME}."/.config")."/imap-notify/config.toml")->slurp);

if ($ARGV[0] // "" eq "--debug") {
	Log::Any::Adapter->set("Stderr", log_level => "trace");
}

#$IO::Socket::SSL::DEBUG = 4;

my @workers;

for my $single_conf ($config->{watches}->@*) {
	$single_conf->{password} = `$single_conf->{passwordeval}`;
	push @workers, mce_async {
		my $app = App::ImapNotify->new($single_conf);
		$app->loop_reconnect();
	}
}

map {$_->join()} @workers;

fun trim($string) {
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}