4 # Name : generate_reminders
5 # Author : Chris Koeritz
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.
18 # Credits: thanks to 'calendar version 3' from kernighan & pike for good approaches.
20 # Note: the calendar format is like so in calendar.dat:
22 # Apr 24 Saint Shanty's Day
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.
29 require "importenv.pl";
33 # all the date alerts go into this temp file.
34 local($TEMPO_FILE) = `mktemp "$TMP/zz_reminder.XXXXXX"`;
37 local($USERNAME) = "$REPLYTO";
38 if (! $USERNAME) { $USERNAME="fred" }
40 #print "TEMPO is $TEMPO_FILE ; USER is $USERNAME ; \n";
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 = "$QUARTZDIR/database/calendar.dat";
49 #print "calfile is $CAL_FILE\n";
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");
56 # the big block here computes the list of days near today, so we can check the
57 # entries in the file.
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.
62 local(@moon_data) = split(' ', $MONTH_DAYS);
64 #print "got moondays of @moon_data\n";
66 local $i, %days_in_month, %next_month;
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";
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";
80 local(@date_as_array) = split(' ', `date`);
84 # now we construct the nearby dates that we care about...
86 local @checking_dates;
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]));
91 local @pair, @new_pair;
93 @pair = ($checking_dates[0], $checking_dates[1]);
94 #print "got a pair @pair\n";
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.
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.
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.
120 # finally we can test the dates in the file and see if they're getting close enough to report on.
122 #print "got nearby dates to check: @checking_dates\n";
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);
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)\" $USERNAME <$TEMPO_FILE\");\n";
137 system("mail -s \"FredMinder: \$(head -1 $TEMPO_FILE)\" $USERNAME <$TEMPO_FILE");
146 # prints the contents of the first parameter's file out to stdout.
147 sub fix_date_for_rollover {
148 local($month) = @_[0];
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};
155 #print "now mon=$month day=$day\n";
156 return ($month, $day);
159 sub test_date_for_proximity {
160 local($month) = @_[0];
162 local($line) = @_[2];
163 #print "test mon=$month day=$day\n";
165 for ($i = 0; $i < @checking_dates; $i += 2) {
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";