]> code.delx.au - gnu-emacs/blob - lisp/gnus/gnus-notifications.el
gnus-notifications.el: Add defcustom for timeout and actions support
[gnu-emacs] / lisp / gnus / gnus-notifications.el
1 ;; gnus-notifications.el -- Send notification on new message in Gnus
2
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This implements notifications using `notifications-notify' on new
26 ;; messages received.
27 ;; Use (add-hook 'gnus-after-getting-new-news-hook 'gnus-notifications)
28 ;; to get notifications just after getting the new news.
29
30 ;;; Code:
31
32 (require 'notifications nil t)
33 (require 'gnus-sum)
34 (require 'gnus-group)
35 (require 'gnus-int)
36 (require 'gnus-art)
37 (require 'gnus-util)
38 (require 'google-contacts nil t) ; Optional
39
40 (defgroup gnus-notifications nil
41 "Send notifications on new message in Gnus."
42 :group 'gnus)
43
44 (defcustom gnus-notifications-use-google-contacts t
45 "Use Google Contacts to retrieve photo."
46 :type 'boolean
47 :group 'gnus-notifications)
48
49 (defcustom gnus-notifications-use-gravatar t
50 "Use Gravatar to retrieve photo."
51 :type 'boolean
52 :group 'gnus-notifications)
53
54 (defcustom gnus-notifications-minimum-level 1
55 "Minimum group level the message should have to be notified.
56 Any message in a group that has a greater value than this will
57 not get notifications."
58 :type 'integer
59 :group 'gnus-notifications)
60
61 (defcustom gnus-notifications-timeout nil
62 "Timeout used for notifications sent via `notifications-notify'."
63 :type 'integer
64 :group 'gnus-notifications)
65
66 (defvar gnus-notifications-sent nil
67 "Notifications already sent.")
68
69 (defvar gnus-notifications-id-to-msg nil
70 "Map notifications ids to messages.")
71
72 (defun gnus-notifications-action (id key)
73 (when (string= key "read")
74 (let ((group-article (assoc id gnus-notifications-id-to-msg)))
75 (when group-article
76 (let ((group (cadr group-article))
77 (article (caddr group-article)))
78 (gnus-fetch-group group (list article)))))))
79
80 (defun gnus-notifications-notify (from subject photo-file)
81 "Send a notification about a new mail.
82 Return a notification id if any, or t on success."
83 (if (fboundp 'notifications-notify)
84 (notifications-notify
85 :title from
86 :body subject
87 :actions '("read" "Read")
88 :on-action 'gnus-notifications-action
89 :app-icon (image-search-load-path "gnus/gnus.png")
90 :app-name "Gnus"
91 :category "email.arrived"
92 :timeout gnus-notifications-timeout
93 :image-path photo-file)
94 (message "New message from %s: %s" from subject)
95 ;; Don't return an id
96 t))
97
98 (defun gnus-notifications-get-photo (mail-address)
99 "Get photo for mail address."
100 (let ((google-photo (when (and gnus-notifications-use-google-contacts
101 (fboundp 'google-contacts-get-photo))
102 (ignore-errors
103 (google-contacts-get-photo mail-address)))))
104 (if google-photo
105 google-photo
106 (when gnus-notifications-use-gravatar
107 (let ((gravatar (ignore-errors
108 (gravatar-retrieve-synchronously mail-address))))
109 (if (eq gravatar 'error)
110 nil
111 (plist-get (cdr gravatar) :data)))))))
112
113 (defun gnus-notifications-get-photo-file (mail-address)
114 "Get a temporary file with an image for MAIL-ADDRESS.
115 You have to delete the temporary image yourself using
116 `delete-image'.
117
118 Returns nil if no image found."
119 (let ((photo (gnus-notifications-get-photo mail-address)))
120 (when photo
121 (let ((photo-file (make-temp-file "gnus-notifications-photo-"))
122 (coding-system-for-write 'binary))
123 (with-temp-file photo-file
124 (insert photo))
125 photo-file))))
126
127 ;;;###autoload
128 (defun gnus-notifications ()
129 "Send a notification on new message.
130 This check for new messages that are in group with a level lower
131 or equal to `gnus-notifications-minimum-level' and send a
132 notification using `notifications-notify' for it.
133
134 This is typically a function to add in
135 `gnus-after-getting-new-news-hook'"
136 (dolist (entry gnus-newsrc-alist)
137 (let ((group (car entry)))
138 ;; Check that the group level is less than
139 ;; `gnus-notifications-minimum-level' and the the group has unread
140 ;; messages.
141 (when (and (<= (gnus-group-level group) gnus-notifications-minimum-level)
142 (let ((unread (gnus-group-unread group)))
143 (and (numberp unread)
144 (> unread 0))))
145 ;; Each group should have an entry in the `gnus-notifications-sent'
146 ;; alist. If not, we add one at this time.
147 (let ((group-notifications (or (assoc group gnus-notifications-sent)
148 ;; Nothing, add one and return it.
149 (assoc group
150 (add-to-list
151 'gnus-notifications-sent
152 (cons group nil))))))
153 (dolist (article (gnus-list-of-unread-articles group))
154 ;; Check if the article already has been notified
155 (unless (memq article (cdr group-notifications))
156 (with-current-buffer nntp-server-buffer
157 (gnus-request-head article group)
158 (article-decode-encoded-words) ; to decode mail addresses, subjects, etc
159 (let* ((address-components (mail-extract-address-components
160 (or (mail-fetch-field "From") "")))
161 (address (cadr address-components)))
162 ;; Ignore mails from ourselves
163 (unless (gnus-string-match-p gnus-ignored-from-addresses
164 address)
165 (let* ((photo-file (gnus-notifications-get-photo-file address))
166 (notification-id (gnus-notifications-notify
167 (or (car address-components) address)
168 (mail-fetch-field "Subject")
169 photo-file)))
170 (when notification-id
171 ;; Register that we did notify this message
172 (setcdr group-notifications (cons article (cdr group-notifications)))
173 (unless (eq notification-id t)
174 ;; Register the notification id for later actions
175 (add-to-list 'gnus-notifications-id-to-msg (list notification-id group article))))
176 (when photo-file
177 (delete-file photo-file)))))))))))))
178
179 (provide 'gnus-notifications)
180
181 ;;; gnus-notifications.el ends here