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