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