Don't download pictures the user hasn't requested.
[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 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
32   "Where Gnus will cache images it downloads from the web."
33   :group 'gnus-art
34   :type 'directory)
35
36 (defcustom gnus-html-cache-size 500000000
37   "The size of the Gnus image cache."
38   :group 'gnus-art
39   :type 'integer)
40
41 ;;;###autoload
42 (defun gnus-article-html (handle)
43   (let ((article-buffer (current-buffer)))
44     (save-restriction
45       (narrow-to-region (1- (point)) (point))
46       (save-excursion
47         (set-buffer (car handle))
48         (call-process-region (point-min) (point-max)
49                              "w3m" 
50                              nil article-buffer nil
51                              "-halfdump"
52                              "-T" "text/html"))
53       (gnus-html-wash-tags))))
54
55 (defun gnus-html-wash-tags ()
56   (let (tag parameters string start end images)
57     ;;(subst-char-in-region (point-min) (point-max) ?_ ? )
58     (goto-char (point-min))
59     (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
60       (setq tag (match-string 1)
61             parameters (match-string 2)
62             start (match-beginning 0))
63       (delete-region start (point))
64       (when (search-forward (concat "</" tag ">"))
65         (delete-region (match-beginning 0) (match-end 0)))
66       (setq end (point))
67       (cond
68        ;; Fetch and insert a picture.
69        ((equal tag "img_alt")
70         (when (string-match "src=\"\\([^\"]+\\)" parameters)
71           (setq parameters (match-string 1 parameters))
72           (when (or (null mm-w3m-safe-url-regexp)
73                     (string-match mm-w3m-safe-url-regexp parameters))
74             (let ((file (gnus-html-image-id parameters)))
75               (if (file-exists-p file)
76                   ;; It's already cached, so just insert it.
77                   (progn
78                     (put-image (create-image file) (point))
79                     ;; Delete the ALT text.
80                     (delete-region start end))
81                 ;; We don't have it, so schedule it for fetching
82                 ;; asynchronously.
83                 (push (list parameters
84                             (set-marker (make-marker) start)
85                             (point-marker))
86                       images))))))
87        ;; Add a link.
88        ((equal tag "a")
89         (when (string-match "href=\"\\([^\"]+\\)" parameters)
90           (setq parameters (match-string 1 parameters))
91           (gnus-article-add-button start end
92                                    'browse-url parameters)
93           (let ((overlay (gnus-make-overlay start end)))
94             (gnus-overlay-put overlay 'evaporate t)
95             (gnus-overlay-put overlay 'gnus-button-url parameters)
96             (when gnus-article-mouse-face
97               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
98        ;; Whatever.  Just ignore the tag.
99        (t
100         ))
101       (goto-char start))
102     (when images
103       (gnus-html-schedule-image-fetching (current-buffer) images))))
104
105 (defun gnus-html-schedule-image-fetching (buffer images)
106   (let* ((url (caar images))
107          (process (start-process
108                    "images" nil "curl"
109                    "-s" "--create-dirs"
110                    "-o" (gnus-html-image-id url)
111                    url)))
112     (set-process-sentinel process 'gnus-html-curl-sentinel)
113     (set-process-plist process (list 'images images
114                                      'buffer buffer))))
115
116 (defun gnus-html-image-id (url)
117   (expand-file-name (sha1 url) gnus-html-cache-directory))
118
119 (defun gnus-html-curl-sentinel (process event)
120   (when (string-match "finished" event)
121     (let* ((images (getf (process-plist process) 'images))
122            (buffer (getf (process-plist process) 'buffer))
123            (spec (pop images))
124            (file (gnus-html-image-id (car spec))))
125       (when (file-exists-p file)
126         (save-excursion
127           (set-buffer buffer)
128           (let ((buffer-read-only nil))
129             (delete-region (cadr spec) (caddr spec))
130             (put-image (create-image file) (cadr spec)))))
131       (when images
132         (gnus-html-schedule-image-fetching buffer images)))))
133
134 (defun gnus-html-prune-cache ()
135   (let ((total-size 0)
136         files)
137     (dolist (file (directory-files gnus-html-cache-directory t nil t))
138       (let ((attributes (file-attributes file)))
139         (unless (nth 0 attributes)
140           (incf total-size (nth 7 attributes))
141           (push (list (time-to-seconds (nth 5 attributes))
142                       (nth 7 attributes) file)
143                 files))))
144     (when (> total-size gnus-html-cache-size)
145       (setq files (sort files (lambda (f1 f2)
146                                 (< (car f1) (car f2)))))
147       (dolist (file files)
148         (when (> total-size gnus-html-cache-size)
149           (decf total-size (cadr file))
150           (delete-file (nth 2 file)))))))
151
152 ;;; gnus-html.el ends here