erc -- Update and prettify package-info.in provides.
[packages] / xemacs-packages / erc / erc-notify.el
1 ;;; erc-notify.el --- Online status change notification
2
3 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Mario Lang <mlang@lexx.delysid.org>
6 ;; Keywords: comm
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; This module defines a new command, /NOTIFY
28 ;; See the docstring of `erc-cmd-NOTIFY' for details.
29
30 ;;; Code:
31
32 (require 'erc)
33 (require 'erc-nets)
34 (eval-when-compile
35  (require 'cl)
36  (require 'pcomplete))
37
38 ;;;; Customizable variables
39
40 (defgroup erc-notify nil
41   "Track online status of certain nicknames."
42   :group 'erc)
43
44 (defcustom erc-notify-list nil
45   "*List of nicknames you want to be notified about online/offline
46 status change."
47   :group 'erc-notify
48   :type '(repeat string))
49
50 (defcustom erc-notify-interval 60
51   "*Time interval (in seconds) for checking online status of notificated
52 people."
53   :group 'erc-notify
54   :type 'integer)
55
56 (defcustom erc-notify-signon-hook nil
57   "*Hook run after someone on `erc-notify-list' has signed on.
58 Two arguments are passed to the function, SERVER and NICK, both
59 strings."
60   :group 'erc-notify
61   :type 'hook
62   :options '(erc-notify-signon))
63
64 (defcustom erc-notify-signoff-hook nil
65   "*Hook run after someone on `erc-notify-list' has signed off.
66 Two arguments are passed to the function, SERVER and NICK, both
67 strings."
68   :group 'erc-notify
69   :type 'hook
70   :options '(erc-notify-signoff))
71
72 (defun erc-notify-signon (server nick)
73   (message "%s signed on at %s" nick server))
74
75 (defun erc-notify-signoff (server nick)
76   (message "%s signed off from %s" nick server))
77
78 ;;;; Internal variables
79
80 (defvar erc-last-ison nil
81   "Last ISON information received through `erc-notify-timer'.")
82 (make-variable-buffer-local 'erc-last-ison)
83
84 (defvar erc-last-ison-time 0
85   "Last time ISON was sent to the server in `erc-notify-timer'.")
86 (make-variable-buffer-local 'erc-last-ison-time)
87
88 ;;;; Setup
89
90 (defun erc-notify-install-message-catalogs ()
91   (erc-define-catalog
92    'english
93    '((notify_current . "Notificated people online: %l")
94      (notify_list    . "Current notify list: %l")
95      (notify_on      . "Detected %n on IRC network %m")
96      (notify_off     . "%n has left IRC network %m"))))
97
98 ;;;###autoload (autoload 'erc-notify-mode "erc-notify" nil t)
99 (define-erc-module notify nil
100   "Periodically check for the online status of certain users and report
101 changes."
102   ((add-hook 'erc-timer-hook 'erc-notify-timer)
103    (add-hook 'erc-server-JOIN-functions 'erc-notify-JOIN)
104    (add-hook 'erc-server-NICK-functions 'erc-notify-NICK)
105    (add-hook 'erc-server-QUIT-functions 'erc-notify-QUIT))
106   ((remove-hook 'erc-timer-hook 'erc-notify-timer)
107    (remove-hook 'erc-server-JOIN-functions 'erc-notify-JOIN)
108    (remove-hook 'erc-server-NICK-functions 'erc-notify-NICK)
109    (remove-hook 'erc-server-QUIT-functions 'erc-notify-QUIT)))
110
111 ;;;; Timer handler
112
113 (defun erc-notify-timer (now)
114   (when (and erc-notify-list
115              (> (erc-time-diff
116                  erc-last-ison-time now)
117                 erc-notify-interval))
118     (erc-once-with-server-event
119      303
120      '(let* ((server (erc-response.sender parsed))
121              (ison-list (delete "" (split-string
122                                     (erc-response.contents parsed))))
123              (new-list ison-list)
124              (old-list (with-current-buffer (erc-server-buffer)
125                          erc-last-ison)))
126         (while new-list
127           (when (not (erc-member-ignore-case (car new-list) old-list))
128             (run-hook-with-args 'erc-notify-signon-hook server (car new-list))
129             (erc-display-message
130              parsed 'notice proc
131              'notify_on ?n (car new-list) ?m (erc-network-name)))
132           (setq new-list (cdr new-list)))
133         (while old-list
134           (when (not (erc-member-ignore-case (car old-list) ison-list))
135             (run-hook-with-args 'erc-notify-signoff-hook server (car old-list))
136             (erc-display-message
137              parsed 'notice proc
138              'notify_off ?n (car old-list) ?m (erc-network-name)))
139           (setq old-list (cdr old-list)))
140         (setq erc-last-ison ison-list)
141         t))
142     (erc-server-send
143      (concat "ISON " (mapconcat 'identity erc-notify-list " ")))
144     (setq erc-last-ison-time now)))
145
146 (defun erc-notify-JOIN (proc parsed)
147   "Check if channel joiner is on `erc-notify-list' and not on `erc-last-ison'.
148 If this condition is satisfied, produce a notify_on message and add the nick
149 to `erc-last-ison' to prevent any further notifications."
150   (let ((nick (erc-extract-nick (erc-response.sender parsed))))
151     (when (and (erc-member-ignore-case nick erc-notify-list)
152                (not (erc-member-ignore-case nick erc-last-ison)))
153       (add-to-list 'erc-last-ison nick)
154       (run-hook-with-args 'erc-notify-signon-hook
155                           (or erc-server-announced-name erc-session-server)
156                           nick)
157       (erc-display-message
158        parsed 'notice proc
159        'notify_on ?n nick ?m (erc-network-name)))
160     nil))
161
162 (defun erc-notify-NICK (proc parsed)
163   "Check if new nick is on `erc-notify-list' and not on `erc-last-ison'.
164 If this condition is satisfied, produce a notify_on message and add the nick
165 to `erc-last-ison' to prevent any further notifications."
166   (let ((nick (erc-response.contents parsed)))
167     (when (and (erc-member-ignore-case nick erc-notify-list)
168                (not (erc-member-ignore-case nick erc-last-ison)))
169       (add-to-list 'erc-last-ison nick)
170       (run-hook-with-args 'erc-notify-signon-hook
171                           (or erc-server-announced-name erc-session-server)
172                           nick)
173       (erc-display-message
174        parsed 'notice proc
175        'notify_on ?n nick ?m (erc-network-name)))
176     nil))
177
178 (defun erc-notify-QUIT (proc parsed)
179   "Check if quitter is on `erc-notify-list' and on `erc-last-ison'.
180 If this condition is satisfied, produce a notify_off message and remove the
181 nick from `erc-last-ison' to prevent any further notifications."
182   (let ((nick (erc-extract-nick (erc-response.sender parsed))))
183     (when (and (erc-member-ignore-case nick erc-notify-list)
184                (erc-member-ignore-case nick erc-last-ison))
185       (setq erc-last-ison (erc-delete-if `(lambda (el)
186                                             (string= ,(erc-downcase nick)
187                                                      (erc-downcase el)))
188                                          erc-last-ison))
189       (run-hook-with-args 'erc-notify-signoff-hook
190                           (or erc-server-announced-name erc-session-server)
191                           nick)
192       (erc-display-message
193        parsed 'notice proc
194        'notify_off ?n nick ?m (erc-network-name)))
195     nil))
196
197 ;;;; User level command
198
199 ;;;###autoload
200 (defun erc-cmd-NOTIFY (&rest args)
201   "Change `erc-notify-list' or list current notify-list members online.
202 Without args, list the current list of notificated people online,
203 with args, toggle notify status of people."
204   (cond
205    ((null args)
206     ;; Print current notificated people (online)
207     (let ((ison (with-current-buffer (erc-server-buffer) erc-last-ison)))
208       (if (not ison)
209           (erc-display-message
210            nil 'notice 'active "No ison-list yet!")
211         (erc-display-message
212          nil 'notice 'active
213          'notify_current ?l ison))))
214    ((string= (car args) "-l")
215     (erc-display-message nil 'notice 'active
216                          'notify_list ?l (mapconcat 'identity erc-notify-list
217                                                     " ")))
218    (t
219     (while args
220       (if (erc-member-ignore-case (car args) erc-notify-list)
221           (progn
222             (setq erc-notify-list (delete (car args) erc-notify-list))
223             ;; Remove the nick from the value of erc-last-ison in
224             ;; every server buffer.  This prevents seeing a signoff
225             ;; notification for a nick that you have just _removed_
226             ;; from your notify list.
227             (dolist (buf (erc-buffer-list))
228               (with-current-buffer buf
229                 (if (erc-server-buffer-p)
230                     (setq erc-last-ison (delete (car args) erc-last-ison))))))
231         (setq erc-notify-list (cons (erc-string-no-properties (car args))
232                                     erc-notify-list)))
233       (setq args (cdr args)))
234     (erc-display-message
235      nil 'notice 'active
236      'notify_list ?l (mapconcat 'identity erc-notify-list " "))))
237   t)
238
239 ;;;###autoload
240 (defun pcomplete/erc-mode/NOTIFY ()
241   (pcomplete-here (pcomplete-erc-all-nicks)))
242
243 (erc-notify-install-message-catalogs)
244
245 (provide 'erc-notify)
246
247 ;;; erc-notify.el ends here
248 ;;
249 ;; Local Variables:
250 ;; indent-tabs-mode: t
251 ;; tab-width: 8
252 ;; End:
253
254 ;; arch-tag: 0fb19dd0-1359-458a-89b7-81dc195a588e