validated calling out to logname
[feisty_meow.git] / scripts / schedule / generate_reminders.pl
1 #!/usr/bin/perl
2
3 ##############
4 #  Name   : generate_reminders
5 #  Author : Chris Koeritz
6 ##############
7 # Copyright (c) 1989-$now By Author.  This script is free software; you can
8 # redistribute it and/or modify it under the terms of the GNU General Public
9 # License as published by the Free Software Foundation:
10 #     http://www.gnu.org/licenses/gpl.html
11 # or under the terms of the GNU Library license:
12 #     http://www.gnu.org/licenses/lgpl.html
13 # at your preference.  Those licenses describe your legal rights to this
14 # software, and no other rights or warranties apply.
15 # Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
16 ##############
17
18 # Credits: thanks to 'calendar version 3' from kernighan & pike for good approaches.
19
20 # Note: the calendar format is like so in calendar.dat:
21 #
22 # Apr 24 Saint Shanty's Day
23 # Jul 8 Normality Day
24 #
25 # Month names must currently be three letters and must be capitalized.
26 # The day must be a number, followed by a space.  Anything after that
27 # space is considered the description of the event.
28
29 use Env qw(CLOUD_BASE TMP);
30
31 ##############
32
33 # all the date alerts go into this temp file.
34 local($TEMPO_FILE) = `mktemp "$TMP/zz_reminder.XXXXXX"`;
35 chop($TEMPO_FILE);
36
37 local($USER_NAME) = "$REPLYTO";
38 if (! $USER_NAME) { $USER_NAME="fred" }
39
40 #print "TEMPO is $TEMPO_FILE ; USER is $USER_NAME ; \n";
41
42 local($CAL_FILE);
43 if (! $CAL_FILE) {
44 #  print "the CAL_FILE variable is not set.\n";
45 #  print "defaulting it to a value that probably does not suit you.\n";
46   $CAL_FILE = "$CLOUD_BASE/magic_cabinet/lists/calendar.dat";
47 }
48
49 #print "calfile is $CAL_FILE\n";
50
51 # we open this really early on, to check if it exists.  we use it way down below.
52 open(DATES, "<$CAL_FILE") || die("failed to open $CAL_FILE");
53
54 ##############
55
56 # the big block here computes the list of days near today, so we can check the
57 # entries in the file.
58
59 local($MONTH_DAYS) = "Jan 31 Feb 28 Mar 31 Apr 30 May 31 Jun 30 Jul 31 Aug 31 Sep 30 Oct 31 Nov 30 Dec 31 Jan 31";
60   # we repeat january so we can wrap around into the future year.
61
62 local(@moon_data) = split(' ', $MONTH_DAYS);
63
64 #print "got moondays of @moon_data\n";
65
66 local $i, %days_in_month, %next_month;
67
68 for ($i = 0; $i < 24; $i += 2) {
69   $days_in_month{@moon_data[$i]} = $moon_data[$i + 1];
70 #print "new day=$moon_data[$i + 1]\n";
71   $next_month{@moon_data[$i]} = $moon_data[$i + 2];
72 #print "new mon=$moon_data[$i + 2]\n";
73 }
74
75 #local(@days) = %days_in_month;
76 #local(@next_month) = %next_month;
77 #print "got days wik @days\n";
78 #print "got next mon wik @next_month\n";
79
80 local(@date_as_array) = split(' ', `date`);
81
82 ##############
83
84 # now we construct the nearby dates that we care about...
85
86 local @checking_dates;
87
88 # push the pair of month and date into our list of dates to be checked.
89 push(@checking_dates, ($date_as_array[1], $date_as_array[2]));
90
91 local @pair, @new_pair;
92
93 @pair = ($checking_dates[0], $checking_dates[1]);
94 #print "got a pair @pair\n";
95
96 # add one day at a time to make sure we report if we're close enough to an event that happens
97 # tomorrow or a couple days from now, etc.
98 for ($i = 1; $i <= 7; $i++) {
99   $pair[1] = $pair[1] + 1;  # increment the day entry by one.
100   @new_pair = &fix_date_for_rollover(@pair);  # fix for rollovers.
101   push(@checking_dates, ($new_pair[0], $new_pair[1]));  # add to the checking list.
102 }
103
104 # add a few days to jump into the future to check every other day for an impending event.
105 for ($i = 1; $i <= 4; $i++) {
106   $pair[1] = $pair[1] + 2;  # increment the day entry by two days.
107   @new_pair = &fix_date_for_rollover(@pair);  # fix for rollovers.
108   push(@checking_dates, ($new_pair[0], $new_pair[1]));  # add to the checking list.
109 }
110
111 # now look a couple weeks forward also (for a total of about 3 weeks total coverage).
112 for ($i = 0; $i < 2; $i++) {
113   $pair[1] = $pair[1] + 7;  # increment the day entry by a week.
114   @new_pair = &fix_date_for_rollover(@pair);  # fix for rollovers.
115   push(@checking_dates, ($new_pair[0], $new_pair[1]));  # add to the checking list.
116 }
117
118 ##############
119
120 # finally we can test the dates in the file and see if they're getting close enough to report on.
121
122 #print "got nearby dates to check: @checking_dates\n";
123
124 while (<DATES>) {
125   local($line) = $_;
126   chop($line);
127   if (length($line) <= 0) { next; }
128 #print "line to check: '$line'\n";
129   local(@test_date) = split(' ', $line);
130   &test_date_for_proximity($test_date[0], $test_date[1], $line);
131 }
132
133 # send mail here if there's anything to say.
134 if (! -z $TEMPO_FILE) {
135   # there are some alerts in there.
136 #print "will run: system(\"mail -s \"FredMinder: \$(head -1 $TEMPO_FILE)\" $USER_NAME <$TEMPO_FILE\");\n";
137   system("mail -s \"FredMinder: \$(head -1 $TEMPO_FILE)\" $USER_NAME <$TEMPO_FILE");
138 }
139
140 unlink $TEMPO_FILE;
141
142 exit 0;
143
144 ##############
145
146 # prints the contents of the first parameter's file out to stdout.
147 sub fix_date_for_rollover {
148   local($month) = @_[0];
149   local($day) = @_[1];
150 #print "fix mon=$month day=$day\n";
151   if ($day > $days_in_month{$month}) {
152     $day = $day % $days_in_month{$month};
153     $month = $next_month{$month};
154   }
155 #print "now mon=$month day=$day\n";
156   return ($month, $day);
157 }
158
159 sub test_date_for_proximity {
160   local($month) = @_[0];
161   local($day) = @_[1];
162   local($line) = @_[2];
163 #print "test mon=$month day=$day\n";
164
165   for ($i = 0; $i < @checking_dates; $i += 2) {
166 #    print "i is $i\n";
167     local($checkmo) = lc $checking_dates[$i];
168     local($checkda) = $checking_dates[$i + 1];
169 #print "checking mon=$checkmo day=$checkda\n";
170     if ( (lc $month eq $checkmo) && ($day eq $checkda) ) {
171       # found a day that's close enough to match; send an alert about it.
172 #print "matching found!  mon=$month day=$day\n";
173       open(OUTY, ">>$TEMPO_FILE") || die("failed to open $TEMPO_FILE");
174       print OUTY $line . "\n";
175       close(OUTY);
176     }
177   }
178 }
179