nnweb.el (nnweb-google-parse-1): Fix minor Y10k bug
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2
3 ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30
31 (eval-when-compile
32   (require 'cl))
33
34 (require 'mm-util)
35 (require 'gnus-ems)
36 (require 'gnus-util)
37 (require 'gnus)
38
39 (defvar gnus-face-properties-alist)
40
41 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
42   "*Directory where X-Face PBM files are stored."
43   :version "22.1"
44   :group 'gnus-fun
45   :type 'directory)
46
47 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
48   "Command for converting a PBM to an X-Face."
49   :version "22.1"
50   :group 'gnus-fun
51   :type 'string)
52
53 (defcustom gnus-convert-image-to-x-face-command
54   "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
55   "Command for converting an image to an X-Face.
56 The command must take a image filename (use \"%s\") as input.
57 The output must be the X-Face header data on stdout."
58   :version "22.1"
59   :group 'gnus-fun
60   :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
61                         "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
62                  (const :tag "convert"
63                         "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
64                  (string)))
65
66 (defcustom gnus-convert-image-to-face-command
67   "convert -scale 48x48! %s -colors %d png:-"
68   "Command for converting an image to a Face.
69
70 The command must take an image filename (first format argument
71 \"%s\") and the number of colors (second format argument: \"%d\")
72 as input.  The output must be the Face header data on stdout in
73 PNG format."
74   :version "22.1"
75   :group 'gnus-fun
76   :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
77                         "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
78                  (const :tag "convert"
79                         "convert -scale 48x48! %s -colors %d png:-")
80                  (string)))
81
82 (defun gnus-shell-command-to-string (command)
83   "Like `shell-command-to-string' except not mingling ERROR."
84   (with-output-to-string
85     (call-process shell-file-name nil (list standard-output nil)
86                   nil shell-command-switch command)))
87
88 (defun gnus-shell-command-on-region (start end command)
89   "A simplified `shell-command-on-region'.
90 Output to the current buffer, replace text, and don't mingle error."
91   (call-process-region start end shell-file-name t
92                        (list (current-buffer) nil)
93                        nil shell-command-switch command))
94
95 ;;;###autoload
96 (defun gnus-random-x-face ()
97   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
98   (interactive)
99   (when (file-exists-p gnus-x-face-directory)
100     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
101            (file (nth (random (length files)) files)))
102       (when file
103         (gnus-shell-command-to-string
104          (format gnus-convert-pbm-to-x-face-command
105                  (shell-quote-argument file)))))))
106
107 (autoload 'message-goto-eoh "message" nil t)
108
109 ;;;###autoload
110 (defun gnus-insert-random-x-face-header ()
111   "Insert a random X-Face header from `gnus-x-face-directory'."
112   (interactive)
113   (let ((data (gnus-random-x-face)))
114     (save-excursion
115       (message-goto-eoh)
116       (if data
117           (insert "X-Face: " data)
118         (message
119          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
120          gnus-x-face-directory)))))
121
122 ;;;###autoload
123 (defun gnus-x-face-from-file (file)
124   "Insert an X-Face header based on an image file.
125
126 Depending on `gnus-convert-image-to-x-face-command' it may accept
127 different input formats."
128   (interactive "fImage file name: ")
129   (when (file-exists-p file)
130     (gnus-shell-command-to-string
131      (format gnus-convert-image-to-x-face-command
132              (shell-quote-argument (expand-file-name file))))))
133
134 ;;;###autoload
135 (defun gnus-face-from-file (file)
136   "Return a Face header based on an image file.
137
138 Depending on `gnus-convert-image-to-face-command' it may accept
139 different input formats."
140   (interactive "fImage file name: ")
141   (when (file-exists-p file)
142     (let ((done nil)
143           (attempt "")
144           (quant 16))
145       (while (and (not done)
146                   (> quant 1))
147         (setq attempt
148               (let ((coding-system-for-read 'binary))
149                 (gnus-shell-command-to-string
150                  (format gnus-convert-image-to-face-command
151                          (shell-quote-argument (expand-file-name file))
152                          quant))))
153         (if (> (length attempt) 726)
154             (progn
155               (setq quant (- quant (if (< quant 10) 1 2)))
156               (gnus-message 9 "Length %d; trying quant %d"
157                             (length attempt) quant))
158           (setq done t)))
159       (if done
160           (mm-with-unibyte-buffer
161             (insert attempt)
162             (gnus-face-encode))
163         nil))))
164
165 (defun gnus-face-encode ()
166   (let ((step 72))
167     (base64-encode-region (point-min) (point-max))
168     (goto-char (point-min))
169     (while (search-forward "\n" nil t)
170       (replace-match ""))
171     (goto-char (point-min))
172     (while (> (- (point-max) (point))
173               step)
174       (forward-char step)
175       (insert "\n ")
176       (setq step 76))
177     (buffer-string)))
178
179 ;;;###autoload
180 (defun gnus-convert-face-to-png (face)
181   "Convert FACE (which is base64-encoded) to a PNG.
182 The PNG is returned as a string."
183   (mm-with-unibyte-buffer
184     (insert face)
185     (ignore-errors
186       (base64-decode-region (point-min) (point-max)))
187     (buffer-string)))
188
189 ;;;###autoload
190 (defun gnus-convert-png-to-face (file)
191   "Convert FILE to a Face.
192 FILE should be a PNG file that's 48x48 and smaller than or equal to
193 726 bytes."
194   (mm-with-unibyte-buffer
195     (insert-file-contents file)
196     (when (> (buffer-size) 726)
197       (error "The file is %d bytes long, which is too long"
198              (buffer-size)))
199     (gnus-face-encode)))
200
201 (defface gnus-x-face '((t (:foreground "black" :background "white")))
202   "Face to show X-Face.
203 The colors from this face are used as the foreground and background
204 colors of the displayed X-Faces."
205   :group 'gnus-article-headers)
206
207 (declare-function article-narrow-to-head   "gnus-art" ())
208 (declare-function gnus-article-goto-header "gnus-art" (header))
209 (declare-function gnus-add-image           "gnus-art" (category image))
210 (declare-function gnus-add-wash-type       "gnus-art" (type))
211
212 (defun gnus-display-x-face-in-from (data)
213   "Display the X-Face DATA in the From header."
214   (require 'gnus-art)
215   (let (pbm)
216     (when (or (gnus-image-type-available-p 'xface)
217               (and (gnus-image-type-available-p 'pbm)
218                    (setq pbm (uncompface data))))
219       (save-excursion
220         (save-restriction
221           (article-narrow-to-head)
222           (gnus-article-goto-header "from")
223           (when (bobp)
224             (insert "From: [no `from' set]\n")
225             (forward-char -17))
226           (gnus-add-image
227            'xface
228            (gnus-put-image
229             (if (gnus-image-type-available-p 'xface)
230                 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
231                        (cdr (assq 'xface gnus-face-properties-alist)))
232               (apply 'gnus-create-image pbm 'pbm t
233                      (cdr (assq 'pbm gnus-face-properties-alist))))
234             nil 'xface))
235           (gnus-add-wash-type 'xface))))))
236
237 (defun gnus-grab-cam-x-face ()
238   "Grab a picture off the camera and make it into an X-Face."
239   (interactive)
240   (shell-command "xawtv-remote snap ppm")
241   (let ((file nil))
242     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
243                                              t "snap.*ppm")))
244       (sleep-for 1))
245     (setq file (car file))
246     (with-temp-buffer
247       (shell-command
248        (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | ppmnorm 2>/dev/null | pnmscale -width 48 | ppmtopgm | pgmtopbm -threshold -value 0.92 | pbmtoxbm | compface"
249                file)
250        (current-buffer))
251       ;;(sleep-for 3)
252       (delete-file file)
253       (buffer-string))))
254
255 (defun gnus-grab-cam-face ()
256   "Grab a picture off the camera and make it into an X-Face."
257   (interactive)
258   (shell-command "xawtv-remote snap ppm")
259   (let ((file nil)
260         result)
261     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
262                                              t "snap.*ppm")))
263       (sleep-for 1))
264     (setq file (car file))
265     (shell-command
266      (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
267              file))
268     (let ((gnus-convert-image-to-face-command
269            (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
270                    (gnus-fun-ppm-change-string))))
271       (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
272     (delete-file file)
273     ;;(delete-file "/tmp/gnus.face.ppm")
274     result))
275
276 (defun gnus-fun-ppm-change-string ()
277   (let* ((possibilities '("%02x0000" "00%02x00" "0000%02x"
278                           "%02x%02x00" "00%02x%02x" "%02x00%02x"))
279          (format (concat "'#%02x%02x%02x' '#"
280                          (nth (random 6) possibilities)
281                          "'"))
282          (values nil))
283   (dotimes (i 255)
284     (push (format format i i i i i i)
285           values))
286   (mapconcat 'identity values " ")))
287
288 (provide 'gnus-fun)
289
290 ;;; gnus-fun.el ends here