1 ;; jabber-alert.el - alert hooks -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2003, 2004, 2005, 2007, 2008 - Magnus Henoch - mange@freemail.hu
4 ;; Copyright (C) 2002, 2003, 2004 - tom berger - object@intelectronica.net
6 ;; This file is a part of jabber.el.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 (require 'jabber-util)
26 (defgroup jabber-alerts nil "auditory and visual alerts for jabber events"
29 (defcustom jabber-alert-message-hooks '(jabber-message-echo
30 jabber-message-scroll)
31 "Hooks run when a new message arrives.
33 Arguments are FROM, BUFFER, TEXT and TITLE. FROM is the JID of
34 the sender, BUFFER is the the buffer where the message can be
35 read, and TEXT is the text of the message. TITLE is the string
36 returned by `jabber-alert-message-function' for these arguments,
37 so that hooks do not have to call it themselves.
39 This hook is meant for user customization of message alerts. For
40 other uses, see `jabber-message-hooks'."
42 :options '(jabber-message-beep
46 jabber-message-display
47 jabber-message-scroll)
48 :group 'jabber-alerts)
50 (defvar jabber-message-hooks nil
51 "Internal hooks run when a new message arrives.
53 This hook works just like `jabber-alert-message-hooks', except that
54 it's not meant to be customized by the user.")
56 (defcustom jabber-alert-message-function
57 'jabber-message-default-message
58 "Function for constructing short message alert messages.
60 Arguments are FROM, BUFFER, and TEXT. This function should return a
61 string containing an appropriate text message, or nil if no message
64 The provided hooks displaying a text message get it from this function,
65 and show no message if it returns nil. Other hooks do what they do
68 :group 'jabber-alerts)
70 (defcustom jabber-alert-muc-hooks '(jabber-muc-echo jabber-muc-scroll)
71 "Hooks run when a new MUC message arrives.
73 Arguments are NICK, GROUP, BUFFER, TEXT and TITLE. NICK is the
74 nickname of the sender. GROUP is the JID of the group. BUFFER
75 is the the buffer where the message can be read, and TEXT is the
76 text of the message. TITLE is the string returned by
77 `jabber-alert-muc-function' for these arguments, so that hooks do
78 not have to call it themselves."
80 :options '(jabber-muc-beep
86 :group 'jabber-alerts)
88 (defvar jabber-muc-hooks '()
89 "Internal hooks run when a new MUC message arrives.
91 This hook works just like `jabber-alert-muc-hooks', except that
92 it's not meant to be customized by the user.")
94 (defcustom jabber-alert-muc-function
95 'jabber-muc-default-message
96 "Function for constructing short message alert messages.
98 Arguments are NICK, GROUP, BUFFER, and TEXT. This function
99 should return a string containing an appropriate text message, or
100 nil if no message should be displayed.
102 The provided hooks displaying a text message get it from this function,
103 and show no message if it returns nil. Other hooks do what they do
106 :group 'jabber-alerts)
108 (defcustom jabber-alert-presence-hooks
109 '(jabber-presence-echo)
110 "Hooks run when a user's presence changes.
112 Arguments are WHO, OLDSTATUS, NEWSTATUS, STATUSTEXT and
113 PROPOSED-ALERT. WHO is a symbol whose text is the JID of the contact,
114 and which has various interesting properties. OLDSTATUS is the old
115 presence or nil if disconnected. NEWSTATUS is the new presence, or
116 one of \"subscribe\", \"unsubscribe\", \"subscribed\" and
117 \"unsubscribed\". TITLE is the string returned by
118 `jabber-alert-presence-message-function' for these arguments."
120 :options '(jabber-presence-beep
122 jabber-presence-switch
123 jabber-presence-display
124 jabber-presence-echo)
125 :group 'jabber-alerts)
127 (defvar jabber-presence-hooks '(jabber-presence-watch)
128 "Internal hooks run when a user's presence changes.
130 This hook works just like `jabber-alert-presence-hooks', except that
131 it's not meant to be customized by the user.")
133 (defcustom jabber-alert-presence-message-function
134 'jabber-presence-default-message
135 "Function for constructing title of presence alert messages.
137 Arguments are WHO, OLDSTATUS, NEWSTATUS and STATUSTEXT. See
138 `jabber-alert-presence-hooks' for documentation. This function
139 should return a string containing an appropriate text message, or nil
140 if no message should be displayed.
142 The provided hooks displaying a text message get it from this function.
143 All hooks refrain from action if this function returns nil."
145 :group 'jabber-alerts)
147 (defcustom jabber-alert-info-message-hooks '(jabber-info-display jabber-info-echo)
148 "Hooks run when an info request is completed.
150 First argument is WHAT, a symbol telling the kind of info request completed.
151 That might be 'roster, for requested roster updates, and 'browse, for
152 browse requests. Second argument in BUFFER, a buffer containing the result.
153 Third argument is PROPOSED-ALERT, containing the string returned by
154 `jabber-alert-info-message-function' for these arguments."
156 :options '(jabber-info-beep
161 :group 'jabber-alerts)
163 (defvar jabber-info-message-hooks '()
164 "Internal hooks run when an info request is completed.
166 This hook works just like `jabber-alert-info-message-hooks',
167 except that it's not meant to be customized by the user.")
169 (defcustom jabber-alert-info-message-function
170 'jabber-info-default-message
171 "Function for constructing info alert messages.
173 Arguments are WHAT, a symbol telling the kind of info request completed,
174 and BUFFER, a buffer containing the result."
176 :group 'jabber-alerts)
178 (defcustom jabber-info-message-alist
179 '((roster . "Roster display updated")
180 (browse . "Browse request completed"))
181 "Alist for info alert messages, used by `jabber-info-default-message'."
182 :type '(alist :key-type symbol :value-type string
183 :options (roster browse))
184 :group 'jabber-alerts)
186 (defcustom jabber-alert-message-wave ""
187 "A sound file to play when a message arrived.
188 See `jabber-alert-message-wave-alist' if you want other sounds
189 for specific contacts."
191 :group 'jabber-alerts)
193 (defcustom jabber-alert-message-wave-alist nil
194 "Specific sound files for messages from specific contacts.
195 The keys are regexps matching the JID, and the values are sound
197 :type '(alist :key-type regexp :value-type file)
198 :group 'jabber-alerts)
200 (defcustom jabber-alert-muc-wave ""
201 "A sound file to play when a MUC message arrived."
203 :group 'jabber-alerts)
205 (defcustom jabber-alert-presence-wave ""
206 "A sound file to play when a presence arrived."
208 :group 'jabber-alerts)
210 (defcustom jabber-alert-presence-wave-alist nil
211 "Specific sound files for presence from specific contacts.
212 The keys are regexps matching the JID, and the values are sound
214 :type '(alist :key-type regexp :value-type file)
215 :group 'jabber-alerts)
217 (defcustom jabber-alert-info-wave ""
218 "A sound file to play when an info query result arrived."
220 :group 'jabber-alerts)
222 (defcustom jabber-play-sound-file 'play-sound-file
223 "A function to call to play alert sound files."
225 :group 'jabber-alerts)
227 (defmacro define-jabber-alert (name docstring function)
228 "Define a new family of external alert hooks.
229 Use this macro when your hooks do nothing except displaying a string
230 in some new innovative way. You write a string display function, and
231 this macro does all the boring and repetitive work.
233 NAME is the name of the alert family. The resulting hooks will be
234 called jabber-{message,muc,presence,info}-NAME.
235 DOCSTRING is the docstring to use for those hooks.
236 FUNCTION is a function that takes one argument, a string,
237 and displays it in some meaningful way. It can be either a
238 lambda form or a quoted function name.
239 The created functions are inserted as options in Customize.
242 \(define-jabber-alert foo \"Send foo alert\" 'foo-message)
243 \(define-jabber-alert bar \"Send bar alert\"
244 (lambda (msg) (bar msg 42)))"
245 (let ((sn (symbol-name name)))
246 (let ((msg (intern (format "jabber-message-%s" sn)))
247 (muc (intern (format "jabber-muc-%s" sn)))
248 (pres (intern (format "jabber-presence-%s" sn)))
249 (info (intern (format "jabber-info-%s" sn))))
251 (defun ,msg (from buffer text title)
254 (funcall ,function text title)))
255 (cl-pushnew (quote ,msg) (get 'jabber-alert-message-hooks 'custom-options))
256 (defun ,muc (nick group buffer text title)
259 (funcall ,function text title)))
260 (cl-pushnew (quote ,muc) (get 'jabber-alert-muc-hooks 'custom-options))
261 (defun ,pres (who oldstatus newstatus statustext title)
264 (funcall ,function statustext title)))
265 (cl-pushnew (quote ,pres) (get 'jabber-alert-presence-hooks 'custom-options))
266 (defun ,info (infotype buffer text)
269 (funcall ,function text)))
270 (cl-pushnew (quote ,info) (get 'jabber-alert-info-message-hooks 'custom-options))))))
273 (define-jabber-alert echo "Show a message in the echo area"
274 (lambda (text &optional title) (message "%s" (or title text))))
275 (define-jabber-alert beep "Beep on event"
276 (lambda (&rest _ignore) (beep)))
278 ;; Message alert hooks
279 (defun jabber-message-default-message (from buffer text)
280 (when (or jabber-message-alert-same-buffer
281 (not (memq (selected-window) (get-buffer-window-list buffer))))
282 (if (jabber-muc-sender-p from)
283 (format "Private message from %s in %s"
284 (jabber-jid-resource from)
285 (jabber-jid-displayname (jabber-jid-user from)))
286 (format "Message from %s" (jabber-jid-displayname from)))))
288 (defcustom jabber-message-alert-same-buffer t
289 "If nil, don't display message alerts for the current buffer."
291 :group 'jabber-alerts)
293 (defcustom jabber-muc-alert-self nil
294 "If nil, don't display MUC alerts for your own messages."
296 :group 'jabber-alerts)
298 (defun jabber-message-wave (from buffer text title)
299 "Play the wave file specified in `jabber-alert-message-wave'."
301 (let* ((case-fold-search t)
302 (bare-jid (jabber-jid-user from))
303 (sound-file (or (cl-dolist (entry jabber-alert-message-wave-alist)
304 (when (string-match (car entry) bare-jid)
305 (cl-return (cdr entry))))
306 jabber-alert-message-wave)))
307 (unless (equal sound-file "")
308 (funcall jabber-play-sound-file sound-file)))))
310 (defun jabber-message-display (from buffer text title)
311 "Display the buffer where a new message has arrived."
313 (display-buffer buffer)))
315 (defun jabber-message-switch (from buffer text title)
316 "Switch to the buffer where a new message has arrived."
318 (switch-to-buffer buffer)))
320 (defun jabber-message-scroll (from buffer text title)
321 "Scroll all nonselected windows where the chat buffer is displayed."
322 ;; jabber-chat-buffer-display will DTRT with point in the buffer.
323 ;; But this change will not take effect in nonselected windows.
324 ;; Therefore we do that manually here.
326 ;; There are three cases:
327 ;; 1. The user started typing a message in this window. Point is
328 ;; greater than jabber-point-insert. In that case, we don't
329 ;; want to move point.
330 ;; 2. Point was at the end of the buffer, but no message was being
331 ;; typed. After displaying the message, point is now close to
332 ;; the end of the buffer. We advance it to the end.
333 ;; 3. The user was perusing history in this window. There is no
334 ;; simple way to distinguish this from 2, so the user loses.
335 (let ((windows (get-buffer-window-list buffer nil t))
336 (new-point-max (with-current-buffer buffer (point-max))))
338 (unless (eq w (selected-window))
339 (set-window-point w new-point-max)))))
342 (defun jabber-muc-default-message (nick group buffer text)
343 (when (or jabber-message-alert-same-buffer
344 (not (memq (selected-window) (get-buffer-window-list buffer))))
346 (when (or jabber-muc-alert-self
347 (not (string= nick (cdr (assoc group *jabber-active-groupchats*)))))
348 (format "Message from %s in %s" nick (jabber-jid-displayname
350 (format "Message in %s" (jabber-jid-displayname group)))))
352 (defun jabber-muc-wave (nick group buffer text title)
353 "Play the wave file specified in `jabber-alert-muc-wave'."
355 (funcall jabber-play-sound-file jabber-alert-muc-wave)))
357 (defun jabber-muc-display (nick group buffer text title)
358 "Display the buffer where a new message has arrived."
360 (display-buffer buffer)))
362 (defun jabber-muc-switch (nick group buffer text title)
363 "Switch to the buffer where a new message has arrived."
365 (switch-to-buffer buffer)))
367 (defun jabber-muc-scroll (nick group buffer text title)
368 "Scroll buffer even if it is in an unselected window."
369 (jabber-message-scroll nil buffer nil nil))
371 ;; Presence alert hooks
372 (defun jabber-presence-default-message (who oldstatus newstatus statustext)
373 "Return a string with the status change if OLDSTATUS and NEWSTATUS differs.
375 Return nil if OLDSTATUS and NEWSTATUS are equal, and in other
376 cases a string of the form \"'name' (jid) is now NEWSTATUS (STATUSTEXT)\".
378 This function is not called directly, but is the default for
379 `jabber-alert-presence-message-function'."
381 ((equal oldstatus newstatus)
385 (if (> (length (get who 'name)) 0)
390 (cdr (assoc newstatus
391 '(("subscribe" . " requests subscription to your presence")
392 ("subscribed" . " has granted presence subscription to you")
393 ("unsubscribe" . " no longer subscribes to your presence")
394 ("unsubscribed" . " cancels your presence subscription"))))
397 (cdr (assoc newstatus jabber-presence-strings))
399 (concat formattedname formattedstatus)))))
401 (defun jabber-presence-only-chat-open-message (who oldstatus newstatus statustext)
402 "Same as `jabber-presence-default-message' but managing the presence messages.
404 Return the same as `jabber-presence-default-message' but only
405 if there is a chat buffer open for WHO, keeping the amount of presence messages
406 at a more manageable level when there are lots of users.
408 This function is not called directly, but can be used as the value for
409 `jabber-alert-presence-message-function'."
410 (when (get-buffer (jabber-chat-get-buffer (jabber-xml-get-attribute xml-data 'from)))
411 (jabber-presence-default-message who oldstatus newstatus statustext)))
413 (defun jabber-presence-wave (who oldstatus newstatus statustext proposed-alert)
414 "Play the wave file specified in `jabber-alert-presence-wave'."
416 (let* ((case-fold-search t)
417 (bare-jid (symbol-name who))
418 (sound-file (or (cl-dolist (entry jabber-alert-presence-wave-alist)
419 (when (string-match (car entry) bare-jid)
420 (cl-return (cdr entry))))
421 jabber-alert-presence-wave)))
422 (unless (equal sound-file "")
423 (funcall jabber-play-sound-file sound-file)))))
425 ;; This is now defined in jabber-roster.el.
426 ;; (defun jabber-presence-update-roster (who oldstatus newstatus statustext proposed-alert)
427 ;; "Update the roster display by calling `jabber-display-roster'"
428 ;; (jabber-display-roster))
430 (defun jabber-presence-display (who oldstatus newstatus statustext proposed-alert)
431 "Display the roster buffer."
433 (display-buffer jabber-roster-buffer)))
435 (defun jabber-presence-switch (who oldstatus newstatus statustext proposed-alert)
436 "Switch to the roster buffer."
438 (switch-to-buffer jabber-roster-buffer)))
442 (defun jabber-info-default-message (infotype buffer)
443 "Function for constructing info alert messages.
445 The argument is INFOTYPE, a symbol telling the kind of info request completed.
446 This function uses `jabber-info-message-alist' to find a message."
447 (concat (cdr (assq infotype jabber-info-message-alist))
448 " (buffer "(buffer-name buffer) ")"))
450 (defun jabber-info-wave (infotype buffer proposed-alert)
451 "Play the wave file specified in `jabber-alert-info-wave'."
453 (funcall jabber-play-sound-file jabber-alert-info-wave)))
455 (defun jabber-info-display (infotype buffer proposed-alert)
456 "Display buffer of completed request."
458 (display-buffer buffer)))
460 (defun jabber-info-switch (infotype buffer proposed-alert)
461 "Switch to buffer of completed request."
463 (switch-to-buffer buffer)))
465 ;;; Personal alert hooks
466 (defmacro define-personal-jabber-alert (name)
467 "From ALERT function, make ALERT-personal function.
469 This makes sense only for MUC.
471 NAME: the name of the sender."
472 (let ((sn (symbol-name name)))
473 (let ((func (intern (format "%s-personal" sn))))
475 (defun ,func (nick group buffer text title)
476 (if (jabber-muc-looks-like-personal-p text group)
477 (,name nick group buffer text title)))
478 (cl-pushnew (quote ,func) (get 'jabber-alert-muc-hooks 'custom-options))))))
480 (define-personal-jabber-alert jabber-muc-beep)
481 (define-personal-jabber-alert jabber-muc-wave)
482 (define-personal-jabber-alert jabber-muc-echo)
483 (define-personal-jabber-alert jabber-muc-switch)
484 (define-personal-jabber-alert jabber-muc-display)
486 (defcustom jabber-autoanswer-alist nil
487 "Specific phrases to autoanswer on specific message.
488 The keys are regexps matching the incoming message text, and the values are
490 :type '(alist :key-type regexp :value-type string)
491 :group 'jabber-alerts)
493 (defun jabber-autoanswer-answer (from buffer text proposed-alert)
494 "Answer automaticaly when incoming text is in `jabber-autoanswer-alist'.
495 Answer automaticaly when incoming text match the first element of
496 `jabber-autoanswer-alist'"
497 (when (and from buffer text proposed-alert jabber-autoanswer-alist)
499 (cl-dolist (entry jabber-autoanswer-alist)
500 (when (string-match (car entry) text)
501 (cl-return (cdr entry))))))
503 (jabber-chat-send jabber-buffer-connection message)))))
504 (cl-pushnew 'jabber-autoanswer-answer (get 'jabber-alert-message-hooks 'custom-options))
506 (defun jabber-autoanswer-answer-muc (nick group buffer text proposed-alert)
507 "Answer automaticaly when incoming text is in `jabber-autoanswer-alist'.
508 Answer automaticaly when incoming text match first element
509 of `jabber-autoanswer-alist'."
510 (when (and nick group buffer text proposed-alert jabber-autoanswer-alist)
512 (cl-dolist (entry jabber-autoanswer-alist)
513 (when (string-match (car entry) text)
514 (cl-return (cdr entry))))))
516 (jabber-chat-send jabber-buffer-connection message)))))
517 (cl-pushnew 'jabber-autoanswer-answer-muc (get 'jabber-alert-muc-hooks 'custom-options))
518 (provide 'jabber-alert)
520 ;;; arch-tag: 725bd73e-c613-4fdc-a11d-3392a7598d4f