b3658cab2f5dbd85883f0f44ba1b5a55223cfa3c
[lugs.git] / lreminder / reminder.pl
1 #!/usr/bin/perl
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 #
17 # Author: Steven Schubiger <stsc@refcnt.org>
18 # Last modified: Tue Jan 6 14:37:04 CET 2015
19
20 use strict;
21 use warnings;
22 use lib qw(lib);
23 use constant true => 1;
24 use constant false => 0;
25
26 use DateTime ();
27 use DBI ();
28 use Encode qw(encode);
29 use File::Basename ();
30 use File::Spec ();
31 use FindBin qw($Bin);
32 use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
33 use Hook::Output::File ();
34 use LUGS::Events::Parser ();
35 use Mail::Sendmail qw(sendmail);
36 use Text::Wrap::Smart::XS qw(fuzzy_wrap);
37 use URI ();
38 use WWW::Mechanize ();
39
40 my $VERSION = '0.50';
41
42 #-----------------------
43 # Start of configuration
44 #-----------------------
45
46 my $Config = {
47 events_url => 'http://www.lugs.ch/lugs/termine/termine.txt',
48 form_url => 'http://lists.lugs.ch/reminder.cgi',
49 mail_from => 'reminder@lugs.ch',
50 dbase_name => '<hidden>',
51 dbase_user => '<hidden>',
52 dbase_pass => '<hidden>',
53 };
54
55 #---------------------
56 # End of configuration
57 #---------------------
58
59 my $dbh = DBI->connect("dbi:mysql(RaiseError=>1):$Config->{dbase_name}", $Config->{dbase_user}, $Config->{dbase_pass});
60 my $file = File::Spec->catfile('tmp', (URI->new($Config->{events_url})->path_segments)[-1]);
61
62 my ($test, $run) = (false, false);
63
64 {
65 getopts(\$test, \$run);
66 my $hook = Hook::Output::File->redirect(
67 stdout => File::Spec->catfile($Bin, 'stdout.out'),
68 stderr => File::Spec->catfile($Bin, 'stderr.out'),
69 );
70 fetch_and_write_events();
71 process_events();
72 }
73
74 sub getopts
75 {
76 my ($test, $run) = @_;
77
78 GetOptions(test => $test, run => $run) or exit;
79
80 if (not $$test || $$run) {
81 die "$0: neither --test nor --run specified, exiting\n";
82 }
83 elsif ($$test && $$run) {
84 die "$0: both --test and --run specified, exiting\n";
85 }
86 return; # --test or --run specified
87 }
88
89 sub fetch_and_write_events
90 {
91 my $mech = WWW::Mechanize->new;
92 my $http = $mech->get($Config->{events_url});
93
94 open(my $fh, '>', $file) or die "Cannot open $file for writing: $!\n";
95 print {$fh} $http->content;
96 close($fh);
97 }
98
99 sub init
100 {
101 my ($parser) = @_;
102
103 $$parser = LUGS::Events::Parser->new($file, {
104 filter_html => true,
105 tag_handlers => {
106 'a href' => [ {
107 rewrite => '$TEXT - <$HREF>',
108 fields => [ qw(responsible) ],
109 }, {
110 rewrite => '$TEXT - $HREF',
111 fields => [ qw(location more) ],
112 } ],
113 },
114 purge_tags => [ qw(location responsible more) ],
115 strip_text => [ 'mailto:' ],
116 });
117 unlink $file;
118 }
119
120 sub process_events
121 {
122 my $parser;
123 init(\$parser);
124
125 while (my $event = $parser->next_event) {
126 my %event = (
127 year => $event->get_event_year,
128 month => $event->get_event_month,
129 day => $event->get_event_day,
130 color => $event->get_event_color,
131 );
132
133 my %sth;
134
135 $sth{subscribers} = $dbh->prepare('SELECT mail, mode, notify FROM subscribers');
136 $sth{subscribers}->execute;
137
138 while (my $subscriber = $sth{subscribers}->fetchrow_hashref) {
139 next unless $subscriber->{mode} == 2;
140
141 $sth{subscriptions} = $dbh->prepare('SELECT * FROM subscriptions WHERE mail = ?');
142 $sth{subscriptions}->execute($subscriber->{mail});
143
144 my $subscriptions = $sth{subscriptions}->fetchrow_hashref;
145 next unless $subscriptions->{$event{color}};
146
147 my $notify = DateTime->now(time_zone => 'Europe/Zurich');
148
149 $subscriber->{notify} ||= 0;
150
151 $notify->add(days => $subscriber->{notify});
152
153 if ($event{year} == $notify->year
154 && $event{month} == $notify->month
155 && $event{day} == $notify->day
156 ) {
157 send_mail($event, $subscriber->{mail});
158 }
159 }
160 }
161 }
162
163 sub send_mail
164 {
165 my ($event, $mail_subscriber) = @_;
166
167 my $year = $event->get_event_year;
168 my $month = $event->get_event_month;
169 my $simple_day = $event->get_event_simple_day;
170 my $wday = $event->get_event_weekday;
171 my $time = $event->get_event_time;
172 my $title = $event->get_event_title;
173 my $color = $event->get_event_color;
174 my $location = $event->get_event_location;
175 my $responsible = $event->get_event_responsible;
176 my $more = $event->get_event_more || '';
177
178 wrap_text(\$more);
179 chomp $more;
180 wrap_text(\$location);
181
182 my $i;
183 my %month_names = map { sprintf('%02d', ++$i) => $_ }
184 qw(Januar Februar Maerz April Mai Juni Juli August
185 September Oktober November Dezember);
186
187 my $month_name = $month_names{$month};
188
189 my $message = (<<"MSG");
190 Wann:\t$wday, $simple_day. $month_name $year, $time Uhr
191 Was :\t$title
192 Wo :\t$location
193 Wer :\t$responsible
194 Info:\t$more
195
196 Web Interface:
197 $Config->{form_url}
198
199 ${\info_string()}
200 MSG
201
202 if ($run) {
203 sendmail(
204 From => $Config->{mail_from},
205 To => $mail_subscriber,
206 Subject => encode('MIME-Q', "LUGS Reminder - $title"),
207 Message => $message,
208 ) or die "Cannot send mail: $Mail::Sendmail::error";
209 }
210 elsif ($test) {
211 printf "[%s] <$mail_subscriber> ($color)\n", scalar localtime;
212 }
213 }
214
215 sub wrap_text
216 {
217 my ($text) = @_;
218
219 return unless length $$text;
220
221 my @chunks = fuzzy_wrap($$text, 70);
222
223 my $wrapped;
224 foreach my $chunk (@chunks) {
225 $wrapped .= ' ' x (defined $wrapped ? 8 : 0);
226 $wrapped .= "$chunk\n";
227 }
228 chomp $wrapped;
229
230 $$text = $wrapped;
231 }
232
233 sub info_string
234 {
235 my $script = File::Basename::basename($0);
236 my $modified = localtime((stat($0))[9]);
237
238 $modified =~ s/(?<=\b) (?:\d{2}\:?){3} (?=\b)//x;
239 $modified =~ s/\s{2,}/ /g;
240
241 my $info = <<"EOT";
242 --
243 running $script v$VERSION - last modified: $modified
244 EOT
245 return do { local $_ = $info; chomp while /\n$/; $_ };
246 }