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