ICal.pm

filter: Statementstmt 98.8% 83 / 84 Branchbran 88.8% 32 / 36 Conditioncond 90.4% 19 / 21 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 93.6% 162 / 173 CC 27 SCAR 22.4
CC
27
Cov
95%
parse
17.2
_vcal10_input_cleanup
7.0
new
5.0
CRAP 9.4
↑ Summary ICal/Entry.pm »
1 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Data::ICal;
5 10 ▶use base qw/Data::ICal::Entry/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Class::ReturnValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Text::vFile::asData;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 our $VERSION = '0.24';
11  
12 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Data::ICal - Generates iCalendar (RFC 2445) calendar files
17  
18 =head1 SYNOPSIS
19  
20 use Data::ICal;
21  
22 my $calendar = Data::ICal->new();
23  
24 my $vtodo = Data::ICal::Entry::Todo->new();
25 $vtodo->add_properties(
26 # ... see Data::ICal::Entry::Todo documentation
27 );
28  
29 # ... or
30 $calendar = Data::ICal->new(filename => 'foo.ics'); # parse existing file
31 $calendar = Data::ICal->new(data => 'BEGIN:VCALENDAR...'); # parse from scalar
32 $calendar->add_entry($vtodo);
33 print $calendar->as_string;
34  
35 =head1 DESCRIPTION
36  
37 A L<Data::ICal> object represents a C<VCALENDAR> object as defined in the
38 iCalendar protocol (RFC 2445, MIME type "text/calendar"), as implemented in many
39 popular calendaring programs such as Apple's iCal.
40  
41 Each L<Data::ICal> object is a collection of "entries", which are objects of a
42 subclass of L<Data::ICal::Entry>. The types of entries defined by iCalendar
43 (which refers to them as "components") include events, to-do items, journal
44 entries, free/busy time indicators, and time zone descriptors; in addition,
45 events and to-do items can contain alarm entries. (Currently, L<Data::ICal>
46 only implements to-do items and events.)
47  
48 L<Data::ICal> is a subclass of L<Data::ICal::Entry>; see its manpage for more
49 methods applicable to L<Data::ICal>.
50  
51 =head1 METHODS
52  
53 =cut
54  
55 =head2 new [ data => $data, ] [ filename => $file ], [ calname => $string ], [ vcal10 => $bool ], [ rfc_strict => $bool ], [ auto_uid => $bool ]
56  
57 Creates a new L<Data::ICal> object.
58  
59 If it is given a filename or data argument is passed, then this parses the
60 content of the file or string into the object. If the C<vcal10> flag is passed,
61 parses it according to vCalendar 1.0, not iCalendar 2.0; this in particular impacts
62 the parsing of continuation lines in quoted-printable sections.
63  
64 If a calname is passed, sets x-wr-calname to the given string. Although
65 not specified in RFC2445, most calendar software respects x-wr-calname
66 as the displayed name of the calendar.
67  
68 If the C<rfc_strict> flag is set to true, will require Data::ICal to
69 include UIDs, as per RFC2445:
70  
71 4.8.4.7 Unique Identifier
72 ... The property MUST be specified in the "VEVENT", "VTODO",
73 "VJOURNAL" or "VFREEBUSY" calendar components"
74  
75 If the C<auto_uid> flag is set to true, will automatically generate a
76 default UID for each type which requires it, based on the RFC-suggested
77 algorithm. Explicitly-set UID attributes will override this
78 auto-generated value.
79  
80 If a filename or data argument is not passed, this just sets the
81 object's C<VERSION> and C<PRODID> properties to "2.0" (or "1.0" if the
82 C<vcal10> flag is passed) and the value of the C<product_id> method
83 respectively.
84  
85 Returns a false value upon failure to open or parse the file or data; this false
86 value is a L<Class::ReturnValue> object and can be queried as to its
87 C<error_message>.
88  
89 =cut
90  
91 22 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
92 22 my $class = shift;
93 22 my $self = $class->SUPER::new(@_);
94  
95 22 my %args = (
96 filename => undef,
97 calname => undef,
98 data => undef,
99 vcal10 => 0,
100 rfc_strict => 0,
101 auto_uid => 0,
102 @_
103 );
104  
105 22 $self->vcal10( $args{vcal10} );
106 22 $self->rfc_strict( $args{rfc_strict} );
107 22 $self->auto_uid( $args{auto_uid} );
108  
109 22 ▶ if ( defined $args{filename} or defined $args{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'filename'} or defined $args{'data'}) { } 15 7 22
Condition100%3/3
defined $args{"filename"} or defined $args{"data"}
l!l&&r!l&&!r
1057
MC/DC100%2/2
defined $args{"filename"} or defined $args{"data"}
defined $args{"filename"} defined $args{"data"}
Truth table100%3/3
A or B
A= defined $args{"filename"}B= defined $args{"data"}
ABresult
1X1
011
000
110  
111 # might return a Class::ReturnValue if parsing fails
112 15 return $self->parse(%args);
113 } else {
114 7 ▶ $self->add_properties(
Branch: 1 branch
BranchTrueFalseTotal
$self->vcal10 ? : 1 6 7
115 version => ( $self->vcal10 ? '1.0' : '2.0' ),
116 prodid => $self->product_id,
117 );
118 $self->add_property('x-wr-calname' => $args{calname})
119 7 ▶ if defined $args{calname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"calname"} 0 7 7
120  
121 7 return $self;
122 }
123 }
124  
125 =head2 parse [ data => $data, ] [ filename => $file, ]
126  
127 Parse a C<.ics> file or string containing one, and populate C<$self>
128 with its contents.
129  
130 Should only be called once on a given object, and will be automatically
131 called by C<new> if you provide arguments to C<new>.
132  
133 Returns C<$self> on success. Returns a false value upon failure to
134 open or parse the file or data; this false value is a
135 L<Class::ReturnValue> object and can be queried as to its
136 C<error_message>.
137  
138 =cut
139  
140 15 ▶sub parse {
Subroutine parse: called CC=17 SCAR=28.4
Pod documented
141 15 my $self = shift;
142 15 my %args = (
143 filename => undef,
144 data => undef,
145 @_
146 );
147  
148 15 ▶ unless ( defined $args{filename} or defined $args{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"filename"} or defined $args{"data"}) 0 15 15
Condition66%2/3
defined $args{"filename"} or defined $args{"data"}
l!l&&r!l&&!r
1050
MC/DC0%0/2
defined $args{"filename"} or defined $args{"data"}
defined $args{"filename"} defined $args{"data"}
Truth table66%2/3
A or B
A= defined $args{"filename"}B= defined $args{"data"}
ABresult
1X1
011
000
149 0 return $self->_error(
150 "parse called with no filename or data specified");
151 }
152  
153 15 my @lines;
154  
155 # open the file (checking as we go, like good little Perl mongers)
156 15 ▶ if ( defined $args{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'filename'}) { } 10 5 15
157 open my $fh, '<', $args{filename}
158 10 ▶ or return $self->_error("could not open '$args{filename}': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $args{"filename"} or return $self->_error(qq[could not open '$args{"filename"}': $!]) 9 1 10
159 9 @lines = map { chomp; $_ } <$fh>;
160 } else {
161 5 @lines = split /\r?\n/, $args{data};
162 }
163  
164 14 ▶ @lines = $self->_vcal10_input_cleanup(@lines) if $self->vcal10;
Branch: 1 branch
BranchTrueFalseTotal
if $self->vcal10 5 9 14
165  
166 # Parse the lines; Text::vFile doesn't want trailing newlines
167 14 my $cal = eval { Text::vFile::asData->new->parse_lines(@lines) };
168 14 ▶ return $self->_error("parse failure: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
169  
170 return $self->_error("parse failure")
171 14 ▶ unless $cal and exists $cal->{objects};
Branch: 1 branch
BranchTrueFalseTotal
unless $cal and exists $cal->{"objects"} 1 13 14
Condition66%2/3
$cal and exists $cal->{"objects"}
!ll&&!rl&&r
0113
MC/DC50%1/2
$cal and exists $cal->{"objects"}
$cal exists $cal->{"objects"}
Truth table66%2/3
A and B
A= $calB= exists $cal->{"objects"}
ABresult
0X0
100
111
172  
173 # loop through all the vcards
174 13 foreach my $object ( @{ $cal->{objects} } ) {
175 13 $self->parse_object($object);
176 }
177  
178 13 my $version_ref = $self->property("version");
179 13 ▶ my $version = $version_ref ? $version_ref->[0]->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$version_ref ? : 12 1 13
180 13 ▶ unless ( defined $version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version) 1 12 13
181 1 return $self->_error("data does not specify a version property");
182 }
183  
184 12 ▶ if ( $version eq '1.0' and not $self->vcal10
Branch: 1 branch
BranchTrueFalseTotal
if ($version eq "1.0" and not $self->vcal10 or $version eq "2.0" and $self->vcal10) 2 10 12
Condition100%9/9
$version eq "1.0" and not $self->vcal10 or $version eq "2.0" and $self->vcal10
l!l&&r!l&&!r
1110
$version eq "1.0" and not $self->vcal10
!ll&&!rl&&r
741
$version eq "2.0" and $self->vcal10
!ll&&!rl&&r
461
MC/DC75%3/4
$version eq "1.0" and not $self->vcal10 or $version eq "2.0" and $self->vcal10
$version eq "1.0" not $self->vcal10 $version eq "2.0" $self->vcal10
Truth table57%4/7
A and not(B) or C and D
A= $version eq "1.0"B= not $self->vcal10C= $version eq "2.0"D= $self->vcal10
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
185 or $version eq '2.0' and $self->vcal10 )
186 {
187 2 ▶ return $self->_error( 'application claims data is'
Branch: 1 branch
BranchTrueFalseTotal
$self->vcal10 ? : 1 1 2
188 . ( $self->vcal10 ? '' : ' not' )
189 . ' vCal 1.0 but doc contains VERSION:'
190 . $version );
191 }
192  
193 10 return $self;
194 }
195  
196 5 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
197 5 my $self = shift;
198 5 my $msg = shift;
199  
200 5 my $ret = Class::ReturnValue->new;
201 5 $ret->as_error( errno => 1, message => $msg );
202 5 return $ret;
203 }
204  
205 =head2 ical_entry_type
206  
207 Returns C<VCALENDAR>, its iCalendar entry name.
208  
209 =cut
210  
211 38 ▶sub ical_entry_type {'VCALENDAR'}
Subroutine ical_entry_type: called CC=1 SCAR=0.0
Pod documented
212  
213 =head2 product_id
214  
215 Returns the product ID used in the calendar's C<PRODID> property; you may
216 wish to override this in a subclass for your own application.
217  
218 =cut
219  
220 7 ▶sub product_id {
Subroutine product_id: called CC=1 SCAR=0.0
Pod documented
221 7 my $self = shift;
222 7 return "Data::ICal $VERSION";
223 }
224  
225 =head2 mandatory_unique_properties
226  
227 According to the iCalendar standard, the following properties must be specified
228 exactly one time for a calendar:
229  
230 prodid version
231  
232 =cut
233  
234 104 ▶sub mandatory_unique_properties {
Subroutine mandatory_unique_properties: called CC=1 SCAR=0.0
Pod documented
235 104 qw(
236 prodid version
237 );
238 }
239  
240 =head2 optional_unique_properties
241  
242 According to the iCalendar standard, the following properties may be specified
243 at most one time for a calendar:
244  
245 calscale method
246  
247 =cut
248  
249 88 ▶sub optional_unique_properties {
Subroutine optional_unique_properties: called CC=1 SCAR=0.0
Pod documented
250 88 qw(
251 calscale method
252 );
253 }
254  
255 # In quoted-printable sections, convert from vcal10 "=\n" line endings to
256 # ical20 "\n ".
257 5 ▶sub _vcal10_input_cleanup {
Subroutine _vcal10_input_cleanup: called CC=7 SCAR=19.5
258 5 my $self = shift;
259 5 my @in_lines = @_;
260  
261 5 my @out_lines;
262  
263 5 my $in_qp = 0;
264 5 ▶LINE: while (@in_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@in_lines) 177 5 182
265 177 my $line = shift @in_lines;
266  
267 177 ▶ if ( not $in_qp and $line =~ /^[^:]+;ENCODING=QUOTED-PRINTABLE/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_qp and $line =~ /^[^:]+;ENCODING=QUOTED-PRINTABLE/i) 4 173 177
Condition100%3/3
not $in_qp and $line =~ /^[^:]+;ENCODING=QUOTED-PRINTABLE/i
!ll&&!rl&&r
71664
MC/DC100%2/2
not $in_qp and $line =~ /^[^:]+;ENCODING=QUOTED-PRINTABLE/i
not $in_qp $line =~ /^[^:]+;ENCODING=QUOTED-PRINTABLE/i
Truth table100%3/3
not(A) and B
A= not $in_qpB= $line =~ /^[^:]+;ENCODING=QUOTED-PRINTABLE/i
ABresult
0X0
100
111
268 4 $in_qp = 1;
269 }
270  
271 177 ▶ unless ($in_qp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_qp) 166 11 177
272 166 push @out_lines, $line;
273 166 next LINE;
274 }
275  
276 11 ▶ if ( $line =~ s/=$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/=$//) { } 7 4 11
277 7 push @out_lines, $line;
278 7 ▶ $in_lines[0] = ' ' . $in_lines[0] if @in_lines;
Branch: 1 branch
BranchTrueFalseTotal
if @in_lines 7 0 7
279 } else {
280 4 push @out_lines, $line;
281 4 $in_qp = 0;
282 }
283 }
284  
285 5 return @out_lines;
286 }
287  
288 =head1 DEPENDENCIES
289  
290 L<Data::ICal> requires L<Class::Accessor>, L<Text::vFile::asData>,
291 L<MIME::QuotedPrint>, and L<Class::ReturnValue>.
292  
293 =head1 BUGS AND LIMITATIONS
294  
295 L<Data::ICal> does not support time zone daylight or standard entries,
296 so time zone components are basically useless.
297  
298 While L<Data::ICal> tries to check which properties are required and
299 repeatable, this only works in simple cases; it does not check for
300 properties that must either both exist or both not exist, or for
301 mutually exclusive properties.
302  
303 L<Data::ICal> does not check to see if property parameter names are
304 known in general or allowed on the particular property.
305  
306 L<Data::ICal> does not check to see if nested entries are nested
307 properly (alarms in todos and events only, everything else in
308 calendars only).
309  
310 The only property encoding supported by L<Data::ICal> is quoted
311 printable.
312  
313 Please report any bugs or feature requests to
314 C<bug-data-ical@rt.cpan.org>, or through the web interface at
315 L<http://rt.cpan.org>.
316  
317  
318 =head1 AUTHOR
319  
320 Best Practical Solutions, LLC E<lt>modules@bestpractical.comE<gt>
321  
322 =head1 LICENCE AND COPYRIGHT
323  
324 Copyright (c) 2005 - 2020, Best Practical Solutions, LLC. All rights reserved.
325  
326 This module is free software; you can redistribute it and/or
327 modify it under the same terms as Perl itself. See L<perlartistic>.
328  
329 =cut
330  
331 10 1;
↑ Summary ICal/Entry.pm »