1 ;; jabber-alert.el - alert hooks
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
)
23 (require 'jabber-autoloads
)
27 (defgroup jabber-alerts nil
"auditory and visual alerts for jabber events"
30 (defcustom jabber-alert-message-hooks
'(jabber-message-echo
31 jabber-message-scroll
)
32 "Hooks run when a new message arrives.
34 Arguments are FROM, BUFFER, TEXT and TITLE. FROM is the JID of
35 the sender, BUFFER is the the buffer where the message can be
36 read, and TEXT is the text of the message. TITLE is the string
37 returned by `jabber-alert-message-function' for these arguments,
38 so that hooks do not have to call it themselves.
40 This hook is meant for user customization of message alerts. For
41 other uses, see `jabber-message-hooks'."
43 :options
'(jabber-message-beep
47 jabber-message-display
48 jabber-message-scroll
)
49 :group
'jabber-alerts
)
51 (defvar jabber-message-hooks nil
52 "Internal hooks run when a new message arrives.
54 This hook works just like `jabber-alert-message-hooks', except that
55 it's not meant to be customized by the user.")
57 (defcustom jabber-alert-message-function
58 'jabber-message-default-message
59 "Function for constructing short message alert messages.
61 Arguments are FROM, BUFFER, and TEXT. This function should return a
62 string containing an appropriate text message, or nil if no message
65 The provided hooks displaying a text message get it from this function,
66 and show no message if it returns nil. Other hooks do what they do
69 :group
'jabber-alerts
)
71 (defcustom jabber-alert-muc-hooks
'(jabber-muc-echo jabber-muc-scroll
)
72 "Hooks run when a new MUC message arrives.
74 Arguments are NICK, GROUP, BUFFER, TEXT and TITLE. NICK is the
75 nickname of the sender. GROUP is the JID of the group. BUFFER
76 is the the buffer where the message can be read, and TEXT is the
77 text of the message. TITLE is the string returned by
78 `jabber-alert-muc-function' for these arguments, so that hooks do
79 not have to call it themselves."
81 :options
'(jabber-muc-beep
87 :group
'jabber-alerts
)
89 (defvar jabber-muc-hooks
'()
90 "Internal hooks run when a new MUC message arrives.
92 This hook works just like `jabber-alert-muc-hooks', except that
93 it's not meant to be customized by the user.")
95 (defcustom jabber-alert-muc-function
96 'jabber-muc-default-message
97 "Function for constructing short message alert messages.
99 Arguments are NICK, GROUP, BUFFER, and TEXT. This function
100 should return a string containing an appropriate text message, or
101 nil if no message should be displayed.
103 The provided hooks displaying a text message get it from this function,
104 and show no message if it returns nil. Other hooks do what they do
107 :group
'jabber-alerts
)
109 (defcustom jabber-alert-presence-hooks
110 '(jabber-presence-echo)
111 "Hooks run when a user's presence changes.
113 Arguments are WHO, OLDSTATUS, NEWSTATUS, STATUSTEXT and
114 PROPOSED-ALERT. WHO is a symbol whose text is the JID of the contact,
115 and which has various interesting properties. OLDSTATUS is the old
116 presence or nil if disconnected. NEWSTATUS is the new presence, or
117 one of \"subscribe\", \"unsubscribe\", \"subscribed\" and
118 \"unsubscribed\". TITLE is the string returned by
119 `jabber-alert-presence-message-function' for these arguments."
121 :options
'(jabber-presence-beep
123 jabber-presence-switch
124 jabber-presence-display
125 jabber-presence-echo
)
126 :group
'jabber-alerts
)
128 (defvar jabber-presence-hooks
'(jabber-presence-watch)
129 "Internal hooks run when a user's presence changes.
131 This hook works just like `jabber-alert-presence-hooks', except that
132 it's not meant to be customized by the user.")
134 (defcustom jabber-alert-presence-message-function
135 'jabber-presence-default-message
136 "Function for constructing title of presence alert messages.
138 Arguments are WHO, OLDSTATUS, NEWSTATUS and STATUSTEXT. See
139 `jabber-alert-presence-hooks' for documentation. This function
140 should return a string containing an appropriate text message, or nil
141 if no message should be displayed.
143 The provided hooks displaying a text message get it from this function.
144 All hooks refrain from action if this function returns nil."
146 :group
'jabber-alerts
)
148 (defcustom jabber-alert-info-message-hooks
'(jabber-info-display jabber-info-echo
)
149 "Hooks run when an info request is completed.
151 First argument is WHAT, a symbol telling the kind of info request completed.
152 That might be 'roster, for requested roster updates, and 'browse, for
153 browse requests. Second argument in BUFFER, a buffer containing the result.
154 Third argument is PROPOSED-ALERT, containing the string returned by
155 `jabber-alert-info-message-function' for these arguments."
157 :options
'(jabber-info-beep
162 :group
'jabber-alerts
)
164 (defvar jabber-info-message-hooks
'()
165 "Internal hooks run when an info request is completed.
167 This hook works just like `jabber-alert-info-message-hooks',
168 except that it's not meant to be customized by the user.")
170 (defcustom jabber-alert-info-message-function
171 'jabber-info-default-message
172 "Function for constructing info alert messages.
174 Arguments are WHAT, a symbol telling the kind of info request completed,
175 and BUFFER, a buffer containing the result."
177 :group
'jabber-alerts
)
179 (defcustom jabber-info-message-alist
180 '((roster .
"Roster display updated")
181 (browse .
"Browse request completed"))
182 "Alist for info alert messages, used by `jabber-info-default-message'."
183 :type
'(alist :key-type symbol
:value-type string
184 :options
(roster browse
))
185 :group
'jabber-alerts
)
187 (defcustom jabber-alert-message-wave
""
188 "A sound file to play when a message arrived.
189 See `jabber-alert-message-wave-alist' if you want other sounds
190 for specific contacts."
192 :group
'jabber-alerts
)
194 (defcustom jabber-alert-message-wave-alist nil
195 "Specific sound files for messages from specific contacts.
196 The keys are regexps matching the JID, and the values are sound
198 :type
'(alist :key-type regexp
:value-type file
)
199 :group
'jabber-alerts
)
201 (defcustom jabber-alert-muc-wave
""
202 "a sound file to play when a MUC message arrived"
204 :group
'jabber-alerts
)
206 (defcustom jabber-alert-presence-wave
""
207 "a sound file to play when a presence arrived"
209 :group
'jabber-alerts
)
211 (defcustom jabber-alert-presence-wave-alist nil
212 "Specific sound files for presence from specific contacts.
213 The keys are regexps matching the JID, and the values are sound
215 :type
'(alist :key-type regexp
:value-type file
)
216 :group
'jabber-alerts
)
218 (defcustom jabber-alert-info-wave
""
219 "a sound file to play when an info query result arrived"
221 :group
'jabber-alerts
)
223 (defcustom jabber-play-sound-file
'play-sound-file
224 "a function to call to play alert sound files"
226 :group
'jabber-alerts
)
228 (defmacro define-jabber-alert
(name docstring function
)
229 "Define a new family of external alert hooks.
230 Use this macro when your hooks do nothing except displaying a string
231 in some new innovative way. You write a string display function, and
232 this macro does all the boring and repetitive work.
234 NAME is the name of the alert family. The resulting hooks will be
235 called jabber-{message,muc,presence,info}-NAME.
236 DOCSTRING is the docstring to use for those hooks.
237 FUNCTION is a function that takes one argument, a string,
238 and displays it in some meaningful way. It can be either a
239 lambda form or a quoted function name.
240 The created functions are inserted as options in Customize.
243 \(define-jabber-alert foo \"Send foo alert\" 'foo-message)
244 \(define-jabber-alert bar \"Send bar alert\"
245 (lambda (msg) (bar msg 42)))"
246 (let ((sn (symbol-name name
)))
247 (let ((msg (intern (format "jabber-message-%s" sn
)))
248 (muc (intern (format "jabber-muc-%s" sn
)))
249 (pres (intern (format "jabber-presence-%s" sn
)))
250 (info (intern (format "jabber-info-%s" sn
))))
252 (defun ,msg
(from buffer text title
)
255 (funcall ,function text title
)))
256 (pushnew (quote ,msg
) (get 'jabber-alert-message-hooks
'custom-options
))
257 (defun ,muc
(nick group buffer text title
)
260 (funcall ,function text title
)))
261 (pushnew (quote ,muc
) (get 'jabber-alert-muc-hooks
'custom-options
))
262 (defun ,pres
(who oldstatus newstatus statustext title
)
265 (funcall ,function statustext title
)))
266 (pushnew (quote ,pres
) (get 'jabber-alert-presence-hooks
'custom-options
))
267 (defun ,info
(infotype buffer text
)
270 (funcall ,function text
)))
271 (pushnew (quote ,info
) (get 'jabber-alert-info-message-hooks
'custom-options
))))))
274 (define-jabber-alert echo
"Show a message in the echo area"
275 (lambda (text &optional title
) (message "%s" (or title text
))))
276 (define-jabber-alert beep
"Beep on event"
277 (lambda (&rest ignore
) (beep)))
279 ;; Message alert hooks
280 (defun jabber-message-default-message (from buffer text
)
281 (when (or jabber-message-alert-same-buffer
282 (not (memq (selected-window) (get-buffer-window-list buffer
))))
283 (if (jabber-muc-sender-p from
)
284 (format "Private message from %s in %s"
285 (jabber-jid-resource from
)
286 (jabber-jid-displayname (jabber-jid-user from
)))
287 (format "Message from %s" (jabber-jid-displayname from
)))))
289 (defcustom jabber-message-alert-same-buffer t
290 "If nil, don't display message alerts for the current buffer."
292 :group
'jabber-alerts
)
294 (defcustom jabber-muc-alert-self nil
295 "If nil, don't display MUC alerts for your own messages."
297 :group
'jabber-alerts
)
299 (defun jabber-message-wave (from buffer text title
)
300 "Play the wave file specified in `jabber-alert-message-wave'"
302 (let* ((case-fold-search t
)
303 (bare-jid (jabber-jid-user from
))
304 (sound-file (or (dolist (entry jabber-alert-message-wave-alist
)
305 (when (string-match (car entry
) bare-jid
)
306 (return (cdr entry
))))
307 jabber-alert-message-wave
)))
308 (unless (equal sound-file
"")
309 (funcall jabber-play-sound-file sound-file
)))))
311 (defun jabber-message-display (from buffer text title
)
312 "Display the buffer where a new message has arrived."
314 (display-buffer buffer
)))
316 (defun jabber-message-switch (from buffer text title
)
317 "Switch to the buffer where a new message has arrived."
319 (switch-to-buffer buffer
)))
321 (defun jabber-message-scroll (from buffer text title
)
322 "Scroll all nonselected windows where the chat buffer is displayed."
323 ;; jabber-chat-buffer-display will DTRT with point in the buffer.
324 ;; But this change will not take effect in nonselected windows.
325 ;; Therefore we do that manually here.
327 ;; There are three cases:
328 ;; 1. The user started typing a message in this window. Point is
329 ;; greater than jabber-point-insert. In that case, we don't
330 ;; want to move point.
331 ;; 2. Point was at the end of the buffer, but no message was being
332 ;; typed. After displaying the message, point is now close to
333 ;; the end of the buffer. We advance it to the end.
334 ;; 3. The user was perusing history in this window. There is no
335 ;; simple way to distinguish this from 2, so the user loses.
336 (let ((windows (get-buffer-window-list buffer nil t
))
337 (new-point-max (with-current-buffer buffer
(point-max))))
339 (unless (eq w
(selected-window))
340 (set-window-point w new-point-max
)))))
343 (defun jabber-muc-default-message (nick group buffer text
)
344 (when (or jabber-message-alert-same-buffer
345 (not (memq (selected-window) (get-buffer-window-list buffer
))))
347 (when (or jabber-muc-alert-self
348 (not (string= nick
(cdr (assoc group
*jabber-active-groupchats
*)))))
349 (format "Message from %s in %s" nick
(jabber-jid-displayname
351 (format "Message in %s" (jabber-jid-displayname group
)))))
353 (defun jabber-muc-wave (nick group buffer text title
)
354 "Play the wave file specified in `jabber-alert-muc-wave'"
356 (funcall jabber-play-sound-file jabber-alert-muc-wave
)))
358 (defun jabber-muc-display (nick group buffer text title
)
359 "Display the buffer where a new message has arrived."
361 (display-buffer buffer
)))
363 (defun jabber-muc-switch (nick group buffer text title
)
364 "Switch to the buffer where a new message has arrived."
366 (switch-to-buffer buffer
)))
368 (defun jabber-muc-scroll (nick group buffer text title
)
369 "Scroll buffer even if it is in an unselected window."
370 (jabber-message-scroll nil buffer nil nil
))
372 ;; Presence alert hooks
373 (defun jabber-presence-default-message (who oldstatus newstatus statustext
)
374 "This function returns nil if OLDSTATUS and NEWSTATUS are equal, and in other
375 cases a string of the form \"'name' (jid) is now NEWSTATUS (STATUSTEXT)\".
377 This function is not called directly, but is the default for
378 `jabber-alert-presence-message-function'."
380 ((equal oldstatus newstatus
)
384 (if (> (length (get who
'name
)) 0)
389 (cdr (assoc newstatus
390 '(("subscribe" .
" requests subscription to your presence")
391 ("subscribed" .
" has granted presence subscription to you")
392 ("unsubscribe" .
" no longer subscribes to your presence")
393 ("unsubscribed" .
" cancels your presence subscription"))))
396 (cdr (assoc newstatus jabber-presence-strings
))
398 (concat formattedname formattedstatus
)))))
400 (defun jabber-presence-only-chat-open-message (who oldstatus newstatus statustext
)
401 "This function returns the same as `jabber-presence-default-message' but only
402 if there is a chat buffer open for WHO, keeping the amount of presence messages
403 at a more manageable level when there are lots of users.
405 This function is not called directly, but can be used as the value for
406 `jabber-alert-presence-message-function'."
407 (when (get-buffer (jabber-chat-get-buffer (jabber-xml-get-attribute xml-data
'from
)))
408 (jabber-presence-default-message who oldstatus newstatus statustext
)))
410 (defun jabber-presence-wave (who oldstatus newstatus statustext proposed-alert
)
411 "Play the wave file specified in `jabber-alert-presence-wave'"
413 (let* ((case-fold-search t
)
414 (bare-jid (symbol-name who
))
415 (sound-file (or (dolist (entry jabber-alert-presence-wave-alist
)
416 (when (string-match (car entry
) bare-jid
)
417 (return (cdr entry
))))
418 jabber-alert-presence-wave
)))
419 (unless (equal sound-file
"")
420 (funcall jabber-play-sound-file sound-file
)))))
422 ;; This is now defined in jabber-roster.el.
423 ;; (defun jabber-presence-update-roster (who oldstatus newstatus statustext proposed-alert)
424 ;; "Update the roster display by calling `jabber-display-roster'"
425 ;; (jabber-display-roster))
427 (defun jabber-presence-display (who oldstatus newstatus statustext proposed-alert
)
428 "Display the roster buffer"
430 (display-buffer jabber-roster-buffer
)))
432 (defun jabber-presence-switch (who oldstatus newstatus statustext proposed-alert
)
433 "Switch to the roster buffer"
435 (switch-to-buffer jabber-roster-buffer
)))
439 (defun jabber-info-default-message (infotype buffer
)
440 "Function for constructing info alert messages.
442 The argument is INFOTYPE, a symbol telling the kind of info request completed.
443 This function uses `jabber-info-message-alist' to find a message."
444 (concat (cdr (assq infotype jabber-info-message-alist
))
445 " (buffer "(buffer-name buffer
) ")"))
447 (defun jabber-info-wave (infotype buffer proposed-alert
)
448 "Play the wave file specified in `jabber-alert-info-wave'"
450 (funcall jabber-play-sound-file jabber-alert-info-wave
)))
452 (defun jabber-info-display (infotype buffer proposed-alert
)
453 "Display buffer of completed request"
455 (display-buffer buffer
)))
457 (defun jabber-info-switch (infotype buffer proposed-alert
)
458 "Switch to buffer of completed request"
460 (switch-to-buffer buffer
)))
462 ;;; Personal alert hooks
463 (defmacro define-personal-jabber-alert
(name)
464 "From ALERT function, make ALERT-personal function. Makes sence only for MUC."
465 (let ((sn (symbol-name name
)))
466 (let ((func (intern (format "%s-personal" sn
))))
468 (defun ,func
(nick group buffer text title
)
469 (if (jabber-muc-looks-like-personal-p text group
)
470 (,name nick group buffer text title
)))
471 (pushnew (quote ,func
) (get 'jabber-alert-muc-hooks
'custom-options
)))))
474 (define-personal-jabber-alert jabber-muc-beep
)
475 (define-personal-jabber-alert jabber-muc-wave
)
476 (define-personal-jabber-alert jabber-muc-echo
)
477 (define-personal-jabber-alert jabber-muc-switch
)
478 (define-personal-jabber-alert jabber-muc-display
)
480 (defcustom jabber-autoanswer-alist nil
481 "Specific phrases to autoanswer on specific message.
482 The keys are regexps matching the incoming message text, and the values are
484 :type
'(alist :key-type regexp
:value-type string
)
485 :group
'jabber-alerts
)
487 (defun jabber-autoanswer-answer (from buffer text proposed-alert
)
488 "Answer automaticaly when incoming text matches first element
489 of `jabber-autoanswer-alist'"
490 (when (and from buffer text proposed-alert jabber-autoanswer-alist
)
492 (dolist (entry jabber-autoanswer-alist
)
493 (when (string-match (car entry
) text
)
494 (return (cdr entry
))))))
496 (jabber-chat-send jabber-buffer-connection message
)))
498 (pushnew 'jabber-autoanswer-answer
(get 'jabber-alert-message-hooks
'custom-options
))
500 (defun jabber-autoanswer-answer-muc (nick group buffer text proposed-alert
)
501 "Answer automaticaly when incoming text matches first element
502 of `jabber-autoanswer-alist'"
503 (when (and nick group buffer text proposed-alert jabber-autoanswer-alist
)
505 (dolist (entry jabber-autoanswer-alist
)
506 (when (string-match (car entry
) text
)
507 (return (cdr entry
))))))
509 (jabber-chat-send jabber-buffer-connection message
)))
511 (pushnew 'jabber-autoanswer-answer-muc
(get 'jabber-alert-muc-hooks
'custom-options
))
513 (provide 'jabber-alert
)
515 ;;; arch-tag: 725bd73e-c613-4fdc-a11d-3392a7598d4f