1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-21 06:55:39 +00:00
emacs/lisp/erc/erc-desktop-notifications.el
Po Lu 6b40d557c4 Port more notification senders to non-XDG systems
* doc/lispref/os.texi (Desktop Notifications): Document that
`:timeout' is now implemented.

* java/org/gnu/emacs/EmacsDesktopNotification.java
(EmacsDesktopNotification): New field delay.
(display1): Set delay on Android 8.0 and up.

* lisp/erc/erc-desktop-notifications.el
(erc-notifications-notify): Call Android or Haiku notification
functions on those systems.

* lisp/gnus/gnus-notifications.el (gnus-notifications-action)
(gnus-notification-close): Remove dismissed notifications from
the notification to message map.
(gnus-notifications-notify): Call android-notifications-notify
if possible.

* src/androidselect.c (android_init_emacs_desktop_notification):
Update accordingly.
(android_notifications_notify_1): New argument TIMEOUT.
(Fandroid_notifications_notify): New argument QCtimeout.
(syms_of_androidselect) <QCtimeout>: New symbol.
2024-03-13 11:01:39 +08:00

121 lines
4.7 KiB
EmacsLisp

;;; erc-desktop-notifications.el --- Send notification on PRIVMSG or mentions -*- lexical-binding:t -*-
;; Copyright (C) 2012-2024 Free Software Foundation, Inc.
;; Author: Julien Danjou <julien@danjou.info>
;; Maintainer: Amin Bandali <bandali@gnu.org>, F. Jason Park <jp@neverwas.me>
;; Keywords: comm
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This implements notifications using `notifications-notify' on
;; PRIVMSG received and on public nickname mentions.
;;; Code:
(require 'erc)
(require 'xml)
(require 'notifications)
(require 'erc-goodies)
(require 'erc-match)
(require 'dbus)
(defgroup erc-notifications nil
"Send notifications on PRIVMSG or mentions."
:version "24.3"
:group 'erc)
(defvar erc-notifications-last-notification nil
"Last notification id.")
(defcustom erc-notifications-icon nil
"Icon to use for notification."
:type '(choice (const :tag "No icon" nil) file))
(defcustom erc-notifications-bus :session
"D-Bus bus to use for notification."
:version "25.1"
:type '(choice (const :tag "Session bus" :session) string))
(defvar dbus-debug) ; used in the macroexpansion of dbus-ignore-errors
(declare-function haiku-notifications-notify "haikuselect.c")
(declare-function android-notifications-notify "androidselect.c")
(defun erc-notifications-notify (nick msg &optional privp)
"Notify that NICK send some MSG, where PRIVP should be non-nil for PRIVMSGs.
This will replace the last notification sent with this function."
;; TODO: can we do this without PRIVP? (by "fixing" ERC's not
;; setting the current buffer to the existing query buffer)
(dbus-ignore-errors
(setq erc-notifications-last-notification
(let* ((channel (if privp (erc-get-buffer nick) (current-buffer)))
(title (format "%s in %s" (xml-escape-string nick t) channel))
(body (xml-escape-string (erc-controls-strip msg) t)))
(funcall (cond ((featurep 'android)
#'android-notifications-notify)
((featurep 'haiku)
#'haiku-notifications-notify)
(t #'notifications-notify))
:bus erc-notifications-bus
:title title
:body body
:replaces-id erc-notifications-last-notification
:app-icon erc-notifications-icon
:actions '("default" "Switch to buffer")
:on-action (lambda (&rest _)
(pop-to-buffer channel)))))))
(defun erc-notifications-PRIVMSG (_proc parsed)
(let ((nick (car (erc-parse-user (erc-response.sender parsed))))
(target (car (erc-response.command-args parsed)))
(msg (erc-response.contents parsed)))
(when (and (erc-current-nick-p target)
(not (and (boundp 'erc-track-exclude)
(member nick erc-track-exclude)))
(not (erc-is-message-ctcp-and-not-action-p msg)))
(erc-notifications-notify nick msg t)))
;; Return nil to continue processing by ERC
nil)
(defun erc-notifications-notify-on-match (match-type nickuserhost msg)
(when (eq match-type 'current-nick)
(let ((nick (nth 0 (erc-parse-user nickuserhost))))
(unless (or (string-match-p "^Server:" nick)
(when (boundp 'erc-track-exclude)
(member nick erc-track-exclude)))
(erc-notifications-notify nick msg)))))
;;;###autoload(autoload 'erc-notifications-mode "erc-desktop-notifications" "" t)
(define-erc-module notifications nil
"Send notifications on private message reception and mentions."
;; Enable
((add-hook 'erc-server-PRIVMSG-functions #'erc-notifications-PRIVMSG)
(add-hook 'erc-text-matched-hook #'erc-notifications-notify-on-match))
;; Disable
((remove-hook 'erc-server-PRIVMSG-functions #'erc-notifications-PRIVMSG)
(remove-hook 'erc-text-matched-hook #'erc-notifications-notify-on-match)))
(provide 'erc-desktop-notifications)
;;; erc-desktop-notifications.el ends here
;; Local Variables:
;; generated-autoload-file: "erc-loaddefs.el"
;; End: