* pop3.el: Require nnheader.
[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   :group 'gnus-fun
38   :type 'directory)
39
40 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
41   "Command for converting a PBM to an X-Face."
42   :group 'gnus-fun
43   :type 'string)
44
45 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
46   "Command for converting an image to an X-Face.
47 By default it takes a GIF filename and output the X-Face header data
48 on stdout."
49   :group 'gnus-fun
50   :type 'string)
51
52 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
53   "Command for converting an image to an Face.
54 By default it takes a JPEG filename and output the Face header data
55 on stdout."
56   :group 'gnus-fun
57   :type 'string)
58
59 (defun gnus-shell-command-to-string (command)
60   "Like `shell-command-to-string' except not mingling ERROR."
61   (with-output-to-string
62     (call-process shell-file-name nil (list standard-output nil)
63                   nil shell-command-switch command)))
64
65 (defun gnus-shell-command-on-region (start end command)
66   "A simplified `shell-command-on-region'.
67 Output to the current buffer, replace text, and don't mingle error."
68   (call-process-region start end shell-file-name t
69                        (list (current-buffer) nil)
70                        nil shell-command-switch command))
71
72 ;;;###autoload
73 (defun gnus-random-x-face ()
74   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
75   (interactive)
76   (when (file-exists-p gnus-x-face-directory)
77     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
78            (file (nth (random (length files)) files)))
79       (when file
80         (gnus-shell-command-to-string
81          (format gnus-convert-pbm-to-x-face-command
82                  (shell-quote-argument file)))))))
83
84 ;;;###autoload
85 (defun gnus-insert-random-x-face-header ()
86   "Insert a random X-Face header from `gnus-x-face-directory'."
87   (interactive)
88   (let ((data (gnus-random-x-face)))
89     (save-excursion
90       (message-goto-eoh)
91       (if data
92           (insert "X-Face: " data)
93         (message
94          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
95          gnus-x-face-directory)))))
96
97 ;;;###autoload
98 (defun gnus-x-face-from-file (file)
99   "Insert an X-Face header based on an image file."
100   (interactive "fImage file name (by default GIF): ")
101   (when (file-exists-p file)
102     (gnus-shell-command-to-string
103      (format gnus-convert-image-to-x-face-command
104              (shell-quote-argument (expand-file-name file))))))
105
106 ;;;###autoload
107 (defun gnus-face-from-file (file)
108   "Return an Face header based on an image file."
109   (interactive "fImage file name (by default JPEG): ")
110   (when (file-exists-p file)
111     (let ((done nil)
112           (attempt "")
113           (quant 16))
114       (while (and (not done)
115                   (> quant 1))
116         (setq attempt
117               (let ((coding-system-for-read 'binary))
118                 (gnus-shell-command-to-string
119                  (format gnus-convert-image-to-face-command
120                          (shell-quote-argument (expand-file-name file))
121                          quant))))
122         (if (> (length attempt) 726)
123             (progn
124               (setq quant (- quant 2))
125               (gnus-message 9 "Length %d; trying quant %d"
126                             (length attempt) quant))
127           (setq done t)))
128       (if done
129           (mm-with-unibyte-buffer
130             (insert attempt)
131             (gnus-face-encode))
132         nil))))
133
134 (defun gnus-face-encode ()
135   (let ((step 72))
136     (base64-encode-region (point-min) (point-max))
137     (goto-char (point-min))
138     (while (search-forward "\n" nil t)
139       (replace-match ""))
140     (goto-char (point-min))
141     (while (> (- (point-max) (point))
142               step)
143       (forward-char step)
144       (insert "\n ")
145       (setq step 76))
146     (buffer-string)))
147
148 ;;;###autoload
149 (defun gnus-convert-face-to-png (face)
150   "Convert FACE (which is base64-encoded) to a PNG.
151 The PNG is returned as a string."
152   (mm-with-unibyte-buffer
153     (insert face)
154     (ignore-errors
155       (base64-decode-region (point-min) (point-max)))
156     (buffer-string)))
157
158 ;;;###autoload
159 (defun gnus-convert-png-to-face (file)
160   "Convert FILE to a Face.
161 FILE should be a PNG file that's 48x48 and smaller than or equal to
162 726 bytes."
163   (mm-with-unibyte-buffer
164     (insert-file-contents file)
165     (when (> (buffer-size) 726)
166       (error "The file is %d bytes long, which is too long"
167              (buffer-size)))
168     (gnus-face-encode)))
169
170 (defface gnus-x-face '((t (:foreground "black" :background "white")))
171   "Face to show X-Face.
172 The colors from this face are used as the foreground and background
173 colors of the displayed X-Faces."
174   :group 'gnus-article-headers)
175
176 (defun gnus-display-x-face-in-from (data)
177   "Display the X-Face DATA in the From header."
178   (let ((default-enable-multibyte-characters nil)
179         pbm)
180     (when (or (gnus-image-type-available-p 'xface)
181               (and (gnus-image-type-available-p 'pbm)
182                    (setq pbm (uncompface data))))
183       (save-excursion
184         (save-restriction
185           (article-narrow-to-head)
186           (gnus-article-goto-header "from")
187           (when (bobp)
188             (insert "From: [no `from' set]\n")
189             (forward-char -17))
190           (gnus-add-image
191            'xface
192            (gnus-put-image
193             (if (gnus-image-type-available-p 'xface)
194                 (gnus-create-image
195                  (concat "X-Face: " data)
196                  'xface t :face 'gnus-x-face)
197               (gnus-create-image
198                pbm 'pbm t :face 'gnus-x-face)) nil 'xface))
199           (gnus-add-wash-type 'xface))))))
200
201 (defun gnus-grab-cam-x-face ()
202   "Grab a picture off the camera and make it into an X-Face."
203   (interactive)
204   (shell-command "xawtv-remote snap ppm")
205   (let ((file nil))
206     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
207                                              t "snap.*ppm")))
208       (sleep-for 1))
209     (setq file (car file))
210     (with-temp-buffer
211       (shell-command
212        (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"
213                file)
214        (current-buffer))
215       ;;(sleep-for 3)
216       (delete-file file)
217       (buffer-string))))
218
219 (defun gnus-grab-cam-face ()
220   "Grab a picture off the camera and make it into an X-Face."
221   (interactive)
222   (shell-command "xawtv-remote snap ppm")
223   (let ((file nil)
224         result)
225     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
226                                              t "snap.*ppm")))
227       (sleep-for 1))
228     (setq file (car file))
229     (shell-command
230      (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
231              file))
232     (let ((gnus-convert-image-to-face-command
233            (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
234                    (gnus-fun-ppm-change-string))))
235       (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
236     (delete-file file)
237     ;;(delete-file "/tmp/gnus.face.ppm")
238     result))
239
240 (defun gnus-fun-ppm-change-string ()
241   (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
242                         "%02x%02x00" "00%02x%02x" "%02x00%02x"))
243          (format (concat "'#%02x%02x%02x' '#"
244                          (nth (random 6) possibilites)
245                          "'"))
246          (values nil))
247   (dotimes (i 255)
248     (push (format format i i i i i i)
249           values))
250   (mapconcat 'identity values " ")))
251
252 (provide 'gnus-fun)
253
254 ;;; gnus-fun.el ends here