*** empty log message ***
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivoluos extension functions to Gnus
2 ;; Copyright (C) 2002 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 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
29   "*Directory where X-Face PBM files are stored."
30   :group 'gnus-fun
31   :type 'directory)
32
33 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
34   "Command for converting a PBM to an X-Face."
35   :group 'gnus-fun
36   :type 'string)
37
38 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
39   "Command for converting a GIF to an X-Face."
40   :group 'gnus-fun
41   :type 'string)
42
43 (defun gnus-shell-command-to-string (command)
44   "Like `shell-command-to-string' except not mingling ERROR."
45   (with-output-to-string
46     (call-process shell-file-name nil (list standard-output nil)
47                   nil shell-command-switch command)))
48
49 (defun gnus-shell-command-on-region (start end command)
50   "A simplified `shell-command-on-region'.
51 Output to the current buffer, replace text, and don't mingle error."
52   (call-process-region start end shell-file-name t
53                        (list (current-buffer) nil)
54                        nil shell-command-switch command))
55
56 ;;;###autoload
57 (defun gnus-random-x-face ()
58   "Insert a random X-Face header from `gnus-x-face-directory'."
59   (interactive)
60   (when (file-exists-p gnus-x-face-directory)
61     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
62            (file (nth (random (length files)) files)))
63       (when file
64         (gnus-shell-command-to-string
65          (format gnus-convert-pbm-to-x-face-command
66                  (shell-quote-argument file)))))))
67
68 ;;;###autoload
69 (defun gnus-x-face-from-file (file)
70   "Insert an X-Face header based on an image file."
71   (interactive "fImage file name:" )
72   (when (file-exists-p file)
73     (gnus-shell-command-to-string
74      (format gnus-convert-image-to-x-face-command
75              (shell-quote-argument file)))))
76
77 (defun gnus-convert-image-to-gray-x-face (file depth)
78   (let* ((mapfile (make-temp-name (expand-file-name "gnus." mm-tmp-directory)))
79          (levels (expt 2 depth))
80          (step (/ 255 (1- levels)))
81          color-alist bits bits-list mask pixel x-faces)
82     (with-temp-file mapfile
83       (insert "P3\n")
84       (insert (format "%d 1\n" levels))
85       (insert "255\n")
86       (dotimes (i levels)
87         (insert (format "%d %d %d\n"
88                         (* step i) (* step i) (* step i)))
89         (push (cons (* step i) i) color-alist)))
90     (when (file-exists-p file)
91       (with-temp-buffer
92         (insert (gnus-shell-command-to-string
93                  (format "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant -fs -map %s | ppmtopgm | pnmnoraw"
94                          (shell-quote-argument file)
95                          mapfile)))
96         (goto-char (point-min))
97         (forward-line 3)
98         (while (setq pixel (ignore-errors (read (current-buffer))))
99           (push (cdr (assq pixel color-alist)) bits-list))
100         (setq bits-list (nreverse bits-list))
101         (dotimes (bit-number depth)
102           (setq mask (expt 2 bit-number))
103           (with-temp-buffer
104             (insert "P1\n48 48\n")
105             (dolist (bits bits-list)
106               (insert (if (zerop (logand bits mask)) "0 " "1 ")))
107             (gnus-shell-command-on-region
108              (point-min) (point-max)
109              ;; the following is taken from xbmtoikon:
110              "pbmtoicon | sed '/^[      ]*[*\\\\/]/d; s/[       ]//g; s/,$//' | tr , '\\012' | sed 's/^0x//; s/^/0x/' | pr -l1 -t -w22 -3 -s, | sed 's/,*$/,/' | compface")
111             (push (buffer-string) x-faces))))
112       (dotimes (i (length x-faces))
113         (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
114                 (nth i x-faces))))
115     (delete-file mapfile)))
116
117 ;;;###autoload
118 (defun gnus-convert-gray-x-face-to-xpm (faces)
119   (let* ((depth (length faces))
120          (scale (/ 255 (1- (expt 2 depth))))
121          (ok-p t)
122          (coding-system-for-read 'binary)
123          (coding-system-for-write 'binary)
124          default-enable-multibyte-characters
125          start bit-array bit-arrays pixel)
126     (with-temp-buffer
127       (dolist (face faces)
128         (erase-buffer)
129         (insert (uncompface face))
130         (gnus-shell-command-on-region
131          (point-min) (point-max)
132          "pnmnoraw")
133         (goto-char (point-min))
134         (forward-line 2)
135         (setq start (point))
136         (insert "[")
137         (while (not (eobp))
138           (forward-char 1)
139           (insert " "))
140         (insert "]")
141         (goto-char start)
142         (setq bit-array (read (current-buffer)))
143         (unless (= (length bit-array) (* 48 48))
144           (setq ok-p nil))
145         (push bit-array bit-arrays))
146       (when ok-p
147         (erase-buffer)
148         (insert "P2\n48 48\n255\n")
149         (dotimes (i (* 48 48))
150           (setq pixel 0)
151           (dotimes (plane depth)
152             (setq pixel (+ (* pixel 2) (aref (nth plane bit-arrays) i))))
153           (insert (number-to-string (* scale pixel)) " "))
154         (gnus-shell-command-on-region
155          (point-min) (point-max)
156          "ppmtoxpm")
157         (buffer-string)))))
158
159 ;;;###autoload
160 (defun gnus-convert-gray-x-face-region (beg end)
161   "Convert the X-Faces in region to a PPM file."
162   (interactive "r")
163   (let ((input (buffer-substring beg end))
164         faces)
165     (with-temp-buffer
166       (insert input)
167       (goto-char (point-min))
168       (while (not (eobp))
169         (save-restriction
170           (mail-header-narrow-to-field)
171           (push (mail-header-field-value) faces)
172           (goto-char (point-max)))))
173     (gnus-convert-gray-x-face-to-xpm faces)))
174
175 (defface gnus-x-face '((t (:foreground "black" :background "white")))
176   "Face to show X-Face.
177 The colors from this face are used as the foreground and background
178 colors of the displayed X-Faces."
179   :group 'gnus-article-headers)
180
181 (defun gnus-display-x-face-in-from (data)
182   "Display the X-Face DATA in the From header."
183   (let ((default-enable-multibyte-characters nil)
184         pbm)
185     (when (or (gnus-image-type-available-p 'xface)
186               (and (gnus-image-type-available-p 'pbm)
187                    (setq pbm (uncompface data))))
188       (save-excursion
189         (save-restriction
190           (article-narrow-to-head)
191           (gnus-article-goto-header "from")
192           (when (bobp)
193             (insert "From: [no `from' set]\n")
194             (forward-char -17))
195           (gnus-add-image
196            'xface
197            (gnus-put-image
198             (if (gnus-image-type-available-p 'xface)
199                 (gnus-create-image
200                  (concat "X-Face: " data)
201                  'xface t :ascent 'center :face 'gnus-x-face)
202               (gnus-create-image
203                pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
204           (gnus-add-wash-type 'xface))))))
205
206 (defun gnus-grab-cam-x-face ()
207   "Grab a picture off the camera and make it into an X-Face."
208   (interactive)
209   (shell-command "xawtv-remote snap ppm")
210   (let ((file nil))
211     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
212                                              t "snap.*ppm")))
213       (sleep-for 1))
214     (setq file (car file))
215     (with-temp-buffer
216       (shell-command
217        (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"
218                file)
219        (current-buffer))
220       ;;(sleep-for 3)
221       (delete-file file)
222       (buffer-string))))
223
224 (defun gnus-grab-gray-x-face ()
225   "Grab a picture off the camera and make it into an X-Face."
226   (interactive)
227   (shell-command "xawtv-remote snap ppm")
228   (let ((file nil))
229     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
230                                              t "snap.*ppm")))
231       (sleep-for 1))
232     (setq file (car file))
233     (with-temp-buffer
234       (shell-command
235        (format "pnmcut -left 70 -top 100 -width 144 -height 144 '%s' | ppmquant 256 2>/dev/null | ppmtogif > '%s.gif'"
236                file file)
237        (current-buffer))
238       (delete-file file))
239     (gnus-convert-image-to-gray-x-face (concat file ".gif") 3)
240     (delete-file (concat file ".gif"))))
241
242 (defun gnus-respond-to-confirmation ()
243   "Respond to a Gmane confirmation message."
244   (interactive)
245   (gnus-summary-show-article 'raw)
246   (set-buffer gnus-article-buffer)
247   (let ((buffer-read-only nil))
248     (goto-char (point-min))
249     (gnus-article-goto-header "Original-To")
250     (replace-match "To:"))
251   (let ((auth nil))
252     (when (and (search-forward "Majordomo" nil t)
253                (re-search-forward "auth.*subscribe.*$" nil t))
254       (setq auth (match-string 0)))
255     (message-wide-reply)
256     (goto-char (point-min))
257     (gnus-article-goto-header "Cc")
258     (replace-match "From:")
259     (message-goto-body)
260     (delete-region (point) (point-max))
261     (when auth
262       (insert auth "\n"))))
263
264 (provide 'gnus-fun)
265
266 ;;; gnus-fun.el ends here