-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathics_process
executable file
·227 lines (173 loc) · 6.17 KB
/
ics_process
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
#! /usr/bin/perl -w
use strict;
use File::Temp qw/tempfile/;
use Getopt::Long;
use Pod::Usage;
use MIME::Parser;
use MIME::Entity;
use Data::ICal;
use DateTime::Format::ICal;
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};
my $from_email = $cfg->{from_email};
######################################################
# Command line parsing
=head1 NAME
ics_process -- Process e-mail that contains iCal parts
=head1 SYNOPSIS
ics_process [--accept|--decline|--display] < multipart_email_text
ics_process --display < event.ics
=head1 DESCRIPTION
ics_process is designed to accept multi-part MIME emails,
extract iCal invitations (RFC5546), and reply to them with
an accept or decline e-mail. Additionaly, it submits
accepted meetings to a CalDAV server.
This makes it essentially possible to work with invitations
and keep your calendar automatically synchronised, directly
from Mutt.
Additionally it can display plain ICS files.
=head2 Setup
=over 4
=item Set up CalDAV and user in the configuration section of the script.
=item Install cadaver, and set its .netrc so that it can
connect to your CalDAV server without prompting for
passwords. If you have your own CA, add your CA certificate
to /usr/local/share/ca-certificates (with a .crt extension)
then run `update-ca-certificates`.
=item Add the following macros to your .muttrc:
macro pager A "|ics_process --accept"
macro pager D "|ics_process --decline"
macro pager S "|ics_process --display"
=back
Now, when viewing an e-mail that contains an invitation,
pressing 'A' will add the event in your calendar and send an
acceptation e-mail, while pressing 'D' will send a decline
e-mail.
=cut
my ($param_accept, $param_decline, $param_display, $help);
GetOptions(
'accept' => \$param_accept,
'decline' => \$param_decline,
'display' => \$param_display,
) or die pod2usage();
die pod2usage(-verbose => 2) if defined $help or defined $param_accept and defined $param_decline or not defined $param_accept and not defined $param_decline and not defined $param_display;
######################################################
# Submits a ICS file to owncloud
sub oc_submit {
my ($data) = @_;
my ($fh, $filename) = tempfile("/tmp/ics_process_XXXXXX", UNLINK => 1);
print "posting $filename\n";
print $fh $data;
close $fh;
`cadaver $oc_server/remote.php/caldav/calendars/$oc_user/personal <<EOC
put $filename
quit
EOC`;
}
######################################################
# Turn a ICal string (any TZ) to a human string, local TZ
# TZ="Australia/Brisbane" 20180320T200000Z => Tue 20 March 12:00
sub ical2local {
my ($ical) = @_;
my $dt = DateTime::Format::ICal->parse_datetime($ical);
$dt->set_time_zone('local');
return $dt->strftime("%a %d %B %H:%M");
}
######################################################
sub ics_display {
my ($data) = @_;
my $ical = Data::ICal->new(data => $data);
foreach my $entry (@{$ical->entries}) {
my $start = $entry->property('dtstart');
next unless defined $start;
$start = ical2local($start->[0]->value);
my $end = $entry->property('dtend');
$end = ical2local($end->[0]->value);
my $organiser = defined $entry->property('organiser') ? $entry->property('organizer')->[0]->value : "";
my $location = defined $entry->property('location') ? $entry->property('location')->[0]->value : "";
my $attendees = $entry->property('attendee');
my $attendee = join "\n\t ", map { $_->value } @$attendees;
my $subject = $entry->property('summary')->[0]->value;
my $data = $entry->property('description')->[0]->value;
print <<EOF;
Organiser: $organiser
Attendee: $attendee
Subject: $subject
Location: $location
Start: $start
Ends: $end
$data
EOF
}
}
######################################################
# Gobble up entire input from stdin
my $parser = new MIME::Parser;
undef $/;
my $raw_input = <STDIN>;
if ($raw_input =~ /^BEGIN:VCALENDAR/) {
# Plain ICS file: display and leave
ics_display($raw_input);
exit(0);
}
# Try parsing MIME
my $input = $parser->parse_data($raw_input);
my $reply_to;
my ($accept, $entity, $ical_data);
my @parts = $input->parts;
# If there are no parts, process the mail itself, as it may
# be a text/calendar.
push @parts, $input unless @parts;
if (@parts) {
foreach my $part (@parts) {
my $type = $part->effective_type;
# If this envelope contains parts, process parts instead
# (This goes recursively searching for text/calendar or
# ICS attachements).
# We can't just rely on "multipart" MIME as other
# classes may be multipart, e.g. "message/rfc722"
if ($part->parts) {
push @parts, $part->parts;
next;
}
# Some applications send ICS as # 'application/octet-stream'... fall back on attachement filename
my $filename = $part->head->recommended_filename // "";
if ($type eq "text/calendar" or $type =~ /applica.*\/ics/ or $filename =~ /ics$/) {
my $body = $part->bodyhandle or die "Unable to open calendar part\n";
$ical_data = $body->as_string;
last;
}
}
}
ics_display($ical_data) if $param_display;
# If accept or decline, send appropriate e-mail
if ($param_accept or $param_decline) {
$accept = $param_accept;
my $output = MIME::Entity->build(
From => $from_email,
To => $input->get('From'),
Subject => (($accept ? "Accepted: ": "Declined: ").$input->get('Subject')),
Data => [ "" ],
);
my $action = $accept ? "ACCEPTED" : "DECLINED";
$ical_data =~ s/NEEDS-ACTION/$action/;
$ical_data =~ s/METHOD:REQUEST/METHOD:REPLY/;
($reply_to) = $ical_data =~ /MAILTO:(.*)/;
$output->attach(
Type => 'text/calendar',
Encoding => 'base64',
Data => $ical_data,
);
$output->replace('To', $reply_to) if defined $reply_to;
$output->send;
}
# Submit to calendar
if ($accept) {
oc_submit $ical_data;
}