(open-protocol-stream): All starttls connections are handled by the network handler.
[gnus] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news xpm annotation glyph faces
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; There are three picon types relevant to Gnus:
27 ;;
28 ;; Persons: person@subdomain.dom
29 ;;          users/dom/subdomain/person/face.gif
30 ;;          usenix/dom/subdomain/person/face.gif
31 ;;          misc/MISC/person/face.gif
32 ;; Domains: subdomain.dom
33 ;;          domain/dom/subdomain/unknown/face.gif
34 ;; Groups:  comp.lang.lisp
35 ;;          news/comp/lang/lisp/unknown/face.gif
36 ;;
37 ;; Original implementation by Wes Hardaker <hardaker@ece.ucdavis.edu>.
38 ;;
39 ;;; Code:
40
41 ;; For Emacs <22.2 and XEmacs.
42 (eval-and-compile
43   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
44
45 (eval-when-compile (require 'cl))
46
47 (require 'gnus)
48 (require 'gnus-art)
49
50 ;;; User variables:
51
52 (defcustom gnus-picon-news-directories '("news")
53   "*List of directories to search for newsgroups faces."
54   :type '(repeat string)
55   :group 'gnus-picon)
56
57 (defcustom gnus-picon-user-directories '("users" "usenix" "local" "misc")
58   "*List of directories to search for user faces."
59   :type '(repeat string)
60   :group 'gnus-picon)
61
62 (defcustom gnus-picon-domain-directories '("domains")
63   "*List of directories to search for domain faces.
64 Some people may want to add \"unknown\" to this list."
65   :type '(repeat string)
66   :group 'gnus-picon)
67
68 (defcustom gnus-picon-file-types
69   (let ((types (list "xbm")))
70     (when (gnus-image-type-available-p 'gif)
71       (push "gif" types))
72     (when (gnus-image-type-available-p 'xpm)
73       (push "xpm" types))
74     types)
75   "*List of suffixes on picon file names to try."
76   :type '(repeat string)
77   :group 'gnus-picon)
78
79 (defcustom gnus-picon-style 'inline
80   "How should picons be displayed.
81 If `inline', the textual representation is replaced.  If `right', picons are
82 added right to the textual representation."
83   ;; FIXME: `right' needs improvement for XEmacs.
84   :type '(choice (const inline)
85                  (const right))
86   :group 'gnus-picon)
87
88 (defcustom gnus-picon-inhibit-top-level-domains t
89   "If non-nil, don't piconify top-level domains.
90 These are often not very interesting."
91   :type 'boolean
92   :group 'gnus-picon)
93
94 ;;; Internal variables:
95
96 (defvar gnus-picon-glyph-alist nil
97   "Picon glyphs cache.
98 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
99 (defvar gnus-picon-cache nil)
100
101 ;;; Functions:
102
103 (defsubst gnus-picon-split-address (address)
104   (setq address (split-string address "@"))
105   (if (stringp (cadr address))
106       (cons (car address) (split-string (cadr address) "\\."))
107     (if (stringp (car address))
108         (split-string (car address) "\\."))))
109
110 (defun gnus-picon-find-face (address directories &optional exact)
111   (let* ((address (gnus-picon-split-address address))
112          (user (pop address))
113          (faddress address)
114          database directory result instance base)
115     (catch 'found
116       (dolist (database gnus-picon-databases)
117         (dolist (directory directories)
118           (setq address faddress
119                 base (expand-file-name directory database))
120           (while address
121             (when (setq result (gnus-picon-find-image
122                                 (concat base "/" (mapconcat 'downcase
123                                                             (reverse address)
124                                                             "/")
125                                         "/" (downcase user) "/")))
126               (throw 'found result))
127             (if exact
128                 (setq address nil)
129               (pop address)))
130           ;; Kludge to search MISC as well.  But not in "news".
131           (unless (string= directory "news")
132             (when (setq result (gnus-picon-find-image
133                                 (concat base "/MISC/" user "/")))
134               (throw 'found result))))))))
135
136 (defun gnus-picon-find-image (directory)
137   (let ((types gnus-picon-file-types)
138         found type file)
139     (while (and (not found)
140                 (setq type (pop types)))
141       (setq found (file-exists-p (setq file (concat directory "face." type)))))
142     (if found
143         file
144       nil)))
145
146 (defun gnus-picon-insert-glyph (glyph category &optional nostring)
147   "Insert GLYPH into the buffer.
148 GLYPH can be either a glyph or a string.  When NOSTRING, no textual
149 replacement is added."
150   ;; Using NOSTRING prevents wrong BBDB entries with `gnus-picon-style' set to
151   ;; 'right.
152   (if (stringp glyph)
153       (insert glyph)
154     (gnus-add-wash-type category)
155     (gnus-add-image category (car glyph))
156     (gnus-put-image (car glyph) (unless nostring (cdr glyph)) category)))
157
158 (defun gnus-picon-create-glyph (file)
159   (or (cdr (assoc file gnus-picon-glyph-alist))
160       (cdar (push (cons file (gnus-create-image
161                               file nil nil
162                               :color-symbols '(("None" . "white"))))
163                   gnus-picon-glyph-alist))))
164
165 ;;; Functions that does picon transformations:
166
167 (declare-function image-size "image.c" (spec &optional pixels frame))
168
169 (defun gnus-picon-transform-address (header category)
170   (gnus-with-article-headers
171    (let ((addresses
172           (mail-header-parse-addresses
173            ;; mail-header-parse-addresses does not work (reliably) on
174            ;; decoded headers.
175            (or
176             (ignore-errors