File: Entry.pm

package info (click to toggle)
libdata-ical-perl 0.24%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 388 kB
  • sloc: perl: 3,465; makefile: 2
file content (640 lines) | stat: -rw-r--r-- 17,508 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use warnings;
use strict;

package Data::ICal::Entry;
use base qw/Class::Accessor/;
use Data::ICal::Property;
use Sys::Hostname qw();         # For unique UIDs for entries
use Carp;

use constant CRLF => "\x0d\x0a";

=head1 NAME

Data::ICal::Entry - Represents an entry in an iCalendar file

=head1 SYNOPSIS

    my $vtodo = Data::ICal::Entry::Todo->new();
    $vtodo->add_property(
    # ... see Data::ICal::Entry::Todo documentation
    );
    $vtodo->add_properties( ... );

    $calendar->add_entry($vtodo);

    $event->add_entry($alarm);
    $event->add_entries($alarm1, ...);

    # or all in one go
    my $vtodo = Data::ICal::Entry::Todo->new( \%props, \@entries );

=head1 DESCRIPTION

A L<Data::ICal::Entry> object represents a single entry in an
iCalendar file.  (Note that the iCalendar RFC refers to entries as
"components".)  iCalendar defines several types of entries, such as
events and to-do lists; each of these corresponds to a subclass of
L<Data::ICal::Entry> (though only to-do lists and events are currently
implemented).  L<Data::ICal::Entry> should be treated as an abstract
base class -- all objects created should be of its subclasses.  The
entire calendar itself (the L<Data::ICal> object) is also represented
as a L<Data::ICal::Entry> object.

Each entry has an entry type (such as C<VCALENDAR> or C<VEVENT>), a
series of "properties", and possibly some sub-entries.  (Only the root
L<Data::ICal> object can have sub-entries, except for alarm entries
contained in events and to-dos (not yet implemented).)

=head1 METHODS

=cut

=head2 new

Creates a new entry object with no properties or sub-entries.

=cut

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new();
    # ALLOW passing arguments here!
    $self->set( properties => {} );
    $self->set( entries    => [] );
    for (@_) {
        ref $_ eq "HASH"  and $self->add_properties( %$_ );
        ref $_ eq "ARRAY" and $self->add_entries( @$_ );
    }
    return $self;
}

=head2 as_string [ crlf => C<CRLF> ]

Returns the entry as an appropriately formatted string (with trailing
newline).

Properties are returned in alphabetical order, with multiple
properties of the same name returned in the order added.  (Property
order is unimportant in iCalendar, and this makes testing easier.)

If any mandatory property is missing, issues a warning.

The string to use as a newline can optionally be specified by giving
the a C<crlf> argument, which defaults to C<\x0d\x0a>, per RFC 2445
spec; this option is primarily for backwards compatibility with
versions of this module before 0.16.

=cut

my $uid = 0;
sub as_string {
    my $self = shift;
    my %args = (
        crlf => CRLF,
        @_
    );
    my $output = $self->header(%args);

    my @mandatory = (
        $self->mandatory_unique_properties,
        $self->mandatory_repeatable_properties,
    );

    if (grep {$_ eq "uid"} @mandatory and !defined $self->properties->{uid}
            and $self->auto_uid) {
      # Per the RFC, create a "persistent, globally unique" UID for this
      # event; "persistent" in this context does not mean consistent
      # across time, but rather "unique across all time"
      $self->add_property(
          uid => time() . '-' .$$ . '-' . $uid++ . '@' . Sys::Hostname::hostname()
      );
    }

    for my $name ( @mandatory ) {
        carp "Mandatory property for " . ( ref $self ) . " missing: $name"
            unless $self->properties->{$name}
                and @{ $self->properties->{$name} };
    }

    my @properties = sort {
        # RFC2445 implies an order (see 4.6 Calendar Components) but does not
        # require it.  However, some applications break if VERSION is not first
        # (see http://icalvalid.cloudapp.net/Default.aspx and [rt.cpan.org # #65447]).
        return -1 if $a eq 'version';
        return  1 if $b eq 'version';
        return $a cmp $b;
    } keys %{ $self->properties };

    for my $name (@properties) {
        $output .= $_
            for map { $_->as_string(%args) } @{ $self->properties->{$name} };
    }

    for my $entry ( @{ $self->entries } ) {
        $output .= $entry->as_string(%args);
    }
    $output .= $self->footer(%args);

    return $output;
}

