]> pere.pagekite.me Git - homepage.git/blob - linux/plan2icalendar
Add debian dependency info.
[homepage.git] / linux / plan2icalendar
1 #!/usr/bin/perl -w
2 #
3 # Author: Petter Reinholdtsen <pere@hungry.com>
4 # Date: 2002-01-10
5 #
6 # Convert plan calender data to/from icalender format.
7 #
8 # plan, <URL:http://www.bitrot.de/>
9 # icalendar, RFC 2445, <URL:http://www.faqs.org/rfcs/rfc2445.html">iCalendar>
10 #
11 # Depend on debian package libdate-ical-perl
12
13 use warnings;
14 use strict;
15
16 use Getopt::Std;
17 use Date::Parse;
18 use Date::Format;
19 use Date::ICal;
20
21 my @events;
22 my %opts;
23 my $debug = 0;
24 unless (getopts('if:o:', \%opts)) {
25 usage();
26 exit 1;
27 }
28
29 my $input = $opts{f};
30 my $output = $opts{o};
31
32 if ($opts{i}) { # Import
33 read_icalendar($input || "test.ical");
34 write_planfile($output || $ENV{HOME}."/.plan.dir/fromical");
35 } else { # Default is to export
36 read_planfile($input || $ENV{HOME}."/.plan.dir/dayplan");
37 write_icalendar($output || "-");
38 }
39 exit 0;
40
41 sub usage {
42 print <<EOF
43 Usage: $0 [-i] [-f infile] [-o outfile]
44
45 Imports or exports between plan and iCalendar.
46
47 -i import ical file
48 -f infile read input from 'infile'
49 -o outfile read output from 'outfile'
50 EOF
51 }
52 sub read_planfile {
53 my ($planfile) = @_;
54 open(PLAN, "<$planfile") || die "Unable to read $planfile";
55 my $planver = <PLAN>;
56 chomp $planver;
57 while (<PLAN>) {
58 chomp;
59 if (m%\d+/\d+/\d+\s%) {
60 my ($date, $start, $duration) = split(/\s+/);
61 my $info = <PLAN>;
62 chomp $info;
63 my ($dontknow, $msg) = $info =~ m/^(.)\t(.+)$/;
64 print STDERR "E: $date $start $duration $msg\n" if $debug;
65
66 my $timestamp = str2time("$date $start");
67 my $isostartstamp = time2str("%Y%m%dT%H%M%S", $timestamp);
68 my $icalstartstamp = Date::ICal->new( epoch => $timestamp );
69
70 my @f = split(/:/, $duration);
71 $timestamp += ($f[0]*60*60 + $f[1]*60 +$f[2]);
72 my $isoendstamp = time2str("%Y%m%dT%H%M%S", $timestamp);
73 my $icalendstamp = Date::ICal->new( epoch => $timestamp );
74
75 print STDERR "IE: $isostartstamp $isoendstamp\n" if $debug;
76 push(@events, {
77 summary => $msg,
78 dtstart => $icalstartstamp->ical,
79 dtend => $icalendstamp->ical,
80 });
81 }
82 }
83 close(PLAN);
84 }
85
86
87 # BEGIN:VCALENDAR
88 # PRODID
89 # :-//test environment//NONSGML plan2icalendar//EN
90 # VERSION
91 # :2.0
92 # BEGIN:VEVENT
93 # CREATED
94 # :20020110T153939
95 # UID
96 # :KOrganizer-141229514.660
97 # SEQUENCE
98 # :0
99 # LAST-MODIFIED
100 # :20020110T153939
101 # DTSTAMP
102 # :20020110T153952
103 # SUMMARY
104 # :SL-veiledning
105 # DTSTART
106 # :20020114T151500
107 # DTEND
108 # :20020114T160000
109 # END:VEVENT
110 # END:VCALENDAR
111 sub write_icalendar {
112 my $filename = shift;
113 open(PLAN, ">$filename") || die "Unable to write to $filename";
114 print PLAN <<EOF;
115 BEGIN:VCALENDAR
116 CALSCALE:GREGORIAN
117 PRODID:-//test environment//NONSGML plan2icalender//EN
118 VERSION:2.0
119 EOF
120 my $count = 0;
121 for my $event (@events) {
122 $event->{uid} = "plan2icalendar-$count" unless $event->{uid};
123 $event->{dtstamp} = "20020110T153952" unless $event->{dtstamp};
124 $event->{'last-modified'} = "20020110T153939" unless $event->{'last-modified'};
125 $event->{sequence} = $count unless $event->{sequence};
126 $event->{created} = "20020110T153939" unless $event->{created};
127 print PLAN <<EOF;
128 BEGIN:VEVENT
129 CREATED:$event->{created}
130 UID:$event->{uid}
131 SEQUENCE:$count
132 LAST-MODIFIED:$event->{'last-modified'}
133 DTSTAMP:$event->{dtstamp}
134 SUMMARY:$event->{summary}
135 DTSTART:$event->{dtstart}
136 DTEND:$event->{dtend}
137 END:VEVENT
138
139 EOF
140 $count++;
141 }
142 print PLAN "END:VCALENDAR\n";
143 close PLAN;
144 }
145
146 #
147 # Should probably leave this task to Net::ICal. But it is not
148 # available in debian/sarge, so I make a quick-n-dirty solution
149 # instead.
150 #
151 sub read_icalendar {
152 my $filename = shift;
153 open (ICALENDAR, "<$filename") or die "Unable to read from $filename";
154 my $oldval = $/;
155 $/ = "\r\n";
156 while (<ICALENDAR>) {
157 chomp;
158 if (m/^BEGIN:VEVENT/) {
159 my %event;
160 while (<ICALENDAR>) {
161 chomp;
162 last if (m/END:VEVENT/);
163 $event{description} = $1 if (m/^DESCRIPTION\s*:\s*(.+)$/);
164 $event{created} = $1 if (m/^CREATED\s*:\s*(.+)$/);
165 $event{dtend} = $1 if (m/^DTEND\s*:\s*(.+)$/);
166 $event{dtstamp} = $1 if (m/^DTSTAMP\s*:\s*(.+)$/);
167 $event{dtstart} = $1 if (m/^DTSTART\s*:\s*(.+)$/);
168 $event{last-modified} = $1 if (m/^LAST-MODIFIED\s*:\s*(.+)$/);
169 $event{sequence} = $1 if (m/^SEQUENCE\s*:\s*(.+)$/);
170 $event{summary} = $1 if (m/^SUMMARY\s*:\s*(.+)$/);
171 $event{uid} = $1 if (m/^UID\s*:\s*(.+)$/);
172 }
173 push @events, \%event;
174 }
175 }
176 close (ICALENDAR);
177 $/ = $oldval;
178 }
179
180 # Simple version covering the chars I need.
181 sub utf8tolocalmap {
182 my $string = shift;
183 $string =~ s/ø/ø/g;
184 $string =~ s/æ/æ/g;
185 $string =~ s/Ã¥/å/g;
186 return $string;
187 }
188
189 sub write_planfile {
190 my $filename = shift;
191 open(PLAN, ">$filename") or die "Unable to write to $filename";
192 for my $event (@events) {
193 # offset = 0 -- assume all timestamps in local time zone
194 my $ical = Date::ICal->new( ical => $event->{dtstart}, offset => 0 );
195 my $date = join("/", $ical->month, $ical->day, $ical->year);
196 my $start = join(":", $ical->hour, $ical->min, $ical->sec);
197
198 my $icalstop = Date::ICal->new( ical => $event->{dtend}, offset => 0 );
199 my $icalduration = $icalstop - $ical;
200 my $duration = join(":",
201 $icalduration->hours || "0",
202 $icalduration->minutes || "0",
203 $icalduration->seconds || "0");
204
205 my $code = "N";
206 my $summary = utf8tolocalmap($event->{summary});
207 print PLAN "$date $start $duration 0:0:0 0:0:0 ---------- 0 0\n";
208 print PLAN "$code\t$summary\n";
209 }
210 }