* lisp/emacs-lisp/gv.el (gv-define-setter): Fix doc typo.
[emacs.git] / lisp / erc / erc-desktop-notifications.el
blob2cc3c80a8ea42ba6b5a0ee2670361b5f102a2e47
1 ;; erc-desktop-notifications.el -- Send notification on PRIVMSG or mentions
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: comm
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/>.
23 ;;; Commentary:
25 ;; This implements notifications using `notifications-notify' on
26 ;; PRIVMSG received and on public nickname mentions.
28 ;;; Code:
30 (require 'erc)
31 (require 'xml)
32 (require 'notifications)
33 (require 'erc-match)
34 (require 'dbus)
36 (defgroup erc-notifications nil
37 "Send notifications on PRIVMSG or mentions."
38 :version "24.3"
39 :group 'erc)
41 (defvar erc-notifications-last-notification nil
42 "Last notification id.")
44 (defcustom erc-notifications-icon nil
45 "Icon to use for notification."
46 :group 'erc-notifications
47 :type 'file)
49 (defun erc-notifications-notify (nick msg)
50 "Notify that NICK send some MSG.
51 This will replace the last notification sent with this function."
52 (dbus-ignore-errors
53 (setq erc-notifications-last-notification
54 (notifications-notify :title (xml-escape-string nick)
55 :body (xml-escape-string msg)
56 :replaces-id erc-notifications-last-notification
57 :app-icon erc-notifications-icon))))
59 (defun erc-notifications-PRIVMSG (proc parsed)
60 (let ((nick (car (erc-parse-user (erc-response.sender parsed))))
61 (target (car (erc-response.command-args parsed)))
62 (msg (erc-response.contents parsed)))
63 (when (and (erc-current-nick-p target)
64 (not (and (boundp 'erc-track-exclude)
65 (member nick erc-track-exclude)))
66 (not (erc-is-message-ctcp-and-not-action-p msg)))
67 (erc-notifications-notify nick msg)))
68 ;; Return nil to continue processing by ERC
69 nil)
71 (defun erc-notifications-notify-on-match (match-type nickuserhost msg)
72 (when (eq match-type 'current-nick)
73 (let ((nick (nth 0 (erc-parse-user nickuserhost))))
74 (unless (or (string-match-p "^Server:" nick)
75 (when (boundp 'erc-track-exclude)
76 (member nick erc-track-exclude)))
77 (erc-notifications-notify nick msg)))))
79 ;;;###autoload(autoload 'erc-notifications-mode "erc-desktop-notifications" "" t)
80 (define-erc-module notifications nil
81 "Send notifications on private message reception and mentions."
82 ;; Enable
83 ((add-hook 'erc-server-PRIVMSG-functions 'erc-notifications-PRIVMSG)
84 (add-hook 'erc-text-matched-hook 'erc-notifications-notify-on-match))
85 ;; Disable
86 ((remove-hook 'erc-server-PRIVMSG-functions 'erc-notifications-PRIVMSG)
87 (remove-hook 'erc-text-matched-hook 'erc-notifications-notify-on-match)))
89 (provide 'erc-desktop-notifications)
91 ;;; erc-desktop-notifications.el ends here