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