-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathics_put
executable file
·191 lines (144 loc) · 4.41 KB
/
ics_put
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
#! /usr/bin/perl -w
# Simply uploads a ICS to a calendar
use strict;
use File::Temp qw/tempfile/;
use Getopt::Long;
use Pod::Usage;
use Data::UUID;
use YAML qw/LoadFile/;
######################################################
# Configuration
# Owncloud URL and user (password is in .netrc, used by
# cadaver)
my $cfg = LoadFile("$ENV{HOME}/.ics_toolsrc");
my $oc_server = $cfg->{oc_server};
my $oc_user = $cfg->{oc_user};
######################################################
# Command line parsing
=head1 NAME
ics_put -- Imports one or several ICS calendar into Owncloud
=head1 SYNOPSIS
ics_put [--verbose] [[--replace] --calendar=my_cal] <event.ics> [<event2.ics> ...]
=head1 DESCRIPTION
I<ics_put> splits an ICS file into atomic events, names the
files according to the UUID of the event, then uploads them
all to an Owncloud calendar.
=head1 OPTIONS
=over 4
=item I<--calendar>
Specifies the calendar in which to add the events. Defaults
to 'personal', which is the default calendar name in
Owncloud.
=item I<--replace>
Removes the contents of the calendar before importing the
events. For safety, you must specify I<--calendar> to use
this option.
=back
=cut
my ($verbose, $param_calendar, $param_replace, $help);
GetOptions(
'verbose' => \$verbose,
'calendar=s' => \$param_calendar,
'replace' => \$param_replace,
'help' => \$help,
) or die pod2usage();
die pod2usage(-verbose => 2) if defined $help;
die "Cowardly refusing to destroy unspecified calendar\n" if defined $param_replace and not defined $param_calendar;
$param_calendar //= "personal";
# Split a large VCALENDAR into unit VCALENDAR containing
# only one VEVENT (owncloud only accepts one event per file)
# Input: absolute path to basename for new files , input ICS
# Output: list of split files
sub ics_split {
my ($in_file) = @_;
my @out;
my $cnt = 1;
open my $in, $in_file or die "$in_file: $!\n";
while (<$in>) {
chomp;
last if /^BEGIN:VEVENT/;
}
my $data = "BEGIN:VEVENT\n";
while (<$in>) {
chomp;
if (/^END:VEVENT/) {
chop $data;
$data =~ /^UID:(.*)/m;
my $uid = $1;
# Add an UID if there isn't one
my $ug = new Data::UUID;
if (defined $uid) {
chop $uid;
} else {
$uid = $ug->create_str();
$data .= "\nUID: $uid";
}
# Outlook seems to sometimes produce several
# events with the same UID (what's 'U' in UID
# for again?), but I need my files
# to really be unique...
my $f_uid = $ug->create_str();
my $filename = "/tmp/$f_uid.ics";
open my $fh, "> $filename" or die "$filename: $!\n";
$cnt++;
print $fh <<EOF;
BEGIN:VCALENDAR
PRODID:ics_split
VERSION:2.0
$data
END:VEVENT
END:VCALENDAR
EOF
push @out, $filename;
$data = "";
} else {
# Fix date if producer didn't follow the standard
# yyyy-mm-ddThh:mm:ss.000000 =>
# yyyymmddThhmmss
if (/DTSTART:(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/) {
warn "fixing date DTSTART:$1$2$3T$4$5$6\n";
$_ = "DTSTART:$1$2$3T$4$5$6";
}
if (/DTEND:(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/) {
warn "fixing date DTSTART:$1$2$3T$4$5$6\n";
$_ = "DTEND:$1$2$3T$4$5$6";
}
$data .= "$_\n";
}
}
return @out;
}
######################################################
sub cadaver_cmd
{
my ($cmd) = @_;
my $out = `cadaver $oc_server/remote.php/caldav/calendars/$oc_user/$param_calendar <<EOC
$cmd`;
warn $out if $verbose or $out =~ /[45]00/; # Owncloud fails with 400 bad request or 500 internal error
}
######################################################
# Erase calendar if needed
if ($param_replace) {
warn "Removing events in $param_calendar\n" if $verbose;
cadaver_cmd(<<EOC
rm *
quit
EOC
);
}
######################################################
# Submits a ICS file to owncloud
my $filename;
while ($filename = shift) {
warn "Splitting $filename\n" if $verbose;
my @files = ics_split $filename;
for my $f (@files) {
warn "Importing $f\n" if $verbose;
cadaver_cmd(<<EOC
put $f
quit
EOC
);
unlink $f or die "$f: $!\n";
}
}