-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcirce-notifications.el
225 lines (192 loc) · 9.44 KB
/
circe-notifications.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
;;; circe-notifications.el --- Add desktop notifications to Circe.
;; Copyright (C) 2014 - 2017 Ruben Maher
;; Version: 1.0
;; Author: Ruben Maher <[email protected]>
;; URL: https://github.com/eqyiel/circe-notifications
;; Package-Requires: ((emacs "24.4") (circe "2.3") (alert "1.2"))
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Contributors:
;; Michael McCracken <[email protected]>
;; Syohei YOSHIDA <[email protected]>
;; Steve Purcell <[email protected]>
;; Chris Barrett
;; Jay Kamat <[email protected]>
;;; Code:
(require 'circe)
(require 'alert)
(defgroup circe-notifications nil
"Add desktop notifications to Circe."
:prefix "circe-notifications-"
:group 'circe)
(defvar circe-notifications-emacs-focused nil
"True if Emacs is currently focused by the window manager.")
(defvar circe-notifications-wait-list nil
"An alist of nicks that have triggered notifications in the last
`circe-notifications-wait-for' seconds.")
(defcustom circe-notifications-alert-style 'libnotify
"`alert' style to use. See `alert-styles' for a list of possibilities. You
can also define your own."
:type 'symbol
:group 'circe-notifications)
(defcustom circe-notifications-alert-severity 'normal
"Severity of a notification. Will be passed to `alert'."
:type 'symbol
:group 'circe-notifications)
(defcustom circe-notifications-alert-icon alert-default-icon
"Icon to use for notifications."
:type 'string
:group 'circe-notifications)
(defcustom circe-notifications-notify-function nil
"If you can't get the level of customization you want with `alert', set this
to your own notification function. Will be called with three arguments: NICK,
BODY and CHANNEL. Leave nil to use the default."
:type 'function
:group 'circe-notifications)
(defcustom circe-notifications-wait-for 90
"The number of seconds to wait before allowing some nick in
`circe-notifications-wait-list' to trigger a notification again."
:type 'integer
:group 'circe-notifications)
(defcustom circe-notifications-watch-strings nil
"A list of strings which can trigger a notification. You don't need to put
your nick here, it is added automagically by
`circe-notifications-nicks-on-all-networks' when it checks the values in
`circe-network-options'."
:type '(repeat string)
:group 'circe-notifications)
(defcustom circe-notifications-check-window-focus t
"If true, disable notifications for visible buffers if Emacs is currently
focused by the window manager."
:type 'boolean
:group 'circe-notifications)
(defun circe-notifications-PRIVMSG (nick userhost _command target text)
(when (circe-notifications-should-notify nick userhost target text)
(circe-notifications-notify nick text target)))
(defun circe-notifications-JOIN (nick userhost _command channel
&optional accountname realname)
(when (circe-notifications-should-notify nick userhost channel "")
(circe-notifications-notify nick (concat "/JOIN " channel) channel)))
(defun circe-notifications-QUIT (nick userhost _command
&optional channel reason)
(when (circe-notifications-should-notify
nick userhost (or channel "") (or reason ""))
(circe-notifications-notify nick "/QUIT" (or channel ""))))
(defun circe-notifications-PART (nick userhost _command channel
&optional reason)
(when (circe-notifications-should-notify nick userhost channel (or reason ""))
(circe-notifications-notify
nick (concat "/PART (" channel ")") (or channel ""))))
(defun circe-notifications-should-notify (nick userhost channel body)
"If NICK is not in either `circe-ignore-list' or `circe-fool-list' (only
applicable if `lui-fools-hidden-p'), CHANNEL is either in `tracking-buffers'
\(i.e., not currently visible) or Emacs is not currently focused by the window
manager (detected if `circe-notifications-check-window-focus' is true), NICK has
not triggered a notification in the last `circe-notifications-wait-for' seconds
and NICK matches any of `circe-notifications-watch-strings', show a desktop
notification."
(unless (cond ((circe--ignored-p nick userhost body))
((and (circe--fool-p nick userhost body)
(lui-fools-hidden-p))))
;; Checking `tracking-buffers' has the benefit of excluding
;; `tracking-ignored-buffers'. Also if a channel is in `tracking-buffers',
;; it is not currently focused by Emacs.
(when (cond ((or (member channel tracking-buffers) ;; message to a channel
(member nick tracking-buffers))) ;; private message
((and circe-notifications-check-window-focus
(not circe-notifications-emacs-focused))))
(when (circe-notifications-not-getting-spammed-by nick)
(when (catch 'return
(dolist (n circe-notifications-watch-strings)
(when (or (string-match n nick)
(string-match n body)
(string-match n channel))
(throw 'return t))))
(progn
(if (assoc nick circe-notifications-wait-list)
(setf (cdr (assoc nick circe-notifications-wait-list))
(float-time))
(setq circe-notifications-wait-list
(append circe-notifications-wait-list
(list (cons nick (float-time))))))
t))))))
(defun circe-notifications-notify (nick body channel)
"Show a desktop notification from NICK with BODY."
(if (and circe-notifications-notify-function
(functionp circe-notifications-notify-function))
(funcall circe-notifications-notify-function
nick body channel)
(alert
body
:severity circe-notifications-alert-severity
:title nick
:icon circe-notifications-alert-icon
:style circe-notifications-alert-style)))
(defun circe-notifications-not-getting-spammed-by (nick)
"Return an alist with NICKs that have triggered notifications in the last
`circe-notifications-wait-for' seconds, or nil if it has been less than
`circe-notifications-wait-for' seconds since the last notification from NICK."
(if (assoc nick circe-notifications-wait-list)
(circe-notifications-wait-a-bit nick) t))
(defun circe-notifications-wait-a-bit (nick)
"Has it has been more than `circe-notifications-wait-for' seconds since
the last message from NICK?"
(let* ((last-time (assoc-default
nick
circe-notifications-wait-list
(lambda (x y)
(string-equal y x))))
(seconds-since (- (float-time) last-time)))
(when (< circe-notifications-wait-for seconds-since)
(setf (cdr (assoc nick circe-notifications-wait-list)) (float-time))
t)))
(defun circe-notifications-nicks-on-all-networks ()
"Get a list of all nicks in use according to `circe-network-options'."
(remove nil (delete-dups (mapcar (lambda (opt)
(plist-get (cdr opt) :nick))
circe-network-options))))
(defun circe-notifications-focus-in-hook ()
(setq circe-notifications-emacs-focused t))
(defun circe-notifications-focus-out-hook ()
(setq circe-notifications-emacs-focused nil))
;;;###autoload
(defun enable-circe-notifications ()
"Turn on notifications."
(interactive)
(setq circe-notifications-watch-strings
(remove nil (delete-dups (append circe-notifications-watch-strings
(circe-notifications-nicks-on-all-networks)
lui-highlight-keywords))))
(advice-add 'circe-display-PRIVMSG :after 'circe-notifications-PRIVMSG)
(advice-add 'circe-display-channel-quit :after 'circe-notifications-QUIT)
(advice-add 'circe-display-JOIN :after 'circe-notifications-JOIN)
(advice-add 'circe-display-PART :after 'circe-notifications-PART)
(add-hook 'focus-in-hook 'circe-notifications-focus-in-hook)
(add-hook 'focus-out-hook 'circe-notifications-focus-out-hook)
;; If alert-user-configuration is nil, the :style keyword is ignored.
;; Workaround for now is just to add a dummy rule that does nothing.
;; https://github.com/jwiegley/alert/issues/30
(unless (append alert-user-configuration
alert-internal-configuration)
(alert-add-rule :continue t)))
(defun disable-circe-notifications ()
"Turn off notifications."
(interactive)
(setq circe-notifications-wait-list nil
circe-notifications-watch-strings nil)
(advice-remove 'circe-display-PRIVMSG 'circe-notifications-PRIVMSG)
(advice-remove 'circe-display-channel-quit 'circe-notifications-QUIT)
(advice-remove 'circe-display-JOIN 'circe-notifications-JOIN)
(advice-remove 'circe-display-PART 'circe-notifications-PART)
(remove-hook 'focus-in-hook 'circe-notifications-focus-in-hook)
(remove-hook 'focus-out-hook 'circe-notifications-focus-out-hook))
(provide 'circe-notifications)
;;; circe-notifications.el ends here