Synch with Gnus.
[elisp/gnus.git-] / lisp / gnus-uu.el
1 ;;; gnus-uu.el --- extract (uu)encoded files in Gnus
2 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Created: 2 Oct 1993
7 ;; Keyword: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-art)
34 (require 'message)
35 (require 'gnus-msg)
36
37 (defgroup gnus-extract nil
38   "Extracting encoded files."
39   :prefix "gnus-uu-"
40   :group 'gnus)
41
42 (defgroup gnus-extract-view nil
43   "Viewwing extracted files."
44   :group 'gnus-extract)
45
46 (defgroup gnus-extract-archive nil
47   "Extracting encoded archives."
48   :group 'gnus-extract)
49
50 (defgroup gnus-extract-post nil
51   "Extracting encoded archives."
52   :prefix "gnus-uu-post"
53   :group 'gnus-extract)
54
55 ;; Default viewing action rules
56
57 (defcustom gnus-uu-default-view-rules
58   '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed 's/\r$//'")
59     ("\\.pas$" "cat %s | sed 's/\r$//'")
60     ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
61     ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "display")
62     ("\\.tga$" "tgatoppm %s | ee -")
63     ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
64      "sox -v .5 %s -t .au -u - > /dev/audio")
65     ("\\.au$" "cat %s > /dev/audio")
66     ("\\.midi?$" "playmidi -f")
67     ("\\.mod$" "str32")
68     ("\\.ps$" "ghostview")
69     ("\\.dvi$" "xdvi")
70     ("\\.html$" "xmosaic")
71     ("\\.mpe?g$" "mpeg_play")
72     ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
73     ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
74      "gnus-uu-archive"))
75   "*Default actions to be taken when the user asks to view a file.
76 To change the behaviour, you can either edit this variable or set
77 `gnus-uu-user-view-rules' to something useful.
78
79 For example:
80
81 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
82 following in your .emacs file:
83
84   (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
85
86 Both these variables are lists of lists with two string elements.  The
87 first string is a regular expression.  If the file name matches this
88 regular expression, the command in the second string is executed with
89 the file as an argument.
90
91 If the command string contains \"%s\", the file name will be inserted
92 at that point in the command string.  If there's no \"%s\" in the
93 command string, the file name will be appended to the command string
94 before executing.
95
96 There are several user variables to tailor the behaviour of gnus-uu to
97 your needs.  First we have `gnus-uu-user-view-rules', which is the
98 variable gnus-uu first consults when trying to decide how to view a
99 file.  If this variable contains no matches, gnus-uu examines the
100 default rule variable provided in this package.  If gnus-uu finds no
101 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
102 match."
103   :group 'gnus-extract-view
104   :type '(repeat (group regexp (string :tag "Command"))))
105
106 (defcustom gnus-uu-user-view-rules nil
107   "What actions are to be taken to view a file.
108 See the documentation on the `gnus-uu-default-view-rules' variable for
109 details."
110   :group 'gnus-extract-view
111   :type '(repeat (group regexp (string :tag "Command"))))
112
113 (defcustom gnus-uu-user-view-rules-end
114   '(("" "file"))
115   "*What actions are to be taken if no rule matched the file name.
116 See the documentation on the `gnus-uu-default-view-rules' variable for
117 details."
118   :group 'gnus-extract-view
119   :type '(repeat (group regexp (string :tag "Command"))))
120
121 ;; Default unpacking commands
122
123 (defcustom gnus-uu-default-archive-rules
124   '(("\\.tar$" "tar xf")
125     ("\\.zip$" "unzip -o")
126     ("\\.ar$" "ar x")
127     ("\\.arj$" "unarj x")
128     ("\\.zoo$" "zoo -e")
129     ("\\.\\(lzh\\|lha\\)$" "lha x")
130     ("\\.Z$" "uncompress")
131     ("\\.gz$" "gunzip")
132     ("\\.arc$" "arc -x"))
133   "*See `gnus-uu-user-archive-rules'."
134   :group 'gnus-extract-archive
135   :type '(repeat (group regexp (string :tag "Command"))))
136
137 (defvar gnus-uu-destructive-archivers
138   (list "uncompress" "gunzip"))
139
140 (defcustom gnus-uu-user-archive-rules nil
141   "A list that can be set to override the default archive unpacking commands.
142 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
143 unpack zip files, say the following:
144   (setq gnus-uu-user-archive-rules
145     '((\"\\\\.tar$\" \"untar\")
146       (\"\\\\.zip$\" \"zip -x\")))"
147   :group 'gnus-extract-archive
148   :type '(repeat (group regexp (string :tag "Command"))))
149
150 (defcustom gnus-uu-ignore-files-by-name nil
151   "*A regular expression saying what files should not be viewed based on name.
152 If, for instance, you want gnus-uu to ignore all .au and .wav files,
153 you could say something like
154
155   (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
156
157 Note that this variable can be used in conjunction with the
158 `gnus-uu-ignore-files-by-type' variable."
159   :group 'gnus-extract
160   :type '(choice (const :tag "off" nil)
161                  (regexp :format "%v")))
162
163 (defcustom gnus-uu-ignore-files-by-type nil
164   "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
165 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
166 you could say something like
167
168   (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
169
170 Note that this variable can be used in conjunction with the
171 `gnus-uu-ignore-files-by-name' variable."
172   :group 'gnus-extract
173   :type '(choice (const :tag "off" nil)
174                  (regexp :format "%v")))
175
176 ;; Pseudo-MIME support
177
178 (defconst gnus-uu-ext-to-mime-list
179   '(("\\.gif$" "image/gif")
180     ("\\.jpe?g$" "image/jpeg")
181     ("\\.tiff?$" "image/tiff")
182     ("\\.xwd$" "image/xwd")
183     ("\\.pbm$" "image/pbm")
184     ("\\.pgm$" "image/pgm")
185     ("\\.ppm$" "image/ppm")
186     ("\\.xbm$" "image/xbm")
187     ("\\.pcx$" "image/pcx")
188     ("\\.tga$" "image/tga")
189     ("\\.ps$" "image/postscript")
190     ("\\.fli$" "video/fli")
191     ("\\.wav$" "audio/wav")
192     ("\\.aiff$" "audio/aiff")
193     ("\\.hcom$" "audio/hcom")
194     ("\\.voc$" "audio/voc")
195     ("\\.smp$" "audio/smp")
196     ("\\.mod$" "audio/mod")
197     ("\\.dvi$" "image/dvi")
198     ("\\.mpe?g$" "video/mpeg")
199     ("\\.au$" "audio/basic")
200     ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
201     ("\\.\\(c\\|h\\)$" "text/source")
202     ("read.*me" "text/plain")
203     ("\\.html$" "text/html")
204     ("\\.bat$" "text/bat")
205     ("\\.[1-6]$" "text/man")
206     ("\\.flc$" "video/flc")
207     ("\\.rle$" "video/rle")
208     ("\\.pfx$" "video/pfx")
209     ("\\.avi$" "video/avi")
210     ("\\.sme$" "video/sme")
211     ("\\.rpza$" "video/prza")
212     ("\\.dl$" "video/dl")
213     ("\\.qt$" "video/qt")
214     ("\\.rsrc$" "video/rsrc")
215     ("\\..*$" "unknown/unknown")))
216
217 ;; Various variables users may set
218
219 (defcustom gnus-uu-tmp-dir
220   (cond ((fboundp 'temp-directory) (temp-directory))
221         ((boundp 'temporary-file-directory) temporary-file-directory)
222         ("/tmp/"))
223   "*Variable saying where gnus-uu is to do its work.
224 Default is \"/tmp/\"."
225   :group 'gnus-extract
226   :type 'directory)
227
228 (defcustom gnus-uu-do-not-unpack-archives nil
229   "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
230 Default is nil."
231   :group 'gnus-extract-archive
232   :type 'boolean)
233
234 (defcustom gnus-uu-ignore-default-view-rules nil
235   "*Non-nil means that gnus-uu will ignore the default viewing rules.
236 Only the user viewing rules will be consulted.  Default is nil."
237   :group 'gnus-extract-view
238   :type 'boolean)
239
240 (defcustom gnus-uu-grabbed-file-functions nil
241   "Functions run on each file after successful decoding.
242 They will be called with the name of the file as the argument.
243 Likely functions you can use in this list are `gnus-uu-grab-view'
244 and `gnus-uu-grab-move'."
245   :group 'gnus-extract
246   :options '(gnus-uu-grab-view gnus-uu-grab-move)
247   :type 'hook)
248
249 (defcustom gnus-uu-ignore-default-archive-rules nil
250   "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
251 Only the user unpacking commands will be consulted.  Default is nil."
252   :group 'gnus-extract-archive
253   :type 'boolean)
254
255 (defcustom gnus-uu-kill-carriage-return t
256   "*Non-nil means that gnus-uu will strip all carriage returns from articles.
257 Default is t."
258   :group 'gnus-extract
259   :type 'boolean)
260
261 (defcustom gnus-uu-view-with-metamail nil
262   "*Non-nil means that files will be viewed with metamail.
263 The gnus-uu viewing functions will be ignored and gnus-uu will try
264 to guess at a content-type based on file name suffixes.  Default
265 it nil."
266   :group 'gnus-extract
267   :type 'boolean)
268
269 (defcustom gnus-uu-unmark-articles-not-decoded nil
270   "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
271 Default is nil."
272   :group 'gnus-extract
273   :type 'boolean)
274
275 (defcustom gnus-uu-correct-stripped-uucode nil
276   "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
277 Default is nil."
278   :group 'gnus-extract
279   :type 'boolean)
280
281 (defcustom gnus-uu-save-in-digest nil
282   "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
283 If this variable is nil, gnus-uu will just save everything in a
284 file without any embellishments.  The digesting almost conforms to RFC1153 -
285 no easy way to specify any meaningful volume and issue numbers were found,
286 so I simply dropped them."
287   :group 'gnus-extract
288   :type 'boolean)
289
290 (defcustom gnus-uu-pre-uudecode-hook nil
291   "Hook run before sending a message to uudecode."
292   :group 'gnus-extract
293   :type 'hook)
294
295 (defcustom gnus-uu-digest-headers
296   '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
297     "^Summary:" "^References:" "^Content-Type:" "^Content-Transfer-Encoding:"
298     "^MIME-Version:" "^Content-Disposition:" "^Content-Description:"
299     "^Content-ID:" "^User-Agent:" "^X-Face:")
300   "*List of regexps to match headers included in digested messages.
301 The headers will be included in the sequence they are matched."
302   :group 'gnus-extract
303   :type '(repeat regexp))
304
305 (defcustom gnus-uu-save-separate-articles nil
306   "*Non-nil means that gnus-uu will save articles in separate files."
307   :group 'gnus-extract
308   :type 'boolean)
309
310 (defcustom gnus-uu-be-dangerous 'ask
311   "*Specifies what to do if unusual situations arise during decoding.
312 If nil, be as conservative as possible.  If t, ignore things that
313 didn't work, and overwrite existing files.  Otherwise, ask each time."
314   :group 'gnus-extract
315   :type '(choice (const :tag "conservative" nil)
316                  (const :tag "ask" ask)
317                  (const :tag "liberal" t)))
318
319 ;; Internal variables
320
321 (defvar gnus-uu-saved-article-name nil)
322
323 (defvar gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
324 (defvar gnus-uu-end-string "^end[ \t]*$")
325
326 (defvar gnus-uu-body-line "^M")
327 (let ((i 61))
328   (while (> (setq i (1- i)) 0)
329     (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
330   (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
331
332 ;"^M.............................................................?$"
333
334 (defvar gnus-uu-shar-begin-string "^#! */bin/sh")
335
336 (defvar gnus-uu-shar-file-name nil)
337 (defvar gnus-uu-shar-name-marker
338   "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
339
340 (defvar gnus-uu-postscript-begin-string "^%!PS-")
341 (defvar gnus-uu-postscript-end-string "^%%EOF$")
342
343 (defvar gnus-uu-file-name nil)
344 (defvar gnus-uu-uudecode-process nil)
345 (defvar gnus-uu-binhex-article-name nil)
346
347 (defvar gnus-uu-work-dir nil)
348
349 (defvar gnus-uu-output-buffer-name " *Gnus UU Output*")
350
351 (defvar gnus-uu-default-dir gnus-article-save-directory)
352 (defvar gnus-uu-digest-from-subject nil)
353 (defvar gnus-uu-digest-buffer nil)
354
355 ;; Keymaps
356
357 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
358   "p" gnus-summary-mark-as-processable
359   "u" gnus-summary-unmark-as-processable
360   "U" gnus-summary-unmark-all-processable
361   "v" gnus-uu-mark-over
362   "s" gnus-uu-mark-series
363   "r" gnus-uu-mark-region
364   "g" gnus-uu-unmark-region
365   "R" gnus-uu-mark-by-regexp
366   "G" gnus-uu-unmark-by-regexp
367   "t" gnus-uu-mark-thread
368   "T" gnus-uu-unmark-thread
369   "a" gnus-uu-mark-all
370   "b" gnus-uu-mark-buffer
371   "S" gnus-uu-mark-sparse
372   "k" gnus-summary-kill-process-mark
373   "y" gnus-summary-yank-process-mark
374   "w" gnus-summary-save-process-mark
375   "i" gnus-uu-invert-processable)
376
377 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
378   ;;"x" gnus-uu-extract-any
379   "m" gnus-summary-save-parts
380   "u" gnus-uu-decode-uu
381   "U" gnus-uu-decode-uu-and-save
382   "s" gnus-uu-decode-unshar
383   "S" gnus-uu-decode-unshar-and-save
384   "o" gnus-uu-decode-save
385   "O" gnus-uu-decode-save
386   "b" gnus-uu-decode-binhex
387   "B" gnus-uu-decode-binhex
388   "p" gnus-uu-decode-postscript
389   "P" gnus-uu-decode-postscript-and-save)
390
391 (gnus-define-keys
392     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
393   "u" gnus-uu-decode-uu-view
394   "U" gnus-uu-decode-uu-and-save-view
395   "s" gnus-uu-decode-unshar-view
396   "S" gnus-uu-decode-unshar-and-save-view
397   "o" gnus-uu-decode-save-view
398   "O" gnus-uu-decode-save-view
399   "b" gnus-uu-decode-binhex-view
400   "B" gnus-uu-decode-binhex-view
401   "p" gnus-uu-decode-postscript-view
402   "P" gnus-uu-decode-postscript-and-save-view)
403
404
405 ;; Commands.
406
407 (defun gnus-uu-decode-uu (&optional n)
408   "Uudecodes the current article."
409   (interactive "P")
410   (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
411
412 (defun gnus-uu-decode-uu-and-save (n dir)
413   "Decodes and saves the resulting file."
414   (interactive
415    (list current-prefix-arg
416          (file-name-as-directory
417           (read-file-name "Uudecode and save in dir: "
418                           gnus-uu-default-dir
419                           gnus-uu-default-dir t))))
420   (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
421
422 (defun gnus-uu-decode-unshar (&optional n)
423   "Unshars the current article."
424   (interactive "P")
425   (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
426
427 (defun gnus-uu-decode-unshar-and-save (n dir)
428   "Unshars and saves the current article."
429   (interactive
430    (list current-prefix-arg
431          (file-name-as-directory
432           (read-file-name "Unshar and save in dir: "
433                           gnus-uu-default-dir
434                           gnus-uu-default-dir t))))
435   (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
436
437 (defun gnus-uu-decode-save (n file)
438   "Saves the current article."
439   (interactive
440    (list current-prefix-arg
441          (read-file-name
442           (if gnus-uu-save-separate-articles
443               "Save articles is dir: "
444             "Save articles in file: ")
445           gnus-uu-default-dir
446           gnus-uu-default-dir)))
447   (setq gnus-uu-saved-article-name file)
448   (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
449
450 (defun gnus-uu-decode-binhex (n dir)
451   "Unbinhexes the current article."
452   (interactive
453    (list current-prefix-arg
454          (file-name-as-directory
455           (read-file-name "Unbinhex and save in dir: "
456                           gnus-uu-default-dir
457                           gnus-uu-default-dir))))
458   (setq gnus-uu-binhex-article-name
459         (make-temp-name (concat gnus-uu-work-dir "binhex")))
460   (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
461
462 (defun gnus-uu-decode-uu-view (&optional n)
463   "Uudecodes and views the current article."
464   (interactive "P")
465   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
466     (gnus-uu-decode-uu n)))
467
468 (defun gnus-uu-decode-uu-and-save-view (n dir)
469   "Decodes, views and saves the resulting file."
470   (interactive
471    (list current-prefix-arg
472          (read-file-name "Uudecode, view and save in dir: "
473                          gnus-uu-default-dir
474                          gnus-uu-default-dir t)))
475   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
476     (gnus-uu-decode-uu-and-save n dir)))
477
478 (defun gnus-uu-decode-unshar-view (&optional n)
479   "Unshars and views the current article."
480   (interactive "P")
481   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
482     (gnus-uu-decode-unshar n)))
483
484 (defun gnus-uu-decode-unshar-and-save-view (n dir)
485   "Unshars and saves the current article."
486   (interactive
487    (list current-prefix-arg
488          (read-file-name "Unshar, view and save in dir: "
489                          gnus-uu-default-dir
490                          gnus-uu-default-dir t)))
491   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
492     (gnus-uu-decode-unshar-and-save n dir)))
493
494 (defun gnus-uu-decode-save-view (n file)
495   "Saves and views the current article."
496   (interactive
497    (list current-prefix-arg
498          (read-file-name  (if gnus-uu-save-separate-articles
499                               "Save articles is dir: "
500                             "Save articles in file: ")
501                           gnus-uu-default-dir gnus-uu-default-dir)))
502   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
503     (gnus-uu-decode-save n file)))
504
505 (defun gnus-uu-decode-binhex-view (n file)
506   "Unbinhexes and views the current article."
507   (interactive
508    (list current-prefix-arg
509          (read-file-name "Unbinhex, view and save in dir: "
510                          gnus-uu-default-dir gnus-uu-default-dir)))
511   (setq gnus-uu-binhex-article-name
512         (make-temp-name (concat gnus-uu-work-dir "binhex")))
513   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
514     (gnus-uu-decode-binhex n file)))
515
516
517 ;; Digest and forward articles
518
519 (defun gnus-uu-digest-mail-forward (&optional n post)
520   "Digests and forwards all articles in this series."
521   (interactive "P")
522   (let ((gnus-uu-save-in-digest t)
523         (file (make-temp-name (nnheader-concat gnus-uu-tmp-dir "forward")))
524         gnus-uu-digest-buffer subject from)
525     (gnus-setup-message 'forward
526       (setq gnus-uu-digest-from-subject nil)
527       (setq gnus-uu-digest-buffer
528             (gnus-get-buffer-create " *gnus-uu-forward*"))
529       (gnus-uu-decode-save n file)
530       (set-buffer gnus-uu-digest-buffer)
531       (let ((fs gnus-uu-digest-from-subject))
532         (when fs
533           (setq from (caar fs)
534                 subject (gnus-simplify-subject-fuzzy (cdar fs))
535                 fs (cdr fs))
536           (while (and fs (or from subject))
537             (when from
538               (unless (string= from (caar fs))
539                 (setq from nil)))
540             (when subject
541               (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
542                                subject)
543                 (setq subject nil)))
544             (setq fs (cdr fs))))
545         (unless subject
546           (setq subject "Digested Articles"))
547         (unless from
548           (setq from
549                 (if (gnus-news-group-p gnus-newsgroup-name)
550                     gnus-newsgroup-name
551                   "Various"))))
552       (mime-edit-enclose-digest-region (point-min) (point-max))
553       (if post
554           (message-news nil (concat "[" from "] " subject))
555         (message-mail nil (concat "[" from "] " subject)))
556       (message-goto-body)
557       ;; Make sure we're at the start of the line.
558       (unless (bolp)
559         (insert "\n"))
560       ;; Insert the forwarded buffer.
561       (insert-buffer gnus-uu-digest-buffer)
562       (kill-buffer gnus-uu-digest-buffer)
563       (set-text-properties (point-min) (point-max) nil)
564       (message-position-point))
565     (setq gnus-uu-digest-from-subject nil)))
566
567 (defun gnus-uu-digest-post-forward (&optional n)
568   "Digest and forward to a newsgroup."
569   (interactive "P")
570   (gnus-uu-digest-mail-forward n t))
571
572 ;; Process marking.
573
574 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
575   "Set the process mark on articles whose subjects match REGEXP.
576 When called interactively, prompt for REGEXP.
577 Optional UNMARK non-nil means unmark instead of mark."
578   (interactive "sMark (regexp): \nP")
579   (let ((articles (gnus-uu-find-articles-matching regexp)))
580     (while articles
581       (if unmark
582           (gnus-summary-remove-process-mark (pop articles))
583         (gnus-summary-set-process-mark (pop articles))))
584     (message ""))
585   (gnus-summary-position-point))
586
587 (defun gnus-uu-unmark-by-regexp (regexp)
588   "Remove the process mark from articles whose subjects match REGEXP.
589 When called interactively, prompt for REGEXP."
590   (interactive "sUnmark (regexp): ")
591   (gnus-uu-mark-by-regexp regexp t))
592
593 (defun gnus-uu-mark-series ()
594   "Mark the current series with the process mark."
595   (interactive)
596   (let ((articles (gnus-uu-find-articles-matching)))
597     (while articles
598       (gnus-summary-set-process-mark (car articles))
599       (setq articles (cdr articles)))
600     (message ""))
601   (gnus-summary-position-point))
602
603 (defun gnus-uu-mark-region (beg end &optional unmark)
604   "Set the process mark on all articles between point and mark."
605   (interactive "r")
606   (save-excursion
607     (goto-char beg)
608     (while (< (point) end)
609       (if unmark
610           (gnus-summary-remove-process-mark (gnus-summary-article-number))
611         (gnus-summary-set-process-mark (gnus-summary-article-number)))
612       (forward-line 1)))
613   (gnus-summary-position-point))
614
615 (defun gnus-uu-unmark-region (beg end)
616   "Remove the process mark from all articles between point and mark."
617   (interactive "r")
618   (gnus-uu-mark-region beg end t))
619
620 (defun gnus-uu-mark-buffer ()
621   "Set the process mark on all articles in the buffer."
622   (interactive)
623   (gnus-uu-mark-region (point-min) (point-max)))
624
625 (defun gnus-uu-unmark-buffer ()
626   "Remove the process mark on all articles in the buffer."
627   (interactive)
628   (gnus-uu-mark-region (point-min) (point-max) t))
629
630 (defun gnus-uu-mark-thread ()
631   "Marks all articles downwards in this thread."
632   (interactive)
633   (gnus-save-hidden-threads
634     (let ((level (gnus-summary-thread-level)))
635       (while (and (gnus-summary-set-process-mark
636                    (gnus-summary-article-number))
637                   (zerop (gnus-summary-next-subject 1 nil t))
638                   (> (gnus-summary-thread-level) level)))))
639   (gnus-summary-position-point))
640
641 (defun gnus-uu-unmark-thread ()
642   "Unmarks all articles downwards in this thread."
643   (interactive)
644   (let ((level (gnus-summary-thread-level)))
645     (while (and (gnus-summary-remove-process-mark
646                  (gnus-summary-article-number))
647                 (zerop (gnus-summary-next-subject 1))
648                 (> (gnus-summary-thread-level) level))))
649   (gnus-summary-position-point))
650
651 (defun gnus-uu-invert-processable ()
652   "Invert the list of process-marked articles."
653   (interactive)
654   (let ((data gnus-newsgroup-data)
655         number)
656     (save-excursion
657       (while data
658         (if (memq (setq number (gnus-data-number (pop data)))
659                   gnus-newsgroup-processable)
660             (gnus-summary-remove-process-mark number)
661           (gnus-summary-set-process-mark number)))))
662   (gnus-summary-position-point))
663
664 (defun gnus-uu-mark-over (&optional score)
665   "Mark all articles with a score over SCORE (the prefix)."
666   (interactive "P")
667   (let ((score (or score gnus-summary-default-score 0))
668         (data gnus-newsgroup-data))
669     (save-excursion
670       (while data
671         (when (> (or (cdr (assq (gnus-data-number (car data))
672                                 gnus-newsgroup-scored))
673                      gnus-summary-default-score 0)
674                  score)
675           (gnus-summary-set-process-mark (caar data)))
676         (setq data (cdr data))))
677     (gnus-summary-position-point)))
678
679 (defun gnus-uu-mark-sparse ()
680   "Mark all series that have some articles marked."
681   (interactive)
682   (let ((marked (nreverse gnus-newsgroup-processable))
683         subject articles total headers)
684     (unless marked
685       (error "No articles marked with the process mark"))
686     (setq gnus-newsgroup-processable nil)
687     (save-excursion
688       (while marked
689         (and (vectorp (setq headers
690                             (gnus-summary-article-header (car marked))))
691              (setq subject (mail-header-subject headers)
692                    articles (gnus-uu-find-articles-matching
693                              (gnus-uu-reginize-string subject))
694                    total (nconc total articles)))
695         (while articles
696           (gnus-summary-set-process-mark (car articles))
697           (setcdr marked (delq (car articles) (cdr marked)))
698           (setq articles (cdr articles)))
699         (setq marked (cdr marked)))
700       (setq gnus-newsgroup-processable (nreverse total)))
701     (gnus-summary-position-point)))
702
703 (defun gnus-uu-mark-all ()
704   "Mark all articles in \"series\" order."
705   (interactive)
706   (setq gnus-newsgroup-processable nil)
707   (save-excursion
708     (let ((data gnus-newsgroup-data)
709           number)
710       (while data
711         (when (and (not (memq (setq number (gnus-data-number (car data)))
712                               gnus-newsgroup-processable))
713                    (vectorp (gnus-data-header (car data))))
714           (gnus-summary-goto-subject number)
715           (gnus-uu-mark-series))
716         (setq data (cdr data)))))
717   (gnus-summary-position-point))
718
719 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
720
721 (defun gnus-uu-decode-postscript (&optional n)
722   "Gets postscript of the current article."
723   (interactive "P")
724   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
725
726 (defun gnus-uu-decode-postscript-view (&optional n)
727   "Gets and views the current article."
728   (interactive "P")
729   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
730     (gnus-uu-decode-postscript n)))
731
732 (defun gnus-uu-decode-postscript-and-save (n dir)
733   "Extracts postscript and saves the current article."
734   (interactive
735    (list current-prefix-arg
736          (file-name-as-directory
737           (read-file-name "Save in dir: "
738                           gnus-uu-default-dir
739                           gnus-uu-default-dir t))))
740   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
741                               n dir nil nil t))
742
743 (defun gnus-uu-decode-postscript-and-save-view (n dir)
744   "Decodes, views and saves the resulting file."
745   (interactive
746    (list current-prefix-arg
747          (read-file-name "Where do you want to save the file(s)? "
748                          gnus-uu-default-dir
749                          gnus-uu-default-dir t)))
750   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
751     (gnus-uu-decode-postscript-and-save n dir)))
752
753
754 ;; Internal functions.
755
756 (defun gnus-uu-decode-with-method (method n &optional save not-insert
757                                           scan cdir)
758   (gnus-uu-initialize scan)
759   (when save
760     (setq gnus-uu-default-dir save))
761   ;; Create the directory we save to.
762   (when (and scan cdir save
763              (not (file-exists-p save)))
764     (make-directory save t))
765   (let ((articles (gnus-uu-get-list-of-articles n))
766         files)
767     (setq files (gnus-uu-grab-articles articles method t))
768     (let ((gnus-current-article (car articles)))
769       (when scan
770         (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
771     (when save
772       (gnus-uu-save-files files save))
773     (when (eq gnus-uu-do-not-unpack-archives nil)
774       (setq files (gnus-uu-unpack-files files)))
775     (setq files (nreverse (gnus-uu-get-actions files)))
776     (or not-insert (not gnus-insert-pseudo-articles)
777         (gnus-summary-insert-pseudos files save))))
778
779 (defun gnus-uu-scan-directory (dir &optional rec)
780   "Return a list of all files under DIR."
781   (let ((files (directory-files dir t))
782         out file)
783     (while (setq file (pop files))
784       (unless (member (file-name-nondirectory file) '("." ".."))
785         (push (list (cons 'name file)
786                     (cons 'article gnus-current-article))
787               out)
788         (when (file-directory-p file)
789           (setq out (nconc (gnus-uu-scan-directory file t) out)))))
790     (if rec
791         out
792       (nreverse out))))
793
794 (defun gnus-uu-save-files (files dir)
795   "Save FILES in DIR."
796   (let ((len (length files))
797         (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
798         to-file file fromdir)
799     (while (setq file (cdr (assq 'name (pop files))))
800       (when (file-exists-p file)
801         (string-match reg file)
802         (setq fromdir (substring file (match-end 0)))
803         (if (file-directory-p file)
804             (gnus-make-directory (concat dir fromdir))
805           (setq to-file (concat dir fromdir))
806           (when (or (not (file-exists-p to-file))
807                     (eq gnus-uu-be-dangerous t)
808                     (and gnus-uu-be-dangerous
809                          (gnus-y-or-n-p (format "%s exists; overwrite? "
810                                                 to-file))))
811             (copy-file file to-file t t)))))
812     (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
813
814 ;; Functions for saving and possibly digesting articles without
815 ;; any decoding.
816
817 ;; Function called by gnus-uu-grab-articles to treat each article.
818 (defun gnus-uu-save-article (buffer in-state)
819   (cond
820    (gnus-uu-save-separate-articles
821     (save-excursion
822       (set-buffer buffer)
823       (gnus-write-buffer-as-coding-system
824        nnheader-text-coding-system
825        (concat gnus-uu-saved-article-name gnus-current-article))
826       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
827             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
828                                                  'begin 'end))
829             ((eq in-state 'last) (list 'end))
830             (t (list 'middle)))))
831    ((not gnus-uu-save-in-digest)
832     (save-excursion
833       (set-buffer buffer)
834       (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
835       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
836             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
837                                                  'begin 'end))
838             ((eq in-state 'last) (list 'end))
839             (t (list 'middle)))))
840    (t
841     (let ((header (gnus-summary-article-header)))
842       (push (cons (mail-header-from header)
843                   (mail-header-subject header))
844             gnus-uu-digest-from-subject))
845     (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
846           beg subj headers headline sorthead body end-string state)
847       (if (or (eq in-state 'first)
848               (eq in-state 'first-and-last))
849           (progn
850             (setq state (list 'begin))
851             (save-excursion
852               (set-buffer (gnus-get-buffer-create "*gnus-uu-body*"))
853               (erase-buffer))
854             (save-excursion
855               (set-buffer (gnus-get-buffer-create "*gnus-uu-pre*"))
856               (erase-buffer)
857               (unless gnus-uu-digest-buffer
858                 (insert (format "From: %s\nSubject: %s Digest\n\n" name name)))
859               (insert "Topics:\n")))
860         (when (not (eq in-state 'end))
861           (setq state (list 'middle))))
862       (save-excursion
863         (set-buffer "*gnus-uu-body*")
864         (goto-char (setq beg (point-max)))
865         (save-excursion
866           (save-restriction
867             (set-buffer buffer)
868             (let (buffer-read-only)
869               (gnus-set-text-properties (point-min) (point-max) nil)
870               ;; These two are necessary for XEmacs 19.12 fascism.
871               (put-text-property (point-min) (point-max) 'invisible nil)
872               (put-text-property (point-min) (point-max) 'intangible nil))
873             (goto-char (point-min))
874             (re-search-forward "\n\n")
875             (unless gnus-uu-digest-buffer
876               ;; Quote all 30-dash lines.
877               (save-excursion
878                 (while (re-search-forward "^-" nil t)
879                   (beginning-of-line)
880                   (delete-char 1)
881                   (insert "- "))))
882             (setq body (buffer-substring (1- (point)) (point-max)))
883             (narrow-to-region (point-min) (point))
884             (if (not (setq headers gnus-uu-digest-headers))
885                 (setq sorthead (buffer-substring (point-min) (point-max)))
886               (while headers
887                 (setq headline (car headers))
888                 (setq headers (cdr headers))
889                 (goto-char (point-min))
890                 (while (re-search-forward headline nil t)
891                   (setq sorthead
892                         (concat sorthead
893                                 (buffer-substring
894                                  (match-beginning 0)
895                                  (or (and (re-search-forward "^[^ \t]" nil t)
896                                           (1- (point)))
897                                      (progn (forward-line 1) (point)))))))))
898             (widen)))
899         (insert message-forward-start-separator)
900         (insert sorthead) (goto-char (point-max))
901         (insert body) (goto-char (point-max))
902         (goto-char beg)
903         (when (re-search-forward "^Subject:" nil t)
904           (setq subj (nnheader-decode-subject
905                       (buffer-substring (match-end 0) (std11-field-end))))
906           (save-excursion
907             (set-buffer "*gnus-uu-pre*")
908             (insert (format "   %s\n" subj)))))
909       (when (or (eq in-state 'last)
910                 (eq in-state 'first-and-last))
911         (if gnus-uu-digest-buffer
912             (with-current-buffer gnus-uu-digest-buffer
913               (erase-buffer)
914               (insert-buffer "*gnus-uu-pre*")
915               (goto-char (point-max))
916               (insert-buffer "*gnus-uu-body*"))
917           (save-excursion
918             (set-buffer "*gnus-uu-pre*")
919             (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
920             (if gnus-uu-digest-buffer
921                 (with-current-buffer gnus-uu-digest-buffer
922                   (erase-buffer)
923                   (insert-buffer "*gnus-uu-pre*"))
924               (gnus-write-buffer-as-coding-system
925                nnheader-text-coding-system gnus-uu-saved-article-name)))
926           (save-excursion
927             (set-buffer "*gnus-uu-body*")
928             (goto-char (point-max))
929             (insert
930              (concat (setq end-string (format "End of %s Digest" name))
931                      "\n"))
932             (insert (concat (make-string (length end-string) ?*) "\n"))
933             (if gnus-uu-digest-buffer
934                 (with-current-buffer gnus-uu-digest-buffer
935                   (goto-char (point-max))
936                   (insert-buffer "*gnus-uu-body*"))
937               (let ((file-name-coding-system nnmail-pathname-coding-system)
938                     (pathname-coding-system nnmail-pathname-coding-system))
939                 (write-region-as-coding-system
940                  nnheader-text-coding-system
941                  (point-min) (point-max) gnus-uu-saved-article-name t)))))
942         (gnus-kill-buffer "*gnus-uu-pre*")
943         (gnus-kill-buffer "*gnus-uu-body*")
944         (push 'end state))
945       (if (memq 'begin state)
946           (cons gnus-uu-saved-article-name state)
947         state)))))
948
949 ;; Binhex treatment - not very advanced.
950
951 (defvar gnus-uu-binhex-body-line
952   "^[^:]...............................................................$")
953 (defvar gnus-uu-binhex-begin-line
954   "^:...............................................................$")
955 (defvar gnus-uu-binhex-end-line
956   ":$")
957
958 (defun gnus-uu-binhex-article (buffer in-state)
959   (let (state start-char)
960     (save-excursion
961       (set-buffer buffer)
962       (widen)
963       (goto-char (point-min))
964       (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
965         (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
966           (setq state (list 'wrong-type))))
967
968       (if (memq 'wrong-type state)
969           ()
970         (beginning-of-line)
971         (setq start-char (point))
972         (if (looking-at gnus-uu-binhex-begin-line)
973             (progn
974               (setq state (list 'begin))
975               (write-region 1 1 gnus-uu-binhex-article-name))
976           (setq state (list 'middle)))
977         (goto-char (point-max))
978         (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
979                                     gnus-uu-binhex-end-line)
980                             nil t)
981         (when (looking-at gnus-uu-binhex-end-line)
982           (setq state (if (memq 'begin state)
983                           (cons 'end state)
984                         (list 'end))))
985         (beginning-of-line)
986         (forward-line 1)
987         (when (file-exists-p gnus-uu-binhex-article-name)
988           (write-region-as-binary start-char (point)
989                                   gnus-uu-binhex-article-name 'append))))
990     (if (memq 'begin state)
991         (cons gnus-uu-binhex-article-name state)
992       state)))
993
994 ;; PostScript
995
996 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
997   (let ((state (list 'ok))
998         start-char end-char file-name)
999     (save-excursion
1000       (set-buffer process-buffer)
1001       (goto-char (point-min))
1002       (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
1003           (setq state (list 'wrong-type))
1004         (beginning-of-line)
1005         (setq start-char (point))
1006         (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
1007             (setq state (list 'wrong-type))
1008           (setq end-char (point))
1009           (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1010           (insert-buffer-substring process-buffer start-char end-char)
1011           (setq file-name (concat gnus-uu-work-dir
1012                                   (cdr gnus-article-current) ".ps"))
1013           (write-region (point-min) (point-max) file-name)
1014           (setq state (list file-name 'begin 'end)))))
1015     state))
1016
1017
1018 ;; Find actions.
1019
1020 (defun gnus-uu-get-actions (files)
1021   (let ((ofiles files)
1022         action name)
1023     (while files
1024       (setq name (cdr (assq 'name (car files))))
1025       (and
1026        (setq action (gnus-uu-get-action name))
1027        (setcar files (nconc (list (if (string= action "gnus-uu-archive")
1028                                       (cons 'action "file")
1029                                     (cons 'action action))
1030                                   (cons 'execute (gnus-uu-command
1031                                                   action name)))
1032                             (car files))))
1033       (setq files (cdr files)))
1034     ofiles))
1035
1036 (defun gnus-uu-get-action (file-name)
1037   (let (action)
1038     (setq action
1039           (gnus-uu-choose-action
1040            file-name
1041            (append
1042             gnus-uu-user-view-rules
1043             (if gnus-uu-ignore-default-view-rules
1044                 nil
1045               gnus-uu-default-view-rules)
1046             gnus-uu-user-view-rules-end)))
1047     (when (and (not (string= (or action "") "gnus-uu-archive"))
1048                gnus-uu-view-with-metamail)
1049       (when (setq action
1050                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
1051         (setq action (format "metamail -d -b -c \"%s\"" action))))
1052     action))
1053
1054
1055 ;; Functions for treating subjects and collecting series.
1056
1057 (defun gnus-uu-reginize-string (string)
1058   ;; Takes a string and puts a \ in front of every special character;
1059   ;; replaces the last thing that looks like "2/3" with "[0-9]+/3"
1060   ;; or, if it can't find something like that, tries "2 of 3", then
1061   ;; finally just replaces the next to last number with "[0-9]+".
1062   (save-excursion
1063     (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1064     (buffer-disable-undo)
1065     (erase-buffer)
1066     (insert (regexp-quote string))
1067
1068     (setq case-fold-search nil)
1069
1070     (end-of-line)
1071     (if (re-search-backward "\\([^0-9]\\)[0-9]+/\\([0-9]+\\)" nil t)
1072         (replace-match "\\1[0-9]+/\\2")
1073
1074       (end-of-line)
1075       (if (re-search-backward "\\([^0-9]\\)[0-9]+[ \t]*of[ \t]*\\([0-9]+\\)"
1076                               nil t)
1077           (replace-match "\\1[0-9]+ of \\2")
1078
1079         (end-of-line)
1080         (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1081                                 nil t)
1082             (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1083
1084     (goto-char 1)
1085     (while (re-search-forward "[ \t]+" nil t)
1086       (replace-match "[ \t]+" t t))
1087
1088     (buffer-substring 1 (point-max))))
1089
1090 (defun gnus-uu-get-list-of-articles (n)
1091   ;; If N is non-nil, the article numbers of the N next articles
1092   ;; will be returned.
1093   ;; If any articles have been marked as processable, they will be
1094   ;; returned.
1095   ;; Failing that, articles that have subjects that are part of the
1096   ;; same "series" as the current will be returned.
1097   (let (articles)
1098     (cond
1099      (n
1100       (setq n (prefix-numeric-value n))
1101       (let ((backward (< n 0))
1102             (n (abs n)))
1103         (save-excursion
1104           (while (and (> n 0)
1105                       (push (gnus-summary-article-number)
1106                             articles)
1107                       (gnus-summary-search-forward nil nil backward))
1108             (setq n (1- n))))
1109         (nreverse articles)))
1110      (gnus-newsgroup-processable
1111       (reverse gnus-newsgroup-processable))
1112      (t
1113       (gnus-uu-find-articles-matching)))))
1114
1115 (defun gnus-uu-string< (l1 l2)
1116   (string< (car l1) (car l2)))
1117
1118 (defun gnus-uu-find-articles-matching
1119   (&optional subject only-unread do-not-translate)
1120   ;; Finds all articles that matches the regexp SUBJECT.  If it is
1121   ;; nil, the current article name will be used.  If ONLY-UNREAD is
1122   ;; non-nil, only unread articles are chosen.  If DO-NOT-TRANSLATE is
1123   ;; non-nil, article names are not equalized before sorting.
1124   (let ((subject (or subject
1125                      (gnus-uu-reginize-string (gnus-summary-article-subject))))
1126         list-of-subjects)
1127     (save-excursion
1128       (when subject
1129         ;; Collect all subjects matching subject.
1130         (let ((case-fold-search t)
1131               (data gnus-newsgroup-data)
1132               subj mark d)
1133           (while data
1134             (setq d (pop data))
1135             (and (not (gnus-data-pseudo-p d))
1136                  (or (not only-unread)
1137                      (= (setq mark (gnus-data-mark d))
1138                         gnus-unread-mark)
1139                      (= mark gnus-ticked-mark)
1140                      (= mark gnus-dormant-mark))
1141                  (setq subj (mail-header-subject (gnus-data-header d)))
1142                  (string-match subject subj)
1143                  (push (cons subj (gnus-data-number d))
1144                        list-of-subjects))))
1145
1146         ;; Expand numbers, sort, and return the list of article
1147         ;; numbers.
1148         (mapcar (lambda (sub) (cdr sub))
1149                 (sort (gnus-uu-expand-numbers
1150                        list-of-subjects
1151                        (not do-not-translate))
1152                       'gnus-uu-string<))))))
1153
1154 (defun gnus-uu-expand-numbers (string-list &optional translate)
1155   ;; Takes a list of strings and "expands" all numbers in all the
1156   ;; strings.  That is, this function makes all numbers equal length by
1157   ;; prepending lots of zeroes before each number.  This is to ease later
1158   ;; sorting to find out what sequence the articles are supposed to be
1159   ;; decoded in.  Returns the list of expanded strings.
1160   (let ((out-list string-list)
1161         string)
1162     (save-excursion
1163       (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1164       (buffer-disable-undo)
1165       (while string-list
1166         (erase-buffer)
1167         (insert (caar string-list))
1168         ;; Translate multiple spaces to one space.
1169         (goto-char (point-min))
1170         (while (re-search-forward "[ \t]+" nil t)
1171           (replace-match " "))
1172         ;; Translate all characters to "a".
1173         (goto-char (point-min))
1174         (when translate
1175           (while (re-search-forward "[A-Za-z]" nil t)
1176             (replace-match "a" t t)))
1177         ;; Expand numbers.
1178         (goto-char (point-min))
1179         (while (re-search-forward "[0-9]+" nil t)
1180           (replace-match
1181            (format "%06d"
1182                    (string-to-int (buffer-substring
1183                                    (match-beginning 0) (match-end 0))))))
1184         (setq string (buffer-substring 1 (point-max)))
1185         (setcar (car string-list) string)
1186         (setq string-list (cdr string-list))))
1187     out-list))
1188
1189
1190 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1191 ;; function.  It takes a list of articles to be grabbed and a function
1192 ;; to apply to each article.
1193 ;;
1194 ;; The function to be called should take two parameters.  The first
1195 ;; parameter is the article buffer.  The function should leave the
1196 ;; result, if any, in this buffer.  Most treatment functions will just
1197 ;; generate files...
1198 ;;
1199 ;; The second parameter is the state of the list of articles, and can
1200 ;; have four values: `first', `middle', `last' and `first-and-last'.
1201 ;;
1202 ;; The function should return a list.  The list may contain the
1203 ;; following symbols:
1204 ;; `error' if an error occurred
1205 ;; `begin' if the beginning of an encoded file has been received
1206 ;;   If the list returned contains a `begin', the first element of
1207 ;;   the list *must* be a string with the file name of the decoded
1208 ;;   file.
1209 ;; `end' if the end of an encoded file has been received
1210 ;; `middle' if the article was a body part of an encoded file
1211 ;; `wrong-type' if the article was not a part of an encoded file
1212 ;; `ok', which can be used everything is ok
1213
1214 (defvar gnus-uu-has-been-grabbed nil)
1215
1216 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1217   (let (art)
1218     (if (not (and gnus-uu-has-been-grabbed
1219                   gnus-uu-unmark-articles-not-decoded))
1220         ()
1221       (when dont-unmark-last-article
1222         (setq art (car gnus-uu-has-been-grabbed))
1223         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1224       (while gnus-uu-has-been-grabbed
1225         (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1226         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1227       (when dont-unmark-last-article
1228         (setq gnus-uu-has-been-grabbed (list art))))))
1229
1230 ;; This function takes a list of articles and a function to apply to
1231 ;; each article grabbed.
1232 ;;
1233 ;; This function returns a list of files decoded if the grabbing and
1234 ;; the process-function has been successful and nil otherwise.
1235 (defun gnus-uu-grab-articles (articles process-function
1236                                        &optional sloppy limit no-errors)
1237   (let ((state 'first)
1238         (gnus-asynchronous nil)
1239         (gnus-inhibit-treatment t)
1240         has-been-begin article result-file result-files process-state
1241         gnus-summary-display-article-function
1242         gnus-article-display-hook gnus-article-prepare-hook gnus-display-mime-function
1243         article-series files)
1244
1245     (while (and articles
1246                 (not (memq 'error process-state))
1247                 (or sloppy
1248                     (not (memq 'end process-state))))
1249
1250       (setq article (pop articles))
1251       (when (vectorp (gnus-summary-article-header article))
1252         (push article article-series)
1253
1254         (unless articles
1255           (if (eq state 'first)
1256               (setq state 'first-and-last)
1257             (setq state 'last)))
1258
1259         (let ((part (gnus-uu-part-number article)))
1260           (gnus-message 6 "Getting article %d%s..."
1261                         article (if (string= part "") "" (concat ", " part))))
1262         (gnus-summary-display-article article)
1263
1264         ;; Push the article to the processing function.
1265         (save-excursion
1266           (set-buffer gnus-original-article-buffer)
1267           (let ((buffer-read-only nil))
1268             (save-excursion
1269               (set-buffer gnus-summary-buffer)
1270               (setq process-state
1271                     (funcall process-function
1272                              gnus-original-article-buffer state)))))
1273
1274         (gnus-summary-remove-process-mark article)
1275
1276         ;; If this is the beginning of a decoded file, we push it
1277         ;; on to a list.
1278         (when (or (memq 'begin process-state)
1279                   (and (or (eq state 'first)
1280                            (eq state 'first-and-last))
1281                        (memq 'ok process-state)))
1282           (when has-been-begin
1283             ;; If there is a `result-file' here, that means that the
1284             ;; file was unsuccessfully decoded, so we delete it.
1285             (when (and result-file
1286                        (file-exists-p result-file)
1287                        (not gnus-uu-be-dangerous)
1288                        (or (eq gnus-uu-be-dangerous t)
1289                            (gnus-y-or-n-p
1290                             (format "Delete unsuccessfully decoded file %s"
1291                                     result-file))))
1292               (delete-file result-file)))
1293           (when (memq 'begin process-state)
1294             (setq result-file (car process-state)))
1295           (setq has-been-begin t))
1296
1297         ;; Check whether we have decoded one complete file.
1298         (when (memq 'end process-state)
1299           (setq article-series nil)
1300           (setq has-been-begin nil)
1301           (if (stringp result-file)
1302               (setq files (list result-file))
1303             (setq files result-file))
1304           (setq result-file (car files))
1305           (while files
1306             (push (list (cons 'name (pop files))
1307                         (cons 'article article))
1308                   result-files))
1309           ;; Allow user-defined functions to be run on this file.
1310           (when gnus-uu-grabbed-file-functions
1311             (let ((funcs gnus-uu-grabbed-file-functions))
1312               (unless (listp funcs)
1313                 (setq funcs (list funcs)))
1314               (while funcs
1315                 (funcall (pop funcs) result-file))))
1316           (setq result-file nil)
1317           ;; Check whether we have decoded enough articles.
1318           (and limit (= (length result-files) limit)
1319                (setq articles nil)))
1320
1321         ;; If this is the last article to be decoded, and
1322         ;; we still haven't reached the end, then we delete
1323         ;; the partially decoded file.
1324         (and (or (eq state 'last) (eq state 'first-and-last))
1325              (not (memq 'end process-state))
1326              result-file
1327              (file-exists-p result-file)
1328              (not gnus-uu-be-dangerous)
1329              (or (eq gnus-uu-be-dangerous t)
1330                  (gnus-y-or-n-p
1331                   (format "Delete incomplete file %s? " result-file)))
1332              (delete-file result-file))
1333
1334         ;; If this was a file of the wrong sort, then
1335         (when (and (or (memq 'wrong-type process-state)
1336                        (memq 'error process-state))
1337                    gnus-uu-unmark-articles-not-decoded)
1338           (gnus-summary-tick-article article t))
1339
1340         ;; Set the new series state.
1341         (if (and (not has-been-begin)
1342                  (not sloppy)
1343                  (or (memq 'end process-state)
1344                      (memq 'middle process-state)))
1345             (progn
1346               (setq process-state (list 'error))
1347               (gnus-message 2 "No begin part at the beginning")
1348               (sleep-for 2))
1349           (setq state 'middle)))
1350
1351       ;; When there are no result-files, then something must be wrong.
1352       (if result-files
1353           (message "")
1354         (cond
1355          ((not has-been-begin)
1356           (gnus-message 2 "Wrong type file"))
1357          ((memq 'error process-state)
1358           (gnus-message 2 "An error occurred during decoding"))
1359          ((not (or (memq 'ok process-state)
1360                    (memq 'end process-state)))
1361           (gnus-message 2 "End of articles reached before end of file")))
1362         ;; Make unsuccessfully decoded articles unread.
1363         (when gnus-uu-unmark-articles-not-decoded
1364           (while article-series
1365             (gnus-summary-tick-article (pop article-series) t)))))
1366
1367     result-files))
1368
1369 (defun gnus-uu-grab-view (file)
1370   "View FILE using the gnus-uu methods."
1371   (let ((action (gnus-uu-get-action file)))
1372     (gnus-execute-command
1373      (if (string-match "%" action)
1374          (format action file)
1375        (concat action " " file))
1376      (eq gnus-view-pseudos 'not-confirm))))
1377
1378 (defun gnus-uu-grab-move (file)
1379   "Move FILE to somewhere."
1380   (when gnus-uu-default-dir
1381     (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1382                            (file-name-nondirectory file))))
1383       (cond ((fboundp 'make-symbolic-link)
1384              (rename-file file to-file)
1385              (unless (file-exists-p file)
1386                (make-symbolic-link to-file file)))
1387             (t
1388              (copy-file file to-file))))))
1389
1390 (defun gnus-uu-part-number (article)
1391   (let* ((header (gnus-summary-article-header article))
1392          (subject (and header (mail-header-subject header)))
1393          (part nil))
1394     (if subject
1395         (while (string-match "[0-9]+/[0-9]+\\|[0-9]+[ \t]+of[ \t]+[0-9]+"
1396                              subject)
1397           (setq part (match-string 0 subject))
1398           (setq subject (substring subject (match-end 0)))))
1399     (or part
1400         (while (string-match "\\([0-9]+\\)[^0-9]+\\([0-9]+\\)" subject)
1401           (setq part (match-string 0 subject))
1402           (setq subject (substring subject (match-end 0)))))
1403     (or part "")))
1404
1405 (defun gnus-uu-uudecode-sentinel (process event)
1406   (delete-process (get-process process)))
1407
1408 (defun gnus-uu-uustrip-article (process-buffer in-state)
1409   ;; Uudecodes a file asynchronously.
1410   (save-excursion
1411     (set-buffer process-buffer)
1412     (let ((state (list 'wrong-type))
1413           process-connection-type case-fold-search buffer-read-only
1414           files start-char)
1415       (goto-char (point-min))
1416
1417       ;; Deal with ^M at the end of the lines.
1418       (when gnus-uu-kill-carriage-return
1419         (save-excursion
1420           (while (search-forward "\r" nil t)
1421             (delete-backward-char 1))))
1422
1423       (while (or (re-search-forward gnus-uu-begin-string nil t)
1424                  (re-search-forward gnus-uu-body-line nil t))
1425         (setq state (list 'ok))
1426         ;; Ok, we are at the first uucoded line.
1427         (beginning-of-line)
1428         (setq start-char (point))
1429
1430         (if (not (looking-at gnus-uu-begin-string))
1431             (setq state (list 'middle))
1432           ;; This is the beginning of a uuencoded article.
1433           ;; We replace certain characters that could make things messy.
1434           (setq gnus-uu-file-name
1435                 (let ((nnheader-file-name-translation-alist
1436                        '((?/ . ?,) (?  . ?_) (?* . ?_) (?$ . ?_))))
1437                   (nnheader-translate-file-chars (match-string 1))))
1438           (replace-match (concat "begin 644 " gnus-uu-file-name) t t)
1439
1440           ;; Remove any non gnus-uu-body-line right after start.
1441           (forward-line 1)
1442           (while (and (not (eobp))
1443                       (not (looking-at gnus-uu-body-line)))
1444             (gnus-delete-line))
1445
1446           ;; If a process is running, we kill it.
1447           (when (and gnus-uu-uudecode-process
1448                      (memq (process-status gnus-uu-uudecode-process)
1449                            '(run stop)))
1450             (delete-process gnus-uu-uudecode-process)
1451             (gnus-uu-unmark-list-of-grabbed t))
1452
1453           ;; Start a new uudecoding process.
1454           (let ((cdir default-directory))
1455             (unwind-protect
1456                 (progn
1457                   (cd gnus-uu-work-dir)
1458                   (setq gnus-uu-uudecode-process
1459                         (start-process
1460                          "*uudecode*"
1461                          (gnus-get-buffer-create gnus-uu-output-buffer-name)
1462                          shell-file-name shell-command-switch
1463                          (format "cd %s %s uudecode" gnus-uu-work-dir
1464                                  gnus-shell-command-separator))))
1465               (cd cdir)))
1466           (set-process-sentinel
1467            gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1468           (setq state (list 'begin))
1469           (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1470
1471         ;; We look for the end of the thing to be decoded.
1472         (if (re-search-forward gnus-uu-end-string nil t)
1473             (push 'end state)
1474           (goto-char (point-max))
1475           (re-search-backward gnus-uu-body-line nil t))
1476
1477         (forward-line 1)
1478
1479         (when gnus-uu-uudecode-process
1480           (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1481             ;; Try to correct mishandled uucode.
1482             (when gnus-uu-correct-stripped-uucode
1483               (gnus-uu-check-correct-stripped-uucode start-char (point)))
1484             (gnus-run-hooks 'gnus-uu-pre-uudecode-hook)
1485
1486             ;; Send the text to the process.
1487             (condition-case nil
1488                 (process-send-region
1489                  gnus-uu-uudecode-process start-char (point))
1490               (error
1491                (progn
1492                  (delete-process gnus-uu-uudecode-process)
1493                  (gnus-message 2 "gnus-uu: Couldn't uudecode")
1494                  (setq state (list 'wrong-type)))))
1495
1496             (if (memq 'end state)
1497                 (progn
1498                   ;; Send an EOF, just in case.
1499                   (ignore-errors
1500                     (process-send-eof gnus-uu-uudecode-process))
1501                   (while (memq (process-status gnus-uu-uudecode-process)
1502                                '(open run))
1503                     (accept-process-output gnus-uu-uudecode-process 1)))
1504               (when (or (not gnus-uu-uudecode-process)
1505                         (not (memq (process-status gnus-uu-uudecode-process)
1506                                    '(run stop))))
1507                 (setq state (list 'wrong-type)))))))
1508
1509       (if (memq 'begin state)
1510           (cons (if (= (length files) 1) (car files) files) state)
1511         state))))
1512
1513 (defvar gnus-uu-unshar-warning
1514   "*** WARNING ***
1515
1516 Shell archives are an archaic method of bundling files for distribution
1517 across computer networks.  During the unpacking process, arbitrary commands
1518 are executed on your system, and all kinds of nasty things can happen.
1519 Please examine the archive very carefully before you instruct Emacs to
1520 unpack it.  You can browse the archive buffer using \\[scroll-other-window].
1521
1522 If you are unsure what to do, please answer \"no\"."
1523   "Text of warning message displayed by `gnus-uu-unshar-article'.
1524 Make sure that this text consists only of few text lines.  Otherwise,
1525 Gnus might fail to display all of it.")
1526
1527
1528 ;; This function is used by `gnus-uu-grab-articles' to treat
1529 ;; a shared article.
1530 (defun gnus-uu-unshar-article (process-buffer in-state)
1531   (let ((state (list 'ok))
1532         start-char)
1533     (save-excursion
1534       (set-buffer process-buffer)
1535       (goto-char (point-min))
1536       (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1537           (setq state (list 'wrong-type))
1538         (save-window-excursion
1539           (save-excursion
1540             (switch-to-buffer (current-buffer))
1541             (delete-other-windows)
1542             (let ((buffer (get-buffer-create (generate-new-buffer-name
1543                                               "*Warning*"))))
1544               (unless
1545                   (unwind-protect
1546                       (with-current-buffer buffer
1547                         (insert (substitute-command-keys 
1548                                  gnus-uu-unshar-warning))
1549                         (goto-char (point-min))
1550                         (display-buffer buffer)
1551                         (yes-or-no-p "This is a shell archive, unshar it? "))
1552                     (kill-buffer buffer))
1553                 (setq state (list 'error))))))
1554         (unless (memq 'error state)
1555           (beginning-of-line)
1556           (setq start-char (point))
1557           (call-process-region
1558            start-char (point-max) shell-file-name nil
1559            (gnus-get-buffer-create gnus-uu-output-buffer-name) nil
1560            shell-command-switch
1561            (concat "cd " gnus-uu-work-dir " "
1562                    gnus-shell-command-separator  " sh")))))
1563     state))
1564
1565 ;; Returns the name of what the shar file is going to unpack.
1566 (defun gnus-uu-find-name-in-shar ()
1567   (let ((oldpoint (point))
1568         res)
1569     (goto-char (point-min))
1570     (when (re-search-forward gnus-uu-shar-name-marker nil t)
1571       (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1572     (goto-char oldpoint)
1573     res))
1574
1575 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1576 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1577 ;; found, or the name of the command to run if such a rule is found.
1578 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1579   (let ((action-list (copy-sequence file-action-list))
1580         (case-fold-search t)
1581         rule action)
1582     (and
1583      (unless no-ignore
1584        (and (not
1585              (and gnus-uu-ignore-files-by-name
1586                   (string-match gnus-uu-ignore-files-by-name file-name)))
1587             (not
1588              (and gnus-uu-ignore-files-by-type
1589                   (string-match gnus-uu-ignore-files-by-type
1590                                 (or (gnus-uu-choose-action
1591                                      file-name gnus-uu-ext-to-mime-list t)
1592                                     ""))))))
1593      (while (not (or (eq action-list ()) action))
1594        (setq rule (car action-list))
1595        (setq action-list (cdr action-list))
1596        (when (string-match (car rule) file-name)
1597          (setq action (cadr rule)))))
1598     action))
1599
1600 (defun gnus-uu-treat-archive (file-path)
1601   ;; Unpacks an archive.  Returns t if unpacking is successful.
1602   (let ((did-unpack t)
1603         action command dir)
1604     (setq action (gnus-uu-choose-action
1605                   file-path (append gnus-uu-user-archive-rules
1606                                     (if gnus-uu-ignore-default-archive-rules
1607                                         nil
1608                                       gnus-uu-default-archive-rules))))
1609
1610     (when (not action)
1611       (error "No unpackers for the file %s" file-path))
1612
1613     (string-match "/[^/]*$" file-path)
1614     (setq dir (substring file-path 0 (match-beginning 0)))
1615
1616     (when (member action gnus-uu-destructive-archivers)
1617       (copy-file file-path (concat file-path "~") t))
1618
1619     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1620
1621     (save-excursion
1622       (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1623       (erase-buffer))
1624
1625     (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1626
1627     (if (= 0 (call-process shell-file-name nil
1628                            (gnus-get-buffer-create gnus-uu-output-buffer-name)
1629                            nil shell-command-switch command))
1630         (message "")
1631       (gnus-message 2 "Error during unpacking of archive")
1632       (setq did-unpack nil))
1633
1634     (when (member action gnus-uu-destructive-archivers)
1635       (rename-file (concat file-path "~") file-path t))
1636
1637     did-unpack))
1638
1639 (defun gnus-uu-dir-files (dir)
1640   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1641         files file)
1642     (while dirs
1643       (if (file-directory-p (setq file (car dirs)))
1644           (setq files (append files (gnus-uu-dir-files file)))
1645         (push file files))
1646       (setq dirs (cdr dirs)))
1647     files))
1648
1649 (defun gnus-uu-unpack-files (files &optional ignore)
1650   ;; Go through FILES and look for files to unpack.
1651   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1652          (ofiles files)
1653          file did-unpack)
1654     (while files
1655       (setq file (cdr (assq 'name (car files))))
1656       (when (and (not (member file ignore))
1657                  (equal (gnus-uu-get-action (file-name-nondirectory file))
1658                         "gnus-uu-archive"))
1659         (push file did-unpack)
1660         (unless (gnus-uu-treat-archive file)
1661           (gnus-message 2 "Error during unpacking of %s" file))
1662         (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1663                (nfiles newfiles))
1664           (while nfiles
1665             (unless (member (car nfiles) totfiles)
1666               (push (list (cons 'name (car nfiles))
1667                           (cons 'original file))
1668                     ofiles))
1669             (setq nfiles (cdr nfiles)))
1670           (setq totfiles newfiles)))
1671       (setq files (cdr files)))
1672     (if did-unpack
1673         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1674       ofiles)))
1675
1676 (defun gnus-uu-ls-r (dir)
1677   (let* ((files (gnus-uu-directory-files dir t))
1678          (ofiles files))
1679     (while files
1680       (when (file-directory-p (car files))
1681         (setq ofiles (delete (car files) ofiles))
1682         (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1683       (setq files (cdr files)))
1684     ofiles))
1685
1686 ;; Various stuff
1687
1688 (defun gnus-uu-directory-files (dir &optional full)
1689   (let (files out file)
1690     (setq files (directory-files dir full))
1691     (while files
1692       (setq file (car files))
1693       (setq files (cdr files))
1694       (unless (member (file-name-nondirectory file) '("." ".."))
1695         (push file out)))
1696     (setq out (nreverse out))
1697     out))
1698
1699 (defun gnus-uu-check-correct-stripped-uucode (start end)
1700   (save-excursion
1701     (let (found beg length)
1702       (if (not gnus-uu-correct-stripped-uucode)
1703           ()
1704         (goto-char start)
1705
1706         (if (re-search-forward " \\|`" end t)
1707             (progn
1708               (goto-char start)
1709               (while (not (eobp))
1710                 (progn
1711                   (when (looking-at "\n")
1712                     (replace-match ""))
1713                   (forward-line 1))))
1714
1715           (while (not (eobp))
1716             (if (looking-at (concat gnus-uu-begin-string "\\|"
1717                                     gnus-uu-end-string))
1718                 ()
1719               (when (not found)
1720                 (beginning-of-line)
1721                 (setq beg (point))
1722                 (end-of-line)
1723                 (setq length (- (point) beg)))
1724               (setq found t)
1725               (beginning-of-line)
1726               (setq beg (point))
1727               (end-of-line)
1728               (when (not (= length (- (point) beg)))
1729                 (insert (make-string (- length (- (point) beg)) ? ))))
1730             (forward-line 1)))))))
1731
1732 (defvar gnus-uu-tmp-alist nil)
1733
1734 (defun gnus-uu-initialize (&optional scan)
1735   (let (entry)
1736     (if (and (not scan)
1737              (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1738                (if (file-exists-p (cdr entry))
1739                    (setq gnus-uu-work-dir (cdr entry))
1740                  (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1741                  nil)))
1742         t
1743       (setq gnus-uu-tmp-dir (file-name-as-directory
1744                              (expand-file-name gnus-uu-tmp-dir)))
1745       (if (not (file-directory-p gnus-uu-tmp-dir))
1746           (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1747         (when (not (file-writable-p gnus-uu-tmp-dir))
1748           (error "Temp directory %s can't be written to"
1749                  gnus-uu-tmp-dir)))
1750
1751       (setq gnus-uu-work-dir
1752             (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1753       (gnus-make-directory gnus-uu-work-dir)
1754       (set-file-modes gnus-uu-work-dir 448)
1755       (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1756       (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1757             gnus-uu-tmp-alist))))
1758
1759
1760 ;; Kills the temporary uu buffers, kills any processes, etc.
1761 (defun gnus-uu-clean-up ()
1762   (let (buf)
1763     (and gnus-uu-uudecode-process
1764          (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1765                '(stop run))
1766          (delete-process gnus-uu-uudecode-process))
1767     (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1768       (kill-buffer buf))))
1769
1770 (defun gnus-quote-arg-for-sh-or-csh (arg)
1771   (let ((pos 0) new-pos accum)
1772     ;; *** bug: we don't handle newline characters properly
1773     (while (setq new-pos (string-match "[!`\"$\\& \t{}]" arg pos))
1774       (push (substring arg pos new-pos) accum)
1775       (push "\\" accum)
1776       (push (list (aref arg new-pos)) accum)
1777       (setq pos (1+ new-pos)))
1778     (if (= pos 0)
1779         arg
1780       (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
1781
1782 ;; Inputs an action and a filename and returns a full command, making sure
1783 ;; that the filename will be treated as a single argument when the shell
1784 ;; executes the command.
1785 (defun gnus-uu-command (action file)
1786   (let ((quoted-file (gnus-quote-arg-for-sh-or-csh file)))
1787     (if (string-match "%s" action)
1788         (format action quoted-file)
1789       (concat action " " quoted-file))))
1790
1791 (defun gnus-uu-delete-work-dir (&optional dir)
1792   "Delete recursively all files and directories under `gnus-uu-work-dir'."
1793   (if dir
1794       (gnus-message 7 "Deleting directory %s..." dir)
1795     (setq dir gnus-uu-work-dir))
1796   (when (and dir
1797              (file-exists-p dir))
1798     (let ((files (directory-files dir t nil t))
1799           file)
1800       (while (setq file (pop files))
1801         (unless (member (file-name-nondirectory file) '("." ".."))
1802           (if (file-directory-p file)
1803               (gnus-uu-delete-work-dir file)
1804             (gnus-message 9 "Deleting file %s..." file)
1805             (delete-file file))))
1806       (delete-directory dir)))
1807   (gnus-message 7 ""))
1808
1809 ;; Initializing
1810
1811 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1812 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1813
1814 \f
1815
1816 ;;;
1817 ;;; uuencoded posting
1818 ;;;
1819
1820 ;; Any function that is to be used as and encoding method will take two
1821 ;; parameters: PATH-NAME and FILE-NAME.  (E.g. "/home/gaga/spiral.jpg"
1822 ;; and "spiral.jpg", respectively.) The function should return nil if
1823 ;; the encoding wasn't successful.
1824 (defcustom gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1825   "Function used for encoding binary files.
1826 There are three functions supplied with gnus-uu for encoding files:
1827 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1828 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1829 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1830 uuencode and adds MIME headers."
1831   :group 'gnus-extract-post
1832   :type '(radio (function-item gnus-uu-post-encode-uuencode)
1833                 (function-item gnus-uu-post-encode-mime)
1834                 (function-item gnus-uu-post-encode-mime-uuencode)
1835                 (function :tag "Other")))
1836
1837 (defcustom gnus-uu-post-include-before-composing nil
1838   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1839 If this variable is t, you can either include an encoded file with
1840 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article."
1841   :group 'gnus-extract-post
1842   :type 'boolean)
1843
1844 (defcustom gnus-uu-post-length 990
1845   "Maximum length of an article.
1846 The encoded file will be split into how many articles it takes to
1847 post the entire file."
1848   :group 'gnus-extract-post
1849   :type 'integer)
1850
1851 (defcustom gnus-uu-post-threaded nil
1852   "Non-nil means that gnus-uu will post the encoded file in a thread.
1853 This may not be smart, as no other decoder I have seen are able to
1854 follow threads when collecting uuencoded articles.  (Well, I have seen
1855 one package that does that - gnus-uu, but somehow, I don't think that
1856 counts...)  The default is nil."
1857   :group 'gnus-extract-post
1858   :type 'boolean)
1859
1860 (defcustom gnus-uu-post-separate-description t
1861   "Non-nil means that the description will be posted in a separate article.
1862 The first article will typically be numbered (0/x).  If this variable
1863 is nil, the description the user enters will be included at the
1864 beginning of the first article, which will be numbered (1/x).  Default
1865 is t."
1866   :group 'gnus-extract-post
1867   :type 'boolean)
1868
1869 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1870 (defvar gnus-uu-post-message-id nil)
1871 (defvar gnus-uu-post-inserted-file-name nil)
1872 (defvar gnus-uu-winconf-post-news nil)
1873
1874 (defun gnus-uu-post-news ()
1875   "Compose an article and post an encoded file."
1876   (interactive)
1877   (setq gnus-uu-post-inserted-file-name nil)
1878   (setq gnus-uu-winconf-post-news (current-window-configuration))
1879
1880   (gnus-summary-post-news)
1881
1882   (let ((map (make-sparse-keymap)))
1883     (set-keymap-parent map (current-local-map))
1884     (use-local-map map))
1885   ;;(local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1886   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1887   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1888   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1889
1890   (when gnus-uu-post-include-before-composing
1891     (save-excursion (setq gnus-uu-post-inserted-file-name
1892                           (gnus-uu-post-insert-binary)))))
1893
1894 (defun gnus-uu-post-insert-binary-in-article ()
1895   "Inserts an encoded file in the buffer.
1896 The user will be asked for a file name."
1897   (interactive)
1898   (save-excursion
1899     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1900
1901 ;; Encodes with uuencode and substitutes all spaces with backticks.
1902 (defun gnus-uu-post-encode-uuencode (path file-name)
1903   (when (gnus-uu-post-encode-file "uuencode" path file-name)
1904     (goto-char (point-min))
1905     (forward-line 1)
1906     (while (re-search-forward " " nil t)
1907       (replace-match "`"))
1908     t))
1909
1910 ;; Encodes with uuencode and adds MIME headers.
1911 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1912   (when (gnus-uu-post-encode-uuencode path file-name)
1913     (gnus-uu-post-make-mime file-name "x-uue")
1914     t))
1915
1916 ;; Encodes with base64 and adds MIME headers
1917 (defun gnus-uu-post-encode-mime (path file-name)
1918   (when (zerop (call-process shell-file-name nil t nil shell-command-switch
1919                              (format "%s %s -o %s" "mmencode" path file-name)))
1920     (gnus-uu-post-make-mime file-name "base64")
1921     t))
1922
1923 ;; Adds MIME headers.
1924 (defun gnus-uu-post-make-mime (file-name encoding)
1925   (goto-char (point-min))
1926   (insert (format "Content-Type: %s; name=\"%s\"\n"
1927                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1928                   file-name))
1929   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1930   (save-restriction
1931     (set-buffer gnus-message-buffer)
1932     (goto-char (point-min))
1933     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1934     (forward-line -1)
1935     (narrow-to-region 1 (point))
1936     (unless (mail-fetch-field "mime-version")
1937       (widen)
1938       (insert "MIME-Version: 1.0\n"))
1939     (widen)))
1940
1941 ;; Encodes a file PATH with COMMAND, leaving the result in the
1942 ;; current buffer.
1943 (defun gnus-uu-post-encode-file (command path file-name)
1944   (= 0 (call-process shell-file-name nil t nil shell-command-switch
1945                      (format "%s %s %s" command path file-name))))
1946
1947 (defun gnus-uu-post-news-inews ()
1948   "Posts the composed news article and encoded file.
1949 If no file has been included, the user will be asked for a file."
1950   (interactive)
1951
1952   (let (file-name)
1953
1954     (if gnus-uu-post-inserted-file-name
1955         (setq file-name gnus-uu-post-inserted-file-name)
1956       (setq file-name (gnus-uu-post-insert-binary)))
1957
1958     (gnus-uu-post-encoded file-name gnus-uu-post-threaded))
1959   (setq gnus-uu-post-inserted-file-name nil)
1960   (when gnus-uu-winconf-post-news
1961     (set-window-configuration gnus-uu-winconf-post-news)))
1962
1963 ;; Asks for a file to encode, encodes it and inserts the result in
1964 ;; the current buffer.  Returns the file name the user gave.
1965 (defun gnus-uu-post-insert-binary ()
1966   (let ((uuencode-buffer-name "*uuencode buffer*")
1967         file-path uubuf file-name)
1968
1969     (setq file-path (read-file-name
1970                      "What file do you want to encode? "))
1971     (when (not (file-exists-p file-path))
1972       (error "%s: No such file" file-path))
1973
1974     (goto-char (point-max))
1975     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1976
1977     ;; #### Unix-specific?
1978     (when (string-match "^~/" file-path)
1979       (setq file-path (concat "$HOME" (substring file-path 1))))
1980     ;; #### Unix-specific?
1981     (if (string-match "/[^/]*$" file-path)
1982         (setq file-name (substring file-path (1+ (match-beginning 0))))
1983       (setq file-name file-path))
1984
1985     (unwind-protect
1986         (if (save-excursion
1987               (set-buffer (setq uubuf
1988                                 (gnus-get-buffer-create uuencode-buffer-name)))
1989               (erase-buffer)
1990               (funcall gnus-uu-post-encode-method file-path file-name))
1991             (insert-buffer-substring uubuf)
1992           (error "Encoding unsuccessful"))
1993       (kill-buffer uubuf))
1994     file-name))
1995
1996 ;; Posts the article and all of the encoded file.
1997 (defun gnus-uu-post-encoded (file-name &optional threaded)
1998   (let ((send-buffer-name "*uuencode send buffer*")
1999         (encoded-buffer-name "*encoded buffer*")
2000         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
2001         (separator (concat mail-header-separator "\n\n"))
2002         uubuf length parts header i end beg
2003         beg-line minlen post-buf whole-len beg-binary end-binary)
2004
2005     (setq post-buf (current-buffer))
2006
2007     (goto-char (point-min))
2008     (when (not (re-search-forward
2009                 (if gnus-uu-post-separate-description
2010                     (concat "^" (regexp-quote gnus-uu-post-binary-separator)
2011                             "$")
2012                   (concat "^" (regexp-quote mail-header-separator) "$"))
2013                 nil t))
2014       (error "Internal error: No binary/header separator"))
2015     (beginning-of-line)
2016     (forward-line 1)
2017     (setq beg-binary (point))
2018     (setq end-binary (point-max))
2019
2020     (save-excursion
2021       (set-buffer (setq uubuf (gnus-get-buffer-create encoded-buffer-name)))
2022       (erase-buffer)
2023       (insert-buffer-substring post-buf beg-binary end-binary)
2024       (goto-char (point-min))
2025       (setq length (count-lines 1 (point-max)))
2026       (setq parts (/ length gnus-uu-post-length))
2027       (unless (< (% length gnus-uu-post-length) 4)
2028         (incf parts)))
2029
2030     (when gnus-uu-post-separate-description
2031       (forward-line -1))
2032     (delete-region (point) (point-max))
2033
2034     (goto-char (point-min))
2035     (re-search-forward
2036      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2037     (beginning-of-line)
2038     (setq header (buffer-substring 1 (point)))
2039
2040     (goto-char (point-min))
2041     (when gnus-uu-post-separate-description
2042       (when (re-search-forward "^Subject: " nil t)
2043         (end-of-line)
2044         (insert (format " (0/%d)" parts)))
2045       (save-excursion
2046         (message-send))
2047       (setq gnus-uu-post-message-id (message-fetch-field "message-id")))
2048
2049     (save-excursion
2050       (setq i 1)
2051       (setq beg 1)
2052       (while (not (> i parts))
2053         (set-buffer (gnus-get-buffer-create send-buffer-name))
2054         (erase-buffer)
2055         (insert header)
2056         (when (and threaded gnus-uu-post-message-id)
2057           (insert "References: " gnus-uu-post-message-id "\n"))
2058         (insert separator)
2059         (setq whole-len
2060               (- 62 (length (format top-string "" file-name i parts ""))))
2061         (when (> 1 (setq minlen (/ whole-len 2)))
2062           (setq minlen 1))
2063         (setq
2064          beg-line
2065          (format top-string
2066                  (make-string minlen ?-)
2067                  file-name i parts
2068                  (make-string
2069                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
2070
2071         (goto-char (point-min))
2072         (when (re-search-forward "^Subject: " nil t)
2073           (end-of-line)
2074           (insert (format " (%d/%d)" i parts)))
2075
2076         (goto-char (point-max))
2077         (save-excursion
2078           (set-buffer uubuf)
2079           (goto-char beg)
2080           (if (= i parts)
2081               (goto-char (point-max))
2082             (forward-line gnus-uu-post-length))
2083           (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
2084             (forward-line -4))
2085           (setq end (point)))
2086         (insert-buffer-substring uubuf beg end)
2087         (insert beg-line "\n")
2088         (setq beg end)
2089         (incf i)
2090         (goto-char (point-min))
2091         (re-search-forward
2092          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2093         (beginning-of-line)
2094         (forward-line 2)
2095         (when (re-search-forward
2096                (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
2097                nil t)
2098           (replace-match "")
2099           (forward-line 1))
2100         (insert beg-line)
2101         (insert "\n")
2102         (let (message-sent-message-via)
2103           (save-excursion
2104             (message-send))
2105           (setq gnus-uu-post-message-id
2106                 (concat (message-fetch-field "references") " "
2107                         (message-fetch-field "message-id"))))))
2108
2109     (gnus-kill-buffer send-buffer-name)
2110     (gnus-kill-buffer encoded-buffer-name)
2111
2112     (when (not gnus-uu-post-separate-description)
2113       (set-buffer-modified-p nil)
2114       (when (fboundp 'bury-buffer)
2115         (bury-buffer)))))
2116
2117 (provide 'gnus-uu)
2118
2119 ;; gnus-uu.el ends here