erc-desktop-notifications.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;; erc-desktop-notifications.el -- Send notification on PRIVMSG or mentions
  2. ;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
  3. ;; Author: Julien Danjou <julien@danjou.info>
  4. ;; Keywords: comm
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This implements notifications using `notifications-notify' on
  18. ;; PRIVMSG received and on public nickname mentions.
  19. ;;; Code:
  20. (require 'erc)
  21. (require 'xml)
  22. (require 'notifications)
  23. (require 'erc-match)
  24. (require 'dbus)
  25. (defgroup erc-notifications nil
  26. "Send notifications on PRIVMSG or mentions."
  27. :version "24.3"
  28. :group 'erc)
  29. (defvar erc-notifications-last-notification nil
  30. "Last notification id.")
  31. (defcustom erc-notifications-icon nil
  32. "Icon to use for notification."
  33. :group 'erc-notifications
  34. :type '(choice (const :tag "No icon" nil) file))
  35. (defcustom erc-notifications-bus :session
  36. "D-Bus bus to use for notification."
  37. :version "25.1"
  38. :group 'erc-notifications
  39. :type '(choice (const :tag "Session bus" :session) string))
  40. (defvar dbus-debug) ; used in the macroexpansion of dbus-ignore-errors
  41. (defun erc-notifications-notify (nick msg)
  42. "Notify that NICK send some MSG.
  43. This will replace the last notification sent with this function."
  44. (dbus-ignore-errors
  45. (setq erc-notifications-last-notification
  46. (notifications-notify :bus erc-notifications-bus
  47. :title (xml-escape-string nick)
  48. :body (xml-escape-string msg)
  49. :replaces-id erc-notifications-last-notification
  50. :app-icon erc-notifications-icon))))
  51. (defun erc-notifications-PRIVMSG (proc parsed)
  52. (let ((nick (car (erc-parse-user (erc-response.sender parsed))))
  53. (target (car (erc-response.command-args parsed)))
  54. (msg (erc-response.contents parsed)))
  55. (when (and (erc-current-nick-p target)
  56. (not (and (boundp 'erc-track-exclude)
  57. (member nick erc-track-exclude)))
  58. (not (erc-is-message-ctcp-and-not-action-p msg)))
  59. (erc-notifications-notify nick msg)))
  60. ;; Return nil to continue processing by ERC
  61. nil)
  62. (defun erc-notifications-notify-on-match (match-type nickuserhost msg)
  63. (when (eq match-type 'current-nick)
  64. (let ((nick (nth 0 (erc-parse-user nickuserhost))))
  65. (unless (or (string-match-p "^Server:" nick)
  66. (when (boundp 'erc-track-exclude)
  67. (member nick erc-track-exclude)))
  68. (erc-notifications-notify nick msg)))))
  69. ;;;###autoload(autoload 'erc-notifications-mode "erc-desktop-notifications" "" t)
  70. (define-erc-module notifications nil
  71. "Send notifications on private message reception and mentions."
  72. ;; Enable
  73. ((add-hook 'erc-server-PRIVMSG-functions 'erc-notifications-PRIVMSG)
  74. (add-hook 'erc-text-matched-hook 'erc-notifications-notify-on-match))
  75. ;; Disable
  76. ((remove-hook 'erc-server-PRIVMSG-functions 'erc-notifications-PRIVMSG)
  77. (remove-hook 'erc-text-matched-hook 'erc-notifications-notify-on-match)))
  78. (provide 'erc-desktop-notifications)
  79. ;;; erc-desktop-notifications.el ends here