summaryrefslogtreecommitdiffstats
path: root/mailer.pl
diff options
context:
space:
mode:
Diffstat (limited to 'mailer.pl')
-rw-r--r--mailer.pl46
1 files changed, 33 insertions, 13 deletions
diff --git a/mailer.pl b/mailer.pl
index c6c1af2..41bd06e 100644
--- a/mailer.pl
+++ b/mailer.pl
@@ -7,13 +7,14 @@ use Sys::Syslog qw(:standard :macros);
use OpenBSD::Pledge;
use OpenBSD::Unveil;
-my $dict_dir = '/var/www/var/lex/dict';
-my $state_file = '/var/www/var/lex/mailer.tsv';
-my $to = 'me@example.com';
-my $from = 'Lex <lex@example.com>';
-my $subject = 'Daily word';
-my $sendmail = '/usr/sbin/sendmail';
-my $syslog_tag = 'lex-mail';
+my $dict_dir = '/var/www/var/lex/dict';
+my $state_file = '/var/www/var/lex/mailer.tsv';
+my $to = 'me@example.com';
+my $from = 'lex@example.com';
+my $from_header = "Lex <$from>";
+my $subject = 'Daily word';
+my $sendmail = '/usr/sbin/sendmail';
+my $syslog_tag = 'lex-mail';
# Connect to /dev/log before pledge/unveil so no late 'unix'
# socket creation occurs
@@ -37,7 +38,8 @@ sub log_die {
}
# Read dictionary files
-if (!opendir(my $dh, $dict_dir)) {
+my $dh;
+if (!opendir($dh, $dict_dir)) {
log_die("Cannot open $dict_dir: $!");
}
my @current_files = grep { -f "$dict_dir/$_" && !/^\./ } readdir($dh);
@@ -45,6 +47,15 @@ closedir($dh);
if (!@current_files) {
syslog(LOG_WARNING, "No files found in %s", $dict_dir);
+ # Truncate state file if it exists
+ if (-f $state_file) {
+ if (open(my $clear_fh, '>', $state_file)) {
+ close($clear_fh);
+ } else {
+ syslog(LOG_ERR, "Failed to clear %s: %s", $state_file, $!);
+ }
+ }
+
closelog();
exit 0;
}
@@ -54,7 +65,8 @@ my %state;
my $today = localtime->ymd;
if (-f $state_file) {
- if (!open(my $sfh, '<', $state_file)) {
+ my $sfh;
+ if (!open($sfh, '<', $state_file)) {
log_die("Cannot open $state_file: $!");
}
while (my $line = <$sfh>) {
@@ -100,7 +112,8 @@ if (@due_files) {
# Read content
my $file_path = "$dict_dir/$selected_file";
-if (!open(my $fh, '<:utf8', $file_path)) {
+my $fh;
+if (!open($fh, '<:utf8', $file_path)) {
log_die("Cannot open $file_path: $!");
}
local $/; # slurp mode
@@ -108,12 +121,13 @@ my $body = <$fh>;
close($fh);
# Send email via direct pipe
-if (!open(my $mail_pipe, '|-', $sendmail, '-i', '-f', $from, $to)) {
+my $mail_pipe;
+if (!open($mail_pipe, '|-', $sendmail, '-i', '-f', $from, $to)) {
log_die("Cannot execute $sendmail: $!");
}
binmode($mail_pipe, ':utf8');
-print $mail_pipe "From: $from\n";
+print $mail_pipe "From: $from_header\n";
print $mail_pipe "To: $to\n";
print $mail_pipe "Subject: $subject - $selected_file\n";
print $mail_pipe "Content-Type: text/plain; charset=UTF-8\n\n";
@@ -142,7 +156,13 @@ $item->{reviews} += 1;
my $now = localtime;
$item->{next_due} = ($now + ($interval * 86400))->ymd;
-if (!open(my $out_fh, '>', $state_file)) {
+# Toss deleted files from the tsv.
+foreach my $file (keys %state) {
+ delete $state{$file} unless $active_files{$file};
+}
+
+my $out_fh;
+if (!open($out_fh, '>', $state_file)) {
log_die("Cannot write to $state_file: $!");
}
foreach my $file (keys %state) {