1 ;;; gnus-icalendar.el --- reply to iCalendar meeting requests
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Jan Tatarik <Jan.Tatarik@gmail.com>
6 ;; Keywords: mail, icalendar, org
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.
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.
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/>.
24 ;; (require 'gnus-icalendar)
25 ;; (gnus-icalendar-setup)
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)
42 (eval-when-compile (require 'cl))
44 (defun gnus-icalendar-find-if (pred seq)
47 (when (funcall pred (car seq))
48 (throw 'found (car seq)))
55 (defclass gnus-icalendar-event ()
56 ((organizer :initarg :organizer
57 :accessor gnus-icalendar-event:organizer
59 :type (or null string))
60 (summary :initarg :summary
61 :accessor gnus-icalendar-event:summary
63 :type (or null string))
64 (description :initarg :description
65 :accessor gnus-icalendar-event:description
67 :type (or null string))
68 (location :initarg :location
69 :accessor gnus-icalendar-event:location
71 :type (or null string))
72 (start-time :initarg :start-time
73 :accessor gnus-icalendar-event:start-time
76 (end-time :initarg :end-time
77 :accessor gnus-icalendar-event:end-time
80 (recur :initarg :recur
81 :accessor gnus-icalendar-event:recur
83 :type (or null string))
85 :accessor gnus-icalendar-event:uid
87 (method :initarg :method
88 :accessor gnus-icalendar-event:method
90 :type (or null string))
92 :accessor gnus-icalendar-event:rsvp
94 :type (or null boolean))
95 (participation-type :initarg :participation-type
96 :accessor gnus-icalendar-event:participation-type
97 :initform 'non-participant
99 (req-participants :initarg :req-participants
100 :accessor gnus-icalendar-event:req-participants
103 (opt-participants :initarg :opt-participants
104 :accessor gnus-icalendar-event:opt-participants
107 "generic iCalendar Event class")
109 (defclass gnus-icalendar-event-request (gnus-icalendar-event)
111 "iCalendar class for REQUEST events")
113 (defclass gnus-icalendar-event-cancel (gnus-icalendar-event)
115 "iCalendar class for CANCEL events")
117 (defclass gnus-icalendar-event-reply (gnus-icalendar-event)
119 "iCalendar class for REPLY events")
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))))
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)))
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))
136 (string-match "INTERVAL=\\([[:digit:]]+\\)" rrule)
137 (or (match-string 1 rrule)
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)))
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)))
148 (date-to-time (timezone-make-date-arpa-standard date nil tz))))
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))
164 (gnus-icalendar-find-if #'attendee-prop-matches-p event-props))))
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))
172 (gmm-labels ((attendee-role (prop) (plist-get (cadr prop) 'ROLE))
173 (attendee-name (prop)
174 (or (plist-get (cadr prop) 'CN)
175 (replace-regexp-in-string "^.*MAILTO:" "" (caddr prop))))
176 (attendees-by-type (type)
178 (lambda (p) (string= (attendee-role p) type))
180 (attendee-names-by-type (type)
181 (mapcar #'attendee-name (attendees-by-type type))))
184 (attendee-names-by-type "REQ-PARTICIPANT")
185 (attendee-names-by-type "OPT-PARTICIPANT")))))
187 (defun gnus-icalendar-event-from-ical (ical &optional attendee-name-or-email)
188 (let* ((event (car (icalendar--all-events ical)))
189 (organizer (replace-regexp-in-string
191 (or (icalendar--get-event-property event 'ORGANIZER) "")))
192 (prop-map '((summary . SUMMARY)
193 (description . DESCRIPTION)
194 (location . LOCATION)
197 (method (caddr (assoc 'METHOD (caddr (car (nreverse ical))))))
198 (attendee (when attendee-name-or-email
199 (gnus-icalendar-event--find-attendee ical attendee-name-or-email)))
200 (attendee-names (gnus-icalendar-event--get-attendee-names ical))
201 (role (plist-get (cadr attendee) 'ROLE))
202 (participation-type (pcase role
203 ("REQ-PARTICIPANT" 'required)
204 ("OPT-PARTICIPANT" 'optional)
205 (_ 'non-participant)))
206 (args (list :method method
208 :start-time (gnus-icalendar-event--decode-datefield event 'DTSTART)
209 :end-time (gnus-icalendar-event--decode-datefield event 'DTEND)
210 :rsvp (string= (plist-get (cadr attendee) 'RSVP) "TRUE")
211 :participation-type participation-type
212 :req-participants (car attendee-names)
213 :opt-participants (cadr attendee-names)))
215 ((string= method "REQUEST") 'gnus-icalendar-event-request)
216 ((string= method "CANCEL") 'gnus-icalendar-event-cancel)
217 ((string= method "REPLY") 'gnus-icalendar-event-reply)
218 (t 'gnus-icalendar-event))))
220 (gmm-labels ((map-property (prop)
221 (let ((value (icalendar--get-event-property event prop)))
223 ;; ugly, but cannot get
224 ;;replace-regexp-in-string work with "\\" as
225 ;;REP, plus we should also handle "\\;"
226 (replace-regexp-in-string
228 (replace-regexp-in-string
229 "\\\\n" "\n" (substring-no-properties value))))))
230 (accumulate-args (mapping)
231 (destructuring-bind (slot . ical-property) mapping
232 (setq args (append (list
233 (intern (concat ":" (symbol-name slot)))
234 (map-property ical-property))
237 (mapc #'accumulate-args prop-map)
238 (apply 'make-instance event-class args))))
240 (defun gnus-icalendar-event-from-buffer (buf &optional attendee-name-or-email)
241 "Parse RFC5545 iCalendar in buffer BUF and return an event object.
243 Return a gnus-icalendar-event object representing the first event
244 contained in the invitation. Return nil for calendars without an event entry.
246 ATTENDEE-NAME-OR-EMAIL is a list of strings that will be matched
247 against the event's attendee names and emails. Invitation rsvp
248 status will be retrieved from the first matching attendee record."
249 (let ((ical (with-current-buffer (icalendar--get-unfolded-buffer (get-buffer buf))
250 (goto-char (point-min))
251 (icalendar--read-element nil nil))))
254 (gnus-icalendar-event-from-ical ical attendee-name-or-email))))
257 ;;; gnus-icalendar-event-reply
260 (defun gnus-icalendar-event--build-reply-event-body (ical-request status identities)
261 (let ((summary-status (capitalize (symbol-name status)))
262 (attendee-status (upcase (symbol-name status)))
264 (gmm-labels ((update-summary (line)
265 (if (string-match "^[^:]+:" line)
266 (replace-match (format "\\&%s: " summary-status) t nil line)
269 (format-time-string "DTSTAMP:%Y%m%dT%H%M%SZ" nil t))
270 (attendee-matches-identity (line)
271 (gnus-icalendar-find-if (lambda (name) (string-match-p name line))
273 (update-attendee-status (line)
274 (when (and (attendee-matches-identity line)
275 (string-match "\\(PARTSTAT=\\)[^;]+" line))
276 (replace-match (format "\\1%s" attendee-status) t nil line)))
277 (process-event-line (line)
278 (when (string-match "^\\([^;:]+\\)" line)
279 (let* ((key (match-string 0 line))
280 ;; NOTE: not all of the below fields are mandatory,
281 ;; but they are often present in other clients'
282 ;; replies. Can be helpful for debugging, too.
285 ((string= key "ATTENDEE") (update-attendee-status line))
286 ((string= key "SUMMARY") (update-summary line))
287 ((string= key "DTSTAMP") (update-dtstamp))
288 ((member key '("ORGANIZER" "DTSTART" "DTEND"
289 "LOCATION" "DURATION" "SEQUENCE"
290 "RECURRENCE-ID" "UID")) line)
293 (push new-line reply-event-lines))))))
295 (mapc #'process-event-line (split-string ical-request "\n"))
297 (unless (gnus-icalendar-find-if (lambda (x) (string-match "^ATTENDEE" x))
299 (error "Could not find an event attendee matching given identity"))
301 (mapconcat #'identity `("BEGIN:VEVENT"
302 ,@(nreverse reply-event-lines)
306 (defun gnus-icalendar-event-reply-from-buffer (buf status identities)
307 "Build a calendar event reply for request contained in BUF.
308 The reply will have STATUS (`accepted', `tentative' or `declined').
309 The reply will be composed for attendees matching any entry
310 on the IDENTITIES list."
311 (gmm-labels ((extract-block (blockname)
313 (let ((block-start-re (format "^BEGIN:%s" blockname))
314 (block-end-re (format "^END:%s" blockname))
316 (when (re-search-forward block-start-re nil t)
317 (setq start (line-beginning-position))
318 (re-search-forward block-end-re)
319 (buffer-substring-no-properties start (line-end-position)))))))
322 (with-current-buffer (icalendar--get-unfolded-buffer (get-buffer buf))
323 (goto-char (point-min))
324 (setq zone (extract-block "VTIMEZONE")
325 event (extract-block "VEVENT")))
328 (let ((contents (list "BEGIN:VCALENDAR"
333 (gnus-icalendar-event--build-reply-event-body event status identities)
336 (mapconcat #'identity (delq nil contents) "\n"))))))
339 ;;; gnus-icalendar-org
341 ;;; TODO: this is an optional feature, and it's only available with org-mode
342 ;;; 7+, so will need to properly handle emacsen with no/outdated org-mode
345 (require 'org-capture)
347 (defgroup gnus-icalendar-org nil
348 "Settings for Calendar Event gnus/org integration."
350 :group 'gnus-icalendar
351 :prefix "gnus-icalendar-org-")
353 (defcustom gnus-icalendar-org-capture-file nil
354 "Target Org file for storing captured calendar events."
355 :type '(choice (const nil) file)
356 :group 'gnus-icalendar-org)
358 (defcustom gnus-icalendar-org-capture-headline nil
359 "Target outline in `gnus-icalendar-org-capture-file' for storing captured events."
360 :type '(repeat string)
361 :group 'gnus-icalendar-org)
363 (defcustom gnus-icalendar-org-template-name "used by gnus-icalendar-org"
364 "Org-mode template name."
366 :group 'gnus-icalendar-org)
368 (defcustom gnus-icalendar-org-template-key "#"
369 "Org-mode template hotkey."
371 :group 'gnus-icalendar-org)
373 (defvar gnus-icalendar-org-enabled-p nil)
376 (defmethod gnus-icalendar-event:org-repeat ((event gnus-icalendar-event))
377 "Return `org-mode' timestamp repeater string for recurring EVENT.
378 Return nil for non-recurring EVENT."
379 (when (gnus-icalendar-event:recurring-p event)
380 (let* ((freq-map '(("HOURLY" . "h")
385 (org-freq (cdr (assoc (gnus-icalendar-event:recurring-freq event) freq-map))))
388 (format "+%s%s" (gnus-icalendar-event:recurring-interval event) org-freq)))))
390 (defmethod gnus-icalendar-event:org-timestamp ((event gnus-icalendar-event))
391 "Build `org-mode' timestamp from EVENT start/end dates and recurrence info."
392 (let* ((start (gnus-icalendar-event:start-time event))
393 (end (gnus-icalendar-event:end-time event))
394 (start-date (format-time-string "%Y-%m-%d %a" start))
395 (start-time (format-time-string "%H:%M" start))
396 (start-at-midnight (string= start-time "00:00"))
397 (end-date (format-time-string "%Y-%m-%d %a" end))
398 (end-time (format-time-string "%H:%M" end))
399 (end-at-midnight (string= end-time "00:00"))
400 (start-end-date-diff (/ (float-time (time-subtract
401 (date-to-time end-date)
402 (date-to-time start-date)))
404 (org-repeat (gnus-icalendar-event:org-repeat event))
405 (repeat (if org-repeat (concat " " org-repeat) ""))
406 (time-1-day '(0 86400)))
408 ;; NOTE: special care is needed with appointments ending at midnight
409 ;; (typically all-day events): the end time has to be changed to 23:59 to
410 ;; prevent org agenda showing the event on one additional day
412 ;; start/end midnight
413 ;; A 0:0 - A+1 0:0 -> A
414 ;; A 0:0 - A+n 0:0 -> A - A+n-1
415 ((and start-at-midnight end-at-midnight) (if (> start-end-date-diff 1)
416 (let ((end-ts (format-time-string "%Y-%m-%d %a" (time-subtract end time-1-day))))
417 (format "<%s>--<%s>" start-date end-ts))
418 (format "<%s%s>" start-date repeat)))
420 ;; A .:. - A+1 0:0 -> A .:.-23:59
421 ;; A .:. - A+n 0:0 -> A .:. - A_n-1
422 (end-at-midnight (if (= start-end-date-diff 1)
423 (format "<%s %s-23:59%s>" start-date start-time repeat)
424 (let ((end-ts (format-time-string "%Y-%m-%d %a" (time-subtract end time-1-day))))
425 (format "<%s %s>--<%s>" start-date start-time end-ts))))
427 ;; A 0:0 - A .:. -> A 0:0-.:. (default 1)
428 ;; A 0:0 - A+n .:. -> A - A+n .:.
429 ((and start-at-midnight
430 (plusp start-end-date-diff)) (format "<%s>--<%s %s>" start-date end-date end-time))
432 ;; A .:. - A .:. -> A .:.-.:.
434 ((zerop start-end-date-diff) (format "<%s %s-%s%s>" start-date start-time end-time repeat))
435 (t (format "<%s %s>--<%s %s>" start-date start-time end-date end-time)))))
437 (defun gnus-icalendar--format-summary-line (summary &optional location)
439 (format "%s (%s)" summary location)
440 (format "%s" summary)))
443 (defun gnus-icalendar--format-participant-list (participants)
444 (mapconcat #'identity participants ", "))
446 ;; TODO: make the template customizable
447 (defmethod gnus-icalendar-event->org-entry ((event gnus-icalendar-event) reply-status)
448 "Return string with new `org-mode' entry describing EVENT."
451 (with-slots (organizer summary description location
453 (let* ((reply (if reply-status (capitalize (symbol-name reply-status))
455 (props `(("ICAL_EVENT" . "t")
457 ("ORGANIZER" . ,(gnus-icalendar-event:organizer event))
458 ("LOCATION" . ,(gnus-icalendar-event:location event))
459 ("PARTICIPATION_TYPE" . ,(symbol-name (gnus-icalendar-event:participation-type event)))
460 ("REQ_PARTICIPANTS" . ,(gnus-icalendar--format-participant-list (gnus-icalendar-event:req-participants event)))
461 ("OPT_PARTICIPANTS" . ,(gnus-icalendar--format-participant-list (gnus-icalendar-event:opt-participants event)))
462 ("RRULE" . ,(gnus-icalendar-event:recur event))
463 ("REPLY" . ,reply))))
465 (insert (format "* %s\n\n"
466 (gnus-icalendar--format-summary-line summary location)))
468 (org-entry-put (point) (car prop) (cdr prop)))
473 (narrow-to-region (point) (point))
474 (insert (gnus-icalendar-event:org-timestamp event)
477 (indent-region (point-min) (point-max) 2)
478 (fill-region (point-min) (point-max))))
482 (defun gnus-icalendar--deactivate-org-timestamp (ts)
483 (replace-regexp-in-string "[<>]"
484 (lambda (m) (cond ((string= m "<") "[")
485 ((string= m ">") "]")))
488 (defun gnus-icalendar-find-org-event-file (event &optional org-file)
489 "Return the name of the file containing EVENT org entry.
490 Return nil when not found.
492 All org agenda files are searched for the EVENT entry. When
493 the optional ORG-FILE argument is specified, only that one file
495 (let ((uid (gnus-icalendar-event:uid event))
496 (files (or org-file (org-agenda-files t 'ifmode))))
498 ((find-event-in (file)
499 (org-check-agenda-file file)
500 (with-current-buffer (find-file-noselect file)
501 (let ((event-pos (org-find-entry-with-id uid)))
503 (string= (cdr (assoc "ICAL_EVENT" (org-entry-properties event-pos)))
505 (throw 'found file))))))
507 (gnus-icalendar-find-if #'find-event-in files))))
510 (defun gnus-icalendar--show-org-event (event &optional org-file)
511 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
513 (switch-to-buffer (find-file file))
514 (goto-char (org-find-entry-with-id (gnus-icalendar-event:uid event)))
518 (defun gnus-icalendar--update-org-event (event reply-status &optional org-file)
519 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
521 (with-current-buffer (find-file-noselect file)
522 (with-slots (uid summary description organizer location recur
523 participation-type req-participants opt-participants) event
524 (let ((event-pos (org-find-entry-with-id uid)))
526 (goto-char event-pos)
528 ;; update the headline, keep todo, priority and tags, if any
530 (let* ((priority (org-entry-get (point) "PRIORITY"))
531 (headline (delq nil (list
532 (org-entry-get (point) "TODO")
533 (when priority (format "[#%s]" priority))
534 (gnus-icalendar--format-summary-line summary location)
535 (org-entry-get (point) "TAGS")))))
537 (re-search-forward "^\\*+ " (line-end-position))
538 (delete-region (point) (line-end-position))
539 (insert (mapconcat #'identity headline " "))))
541 ;; update props and description
542 (let ((entry-end (org-entry-end-position))
543 (entry-outline-level (org-outline-level)))
545 ;; delete body of the entry, leave org drawers intact
547 (org-narrow-to-element)
548 (goto-char entry-end)
549 (re-search-backward "^[\t ]*:END:")
551 (delete-region (point) entry-end))
553 ;; put new event description in the entry body
556 (narrow-to-region (point) (point))
558 (gnus-icalendar-event:org-timestamp event)
560 (replace-regexp-in-string "[\n]+$" "\n" description)
562 (indent-region (point-min) (point-max) (1+ entry-outline-level))
563 (fill-region (point-min) (point-max))))
565 ;; update entry properties
567 ((update-org-entry (position property value)
570 (org-entry-delete position property)
571 (org-entry-put position property value))))
573 (update-org-entry event-pos "ORGANIZER" organizer)
574 (update-org-entry event-pos "LOCATION" location)
575 (update-org-entry event-pos "PARTICIPATION_TYPE" (symbol-name participation-type))
576 (update-org-entry event-pos "REQ_PARTICIPANTS" (gnus-icalendar--format-participant-list req-participants))
577 (update-org-entry event-pos "OPT_PARTICIPANTS" (gnus-icalendar--format-participant-list opt-participants))
578 (update-org-entry event-pos "RRULE" recur)
579 (update-org-entry event-pos "REPLY"
580 (if reply-status (capitalize (symbol-name reply-status))
582 (save-buffer)))))))))
585 (defun gnus-icalendar--cancel-org-event (event &optional org-file)
586 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
588 (with-current-buffer (find-file-noselect file)
589 (let ((event-pos (org-find-entry-with-id (gnus-icalendar-event:uid event))))
591 (let ((ts (org-entry-get event-pos "DT")))
593 (org-entry-put event-pos "DT" (gnus-icalendar--deactivate-org-timestamp ts))
594 (save-buffer)))))))))
597 (defun gnus-icalendar--get-org-event-reply-status (event &optional org-file)
598 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
601 (with-current-buffer (find-file-noselect file)
602 (let ((event-pos (org-find-entry-with-id (gnus-icalendar-event:uid event))))
603 (org-entry-get event-pos "REPLY")))))))
606 (defun gnus-icalendar-insinuate-org-templates ()
607 (unless (gnus-icalendar-find-if (lambda (x) (string= (cadr x) gnus-icalendar-org-template-name))
608 org-capture-templates)
609 (setq org-capture-templates
610 (append `((,gnus-icalendar-org-template-key
611 ,gnus-icalendar-org-template-name
613 (file+olp ,gnus-icalendar-org-capture-file ,@gnus-icalendar-org-capture-headline)
615 :immediate-finish t))
616 org-capture-templates))
618 ;; hide the template from interactive template selection list
620 ;; NOTE: doesn't work when capturing from string
621 ;; (when (boundp 'org-capture-templates-contexts)
622 ;; (push `(,gnus-icalendar-org-template-key "" ((in-mode . "gnus-article-mode")))
623 ;; org-capture-templates-contexts))
626 (defun gnus-icalendar:org-event-save (event reply-status)
628 (org-capture-string (gnus-icalendar-event->org-entry event reply-status)
629 gnus-icalendar-org-template-key)))
631 (defun gnus-icalendar-show-org-agenda (event)
632 (let* ((time-delta (time-subtract (gnus-icalendar-event:end-time event)
633 (gnus-icalendar-event:start-time event)))
634 (duration-days (1+ (/ (+ (* (car time-delta) (expt 2 16))
638 (org-agenda-list nil (gnus-icalendar-event:start event) duration-days)))
640 (defmethod gnus-icalendar-event:sync-to-org ((event gnus-icalendar-event-request) reply-status)
641 (if (gnus-icalendar-find-org-event-file event)
642 (gnus-icalendar--update-org-event event reply-status)
643 (gnus-icalendar:org-event-save event reply-status)))
645 (defmethod gnus-icalendar-event:sync-to-org ((event gnus-icalendar-event-cancel) reply-status)
646 (when (gnus-icalendar-find-org-event-file event)
647 (gnus-icalendar--cancel-org-event event)))
649 (defun gnus-icalendar-org-setup ()
650 (if (and gnus-icalendar-org-capture-file gnus-icalendar-org-capture-headline)
652 (gnus-icalendar-insinuate-org-templates)
653 (setq gnus-icalendar-org-enabled-p t))
654 (message "Cannot enable Calendar->Org: missing capture file, headline")))
660 (defgroup gnus-icalendar nil
661 "Settings for inline display of iCalendar invitations."
664 :prefix "gnus-icalendar-")
666 (defcustom gnus-icalendar-reply-bufname "*CAL*"
667 "Buffer used for building iCalendar invitation reply."
669 :group 'gnus-icalendar)
671 (defcustom gnus-icalendar-additional-identities nil
672 "We need to know your identity to make replies to calendar requests work.
674 Gnus will only offer you the Accept/Tentative/Decline buttons for
675 calendar events if any of your identities matches at least one
678 Your identity is guessed automatically from the variables `user-full-name',
679 `user-mail-address', and `gnus-ignored-from-addresses'.
681 If you need even more aliases you can define them here. It really
682 only makes sense to define names or email addresses."
684 :type '(repeat string)
685 :group 'gnus-icalendar)
687 (make-variable-buffer-local
688 (defvar gnus-icalendar-reply-status nil))
690 (make-variable-buffer-local
691 (defvar gnus-icalendar-event nil))
693 (make-variable-buffer-local
694 (defvar gnus-icalendar-handle nil))
696 (defun gnus-icalendar-identities ()
697 "Return list of regexp-quoted names and email addresses belonging to the user.
699 These will be used to retrieve the RSVP information from ical events."
701 (mapcar (lambda (x) (if (listp x) x (list x)))
702 (list user-full-name (regexp-quote user-mail-address)
703 ; NOTE: these can be lists
704 gnus-ignored-from-addresses ; already regexp-quoted
705 (mapcar #'regexp-quote gnus-icalendar-additional-identities)))))
707 ;; TODO: make the template customizable
708 (defmethod gnus-icalendar-event->gnus-calendar ((event gnus-icalendar-event) &optional reply-status)
709 "Format an overview of EVENT details."
710 (gmm-labels ((format-header (x)
712 (propertize (concat (car x) ":") 'face 'bold)
715 (with-slots (organizer summary description location recur uid
716 method rsvp participation-type) event
717 (let ((headers `(("Summary" ,summary)
718 ("Location" ,(or location ""))
719 ("Time" ,(gnus-icalendar-event:org-timestamp event))
720 ("Organizer" ,organizer)
721 ("Attendance" ,(if (eq participation-type 'non-participant)
722 "You are not listed as an attendee"
723 (capitalize (symbol-name participation-type))))
724 ("Method" ,method))))
726 (when (and (not (gnus-icalendar-event-reply-p event)) rsvp)
727 (setq headers (append headers
728 `(("Status" ,(or reply-status "Not replied yet"))))))
731 (mapconcat #'format-header headers "\n")
735 (defmacro gnus-icalendar-with-decoded-handle (handle &rest body)
736 "Execute BODY in buffer containing the decoded contents of HANDLE."
737 (let ((charset (make-symbol "charset")))
738 `(let ((,charset (cdr (assoc 'charset (mm-handle-type ,handle)))))
740 (mm-insert-part ,handle)
741 (when (string= ,charset "utf-8")
742 (mm-decode-coding-region (point-min) (point-max) 'utf-8))
747 (defun gnus-icalendar-event-from-handle (handle &optional attendee-name-or-email)
748 (gnus-icalendar-with-decoded-handle handle
749 (gnus-icalendar-event-from-buffer (current-buffer) attendee-name-or-email)))
751 (defun gnus-icalendar-insert-button (text callback data)
752 ;; FIXME: the gnus-mime-button-map keymap does not make sense for this kind
754 (let ((start (point)))
755 (gnus-add-text-properties
758 (insert "[ " text " ]")
762 keymap ,gnus-mime-button-map
763 face ,gnus-article-button-face
765 (widget-convert-button 'link start (point)
766 :action 'gnus-widget-press-button
767 :button-keymap gnus-widget-button-keymap)))
769 (defun gnus-icalendar-send-buffer-by-mail (buffer-name subject)
770 (let ((message-signature nil))
771 (with-current-buffer gnus-summary-buffer
774 (mml-insert-multipart "alternative")
775 (mml-insert-empty-tag 'part 'type "text/plain")
776 (mml-attach-buffer buffer-name "text/calendar; method=REPLY; charset=UTF-8")
777 (message-goto-subject)
778 (delete-region (line-beginning-position) (line-end-position))
779 (insert "Subject: " subject)
780 (message-send-and-exit))))
782 (defun gnus-icalendar-reply (data)
783 (let* ((handle (car data))
786 (reply (gnus-icalendar-with-decoded-handle handle
787 (gnus-icalendar-event-reply-from-buffer
788 (current-buffer) status (gnus-icalendar-identities)))))
791 (gmm-labels ((fold-icalendar-buffer ()
792 (goto-char (point-min))
793 (while (re-search-forward "^\\(.\\{72\\}\\)\\(.+\\)$" nil t)
794 (replace-match "\\1\n \\2")
795 (goto-char (line-beginning-position)))))
796 (let ((subject (concat (capitalize (symbol-name status))
797 ": " (gnus-icalendar-event:summary event))))
799 (with-current-buffer (get-buffer-create gnus-icalendar-reply-bufname)
800 (delete-region (point-min) (point-max))
802 (fold-icalendar-buffer)
803 (gnus-icalendar-send-buffer-by-mail (buffer-name) subject))
805 ;; Back in article buffer
806 (setq-local gnus-icalendar-reply-status status)
807 (when gnus-icalendar-org-enabled-p
808 (gnus-icalendar--update-org-event event status)
809 ;; refresh article buffer to update the reply status
810 (with-current-buffer gnus-summary-buffer
811 (gnus-summary-show-article))))))))
813 (defun gnus-icalendar-sync-event-to-org (event)
814 (gnus-icalendar-event:sync-to-org event gnus-icalendar-reply-status))
816 (defmethod gnus-icalendar-event:inline-reply-buttons ((event gnus-icalendar-event) handle)
817 (when (gnus-icalendar-event:rsvp event)
818 `(("Accept" gnus-icalendar-reply (,handle accepted ,event))
819 ("Tentative" gnus-icalendar-reply (,handle tentative ,event))
820 ("Decline" gnus-icalendar-reply (,handle declined ,event)))))
822 (defmethod gnus-icalendar-event:inline-reply-buttons ((event gnus-icalendar-event-reply) handle)
823 "No buttons for REPLY events."
826 (defmethod gnus-icalendar-event:inline-reply-status ((event gnus-icalendar-event))
827 (or (when gnus-icalendar-org-enabled-p
828 (gnus-icalendar--get-org-event-reply-status event))
831 (defmethod gnus-icalendar-event:inline-reply-status ((event gnus-icalendar-event-reply))
832 "No reply status for REPLY events."
836 (defmethod gnus-icalendar-event:inline-org-buttons ((event gnus-icalendar-event))
837 (let* ((org-entry-exists-p (gnus-icalendar-find-org-event-file event))
838 (export-button-text (if org-entry-exists-p "Update Org Entry" "Export to Org")))
841 `("Show Agenda" gnus-icalendar-show-org-agenda ,event)
842 (when (gnus-icalendar-event-request-p event)
843 `(,export-button-text gnus-icalendar-sync-event-to-org ,event))
844 (when org-entry-exists-p
845 `("Show Org Entry" gnus-icalendar--show-org-event ,event))))))
848 (defmethod gnus-icalendar-event:inline-org-buttons ((event gnus-icalendar-event-cancel))
849 (let ((org-entry-exists-p (gnus-icalendar-find-org-event-file event)))
852 `("Show Agenda" gnus-icalendar-show-org-agenda ,event)
853 (when org-entry-exists-p
854 `("Update Org Entry" gnus-icalendar-sync-event-to-org ,event))
855 (when org-entry-exists-p
856 `("Show Org Entry" gnus-icalendar--show-org-event ,event))))))
859 (defun gnus-icalendar-mm-inline (handle)
860 (let ((event (gnus-icalendar-event-from-handle handle (gnus-icalendar-identities))))
862 (setq gnus-icalendar-reply-status nil)
865 (gmm-labels ((insert-button-group (buttons)
868 (apply 'gnus-icalendar-insert-button x)
874 (gnus-icalendar-event:inline-reply-buttons event handle))
876 (when gnus-icalendar-org-enabled-p
877 (insert-button-group (gnus-icalendar-event:inline-org-buttons event)))
879 (setq gnus-icalendar-event event
880 gnus-icalendar-handle handle)
882 (insert (gnus-icalendar-event->gnus-calendar
884 (gnus-icalendar-event:inline-reply-status event)))))))
886 (defun gnus-icalendar-save-part (handle)
888 (when (and (equal (car (mm-handle-type handle)) "text/calendar")
889 (setq event (gnus-icalendar-event-from-handle handle (gnus-icalendar-identities))))
891 (gnus-icalendar-event:sync-to-org event))))
894 (defun gnus-icalendar-save-event ()
895 "Save the Calendar event in the text/calendar part under point."
897 (gnus-article-check-buffer)
898 (let ((data (get-text-property (point) 'gnus-data)))
900 (gnus-icalendar-save-part data))))
902 (defun gnus-icalendar-reply-accept ()
903 "Accept invitation in the current article."
905 (with-current-buffer gnus-article-buffer
906 (gnus-icalendar-reply (list gnus-icalendar-handle 'accepted gnus-icalendar-event))
907 (setq-local gnus-icalendar-reply-status 'accepted)))
909 (defun gnus-icalendar-reply-tentative ()
910 "Send tentative response to invitation in the current article."
912 (with-current-buffer gnus-article-buffer
913 (gnus-icalendar-reply (list gnus-icalendar-handle 'tentative gnus-icalendar-event))
914 (setq-local gnus-icalendar-reply-status 'tentative)))
916 (defun gnus-icalendar-reply-decline ()
917 "Decline invitation in the current article."
919 (with-current-buffer gnus-article-buffer
920 (gnus-icalendar-reply (list gnus-icalendar-handle 'declined gnus-icalendar-event))
921 (setq-local gnus-icalendar-reply-status 'declined)))
923 (defun gnus-icalendar-event-export ()
924 "Export calendar event to `org-mode', or update existing agenda entry."
926 (with-current-buffer gnus-article-buffer
927 (gnus-icalendar-sync-event-to-org gnus-icalendar-event))
928 ;; refresh article buffer in case the reply had been sent before initial org
930 (with-current-buffer gnus-summary-buffer
931 (gnus-summary-show-article)))
933 (defun gnus-icalendar-event-show ()
934 "Display `org-mode' agenda entry related to the calendar event."
936 (gnus-icalendar--show-org-event
937 (with-current-buffer gnus-article-buffer
938 gnus-icalendar-event)))
940 (defun gnus-icalendar-event-check-agenda ()
941 "Display `org-mode' agenda for days between event start and end dates."
943 (gnus-icalendar-show-org-agenda
944 (with-current-buffer gnus-article-buffer gnus-icalendar-event)))
946 (defvar gnus-mime-action-alist) ; gnus-art
948 (defun gnus-icalendar-setup ()
949 (add-to-list 'mm-inlined-types "text/calendar")
950 (add-to-list 'mm-automatic-display "text/calendar")
951 (add-to-list 'mm-inline-media-tests '("text/calendar" gnus-icalendar-mm-inline identity))
953 (gnus-define-keys (gnus-summary-calendar-map "i" gnus-summary-mode-map)
954 "a" gnus-icalendar-reply-accept
955 "t" gnus-icalendar-reply-tentative
956 "d" gnus-icalendar-reply-decline
957 "c" gnus-icalendar-event-check-agenda
958 "e" gnus-icalendar-event-export
959 "s" gnus-icalendar-event-show)
962 (add-to-list 'gnus-mime-action-alist
963 (cons "save calendar event" 'gnus-icalendar-save-event)
966 (provide 'gnus-icalendar)
968 ;;; gnus-icalendar.el ends here