-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathics_create
executable file
·118 lines (92 loc) · 2.75 KB
/
ics_create
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
#! /usr/bin/perl -w
# Create a new ICS event
use Getopt::Long;
use Pod::Usage;
use Data::ICal;
use Data::UUID;
use DateTime::Format::Strptime;
use DateTime::Format::ICal;
=head1 NAME
ics_create -- Creates an iCal file
=head1 SYNOPSIS
ics_create --begin "22/11/2015 20:00" --end "22/11/2015 23:00" -s "Birthday party" --location "Home" <<EOF
You're all invited to a birthday party!
EOF
=cut
my $ical_parser = DateTime::Format::ICal->new;
my $parser = DateTime::Format::Strptime->new(
pattern => '%d/%m/%Y %H:%M',
on_error => 'undef',
);
my ($param_begin, $param_end, $param_subject, $param_location, $help);
GetOptions(
'begin=s' => \$param_begin,
'end=s' => \$param_end,
'subject=s' => \$param_subject,
'location=s' => \$param_location,
'help' => \$help,
) or die pod2usage();
die pod2usage(-verbose => 2) if defined $help;
################################################################################
# If we're missing fields, request them from stdin
# Turns a "dd/mm/yy hh:mm" to ICAL date
sub str2ical {
my $date = $parser->parse_datetime($_[0]);
return undef if not defined $date;
return $ical_parser->format_datetime($date);
}
sub prompt_date {
my ($prompt) = @_;
my $date;
do {
print STDERR "$prompt";
my $in = <>;
$date = str2ical($in);
warn "Illegal date\n" if not defined $date;
} until defined $date;
return $date;
}
if (defined $param_begin) {
$ical_begin = str2ical($param_begin);
} else {
$ical_begin = prompt_date "Event start date (dd/mm/yy hh:mm): ";
}
if (defined $param_end) {
$ical_end = str2ical($param_end);
} else {
$ical_end = prompt_date "Event end date (dd/mm/yy hh:mm): ";
}
die "Begin date illegal\n" unless defined $ical_begin;
die "End date illegal\n" unless defined $ical_end;
if (not defined $param_subject) {
print STDERR "Subject: ";
$param_subject = <>;
}
if (not defined $param_location) {
print STDERR "Location: ";
$param_location = <>;
}
################################################################################
# Consolidates the rest of stdin to description
my $description = "";
while (<>) {
$description .= $_;
}
################################################################################
# Create ICal date
my $ug = Data::UUID->new;
my $ical = Data::ICal->new();
use Data::ICal::Entry::Event;
my $event = Data::ICal::Entry::Event->new();
my $stamp = DateTime::Format::ICal->format_datetime(DateTime->now);
$event->add_properties(
summary => $param_subject,
description => $description,
dtstart => $ical_begin,
dtend => $ical_end,
dtstamp => $stamp,
location => $param_location,
uid => $ug->to_string($ug->create()),
);
$ical->add_entry($event);
print $ical->as_string;