gnus-msg.el (gnus-setup-message): Fix last commit
[gnus] / lisp / gnus-icalendar.el
1 ;;; gnus-icalendar.el --- reply to iCalendar meeting requests
2
3 ;; Copyright (C) 2013  Free Software Foundation, Inc.
4
5 ;; Author: Jan Tatarik <Jan.Tatarik@gmail.com>
6 ;; Keywords: mail, icalendar, org
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; To install:
24 ;; (require 'gnus-icalendar)
25 ;; (gnus-icalendar-setup)
26
27 ;; to enable optional iCalendar->Org sync functionality
28 ;; NOTE: both the capture file and the headline(s) inside must already exist
29 ;; (setq gnus-icalendar-org-capture-file "~/org/notes.org")
30 ;; (setq gnus-icalendar-org-capture-headline '("Calendar"))
31 ;; (gnus-icalendar-org-setup)
32
33
34 ;;; Code:
35
36 (require 'icalendar)
37 (require 'eieio)
38 (require 'gmm-utils)
39 (require 'mm-decode)
40 (require 'gnus-sum)
41
42 (eval-when-compile (require 'cl))
43
44 (defun gnus-icalendar-find-if (pred seq)
45   (catch 'found
46     (while seq
47       (when (funcall pred (car seq))
48         (throw 'found (car seq)))
49       (pop seq))))
50
51 ;;;
52 ;;; ical-event
53 ;;;
54
55 (defclass gnus-icalendar-event ()
56   ((organizer :initarg :organizer
57               :accessor gnus-icalendar-event:organizer
58               :initform ""
59               :type (or null string))
60    (summary :initarg :summary
61             :accessor gnus-icalendar-event:summary
62             :initform ""
63             :type (or null string))
64    (description :initarg :description
65                 :accessor gnus-icalendar-event:description
66                 :initform ""
67                 :type (or null string))
68    (location :initarg :location
69              :accessor gnus-icalendar-event:location
70              :initform ""
71              :type (or null string))
72    (start-time :initarg :start-time
73           :accessor gnus-icalendar-event:start-time
74           :initform ""
75           :type (or null t))
76    (end-time :initarg :end-time
77         :accessor gnus-icalendar-event:end-time
78         :initform ""
79         :type (or null t))
80    (recur :initarg :recur
81           :accessor gnus-icalendar-event:recur
82           :initform ""
83           :type (or null string))
84    (uid :initarg :uid
85         :accessor gnus-icalendar-event:uid
86         :type string)
87    (method :initarg :method
88            :accessor gnus-icalendar-event:method
89            :initform "PUBLISH"
90            :type (or null string))
91    (rsvp :initarg :rsvp
92          :accessor gnus-icalendar-event:rsvp
93          :initform nil
94          :type (or null boolean))
95    (participation-type :initarg :participation-type
96          :accessor gnus-icalendar-event:participation-type
97          :initform 'non-participant
98          :type (or null t))
99    (req-participants :initarg :req-participants
100          :accessor gnus-icalendar-event:req-participants
101          :initform nil
102          :type (or null t))
103    (opt-participants :initarg :opt-participants
104          :accessor gnus-icalendar-event:opt-participants
105          :initform nil
106          :type (or null t)))
107   "generic iCalendar Event class")
108
109 (defclass gnus-icalendar-event-request (gnus-icalendar-event)
110   nil
111   "iCalendar class for REQUEST events")
112
113 (defclass gnus-icalendar-event-cancel (gnus-icalendar-event)
114   nil
115   "iCalendar class for CANCEL events")
116
117 (defclass gnus-icalendar-event-reply (gnus-icalendar-event)
118   nil
119   "iCalendar class for REPLY events")
120
121 (defmethod gnus-icalendar-event:recurring-p ((event gnus-icalendar-event))
122   "Return t if EVENT is recurring."
123   (not (null (gnus-icalendar-event:recur event))))
124
125 (defmethod gnus-icalendar-event:recurring-freq ((event gnus-icalendar-event))
126   "Return recurring frequency of EVENT."
127   (let ((rrule (gnus-icalendar-event:recur event)))
128     (string-match "FREQ=\\([[:alpha:]]+\\)" rrule)
129     (match-string 1 rrule)))
130
131 (defmethod gnus-icalendar-event:recurring-interval ((event gnus-icalendar-event))
132   "Return recurring interval of EVENT."
133   (let ((rrule (gnus-icalendar-event:recur event))
134         (default-interval 1))
135
136     (string-match "INTERVAL=\\([[:digit:]]+\\)" rrule)
137     (or (match-string 1 rrule)
138         default-interval)))
139
140 (defmethod gnus-icalendar-event:start ((event gnus-icalendar-event))
141   (format-time-string "%Y-%m-%d %H:%M" (gnus-icalendar-event:start-time event)))
142
143 (defun gnus-icalendar-event--decode-datefield (ical field)
144   (let* ((date (icalendar--get-event-property ical field))
145          (date-props (icalendar--get-event-property-attributes ical field))
146          (tz (plist-get date-props 'TZID)))
147
148     (date-to-time (timezone-make-date-arpa-standard date nil tz))))
149
150 (defun gnus-icalendar-event--find-attendee (ical name-or-email)
151   (let* ((event (car (icalendar--all-events ical)))
152          (event-props (caddr event)))
153     (gmm-labels ((attendee-name (att) (plist-get (cadr att) 'CN))
154                  (attendee-email (att)
155                    (replace-regexp-in-string "^.*MAILTO:" "" (caddr att)))
156                  (attendee-prop-matches-p (prop)
157                    (and (eq (car prop) 'ATTENDEE)
158                         (or (member (attendee-name prop) name-or-email)
159                             (let ((att-email (attendee-email prop)))
160                               (gnus-icalendar-find-if (lambda (email)
161                                                         (string-match email att-email))
162                                                       name-or-email))))))
163
164       (gnus-icalendar-find-if #'attendee-prop-matches-p event-props))))
165
166 (defun gnus-icalendar-event--get-attendee-names (ical)
167   (let* ((event (car (icalendar--all-events ical)))
168          (attendee-props (gnus-remove-if-not
169                           (lambda (p) (eq (car p) 'ATTENDEE))
170                           (caddr event))))
171
172     (gmm-labels ((attendee-role (prop) (plist-get (cadr prop) 'ROLE))
173                  (attendee-name (prop) (plist-get (cadr prop) 'CN))
174                  (attendees-by-type (type)
175                    (gnus-remove-if-not
176                     (lambda (p) (string= (attendee-role p) type))
177                     attendee-props))
178                  (attendee-names-by-type (type)
179                     (mapcar #'attendee-name (attendees-by-type type))))
180
181       (list
182        (attendee-names-by-type "REQ-PARTICIPANT")
183        (attendee-names-by-type "OPT-PARTICIPANT")))))
184
185 (defun gnus-icalendar-event-from-ical (ical &optional attendee-name-or-email)
186   (let* ((event (car (icalendar--all-events ical)))
187          (organizer (replace-regexp-in-string
188                      "^.*MAILTO:" ""
189                      (or (icalendar--get-event-property event 'ORGANIZER) "")))
190          (prop-map '((summary . SUMMARY)
191                      (description . DESCRIPTION)
192                      (location . LOCATION)
193                      (recur . RRULE)
194                      (uid . UID)))
195          (method (caddr (assoc 'METHOD (caddr (car (nreverse ical))))))
196          (attendee (when attendee-name-or-email
197                      (gnus-icalendar-event--find-attendee ical attendee-name-or-email)))
198          (attendee-names (gnus-icalendar-event--get-attendee-names ical))
199          (role (plist-get (cadr attendee) 'ROLE))
200          (participation-type (pcase role
201                               ("REQ-PARTICIPANT" 'required)
202                               ("OPT-PARTICIPANT" 'optional)
203                               (_                 'non-participant)))
204          (args (list :method method
205                      :organizer organizer
206                      :start-time (gnus-icalendar-event--decode-datefield event 'DTSTART)
207                      :end-time (gnus-icalendar-event--decode-datefield event 'DTEND)
208                      :rsvp (string= (plist-get (cadr attendee) 'RSVP) "TRUE")
209                      :participation-type participation-type
210                      :req-participants (car attendee-names)
211                      :opt-participants (cadr attendee-names)))
212          (event-class (cond
213                        ((string= method "REQUEST") 'gnus-icalendar-event-request)
214                        ((string= method "CANCEL") 'gnus-icalendar-event-cancel)
215                        ((string= method "REPLY") 'gnus-icalendar-event-reply)
216                        (t 'gnus-icalendar-event))))
217
218     (gmm-labels ((map-property (prop)
219                    (let ((value (icalendar--get-event-property event prop)))
220                      (when value
221                        ;; ugly, but cannot get
222                        ;;replace-regexp-in-string work with "\\" as
223                        ;;REP, plus we should also handle "\\;"
224                        (replace-regexp-in-string
225                         "\\\\," ","
226                         (replace-regexp-in-string
227                          "\\\\n" "\n" (substring-no-properties value))))))
228                  (accumulate-args (mapping)
229                    (destructuring-bind (slot . ical-property) mapping
230                      (setq args (append (list
231                                          (intern (concat ":" (symbol-name slot)))
232                                          (map-property ical-property))
233                                         args)))))
234
235       (mapc #'accumulate-args prop-map)
236       (apply 'make-instance event-class args))))
237
238 (defun gnus-icalendar-event-from-buffer (buf &optional attendee-name-or-email)
239   "Parse RFC5545 iCalendar in buffer BUF and return an event object.
240
241 Return a gnus-icalendar-event object representing the first event
242 contained in the invitation. Return nil for calendars without an event entry.
243
244 ATTENDEE-NAME-OR-EMAIL is a list of strings that will be matched
245 against the event's attendee names and emails. Invitation rsvp
246 status will be retrieved from the first matching attendee record."
247   (let ((ical (with-current-buffer (icalendar--get-unfolded-buffer (get-buffer buf))
248                 (goto-char (point-min))
249                 (icalendar--read-element nil nil))))
250
251     (when ical
252       (gnus-icalendar-event-from-ical ical attendee-name-or-email))))
253
254 ;;;
255 ;;; gnus-icalendar-event-reply
256 ;;;
257
258 (defun gnus-icalendar-event--build-reply-event-body (ical-request status identities)
259   (let ((summary-status (capitalize (symbol-name status)))
260         (attendee-status (upcase (symbol-name status)))
261         reply-event-lines)
262     (gmm-labels ((update-summary (line)
263                    (if (string-match "^[^:]+:" line)
264                        (replace-match (format "\\&%s: " summary-status) t nil line)
265                      line))
266                  (update-dtstamp ()
267                    (format-time-string "DTSTAMP:%Y%m%dT%H%M%SZ" nil t))
268                  (attendee-matches-identity (line)
269                    (gnus-icalendar-find-if (lambda (name) (string-match-p name line))
270                                            identities))
271                  (update-attendee-status (line)
272                    (when (and (attendee-matches-identity line)
273                               (string-match "\\(PARTSTAT=\\)[^;]+" line))
274                      (replace-match (format "\\1%s" attendee-status) t nil line)))
275                  (process-event-line (line)
276                    (when (string-match "^\\([^;:]+\\)" line)
277                      (let* ((key (match-string 0 line))
278                             ;; NOTE: not all of the below fields are mandatory,
279                             ;; but they are often present in other clients'
280                             ;; replies. Can be helpful for debugging, too.
281                             (new-line
282                              (cond
283                               ((string= key "ATTENDEE") (update-attendee-status line))
284                               ((string= key "SUMMARY") (update-summary line))
285                               ((string= key "DTSTAMP") (update-dtstamp))
286                               ((member key '("ORGANIZER" "DTSTART" "DTEND"
287                                              "LOCATION" "DURATION" "SEQUENCE"
288                                              "RECURRENCE-ID" "UID")) line)
289                               (t nil))))
290                        (when new-line
291                          (push new-line reply-event-lines))))))
292
293       (mapc #'process-event-line (split-string ical-request "\n"))
294
295       (unless (gnus-icalendar-find-if (lambda (x) (string-match "^ATTENDEE" x))
296                           reply-event-lines)
297         (error "Could not find an event attendee matching given identity"))
298
299       (mapconcat #'identity `("BEGIN:VEVENT"
300                               ,@(nreverse reply-event-lines)
301                               "END:VEVENT")
302                  "\n"))))
303
304 (defun gnus-icalendar-event-reply-from-buffer (buf status identities)
305   "Build a calendar event reply for request contained in BUF.
306 The reply will have STATUS (`accepted', `tentative' or  `declined').
307 The reply will be composed for attendees matching any entry
308 on the IDENTITIES list."
309   (gmm-labels ((extract-block (blockname)
310                (save-excursion
311                  (let ((block-start-re (format "^BEGIN:%s" blockname))
312                        (block-end-re (format "^END:%s" blockname))
313                        start)
314                    (when (re-search-forward block-start-re nil t)
315                      (setq start (line-beginning-position))
316                      (re-search-forward block-end-re)
317                      (buffer-substring-no-properties start (line-end-position)))))))
318
319     (let (zone event)
320       (with-current-buffer (icalendar--get-unfolded-buffer (get-buffer buf))
321         (goto-char (point-min))
322         (setq zone (extract-block "VTIMEZONE")
323               event (extract-block "VEVENT")))
324
325       (when event
326         (let ((contents (list "BEGIN:VCALENDAR"
327                               "METHOD:REPLY"
328                               "PRODID:Gnus"
329                               "VERSION:2.0"
330                               zone
331                               (gnus-icalendar-event--build-reply-event-body event status identities)
332                               "END:VCALENDAR")))
333
334           (mapconcat #'identity (delq nil contents) "\n"))))))
335
336 ;;;
337 ;;; gnus-icalendar-org
338 ;;;
339 ;;; TODO: this is an optional feature, and it's only available with org-mode
340 ;;; 7+, so will need to properly handle emacsen with no/outdated org-mode
341
342 (require 'org)
343 (require 'org-capture)
344
345 (defgroup gnus-icalendar-org nil
346   "Settings for Calendar Event gnus/org integration."
347   :group 'gnus-icalendar
348   :prefix "gnus-icalendar-org-")
349
350 (defcustom gnus-icalendar-org-capture-file nil
351   "Target Org file for storing captured calendar events."
352   :type '(choice (const nil) file)
353   :group 'gnus-icalendar-org)
354
355 (defcustom gnus-icalendar-org-capture-headline nil
356   "Target outline in `gnus-icalendar-org-capture-file' for storing captured events."
357   :type '(repeat string)
358   :group 'gnus-icalendar-org)
359
360 (defcustom gnus-icalendar-org-template-name "used by gnus-icalendar-org"
361   "Org-mode template name."
362   :type '(string)
363   :group 'gnus-icalendar-org)
364
365 (defcustom gnus-icalendar-org-template-key "#"
366   "Org-mode template hotkey."
367   :type '(string)
368   :group 'gnus-icalendar-org)
369
370 (defvar gnus-icalendar-org-enabled-p nil)
371
372
373 (defmethod gnus-icalendar-event:org-repeat ((event gnus-icalendar-event))
374   "Return `org-mode' timestamp repeater string for recurring EVENT.
375 Return nil for non-recurring EVENT."
376   (when (gnus-icalendar-event:recurring-p event)
377     (let* ((freq-map '(("HOURLY" . "h")
378                        ("DAILY" . "d")
379                        ("WEEKLY" . "w")
380                        ("MONTHLY" . "m")
381                        ("YEARLY" . "y")))
382            (org-freq (cdr (assoc (gnus-icalendar-event:recurring-freq event) freq-map))))
383
384       (when org-freq
385         (format "+%s%s" (gnus-icalendar-event:recurring-interval event) org-freq)))))
386
387 (defmethod gnus-icalendar-event:org-timestamp ((event gnus-icalendar-event))
388   "Build `org-mode' timestamp from EVENT start/end dates and recurrence info."
389   (let* ((start (gnus-icalendar-event:start-time event))
390          (end (gnus-icalendar-event:end-time event))
391          (start-date (format-time-string "%Y-%m-%d %a" start))
392          (start-time (format-time-string "%H:%M" start))
393          (start-at-midnight (string= start-time "00:00"))
394          (end-date (format-time-string "%Y-%m-%d %a" end))
395          (end-time (format-time-string "%H:%M" end))
396          (end-at-midnight (string= end-time "00:00"))
397          (start-end-date-diff (/ (float-time (time-subtract
398                                         (date-to-time end-date)
399                                         (date-to-time start-date)))
400                                  86400))
401          (org-repeat (gnus-icalendar-event:org-repeat event))
402          (repeat (if org-repeat (concat " " org-repeat) ""))
403          (time-1-day '(0 86400)))
404
405     ;; NOTE: special care is needed with appointments ending at midnight
406     ;; (typically all-day events): the end time has to be changed to 23:59 to
407     ;; prevent org agenda showing the event on one additional day
408     (cond
409      ;; start/end midnight
410      ;; A 0:0 - A+1 0:0 -> A
411      ;; A 0:0 - A+n 0:0 -> A - A+n-1
412      ((and start-at-midnight end-at-midnight) (if (> start-end-date-diff 1)
413                                                   (let ((end-ts (format-time-string "%Y-%m-%d %a" (time-subtract end time-1-day))))
414                                                     (format "<%s>--<%s>" start-date end-ts))
415                                                 (format "<%s%s>" start-date repeat)))
416      ;; end midnight
417      ;; A .:. - A+1 0:0 -> A .:.-23:59
418      ;; A .:. - A+n 0:0 -> A .:. - A_n-1
419      (end-at-midnight (if (= start-end-date-diff 1)
420                           (format "<%s %s-23:59%s>" start-date start-time repeat)
421                         (let ((end-ts (format-time-string "%Y-%m-%d %a" (time-subtract end time-1-day))))
422                           (format "<%s %s>--<%s>" start-date start-time end-ts))))
423      ;; start midnight
424      ;; A 0:0 - A .:. -> A 0:0-.:. (default 1)
425      ;; A 0:0 - A+n .:. -> A - A+n .:.
426      ((and start-at-midnight
427            (plusp start-end-date-diff)) (format "<%s>--<%s %s>" start-date end-date end-time))
428      ;; default
429      ;; A .:. - A .:. -> A .:.-.:.
430      ;; A .:. - B .:.
431      ((zerop start-end-date-diff) (format "<%s %s-%s%s>" start-date start-time end-time repeat))
432      (t (format "<%s %s>--<%s %s>" start-date start-time end-date end-time)))))
433
434 (defun gnus-icalendar--format-summary-line (summary &optional location)
435   (if location
436       (format "%s (%s)" summary location)
437     (format "%s" summary)))
438
439
440 (defun gnus-icalendar--format-participant-list (participants)
441   (mapconcat #'identity participants ", "))
442
443 ;; TODO: make the template customizable
444 (defmethod gnus-icalendar-event->org-entry ((event gnus-icalendar-event) reply-status)
445   "Return string with new `org-mode' entry describing EVENT."
446   (with-temp-buffer
447     (org-mode)
448     (with-slots (organizer summary description location
449                            recur uid) event
450       (let* ((reply (if reply-status (capitalize (symbol-name reply-status))
451                       "Not replied yet"))
452              (props `(("ICAL_EVENT" . "t")
453                       ("ID" . ,uid)
454                       ("DT" . ,(gnus-icalendar-event:org-timestamp event))
455                       ("ORGANIZER" . ,(gnus-icalendar-event:organizer event))
456                       ("LOCATION" . ,(gnus-icalendar-event:location event))
457                       ("PARTICIPATION_TYPE" . ,(symbol-name (gnus-icalendar-event:participation-type event)))
458                       ("REQ_PARTICIPANTS" . ,(gnus-icalendar--format-participant-list (gnus-icalendar-event:req-participants event)))
459                       ("OPT_PARTICIPANTS" . ,(gnus-icalendar--format-participant-list (gnus-icalendar-event:opt-participants event)))
460                       ("RRULE" . ,(gnus-icalendar-event:recur event))
461                       ("REPLY" . ,reply))))
462
463         (insert (format "* %s\n\n"
464                         (gnus-icalendar--format-summary-line summary location)))
465         (mapc (lambda (prop)
466                 (org-entry-put (point) (car prop) (cdr prop)))
467               props))
468
469       (when description
470         (save-restriction
471           (narrow-to-region (point) (point))
472           (insert description)
473           (indent-region (point-min) (point-max) 2)
474           (fill-region (point-min) (point-max))))
475
476       (buffer-string))))
477
478 (defun gnus-icalendar--deactivate-org-timestamp (ts)
479   (replace-regexp-in-string "[<>]"
480                             (lambda (m) (cond ((string= m "<") "[")
481                                               ((string= m ">") "]")))
482                             ts))
483
484 (defun gnus-icalendar-find-org-event-file (event &optional org-file)
485   "Return the name of the file containing EVENT org entry.
486 Return nil when not found.
487
488 All org agenda files are searched for the EVENT entry.  When
489 the optional ORG-FILE argument is specified, only that one file
490 is searched."
491   (let ((uid (gnus-icalendar-event:uid event))
492         (files (or org-file (org-agenda-files t 'ifmode))))
493     (gmm-labels
494         ((find-event-in (file)
495            (org-check-agenda-file file)
496            (with-current-buffer (find-file-noselect file)
497              (let ((event-pos (org-find-entry-with-id uid)))
498                (when (and event-pos
499                           (string= (cdr (assoc "ICAL_EVENT" (org-entry-properties event-pos)))
500                                    "t"))
501                  (throw 'found file))))))
502
503       (gnus-icalendar-find-if #'find-event-in files))))
504
505
506 (defun gnus-icalendar--show-org-event (event &optional org-file)
507   (let ((file (gnus-icalendar-find-org-event-file event org-file)))
508     (when file
509       (switch-to-buffer (find-file file))
510       (goto-char (org-find-entry-with-id (gnus-icalendar-event:uid event)))
511       (org-show-entry))))
512
513
514 (defun gnus-icalendar--update-org-event (event reply-status &optional org-file)
515   (let ((file (gnus-icalendar-find-org-event-file event org-file)))
516     (when file
517       (with-current-buffer (find-file-noselect file)
518         (with-slots (uid summary description organizer location recur
519                          participation-type req-participants opt-participants) event
520           (let ((event-pos (org-find-entry-with-id uid)))
521             (when event-pos
522               (goto-char event-pos)
523
524               ;; update the headline, keep todo, priority and tags, if any
525               (save-excursion
526                 (let* ((priority (org-entry-get (point) "PRIORITY"))
527                        (headline (delq nil (list
528                                             (org-entry-get (point) "TODO")
529                                             (when priority (format "[#%s]" priority))
530                                             (gnus-icalendar--format-summary-line summary location)
531                                             (org-entry-get (point) "TAGS")))))
532
533                   (re-search-forward "^\\*+ " (line-end-position))
534                   (delete-region (point) (line-end-position))
535                   (insert (mapconcat #'identity headline " "))))
536
537               ;; update props and description
538               (let ((entry-end (org-entry-end-position))
539                     (entry-outline-level (org-outline-level)))
540
541                 ;; delete body of the entry, leave org drawers intact
542                 (save-restriction
543                   (org-narrow-to-element)
544                   (goto-char entry-end)
545                   (re-search-backward "^[\t ]*:END:")
546                   (forward-line)
547                   (delete-region (point) entry-end))
548
549                 ;; put new event description in the entry body
550                 (when description
551                   (save-restriction
552                     (narrow-to-region (point) (point))
553                     (insert "\n" (replace-regexp-in-string "[\n]+$" "\n" description) "\n")
554                     (indent-region (point-min) (point-max) (1+ entry-outline-level))
555                     (fill-region (point-min) (point-max))))
556
557                 ;; update entry properties
558                 (org-entry-put event-pos "DT" (gnus-icalendar-event:org-timestamp event))
559                 (org-entry-put event-pos "ORGANIZER" organizer)
560                 (org-entry-put event-pos "LOCATION" location)
561                 (org-entry-put event-pos "PARTICIPATION_TYPE" (symbol-name participation-type))
562                 (org-entry-put event-pos "REQ_PARTICIPANTS" (gnus-icalendar--format-participant-list req-participants))
563                 (org-entry-put event-pos "OPT_PARTICIPANTS" (gnus-icalendar--format-participant-list opt-participants))
564                 (org-entry-put event-pos "RRULE" recur)
565                 (when reply-status (org-entry-put event-pos "REPLY"
566                                                   (capitalize (symbol-name reply-status))))
567                 (save-buffer)))))))))
568
569
570 (defun gnus-icalendar--cancel-org-event (event &optional org-file)
571   (let ((file (gnus-icalendar-find-org-event-file event org-file)))
572     (when file
573       (with-current-buffer (find-file-noselect file)
574         (let ((event-pos (org-find-entry-with-id (gnus-icalendar-event:uid event))))
575           (when event-pos
576             (let ((ts (org-entry-get event-pos "DT")))
577               (when ts
578                 (org-entry-put event-pos "DT" (gnus-icalendar--deactivate-org-timestamp ts))
579                 (save-buffer)))))))))
580
581
582 (defun gnus-icalendar--get-org-event-reply-status (event &optional org-file)
583   (let ((file (gnus-icalendar-find-org-event-file event org-file)))
584     (when file
585       (save-excursion
586         (with-current-buffer (find-file-noselect file)
587           (let ((event-pos (org-find-entry-with-id (gnus-icalendar-event:uid event))))
588             (org-entry-get event-pos "REPLY")))))))
589
590
591 (defun gnus-icalendar-insinuate-org-templates ()
592   (unless (gnus-icalendar-find-if (lambda (x) (string= (cadr x) gnus-icalendar-org-template-name))
593                       org-capture-templates)
594     (setq org-capture-templates
595           (append `((,gnus-icalendar-org-template-key
596                      ,gnus-icalendar-org-template-name
597                      entry
598                      (file+olp ,gnus-icalendar-org-capture-file ,@gnus-icalendar-org-capture-headline)
599                      "%i"
600                      :immediate-finish t))
601                   org-capture-templates))
602
603     ;; hide the template from interactive template selection list
604     ;; (org-capture)
605     ;; NOTE: doesn't work when capturing from string
606     ;; (when (boundp 'org-capture-templates-contexts)
607     ;;   (push `(,gnus-icalendar-org-template-key "" ((in-mode . "gnus-article-mode")))
608     ;;         org-capture-templates-contexts))
609     ))
610
611 (defun gnus-icalendar:org-event-save (event reply-status)
612   (with-temp-buffer
613     (org-capture-string (gnus-icalendar-event->org-entry event reply-status)
614                         gnus-icalendar-org-template-key)))
615
616 (defun gnus-icalendar-show-org-agenda (event)
617   (let* ((time-delta (time-subtract (gnus-icalendar-event:end-time event)
618                                     (gnus-icalendar-event:start-time event)))
619          (duration-days (1+ (/ (+ (* (car time-delta) (expt 2 16))
620                                   (cadr time-delta))
621                                86400))))
622
623     (org-agenda-list nil (gnus-icalendar-event:start event) duration-days)))
624
625 (defmethod gnus-icalendar-event:sync-to-org ((event gnus-icalendar-event-request) reply-status)
626   (if (gnus-icalendar-find-org-event-file event)
627       (gnus-icalendar--update-org-event event reply-status)
628     (gnus-icalendar:org-event-save event reply-status)))
629
630 (defmethod gnus-icalendar-event:sync-to-org ((event gnus-icalendar-event-cancel) reply-status)
631   (when (gnus-icalendar-find-org-event-file event)
632     (gnus-icalendar--cancel-org-event event)))
633
634 (defun gnus-icalendar-org-setup ()
635   (if (and gnus-icalendar-org-capture-file gnus-icalendar-org-capture-headline)
636       (progn
637         (gnus-icalendar-insinuate-org-templates)
638         (setq gnus-icalendar-org-enabled-p t))
639     (message "Cannot enable Calendar->Org: missing capture file, headline")))
640
641 ;;;
642 ;;; gnus-icalendar
643 ;;;
644
645 (defgroup gnus-icalendar nil
646   "Settings for inline display of iCalendar invitations."
647   :group 'gnus-article
648   :prefix "gnus-icalendar-")
649
650 (defcustom gnus-icalendar-reply-bufname "*CAL*"
651   "Buffer used for building iCalendar invitation reply."
652   :type '(string)
653   :group 'gnus-icalendar)
654
655 (defcustom gnus-icalendar-additional-identities nil
656   "We need to know your identity to make replies to calendar requests work.
657
658 Gnus will only offer you the Accept/Tentative/Decline buttons for
659 calendar events if any of your identities matches at least one
660 RSVP participant.
661
662 Your identity is guessed automatically from the variables `user-full-name',
663 `user-mail-address', and `gnus-ignored-from-addresses'.
664
665 If you need even more aliases you can define them here.  It really
666 only makes sense to define names or email addresses."
667
668   :type '(repeat string)
669   :group 'gnus-icalendar)
670
671 (make-variable-buffer-local
672  (defvar gnus-icalendar-reply-status nil))
673
674 (make-variable-buffer-local
675  (defvar gnus-icalendar-event nil))
676
677 (make-variable-buffer-local
678  (defvar gnus-icalendar-handle nil))
679
680 (defvar gnus-icalendar-identities
681   (apply #'append
682          (mapcar (lambda (x) (if (listp x) x (list x)))
683                  (list user-full-name (regexp-quote user-mail-address)
684                        ; NOTE: these can be lists
685                        gnus-ignored-from-addresses ; already regexp-quoted
686                        (mapcar #'regexp-quote gnus-icalendar-additional-identities)))))
687
688 ;; TODO: make the template customizable
689 (defmethod gnus-icalendar-event->gnus-calendar ((event gnus-icalendar-event) &optional reply-status)
690   "Format an overview of EVENT details."
691   (gmm-labels ((format-header (x)
692             (format "%-12s%s"
693                     (propertize (concat (car x) ":") 'face 'bold)
694                     (cadr x))))
695
696     (with-slots (organizer summary description location recur uid
697                            method rsvp participation-type) event
698       (let ((headers `(("Summary" ,summary)
699                       ("Location" ,(or location ""))
700                       ("Time" ,(gnus-icalendar-event:org-timestamp event))
701                       ("Organizer" ,organizer)
702                       ("Attendance" ,(if (eq participation-type 'non-participant)
703                                          "You are not listed as an attendee"
704                                        (capitalize (symbol-name participation-type))))
705                       ("Method" ,method))))
706
707        (when (and (not (gnus-icalendar-event-reply-p event)) rsvp)
708          (setq headers (append headers
709                                `(("Status" ,(or reply-status "Not replied yet"))))))
710
711        (concat
712         (mapconcat #'format-header headers "\n")
713         "\n\n"
714         description)))))
715
716 (defmacro gnus-icalendar-with-decoded-handle (handle &rest body)
717   "Execute BODY in buffer containing the decoded contents of HANDLE."
718   (let ((charset (make-symbol "charset")))
719     `(let ((,charset (cdr (assoc 'charset (mm-handle-type ,handle)))))
720        (with-temp-buffer
721          (mm-insert-part ,handle)
722          (when (string= ,charset "utf-8")
723            (mm-decode-coding-region (point-min) (point-max) 'utf-8))
724
725          ,@body))))
726
727
728 (defun gnus-icalendar-event-from-handle (handle &optional attendee-name-or-email)
729   (gnus-icalendar-with-decoded-handle handle
730                        (gnus-icalendar-event-from-buffer (current-buffer) attendee-name-or-email)))
731
732 (defun gnus-icalendar-insert-button (text callback data)
733   ;; FIXME: the gnus-mime-button-map keymap does not make sense for this kind
734   ;; of button.
735   (let ((start (point)))
736     (gnus-add-text-properties
737      start
738      (progn
739        (insert "[ " text " ]")
740        (point))
741      `(gnus-callback
742        ,callback
743        keymap ,gnus-mime-button-map
744        face ,gnus-article-button-face
745        gnus-data ,data))
746     (widget-convert-button 'link start (point)
747                            :action 'gnus-widget-press-button
748                            :button-keymap gnus-widget-button-keymap)))
749
750 (defun gnus-icalendar-send-buffer-by-mail (buffer-name subject)
751   (let ((message-signature nil))
752     (with-current-buffer gnus-summary-buffer
753       (gnus-summary-reply)
754       (message-goto-body)
755       (mml-insert-multipart "alternative")
756       (mml-insert-empty-tag 'part 'type "text/plain")
757       (mml-attach-buffer buffer-name "text/calendar; method=REPLY; charset=UTF-8")
758       (message-goto-subject)
759       (delete-region (line-beginning-position) (line-end-position))
760       (insert "Subject: " subject)
761       (message-send-and-exit))))
762
763 (defun gnus-icalendar-reply (data)
764   (let* ((handle (car data))
765          (status (cadr data))
766          (event (caddr data))
767          (reply (gnus-icalendar-with-decoded-handle handle
768                   (gnus-icalendar-event-reply-from-buffer
769                    (current-buffer) status gnus-icalendar-identities))))
770
771     (when reply
772       (gmm-labels ((fold-icalendar-buffer ()
773                (goto-char (point-min))
774                (while (re-search-forward "^\\(.\\{72\\}\\)\\(.+\\)$" nil t)
775                  (replace-match "\\1\n \\2")
776                  (goto-char (line-beginning-position)))))
777         (let ((subject (concat (capitalize (symbol-name status))
778                                ": " (gnus-icalendar-event:summary event))))
779
780           (with-current-buffer (get-buffer-create gnus-icalendar-reply-bufname)
781             (delete-region (point-min) (point-max))
782             (insert reply)
783             (fold-icalendar-buffer)
784             (gnus-icalendar-send-buffer-by-mail (buffer-name) subject))
785
786           ;; Back in article buffer
787           (setq-local gnus-icalendar-reply-status status)
788           (when gnus-icalendar-org-enabled-p
789             (gnus-icalendar--update-org-event event status)
790             ;; refresh article buffer to update the reply status
791             (with-current-buffer gnus-summary-buffer
792               (gnus-summary-show-article))))))))
793
794 (defun gnus-icalendar-sync-event-to-org (event)
795   (gnus-icalendar-event:sync-to-org event gnus-icalendar-reply-status))
796
797 (defmethod gnus-icalendar-event:inline-reply-buttons ((event gnus-icalendar-event) handle)
798   (when (gnus-icalendar-event:rsvp event)
799     `(("Accept" gnus-icalendar-reply (,handle accepted ,event))
800       ("Tentative" gnus-icalendar-reply (,handle tentative ,event))
801       ("Decline" gnus-icalendar-reply (,handle declined ,event)))))
802
803 (defmethod gnus-icalendar-event:inline-reply-buttons ((event gnus-icalendar-event-reply) handle)
804   "No buttons for REPLY events."
805   nil)
806
807 (defmethod gnus-icalendar-event:inline-reply-status ((event gnus-icalendar-event))
808   (or (when gnus-icalendar-org-enabled-p
809         (gnus-icalendar--get-org-event-reply-status event))
810       "Not replied yet"))
811
812 (defmethod gnus-icalendar-event:inline-reply-status ((event gnus-icalendar-event-reply))
813   "No reply status for REPLY events."
814   nil)
815
816
817 (defmethod gnus-icalendar-event:inline-org-buttons ((event gnus-icalendar-event))
818   (let* ((org-entry-exists-p (gnus-icalendar-find-org-event-file event))
819          (export-button-text (if org-entry-exists-p "Update Org Entry" "Export to Org")))
820
821     (delq nil (list
822                `("Show Agenda" gnus-icalendar-show-org-agenda ,event)
823                (when (gnus-icalendar-event-request-p event)
824                  `(,export-button-text gnus-icalendar-sync-event-to-org ,event))
825                (when org-entry-exists-p
826                  `("Show Org Entry" gnus-icalendar--show-org-event ,event))))))
827
828
829 (defmethod gnus-icalendar-event:inline-org-buttons ((event gnus-icalendar-event-cancel))
830   (let ((org-entry-exists-p (gnus-icalendar-find-org-event-file event)))
831
832     (delq nil (list
833                `("Show Agenda" gnus-icalendar-show-org-agenda ,event)
834                (when org-entry-exists-p
835                  `("Update Org Entry" gnus-icalendar-sync-event-to-org ,event))
836                (when org-entry-exists-p
837                  `("Show Org Entry" gnus-icalendar--show-org-event ,event))))))
838
839
840 (defun gnus-icalendar-mm-inline (handle)
841   (let ((event (gnus-icalendar-event-from-handle handle gnus-icalendar-identities)))
842
843     (setq gnus-icalendar-reply-status nil)
844
845     (when event
846       (gmm-labels ((insert-button-group (buttons)
847                 (when buttons
848                   (mapc (lambda (x)
849                           (apply 'gnus-icalendar-insert-button x)
850                           (insert "    "))
851                         buttons)
852                   (insert "\n\n"))))
853
854         (insert-button-group
855          (gnus-icalendar-event:inline-reply-buttons event handle))
856
857         (when gnus-icalendar-org-enabled-p
858           (insert-button-group (gnus-icalendar-event:inline-org-buttons event)))
859
860         (setq gnus-icalendar-event event
861               gnus-icalendar-handle handle)
862
863         (insert (gnus-icalendar-event->gnus-calendar
864                  event
865                  (gnus-icalendar-event:inline-reply-status event)))))))
866
867 (defun gnus-icalendar-save-part (handle)
868   (let (event)
869     (when (and (equal (car (mm-handle-type handle)) "text/calendar")
870                (setq event (gnus-icalendar-event-from-handle handle gnus-icalendar-identities)))
871
872       (gnus-icalendar-event:sync-to-org event))))
873
874
875 (defun gnus-icalendar-save-event ()
876   "Save the Calendar event in the text/calendar part under point."
877   (interactive)
878   (gnus-article-check-buffer)
879   (let ((data (get-text-property (point) 'gnus-data)))
880     (when data
881       (gnus-icalendar-save-part data))))
882
883 (defun gnus-icalendar-reply-accept ()
884   "Accept invitation in the current article."
885   (interactive)
886   (with-current-buffer gnus-article-buffer
887     (gnus-icalendar-reply (list gnus-icalendar-handle 'accepted gnus-icalendar-event))
888     (setq-local gnus-icalendar-reply-status 'accepted)))
889
890 (defun gnus-icalendar-reply-tentative ()
891   "Send tentative response to invitation in the current article."
892   (interactive)
893   (with-current-buffer gnus-article-buffer
894     (gnus-icalendar-reply (list gnus-icalendar-handle 'tentative gnus-icalendar-event))
895     (setq-local gnus-icalendar-reply-status 'tentative)))
896
897 (defun gnus-icalendar-reply-decline ()
898   "Decline invitation in the current article."
899   (interactive)
900   (with-current-buffer gnus-article-buffer
901     (gnus-icalendar-reply (list gnus-icalendar-handle 'declined gnus-icalendar-event))
902     (setq-local gnus-icalendar-reply-status 'declined)))
903
904 (defun gnus-icalendar-event-export ()
905   "Export calendar event to `org-mode', or update existing agenda entry."
906   (interactive)
907   (with-current-buffer gnus-article-buffer
908     (gnus-icalendar-sync-event-to-org gnus-icalendar-event))
909   ;; refresh article buffer in case the reply had been sent before initial org
910   ;; export
911   (with-current-buffer gnus-summary-buffer
912     (gnus-summary-show-article)))
913
914 (defun gnus-icalendar-event-show ()
915   "Display `org-mode' agenda entry related to the calendar event."
916   (interactive)
917   (gnus-icalendar--show-org-event
918    (with-current-buffer gnus-article-buffer
919      gnus-icalendar-event)))
920
921 (defun gnus-icalendar-event-check-agenda ()
922   "Display `org-mode' agenda for days between event start and end dates."
923   (interactive)
924   (gnus-icalendar-show-org-agenda
925    (with-current-buffer gnus-article-buffer gnus-icalendar-event)))
926
927 (defvar gnus-mime-action-alist)         ; gnus-art
928
929 (defun gnus-icalendar-setup ()
930   (add-to-list 'mm-inlined-types "text/calendar")
931   (add-to-list 'mm-automatic-display "text/calendar")
932   (add-to-list 'mm-inline-media-tests '("text/calendar" gnus-icalendar-mm-inline identity))
933
934   (gnus-define-keys (gnus-summary-calendar-map "i" gnus-summary-mode-map)
935     "a" gnus-icalendar-reply-accept
936     "t" gnus-icalendar-reply-tentative
937     "d" gnus-icalendar-reply-decline
938     "c" gnus-icalendar-event-check-agenda
939     "e" gnus-icalendar-event-export
940     "s" gnus-icalendar-event-show)
941
942   (require 'gnus-art)
943   (add-to-list 'gnus-mime-action-alist
944                (cons "save calendar event" 'gnus-icalendar-save-event)
945                t))
946
947 (provide 'gnus-icalendar)
948
949 ;;; gnus-icalendar.el ends here