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