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