dotfiles/lisp/erc-notify.el

66 lines
2.5 KiB
EmacsLisp
Raw Normal View History

2014-11-21 11:43:09 -07:00
;;; Notify me when a keyword is matched (someone wants to reach me)
(defvar my-erc-page-message "%s is calling your name."
"Format of message to display in dialog box")
(defvar my-erc-page-nick-alist nil
"Alist of nicks and the last time they tried to trigger a
notification")
(defvar my-erc-page-timeout 30
"Number of seconds that must elapse between notifications from
the same person.")
(defun my-erc-page-popup-notification (nick msg)
(notification-center-command (format "%s says:" nick) msg))
(defun my-erc-page-allowed (nick &optional delay)
"Return non-nil if a notification should be made for NICK.
If DELAY is specified, it will be the minimum time in seconds
that can occur between two notifications. The default is
`my-erc-page-timeout'."
(unless delay (setq delay my-erc-page-timeout))
(let ((cur-time (time-to-seconds (current-time)))
(cur-assoc (assoc nick my-erc-page-nick-alist))
(last-time))
(if cur-assoc
(progn
(setq last-time (cdr cur-assoc))
(setcdr cur-assoc cur-time)
(> (abs (- cur-time last-time)) delay))
(push (cons nick cur-time) my-erc-page-nick-alist)
t)))
(defun my-erc-page-me (match-type nick message)
"Notify the current user when someone sends a message that
matches a regexp in `erc-keywords'."
(interactive)
(when (and (eq match-type 'keyword)
;; I don't want to see anything from the erc server
(null (string-match "\\`\\([sS]erver\\|localhost\\)" nick))
;; or bots
(null (string-match "\\(bot\\|serv\\)!" nick))
;; or from those who abuse the system
(my-erc-page-allowed nick))
(my-erc-page-popup-notification nick)))
(defun my-erc-page-me-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 (erc-is-message-ctcp-and-not-action-p msg))
(my-erc-page-allowed nick))
(my-erc-page-popup-notification nick msg)
nil)))
(add-hook 'erc-text-matched-hook 'my-erc-page-me)
(add-hook 'erc-server-PRIVMSG-functions 'my-erc-page-me-PRIVMSG)
(defvar notify-function #'notification-center)
(defun notification-center-command (title message)
(flet ((encfn (s) (encode-coding-string s (keyboard-coding-system))))
(shell-command (format "osascript -e 'display notification \"%s\" with title \"%s\"'"
(encfn message) (encfn title)))))