3 # -*- perl -*- vim: ft=perl sw=4 sts=4
5 # Brief usage instructions:
6 # Create a ~/.sct6rc that has SID=yourssn and PIN=yourpin
7 # You'll need to change the url below if you go anywhere but TAMUK.
10 use Date::Calc::Object qw(Day_of_Week Decode_Day_of_Week Decode_Month Week_of_Year Monday_of_Week Day_of_Week_Abbreviation Delta_Days Add_Delta_Days Nth_Weekday_of_Month_Year Gmtime Mktime);
11 use Date::Calendar::Profiles qw($Profiles);
12 use Date::Calendar::Year;
15 use vars qw(%opts %faculty);
19 $opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
20 "Martin Luther King Day" => "3/Mon/Jan",
21 "Good Friday" => "-2",
22 "Spring Break Monday" => \&Spring_Break,
23 "Spring Break Tuesday" => \&Spring_Break,
24 "Spring Break Wednesday" => \&Spring_Break,
25 "Spring Break Thursday" => \&Spring_Break,
26 "Spring Break Friday" => \&Spring_Break,
27 "Spring Break Saturday" => \&Spring_Break,
28 "Study Day" => "4/Thu/Apr", # ?
29 "Memorial Day" => "5/Mon/May",
30 "Independence Day" => \&US_Independence,
31 "Labor Day" => \&US_Labor,
32 #"Columbus Day" => "2/Mon/Oct",
33 "Thanksgiving Day" => "4/Thu/Nov",
34 "Thanksgiving Friday" => \&Thanksgiving_Friday,
38 my($year,$label) = @_;
39 $label =~ s/^Spring Break //;
40 return( Add_Delta_Days(
41 Nth_Weekday_of_Month_Year($year,1,1,1),
42 7*(11-1) # This is for the 11th Monday of the year
43 +Decode_Day_of_Week($label)-1) );
46 sub Thanksgiving_Friday {
47 my($year,$label) = @_;
48 return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
50 sub US_Independence # Fourth of July
52 my($year,$label) = @_;
53 return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
55 sub US_Labor # First Monday after the first Sunday in September
57 my($year,$label) = @_;
58 return( Add_Delta_Days(
59 Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
62 my $config = $ENV{HOME} . "/.schedprocrc";
63 if (($ARGV[0] || "") eq '-F') {
69 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
75 next unless m/^([^=]*)=(.*)/;
80 my $schedurl = $ENV{HOME} . "/schedule.xml";
81 $schedurl = $opts{'schedule'} if(defined($opts{'schedule'}));
82 my $gradeurl = $ENV{HOME} . "/grades.xml";
83 $gradeurl = $opts{'grades'} if(defined($opts{'schedule'}));
86 my ($section, $number) = split("-", shift);
88 foreach (split //, $section) {$id=26*$id+(ord($_)-1)%32;}
89 $id=10000*$id+$number;
95 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
96 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
97 my $firstday = Date::Calc->new($1,$2,$3);
98 my $lastday = Date::Calc->new($4,$5,$6);
99 my $today = Date::Calc->new(Date::Calc->localtime(time+3600*6)->date);
101 foreach(split(" ",$class{'days'})) {
102 push @days, Decode_Day_of_Week($_);
105 @off = @{$class{'off'}} if ($class{'off'});
106 for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
107 next unless(grep($_ == Day_of_Week($today->date), @days));
109 next if(grep($_ == "$today", @off));
112 $today = Date::Calc->new(Date::Calc->gmtime->date);
113 for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
114 next unless(grep($_ == Day_of_Week($today->date), @days));
115 next if(grep($_ == "$today", @off));
121 sub off_for_holidays {
123 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
124 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
125 my $firstday = Date::Calc->new($1,$2,$3);
126 my $lastday = Date::Calc->new($4,$5,$6);
127 my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
130 foreach(split(" ",$class{days})) {
131 $days[Decode_Day_of_Week($_)] = 1;
133 foreach ($year->search("")) {
135 #foreach my $x ($year->labels($_)) {
136 # $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
138 next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
140 if (defined($days[Day_of_Week($_->date)]) && $good > 0);
142 return wantarray ? @holidays : "@holidays";
147 my $time = $class{'begin'} . "-" . $class{'end'};
148 $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
149 my $lastday = Date::Calc->new($1,$2,$3);
150 return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
151 my $beginning="(08:00-09:15|11:00-12:15|14:00-15:15|17:00-18:15|17:30-18:45|20:00-21:15|20:30-21:45)";
152 my $ending ="(06:30-07:45|09:30-10:45|12:30-13:45|15:30-16:45|18:30-19:45|19:00-20:15)";
153 my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
155 if($time =~ /$beginning/) {
157 } elsif($time =~ /$ending/) {
160 } elsif($days eq "2 4") {
161 if($time =~ /$beginning/) {
163 } elsif($time =~ /$ending/) {
166 } elsif($days eq "5") {
169 return wantarray ? () : "";
173 local $_ = shift || "";
175 s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
176 s/\b(I)(i*)\b/$1\U$2/g;
178 s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
179 s/\b(Mc)([a-z])/$1\u$2/g;
180 s/\b(Tcp\/Ip|Pc|Tba)\b/\U$&/g;
181 s/\bThru\b/Through/g;
182 s/\bAcct\b/Accounting/g;
183 s/\bAmer\b/American/g;
184 s/\bChem\b/Chemistry/g;
185 s/\bComp\b/Composition/g;
188 s/\bIntro\b/Introduction/g;
189 s/\bPrgm\b/Programming/g;
190 s/\bOp Sys\b/Operating System/g;
191 #s/\bGovt\b/Government/g;
192 s/\bLit\b/Literature/g;
193 s/\bPrin\b/Principles/g;
194 s/\bBus\b/Business/g;
201 if($schedurl =~ /:\/\//) {
202 my $ua = LWP::UserAgent->new;
205 # $ua->cookie_jar( {} );
206 $response = $ua->get("$schedurl") or die "$!";
207 die $response->status_line unless $response->is_success;
208 $content = $response->content;
210 open(F,$schedurl) || die "$!";
211 $content = join ("", <F>);
214 my $ref = XMLin($content, ForceArray => [ 'class', 'off' ], KeyAttr => "");
215 return @{$ref->{'class'}};
220 if($gradeurl =~ /:\/\//) {
221 my $ua = LWP::UserAgent->new;
224 # $ua->cookie_jar( {} );
225 $response = $ua->get("$gradeurl") or die "$!";
226 die $response->status_line unless $response->is_success;
227 $content = $response->content;
229 open(F,$schedurl) || die "$!";
230 $content = join ("", <F>);
233 my $ref = XMLin($content, ForceArray => [ 'class', 'cumulative' ], KeyAttr => "");
239 my @showheaders = ("Section ID", "Course Title", "Grade", "Earned<br />Hours", "Quality<br />Hours", "Quality<br />Points");
240 #$file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
241 $grades = get_grades(@_);
244 print '<table id="grades" cellpadding="3" cellspacing="0">'."\n<tr>";
245 print '<th align="left" class="sectionid">', $showheaders[0];
246 print '</th><th align="left" class="coursetitle">', $showheaders[1];
247 print '</th><th align="center" class="grade">';
248 print join('</th><th align="center" class="right-number">',@showheaders[2 .. 5]);
249 print "</th></tr>\n";
250 foreach my $ts ($grades->{'class'}) {
251 foreach my $row (@$ts) {
252 print '<tr class="'.$shade.'">';
253 $shade = ($shade eq "dark"?"light":"dark");
254 print '<td align="left" class="sectionid">', $row->{'id'}, '</td>';
255 print '<td align="left" class="coursetitle">', $row->{'title'}, '</td>';
256 print '<td align="center" class="grade">';
257 #print join('</td><td>', @$row[0 .. 1]);
258 print join('</td><td align="right" class="right-number">', ($row->{'grade'},$row->{'earned'},$row->{'hours'},$row->{'points'}));
259 print "</td></tr>\n";
262 my $lastrow = $grades->{'cumulative'}->[scalar @{$grades->{'cumulative'}}-1];
263 print '<tr class="cumulative '.$shade.'"><td id="cumulative" colspan="2">Cumulative: through ', capitalize($lastrow->{'term'});
264 print '</td><td align="center" id="gpa" class="grade">';
265 print $lastrow->{'gpa'};
266 print '</td><td align="right" class="right-number">';
267 print join('</td><td align="right" class="right-number">', ($lastrow->{'earned'}, $lastrow->{'hours'}, $lastrow->{'points'}));
268 print "</td></tr>\n";
272 sub get_faculty_email {
273 my ($name, $school, $email);
274 if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) {
275 open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
278 m/"([^"]*)",([^,]*),([^,]*)/; # "
279 ($name, $email, $school) = ($1, $2, $3);
280 $name =~ s/ [A-Z]\.//g;
283 $faculty{$name} = $email;
287 $name =~ s/ [A-Z]r?\.//g;
290 return $faculty{$name};
295 "X-SC-Subject: New Years Day\nX-SC-Category: Holiday\nX-SC-Cond: 1 Jan\nX-SC-Duration: 00010101-\nX-SC-Record-Id: <New_Years_Day\@from.sctweb>\n",
296 "X-SC-Subject: Martin Luther King, Jr. Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Jan\nX-SC-Duration: 19870119-\nX-SC-Record-Id: <Martin_Luther_King_Jr_Day\@from.sctweb>\n",
297 "X-SC-Subject: Presidents Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Feb\nX-SC-Duration: 19710515-\nX-SC-Record-Id: <Presidents_Day\@from.sctweb>\n",
298 "X-SC-Subject: Memorial Day\nX-SC-Category: Holiday\nX-SC-Cond: Last Mon May\nX-SC-Duration: 19710531-\nX-SC-Record-Id: <Memorial_Day\@from.sctweb>\n",
299 "X-SC-Subject: Independence Day\nX-SC-Category: Holiday\nX-SC-Cond: 4 Jul\nX-SC-Duration: 17760704-\nX-SC-Record-Id: <Independence_Day\@from.sctweb>\n",
300 "X-SC-Subject: Labor Day\nX-SC-Category: Holiday\nX-SC-Cond: 1st Mon Sep\nX-SC-Duration: 18840901-\nX-SC-Record-Id: <Labor_Day\@from.sctweb>\n",
301 "X-SC-Subject: Columbus Day\nX-SC-Category: Holiday\nX-SC-Cond: 2nd Mon Oct\nX-SC-Duration: 19711011-\nX-SC-Record-Id: <Columbus_Day\@from.sctweb>\n",
302 "X-SC-Subject: Veterans Day\nX-SC-Category: Holiday\nX-SC-Cond: 11 Nov\nX-SC-Duration: 19261111-\nX-SC-Record-Id: <Veterans_Day\@from.sctweb>\n",
303 "X-SC-Subject: Thanksgiving\nX-SC-Category: Holiday\nX-SC-Cond: 4th Thu Nov\nX-SC-Duration: 14921122-\nX-SC-Record-Id: <Thanksgiving\@from.sctweb>\n",
304 "X-SC-Subject: Christmas\nX-SC-Category: Holiday\nX-SC-Cond: 25 Dec\nX-SC-Duration: 00011225-\nX-SC-Record-Id: <Christmas\@from.sctweb>\n",
308 sub do_mhc_schedule {
310 my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
311 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
312 my ($file, $current, @mhc, @schedule);
313 $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
314 @schedule = get_schedule(@_);
315 @mhc = get_mhc_header;
316 if(defined($file) && (-d $file)) {
319 $_ =~ s/X-SC-Subject: ([^\n]*)/X-SC-Subject: $1\nSubject: $1/;
320 $_ =~ s/X-SC-Category: ([^\n]*)/X-SC-Category: $1\nFrom: $1/;
321 $_ =~ s/X-SC-Duration: (\d\d\d\d)(\d\d)(\d\d)-/"X-SC-Duration: $1$2$3-\nDate: $3 " . $mon[$2-1] . " 1970 12:00:00 +0000"/e;
326 foreach my $row (@schedule) {
327 map {s/\n/-/g if defined; $_} %$row;
328 my $id=generate_id($row->{'id'});
329 $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
330 $row->{'days'} =~ s/^ //;
331 #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
333 @day = map { "!" . $_ } (@{$row->{'off'}}) if ($row->{'off'});
336 $current .= "X-SC-Subject: " . $row->{'title'} . "\n";
337 $current .= "X-SC-Location: " . $row->{'location'} . "\n";
338 $current .= "X-SC-Category: School\n";
339 $current .= "X-SC-Cond: " . $row->{'days'} . "\n";
340 $current .= "X-SC-Time: ".$row->{'begin'}."-".$row->{'end'}."\n";
341 $current .= "X-SC-Duration: " . $row->{'duration'} . "\n";
342 $current .= "X-SC-Day: @day\n" if(exists $day[0]);
343 $current .= "X-SC-Alarm: 15 minutes\n";
344 $current .= "X-SC-Record-Id: <".$row->{'id'}."\@from.sctweb>\n";
345 if(defined($file) && (-d $file)) {
346 my $email = get_faculty_email $row->{'instructor'};
347 $row->{'instructor'} = '"' . $row->{'instructor'} . '" <'. ($email || ($1 || "unknown") . "\@from.sctweb") . ">";
348 $row->{'duration'} =~ /^(\d\d\d\d)(\d\d)(\d\d)-\d{8}$/;
349 $row->{'begin'} =~ /^(\d\d):(\d\d)$/;
350 my $next = next_class(%$row);
351 my @date = Gmtime(Mktime($next->date,$1,$2,0));
352 $current .= sprintf "Date: %s, %2d %s %4d %02d:%02d:00 +0000\n", Day_of_Week_Abbreviation($date[7]), $date[2], $mon[$date[1]-1], $date[0], $date[3], $date[4], $date[5];
353 $current .= "Subject: " . $row->{'title'} . "\n";
354 $current .= "From: " . $row->{'instructor'} . "\n";
359 if(defined($file) && (-d $file)) {
361 foreach my $name (<$file/[1-9]*>) {
362 next unless $name =~ /^$file\/[1-9][0-9]*$/;
367 unlink $name if /^X-SC-Record-Id: <.*\@from.sctweb>/;
373 open FH, ">$file/$i" or die $!;
378 open(STDOUT, ">" . $file) if(defined($file));
379 print "# MHC school schedule\n# Autogenerated by sctweb ".localtime()."\n\n";
380 print join("\n", @mhc);
384 sub do_csv_schedule {
386 my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
387 my @mon = qw(Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.);
388 my (@terms, $file, $current, @mhc, @schedule);
389 $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
390 @schedule = get_schedule(@_);
391 open(STDOUT, ">" . $file) if(defined($file));
392 foreach my $row (@schedule) {
393 map {s/\n/-/g if defined; $_} %$row;
394 my $id=generate_id($row->{'id'});
395 $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
396 $row->{'days'} =~ s/^ //;
397 my $next = next_class(%$row);
400 $current .= $row->{'id'} . ",";
401 $current .= $row->{'title'} . ",";
402 $current .= '"' . $row->{'instructor'} . '",';
403 $next =~ /^(\d\d\d\d)(\d\d)(\d\d)$/;
404 $current .= $1 . "-". $2 ."-" . $3 . "\n";
409 sub do_vcalendar_schedule {
411 my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU");
412 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
413 my @schedule = get_schedule(@_);
414 open(STDOUT, ">>" . $file) if(defined($file) && (! -d $file));
415 open(STDOUT, ">/dev/null") if(defined($file) && (-d $file));
416 print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
417 foreach my $row (@schedule) {
418 map { s/\n/-/g; $_} %$row;
419 $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
420 $row->{'days'} =~ s/^ //;
422 @day = @{$row->{'off'}} if ($row->{'off'});
424 if(exists($day[0])) {
425 $day = join(";", @day);
426 $day =~ s/\b(\d{8})\b/$1T000000/g;
428 my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
429 my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
432 if(defined($file) && (-d $file)) {
433 open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!";
435 print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
437 print "BEGIN:VEVENT\r\n";
438 print "SUMMARY:", $row->{'title'}, "\r\n";
439 print "DESCRIPTION:", $row->{'id'}, "\r\n";
440 print "LOCATION:", $row->{'location'}, "\r\n";
441 print "CATEGORIES:Education\r\n";
442 print "DTSTART:", $startdate."T".$starttime, "00\r\n";
443 print "DTEND:", $startdate."T".$stoptime, "00\r\n";
444 print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n";
445 print("EXDATE:$day\r\n") if($day);
446 print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{'name'}, "\r\n" if(defined($opts{'name'}));
447 print "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, " <" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), ">\r\n";
448 print "END:VEVENT\r\n";
449 if(defined($file) && (-d $file)) {
450 print "END:VCALENDAR\r\n";
455 print "END:VCALENDAR\r\n";
458 sub do_xml_schedule {
459 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
460 my $schedule = { class => [ get_schedule(@_) ] };
461 my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
471 sub do_html_schedule {
472 my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Duration", "Location");
474 my @schedule = get_schedule(@_);
475 print '<table id="schedule" cellpadding="3" cellspacing="0">'."\n<tr><th>";
476 print join("</th><th>",@showheaders);
477 print "</th></tr>\n";
478 foreach my $row (@schedule) {
479 map { s/\n/<br \/>/g; $_} %$row;
480 #$row->{'time'} =~ s/-/-<wbr \/>/;
481 $row->{'duration'} =~ s/-/<br \/>/g;
482 $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g;
483 print '<tr class="'.$shade.'">';
484 $shade = ($shade eq "dark"?"light":"dark");
485 print '<td class="idtitle">';
486 print '<span class="sectionid">' .$row->{'id'}. '</span><br />';
487 print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
488 my $instructor = $row->{'instructor'};
489 my $email = get_faculty_email($row->{'instructor'});
491 #print '<td><a href="mailto:' . $email . '">'
492 #. $row->{'instructor'} . "</a></td>";
494 print "<td>" . $row->{'instructor'} . "</td>";
496 print "<td>" . $row->{'days'} . "</td>";
497 print "<td>" . $row->{'begin'}."-<wbr />".$row->{'end'} . "</td>";
498 print "<td>" . $row->{'duration'} . "</td>";
499 print "<td>" . $row->{'location'} . "</td>";
505 if ($arg eq "-s" || $arg eq "-h") {
506 do_html_schedule(@ARGV);
507 } elsif ($arg eq "-m") {
508 do_mhc_schedule(@ARGV);
509 } elsif ($arg eq "-c") {
510 do_csv_schedule(@ARGV);
511 } elsif ($arg eq "-v") {
512 do_vcalendar_schedule(@ARGV);
513 } elsif ($arg eq "-x") {
514 do_xml_schedule(@ARGV);
515 } elsif ($arg eq "-g") {
516 do_html_grades(@ARGV);