Source

erc / erc-nicklist.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
;;; erc-nicklist.el --- Display channel nicknames in a side buffer.

;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.

;; Filename: erc-nicklist.el
;; Author: Lawrence Mitchell <wence@gmx.li>
;; Created: 2004-04-30
;; Keywords: IRC chat client Internet

;; This file is part of GNU Emacs.

;; GNU Emacs 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 2, or (at your option)
;; any later version.

;; GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:
;;
;; This provides a minimal mIRC style nicklist buffer for ERC.  To
;; activate, do M-x erc-nicklist RET in the channel buffer you want
;; the nicklist to appear for.  To close and quit the nicklist
;; buffer, do M-x erc-nicklist-quit RET.
;;
;; TODO:
;; o Somehow associate nicklist windows with channel windows so they
;;   appear together, and if one gets buried, then the other does.
;;
;; o Make "Query" and "Message" work.
;;
;; o Prettify the actual list of nicks in some way.
;;
;; o Add a proper erc-module that people can turn on and off, figure
;;   out a way of creating the nicklist window at an appropriate time
;;   --- probably in `erc-join-hook'.
;;
;; o Ensure XEmacs compatibility --- the mouse-menu support is likely
;;   broken.
;;
;; o Add option to display in a separate frame --- will again need to
;;   be able to associate the nicklist with the currently active
;;   channel buffer or something similar.
;;
;; o Allow toggling of visibility of nicklist via ERC commands.

;;; History:
;;

;; Changes by Edgar Gon�alves <edgar.goncalves@inesc-id.pt>
;; Jun 25 2005:
;;     - images are changed to a standard set of names.
;;     - /images now contain gaim's status icons.
;; May 31 2005:
;;     - tooltips are improved. they try to access bbdb for a nice nick!
;; Apr 26 2005:
;;     - erc-nicklist-channel-users-info was fixed (sorting bug)
;;     - Away names don't need parenthesis when using icons
;; Apr 26 2005:
;;     - nicks can display icons of their connection type (msn, icq, for now)
;; Mar 15 2005:
;;     - nicks now are different for unvoiced and op users
;;     - nicks now have tooltips displaying more info
;; Mar 18 2005:
;;     - queries now work ok, both on menu and keyb shortcut RET.
;;     - nicklist is now sorted ignoring the case. Voiced nicks will
;;       appear according to `erc-nicklist-voiced-position'.

;;; Code:

