blib/lib/WWW/WCAP.pm

filter: Statementstmt 42.2% 57 / 135 Branchbran 2.0% 1 / 48 Conditioncond 3.3% 1 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 88.2% 15 / 17 Podpod 100% 4 / 4 totaltotal 31.3% 78 / 249 CC 36 SCAR 65.7
CC
36
Cov
28%
parse_ical
1122.0
do_request
9.1
logout
2.0
CRAP 713.0
↑ Summary t/base/wcap.t »
1 # WWW::WCAP
2 # Access to a WCAP-enabled calendar server
3 #
4 # $Id: WCAP.pm,v 0.1 2003/08/04 04:08:46 nate Exp $
5  
6 =head1 NAME
7  
8 WWW::WCAP - Access to a WCAP-enabled calendar server
9  
10 =head1 SYNOPSIS
11  
12 # Should be OOP-based, not procedural...
13 use WWW::WCAP qw(login do_request parse_ical logout);
14 my $session_id = login($username,$password);
15 my $ret = do_request($session_id, $wcap_command, @wcap_parameters);
16 my $html = parse_ical($ret->content);
17 my $ret = logout($session_id);
18  
19 =head1 DESCRIPTION
20  
21 Based on documentation provided in Sun ONE Calendar Server Programmer's
22 Manual, August 2002 (iCS 5.1.1 Programmer's Manual.pdf), see
23 http://docs.sun.com/prod/s1.s1cals for more details.
24  
25 =over 4
26  
27 =cut
28  
29 package WWW::WCAP;
30 1 our $VERSION = sprintf("%d.%02d", q$Revision: 0.1 $ =~ /(\d+)\.(\d+)/);
31  
32 1 require Exporter;
33 1 our @ISA = qw(Exporter);
34 1 our @EXPORT_OK = qw(login do_request parse_ical logout);
35  
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use LWP::UserAgent; # 'Fake' browser
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use HTTP::Request::Common; # For 'GET' primative
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use HTTP::Cookies; # Cookie jar for session
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Data::Dumper; # For debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Carp; # For warning
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Date::Calc qw(Add_Delta_DHMS Date_to_Text_Long Today); # For parsing
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 ###########################################################################
45 # Globals
46 1 ▶use constant DEBUG_LOW => 1; # minimal debug info
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant DEBUG_MEDIUM => 2; # moderate debug info
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant DEBUG_HIGH => 3; # verbose debug info
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant DEBUG => DEBUG_MEDIUM; # Debugging is ON/off
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant CAL_SERVER => 'yourhost.example.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant GMT_DELTA => 11; # Don't forget to change for daylight savings :P
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # Note -- the default is JavaScript. You *really* don't want to try to
54 # parse the JavaScript.
55 1 my %response_formats = (
56 ical => 'text/calendar',
57 xml => 'text/xml',
58 javascript => 'text/js',
59 );
60  
61 # Response in ical format
62 1 my $fmt = 'fmt-out=' . $response_formats{'ical'};
63  
64 1 my $ua = new LWP::UserAgent;
65 1 $ua->cookie_jar(HTTP::Cookies->new());
66  
67 ###########################################################################
68 # Methods and internal functions ##########################################
69 ###########################################################################
70  
71 =item login($username,$password)
72  
73 Log in to the calendar server. Returns a session ID.
74  
75 =cut
76  
77 1 ▶sub login($$) {
Subroutine login: called CC=1 SCAR=0.0
Pod documented
78 1 my ($user,$pass) = @_;
79 1 my $response = $ua->request(GET 'http://' . CAL_SERVER .
80 '/login.wcap?user=' . $user . '&password= ' .$pass);
81 1 my ($id) = $response->base =~ /id=([^\&]+)/;
82 1 carp "WCAP: Login id is $id" if DEBUG > DEBUG_LOW;
83 1 return $id;
84 } # end login
85 ##########################################################################
86  
87 =item do_request($session_id, $wcap_command, @wcap_parameters)
88  
89 Send a WCAP request. Returns a hashref of error status and contents.
90  
91 NOTE: There is a limit to the number of characters that may be passed
92 in for each parameter. The limit per parameter is 1000 characters.
93 (p80 Sun ONE Calendar Server Programmer's Manual, August 2002)
94  
95 =cut
96  
97 1 ▶sub do_request {
Subroutine do_request: called CC=4 SCAR=22.1
Pod documented
98 1 my ($id,$command,@args) = @_;
99 1 ▶ if (!defined $id || !defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id and defined $command) 1 0 1
Condition33%1/3
defined $id and defined $command
!ll&&!rl&&r
100
MC/DC0%0/2
defined $id and defined $command
defined $id defined $command
Truth table33%1/3
A and B
A= defined $idB= defined $command
ABresult
0X0
100
111
100 1 warn "Missing parameter for do_request()" if DEBUG > DEBUG_LOW;
101 1 die @_;
102 0 return undef;
103 }
104 0 my $req = 'http://' . CAL_SERVER . '/' . $command .
105 '?id=' . $id;
106 0 push(@args,$fmt); # Cheat for now, and add directly
107 0 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
108 # Need to encode args...
109 0 $req .= '&' . join('&', @args);
110 }
111 0 print "Fetching $req" if DEBUG > DEBUG_LOW;
112 0 my $response = $ua->request(GET $req);
113 return {
114 0 status => $response->is_success,
115 content => $response->content,
116 };
117 } # end do_request
118 ##########################################################################
119  
120 =item parse_ical
121  
122 Parse the iCal data returned (currently dumps it out as HTML).
123  
124 =cut
125  
126 0 ▶sub parse_ical($) {
Subroutine parse_ical: not called CC=33 SCAR=70.2
Pod documented
127 0 my $content = shift;
128 0 my %eventList;
129 0 my @lines = split(/\n/, $content);
130 0 ▶ while (my $line = shift @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 0 0 0
131 # Should use MIME::Parser, but doesn't seem to save us much...
132 0 ▶ next unless $line =~ m/^BEGIN:VEVENT/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^BEGIN:VEVENT/ 0 0 0
133 0 my %event;
134 0 my $continuing_line = "";
135 0 ▶ while (my $line = shift @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 0 0 0
136 0 ▶ last if $line =~ m/^END:VEVENT/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^END:VEVENT/ 0 0 0
137 0 chomp $line;
138 0 $line =~ s/\r//;
139 0 my $foo = 0;
140 0 ▶ $foo = 1 if $line =~ /CAT/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /CAT/ 0 0 0
141 0 ▶ if ($line =~ /^ (.*)/) { # A continuing line
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ (.*)/) 0 0 0
142 0 $continuing_line .= $1;
143 0 next;
144 }
145 0 ▶ if ($continuing_line ne "") { # We don't know if there are continuing
Branch: 1 branch
BranchTrueFalseTotal
if ($continuing_line ne "") 0 0 0
146 # lines until there are/aren't...
147 0 ▶ if (my ($field,$data) = $continuing_line =~ /^([A-Z]+):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $continuing_line =~ /^([A-Z]+):(.*)/) { } 0 0 0
148 0 $event{$field} = $data;
149 } else {
150 #carp "Couldn't understand line: $continuing_line";
151 }
152 }
153 0 $continuing_line = $line;
154 }
155 0 ▶ if ($continuing_line ne "") { # Catch the last line
Branch: 1 branch
BranchTrueFalseTotal
if ($continuing_line ne "") 0 0 0
156 0 ▶ if (my ($field,$data) = $continuing_line =~ /^([A-Z]+):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $continuing_line =~ /^([A-Z]+):(.*)/) { } 0 0 0
157 0 $event{$field} = $data;
158 } else {
159 #carp "Couldn't understand line: $continuing_line";
160 }
161 }
162 #use Data::Dumper;
163 #print Dumper(\%event);
164 0 my ($date,$start_time,$end_time,$requestor,$status,$description);
165 0 ▶ if (my ($yy, $mm, $dd, $hrs, $mins, $secs) = $event{'DTSTART'} =~ /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $event{'DTSTART'} =~ /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/) { } 0 0 0
166 0 my ($year,$month,$day, $hour,$min,$sec) = Add_Delta_DHMS($yy,$mm,$dd, $hrs,$mins,$secs, 0, GMT_DELTA, 0, 0);
167 0 $date = sprintf("%4.4d-%2.2d-%2.2d", $year, $month,$day);
168 0 $start_time = sprintf("%2.2d:%2.2d", $hour, $min);
169 0 ▶ if (my ($d_hrs, $d_mins, $d_secs) = $event{'DURATION'} =~ /P0DT(\d+)H(\d+)M(\d+)S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $event{'DURATION'} =~ /P0DT(\d+)H(\d+)M(\d+)S/) { } 0 0 0
170 0 my ($year,$month,$day, $hour,$min,$sec) = Add_Delta_DHMS($yy,$mm,$dd, $hrs,$mins,$secs, 0, $d_hrs + GMT_DELTA, $d_mins, $d_secs);
171 0 $end_time = sprintf("%2.2d:%2.2d", $hour, $min);
172 } else {
173 # Daily events don't have durations...
174 0 ▶ carp "Couldn't parse duration: ($event{'DURATION'}) for $event{'SUMMARY'}" if $event{'CATEGORIES'} ne 'DAILY NOTE' && $event{'CATEGORIES'} ne 'DAY EVENT' && $event{'CATEGORIES'} ne 'HOLIDAY';
Branch: 1 branch
BranchTrueFalseTotal
if $event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT" and $event{"CATEGORIES"} ne "HOLIDAY" 0 0 0
Condition0%0/6
$event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT" and $event{"CATEGORIES"} ne "HOLIDAY"
!ll&&!rl&&r
000
$event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT"
!ll&&!rl&&r
000
MC/DC0%0/3
$event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT" and $event{"CATEGORIES"} ne "HOLIDAY"
$event{"CATEGORIES"} ne "DAILY NOTE" $event{"CATEGORIES"} ne "DAY EVENT" $event{"CATEGORIES"} ne "HOLIDAY"
Truth table0%0/4
A and B and C
A= $event{"CATEGORIES"} ne "DAILY NOTE"B= $event{"CATEGORIES"} ne "DAY EVENT"C= $event{"CATEGORIES"} ne "HOLIDAY"
ABCresult
0XX0
10X0
1100
1111
175 }
176 } else {
177 0 carp "Couldn't parse date/time: $event{'DTSTART'}";
178 }
179 0 ▶ carp "Not an appointment, daily note, day event or holiday: $event{'CATEGORIES'}" if $event{'CATEGORIES'} ne "APPOINTMENT" && $event{'CATEGORIES'} ne 'DAILY NOTE' && $event{'CATEGORIES'} ne 'DAY EVENT' && $event{'CATEGORIES'} ne 'HOLIDAY';
Branch: 1 branch
BranchTrueFalseTotal
if $event{"CATEGORIES"} ne "APPOINTMENT" and $event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT" and $event{"CATEGORIES"} ne "HOLIDAY" 0 0 0
Condition0%0/9
$event{"CATEGORIES"} ne "APPOINTMENT" and $event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT" and $event{"CATEGORIES"} ne "HOLIDAY"
!ll&&!rl&&r
000
$event{"CATEGORIES"} ne "APPOINTMENT" and $event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT"
!ll&&!rl&&r
000
$event{"CATEGORIES"} ne "APPOINTMENT" and $event{"CATEGORIES"} ne "DAILY NOTE"
!ll&&!rl&&r
000
MC/DC0%0/4
$event{"CATEGORIES"} ne "APPOINTMENT" and $event{"CATEGORIES"} ne "DAILY NOTE" and $event{"CATEGORIES"} ne "DAY EVENT" and $event{"CATEGORIES"} ne "HOLIDAY"
$event{"CATEGORIES"} ne "APPOINTMENT" $event{"CATEGORIES"} ne "DAILY NOTE" $event{"CATEGORIES"} ne "DAY EVENT" $event{"CATEGORIES"} ne "HOLIDAY"
Truth table0%0/5
A and B and C and D
A= $event{"CATEGORIES"} ne "APPOINTMENT"B= $event{"CATEGORIES"} ne "DAILY NOTE"C= $event{"CATEGORIES"} ne "DAY EVENT"D= $event{"CATEGORIES"} ne "HOLIDAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
180  
181 0 ▶ if (my ($first,$last) = $event{'ORGANIZER'} =~ /mailto:(.*)\.(.*)\@/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $event{'ORGANIZER'} =~ /mailto:(.*)\.(.*)\@/) { } 0 0 0
182 0 $requestor = " ($first $last)";
183 0 ▶ $requestor = "" if $requestor eq " (Nathan Bailey)";
Branch: 1 branch
BranchTrueFalseTotal
if $requestor eq " (Nathan Bailey)" 0 0 0
184 } else {
185 #carp "Couldn't parse requestor: $event{'ORGANIZER'} for $event{'SUMMARY'}" if $event{'CATEGORIES'} ne 'HOLIDAY';
186 }
187 # Big time cheat for now...
188 0 if (1 || $event{'CATEGORIES'} eq "APPOINTMENT") {
189 #carp "Not confirmed" if $event{'STATUS'} ne "CONFIRMED";
190 0 my $location = "";
191 0 ▶ if (defined $event{'LOCATION'} && $event{'LOCATION'} ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $event{"LOCATION"} and $event{"LOCATION"} ne "") 0 0 0
Condition0%0/3
defined $event{"LOCATION"} and $event{"LOCATION"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $event{"LOCATION"} and $event{"LOCATION"} ne ""
defined $event{"LOCATION"} $event{"LOCATION"} ne ""
Truth table0%0/3
A and B
A= defined $event{"LOCATION"}B= $event{"LOCATION"} ne ""
ABresult
0X0
100
111
192 0 $location = " [$event{'LOCATION'}]";
193 0 $location =~ s#\\##g;
194 }
195 0 $eventList{$date.'_'.$start_time.'_'.$event{'SUMMARY'}} = "($start_time-$end_time) $event{'SUMMARY'}$requestor$location\n";
196 } elsif ($event{'CATEGORIES'} eq 'DAILY NOTE') {
197 $eventList{$date.'_dailynote_'.$event{'SUMMARY'}} = "<b>Note:</b> $event{'SUMMARY'}$requestor\n";
198 }
199 }
200  
201 0 my $prev = "";
202 0 my ($t_year,$t_month,$t_day) = Today();
203 0 my $today = 0;
204 0 my @k = keys %eventList;
205 0 ▶ print "<p><small>Current as at: ".`/bin/date`."</small></p>" if $#k >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#k >= 0 0 0 0
206 0 foreach my $e (sort @k) {
207 0 my ($date,$start_time,$summary) = split(/_/,$e);
208 0 ▶ if ($date ne $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date ne $prev) 0 0 0
209 0 print "</ul>\n";
210 0 ▶ if ($today) {
Branch: 1 branch
BranchTrueFalseTotal
if ($today) 0 0 0
211 0 print '</td></tr></table>';
212 0 $today = 0;
213 }
214 0 my ($year, $month, $day) = split(/-/,$date);
215 0 my $pretty_day = Date_to_Text_Long($year, $month, $day);
216 0 $pretty_day =~ s/ $year//;
217 0 print "<h3>$pretty_day</h3>\n";
218 0 ▶ if (!$today && $day == $t_day && $month == $t_month && $year == $t_year) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $today and $day == $t_day and $month == $t_month and $year == $t_year) 0 0 0
Condition0%0/9
not $today and $day == $t_day and $month == $t_month and $year == $t_year
!ll&&!rl&&r
000
not $today and $day == $t_day and $month == $t_month
!ll&&!rl&&r
000
not $today and $day == $t_day
!ll&&!rl&&r
000
MC/DC0%0/4
not $today and $day == $t_day and $month == $t_month and $year == $t_year
not $today $day == $t_day $month == $t_month $year == $t_year
Truth table0%0/5
not(A) and B and C and D
A= not $todayB= $day == $t_dayC= $month == $t_monthD= $year == $t_year
ABCDresult
0XXX0
10XX0
110X0
11100
11111
219 0 print '<table><tr><td bgcolor="#00FF00">';
220 0 $today = 1;
221 }
222 0 $prev = $date;
223 0 print "<ul>\n";
224 }
225 0 print "<li> $eventList{$e}";
226 }
227 0 ▶ if ($prev eq "") { # Blank calendar...
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq '') { } 0 0 0
228 0 print "<h3>No events available</h3>";
229 } else {
230 0 print "</ul>\n"
231 }
232 } # end parse_ical
233 ##########################################################################
234  
235 =item logout($session_id)
236  
237 Log in to the calendar server. Returns a session ID.
238  
239 =cut
240  
241 0 ▶sub logout($) {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
242 0 my $id = shift;
243 0 return do_request($id,'logout.wcap');
244 } # end logout
245 ##########################################################################
246  
247 1 1;
248  
249 __END__
↑ Summary t/base/wcap.t »