Relicense "GPLv2 or later" files to "GPLv3 or later".
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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, or (at your option)
13 ;; 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; 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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl))
31
32 (require 'mm-util)
33 (require 'gnus-ems)
34 (require 'gnus-util)
35 (require 'gnus)
36
37 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
38   "*Directory where X-Face PBM files are stored."
39   :version "22.1"
40   :group 'gnus-fun
41   :type 'directory)
42
43 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
44   "Command for converting a PBM to an X-Face."
45   :version "22.1"
46   :group 'gnus-fun
47   :type 'string)
48
49 (defcustom gnus-convert-image-to-x-face-command
50   "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
51   "Command for converting an image to an X-Face.
52 The command must take a image filename (use \"%s\") as input.
53 The output must be the Face header data on stdout in PNG format.
54
55 By default it takes a GIF filename and output the X-Face header data
56 on stdout."
57   :version "22.1"
58   :group 'gnus-fun
59   :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
60                         "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
61                  (const :tag "convert"
62                         "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
63                  (string)))
64
65 (defcustom gnus-convert-image-to-face-command
66   "convert -scale 48x48! %s -colors %d png:-"
67   "Command for converting an image to a Face.
68
69 The command must take an image filename (first format argument
70 \"%s\") and the number of colors (second format argument: \"%d\")
71 as input.  The output must be the Face header data on stdout in
72 PNG format."
73   :version "22.1"
74   :group 'gnus-fun
75   :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
76                         "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
77                  (const :tag "convert"
78                         "convert -scale 48x48! %s -colors %d png:-")
79                  (string)))
80
81 (defun gnus-shell-command-to-string (command)
82   "Like `shell-command-to-string' except not mingling ERROR."
83   (with-output-to-string
84     (call-process shell-file-name nil (list standard-output nil)
85                   nil shell-command-switch command)))
86
87 (defun gnus-shell-command-on-region (start end command)
88   "A simplified `shell-command-on-region'.
89 Output to the current buffer, replace text, and don't mingle error."
90   (call-process-region start end shell-file-name t
91                        (list (current-buffer) nil)
92                        nil shell-command-switch command))
93
94 ;;;###autoload
95 (defun gnus-random-x-face ()
96   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
97   (interactive)
98   (when (file-exists-p gnus-x-face-directory)
99     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
100            (file (nth (random (length files)) files)))
101       (when file
102         (gnus-shell-command-to-string
103          (format gnus-convert-pbm-to-x-face-command
104                  (shell-quote-argument file)))))))
105
106 ;;;###autoload
107 (defun gnus-insert-random-x-face-header ()
108   "Insert a random X-Face header from `gnus-x-face-directory'."
109   (interactive)
110   (let ((data (gnus-random-x-face)))
111     (save-excursion
112       (message-goto-eoh)
113       (if data
114           (insert "X-Face: " data)
115         (message
116          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
117          gnus-x-face-directory)))))
118
119 ;;;###autoload
120 (defun gnus-x-face-from-file (file)
121   "Insert an X-Face header based on an image file.
122
123 Depending on `gnus-convert-image-to-x-face-command' it may accept
124 different input formats."
125   (interactive "fImage file name: ")
126   (when (file-exists-p file)
127     (gnus-shell-command-to-string
128      (format gnus-convert-image-to-x-face-command
129              (shell-quote-argument (expand-file-name file))))))
130
131 ;;;###autoload
132 (defun gnus-face-from-file (file)
133   "Return a Face header based on an image file.
134
135 Depending on `gnus-convert-image-to-face-command' it may accept
136 different input formats."
137   (interactive "fImage file name: ")
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 (if (< quant 10) 1 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