1 ;; gnus-notifications.el -- Send notification on new message in Gnus
3 ;; Copyright (C) 2012-2015 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; This implements notifications using `notifications-notify' on new
27 ;; Use (add-hook 'gnus-after-getting-new-news-hook 'gnus-notifications)
28 ;; to get notifications just after getting the new news.
33 (require 'notifications))
40 (require 'google-contacts)) ; Optional
43 (defgroup gnus-notifications nil
44 "Send notifications on new message in Gnus."
48 (defcustom gnus-notifications-use-google-contacts t
49 "Use Google Contacts to retrieve photo."
51 :group 'gnus-notifications)
53 (defcustom gnus-notifications-use-gravatar t
54 "Use Gravatar to retrieve photo."
56 :group 'gnus-notifications)
58 (defcustom gnus-notifications-minimum-level 1
59 "Minimum group level the message should have to be notified.
60 Any message in a group that has a greater value than this will
61 not get notifications."
63 :group 'gnus-notifications)
65 (defcustom gnus-notifications-timeout nil
66 "Timeout used for notifications sent via `notifications-notify'."
67 :type '(choice (const :tag "Server default" nil)
68 (integer :tag "Milliseconds"))
69 :group 'gnus-notifications)
71 (defvar gnus-notifications-sent nil
72 "Notifications already sent.")
74 (defvar gnus-notifications-id-to-msg nil
75 "Map notifications ids to messages.")
77 (defun gnus-notifications-action (id key)
78 (when (string= key "read")
79 (let ((group-article (assoc id gnus-notifications-id-to-msg)))
81 (let ((group (cadr group-article))
82 (article (nth 2 group-article)))
83 (gnus-fetch-group group (list article))
84 (when (window-system) (x-focus-frame (selected-frame))))))))
86 (defun gnus-notifications-notify (from subject photo-file)
87 "Send a notification about a new mail.
88 Return a notification id if any, or t on success."
89 (if (fboundp 'notifications-notify)
90 (gnus-funcall-no-warning
94 :actions '("read" "Read")
95 :on-action 'gnus-notifications-action
96 :app-icon (gnus-funcall-no-warning
97 'image-search-load-path "gnus/gnus.png")
98 :image-path photo-file
100 :category "email.arrived"
101 :timeout gnus-notifications-timeout)
102 (message "New message from %s: %s" from subject)
103 ;; Don't return an id
106 (declare-function gravatar-retrieve-synchronously "gravatar.el"
109 (defun gnus-notifications-get-photo (mail-address)
110 "Get photo for mail address."
111 (let ((google-photo (when (and gnus-notifications-use-google-contacts
112 (fboundp 'google-contacts-get-photo))
114 (gnus-funcall-no-warning
115 'google-contacts-get-photo mail-address)))))
118 (when gnus-notifications-use-gravatar
119 (let ((gravatar (ignore-errors
120 (gravatar-retrieve-synchronously mail-address))))
121 (if (eq gravatar 'error)
123 (plist-get (cdr gravatar) :data)))))))
125 (defun gnus-notifications-get-photo-file (mail-address)
126 "Get a temporary file with an image for MAIL-ADDRESS.
127 You have to delete the temporary image yourself using
130 Returns nil if no image found."
131 (let ((photo (gnus-notifications-get-photo mail-address)))
133 (let ((photo-file (make-temp-file "gnus-notifications-photo-"))
134 (coding-system-for-write 'binary))
135 (with-temp-file photo-file
140 (defun gnus-notifications ()
141 "Send a notification on new message.
142 This check for new messages that are in group with a level lower
143 or equal to `gnus-notifications-minimum-level' and send a
144 notification using `notifications-notify' for it.
146 This is typically a function to add in
147 `gnus-after-getting-new-news-hook'"
148 (dolist (entry gnus-newsrc-alist)
149 (let ((group (car entry)))
150 ;; Check that the group level is less than
151 ;; `gnus-notifications-minimum-level' and the the group has unread
153 (when (and (<= (gnus-group-level group) gnus-notifications-minimum-level)
154 (let ((unread (gnus-group-unread group)))
155 (and (numberp unread)
157 ;; Each group should have an entry in the `gnus-notifications-sent'
158 ;; alist. If not, we add one at this time.
159 (let ((group-notifications (or (assoc group gnus-notifications-sent)
160 ;; Nothing, add one and return it.
163 'gnus-notifications-sent
164 (cons group nil))))))
165 (dolist (article (gnus-list-of-unread-articles group))
166 ;; Check if the article already has been notified
167 (unless (memq article (cdr group-notifications))
168 (with-current-buffer nntp-server-buffer
169 (gnus-request-head article group)
170 (article-decode-encoded-words) ; to decode mail addresses, subjects, etc
171 (let* ((address-components (mail-extract-address-components
172 (or (mail-fetch-field "From") "")))
173 (address (cadr address-components)))
174 ;; Ignore mails from ourselves
175 (unless (and gnus-ignored-from-addresses
177 (gnus-string-match-p gnus-ignored-from-addresses
179 (let* ((photo-file (gnus-notifications-get-photo-file address))
180 (notification-id (gnus-notifications-notify
181 (or (car address-components) address)
182 (mail-fetch-field "Subject")
184 (when notification-id
185 ;; Register that we did notify this message
186 (setcdr group-notifications (cons article (cdr group-notifications)))
187 (unless (eq notification-id t)
188 ;; Register the notification id for later actions
189 (add-to-list 'gnus-notifications-id-to-msg (list notification-id group article))))
191 (delete-file photo-file)))))))))))))
193 (provide 'gnus-notifications)
195 ;;; gnus-notifications.el ends here