(require 'erc)
(condition-case nil
    (require 'erc-bbdb)
  (error nil))
(eval-when-compile (require 'cl))

(defconst erc-nicklist-version "$Revision$"
  "ERC Nicklist version.")

(defgroup erc-nicklist nil
  "Display a list of nicknames in a separate window."
  :group 'erc)

(defcustom erc-nicklist-use-icons t
  "*If non-nil, display an icon instead of the name of the chat medium.
By \"chat medium\", we mean IRC, AOL, MSN, ICQ, etc."
  :group 'erc-nicklist
  :type 'boolean)

(defcustom erc-nicklist-icons-directory
  (concat default-directory "images/")
  "*Directory of the PNG files for chat icons.
Icons are displayed if `erc-nicklist-use-icons' is non-nil."
  :group 'erc-nicklist
  :type 'string)

(defcustom erc-nicklist-voiced-position 'bottom
  "*Position of voiced nicks in the nicklist.
The value can be `top', `bottom' or nil (don't sort)."
  :group 'erc-nicklist
  :type  '(choice
	   (const :tag "Top" 'top)
	   (const :tag "Bottom" 'bottom)
	   (const :tag "Mixed" nil)))

(defcustom erc-nicklist-window-size 20.0
  "*The size of the nicklist window.

This specifies a percentage of the channel window width.

A negative value means the nicklist window appears on the left of the
channel window, and vice versa."
  :group 'erc-nicklist
  :type 'float)


(defun erc-nicklist-buffer-name (&optional buffer)
  "Return the buffer name for a nicklist associated with BUFFER.

If BUFFER is nil, use the value of `current-buffer'."
  (format " *%s-nicklist*" (buffer-name (or buffer (current-buffer)))))

(defun erc-nicklist-make-window ()
  "Create an ERC nicklist window.

See also `erc-nicklist-window-size'."
  (let ((width (floor (* (window-width) (/ erc-nicklist-window-size 100.0))))
	(buffer (erc-nicklist-buffer-name))
	window)
    (split-window-horizontally (- width))
    (setq window (next-window))
    (set-window-buffer window (get-buffer-create buffer))
    (with-current-buffer buffer
      (set-window-dedicated-p window t))))


(defvar erc-nicklist-images-alist '()
  "Alist that maps a connection type to an icon.")

(defun erc-nicklist-insert-medium-name-or-icon (host channel is-away)
  "Inserts an icon or a string identifying the current host type.
This is configured using `erc-nicklist-use-icons' and
`erc-nicklist-icons-directory'."
  ;; identify the network (for bitlebee usage):
  (let ((bitlbee-p (save-match-data
		     (string-match "\\`&bitlbee\\b"
				   (buffer-name channel)))))
    (cond ((and bitlbee-p
		(string= "login.icq.com" host))
	   (if erc-nicklist-use-icons
	       (if is-away
		   (insert-image (cdr (assoc 'icq-away
					     erc-nicklist-images-alist)))
		 (insert-image (cdr (assoc 'icq
					   erc-nicklist-images-alist))))
	     (insert "ICQ")))
	  (bitlbee-p
	   (if erc-nicklist-use-icons
	       (if is-away
		   (insert-image (cdr (assoc 'msn-away
					     erc-nicklist-images-alist)))
		 (insert-image (cdr (assoc 'msn
					   erc-nicklist-images-alist))))
	     (insert "MSN")))
	  (t
	   (if erc-nicklist-use-icons
	       (if is-away
		   (insert-image (cdr (assoc 'irc-away
					     erc-nicklist-images-alist)))
		 (insert-image (cdr (assoc 'irc
					   erc-nicklist-images-alist))))
	     (insert "IRC"))))
    (insert " ")))

(defun erc-nicklist-search-for-nick (finger-host)
  "Return the bitlbee-nick field for this contact given FINGER-HOST.
Seach for the BBDB record of this contact.  If not found, return nil."
  (when (boundp 'erc-bbdb-bitlbee-name-field)
    (let ((record (car
		   (erc-member-if
		    #'(lambda (r)
			(let ((fingers (bbdb-record-finger-host r)))
			  (when fingers
			    (string-match finger-host
					  (car (bbdb-record-finger-host r))))))
		    (bbdb-records)))))
      (when record
	(bbdb-get-field record erc-bbdb-bitlbee-name-field)))))

(defun erc-nicklist-insert-contents (channel)
  "Insert the nicklist contents, with text properties and the optional images."
  (setq buffer-read-only nil)
  (erase-buffer)
  (dolist (u (erc-nicklist-channel-users-info channel))
    (let* ((server-user (car u))
	   (channel-user (cdr u))
	   (nick     (erc-server-user-nickname server-user))
	   (host     (erc-server-user-host server-user))
	   (login    (erc-server-user-login server-user))
	   (full-name(erc-server-user-full-name server-user))
	   (info     (erc-server-user-info server-user))
	   (channels (erc-server-user-buffers server-user))
	   (op       (erc-channel-user-op channel-user))
	   (voice    (erc-channel-user-voice channel-user))
	   (bbdb-nick (erc-nicklist-search-for-nick (concat login "@" host)))
	   (away-status (if voice "" "\n(Away)"))
	   (balloon-text (concat bbdb-nick (if (string= "" bbdb-nick)
					       "" "\n")
				 "Login: " login "@" host
				 away-status)))
      (erc-nicklist-insert-medium-name-or-icon host channel (not voice))
      (unless (or voice erc-nicklist-use-icons)
	(setq nick (concat "(" nick ")")))
      (when op
	(setq nick (concat nick " (OP)")))
      (insert (erc-propertize nick
			      'erc-nicklist-nick nick
			      'mouse-face 'highlight
			      'erc-nicklist-channel channel
			      'help-echo balloon-text)
	      "\n")))
  (erc-nicklist-mode))


(defun erc-nicklist ()
  "Create an ERC nicklist buffer."
  (interactive)
  (let ((channel (current-buffer)))
    (unless (or (not erc-nicklist-use-icons)
		erc-nicklist-images-alist)
      (setq erc-nicklist-images-alist
	    `((msn      . ,(create-image (concat erc-nicklist-icons-directory
						 "msn-online.png")))
	      (msn-away . ,(create-image (concat erc-nicklist-icons-directory
						 "msn-offline.png")))
	      (irc      . ,(create-image (concat erc-nicklist-icons-directory
						 "irc-online.png")))
	      (irc-away . ,(create-image (concat erc-nicklist-icons-directory
						 "irc-offline.png")))
	      (icq      . ,(create-image (concat erc-nicklist-icons-directory
						 "icq-online.png")))
	      (icq-away . ,(create-image (concat erc-nicklist-icons-directory
						 "icq-offline.png"))))))
    (erc-nicklist-make-window)
    (with-current-buffer (get-buffer (erc-nicklist-buffer-name channel))
      (erc-nicklist-insert-contents channel)))
  (add-hook 'erc-channel-members-changed-hook #'erc-nicklist-update))

(defun erc-nicklist-update ()
  "Update the ERC nicklist buffer."
  (let ((b (get-buffer (erc-nicklist-buffer-name)))
	(channel (current-buffer)))
    (when b
      (with-current-buffer b
	(erc-nicklist-insert-contents channel)))))

(defvar erc-nicklist-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "<down-mouse-3>") 'erc-nicklist-menu)
    (define-key map "\C-j" 'erc-nicklist-kbd-menu)
    (define-key map "q"  'erc-nicklist-quit)
    (define-key map (kbd "RET") 'erc-nicklist-kbd-cmd-QUERY)
    map)
  "Keymap for `erc-nicklist-mode'.")

(define-derived-mode erc-nicklist-mode fundamental-mode
  "Nicklist"
  "Major mode for the ERC nicklist buffer."
  (setq buffer-read-only t))

(defun erc-nicklist-call-erc-command (command point buffer window)
  "Call an ERC COMMAND.

Depending on what COMMAND is, it's called with one of POINT, BUFFER,
or WINDOW as arguments."
  (when command
    (let* ((p (text-properties-at point))
	   (b (plist-get p 'erc-nicklist-channel)))
      (if (memq command '(erc-nicklist-quit ignore))
	  (funcall command window)
	;; EEEK!  Horrble, but it's the only way we can ensure the
	;; response goes to the correct buffer.
	(erc-set-active-buffer b)
	(switch-to-buffer-other-window b)
	(funcall command (plist-get p 'erc-nicklist-nick))))))

(defun erc-nicklist-cmd-QUERY (user &optional server)
  "Opens a query buffer with USER."
  ;; FIXME: find a way to switch to that buffer afterwards...
  (let ((send (if server
		  (format "QUERY %s %s" user server)
		  (format "QUERY %s" user))))
    (erc-cmd-QUERY user)
    t))

(defun erc-nicklist-kbd-cmd-QUERY (&optional window)
  (interactive)
  (let* ((p      (text-properties-at (point)))
	 (server (plist-get p 'erc-nicklist-channel))
	 (nick   (plist-get p 'erc-nicklist-nick))
	 (nick   (or (and (string-match "(\\(.*\\))" nick)
			  (match-string 1 nick))
		     nick))
	 (nick   (or (and (string-match "\\+\\(.*\\)" nick)
			  (match-string 1 nick))
		     nick))
	 (send   (format "QUERY %s %s" nick server)))
    (switch-to-buffer-other-window server)
    (erc-cmd-QUERY nick)))


(defvar erc-nicklist-menu
  (let ((map (make-sparse-keymap "Action")))
    (define-key map [erc-cmd-WHOIS]
      '("Whois" . erc-cmd-WHOIS))
    (define-key map [erc-cmd-DEOP]
      '("Deop" . erc-cmd-DEOP))
    (define-key map [erc-cmd-MSG]
      '("Message" . erc-cmd-MSG)) ;; TODO!
    (define-key map [erc-nicklist-cmd-QUERY]
      '("Query" . erc-nicklist-kbd-cmd-QUERY))
    (define-key map [ignore]
      '("Cancel" . ignore))
    (define-key map [erc-nicklist-quit]
      '("Close nicklist" . erc-nicklist-quit))
    map)
  "Menu keymap for the ERC nicklist.")

(defun erc-nicklist-quit (&optional window)
  "Delete the ERC nicklist.

Deletes WINDOW and stops updating the nicklist buffer."
  (interactive)
  (let ((b (window-buffer window)))
    (with-current-buffer b
      (set-buffer-modified-p nil)
      (kill-this-buffer)
      (remove-hook 'erc-channel-members-changed-hook 'erc-nicklist-update))))


(defun erc-nicklist-kbd-menu ()
  "Show the ERC nicklist menu."
  (interactive)
  (let* ((point (point))
	 (window (selected-window))
	 (buffer (current-buffer)))
    (with-current-buffer buffer
      (erc-nicklist-call-erc-command
       (car (x-popup-menu point
			  erc-nicklist-menu))
       point
       buffer
       window))))

(defun erc-nicklist-menu (&optional arg)
  "Show the ERC nicklist menu.

ARG is a parametrized event (see `interactive')."
  (interactive "e")
  (let* ((point (nth 1 (cadr arg)))
	 (window (car (cadr arg)))
	 (buffer (window-buffer window)))
    (with-current-buffer buffer
      (erc-nicklist-call-erc-command
       (car (x-popup-menu arg
			  erc-nicklist-menu))
       point
       buffer
       window))))


(defun erc-nicklist-channel-users-info (channel)
  "Return a nick-sorted list of all users on CHANNEL.
Result are elements in the form (SERVER-USER . CHANNEL-USER). The
list has all the voiced users according to
`erc-nicklist-voiced-position'."
  (let* ((nicks (erc-sort-channel-users-alphabetically
		 (with-current-buffer channel (erc-get-channel-user-list)))))
    (if erc-nicklist-voiced-position
	(let ((voiced-nicks (erc-remove-if-not
			     #'(lambda (x)
				 (null (erc-channel-user-voice (cdr x))))
			     nicks))
	      (devoiced-nicks (erc-remove-if-not
			       #'(lambda (x)
				   (erc-channel-user-voice
				    (cdr x)))
			       nicks)))
	  (cond ((eq erc-nicklist-voiced-position 'top)
		 (append devoiced-nicks voiced-nicks))
		((eq erc-nicklist-voiced-position 'bottom)
		 (append voiced-nicks devoiced-nicks))))
      nicks)))



(provide 'erc-nicklist)

;;; erc-nicklist.el ends here
;;
;; Local Variables:
;; indent-tabs-mode: t
;; tab-width: 8
;; End:

;; arch-tag: db37a256-87a7-4544-bd90-e5f16c9f5ca5