Sync up with Pteroductyl Gnus 0.62
[elisp/gnus.git-] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2 ;; Copyright (C) 1996,97,98 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 ;;; Internal variables:
152
153 (defvar gnus-picons-processes-alist nil
154   "Picons processes currently running and their environment.")
155 (defvar gnus-picons-glyph-alist nil
156   "Picons glyphs cache.
157 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
158 (defvar gnus-picons-url-alist nil
159   "Picons file names cache.
160 List of pairs (KEY . NAME) where KEY is (USER HOST DBS) and NAME is an URL.")
161
162 (defvar gnus-picons-jobs-alist nil
163   "List of jobs that still need be done.
164 This is a list of (SYM-ANN TAG ARGS...) where SYM-ANN three annotations list,
165 TAG is one of `picon' or `search' indicating that the job should query a
166 picon or do a search for picons file names, and ARGS is some additionnal
167 arguments necessary for the job.")
168
169 (defvar gnus-picons-job-already-running nil
170   "Lock to ensure only one stream of http requests is running.")
171
172 ;;; Functions:
173
174 (defun gnus-picons-remove-all ()
175   "Removes all picons from the Gnus display(s)."
176   (interactive)
177   (map-extents (function (lambda (ext unused) (delete-annotation ext) nil))
178                nil nil nil nil nil 'gnus-picon)
179   (setq gnus-picons-jobs-alist '())
180   ;; notify running job that it may have been preempted
181   (if (and (listp gnus-picons-job-already-running)
182            gnus-picons-job-already-running)
183       (setq gnus-picons-job-already-running t)))
184
185 (defun gnus-get-buffer-name (variable)
186   "Returns the buffer name associated with the contents of a variable."
187   (let ((buf (gnus-get-buffer-create (gnus-window-to-buffer-helper
188                                  (cdr 
189                                   (assq variable gnus-window-to-buffer))))))
190     (and buf
191          (buffer-name buf))))
192
193 (defun gnus-picons-buffer-name ()
194   (cond ((or (stringp gnus-picons-display-where)
195              (bufferp gnus-picons-display-where))
196          gnus-picons-display-where)
197         ((eq gnus-picons-display-where 'picons)
198          (if gnus-single-article-buffer
199              "*Picons*"
200            (concat "*Picons " gnus-newsgroup-name "*")))
201         (t
202          (gnus-get-buffer-name gnus-picons-display-where))))
203
204 (defun gnus-picons-kill-buffer ()
205   (let ((buf (get-buffer (gnus-picons-buffer-name))))
206     (if (buffer-live-p buf)
207         (kill-buffer buf))))
208
209 (defun gnus-picons-setup-buffer ()
210   (let ((name (gnus-picons-buffer-name)))
211     (save-excursion
212       (if (get-buffer name)
213           (set-buffer name)
214         (set-buffer (gnus-get-buffer-create name))
215         (buffer-disable-undo)
216         (setq buffer-read-only t)
217         (add-hook 'gnus-summary-prepare-exit-hook 'gnus-picons-kill-buffer))
218       (current-buffer))))
219
220 (defun gnus-picons-set-buffer ()
221   (set-buffer (gnus-picons-setup-buffer))
222   (goto-char (point-min))
223   (if (and (eq gnus-picons-display-where 'article)
224            gnus-picons-display-article-move-p)
225       (if (search-forward "\n\n" nil t)
226           (forward-line -1)
227         (goto-char (point-max)))
228     (setq buffer-read-only t)
229     (unless gnus-picons-has-modeline-p
230       (set-specifier has-modeline-p
231                      (list (list (current-buffer)
232                                  (cons nil gnus-picons-has-modeline-p)))))))
233
234 (defun gnus-picons-prepare-for-annotations ()
235   "Prepare picons buffer for putting annotations."
236   ;; let drawing catch up
237   (when gnus-picons-refresh-before-display
238     (sit-for 0))
239   (gnus-picons-set-buffer)
240   (gnus-picons-remove-all))
241
242 (defun gnus-picons-make-annotation (&rest args)
243   (let ((annot (apply 'make-annotation args)))
244     (set-extent-property annot 'gnus-picon t)
245     (set-extent-property annot 'duplicable t)
246     annot))
247
248 (defun gnus-picons-article-display-x-face ()
249   "Display the x-face header bitmap in the 'gnus-picons-display-where buffer."
250   (let ((gnus-article-x-face-command 'gnus-picons-display-x-face))
251     (gnus-article-display-x-face)))
252
253 (defun gnus-picons-x-face-sentinel (process event)
254   (when (memq process gnus-picons-processes-alist)
255     (setq gnus-picons-processes-alist
256           (delq process gnus-picons-processes-alist))
257     (gnus-picons-set-buffer)
258     (gnus-picons-make-annotation (make-glyph gnus-picons-x-face-file-name)
259                                  nil 'text)
260     (when (file-exists-p gnus-picons-x-face-file-name)
261       (delete-file gnus-picons-x-face-file-name))))
262
263 (defun gnus-picons-display-x-face (beg end)
264   "Function to display the x-face header in the picons window.
265 To use:  (setq gnus-article-x-face-command 'gnus-picons-display-x-face)"
266   (interactive)
267   (if (featurep 'xface)
268       ;; Use builtin support
269       (save-excursion
270         ;; Don't remove this binding, it is really needed: when
271         ;; `gnus-picons-set-buffer' changes buffer (like when it is
272         ;; set to display picons outside of the article buffer), BEG
273         ;; and END still refer the buffer current now !
274         (let ((buf (current-buffer)))
275           (gnus-picons-set-buffer)
276           (gnus-picons-make-annotation
277            (vector 'xface
278                    :data (concat "X-Face: " (buffer-substring beg end buf)))
279            nil 'text nil nil nil t)))
280     ;; convert the x-face header to a .xbm file
281     (let* ((process-connection-type nil)
282            (process (start-process-shell-command "gnus-x-face" nil 
283                                                  gnus-picons-convert-x-face)))
284       (push process gnus-picons-processes-alist)
285       (process-kill-without-query process)
286       (set-process-sentinel process 'gnus-picons-x-face-sentinel)
287       (process-send-region process beg end)
288       (process-send-eof process))))
289
290 (defun gnus-article-display-picons ()
291   "Display faces for an author and her domain in gnus-picons-display-where."
292   (interactive)
293   (let (from at-idx)
294     (when (and (featurep 'xpm)
295                (or (not (fboundp 'device-type)) (equal (device-type) 'x))
296                (setq from (mail-fetch-field "from"))
297                (setq from (downcase (or (cadr
298                                          (funcall gnus-extract-address-components
299                                                   from))
300                                         "")))
301                (or (setq at-idx (string-match "@" from))
302                    (setq at-idx (length from))))
303       (save-excursion
304         (let ((username (downcase (substring from 0 at-idx)))
305               (addrs (if (eq at-idx (length from))
306                          (if gnus-local-domain
307                              (message-tokenize-header gnus-local-domain "."))
308                        (message-tokenize-header (substring from (1+ at-idx))
309                                                 "."))))
310           (gnus-picons-prepare-for-annotations)
311           (gnus-group-display-picons)
312           (unless gnus-picons-display-article-move-p
313             (save-restriction
314               (let ((buffer-read-only nil))
315                 (when (re-search-forward "^From: " nil t)
316                   (narrow-to-region (point) (gnus-point-at-eol))
317                   (when (search-forward from nil t)
318                     (gnus-put-text-property
319                      (match-beginning 0) (match-end 0)
320                      'invisible t))))))
321           (if (null gnus-picons-piconsearch-url)
322               (progn
323                 (gnus-picons-display-pairs (gnus-picons-lookup-pairs
324                                             addrs
325                                             gnus-picons-domain-directories)
326                                            gnus-picons-display-as-address
327                                            "." t)
328                 (if (and gnus-picons-display-as-address addrs)
329                     (gnus-picons-make-annotation
330                      [string :data "@"] nil 'text nil nil nil t))
331                 (gnus-picons-display-picon-or-name
332                  (gnus-picons-lookup-user username addrs)
333                  username t))
334             (push (list 'gnus-article-annotations 'search username addrs
335                         gnus-picons-domain-directories t)
336                   gnus-picons-jobs-alist)
337             (gnus-picons-next-job)))))))
338
339 (defun gnus-group-display-picons ()
340   "Display icons for the group in the `gnus-picons-display-where' buffer."
341   (interactive)
342   (when (and (featurep 'xpm)
343              (or (not (fboundp 'device-type)) (equal (device-type) 'x))
344              (or (null gnus-picons-group-excluded-groups)
345                  (not (string-match gnus-picons-group-excluded-groups
346                                     gnus-newsgroup-name))))
347     (let* ((newsgroups (mail-fetch-field "newsgroups"))
348            (groups
349             (if (or gnus-picons-display-article-move-p
350                     (not newsgroups))(mail-fetch-field "newsgroups")
351                 (list (gnus-group-real-name gnus-newsgroup-name))
352               (split-string newsgroups ",")))
353            group)
354       (save-excursion
355         (gnus-picons-prepare-for-annotations)
356         (while (setq group (pop groups))
357           (unless gnus-picons-display-article-move-p
358             (save-restriction
359               (let ((buffer-read-only nil))
360                 (goto-char (point-min))
361                 (when (re-search-forward "^Newsgroups:" nil t)
362                   (narrow-to-region (point) (gnus-point-at-eol))
363                   (when (search-forward group nil t)
364                     (gnus-put-text-property
365                      (match-beginning 0) (match-end 0)
366                      'invisible t))))))
367           (if (null gnus-picons-piconsearch-url)
368               (gnus-picons-display-pairs
369                (gnus-picons-lookup-pairs
370                 (reverse (split-string group "\\."))
371                 gnus-picons-news-directories)
372                t ".")
373             (push (list 'gnus-group-annotations 'search nil
374                         (split-string group "\\.")
375                         (if (listp gnus-picons-news-directories)
376                             gnus-picons-news-directories
377                           (list gnus-picons-news-directories))
378                         nil)
379                   gnus-picons-jobs-alist)
380             (gnus-picons-next-job))
381
382           (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all))))))
383
384 (defun gnus-picons-lookup-internal (addrs dir)
385   (setq dir (expand-file-name dir gnus-picons-database))
386   (gnus-picons-try-face (dolist (part (reverse addrs) dir)
387                           (setq dir (expand-file-name part dir)))))
388
389 (defun gnus-picons-lookup (addrs dirs)
390   "Lookup the picon for ADDRS in databases DIRS.
391 Returns the picon filename or NIL if none found."
392   (let (result)
393     (while (and dirs (null result))
394       (setq result (gnus-picons-lookup-internal addrs (pop dirs))))
395     result))
396
397 (defun gnus-picons-lookup-user-internal (user domains)
398   (let ((dirs gnus-picons-user-directories)
399         domains-tmp dir picon)
400     (while (and dirs (null picon))
401       (setq domains-tmp domains
402             dir (pop dirs))
403       (while (and domains-tmp
404                   (null (setq picon (gnus-picons-lookup-internal
405                                      (cons user domains-tmp) dir))))
406         (pop domains-tmp))
407       ;; Also make a try in MISC subdir
408       (unless picon
409         (setq picon (gnus-picons-lookup-internal (list user "MISC") dir))))
410     picon))
411
412 (defun gnus-picons-lookup-user (user domains)
413   "Lookup the picon for USER at DOMAINS.
414 USER is a string containing a name.
415 DOMAINS is a list of strings from the fully qualified domain name."
416   (or (gnus-picons-lookup-user-internal user domains)
417       (gnus-picons-lookup-user-internal "unknown" domains)))
418
419 (defun gnus-picons-lookup-pairs (domains directories)
420   "Lookup picons for DOMAINS and all its parents in DIRECTORIES.
421 Returns a list of PAIRS whose CAR is the picon filename or NIL if
422 none, and whose CDR is the corresponding element of DOMAINS."
423   (let (picons)
424     (setq directories (if (listp directories)
425                           directories
426                         (list directories)))
427     (while domains
428       (push (list (gnus-picons-lookup (cons "unknown" domains) directories)
429                   (pop domains))
430             picons))
431     picons))
432
433 (defun gnus-picons-display-picon-or-name (picon name &optional right-p)
434   (cond (picon (gnus-picons-display-glyph picon name right-p))
435         (gnus-picons-display-as-address (list (gnus-picons-make-annotation
436                                                (vector 'string :data name)
437                                                nil 'text
438                                                nil nil nil right-p)))))
439
440 (defun gnus-picons-display-pairs (pairs &optional bar-p dot-p right-p)
441   "Display picons in list PAIRS."
442   (let ((domain-p (and gnus-picons-display-as-address dot-p))
443         pair picons)
444     (when (and bar-p domain-p right-p
445                gnus-picons-display-article-move-p)
446       (setq picons (gnus-picons-display-glyph
447                     (let ((gnus-picons-file-suffixes '("xbm")))
448                       (gnus-picons-try-face
449                        gnus-xmas-glyph-directory "bar."))
450                     nil right-p)))
451     (while (setq pair (pop pairs))
452       (setq picons (nconc picons
453                           (gnus-picons-display-picon-or-name
454                            (car pair) (cadr pair) right-p)
455                           (if (and domain-p pairs)
456                               (list (gnus-picons-make-annotation
457                                      (vector 'string :data dot-p)
458                                      nil 'text nil nil nil right-p))))))
459     picons))
460
461 (defun gnus-picons-try-face (dir &optional filebase)
462   (let* ((dir (file-name-as-directory dir))
463          (filebase (or filebase "face."))
464          (key (concat dir filebase))
465          (glyph (cdr (assoc key gnus-picons-glyph-alist)))
466          (suffixes gnus-picons-file-suffixes)
467          f suf)
468     (while (setq suf (pop suffixes))
469       (when (file-exists-p (setq f (expand-file-name
470                                     (concat filebase suf)
471                                     dir)))
472         (setq suffixes nil
473               glyph (make-glyph f))
474         (when (equal suf "xbm")
475           (set-glyph-face glyph 'gnus-picons-xbm-face))
476         (push (cons key glyph) gnus-picons-glyph-alist)))
477     glyph))
478
479 (defun gnus-picons-display-glyph (glyph &optional part rightp)
480   (set-glyph-baseline glyph 70)
481   (let ((new (gnus-picons-make-annotation
482               glyph (point) 'text nil nil nil rightp)))
483     (when (and part gnus-picons-display-as-address)
484       (set-annotation-data
485        new (cons new (make-glyph (vector 'string :data part))))
486       (set-annotation-action new 'gnus-picons-action-toggle))
487     (nconc
488      (list new)
489      (if (and (eq major-mode 'gnus-article-mode)
490               (not gnus-picons-display-as-address)
491               (not part))
492          (list (gnus-picons-make-annotation [string :data " "] (point)
493                                             'text nil nil nil rightp))))))
494
495 (defun gnus-picons-action-toggle (data)
496   "Toggle annotation."
497   (interactive "e")
498   (let* ((annot (car data))
499          (glyph (annotation-glyph annot)))
500     (set-annotation-glyph annot (cdr data))
501     (set-annotation-data annot (cons annot glyph))))
502
503 (defun gnus-picons-clear-cache ()
504   "Clear the picons cache."
505   (interactive)
506   (setq gnus-picons-glyph-alist nil
507         gnus-picons-url-alist nil))
508
509 (gnus-add-shutdown 'gnus-picons-close 'gnus)
510
511 (defun gnus-picons-close ()
512   "Shut down the picons."
513   (if gnus-picons-clear-cache-on-shutdown
514       (gnus-picons-clear-cache)))
515
516 ;;; Query a remote DB.  This requires some stuff from w3 !
517
518 (require 'url)
519 (require 'w3-forms)
520
521 (defun gnus-picons-url-retrieve (url fn arg)
522   (let ((old-asynch (default-value 'url-be-asynchronous))
523         (url-working-buffer (generate-new-buffer " *picons*"))
524         (url-package-name "Gnus")
525         (url-package-version gnus-version-number)
526         url-request-method)
527     (setq-default url-be-asynchronous t)
528     (save-excursion
529       (set-buffer url-working-buffer)
530       (setq url-be-asynchronous t
531             url-current-callback-data arg
532             url-current-callback-func fn)
533       (url-retrieve url t))
534     (setq-default url-be-asynchronous old-asynch)))
535
536 (defun gnus-picons-make-glyph (type)
537   "Make a TYPE glyph using current buffer as data.  Handles xbm nicely."
538   (cond ((null type) nil)
539         ((eq type 'xbm) (let ((fname (make-temp-name "/tmp/picon")))
540                           (write-region (point-min) (point-max) fname
541                                         nil 'quiet)
542                           (prog1 (make-glyph (vector 'xbm :file fname))
543                             (delete-file fname))))
544         (t (make-glyph (vector type :data (buffer-string))))))
545
546 ;;; Parsing of piconsearch result page.
547
548 ;; Assumes:
549 ;; 1 - each value field has the form: "<strong>key</strong> = <kbd>value</kbd>"
550 ;; 2 - a "<p>" separates the keywords from the results
551 ;; 3 - every results begins by the path within the database at the beginning
552 ;;     of the line in raw text.
553 ;; 3b - and the href following it is the preferred image type.
554
555 ;; if 1 or 2 is not met, it will probably cause an error.  The other
556 ;; will go undetected
557
558 (defun gnus-picons-parse-value (name)
559   (goto-char (point-min))
560   (re-search-forward (concat "<strong>"
561                              (regexp-quote name)
562                              "</strong> *= *<kbd> *\\([^ <][^<]*\\) *</kbd>"))
563   (buffer-substring (match-beginning 1) (match-end 1)))
564
565 (defun gnus-picons-parse-filenames ()
566   ;; returns an alist of ((USER ADDRS DB) . URL)
567   (let* ((case-fold-search t)
568          (user (gnus-picons-parse-value "user"))
569          (host (gnus-picons-parse-value "host"))
570          (dbs (message-tokenize-header (gnus-picons-parse-value "db") " "))
571          (start-re
572           (concat
573            ;; dbs
574            "^\\(" (mapconcat 'identity dbs "\\|") "\\)/"
575            ;; host
576            "\\(\\(" (replace-in-string host "\\." "/\\|" t) "/\\|MISC/\\)*\\)"
577            ;; user
578            "\\(" (regexp-quote user) "\\|unknown\\)/"
579            "face\\."))
580          cur-db cur-host cur-user types res)
581     ;; now point will be somewhere in the header.  Find beginning of
582     ;; entries
583     (re-search-forward "<p>[ \t\n]*")
584     (while (re-search-forward start-re nil t)
585       (setq cur-db (buffer-substring (match-beginning 1) (match-end 1))
586             cur-host (buffer-substring (match-beginning 2) (match-end 2))
587             cur-user (buffer-substring (match-beginning 4) (match-end 4))
588             cur-host (nreverse (message-tokenize-header cur-host "/")))
589       ;; XXX - KLUDGE: there is a blank picon in news/MISC/unknown
590       (unless (and (string-equal cur-db "news")
591                    (string-equal cur-user "unknown")
592                    (equal cur-host '("MISC")))
593         ;; ok now we have found an entry (USER HOST DB), find the
594         ;; corresponding picon URL
595         (save-restriction
596           ;; restrict region to this entry
597           (narrow-to-region (point) (search-forward "<br>"))
598           (goto-char (point-min))
599           (setq types gnus-picons-file-suffixes)
600           (while (and types
601                       (not (re-search-forward
602                             (concat "<a[ \t\n]+href=\"\\([^\"]*\\."
603                                     (regexp-quote (car types)) "\\)\"")
604                             nil t)))
605             (pop types))
606           (push (cons (list cur-user cur-host cur-db)
607                       (buffer-substring (match-beginning 1) (match-end 1)))
608                 res))))
609     (nreverse res)))
610
611 ;;; picon network display functions :
612
613 (defun gnus-picons-network-display-internal (sym-ann glyph part right-p)
614   (gnus-picons-set-buffer)
615   (gnus-picons-display-picon-or-name glyph part right-p)
616   (gnus-picons-next-job-internal))
617
618 (defun gnus-picons-network-display-callback (url part sym-ann right-p)
619   (let ((glyph (gnus-picons-make-glyph (cdr (assoc url-current-mime-type
620                                                    w3-image-mappings)))))
621     (kill-buffer (current-buffer))
622     (push (cons url glyph) gnus-picons-glyph-alist)
623     ;; only do the job if it has not been preempted.
624     (if (equal gnus-picons-job-already-running
625                (list sym-ann 'picon url part right-p))
626         (gnus-picons-network-display-internal sym-ann glyph part right-p)
627       (gnus-picons-next-job-internal))))
628
629 (defun gnus-picons-network-display (url part sym-ann right-p)
630   (let ((cache (assoc url gnus-picons-glyph-alist)))
631     (if (or cache (null url))
632         (gnus-picons-network-display-internal sym-ann (cdr cache) part right-p)
633       (gnus-picons-url-retrieve url 'gnus-picons-network-display-callback
634                                 (list url part sym-ann right-p)))))
635
636 ;;; search job functions
637
638 (defun gnus-picons-network-search-internal (user addrs dbs sym-ann right-p
639                                                  &optional fnames)
640   (let (curkey dom pfx url dbs-tmp cache new-jobs)
641     ;; First do the domain search
642     (dolist (part (if right-p
643                       (reverse addrs)
644                     addrs))
645       (setq pfx (nconc (list part) pfx)
646             dom (cond ((and dom right-p) (concat part "." dom))
647                       (dom (concat dom "." part))
648                       (t part))
649             curkey (list "unknown" dom dbs))
650       (when (null (setq cache (assoc curkey gnus-picons-url-alist)))
651         ;; This one is not yet in the cache, create a new entry
652         ;; Search for an entry
653         (setq dbs-tmp dbs
654               url nil)
655         (while (and dbs-tmp (null url))
656           (setq url (or (cdr (assoc (list "unknown" pfx (car dbs-tmp)) fnames))
657                         (and (eq dom part)
658                              ;; This is the first component.  Try the
659                              ;; catch-all MISC component
660                              (cdr (assoc (list "unknown"
661                                                '("MISC")
662                                                (car dbs-tmp))
663                                          fnames)))))
664           (pop dbs-tmp))
665         (push (setq cache (cons curkey url)) gnus-picons-url-alist))
666       ;; Put this glyph in the job list
667       (if (and (not (eq dom part)) gnus-picons-display-as-address)
668           (push (list sym-ann "." right-p) new-jobs))
669       (push (list sym-ann 'picon (cdr cache) part right-p) new-jobs))
670     ;; next, the user search
671     (when user
672       (setq curkey (list user dom gnus-picons-user-directories))
673       (if (null (setq cache (assoc curkey gnus-picons-url-alist)))
674           (let ((users (list user "unknown"))
675                 dirs usr domains-tmp dir picon)
676             (while (and users (null picon))
677               (setq dirs gnus-picons-user-directories
678                     usr (pop users))
679               (while (and dirs (null picon))
680                 (setq domains-tmp addrs
681                       dir (pop dirs))
682                 (while (and domains-tmp
683                             (null (setq picon (assoc (list usr domains-tmp dir)
684                                                      fnames))))
685                   (pop domains-tmp))
686                 (unless picon
687                   (setq picon (assoc (list usr '("MISC") dir) fnames)))))
688             (push (setq cache (cons curkey (cdr picon)))
689                   gnus-picons-url-alist)))
690       (if (and gnus-picons-display-as-address new-jobs)
691           (push (list sym-ann "@" right-p) new-jobs))
692       (push (list sym-ann 'picon (cdr cache) user right-p) new-jobs))
693     (if (and gnus-picons-display-as-address (not right-p))
694         (push (list sym-ann 'bar right-p) new-jobs))
695     ;; only put the jobs in the queue if this job has not been preempted.
696     (if (equal gnus-picons-job-already-running
697                (list sym-ann 'search user addrs dbs right-p))
698         (setq gnus-picons-jobs-alist
699               (nconc (if (and gnus-picons-display-as-address right-p)
700                          (list (list sym-ann 'bar right-p)))
701                      (nreverse new-jobs)
702                      gnus-picons-jobs-alist)))
703     (gnus-picons-next-job-internal)))
704
705 (defun gnus-picons-network-search-callback (user addrs dbs sym-ann right-p)
706   (gnus-picons-network-search-internal user addrs dbs sym-ann right-p
707                                        (prog1 (gnus-picons-parse-filenames)
708                                          (kill-buffer (current-buffer)))))
709
710 ;; Initiate a query on the picon database
711 (defun gnus-picons-network-search (user addrs dbs sym-ann right-p)
712   (let* ((host (mapconcat 'identity addrs "."))
713          (key (list (or user "unknown") host (if user
714                                                   gnus-picons-user-directories
715                                                 dbs)))
716          (cache (assoc key gnus-picons-url-alist)))
717     (if (null cache)
718         (gnus-picons-url-retrieve
719          (concat gnus-picons-piconsearch-url
720                  "?user=" (w3-form-encode-xwfu (or user "unknown"))
721                  "&host=" (w3-form-encode-xwfu host)
722                  "&db=" (mapconcat 'w3-form-encode-xwfu
723                                    (if user
724                                        (append dbs
725                                                gnus-picons-user-directories)
726                                      dbs)
727                                    "+"))
728          'gnus-picons-network-search-callback
729          (list user addrs dbs sym-ann right-p))
730       (gnus-picons-network-search-internal user addrs dbs sym-ann right-p))))
731
732 ;;; Main jobs dispatcher function
733
734 (defun gnus-picons-next-job-internal ()
735   (if (setq gnus-picons-job-already-running (pop gnus-picons-jobs-alist))
736       (let* ((job gnus-picons-job-already-running)
737              (sym-ann (pop job))
738              (tag (pop job)))
739         (if tag
740             (cond ((stringp tag);; (SYM-ANN "..." RIGHT-P)
741                    (gnus-picons-network-display-internal sym-ann nil tag
742                                                          (pop job)))
743                   ((and (eq 'bar tag)
744                         gnus-picons-display-article-move-p)
745                    (gnus-picons-network-display-internal
746                     sym-ann
747                     (let ((gnus-picons-file-suffixes '("xbm")))
748                       (gnus-picons-try-face
749                        gnus-xmas-glyph-directory "bar."))
750                     nil (pop job)))
751                   ((eq 'search tag);; (SYM-ANN 'search USER ADDRS DBS RIGHT-P)
752                    (gnus-picons-network-search
753                     (pop job) (pop job) (pop job) sym-ann (pop job)))
754                   ((eq 'picon tag);; (SYM-ANN 'picon URL PART RIGHT-P)
755                    (gnus-picons-network-display
756                     (pop job) (pop job) sym-ann (pop job)))
757                   (t (setq gnus-picons-job-already-running nil)
758                      (error "Unknown picon job tag %s" tag)))))))
759
760 (defun gnus-picons-next-job ()
761   "Start processing the job queue if it is not in progress."
762   (unless gnus-picons-job-already-running
763     (gnus-picons-next-job-internal)))
764
765 (provide 'gnus-picon)
766
767 ;;; gnus-picon.el ends here