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