Commit | Line | Data |
---|---|---|
6938736c | 1 | ;; erc-desktop-notifications.el -- Send notification on PRIVMSG or mentions |
f1e8a7f1 | 2 | |
ba318903 | 3 | ;; Copyright (C) 2012-2014 Free Software Foundation, Inc. |
f1e8a7f1 JD |
4 | |
5 | ;; Author: Julien Danjou <julien@danjou.info> | |
6 | ;; Keywords: comm | |
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 | |
26 | ;; PRIVMSG received and on public nickname mentions. | |
27 | ||
28 | ;;; Code: | |
29 | ||
30 | (require 'erc) | |
31 | (require 'xml) | |
32 | (require 'notifications) | |
33 | (require 'erc-match) | |
34 | (require 'dbus) | |
35 | ||
36 | (defgroup erc-notifications nil | |
37 | "Send notifications on PRIVMSG or mentions." | |
d1a1c7e6 | 38 | :version "24.3" |
f1e8a7f1 JD |
39 | :group 'erc) |
40 | ||
41 | (defvar erc-notifications-last-notification nil | |
42 | "Last notification id.") | |
43 | ||
44 | (defcustom erc-notifications-icon nil | |
45 | "Icon to use for notification." | |
46 | :group 'erc-notifications | |
a931698a | 47 | :type '(choice (const :tag "No icon" nil) file)) |
f1e8a7f1 | 48 | |
1143ebe1 GM |
49 | (defvar dbus-debug) ; used in the macroexpansion of dbus-ignore-errors |
50 | ||
f1e8a7f1 JD |
51 | (defun erc-notifications-notify (nick msg) |
52 | "Notify that NICK send some MSG. | |
53 | This will replace the last notification sent with this function." | |
54 | (dbus-ignore-errors | |
55 | (setq erc-notifications-last-notification | |
56 | (notifications-notify :title (xml-escape-string nick) | |
57 | :body (xml-escape-string msg) | |
58 | :replaces-id erc-notifications-last-notification | |
59 | :app-icon erc-notifications-icon)))) | |
60 | ||
61 | (defun erc-notifications-PRIVMSG (proc parsed) | |
62 | (let ((nick (car (erc-parse-user (erc-response.sender parsed)))) | |
63 | (target (car (erc-response.command-args parsed))) | |
64 | (msg (erc-response.contents parsed))) | |
65 | (when (and (erc-current-nick-p target) | |
66 | (not (and (boundp 'erc-track-exclude) | |
67 | (member nick erc-track-exclude))) | |
68 | (not (erc-is-message-ctcp-and-not-action-p msg))) | |
69 | (erc-notifications-notify nick msg))) | |
70 | ;; Return nil to continue processing by ERC | |
71 | nil) | |
72 | ||
73 | (defun erc-notifications-notify-on-match (match-type nickuserhost msg) | |
74 | (when (eq match-type 'current-nick) | |
75 | (let ((nick (nth 0 (erc-parse-user nickuserhost)))) | |
76 | (unless (or (string-match-p "^Server:" nick) | |
77 | (when (boundp 'erc-track-exclude) | |
78 | (member nick erc-track-exclude))) | |
79 | (erc-notifications-notify nick msg))))) | |
80 | ||
6938736c | 81 | ;;;###autoload(autoload 'erc-notifications-mode "erc-desktop-notifications" "" t) |
f1e8a7f1 JD |
82 | (define-erc-module notifications nil |
83 | "Send notifications on private message reception and mentions." | |
84 | ;; Enable | |
85 | ((add-hook 'erc-server-PRIVMSG-functions 'erc-notifications-PRIVMSG) | |
86 | (add-hook 'erc-text-matched-hook 'erc-notifications-notify-on-match)) | |
87 | ;; Disable | |
88 | ((remove-hook 'erc-server-PRIVMSG-functions 'erc-notifications-PRIVMSG) | |
89 | (remove-hook 'erc-text-matched-hook 'erc-notifications-notify-on-match))) | |
90 | ||
6938736c | 91 | (provide 'erc-desktop-notifications) |
f1e8a7f1 | 92 | |
6938736c | 93 | ;;; erc-desktop-notifications.el ends here |