-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttn-letterbox-notifyEmail.pm
executable file
·249 lines (203 loc) · 7.85 KB
/
ttn-letterbox-notifyEmail.pm
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
#!/bin/perl -w -T
#
# TheThingsNetwork HTTP letter box sensor notification via E-Mail
#
# (P) & (C) 2021-2024 Dr. Peter Bieringer <[email protected]>
#
# License: GPLv3
#
# Authors: Dr. Peter Bieringer (bie)
#
# Required system features
# - available Perl module MIME::Lite
# successfully tested on EL8 using RPM perl-MIME-Lite using
# smtp via localhost (but this has delays on script response)
#
# Required configuration:
# - enable sending messages (otherwise dry-run)
# notifyEmail.enable=1
#
# - sender E-Mail address (must be permitted to send)
# EXAMPLE:
#
# Optional configuration:
# - control debug
# notifyEmail.enable=1
#
# Honors entries starting with "email=" from "@notify_list" provided by main CGI
#
# Changelog:
# 20210628/bie: initial version (based on ttn-letterbox-notifyEmail.pm)
# 20210820/bie: log empty recipent list only on debug level
# 20211001/bie: adjust German translation
# 20211030/bie: add support for v3 API
# 20220218/bie: remove support of debug option by environment
# 20220421/bie: return earlier from notifyEmail_init when not enabled
# 20240119/bie: add support for device alias, cosmetics
# 20240326/bie: add Unicode chars for clock and part of day, remove seconds from timestamp
#
# TODO: implement faster mail delivery methods like "mailx"
use strict;
use warnings;
use utf8;
use Encode;
require MIME::Lite;
## globals
our %hooks;
our %config;
our %features;
our %translations;
our $language;
our @notify_list;
## prototyping
sub notifyEmail_init();
sub notifyEmail_store_data($$$);
## hooks
$hooks{'notifyEmail'}->{'init'} = \¬ifyEmail_init;
$hooks{'notifyEmail'}->{'store_data'} = \¬ifyEmail_store_data;
## translations
$translations{'emptied'}->{'de'} = "GELEERT";
$translations{'filled'}->{'de'} = "GEFÜLLT";
$translations{'at'}->{'de'} = "am";
$translations{'At'}->{'de'} = "Am";
## active status (= passed all validity checks)
my $notifyEmail_active = 0;
my $notifyEmail_enable = 0;
# clock icons 0000 -> 11:30
my @icon_clock = (
"🕛", "🕧", "🕐", "🕜", "🕑", "🕝",
"🕒", "🕞", "🕓", "🕟", "🕔", "🕠",
"🕕", "🕡", "🕖", "🕢", "🕗", "🕣",
"🕘", "🕤", "🕙", "🕥", "🕚", "🕦"
);
# day icon
my @icon_day = (
"🌆" , # night
"🌅" , # sunrise
"🌞" , # day
"🌇" , # sunset
);
############
## init module
############
sub notifyEmail_init() {
# set feature
$features{'notify'} = 1;
if (defined $config{'notifyEmail.debug'} && $config{'notifyEmail.debug'} eq "0") {
undef $config{'notifyEmail.debug'};
};
logging("notifyEmail/init: called") if defined $config{'notifyEmail.debug'};
if (! defined $config{'notifyEmail.enable'}) {
logging("notifyEmail/init/NOTICE: missing entry in config file: notifyEmail.enable -> notifications not enabled") if defined $config{'notifyEmail.debug'};
$config{'notifyEmail.enable'} = "0";
};
if ($config{'notifyEmail.enable'} ne "1") {
logging("notifyEmail/init/NOTICE: notifyEmail.enable is not '1' -> notifications not enabled") if defined $config{'notifyEmail.debug'};
return 0;
} else {
$notifyEmail_enable = 1;
};
if (! defined $config{'notifyEmail.sender'}) {
logging("notifyEmail/init/ERROR: missing entry in config file: notifyEmail.sender");
return 0;
};
if ($config{'notifyEmail.sender'} !~ /^[0-9a-z\.\-\+]+\@[0-9a-z\.\-]+$/o) {
logging("notifyEmail/init/ERROR: notifyEmail.sender is not a valid E-Mail address: " . $config{'notifyEmail.sender'});
return 0;
};
$notifyEmail_active = 1;
};
## store data
sub notifyEmail_store_data($$$) {
my $dev_id = $_[0];
my $timeReceived = $_[1];
my $content = $_[2];
return if ($notifyEmail_active != 1); # nothing to do
my $payload;
$payload = $content->{'uplink_message'}->{'decoded_payload'}; # v3 (default)
$payload = $content->{'payload_fields'} if (! defined $payload); # v2 (fallback)
my $status = $payload->{'box'};
logging("notifyEmail/store_data: called with sensor=$dev_id boxstatus=$status") if defined $config{'notifyEmail.debug'};
if ($status =~ /^(filled|emptied)$/o) {
# filter list
logging("notifyEmail/store_data: notification list: " . join(' ', @notify_list)) if defined $config{'notifyEmail.debug'};
my @notify_list_filtered = grep /^email=/, @notify_list;
logging("notifyEmail/store_data: notification list filtered: " . join(' ', @notify_list_filtered)) if defined $config{'notifyEmail.debug'};
if (scalar(@notify_list_filtered) == 0) {
logging("notifyEmail/store_data: no related entry found in notification list") if defined $config{'notifyEmail.debug'};
return 0;
};
logging("notifyEmail/store_data: notification list: " . join(' ', @notify_list_filtered)) if defined $config{'notifyEmail.debug'};
foreach my $receiver (@notify_list_filtered) {
$receiver =~ s/^email=//o; # remove prefix
if ($receiver !~ /^([0-9a-z\.\-\+]+\@[0-9a-z\.\-]+)(;[a-z]{2})?$/o) {
logging("notifyEmail/store_data: notification receiver not a valid E-Mail address + optional language token (SKIP): " . $receiver);
next;
};
my $recipient = $1;
if (defined $2) {
$language = $2;
$language =~ s/^;//o; # remove separator
};
my $icon = "";
if ($status =~ /^(filled)$/o) {
$icon = "📬 ";
} elsif ($status =~ /^(emptied)$/o) {
$icon = "📪 ";
};
# time and day icons
my $hour = int(strftime("%H", localtime(str2time($timeReceived))));
my $minute = int(strftime("%M", localtime(str2time($timeReceived))));
my $index_day = 0;
$index_day++ if ($hour >= 6);
$index_day++ if ($hour >= 8);
$index_day++ if ($hour >= 18);
$index_day = 0 if ($hour >= 20);
$hour -= 12 if ($hour >= 12);
my $index_clock = ($hour * 60 + $minute) / 30; # 0-23
$icon .= $icon_day[$index_day] . $icon_clock[$index_clock];
my $subject = translate("letterbox") . " ";
$subject .= $icon . " ";
if (defined $config {"alias." . $dev_id}) {
$subject .= $config {"alias." . $dev_id};
} else {
$subject .= $dev_id;
};
$subject .= " " . translate($status) . " " . translate("at") . " " . strftime("%Y-%m-%d %H:%M %Z", localtime(str2time($timeReceived)));
my $data;
if (defined $config {"alias." . $dev_id}) {
$data .= "Sensor: " . $config {"alias." . $dev_id} . " (" . $dev_id . ")\n";
} else {
$data .= "Sensor: " . $dev_id . "\n";
}
$data .= translate("boxstatus") . ": " . translate($status) . "\n";
$data .= translate("at") . ": " . strftime("%Y-%m-%d %H:%M:%S %Z", localtime(str2time($timeReceived))) . "\n";
logging("notifyEmail/store_data: send notification: $dev_id/$status/$receiver") if defined $config{'notifyEmail.debug'};
if ($notifyEmail_enable != 1) {
logging("notifyEmail/store_data/NOTICE: would send E-Mail via MIME::Lite to (if enabled): $recipient");
# skip
} else {
logging("notifyEmail/store_data: call MIME::Lite now with recipient $recipient") if defined $config{'notifyEmail.debug'};
# action
my $msg = MIME::Lite->new(
From => $config{'notifyEmail.sender'},
To => $recipient,
Subject => encode("MIME-Header", $subject),
Data => $data,
Encoding => 'base64',
);
$msg->send("smtp", "localhost"); # delays end of script
my $rc = $?;
logging("notifyEmail/store_data: result of called MIME::Lite: $rc") if defined $config{'notifyEmail.debug'};
if ($rc == 0) {
logging("notifyEmail: notification SUCCESS: $dev_id/$status/$receiver");
} else {
logging("notifyEmail: notification PROBLEM: $dev_id/$status/$receiver (rc=" . $rc . ")");
};
};
};
};
};
return 1;
# vim: set noai ts=2 sw=2 et: