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