=head2 add_entry $entry

Adds an entry to this entry.  (According to the standard, this should
only be called on either a to-do or event entry with an alarm entry,
or on a calendar entry (L<Data::ICal>) with a to-do, event, journal,
timezone, or free/busy entry.)

Returns true if the entry was successfully added, and false otherwise
(perhaps because you tried to add an entry of an invalid type, but
this check hasn't been implemented yet).

=cut

sub add_entry {
    my $self  = shift;
    my $entry = shift;
    push @{ $self->{entries} }, $entry;

    $entry->vcal10( $self->vcal10 );
    $entry->rfc_strict( $self->rfc_strict );
    $entry->auto_uid( $self->auto_uid );

    return $self;
}

=head2 add_entries $entry1, [$entry2, ...]

Convenience function to call C<add_entry> several times with a list
of entries.

=cut

sub add_entries {
    my $self  = shift;
    $self->add_entry( $_ ) for @_;
    return $self;
}

=head2 entries

Returns a reference to the array of subentries of this entry.

=cut

__PACKAGE__->mk_ro_accessors('entries');

=head2 properties

Returns a reference to the hash of properties of this entry.  The keys
are property names and the values are array references containing
L<Data::ICal::Property> objects.

=cut

__PACKAGE__->mk_ro_accessors('properties');

=head2 property

Given a property name returns a reference to the array of
L<Data::ICal::Property> objects.

=cut

sub property {
    my $self = shift;
    my $prop = lc shift;
    return $self->{'properties'}->{$prop};
}

=head2 add_property $propname => $propval

Creates a new L<Data::ICal::Property> object with name C<$propname>
and value C<$propval> and adds it to the event.

If the property is not known to exist for that object type and does
not begin with C<X->, issues a warning.

If the property is known to be unique, replaces the original property.

To specify parameters for the property, let C<$propval> be a
two-element array reference where the first element is the property
value and the second element is a hash reference.  The keys of the
hash are parameter names; the values should be either strings or array
references of strings, depending on whether the parameter should have
one or multiple (to be comma-separated) values.

Examples of setting parameters:

 # Add a property with a parameter of VALUE set to 'DATE'
 $event->add_property( rdate => [ $date, { VALUE => 'DATE' } ] );

=cut

sub add_property {
    my $self = shift;
    my $prop = lc shift;
    my $val  = shift;

    return unless defined $prop;

    unless ( $self->is_property($prop) or $prop =~ /^x-/i ) {
        carp "Unknown property for " . ( ref $self ) . ": $prop";
    }

    if ( $self->is_unique($prop) ) {

        # It should be unique, so clear out anything we might have first
        $self->properties->{$prop} = [];
    }

    $val = [ $val, {} ] unless ref $val eq 'ARRAY';

    my ( $prop_value, $param_hash ) = @$val;

    my $p = Data::ICal::Property->new( $prop => $prop_value, $param_hash );
    $p->vcal10( $self->vcal10 );

    push @{ $self->properties->{$prop} }, $p;
    return $self;
}

=head2 add_properties $propname1 => $propval1, [$propname2 => $propname2, ...]

Convenience function to call C<add_property> several times with a list
of properties.

This method is guaranteed to call add C<add_property> on them in the
order given, so that unique properties given later in the call will
take precedence over those given earlier.  (This is unrelated to the
order of properties when the entry is rendered as a string, though.)

Parameters for the properties are specified in the same way as in
C<add_property>.

=cut

sub add_properties {
    my $self = shift;

    if ( @_ % 2 ) {
        carp "Odd number of elements in add_properties call";
        return;
    }

    while (@_) {
        my $prop = shift;
        my $val  = shift;
        $self->add_property( $prop => $val );
    }
    return $self;
}

=head2 mandatory_unique_properties

Subclasses should override this method (which returns an empty list by
default) to provide a list of lower case strings identifying the
properties which must appear exactly once in the subclass's entry
type.

=cut

sub mandatory_unique_properties { () }

=head2 mandatory_repeatable_properties

Subclasses should override this method (which returns an empty list by
default) to provide a list of lower case strings identifying the
properties which must appear at least once in the subclass's entry
type.

=cut

sub mandatory_repeatable_properties { () }

=head2 optional_unique_properties

Subclasses should override this method (which returns an empty list by
default) to provide a list of lower case strings identifying the
properties which must appear at most once in the subclass's entry
type.

=cut

sub optional_unique_properties { () }

=head2 optional_repeatable_properties

Subclasses should override this method (which returns an empty list by
default) to provide a list of lower case strings identifying the
properties which may appear zero, one, or more times in the subclass's
entry type.

=cut

sub optional_repeatable_properties { () }

=head2 is_property $name

Returns a boolean value indicating whether or not the property
C<$name> is known to the class (that is, if it's listed in
C<(mandatory/optional)_(unique/repeatable)_properties>).

=cut

sub is_property {
    my $self = shift;
    my $name = shift;
    return scalar grep { $_ eq $name } $self->mandatory_unique_properties,
        $self->mandatory_repeatable_properties,
        $self->optional_unique_properties,
        $self->optional_repeatable_properties;
}

=head2 is_mandatory $name

Returns a boolean value indicating whether or not the property
C<$name> is known to the class as mandatory (that is, if it's listed
in C<mandatory_(unique/repeatable)_properties>).

=cut

sub is_mandatory {
    my $self = shift;
    my $name = shift;
    return scalar grep { $_ eq $name } $self->mandatory_unique_properties,
        $self->mandatory_repeatable_properties;
}

=head2 is_optional $name

Returns a boolean value indicating whether or not the property
C<$name> is known to the class as optional (that is, if it's listed in
C<optional_(unique/repeatable)_properties>).

=cut

sub is_optional {
    my $self = shift;
    my $name = shift;
    return scalar grep { $_ eq $name } $self->optional_unique_properties,
        $self->optional_repeatable_properties;
}

=head2 is_unique $name

Returns a boolean value indicating whether or not the property
C<$name> is known to the class as unique (that is, if it's listed in
C<(mandatory/optional)_unique_properties>).

=cut

sub is_unique {
    my $self = shift;
    my $name = shift;
    return scalar grep { $_ eq $name } $self->mandatory_unique_properties,
        $self->optional_unique_properties;
}

=head2 is_repeatable $name

Returns a boolean value indicating whether or not the property
C<$name> is known to the class as repeatable (that is, if it's listed
in C<(mandatory/optional)_repeatable_properties>).

=cut

sub is_repeatable {
    my $self = shift;
    my $name = shift;
    return scalar grep { $_ eq $name } $self->mandatory_repeatable_properties,
        $self->optional_repeatable_properties;
}

=head2 ical_entry_type

Subclasses should override this method to provide the identifying type
name of the entry (such as C<VCALENDAR> or C<VTODO>).

=cut

sub ical_entry_type {'UNDEFINED'}

=head2 vcal10 [$bool]

Gets or sets a boolean saying whether this entry should be interpreted
as vCalendar 1.0 (as opposed to iCalendar 2.0).  Generally, you can
just set this on your main L<Data::ICal> object when you construct it;
C<add_entry> automatically makes sure that sub-entries end up with the
same value as their parents.

=cut

__PACKAGE__->mk_accessors('vcal10');

=head2 rfc_strict [$bool]

Gets or sets a boolean saying whether this entry will complain about
missing UIDs as per RFC2446. Defaults to false, for backwards
compatibility.  Generally, you can just set this on your main
L<Data::ICal> object when you construct it; C<add_entry> automatically
makes sure that sub-entries end up with the same value as their parents.

=cut

__PACKAGE__->mk_accessors('rfc_strict');

=head2 auto_uid [$bool]

Gets or sets a boolean saying whether this entry should automatically
generate its own persistently unique UIDs.  Defaults to false.
Generally, you can just set this on your main L<Data::ICal> object when
you construct it; C<add_entry> automatically makes sure that sub-entries
end up with the same value as their parents.

=cut

__PACKAGE__->mk_accessors('auto_uid');

=head2 header

Returns the header line for the entry (including trailing newline).

=cut

sub header {
    my $self = shift;
    my %args = (
        crlf => CRLF,
        @_
    );
    return 'BEGIN:' . $self->ical_entry_type . $args{crlf};
}

=head2 footer

Returns the footer line for the entry (including trailing newline).

=cut

sub footer {
    my $self = shift;
    my %args = (
        crlf => CRLF,
        @_
    );
    return 'END:' . $self->ical_entry_type . $args{crlf};
}

# mapping of event types to class (under the Data::Ical::Event namespace)
my %_generic = (
    vevent    => 'Event',
    vtodo     => 'Todo',
    vjournal  => 'Journal',
    vfreebusy => 'FreeBusy',
    vtimezone => 'TimeZone',
    standard  => 'TimeZone::Standard',
    daylight  => 'TimeZone::Daylight',
);

=head2 parse_object

Translate a L<Text::vFile::asData> sub object into the appropriate
L<Data::iCal::Event> subtype.

=cut

# TODO: this is currently recursive which could blow the stack -
#       it might be worth refactoring to make it sequential
sub parse_object {
    my ( $self, $object ) = @_;

    my $type = $object->{type};

    my $new_self;

    # First check to see if it's generic long name just in case there
    # event turns out to be a VGENERIC entry type
    if ( my $class = $_generic{ lc($type) } ) {
        $new_self = $self->_parse_data_ical_generic( $class, $object );

        # then look for specific overrides
    } elsif ( my $sub = $self->can( '_parse_' . lc($type) ) ) {
        $new_self = $self->$sub($object);

        # complain
    } else {
        warn "Can't parse type $type yet";
        return;
    }

    # recurse through sub-objects
    foreach my $sub_object ( @{ $object->{objects} } ) {
        $new_self->parse_object($sub_object);
    }

    return $self;
}

# special because we want to use ourselves as the parent
sub _parse_vcalendar {
    my ( $self, $object ) = @_;
    $self->_parse_generic_event( $self, $object );
    return $self;
}

# mapping of action types to class (under the Data::Ical::Event::Alarm namespace)
my %_action_map = (
    AUDIO     => 'Audio',
    DISPLAY   => 'Display',
    EMAIL     => 'Email',
    PROCEDURE => 'Procedure',
    NONE      => 'None',
    URI       => 'URI',
);

# alarms have actions
sub _parse_valarm {
    my ( $parent, $object ) = @_;

    # ick
    my $action = $object->{properties}->{ACTION}->[0]->{value};
    die "Can't parse VALARM with action $action"
        unless exists $_action_map{$action};

    $action = $_action_map{$action};
    my $alarm_class = "Data::ICal::Entry::Alarm::$action";
    eval "require $alarm_class";
    die "Failed to require $alarm_class : $@" if $@;

    $alarm_class->import;
    my $alarm = $alarm_class->new;
    $parent->_parse_generic_event( $alarm, $object );
    $parent->add_entry($alarm);
    return $alarm;
}

# generic event handler
sub _parse_data_ical_generic {
    my ( $parent, $class, $object ) = @_;

    my $entry_class = "Data::ICal::Entry::$class";
    eval "require $entry_class";
    die "Failed to require $entry_class : $@" if $@;

    $entry_class->import;
    my $entry = $entry_class->new;
    $entry->vcal10($parent->vcal10);
    $parent->_parse_generic_event( $entry, $object );
    $parent->add_entry($entry);
    return $entry;
}

# handle transferring of properties
sub _parse_generic_event {
    my ( $parent, $entry, $object ) = @_;

    my $p = $object->{properties};
    for my $key ( sort keys %$p ) {
        foreach my $occurence (@{ $p->{$key} }) {
            my $prop;

            # Unescapes, but only in v2, and not if it's explicitly not TEXT
            if (not $parent->vcal10
                and (  not $occurence->{param}
                    or not defined $occurence->{param}{VALUE}
                    or $occurence->{param}{VALUE} eq "TEXT" )
                )
            {
                $occurence->{value} =~ s/\\([;,\\])/$1/g;
                $occurence->{value} =~ s/\\n/\n/ig;
            }

            # handle optional params and 'normal' key/value pairs
            # TODO: line wrapping?
            if ( $occurence->{param} ) {
                $prop = [ $occurence->{value}, $occurence->{param} ];
            } else {
                $prop = $occurence->{value};
            }
            $entry->add_property( lc($key) => $prop );
        }
    }
    return $entry;
}

=head1 AUTHOR

Best Practical Solutions, LLC E<lt>modules@bestpractical.comE<gt>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2005 - 2020, Best Practical Solutions, LLC.  All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=cut

1;