1 ;;; gnus-icalendar.el --- reply to iCalendar meeting requests
3 ;; Copyright (C) 2013 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-required :initarg :participation-required
96 :accessor gnus-icalendar-event:participation-required
98 :type (or null boolean))
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) (plist-get (cadr prop) 'CN))
174 (attendees-by-type (type)
176 (lambda (p) (string= (attendee-role p) type))
178 (attendee-names-by-type (type)
179 (mapcar #'attendee-name (attendees-by-type type))))
182 (attendee-names-by-type "REQ-PARTICIPANT")
183 (attendee-names-by-type "OPT-PARTICIPANT")))))
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
189 (or (icalendar--get-event-property event 'ORGANIZER) "")))
190 (prop-map '((summary . SUMMARY)
191 (description . DESCRIPTION)
192 (location . LOCATION)
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 (args (list :method method
201 :start-time (gnus-icalendar-event--decode-datefield event 'DTSTART)
202 :end-time (gnus-icalendar-event--decode-datefield event 'DTEND)
203 :rsvp (string= (plist-get (cadr attendee) 'RSVP)
205 :participation-required (string= (plist-get (cadr attendee) 'ROLE)
207 :req-participants (cdar attendee-names)
208 :opt-participants (cadr attendee-names)))
210 ((string= method "REQUEST") 'gnus-icalendar-event-request)
211 ((string= method "CANCEL") 'gnus-icalendar-event-cancel)
212 ((string= method "REPLY") 'gnus-icalendar-event-reply)
213 (t 'gnus-icalendar-event))))
215 (gmm-labels ((map-property (prop)
216 (let ((value (icalendar--get-event-property event prop)))
218 ;; ugly, but cannot get
219 ;;replace-regexp-in-string work with "\\" as
220 ;;REP, plus we should also handle "\\;"
221 (replace-regexp-in-string
223 (replace-regexp-in-string
224 "\\\\n" "\n" (substring-no-properties value))))))
225 (accumulate-args (mapping)
226 (destructuring-bind (slot . ical-property) mapping
227 (setq args (append (list
228 (intern (concat ":" (symbol-name slot)))
229 (map-property ical-property))
232 (mapc #'accumulate-args prop-map)
233 (apply 'make-instance event-class args))))
235 (defun gnus-icalendar-event-from-buffer (buf &optional attendee-name-or-email)
236 "Parse RFC5545 iCalendar in buffer BUF and return an event object.
238 Return a gnus-icalendar-event object representing the first event
239 contained in the invitation. Return nil for calendars without an event entry.
241 ATTENDEE-NAME-OR-EMAIL is a list of strings that will be matched
242 against the event's attendee names and emails. Invitation rsvp
243 status will be retrieved from the first matching attendee record."
244 (let ((ical (with-current-buffer (icalendar--get-unfolded-buffer (get-buffer buf))
245 (goto-char (point-min))
246 (icalendar--read-element nil nil))))
249 (gnus-icalendar-event-from-ical ical attendee-name-or-email))))
252 ;;; gnus-icalendar-event-reply
255 (defun gnus-icalendar-event--build-reply-event-body (ical-request status identities)
256 (let ((summary-status (capitalize (symbol-name status)))
257 (attendee-status (upcase (symbol-name status)))
259 (gmm-labels ((update-summary (line)
260 (if (string-match "^[^:]+:" line)
261 (replace-match (format "\\&%s: " summary-status) t nil line)
264 (format-time-string "DTSTAMP:%Y%m%dT%H%M%SZ" nil t))
265 (attendee-matches-identity (line)
266 (gnus-icalendar-find-if (lambda (name) (string-match-p name line))
268 (update-attendee-status (line)
269 (when (and (attendee-matches-identity line)
270 (string-match "\\(PARTSTAT=\\)[^;]+" line))
271 (replace-match (format "\\1%s" attendee-status) t nil line)))
272 (process-event-line (line)
273 (when (string-match "^\\([^;:]+\\)" line)
274 (let* ((key (match-string 0 line))
275 ;; NOTE: not all of the below fields are mandatory,
276 ;; but they are often present in other clients'
277 ;; replies. Can be helpful for debugging, too.
280 ((string= key "ATTENDEE") (update-attendee-status line))
281 ((string= key "SUMMARY") (update-summary line))
282 ((string= key "DTSTAMP") (update-dtstamp))
283 ((member key '("ORGANIZER" "DTSTART" "DTEND"
284 "LOCATION" "DURATION" "SEQUENCE"
285 "RECURRENCE-ID" "UID")) line)
288 (push new-line reply-event-lines))))))
290 (mapc #'process-event-line (split-string ical-request "\n"))
292 (unless (gnus-icalendar-find-if (lambda (x) (string-match "^ATTENDEE" x))
294 (error "Could not find an event attendee matching given identity"))
296 (mapconcat #'identity `("BEGIN:VEVENT"
297 ,@(nreverse reply-event-lines)
301 (defun gnus-icalendar-event-reply-from-buffer (buf status identities)
302 "Build a calendar event reply for request contained in BUF.
303 The reply will have STATUS (`accepted', `tentative' or `declined').
304 The reply will be composed for attendees matching any entry
305 on the IDENTITIES list."
306 (gmm-labels ((extract-block (blockname)
308 (let ((block-start-re (format "^BEGIN:%s" blockname))
309 (block-end-re (format "^END:%s" blockname))
311 (when (re-search-forward block-start-re nil t)
312 (setq start (line-beginning-position))
313 (re-search-forward block-end-re)
314 (buffer-substring-no-properties start (line-end-position)))))))
317 (with-current-buffer (icalendar--get-unfolded-buffer (get-buffer buf))
318 (goto-char (point-min))
319 (setq zone (extract-block "VTIMEZONE")
320 event (extract-block "VEVENT")))
323 (let ((contents (list "BEGIN:VCALENDAR"
328 (gnus-icalendar-event--build-reply-event-body event status identities)
331 (mapconcat #'identity (delq nil contents) "\n"))))))
334 ;;; gnus-icalendar-org
336 ;;; TODO: this is an optional feature, and it's only available with org-mode
337 ;;; 7+, so will need to properly handle emacsen with no/outdated org-mode
340 (require 'org-capture)
342 (defgroup gnus-icalendar-org nil
343 "Settings for Calendar Event gnus/org integration."
344 :group 'gnus-icalendar
345 :prefix "gnus-icalendar-org-")
347 (defcustom gnus-icalendar-org-capture-file nil
348 "Target Org file for storing captured calendar events."
349 :type '(choice (const nil) file)
350 :group 'gnus-icalendar-org)
352 (defcustom gnus-icalendar-org-capture-headline nil
353 "Target outline in `gnus-icalendar-org-capture-file' for storing captured events."
354 :type '(repeat string)
355 :group 'gnus-icalendar-org)
357 (defcustom gnus-icalendar-org-template-name "used by gnus-icalendar-org"
358 "Org-mode template name."
360 :group 'gnus-icalendar-org)
362 (defcustom gnus-icalendar-org-template-key "#"
363 "Org-mode template hotkey."
365 :group 'gnus-icalendar-org)
367 (defvar gnus-icalendar-org-enabled-p nil)
370 (defmethod gnus-icalendar-event:org-repeat ((event gnus-icalendar-event))
371 "Return `org-mode' timestamp repeater string for recurring EVENT.
372 Return nil for non-recurring EVENT."
373 (when (gnus-icalendar-event:recurring-p event)
374 (let* ((freq-map '(("HOURLY" . "h")
379 (org-freq (cdr (assoc (gnus-icalendar-event:recurring-freq event) freq-map))))
382 (format "+%s%s" (gnus-icalendar-event:recurring-interval event) org-freq)))))
384 (defmethod gnus-icalendar-event:org-timestamp ((event gnus-icalendar-event))
385 "Build `org-mode' timestamp from EVENT start/end dates and recurrence info."
386 (let* ((start (gnus-icalendar-event:start-time event))
387 (end (gnus-icalendar-event:end-time event))
388 (start-date (format-time-string "%Y-%m-%d %a" start))
389 (start-time (format-time-string "%H:%M" start))
390 (end-date (format-time-string "%Y-%m-%d %a" end))
391 (end-time (format-time-string "%H:%M" end))
392 (org-repeat (gnus-icalendar-event:org-repeat event))
393 (repeat (if org-repeat (concat " " org-repeat) "")))
395 (if (equal start-date end-date)
396 (format "<%s %s-%s%s>" start-date start-time end-time repeat)
397 (format "<%s %s>--<%s %s>" start-date start-time end-date end-time))))
399 (defun gnus-icalendar--format-summary-line (summary &optional location)
401 (format "%s (%s)" summary location)
402 (format "%s" summary)))
405 (defun gnus-icalendar--format-participant-list (participants)
406 (mapconcat #'identity participants ", "))
408 ;; TODO: make the template customizable
409 (defmethod gnus-icalendar-event->org-entry ((event gnus-icalendar-event) reply-status)
410 "Return string with new `org-mode' entry describing EVENT."
413 (with-slots (organizer summary description location
415 (let* ((reply (if reply-status (capitalize (symbol-name reply-status))
417 (props `(("ICAL_EVENT" . "t")
419 ("DT" . ,(gnus-icalendar-event:org-timestamp event))
420 ("ORGANIZER" . ,(gnus-icalendar-event:organizer event))
421 ("LOCATION" . ,(gnus-icalendar-event:location event))
422 ("PARTICIPATION_REQUIRED" . ,(when (gnus-icalendar-event:participation-required event) "t"))
423 ("REQ_PARTICIPANTS" . ,(gnus-icalendar--format-participant-list (gnus-icalendar-event:req-participants event)))
424 ("OPT_PARTICIPANTS" . ,(gnus-icalendar--format-participant-list (gnus-icalendar-event:opt-participants event)))
425 ("RRULE" . ,(gnus-icalendar-event:recur event))
426 ("REPLY" . ,reply))))
428 (insert (format "* %s\n\n"
429 (gnus-icalendar--format-summary-line summary location)))
431 (org-entry-put (point) (car prop) (cdr prop)))
436 (narrow-to-region (point) (point))
438 (indent-region (point-min) (point-max) 2)
439 (fill-region (point-min) (point-max))))
443 (defun gnus-icalendar--deactivate-org-timestamp (ts)
444 (replace-regexp-in-string "[<>]"
445 (lambda (m) (cond ((string= m "<") "[")
446 ((string= m ">") "]")))
449 (defun gnus-icalendar-find-org-event-file (event &optional org-file)
450 "Return the name of the file containing EVENT org entry.
451 Return nil when not found.
453 All org agenda files are searched for the EVENT entry. When
454 the optional ORG-FILE argument is specified, only that one file
456 (let ((uid (gnus-icalendar-event:uid event))
457 (files (or org-file (org-agenda-files t 'ifmode))))
459 ((find-event-in (file)
460 (org-check-agenda-file file)
461 (with-current-buffer (find-file-noselect file)
462 (let ((event-pos (org-find-entry-with-id uid)))
464 (string= (cdr (assoc "ICAL_EVENT" (org-entry-properties event-pos)))
466 (throw 'found file))))))
468 (gnus-icalendar-find-if #'find-event-in files))))
471 (defun gnus-icalendar--show-org-event (event &optional org-file)
472 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
474 (switch-to-buffer (find-file file))
475 (goto-char (org-find-entry-with-id (gnus-icalendar-event:uid event)))
479 (defun gnus-icalendar--update-org-event (event reply-status &optional org-file)
480 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
482 (with-current-buffer (find-file-noselect file)
483 (with-slots (uid summary description organizer location recur
484 participation-required req-participants opt-participants) event
485 (let ((event-pos (org-find-entry-with-id uid)))
487 (goto-char event-pos)
489 ;; update the headline, keep todo, priority and tags, if any
491 (let* ((priority (org-entry-get (point) "PRIORITY"))
492 (headline (delq nil (list
493 (org-entry-get (point) "TODO")
494 (when priority (format "[#%s]" priority))
495 (gnus-icalendar--format-summary-line summary location)
496 (org-entry-get (point) "TAGS")))))
498 (re-search-forward "^\\*+ " (line-end-position))
499 (delete-region (point) (line-end-position))
500 (insert (mapconcat #'identity headline " "))))
502 ;; update props and description
503 (let ((entry-end (org-entry-end-position))
504 (entry-outline-level (org-outline-level)))
506 ;; delete body of the entry, leave org drawers intact
508 (org-narrow-to-element)
509 (goto-char entry-end)
510 (re-search-backward "^[\t ]*:END:")
512 (delete-region (point) entry-end))
514 ;; put new event description in the entry body
517 (narrow-to-region (point) (point))
518 (insert "\n" (replace-regexp-in-string "[\n]+$" "\n" description) "\n")
519 (indent-region (point-min) (point-max) (1+ entry-outline-level))
520 (fill-region (point-min) (point-max))))
522 ;; update entry properties
523 (org-entry-put event-pos "DT" (gnus-icalendar-event:org-timestamp event))
524 (org-entry-put event-pos "ORGANIZER" organizer)
525 (org-entry-put event-pos "LOCATION" location)
526 (org-entry-put event-pos "PARTICIPATION_REQUIRED" (when participation-required "t"))
527 (org-entry-put event-pos "REQ_PARTICIPANTS" (gnus-icalendar--format-participant-list req-participants))
528 (org-entry-put event-pos "OPT_PARTICIPANTS" (gnus-icalendar--format-participant-list opt-participants))
529 (org-entry-put event-pos "RRULE" recur)
530 (when reply-status (org-entry-put event-pos "REPLY"
531 (capitalize (symbol-name reply-status))))
532 (save-buffer)))))))))
535 (defun gnus-icalendar--cancel-org-event (event &optional org-file)
536 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
538 (with-current-buffer (find-file-noselect file)
539 (let ((event-pos (org-find-entry-with-id (gnus-icalendar-event:uid event))))
541 (let ((ts (org-entry-get event-pos "DT")))
543 (org-entry-put event-pos "DT" (gnus-icalendar--deactivate-org-timestamp ts))
544 (save-buffer)))))))))
547 (defun gnus-icalendar--get-org-event-reply-status (event &optional org-file)
548 (let ((file (gnus-icalendar-find-org-event-file event org-file)))
551 (with-current-buffer (find-file-noselect file)
552 (let ((event-pos (org-find-entry-with-id (gnus-icalendar-event:uid event))))
553 (org-entry-get event-pos "REPLY")))))))
556 (defun gnus-icalendar-insinuate-org-templates ()
557 (unless (gnus-icalendar-find-if (lambda (x) (string= (cadr x) gnus-icalendar-org-template-name))
558 org-capture-templates)
559 (setq org-capture-templates
560 (append `((,gnus-icalendar-org-template-key
561 ,gnus-icalendar-org-template-name
563 (file+olp ,gnus-icalendar-org-capture-file ,@gnus-icalendar-org-capture-headline)
565 :immediate-finish t))
566 org-capture-templates))
568 ;; hide the template from interactive template selection list
570 ;; NOTE: doesn't work when capturing from string
571 ;; (when (boundp 'org-capture-templates-contexts)
572 ;; (push `(,gnus-icalendar-org-template-key "" ((in-mode . "gnus-article-mode")))
573 ;; org-capture-templates-contexts))
576 (defun gnus-icalendar:org-event-save (event reply-status)
578 (org-capture-string (gnus-icalendar-event->org-entry event reply-status)
579 gnus-icalendar-org-template-key)))
581 (defun gnus-icalendar-show-org-agenda (event)
582 (let* ((time-delta (time-subtract (gnus-icalendar-event:end-time event)
583 (gnus-icalendar-event:start-time event)))
584 (duration-days (1+ (/ (+ (* (car time-delta) (expt 2 16))
588 (org-agenda-list nil (gnus-icalendar-event:start event) duration-days)))
590 (defmethod gnus-icalendar-event:sync-to-org ((event gnus-icalendar-event-request) reply-status)
591 (if (gnus-icalendar-find-org-event-file event)
592 (gnus-icalendar--update-org-event event reply-status)
593 (gnus-icalendar:org-event-save event reply-status)))
595 (defmethod gnus-icalendar-event:sync-to-org ((event gnus-icalendar-event-cancel) reply-status)
596 (when (gnus-icalendar-find-org-event-file event)
597 (gnus-icalendar--cancel-org-event event)))
599 (defun gnus-icalendar-org-setup ()
600 (if (and gnus-icalendar-org-capture-file gnus-icalendar-org-capture-headline)
602 (gnus-icalendar-insinuate-org-templates)
603 (setq gnus-icalendar-org-enabled-p t))
604 (message "Cannot enable Calendar->Org: missing capture file, headline")))
610 (defgroup gnus-icalendar nil
611 "Settings for inline display of iCalendar invitations."
613 :prefix "gnus-icalendar-")
615 (defcustom gnus-icalendar-reply-bufname "*CAL*"
616 "Buffer used for building iCalendar invitation reply."
618 :group 'gnus-icalendar)
620 (make-variable-buffer-local
621 (defvar gnus-icalendar-reply-status nil))
623 (make-variable-buffer-local
624 (defvar gnus-icalendar-event nil))
626 (make-variable-buffer-local
627 (defvar gnus-icalendar-handle nil))
629 (defvar gnus-icalendar-identities
631 (mapcar (lambda (x) (if (listp x) x (list x)))
632 (list user-full-name (regexp-quote user-mail-address)
633 ; NOTE: this one can be a list
634 gnus-ignored-from-addresses))))
636 ;; TODO: make the template customizable
637 (defmethod gnus-icalendar-event->gnus-calendar ((event gnus-icalendar-event) &optional reply-status)
638 "Format an overview of EVENT details."
639 (gmm-labels ((format-header (x)
641 (propertize (concat (car x) ":") 'face 'bold)
644 (with-slots (organizer summary description location recur uid
645 method rsvp participation-required) event
646 (let ((headers `(("Summary" ,summary)
647 ("Location" ,(or location ""))
648 ("Time" ,(gnus-icalendar-event:org-timestamp event))
649 ("Organizer" ,organizer)
650 ("Attendance" ,(if participation-required "Required" "Optional"))
651 ("Method" ,method))))
653 (when (and (not (gnus-icalendar-event-reply-p event)) rsvp)
654 (setq headers (append headers
655 `(("Status" ,(or reply-status "Not replied yet"))))))
658 (mapconcat #'format-header headers "\n")
662 (defmacro gnus-icalendar-with-decoded-handle (handle &rest body)
663 "Execute BODY in buffer containing the decoded contents of HANDLE."
664 (let ((charset (make-symbol "charset")))
665 `(let ((,charset (cdr (assoc 'charset (mm-handle-type ,handle)))))
667 (mm-insert-part ,handle)
668 (when (string= ,charset "utf-8")
669 (mm-decode-coding-region (point-min) (point-max) 'utf-8))
674 (defun gnus-icalendar-event-from-handle (handle &optional attendee-name-or-email)
675 (gnus-icalendar-with-decoded-handle handle
676 (gnus-icalendar-event-from-buffer (current-buffer) attendee-name-or-email)))
678 (defun gnus-icalendar-insert-button (text callback data)
679 ;; FIXME: the gnus-mime-button-map keymap does not make sense for this kind
681 (let ((start (point)))
682 (gnus-add-text-properties
685 (insert "[ " text " ]")
689 keymap ,gnus-mime-button-map
690 face ,gnus-article-button-face
692 (widget-convert-button 'link start (point)
693 :action 'gnus-widget-press-button
694 :button-keymap gnus-widget-button-keymap)))
696 (defun gnus-icalendar-send-buffer-by-mail (buffer-name subject)
697 (let ((message-signature nil))
698 (with-current-buffer gnus-summary-buffer
701 (mml-insert-multipart "alternative")
702 (mml-insert-empty-tag 'part 'type "text/plain")
703 (mml-attach-buffer buffer-name "text/calendar; method=REPLY; charset=UTF-8")
704 (message-goto-subject)
705 (delete-region (line-beginning-position) (line-end-position))
706 (insert "Subject: " subject)
707 (message-send-and-exit))))
709 (defun gnus-icalendar-reply (data)
710 (let* ((handle (car data))
713 (reply (gnus-icalendar-with-decoded-handle handle
714 (gnus-icalendar-event-reply-from-buffer
715 (current-buffer) status gnus-icalendar-identities))))
718 (gmm-labels ((fold-icalendar-buffer ()
719 (goto-char (point-min))
720 (while (re-search-forward "^\\(.\\{72\\}\\)\\(.+\\)$" nil t)
721 (replace-match "\\1\n \\2")
722 (goto-char (line-beginning-position)))))
723 (let ((subject (concat (capitalize (symbol-name status))
724 ": " (gnus-icalendar-event:summary event))))
726 (with-current-buffer (get-buffer-create gnus-icalendar-reply-bufname)
727 (delete-region (point-min) (point-max))
729 (fold-icalendar-buffer)
730 (gnus-icalendar-send-buffer-by-mail (buffer-name) subject))
732 ;; Back in article buffer
733 (setq-local gnus-icalendar-reply-status status)
734 (when gnus-icalendar-org-enabled-p
735 (gnus-icalendar--update-org-event event status)
736 ;; refresh article buffer to update the reply status
737 (with-current-buffer gnus-summary-buffer
738 (gnus-summary-show-article))))))))
740 (defun gnus-icalendar-sync-event-to-org (event)
741 (gnus-icalendar-event:sync-to-org event gnus-icalendar-reply-status))
743 (defmethod gnus-icalendar-event:inline-reply-buttons ((event gnus-icalendar-event) handle)
744 (when (gnus-icalendar-event:rsvp event)
745 `(("Accept" gnus-icalendar-reply (,handle accepted ,event))
746 ("Tentative" gnus-icalendar-reply (,handle tentative ,event))
747 ("Decline" gnus-icalendar-reply (,handle declined ,event)))))
749 (defmethod gnus-icalendar-event:inline-reply-buttons ((event gnus-icalendar-event-reply) handle)
750 "No buttons for REPLY events."
753 (defmethod gnus-icalendar-event:inline-reply-status ((event gnus-icalendar-event))
754 (or (when gnus-icalendar-org-enabled-p
755 (gnus-icalendar--get-org-event-reply-status event))
758 (defmethod gnus-icalendar-event:inline-reply-status ((event gnus-icalendar-event-reply))
759 "No reply status for REPLY events."
763 (defmethod gnus-icalendar-event:inline-org-buttons ((event gnus-icalendar-event))
764 (let* ((org-entry-exists-p (gnus-icalendar-find-org-event-file event))
765 (export-button-text (if org-entry-exists-p "Update Org Entry" "Export to Org")))
768 `("Show Agenda" gnus-icalendar-show-org-agenda ,event)
769 (when (gnus-icalendar-event-request-p event)
770 `(,export-button-text gnus-icalendar-sync-event-to-org ,event))
771 (when org-entry-exists-p
772 `("Show Org Entry" gnus-icalendar--show-org-event ,event))))))
775 (defmethod gnus-icalendar-event:inline-org-buttons ((event gnus-icalendar-event-cancel))
776 (let ((org-entry-exists-p (gnus-icalendar-find-org-event-file event)))
779 `("Show Agenda" gnus-icalendar-show-org-agenda ,event)
780 (when org-entry-exists-p
781 `("Update Org Entry" gnus-icalendar-sync-event-to-org ,event))
782 (when org-entry-exists-p
783 `("Show Org Entry" gnus-icalendar--show-org-event ,event))))))
786 (defun gnus-icalendar-mm-inline (handle)
787 (let ((event (gnus-icalendar-event-from-handle handle gnus-icalendar-identities)))
789 (setq gnus-icalendar-reply-status nil)
792 (gmm-labels ((insert-button-group (buttons)
795 (apply 'gnus-icalendar-insert-button x)
801 (gnus-icalendar-event:inline-reply-buttons event handle))
803 (when gnus-icalendar-org-enabled-p
804 (insert-button-group (gnus-icalendar-event:inline-org-buttons event)))
806 (setq gnus-icalendar-event event
807 gnus-icalendar-handle handle)
809 (insert (gnus-icalendar-event->gnus-calendar
811 (gnus-icalendar-event:inline-reply-status event)))))))
813 (defun gnus-icalendar-save-part (handle)
815 (when (and (equal (car (mm-handle-type handle)) "text/calendar")
816 (setq event (gnus-icalendar-event-from-handle handle gnus-icalendar-identities)))
818 (gnus-icalendar-event:sync-to-org event))))
821 (defun gnus-icalendar-save-event ()
822 "Save the Calendar event in the text/calendar part under point."
824 (gnus-article-check-buffer)
825 (let ((data (get-text-property (point) 'gnus-data)))
827 (gnus-icalendar-save-part data))))
829 (defun gnus-icalendar-reply-accept ()
830 "Accept invitation in the current article."
832 (with-current-buffer gnus-article-buffer
833 (gnus-icalendar-reply (list gnus-icalendar-handle 'accepted gnus-icalendar-event))
834 (setq-local gnus-icalendar-reply-status 'accepted)))
836 (defun gnus-icalendar-reply-tentative ()
837 "Send tentative response to invitation in the current article."
839 (with-current-buffer gnus-article-buffer
840 (gnus-icalendar-reply (list gnus-icalendar-handle 'tentative gnus-icalendar-event))
841 (setq-local gnus-icalendar-reply-status 'tentative)))
843 (defun gnus-icalendar-reply-decline ()
844 "Decline invitation in the current article."
846 (with-current-buffer gnus-article-buffer
847 (gnus-icalendar-reply (list gnus-icalendar-handle 'declined gnus-icalendar-event))
848 (setq-local gnus-icalendar-reply-status 'declined)))
850 (defun gnus-icalendar-event-export ()
851 "Export calendar event to `org-mode', or update existing agenda entry."
853 (with-current-buffer gnus-article-buffer
854 (gnus-icalendar-sync-event-to-org gnus-icalendar-event))
855 ;; refresh article buffer in case the reply had been sent before initial org
857 (with-current-buffer gnus-summary-buffer
858 (gnus-summary-show-article)))
860 (defun gnus-icalendar-event-show ()
861 "Display `org-mode' agenda entry related to the calendar event."
863 (gnus-icalendar--show-org-event
864 (with-current-buffer gnus-article-buffer
865 gnus-icalendar-event)))
867 (defun gnus-icalendar-event-check-agenda ()
868 "Display `org-mode' agenda for days between event start and end dates."
870 (gnus-icalendar-show-org-agenda
871 (with-current-buffer gnus-article-buffer gnus-icalendar-event)))
873 (defvar gnus-mime-action-alist) ; gnus-art
875 (defun gnus-icalendar-setup ()
876 (add-to-list 'mm-inlined-types "text/calendar")
877 (add-to-list 'mm-automatic-display "text/calendar")
878 (add-to-list 'mm-inline-media-tests '("text/calendar" gnus-icalendar-mm-inline identity))
880 (gnus-define-keys (gnus-summary-calendar-map "i" gnus-summary-mode-map)
881 "a" gnus-icalendar-reply-accept
882 "t" gnus-icalendar-reply-tentative
883 "d" gnus-icalendar-reply-decline
884 "c" gnus-icalendar-event-check-agenda
885 "e" gnus-icalendar-event-export
886 "s" gnus-icalendar-event-show)
889 (add-to-list 'gnus-mime-action-alist
890 (cons "save calendar event" 'gnus-icalendar-save-event)
893 (provide 'gnus-icalendar)
895 ;;; gnus-icalendar.el ends here