Revision: mange@freemail.hu--2005/emacs-jabber--cvs-head--0--patch-556
[emacs-jabber.git] / jabber-alert.el
blob461a9a1742656b01fa8aa237dff70cc12a1771db
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)
25 (require 'cl)
27 (defgroup jabber-alerts nil "auditory and visual alerts for jabber events"
28 :group 'jabber)
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 PROPOSED-ALERT. FROM is the JID
35 of the sender, BUFFER is the the buffer where the message can be read,
36 and TEXT is the text of the message. PROPOSED-ALERT is the string
37 returned by `jabber-alert-message-function' for these arguments, so that
38 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'."
42 :type 'hook
43 :options '(jabber-message-beep
44 jabber-message-wave
45 jabber-message-echo
46 jabber-message-switch
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 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
63 should be displayed.
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
67 every time."
68 :type 'function
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 PROPOSED-ALERT. NICK
75 is the nickname of the sender. GROUP is the JID of the group.
76 BUFFER is the the buffer where the message can be read, and TEXT
77 is the text of the message. PROPOSED-ALERT is the string
78 returned by `jabber-alert-muc-function' for these arguments,
79 so that hooks do not have to call it themselves."
80 :type 'hook
81 :options '(jabber-muc-beep
82 jabber-muc-wave
83 jabber-muc-echo
84 jabber-muc-switch
85 jabber-muc-display
86 jabber-muc-scroll)
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 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
105 every time."
106 :type 'function
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\". PROPOSED-ALERT is the string returned by
119 `jabber-alert-presence-message-function' for these arguments."
120 :type 'hook
121 :options '(jabber-presence-beep
122 jabber-presence-wave
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 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."
145 :type 'function
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."
156 :type 'hook
157 :options '(jabber-info-beep
158 jabber-info-wave
159 jabber-info-echo
160 jabber-info-switch
161 jabber-info-display)
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."
176 :type 'function
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."
191 :type 'file
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
197 files."
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"
203 :type 'file
204 :group 'jabber-alerts)
206 (defcustom jabber-alert-presence-wave ""
207 "a sound file to play when a presence arrived"
208 :type 'file
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
214 files."
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"
220 :type 'file
221 :group 'jabber-alerts)
223 (defcustom jabber-play-sound-file 'play-sound-file
224 "a function to call to play alert sound files"
225 :type 'function
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.
242 Examples:
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))))
251 `(progn
252 (defun ,msg (from buffer text proposed-alert)
253 ,docstring
254 (when proposed-alert
255 (funcall ,function proposed-alert)))
256 (pushnew (quote ,msg) (get 'jabber-alert-message-hooks 'custom-options))
257 (defun ,muc (nick group buffer text proposed-alert)
258 ,docstring
259 (when proposed-alert
260 (funcall ,function proposed-alert)))
261 (pushnew (quote ,muc) (get 'jabber-alert-muc-hooks 'custom-options))
262 (defun ,pres (who oldstatus newstatus statustext proposed-alert)
263 ,docstring
264 (when proposed-alert
265 (funcall ,function proposed-alert)))
266 (pushnew (quote ,pres) (get 'jabber-alert-presence-hooks 'custom-options))
267 (defun ,info (infotype buffer proposed-alert)
268 ,docstring
269 (when proposed-alert
270 (funcall ,function proposed-alert)))
271 (pushnew (quote ,info) (get 'jabber-alert-info-message-hooks 'custom-options))))))
273 ;; Alert hooks
274 (define-jabber-alert echo "Show a message in the echo area"
275 (lambda (msg) (message "%s" msg)))
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."
291 :type 'boolean
292 :group 'jabber-alerts)
294 (defcustom jabber-muc-alert-self nil
295 "If nil, don't display MUC alerts for your own messages."
296 :type 'boolean
297 :group 'jabber-alerts)
299 (defun jabber-message-wave (from buffer text proposed-alert)
300 "Play the wave file specified in `jabber-alert-message-wave'"
301 (when proposed-alert
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 proposed-alert)
312 "Display the buffer where a new message has arrived."
313 (when proposed-alert
314 (display-buffer buffer)))
316 (defun jabber-message-switch (from buffer text proposed-alert)
317 "Switch to the buffer where a new message has arrived."
318 (when proposed-alert
319 (switch-to-buffer buffer)))
321 (defun jabber-message-scroll (from buffer text proposed-alert)
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))))
338 (dolist (w windows)
339 (unless (eq w (selected-window))
340 (set-window-point w new-point-max)))))
342 ;; MUC alert hooks
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))))
346 (if nick
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
350 group)))
351 (format "Message in %s" (jabber-jid-displayname group)))))
353 (defun jabber-muc-wave (nick group buffer text proposed-alert)
354 "Play the wave file specified in `jabber-alert-muc-wave'"
355 (when proposed-alert
356 (funcall jabber-play-sound-file jabber-alert-muc-wave)))
358 (defun jabber-muc-display (nick group buffer text proposed-alert)
359 "Display the buffer where a new message has arrived."
360 (when proposed-alert
361 (display-buffer buffer)))
363 (defun jabber-muc-switch (nick group buffer text proposed-alert)
364 "Switch to the buffer where a new message has arrived."
365 (when proposed-alert
366 (switch-to-buffer buffer)))
368 (defun jabber-muc-scroll (nick group buffer text proposed-alert)
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'."
379 (cond
380 ((equal oldstatus newstatus)
381 nil)
383 (let ((formattedname
384 (if (> (length (get who 'name)) 0)
385 (get who 'name)
386 (symbol-name who)))
387 (formattedstatus
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"))))
394 (concat " is now "
396 (cdr (assoc newstatus jabber-presence-strings))
397 newstatus))))
398 (formattedtext
399 (if (> (length statustext) 0)
400 (concat " (" statustext ")")
401 "")))
402 (concat formattedname formattedstatus formattedtext)))))
404 (defun jabber-presence-wave (who oldstatus newstatus statustext proposed-alert)
405 "Play the wave file specified in `jabber-alert-presence-wave'"
406 (when proposed-alert
407 (let* ((case-fold-search t)
408 (bare-jid (symbol-name who))
409 (sound-file (or (dolist (entry jabber-alert-presence-wave-alist)
410 (when (string-match (car entry) bare-jid)
411 (return (cdr entry))))
412 jabber-alert-presence-wave)))
413 (unless (equal sound-file "")
414 (funcall jabber-play-sound-file sound-file)))))
416 ;; This is now defined in jabber-roster.el.
417 ;; (defun jabber-presence-update-roster (who oldstatus newstatus statustext proposed-alert)
418 ;; "Update the roster display by calling `jabber-display-roster'"
419 ;; (jabber-display-roster))
421 (defun jabber-presence-display (who oldstatus newstatus statustext proposed-alert)
422 "Display the roster buffer"
423 (when proposed-alert
424 (display-buffer jabber-roster-buffer)))
426 (defun jabber-presence-switch (who oldstatus newstatus statustext proposed-alert)
427 "Switch to the roster buffer"
428 (when proposed-alert
429 (switch-to-buffer jabber-roster-buffer)))
431 ;;; Info alert hooks
433 (defun jabber-info-default-message (infotype buffer)
434 "Function for constructing info alert messages.
436 The argument is INFOTYPE, a symbol telling the kind of info request completed.
437 This function uses `jabber-info-message-alist' to find a message."
438 (concat (cdr (assq infotype jabber-info-message-alist))
439 " (buffer "(buffer-name buffer) ")"))
441 (defun jabber-info-wave (infotype buffer proposed-alert)
442 "Play the wave file specified in `jabber-alert-info-wave'"
443 (if proposed-alert
444 (funcall jabber-play-sound-file jabber-alert-info-wave)))
446 (defun jabber-info-display (infotype buffer proposed-alert)
447 "Display buffer of completed request"
448 (when proposed-alert
449 (display-buffer buffer)))
451 (defun jabber-info-switch (infotype buffer proposed-alert)
452 "Switch to buffer of completed request"
453 (when proposed-alert
454 (switch-to-buffer buffer)))
456 ;;; Personal alert hooks
457 (defmacro define-personal-jabber-alert (name)
458 "From ALERT function, make ALERT-personal function. Makes sence only for MUC."
459 (let ((sn (symbol-name name)))
460 (let ((func (intern (format "%s-personal" sn))))
461 `(progn
462 (defun ,func (nick group buffer text proposed-alert)
463 (if (jabber-muc-looks-like-personal-p text group)
464 (,name nick group buffer text proposed-alert)))
465 (pushnew (quote ,func) (get 'jabber-alert-muc-hooks 'custom-options)))))
468 (define-personal-jabber-alert jabber-muc-beep)
469 (define-personal-jabber-alert jabber-muc-wave)
470 (define-personal-jabber-alert jabber-muc-echo)
471 (define-personal-jabber-alert jabber-muc-switch)
472 (define-personal-jabber-alert jabber-muc-display)
474 (provide 'jabber-alert)
476 ;;; arch-tag: 725bd73e-c613-4fdc-a11d-3392a7598d4f