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