1 ;; gnus-notifications.el -- Send notification on new message in Gnus
3 ;; Copyright (C) 2012 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.
32 (require 'notifications nil t)
38 (require 'google-contacts nil t) ; Optional
40 (defgroup gnus-notifications nil
41 "Send notifications on new message in Gnus."
44 (defcustom gnus-notifications-use-google-contacts t
45 "Use Google Contacts to retrieve photo."
47 :group 'gnus-notifications)
49 (defcustom gnus-notifications-use-gravatar t
50 "Use Gravatar to retrieve photo."
52 :group 'gnus-notifications)
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."
59 :group 'gnus-notifications)
61 (defcustom gnus-notifications-timeout nil
62 "Timeout used for notifications sent via `notifications-notify'."
64 :group 'gnus-notifications)
66 (defvar gnus-notifications-sent nil
67 "Notifications already sent.")
69 (defvar gnus-notifications-id-to-msg nil
70 "Map notifications ids to messages.")
72 (defun gnus-notifications-action (id key)
73 (when (string= key "read")
74 (let ((group-article (assoc id gnus-notifications-id-to-msg)))
76 (let ((group (cadr group-article))
77 (article (caddr group-article)))
78 (gnus-fetch-group group (list article)))))))
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)
87 :actions '("read" "Read")
88 :on-action 'gnus-notifications-action
89 :app-icon (image-search-load-path "gnus/gnus.png")
91 :category "email.arrived"
92 :timeout gnus-notifications-timeout
93 :image-path photo-file)
94 (message "New message from %s: %s" from subject)
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))
103 (google-contacts-get-photo mail-address)))))
106 (when gnus-notifications-use-gravatar
107 (let ((gravatar (ignore-errors
108 (gravatar-retrieve-synchronously mail-address))))
109 (if (eq gravatar 'error)
111 (plist-get (cdr gravatar) :data)))))))
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
118 Returns nil if no image found."
119 (let ((photo (gnus-notifications-get-photo mail-address)))
121 (let ((photo-file (make-temp-file "gnus-notifications-photo-"))
122 (coding-system-for-write 'binary))
123 (with-temp-file photo-file
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.
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
141 (when (and (<= (gnus-group-level group) gnus-notifications-minimum-level)
142 (let ((unread (gnus-group-unread group)))
143 (and (numberp unread)
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.
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
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")
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))))
177 (delete-file photo-file)))))))))))))
179 (provide 'gnus-notifications)
181 ;;; gnus-notifications.el ends here