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