gnus-html.el, message.el: Add custom version to new variables; gnus-sum.el: Bump...
[gnus] / lisp / gnus-html.el
1 ;;; gnus-html.el --- Quoted-Printable functions
2
3 ;; Copyright (C) 2010  Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html, web
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; The idea is to provide a simple, fast and pretty minimal way to
26 ;; render HTML (including links and images) in a buffer, based on an
27 ;; external HTML renderer (i.e., w3m).
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'mm-decode))
33 (require 'mm-url)
34
35 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
36   "Where Gnus will cache images it downloads from the web."
37   :version "24.1"
38   :group 'gnus-art
39   :type 'directory)
40
41 (defcustom gnus-html-cache-size 500000000
42   "The size of the Gnus image cache."
43   :version "24.1"
44   :group 'gnus-art
45   :type 'integer)
46
47 (defcustom gnus-html-frame-width 70
48   "What width to use when rendering HTML."
49   :version "24.1"
50   :group 'gnus-art
51   :type 'integer)
52
53 (defcustom gnus-blocked-images "."
54   "Images that have URLs matching this regexp will be blocked."
55   :version "24.1"
56   :group 'gnus-art
57   :type 'regexp)
58
59 ;;;###autoload
60 (defun gnus-article-html (handle)
61   (let ((article-buffer (current-buffer)))
62     (save-restriction
63       (narrow-to-region (point) (point))
64       (save-excursion
65         (mm-with-part handle
66           (let* ((coding-system-for-read 'utf-8)
67                  (coding-system-for-write 'utf-8)
68                  (default-process-coding-system
69                    (cons coding-system-for-read coding-system-for-write)))
70             (call-process-region (point-min) (point-max)
71                                  "w3m" 
72                                  nil article-buffer nil
73                                  "-halfdump"
74                                  "-no-cookie"
75                                  "-I" "UTF-8"
76                                  "-O" "UTF-8"
77                                  "-o" "ext_halfdump=1"
78                                  "-o" "pre_conv=1"
79                                  "-t" (format "%s" tab-width)
80                                  "-cols" (format "%s" gnus-html-frame-width)
81                                  "-o" "display_image=off"
82                                  "-T" "text/html"))))
83       (gnus-html-wash-tags))))
84
85 (defvar gnus-article-mouse-face)
86
87 (defun gnus-html-wash-tags ()
88   (let (tag parameters string start end images url)
89     (mm-url-decode-entities)
90     (goto-char (point-min))
91     (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
92       (setq tag (match-string 1)
93             parameters (match-string 2)
94             start (match-beginning 0))
95       (when (plusp (length parameters))
96         (set-text-properties 0 (1- (length parameters)) nil parameters))
97       (delete-region start (point))
98       (when (search-forward (concat "</" tag ">") nil t)
99         (delete-region (match-beginning 0) (match-end 0)))
100       (setq end (point))
101       (cond
102        ;; Fetch and insert a picture.
103        ((equal tag "img_alt")
104         (when (string-match "src=\"\\([^\"]+\\)" parameters)
105           (setq url (match-string 1 parameters))
106           (if (string-match "^cid:\\(.*\\)" url)
107               ;; URLs with cid: have their content stashed in other
108               ;; parts of the MIME structure, so just insert them
109               ;; immediately.
110               (let ((handle (mm-get-content-id
111                              (setq url (match-string 1 url))))
112                     image)
113                 (when handle
114                   (mm-with-part handle
115                     (setq image (gnus-create-image (buffer-string)
116                                                    nil t))))
117                 (when image
118                   (delete-region start end)
119                   (gnus-put-image image)))
120             ;; Normal, external URL.
121             (when (or (null gnus-blocked-images)
122                       (not (string-match gnus-blocked-images url)))
123               (let ((file (gnus-html-image-id url)))
124                 (if (file-exists-p file)
125                     ;; It's already cached, so just insert it.
126                     (when (gnus-html-put-image file (point))
127                       ;; Delete the ALT text.
128                       (delete-region start end))
129                   ;; We don't have it, so schedule it for fetching
130                   ;; asynchronously.
131                   (push (list url
132                               (set-marker (make-marker) start)
133                               (point-marker))
134                         images)))))))
135        ;; Add a link.
136        ((equal tag "a")
137         (when (string-match "href=\"\\([^\"]+\\)" parameters)
138           (setq url (match-string 1 parameters))
139           (gnus-article-add-button start end
140                                    'browse-url url
141                                    url)
142           (let ((overlay (gnus-make-overlay start end)))
143             (gnus-overlay-put overlay 'evaporate t)
144             (gnus-overlay-put overlay 'gnus-button-url url)
145             (when gnus-article-mouse-face
146               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
147        ;; Whatever.  Just ignore the tag.
148        (t
149         ))
150       (goto-char start))
151     (goto-char (point-min))
152     ;; The output from -halfdump isn't totally regular, so strip
153     ;; off any </pre_int>s that were left over.
154     (while (re-search-forward "</pre_int>" nil t)
155       (replace-match "" t t))
156     (when images
157       (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
158
159 (defun gnus-html-schedule-image-fetching (buffer images)
160   (let* ((url (caar images))
161          (process (start-process
162                    "images" nil "curl"
163                    "-s" "--create-dirs"
164                    "--location"
165                    "--max-time" "60"
166                    "-o" (gnus-html-image-id url)
167                    url)))
168     (process-kill-without-query process)
169     (set-process-sentinel process 'gnus-html-curl-sentinel)
170     (gnus-set-process-plist process (list 'images images
171                                           'buffer buffer))))
172
173 (defun gnus-html-image-id (url)
174   (expand-file-name (sha1 url) gnus-html-cache-directory))
175
176 (defun gnus-html-curl-sentinel (process event)
177   (when (string-match "finished" event)
178     (let* ((images (gnus-process-get process 'images))
179            (buffer (gnus-process-get process 'buffer))
180            (spec (pop images))
181            (file (gnus-html-image-id (car spec))))
182       (when (and (buffer-live-p buffer)
183                  ;; If the position of the marker is 1, then that
184                  ;; means that the text it was in has been deleted;
185                  ;; i.e., that the user has selected a different
186                  ;; article before the image arrived.
187                  (not (= (marker-position (cadr spec)) (point-min))))
188         (with-current-buffer buffer
189           (let ((inhibit-read-only t))
190             (when (gnus-html-put-image file (cadr spec))
191               (delete-region (1+ (cadr spec)) (caddr spec))))))
192       (when images
193         (gnus-html-schedule-image-fetching buffer images)))))
194
195 (defun gnus-html-put-image (file point)
196   (when (display-graphic-p)
197     (let ((image (ignore-errors
198                    (gnus-create-image file))))
199       (save-excursion
200         (goto-char point)
201         (if (and image
202                  ;; Kludge to avoid displaying 30x30 gif images, which
203                  ;; seems to be a signal of a broken image.
204                  (not (and (listp image)
205                            (eq (plist-get (cdr image) :type) 'gif)
206                            (= (car (image-size image t)) 30)
207                            (= (cdr (image-size image t)) 30))))
208             (progn
209               (gnus-put-image image)
210               t)
211           (when (fboundp 'find-image)
212             (gnus-put-image (find-image
213                              '((:type xpm :file "lock-broken.xpm")))))
214           nil)))))
215
216 (defun gnus-html-prune-cache ()
217   (let ((total-size 0)
218         files)
219     (dolist (file (directory-files gnus-html-cache-directory t nil t))
220       (let ((attributes (file-attributes file)))
221         (unless (nth 0 attributes)
222           (incf total-size (nth 7 attributes))
223           (push (list (time-to-seconds (nth 5 attributes))
224                       (nth 7 attributes) file)
225                 files))))
226     (when (> total-size gnus-html-cache-size)
227       (setq files (sort files (lambda (f1 f2)
228                                 (< (car f1) (car f2)))))
229       (dolist (file files)
230         (when (> total-size gnus-html-cache-size)
231           (decf total-size (cadr file))
232           (delete-file (nth 2 file)))))))
233
234 ;;;###autoload
235 (defun gnus-html-prefetch-images (summary)
236   (let (blocked-images urls)
237     (when (buffer-live-p summary)
238       (with-current-buffer summary
239         (setq blocked-images gnus-blocked-images))
240       (save-match-data
241         (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
242           (let ((url (match-string 1)))
243             (when (or (null blocked-images)
244                       (not (string-match blocked-images url)))
245               (unless (file-exists-p (gnus-html-image-id url))
246                 (push url urls)
247                 (push (gnus-html-image-id url) urls)
248                 (push "-o" urls)))))
249         (let ((process
250                (apply 'start-process 
251                       "images" nil "curl"
252                       "-s" "--create-dirs"
253                       "--location"
254                       "--max-time" "60"
255                       urls)))
256           (process-kill-without-query process))))))
257
258 (provide 'gnus-html)
259
260 ;;; gnus-html.el ends here