vCalendar fixes
[tpope-extra.git] / perl / schedproc
index ec8d99cc6031b0f6bf2fe09ed66945ab32190e74..81f4a1aec943be197811efe3cfe9675544930d98 100755 (executable)
@@ -82,7 +82,7 @@ undef $opts{'out'} if(($opts{'out'}||"") eq "-");
 sub generate_id {
     my ($section, $number) = split("-", shift);
     my $id = 0;
-    foreach (split //, $section) {$id=26*$id+(ord($_)-1)%32;}
+    foreach my $c (split //, $section) {$id=26*$id+(ord($c)-1)%32;}
     $id=10000*$id+$number;
     return $id;
 }
@@ -96,8 +96,8 @@ sub first_class {
     my $lastday = Date::Calc->new($4,$5,$6);
     my $today;
     my @days=();
-    foreach(split("",$class{'days'})) {
-       push @days, $days{$_};
+    foreach my $d (split("",$class{'days'})) {
+       push @days, $days{$d};
     }
     my @off = ();
     @off = @{$class{'off'}} if ($class{'off'});
@@ -118,8 +118,8 @@ sub next_class {
     my $lastday = Date::Calc->new($4,$5,$6);
     my $today = Date::Calc->new(Date::Calc->localtime(time+3600*6)->date);
     my @days=();
-    foreach(split("",$class{'days'})) {
-       push @days, $days{$_};
+    foreach my $d(split("",$class{'days'})) {
+       push @days, $days{$d};
     }
     my @off = ();
     @off = @{$class{'off'}} if ($class{'off'});
@@ -137,7 +137,7 @@ sub next_class {
     return undef;
 }
 
-sub ical_datetime {
+sub vcal_datetime {
     my $date=shift;
     my $time=shift;
     $date =~ /(\d\d\d\d)(\d\d)(\d\d)/;
@@ -219,9 +219,8 @@ sub load_faculty {
     if(($opts{'faculty'}) && ! %faculty) {
        $faculty{'done'} = "true";
        $content = read_fileurl($opts{'faculty'},1);
-       foreach $_ (split("\n", $content)) {
-           m/"([^"]*)",([^,]*),([^,]*)/; # "
-           ($name, $email, $url) = ($1, $2, $3);
+       foreach my $f (split("\n", $content)) {
+           ($name, $email, $url) = $f =~ m/"([^"]*)",([^,]*),([^,]*)/; # "
            next unless ($name);
            $name =~ s/ [A-Z]\.//g;
            $name = lc $name;
@@ -277,10 +276,11 @@ sub do_mhc_schedule {
     @mhc = get_mhc_header;
     if(defined($file) && (-d $file)) {
        my @mhc2;
+       local $_;
        foreach (@mhc) {
-           $_ =~ s/X-SC-Subject: ([^\n]*)/X-SC-Subject: $1\nSubject: $1/;
-           $_ =~ s/X-SC-Category: ([^\n]*)/X-SC-Category: $1\nFrom: $1/;
-           $_ =~ 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;
+           s/X-SC-Subject: ([^\n]*)/X-SC-Subject: $1\nSubject: $1/;
+           s/X-SC-Category: ([^\n]*)/X-SC-Category: $1\nFrom: $1/;
+           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;
            push @mhc2, $_;
        }
        @mhc = @mhc2;
@@ -293,7 +293,7 @@ sub do_mhc_schedule {
        $row->{'days'} =~ s/^ //;
        #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
        my @day = ();
-       @day = map { "!" . $_ } (@{$row->{'off'}}) if ($row->{'off'});
+       @day = map { "!$_" } (@{$row->{'off'}}) if ($row->{'off'});
        $current = "";
        #print "# $id\n";
        $current .= "X-SC-Subject: " . $row->{'title'} . "\n";
@@ -330,10 +330,10 @@ sub do_mhc_schedule {
            }
        }
        my $i=0;
-       foreach (@mhc) {
+       foreach my $h (@mhc) {
            while(-f ++$i) {}
            open (FH, ">$file/$i") || die $!;
-           print FH $_;
+           print FH $h;
            close FH;
        }
     } else {
@@ -379,18 +379,18 @@ sub do_vcalendar_schedule {
     print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
     foreach my $row (@schedule) {
        map { s/\n/-/g; $_} %$row;
+       my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
+       my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
+       $starttime =~ s/://;
+       $stoptime =~ s/://;
+       my $first = first_class(%$row);
        my @day = ();
        @day = @{$row->{'off'}} if ($row->{'off'});
        my $day = "";
        if(exists($day[0])) {
            $day = join(";", @day);
-           $day =~ s/\b(\d{8})\b/$1T000000/g;
+           $day =~ s/\b(\d{8})\b/$1T${starttime}00/g;
        }
-       my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
-       my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
-       $starttime =~ s/://;
-       $stoptime =~ s/://;
-       my $first = first_class(%$row);
        if(defined($file) && (-d $file)) {
            open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!";
            select FH;
@@ -401,9 +401,10 @@ sub do_vcalendar_schedule {
        print "DESCRIPTION:", $row->{'id'}, "\r\n";
        print "LOCATION:", $row->{'location'}, "\r\n";
        print "CATEGORIES:Education\r\n";
-       #print "DTSTART:", ical_datetime(first_class(%$row),$starttime), "\r\n";
-       print "DTSTART:", $first."T".$starttime, "00\r\n";
-       print "DTEND:", $first."T".$stoptime, "00\r\n";
+       print "DTSTART:", vcal_datetime($first,$starttime), "\r\n";
+       print "DTEND:", vcal_datetime($first,$stoptime), "\r\n";
+       #print "DTSTART:", $first."T".$starttime, "00\r\n";
+       #print "DTEND:", $first."T".$stoptime, "00\r\n";
        $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
        $row->{'days'} =~ s/^ //;
        print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n";
@@ -445,43 +446,50 @@ END:DAYLIGHT$r
 END:VTIMEZONE$r
 EOF
     ;
+    my $now = Date::Calc->now();
+    $now = sprintf ("%02d%02d%02dT%02d%02d%02dZ", $now->datetime());
     my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU");
     my $file = $opts{'out'};
     my @schedule = get_schedule(@_);
     open(STDOUT, ">" . $file) || die $! if(defined($file) && (! -d $file));
     open(STDOUT, ">/dev/null") || die $! if(defined($file) && (-d $file));
-    print "BEGIN:VCALENDAR$r\nVERSION:2.0$r\n$timezone";
+    print "BEGIN:VCALENDAR$r\nPRODID:-//Tim Pope//NONSGML Schedproc//EN$r\nVERSION:2.0$r\nMETHOD:PUBLISH$r\n$timezone";
     foreach my $row (@schedule) {
        map { s/\n/-/g; $_} %$row;
-       my @day = ();
-       @day = @{$row->{'off'}} if ($row->{'off'});
        my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
        my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
        $starttime =~ s/://;
        $stoptime =~ s/://;
        my $first = first_class(%$row);
+       my @day = ();
+       @day = @{$row->{'off'}} if ($row->{'off'});
+       my $day = "";
+       if(exists($day[0])) {
+           $day = join(",", @day);
+           #$day =~ s/\b(\d{8})\b/$1T${starttime}00/g;
+       }
        if(defined($file) && (-d $file)) {
            open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!";
            select FH;
-           print "BEGIN:VCALENDAR$r\nVERSION:2.0$r\n$timezone";
+           print "BEGIN:VCALENDAR$r\nPRODID:-//Tim Pope//NONSGML Schedproc//EN$r\nVERSION:2.0$r\nMETHOD:PUBLISH$r\n$timezone";
        }
        print "BEGIN:VEVENT$r\n";
+       print "ORGANIZER:mailto:" . $opts{'email'} . "$r\n" if $opts{'email'};
+       print "UID:" . $row->{'id'} . "\@from.sctweb$r\n";
+       print "DTSTAMP:$now$r\n";
        print "SUMMARY:", $row->{'title'}, "$r\n";
        print "DESCRIPTION:", $row->{'id'}, "$r\n";
        print "LOCATION:", $row->{'location'}, "$r\n";
        print "CATEGORIES:Education$r\n";
        print "TRANSP:OPAQUE$r\n";
-       #print "DTSTART;$tzn$first,$starttime), "$r\n";
-       #print "DTEND;$tzn$first,$stoptime), "$r\n";
        print "DTSTART;TZID=\"$tzn\":", $first."T".$starttime, "00$r\n";
        print "DTEND;TZID=\"$tzn\":", $first."T".$stoptime, "00$r\n";
        $row->{'days'} =~ s/([MTWRFS])/,$days{$1}/g;
        $row->{'days'} =~ s/^,//;
-       $stopdate++;
+       #$stopdate++;
        print "RRULE:FREQ=WEEKLY;UNTIL=${stopdate}T000000Z;BYDAY=", $row->{'days'}, "$r\n";
-       foreach my $day (@day) {
-           print("EXDATE;TZID=\"$tzn\":${day}T$starttime$r\n");
-       }
+       #print("EXDATE;TZID=\"$tzn\":$day$r\n") if($day);
+       print("EXDATE;VALUE=DATE:$day$r\n") if($day);
        print "ATTENDEE;CN=".$row->{'instructor'}.";RSVP=FALSE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), "$r\n";
        print "ATTENDEE;CN=".$opts{'name'}.";RSVP=FALSE;PARTSTAT=ACCEPTED;ROLE=REQ-PARTICIPANT:mailto:" . ($opts{'email'} || "fake\@ddress"), "$r\n" if $opts{'name'};
        print "END:VEVENT$r\n";
@@ -543,7 +551,7 @@ sub do_html_schedule {
            $b=~s/(1[3-9]|2\d|00):(\d\d)AM/sprintf "%d:%02dPM",abs $1-12,$2/e;
            print "<td>$a-<wbr />$b</td>";
            print "<td>" . $row->{'duration'} . "</td>";
-           print "<td>" . $row->{'location'} . "</td>";
+           print "<td>" . (ref($row->{'location'})?"":$row->{'location'}) . "</td>";
            print "</tr>\n";
        }
     print "</table>\n";