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