*** empty log message ***
[gnus] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
5 ;; Keywords: news xpm annotation glyph faces
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 ;; (require 'xpm)
30 (require 'annotations)
31 (require 'custom)
32 (require 'gnus-art)
33 (require 'gnus-win)
34
35 ;;; User variables:
36
37 (defgroup picons nil
38   "Show pictures of people, domains, and newsgroups (XEmacs).
39 For this to work, you must add gnus-group-display-picons to the
40 gnus-summary-display-hook or to the gnus-article-display-hook
41 depending on what gnus-picons-display-where is set to.  You must
42 also add gnus-article-display-picons to gnus-article-display-hook."
43   :group 'gnus-visual)
44
45 (defcustom gnus-picons-display-where 'picons
46   "Where to display the group and article icons.
47 Legal values are `article' and `picons'."
48   :type '(choice symbol string)
49   :group 'picons)
50
51 (defcustom gnus-picons-has-modeline-p t
52   "*Whether the picons window should have a modeline.
53 This is only useful if `gnus-picons-display-where' is `picons'."
54   :type 'boolean
55   :group 'picons)
56
57 (defcustom gnus-picons-database "/usr/local/faces"
58   "*Defines the location of the faces database.
59 For information on obtaining this database of pretty pictures, please
60 see http://www.cs.indiana.edu/picons/ftp/index.html"
61   :type 'directory
62   :group 'picons)
63
64 (defcustom gnus-picons-news-directories '("news")
65   "*List of directories to search for newsgroups faces."
66   :type '(repeat string)
67   :group 'picons)
68 (define-obsolete-variable-alias 'gnus-picons-news-directory
69   'gnus-picons-news-directories)
70
71 (defcustom gnus-picons-user-directories '("local" "users" "usenix" "misc")
72   "*List of directories to search for user faces."
73   :type '(repeat string)
74   :group 'picons)
75
76 (defcustom gnus-picons-domain-directories '("domains")
77   "*List of directories to search for domain faces.
78 Some people may want to add \"unknown\" to this list."
79   :type '(repeat string)
80   :group 'picons)
81
82 (defcustom gnus-picons-refresh-before-display nil
83   "*If non-nil, display the article buffer before computing the picons."
84   :type 'boolean
85   :group 'picons)
86
87 (defcustom gnus-picons-group-excluded-groups nil
88   "*If this regexp matches the group name, group picons will be disabled."
89   :type 'regexp
90   :group 'picons)
91
92 (defcustom gnus-picons-x-face-file-name
93   (format "/tmp/picon-xface.%s.xbm" (user-login-name))
94   "*The name of the file in which to store the converted X-face header."
95   :type 'string
96   :group 'picons)
97
98 (defcustom gnus-picons-convert-x-face (format "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | pbmtoxbm > %s" gnus-picons-x-face-file-name)
99   "*Command to convert the x-face header into a xbm file."
100   :type 'string
101   :group 'picons)
102
103 (defcustom gnus-picons-display-as-address t
104   "*If t display textual email addresses along with pictures."
105   :type 'boolean
106   :group 'picons)
107
108 (defcustom gnus-picons-file-suffixes
109   (when (featurep 'x)
110     (let ((types (list "xbm")))
111       (when (featurep 'gif)
112         (push "gif" types))
113       (when (featurep 'xpm)
114         (push "xpm" types))
115       types))
116   "*List of suffixes on picon file names to try."
117   :type '(repeat string)
118   :group 'picons)
119
120 (defcustom gnus-picons-display-article-move-p nil
121   "*Whether to move point to first empty line when displaying picons.
122 This has only an effect if `gnus-picons-display-where' has value `article'."
123   :type 'boolean
124   :group 'picons)
125
126 (defcustom gnus-picons-clear-cache-on-shutdown t
127   "*Whether to clear the picons cache when exiting gnus.
128 Gnus caches every picons it finds while it is running.  This saves
129 some time in the search process but eats some memory.  If this
130 variable is set to nil, Gnus will never clear the cache itself; you
131 will have to manually call `gnus-picons-clear-cache' to clear it.
132 Otherwise the cache will be cleared every time you exit Gnus."
133   :type 'boolean
134   :group 'picons)
135
136 (defcustom gnus-picons-piconsearch-url nil
137   "*The url to query for picons.  Setting this to nil will disable it.
138 The only publicly available address currently known is
139 http://www.cs.indiana.edu:800/piconsearch.  If you know of any other,
140 please tell me so that we can list it."
141   :type '(choice (const :tag "Disable" :value nil)
142                  (const :tag "www.cs.indiana.edu"
143                         :value "http://www.cs.indiana.edu:800/piconsearch")
144                  (string))
145   :group 'picons)
146
147 (defface gnus-picons-xbm-face '((t (:foreground "black" :background "white")))
148   "Face to show xbm picons in."
149   :group 'picons)
150
151 (defface gnus-picons-face '((t (:foreground "black" :background "white")))
152   "Face to show picons in."
153   :group 'picons)
154
155 (defcustom gnus-picons-setup-hook nil
156   "Hook run in Picons buffers."
157   :group 'picons
158   :type 'hook)
159
160 ;;; Internal variables:
161
162 (defvar gnus-picons-setup-p nil)
163 (defvar gnus-picons-processes-alist nil
164   "Picons processes currently running and their environment.")
165 (defvar gnus-picons-glyph-alist nil
166   "Picons glyphs cache.
167 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
168 (defvar gnus-picons-url-alist nil
169   "Picons file names cache.
170 List of pairs (KEY . NAME) where KEY is (USER HOST DBS) and NAME is an URL.")
171
172 (defvar gnus-picons-jobs-alist nil
173   "List of jobs that still need be done.
174 This is a list of (SYM-ANN TAG ARGS...) where SYM-ANN three annotations list,
175 TAG is one of `picon' or `search' indicating that the job should query a
176 picon or do a search for picons file names, and ARGS is some additionnal
177 arguments necessary for the job.")
178
179 (defvar gnus-picons-job-already-running nil
180   "Lock to ensure only one stream of http requests is running.")
181
182 ;;; Functions:
183
184 (defun gnus-picons-remove-all ()
185   "Removes all picons from the Gnus display(s)."
186   (interactive)
187   (map-extents (function (lambda (ext unused) (delete-annotation ext) nil))
188                nil nil nil nil nil 'gnus-picon)
189   (setq gnus-picons-jobs-alist '())
190   ;; notify running job that it may have been preempted
191   (if (and (listp gnus-picons-job-already-running)
192            gnus-picons-job-already-running)
193       (setq gnus-picons-job-already-running t)))
194
195 (defun gnus-get-buffer-name (variable)
196   "Returns the buffer name associated with the contents of a variable."
197   (let ((buf (gnus-get-buffer-create
198               (gnus-window-to-buffer-helper
199                (cdr (assq variable gnus-window-to-buffer))))))
200     (and buf
201          (buffer-name buf))))
202
203 (defun gnus-picons-buffer-name ()
204   (cond ((or (stringp gnus-picons-display-where)
205              (bufferp gnus-picons-display-where))
206          gnus-picons-display-where)
207         ((eq gnus-picons-display-where 'picons)
208          (if gnus-single-article-buffer
209              "*Picons*"
210            (concat "*Picons " gnus-newsgroup-name "*")))
211         (t
212          (gnus-get-buffer-name gnus-picons-display-where))))
213
214 (defun gnus-picons-kill-buffer ()
215   (let ((buf (get-buffer (gnus-picons-buffer-name))))
216     (when (and (buffer-live-p buf)
217                (string-match "Picons" (buffer-name buf)))
218       (kill-buffer buf))))
219
220 (defun gnus-picons-setup-buffer ()
221   (let ((name (gnus-picons-buffer-name)))
222     (save-excursion
223       (if (and (get-buffer name)
224                (with-current-buffer name
225                  gnus-picons-setup-p))
226           (set-buffer name)
227         (set-buffer (gnus-get-buffer-create name))
228         (buffer-disable-undo)
229         (setq buffer-read-only t)
230         (run-hooks 'gnus-picons-setup-hook)
231         (set (make-local-variable 'gnus-picons-setup-p) t)
232         (add-hook 'gnus-summary-prepare-exit-hook 'gnus-picons-kill-buffer))
233       (current-buffer))))
234
235 (defun gnus-picons-set-buffer ()
236   (set-buffer (gnus-picons-setup-buffer))
237   (goto-char (point-min))
238   (if (and (eq gnus-picons-display-where 'article)
239            gnus-picons-display-article-move-p)
240       (if (search-forward "\n\n" nil t)
241           (forward-line -1)
242         (goto-char (point-max)))
243     (setq buffer-read-only t)
244     (unless gnus-picons-has-modeline-p
245       (set-specifier has-modeline-p
246                      (list (list (current-buffer)
247                                  (cons nil gnus-picons-has-modeline-p)))))))
248
249 (defun gnus-picons-prepare-for-annotations ()
250   "Prepare picons buffer for putting annotations."
251   ;; let drawing catch up
252   (when gnus-picons-refresh-before-display
253     (sit-for 0))
254   (gnus-picons-set-buffer)
255   (gnus-picons-remove-all))
256
257 (defun gnus-picons-make-annotation (&rest args)
258   (let ((annot (apply 'make-annotation args)))
259     (set-extent-property annot 'gnus-picon t)
260     (set-extent-property annot 'duplicable t)
261     annot))
262
263 (defun gnus-picons-article-display-x-face ()
264   "Display the x-face header bitmap in the 'gnus-picons-display-where buffer."
265   (let ((gnus-article-x-face-command 'gnus-picons-display-x-face))
266     (gnus-article-display-x-face)))
267
268 (defun gnus-picons-x-face-sentinel (process event)
269   (when (memq process gnus-picons-processes-alist)
270     (setq gnus-picons-processes-alist
271           (delq process gnus-picons-processes-alist))
272     (gnus-picons-set-buffer)
273     (gnus-picons-make-annotation
274      (make-glyph gnus-picons-x-face-file-name) nil 'text)
275     (when (file-exists-p gnus-picons-x-face-file-name)
276       (delete-file gnus-picons-x-face-file-name))))
277
278 (defun gnus-picons-display-x-face (beg end)
279   "Function to display the x-face header in the picons window.
280 To use:  (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
281   (interactive)
282   (if (featurep 'xface)
283       ;; Use builtin support
284       (save-excursion
285         ;; Don't remove this binding, it is really needed: when
286         ;; `gnus-picons-set-buffer' changes buffer (like when it is
287         ;; set to display picons outside of the article buffer), BEG
288         ;; and END still refer the buffer current now !
289         (let ((buf (current-buffer)))
290           (gnus-picons-set-buffer)
291           (gnus-picons-make-annotation
292            (vector 'xface
293                    :data (concat "X-Face: " (buffer-substring beg end buf)))
294            nil 'text nil nil nil t)))
295     ;; convert the x-face header to a .xbm file
296     (let* ((process-connection-type nil)
297            (process (start-process-shell-command
298                      "gnus-x-face" nil gnus-picons-convert-x-face)))
299       (push process gnus-picons-processes-alist)
300       (process-kill-without-query process)
301       (set-process-sentinel process 'gnus-picons-x-face-sentinel)
302       (process-send-region process beg end)
303       (process-send-eof process))))
304
305 (defun gnus-article-display-picons ()
306   "Display faces for an author and her domain in gnus-picons-display-where."
307   (interactive)
308   (let (from at-idx)
309     (when (and (featurep 'xpm)
310                (or (not (fboundp 'device-type)) (equal (device-type) 'x))
311                (setq from (mail-fetch-field "from"))
312                (setq from (downcase (or (cadr (mail-extract-address-components
313                                                from))
314                                         "")))
315                (or (setq at-idx (string-match "@" from))
316                    (setq at-idx (length from))))
317       (save-excursion
318         (let ((username (downcase (substring from 0 at-idx)))
319               (addrs (if (eq at-idx (length from))
320                          (if gnus-local-domain
321                              (message-tokenize-header gnus-local-domain "."))
322                        (message-tokenize-header (substring from (1+ at-idx))
323                                                 "."))))
324           (gnus-picons-prepare-for-annotations)
325           (gnus-group-display-picons)
326           (unless gnus-picons-display-article-move-p
327             (let ((buffer-read-only nil)
328                   (case-fold-search t))
329               (when (re-search-forward "^From *: *" nil t)
330                 (when (search-forward from (gnus-point-at-eol) t)
331                   (gnus-put-text-property
332                    (match-beginning 0) (match-end 0)
333                    'invisible t)))))
334           (if (null gnus-picons-piconsearch-url)
335               (progn
336                 (gnus-picons-display-pairs (gnus-picons-lookup-pairs
337                                             addrs
338                                             gnus-picons-domain-directories)
339                                            gnus-picons-display-as-address
340                                            "." t)
341                 (if (and gnus-picons-display-as-address addrs)
342                     (gnus-picons-make-annotation
343                      [string :data "@"] nil 'text nil nil nil t))
344                 (gnus-picons-display-picon-or-name
345                  (gnus-picons-lookup-user username addrs)
346                  username t))
347             (push (list 'gnus-article-annotations 'search username addrs
348                         gnus-picons-domain-directories t (point-marker))
349                   gnus-picons-jobs-alist)
350             (gnus-picons-next-job)))))))
351
352 (defun gnus-group-display-picons ()
353   "Display icons for the group in the `gnus-picons-display-where' buffer."
354   (interactive)
355   (when (and (featurep 'xpm)
356              (or (not (fboundp 'device-type)) (equal (device-type) 'x))
357              (or (null gnus-picons-group-excluded-groups)
358                  (not (string-match gnus-picons-group-excluded-groups
359                                     gnus-newsgroup-name))))
360     (let* ((newsgroups (mail-fetch-field "newsgroups"))
361            (groups
362             (if (or gnus-picons-display-article-move-p
363                     (not newsgroups))
364                 (list (gnus-group-real-name gnus-newsgroup-name))
365               (split-string newsgroups ",")))
366            group)
367       (save-excursion
368         (gnus-picons-prepare-for-annotations)
369         (while (setq group (pop groups))
370           (unless gnus-picons-display-article-move-p
371             (let ((buffer-read-only nil)
372                   (case-fold-search t))
373               (goto-char (point-min))
374               (if (and (re-search-forward "^Newsgroups *: *" nil t)
375                        (search-forward group (gnus-point-at-eol) t))
376                   (gnus-put-text-property
377                    (match-beginning 0) (match-end 0)
378                    'invisible t)
379                 (goto-char (point-min))
380                 (search-forward "\n\n")
381                 (backward-char 1))))
382           (if (null gnus-picons-piconsearch-url)
383               (gnus-picons-display-pairs
384                (gnus-picons-lookup-pairs
385                 (reverse (split-string group "\\."))
386                 gnus-picons-news-directories)
387                t ".")
388             (push (list 'gnus-group-annotations 'search nil
389                         (split-string group "\\.")
390                         (if (listp gnus-picons-news-directories)
391                             gnus-picons-news-directories
392                           (list gnus-picons-news-directories))
393                         nil (point-marker))
394                   gnus-picons-jobs-alist)
395             (gnus-picons-next-job))
396
397           (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all))))))
398
399 (defun gnus-picons-lookup-internal (addrs dir)
400   (setq dir (expand-file-name dir gnus-picons-database))
401   (gnus-picons-try-face (dolist (part (reverse addrs) dir)
402                           (setq dir (expand-file-name part dir)))))
403
404 (defun gnus-picons-lookup (addrs dirs)
405   "Lookup the picon for ADDRS in databases DIRS.
406 Returns the picon filename or NIL if none found."
407   (let (result)
408     (while (and dirs (null result))
409       (setq result (gnus-picons-lookup-internal addrs (pop dirs))))
410     result))
411
412 (defun gnus-picons-lookup-user-internal (user domains)
413   (let ((dirs gnus-picons-user-directories)
414         domains-tmp dir picon)
415     (while (and dirs (null picon))
416       (setq domains-tmp domains
417             dir (pop dirs))
418       (while (and domains-tmp
419                   (null (setq picon (gnus-picons-lookup-internal
420                                      (cons user domains-tmp) dir))))
421         (pop domains-tmp))
422       ;; Also make a try in MISC subdir
423       (unless picon
424         (setq picon (gnus-picons-lookup-internal (list user "MISC") dir))))
425     picon))
426
427 (defun gnus-picons-lookup-user (user domains)
428   "Lookup the picon for USER at DOMAINS.
429 USER is a string containing a name.
430 DOMAINS is a list of strings from the fully qualified domain name."
431   (or (gnus-picons-lookup-user-internal user domains)
432       (gnus-picons-lookup-user-internal "unknown" domains)))
433
434 (defun gnus-picons-lookup-pairs (domains directories)
435   "Lookup picons for DOMAINS and all its parents in DIRECTORIES.
436 Returns a list of PAIRS whose CAR is the picon filename or NIL if
437 none, and whose CDR is the corresponding element of DOMAINS."
438   (let (picons)
439     (setq directories (if (listp directories)
440                           directories
441                         (list directories)))
442     (while domains
443       (push (list (gnus-picons-lookup (cons "unknown" domains) directories)
444                   (pop domains))
445             picons))
446     picons))
447
448 (defun gnus-picons-display-picon-or-name (picon name &optional right-p)
449   (cond (picon (gnus-picons-display-glyph picon name right-p))
450         (gnus-picons-display-as-address (list (gnus-picons-make-annotation
451                                                (vector 'string :data name)
452                                                nil 'text
453                                                nil nil nil right-p)))))
454
455 (defun gnus-picons-display-pairs (pairs &optional bar-p dot-p right-p)
456   "Display picons in list PAIRS."
457   (let ((domain-p (and gnus-picons-display-as-address dot-p))
458         pair picons)
459     (when (and bar-p domain-p right-p
460                gnus-picons-display-article-move-p)
461       (setq picons (gnus-picons-display-glyph
462                     (let ((gnus-picons-file-suffixes '("xbm")))
463                       (gnus-picons-try-face
464                        gnus-xmas-glyph-directory "bar."))
465                     nil right-p)))
466     (while (setq pair (pop pairs))
467       (setq picons (nconc picons
468                           (gnus-picons-display-picon-or-name
469                            (car pair) (cadr pair) right-p)
470                           (if (and domain-p pairs)
471                               (list (gnus-picons-make-annotation
472                                      (vector 'string :data dot-p)
473                                      nil 'text nil nil nil right-p))))))
474     picons))
475
476 (defun gnus-picons-try-face (dir &optional filebase)
477   (let* ((dir (file-name-as-directory dir))
478          (filebase (or filebase "face."))
479          (key (concat dir filebase))
480          (glyph (cdr (assoc key gnus-picons-glyph-alist)))
481          (suffixes gnus-picons-file-suffixes)
482          f suf)
483     (while (setq suf (pop suffixes))
484       (when (file-exists-p (setq f (expand-file-name
485                                     (concat filebase suf)
486                                     dir)))
487         (setq suffixes nil
488               glyph (make-glyph f))
489         (if (equal suf "xbm")
490             (set-glyph-face glyph 'gnus-picons-xbm-face)
491           (set-glyph-face glyph 'gnus-picons-face))
492         (push (cons key glyph) gnus-picons-glyph-alist)))
493     glyph))
494
495 (defun gnus-picons-display-glyph (glyph &optional part rightp)
496   (set-glyph-baseline glyph 70)
497   (let ((new (gnus-picons-make-annotation
498               glyph (point) 'text nil nil nil rightp)))
499     (when (and part gnus-picons-display-as-address)
500       (set-annotation-data
501        new (cons new (make-glyph (vector 'string :data part))))
502       (set-annotation-action new 'gnus-picons-action-toggle))
503     (nconc
504      (list new)
505      (if (and (eq major-mode 'gnus-article-mode)
506               (not gnus-picons-display-as-address)
507               (not part))
508          (list (gnus-picons-make-annotation [string :data " "] (point)
509                                             'text nil nil nil rightp))))))
510
511 (defun gnus-picons-action-toggle (data)
512   "Toggle annotation."
513   (interactive "e")
514   (let* ((annot (car data))
515          (glyph (annotation-glyph annot)))
516     (set-annotation-glyph annot (cdr data))
517     (set-annotation-data annot (cons annot glyph))))
518
519 (defun gnus-picons-clear-cache ()
520   "Clear the picons cache."
521   (interactive)
522   (setq gnus-picons-glyph-alist nil
523         gnus-picons-url-alist nil))
524
525 (gnus-add-shutdown 'gnus-picons-close 'gnus)
526
527 (defun gnus-picons-close ()
528   "Shut down the picons."
529   (if gnus-picons-clear-cache-on-shutdown
530       (gnus-picons-clear-cache)))
531
532 ;;; Query a remote DB.  This requires some stuff from w3 !
533
534 (eval-and-compile
535   (ignore-errors
536     (require 'url)
537     (require 'w3-forms)))
538
539 (defun gnus-picons-url-retrieve (url fn arg)
540   (let ((old-asynch (default-value 'url-be-asynchronous))
541         (url-working-buffer (generate-new-buffer " *picons*"))
542         (url-package-name "Gnus")
543         (url-package-version gnus-version-number)
544         url-request-method)
545     (setq-default url-be-asynchronous t)
546     (save-excursion
547       (set-buffer url-working-buffer)
548       (setq url-be-asynchronous t
549             url-current-callback-data arg
550             url-current-callback-func fn)
551       (url-retrieve url t))
552     (setq-default url-be-asynchronous old-asynch)))
553
554 (defun gnus-picons-make-glyph (type)
555   "Make a TYPE glyph using current buffer as data.  Handles xbm nicely."
556   (cond ((null type) nil)
557         ((eq type 'xbm) (let ((fname (make-temp-name "/tmp/picon")))
558                           (write-region (point-min) (point-max) fname
559                                         nil 'quiet)
560                           (prog1 (make-glyph (vector 'xbm :file fname))
561                             (delete-file fname))))
562         (t (make-glyph (vector type :data (buffer-string))))))
563
564 ;;; Parsing of piconsearch result page.
565
566 ;; Assumes:
567 ;; 1 - each value field has the form: "<strong>key</strong> = <kbd>value</kbd>"
568 ;; 2 - a "<p>" separates the keywords from the results
569 ;; 3 - every results begins by the path within the database at the beginning
570 ;;     of the line in raw text.
571 ;; 3b - and the href following it is the preferred image type.
572
573 ;; if 1 or 2 is not met, it will probably cause an error.  The other
574 ;; will go undetected
575
576 (defun gnus-picons-parse-value (name)
577   (goto-char (point-min))
578   (if (re-search-forward (concat "<strong>"
579                              (regexp-quote name)
580                              "</strong> *= *<kbd> *\\([^ <][^<]*\\) *</kbd>")
581                          nil t)
582       (buffer-substring (match-beginning 1) (match-end 1))))
583
584 (defun gnus-picons-parse-filenames ()
585   ;; returns an alist of ((USER ADDRS DB) . URL)
586   (let ((case-fold-search t)
587         (user (gnus-picons-parse-value "user"))
588         (host (gnus-picons-parse-value "host"))
589         (dbs (message-tokenize-header (gnus-picons-parse-value "db") " "))
590         start-re cur-db cur-host cur-user types res)
591     ;; now point will be somewhere in the header.  Find beginning of
592     ;; entries
593     (when (and user host dbs)
594       (setq start-re
595             (concat
596              ;; dbs
597              "^\\(" (mapconcat 'identity dbs "\\|") "\\)/"
598              ;; host
599              "\\(\\(" (replace-in-string host "\\." "/\\|" t) "/\\|MISC/\\)*\\)"
600              ;; user
601              "\\(" (regexp-quote user) "\\|unknown\\)/"
602              "face\\."))
603       (re-search-forward "<p>[ \t\n]*")
604       (while (re-search-forward start-re nil t)
605         (setq cur-db (buffer-substring (match-beginning 1) (match-end 1))
606               cur-host (buffer-substring (match-beginning 2) (match-end 2))
607               cur-user (buffer-substring (match-beginning 4) (match-end 4))
608               cur-host (nreverse (message-tokenize-header cur-host "/")))
609         ;; XXX - KLUDGE: there is a blank picon in news/MISC/unknown
610         (unless (and (string-equal cur-db "news")
611                      (string-equal cur-user "unknown")
612                      (equal cur-host '("MISC")))
613           ;; ok now we have found an entry (USER HOST DB), find the
614           ;; corresponding picon URL
615           (save-restriction
616             ;; restrict region to this entry
617             (narrow-to-region (point) (search-forward "<br>"))
618             (goto-char (point-min))
619             (setq types gnus-picons-file-suffixes)
620             (while (and types
621                         (not (re-search-forward
622                               (concat "<a[ \t\n]+href=\"\\([^\"]*\\."
623                                       (regexp-quote (car types)) "\\)\"")
624                               nil t)))
625               (pop types))
626             (push (cons (list cur-user cur-host cur-db)
627                         (buffer-substring (match-beginning 1) (match-end 1)))
628                   res))))
629       (nreverse res))))
630
631 ;;; picon network display functions :
632
633 (defun gnus-picons-network-display-internal (sym-ann glyph part right-p marker)
634   (let ((buf (marker-buffer marker))
635         (pos (marker-position marker)))
636     (if (and buf pos)
637         (save-excursion
638           (set-buffer buf)
639           (goto-char pos)
640           (gnus-picons-display-picon-or-name glyph part right-p))))
641   (gnus-picons-next-job-internal))
642
643 (defun gnus-picons-network-display-callback (url part sym-ann right-p marker)
644   (let ((glyph (gnus-picons-make-glyph (cdr (assoc url-current-mime-type
645                                                    w3-image-mappings)))))
646     (kill-buffer (current-buffer))
647     (push (cons url glyph) gnus-picons-glyph-alist)
648     ;; only do the job if it has not been preempted.
649     (if (equal gnus-picons-job-already-running
650                (list sym-ann 'picon url part right-p marker))
651         (gnus-picons-network-display-internal sym-ann glyph part right-p marker)
652       (gnus-picons-next-job-internal))))
653
654 (defun gnus-picons-network-display (url part sym-ann right-p marker)
655   (let ((cache (assoc url gnus-picons-glyph-alist)))
656     (if (or cache (null url))
657         (gnus-picons-network-display-internal
658          sym-ann (cdr cache) part right-p marker)
659       (gnus-picons-url-retrieve url 'gnus-picons-network-display-callback
660                                 (list url part sym-ann right-p marker)))))
661
662 ;;; search job functions
663
664 (defun gnus-picons-display-bar-p ()
665   (and (not (eq gnus-picons-display-where 'article))
666        gnus-picons-display-as-address))
667
668 (defun gnus-picons-network-search-internal (user addrs dbs sym-ann right-p
669                                                  marker &optional fnames)
670   (let (curkey dom pfx url dbs-tmp cache new-jobs)
671     ;; First do the domain search
672     (dolist (part (if right-p
673                       (reverse addrs)
674                     addrs))
675       (setq pfx (nconc (list part) pfx)
676             dom (cond ((and dom right-p) (concat part "." dom))
677                       (dom (concat dom "." part))
678                       (t part))
679             curkey (list "unknown" dom dbs))
680       (when (null (setq cache (assoc curkey gnus-picons-url-alist)))
681         ;; This one is not yet in the cache, create a new entry
682         ;; Search for an entry
683         (setq dbs-tmp dbs
684               url nil)
685         (while (and dbs-tmp (null url))
686           (setq url (or (cdr (assoc (list "unknown" pfx (car dbs-tmp)) fnames))
687                         (and (eq dom part)
688                              ;; This is the first component.  Try the
689                              ;; catch-all MISC component
690                              (cdr (assoc (list "unknown"
691                                                '("MISC")
692                                                (car dbs-tmp))
693                                          fnames)))))
694           (pop dbs-tmp))
695         (push (setq cache (cons curkey url)) gnus-picons-url-alist))
696       ;; Put this glyph in the job list
697       (if (and (not (eq dom part)) gnus-picons-display-as-address)
698           (push (list sym-ann "." right-p marker) new-jobs))
699       (push (list sym-ann 'picon (cdr cache) part right-p marker) new-jobs))
700     ;; next, the user search
701     (when user
702       (setq curkey (list user dom gnus-picons-user-directories))
703       (if (null (setq cache (assoc curkey gnus-picons-url-alist)))
704           (let ((users (list user "unknown"))
705                 dirs usr domains-tmp dir picon)
706             (while (and users (null picon))
707               (setq dirs gnus-picons-user-directories
708                     usr (pop users))
709               (while (and dirs (null picon))
710                 (setq domains-tmp addrs
711                       dir (pop dirs))
712                 (while (and domains-tmp
713                             (null (setq picon (assoc (list usr domains-tmp dir)
714                                                      fnames))))
715                   (pop domains-tmp))
716                 (unless picon
717                   (setq picon (assoc (list usr '("MISC") dir) fnames)))))
718             (push (setq cache (cons curkey (cdr picon)))
719                   gnus-picons-url-alist)))
720       (if (and gnus-picons-display-as-address new-jobs)
721           (push (list sym-ann "@" right-p marker) new-jobs))
722       (push (list sym-ann 'picon (cdr cache) user right-p marker) new-jobs))
723     (if (and (gnus-picons-display-bar-p) (not right-p))
724         (push (list sym-ann 'bar right-p marker) new-jobs))
725     ;; only put the jobs in the queue if this job has not been preempted.
726     (if (equal gnus-picons-job-already-running
727                (list sym-ann 'search user addrs dbs right-p marker))
728         (setq gnus-picons-jobs-alist
729               (nconc (if (and (gnus-picons-display-bar-p) right-p)
730                          (list (list sym-ann 'bar right-p marker)))
731                      (nreverse new-jobs)
732                      gnus-picons-jobs-alist)))
733     (gnus-picons-next-job-internal)))
734
735 (defun gnus-picons-network-search-callback (user addrs dbs sym-ann right-p
736                                                  marker)
737   (gnus-picons-network-search-internal
738    user addrs dbs sym-ann right-p marker
739    (prog1
740        (gnus-picons-parse-filenames)
741      (kill-buffer (current-buffer)))))
742
743 ;; Initiate a query on the picon database
744 (defun gnus-picons-network-search (user addrs dbs sym-ann right-p marker)
745   (let* ((host (mapconcat 'identity addrs "."))
746          (key (list (or user "unknown") host (if user
747                                                   gnus-picons-user-directories
748                                                 dbs)))
749          (cache (assoc key gnus-picons-url-alist)))
750     (if (null cache)
751         (gnus-picons-url-retrieve
752          (concat gnus-picons-piconsearch-url
753                  "?user=" (w3-form-encode-xwfu (or user "unknown"))
754                  "&host=" (w3-form-encode-xwfu host)
755                  "&db=" (mapconcat 'w3-form-encode-xwfu
756                                    (if user
757                                        (append dbs
758                                                gnus-picons-user-directories)
759                                      dbs)
760                                    "+"))
761          'gnus-picons-network-search-callback
762          (list user addrs dbs sym-ann right-p marker))
763       (gnus-picons-network-search-internal
764        user addrs dbs sym-ann right-p marker))))
765
766 ;;; Main jobs dispatcher function
767
768 (defun gnus-picons-next-job-internal ()
769   (when (setq gnus-picons-job-already-running (pop gnus-picons-jobs-alist))
770     (let* ((job gnus-picons-job-already-running)
771            (sym-ann (pop job))
772            (tag (pop job)))
773       (when tag
774         (cond
775          ((stringp tag);; (SYM-ANN "..." RIGHT-P MARKER)
776           (gnus-picons-network-display-internal
777            sym-ann nil tag (pop job) (pop job)))
778          ((eq 'bar tag);; (SYM-ANN 'bar RIGHT-P MARKER)
779           (gnus-picons-network-display-internal
780            sym-ann
781            (let ((gnus-picons-file-suffixes '("xbm")))
782              (gnus-picons-try-face
783               gnus-xmas-glyph-directory "bar."))
784            nil (pop job) (pop job)))
785          ((eq 'search tag);; (SYM-ANN 'search USER ADDRS DBS RIGHT-P MARKER)
786           (gnus-picons-network-search
787            (pop job) (pop job) (pop job) sym-ann (pop job) (pop job)))
788          ((eq 'picon tag);; (SYM-ANN 'picon URL PART RIGHT-P MARKER)
789           (gnus-picons-network-display
790            (pop job) (pop job) sym-ann (pop job) (pop job)))
791          (t
792           (setq gnus-picons-job-already-running nil)
793           (error "Unknown picon job tag %s" tag)))))))
794
795 (defun gnus-picons-next-job ()
796   "Start processing the job queue if it is not in progress."
797   (unless gnus-picons-job-already-running
798     (gnus-picons-next-job-internal)))
799
800 (provide 'gnus-picon)
801
802 ;;; gnus-picon.el ends here