Fix translations.
[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     (message "%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 ()
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     (message "Marked %d articles" l))
590   (gnus-summary-position-point))
591
592 (defun gnus-uu-mark-region (beg end &optional unmark)
593   "Set the process mark on all articles between point and mark."
594   (interactive "r")
595   (save-excursion
596     (goto-char beg)
597     (while (< (point) end)
598       (if unmark
599           (gnus-summary-remove-process-mark (gnus-summary-article-number))
600         (gnus-summary-set-process-mark (gnus-summary-article-number)))
601       (forward-line 1)))
602   (gnus-summary-position-point))
603
604 (defun gnus-uu-unmark-region (beg end)
605   "Remove the process mark from all articles between point and mark."
606   (interactive "r")
607   (gnus-uu-mark-region beg end t))
608
609 (defun gnus-uu-mark-buffer ()
610   "Set the process mark on all articles in the buffer."
611   (interactive)
612   (gnus-uu-mark-region (point-min) (point-max)))
613
614 (defun gnus-uu-unmark-buffer ()
615   "Remove the process mark on all articles in the buffer."
616   (interactive)
617   (gnus-uu-mark-region (point-min) (point-max) t))
618
619 (defun gnus-uu-mark-thread ()
620   "Marks all articles downwards in this thread."
621   (interactive)
622   (gnus-save-hidden-threads
623     (let ((level (gnus-summary-thread-level)))
624       (while (and (gnus-summary-set-process-mark
625                    (gnus-summary-article-number))
626                   (zerop (gnus-summary-next-subject 1 nil t))
627                   (> (gnus-summary-thread-level) level)))))
628   (gnus-summary-position-point))
629
630 (defun gnus-uu-unmark-thread ()
631   "Unmarks all articles downwards in this thread."
632   (interactive)
633   (let ((level (gnus-summary-thread-level)))
634     (while (and (gnus-summary-remove-process-mark
635                  (gnus-summary-article-number))
636                 (zerop (gnus-summary-next-subject 1))
637                 (> (gnus-summary-thread-level) level))))
638   (gnus-summary-position-point))
639
640 (defun gnus-uu-invert-processable ()
641   "Invert the list of process-marked articles."
642   (interactive)
643   (let ((data gnus-newsgroup-data)
644         number)
645     (save-excursion
646       (while data
647         (if (memq (setq number (gnus-data-number (pop data)))
648                   gnus-newsgroup-processable)
649             (gnus-summary-remove-process-mark number)
650           (gnus-summary-set-process-mark number)))))
651   (gnus-summary-position-point))
652
653 (defun gnus-uu-mark-over (&optional score)
654   "Mark all articles with a score over SCORE (the prefix)."
655   (interactive "P")
656   (let ((score (or score gnus-summary-default-score 0))
657         (data gnus-newsgroup-data))
658     (save-excursion
659       (while data
660         (when (> (or (cdr (assq (gnus-data-number (car data))
661                                 gnus-newsgroup-scored))
662                      gnus-summary-default-score 0)
663                  score)
664           (gnus-summary-set-process-mark (caar data)))
665         (setq data (cdr data))))
666     (gnus-summary-position-point)))
667
668 (defun gnus-uu-mark-sparse ()
669   "Mark all series that have some articles marked."
670   (interactive)
671   (let ((marked (nreverse gnus-newsgroup-processable))
672         subject articles total headers)
673     (unless marked
674       (error "No articles marked with the process mark"))
675     (setq gnus-newsgroup-processable nil)
676     (save-excursion
677       (while marked
678         (and (vectorp (setq headers
679                             (gnus-summary-article-header (car marked))))
680              (setq subject (mail-header-subject headers)
681                    articles (gnus-uu-find-articles-matching
682                              (gnus-uu-reginize-string subject))
683                    total (nconc total articles)))
684         (while articles
685           (gnus-summary-set-process-mark (car articles))
686           (setcdr marked (delq (car articles) (cdr marked)))
687           (setq articles (cdr articles)))
688         (setq marked (cdr marked)))
689       (setq gnus-newsgroup-processable (nreverse total)))
690     (gnus-summary-position-point)))
691
692 (defun gnus-uu-mark-all ()
693   "Mark all articles in \"series\" order."
694   (interactive)
695   (setq gnus-newsgroup-processable nil)
696   (save-excursion
697     (let ((data gnus-newsgroup-data)
698           number)
699       (while data
700         (when (and (not (memq (setq number (gnus-data-number (car data)))
701                               gnus-newsgroup-processable))
702                    (vectorp (gnus-data-header (car data))))
703           (gnus-summary-goto-subject number)
704           (gnus-uu-mark-series))
705         (setq data (cdr data)))))
706   (gnus-summary-position-point))
707
708 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
709
710 (defun gnus-uu-decode-postscript (&optional n)
711   "Gets postscript of the current article."
712   (interactive "P")
713   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
714
715 (defun gnus-uu-decode-postscript-view (&optional n)
716   "Gets and views the current article."
717   (interactive "P")
718   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
719     (gnus-uu-decode-postscript n)))
720
721 (defun gnus-uu-decode-postscript-and-save (n dir)
722   "Extracts postscript and saves the current article."
723   (interactive
724    (list current-prefix-arg
725          (file-name-as-directory
726           (read-file-name "Save in dir: "
727                           gnus-uu-default-dir
728                           gnus-uu-default-dir t))))
729   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
730                               n dir nil nil t))
731
732 (defun gnus-uu-decode-postscript-and-save-view (n dir)
733   "Decodes, views and saves the resulting file."
734   (interactive
735    (list current-prefix-arg
736          (read-file-name "Where do you want to save the file(s)? "
737                          gnus-uu-default-dir
738                          gnus-uu-default-dir t)))
739   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
740     (gnus-uu-decode-postscript-and-save n dir)))
741
742
743 ;; Internal functions.
744
745 (defun gnus-uu-decode-with-method (method n &optional save not-insert
746                                           scan cdir)
747   (gnus-uu-initialize scan)
748   (when save
749     (setq gnus-uu-default-dir save))
750   ;; Create the directory we save to.
751   (when (and scan cdir save
752              (not (file-exists-p save)))
753     (make-directory save t))
754   (let ((articles (gnus-uu-get-list-of-articles n))
755         files)
756     (setq files (gnus-uu-grab-articles articles method t))
757     (let ((gnus-current-article (car articles)))
758       (when scan
759         (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
760     (when save
761       (gnus-uu-save-files files save))
762     (when (eq gnus-uu-do-not-unpack-archives nil)
763       (setq files (gnus-uu-unpack-files files)))
764     (setq files (nreverse (gnus-uu-get-actions files)))
765     (or not-insert (not gnus-insert-pseudo-articles)
766         (gnus-summary-insert-pseudos files save))))
767
768 (defun gnus-uu-scan-directory (dir &optional rec)
769   "Return a list of all files under DIR."
770   (let ((files (directory-files dir t))
771         out file)
772     (while (setq file (pop files))
773       (unless (member (file-name-nondirectory file) '("." ".."))
774         (push (list (cons 'name file)
775                     (cons 'article gnus-current-article))
776               out)
777         (when (file-directory-p file)
778           (setq out (nconc (gnus-uu-scan-directory file t) out)))))
779     (if rec
780         out
781       (nreverse out))))
782
783 (defun gnus-uu-save-files (files dir)
784   "Save FILES in DIR."
785   (let ((len (length files))
786         (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
787         to-file file fromdir)
788     (while (setq file (cdr (assq 'name (pop files))))
789       (when (file-exists-p file)
790         (string-match reg file)
791         (setq fromdir (substring file (match-end 0)))
792         (if (file-directory-p file)
793             (gnus-make-directory (concat dir fromdir))
794           (setq to-file (concat dir fromdir))
795           (when (or (not (file-exists-p to-file))
796                     (eq gnus-uu-be-dangerous t)
797                     (and gnus-uu-be-dangerous
798                          (gnus-y-or-n-p (format "%s exists; overwrite? "
799                                                 to-file))))
800             (copy-file file to-file t t)))))
801     (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
802
803 ;; Functions for saving and possibly digesting articles without
804 ;; any decoding.
805
806 ;; Function called by gnus-uu-grab-articles to treat each article.
807 (defun gnus-uu-save-article (buffer in-state)
808   (cond
809    (gnus-uu-save-separate-articles
810     (save-excursion
811       (set-buffer buffer)
812       (gnus-write-buffer-as-coding-system
813        nnheader-text-coding-system
814        (concat gnus-uu-saved-article-name gnus-current-article))
815       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
816             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
817                                                  'begin 'end))
818             ((eq in-state 'last) (list 'end))
819             (t (list 'middle)))))
820    ((not gnus-uu-save-in-digest)
821     (save-excursion
822       (set-buffer buffer)
823       (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
824       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
825             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
826                                                  'begin 'end))
827             ((eq in-state 'last) (list 'end))
828             (t (list 'middle)))))
829    (t
830     (let ((header (gnus-summary-article-header)))
831       (push (cons (mail-header-from header)
832                   (mail-header-subject header))
833             gnus-uu-digest-from-subject))
834     (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
835           beg subj headers headline sorthead body end-string state)
836       (if (or (eq in-state 'first)
837               (eq in-state 'first-and-last))
838           (progn
839             (setq state (list 'begin))
840             (save-excursion
841               (set-buffer (gnus-get-buffer-create "*gnus-uu-body*"))
842               (erase-buffer))
843             (save-excursion
844               (set-buffer (gnus-get-buffer-create "*gnus-uu-pre*"))
845               (erase-buffer)
846               (insert (format
847                        "Date: %s\nFrom: %s\nSubject: %s Digest\n\n"
848                        (message-make-date) name name))
849               (insert "Topics:\n")))
850         (when (not (eq in-state 'end))
851           (setq state (list 'middle))))
852       (save-excursion
853         (set-buffer "*gnus-uu-body*")
854         (goto-char (setq beg (point-max)))
855         (save-excursion
856           (save-restriction
857             (set-buffer buffer)
858             (let (buffer-read-only)
859               (set-text-properties (point-min) (point-max) nil)
860               ;; These two are necessary for XEmacs 19.12 fascism.
861               (put-text-property (point-min) (point-max) 'invisible nil)
862               (put-text-property (point-min) (point-max) 'intangible nil))
863             (goto-char (point-min))
864             (search-forward "\n\n")
865             (unless gnus-uu-digest-buffer
866               ;; Quote all 30-dash lines.
867               (save-excursion
868                 (while (re-search-forward "^-" nil t)
869                   (beginning-of-line)
870                   (delete-char 1)
871                   (insert "- "))))
872             (setq body (buffer-substring (1- (point)) (point-max)))
873             (narrow-to-region (point-min) (point))
874             (if (not (setq headers gnus-uu-digest-headers))
875                 (setq sorthead (buffer-string))
876               (while headers
877                 (setq headline (car headers))
878                 (setq headers (cdr headers))
879                 (goto-char (point-min))
880                 (while (re-search-forward headline nil t)
881                   (setq sorthead
882                         (concat sorthead
883                                 (buffer-substring
884                                  (match-beginning 0)
885                                  (or (and (re-search-forward "^[^ \t]" nil t)
886                                           (1- (point)))
887                                      (progn (forward-line 1) (point)))))))))
888             (widen)))
889         (insert message-forward-start-separator)
890         (insert sorthead) (goto-char (point-max))
891         (insert body) (goto-char (point-max))
892         (goto-char beg)
893         (when (re-search-forward "^Subject:" nil t)
894           (setq subj (nnheader-decode-subject
895                       (buffer-substring (match-end 0) (std11-field-end)))))
896         (when subj
897           (save-excursion
898             (set-buffer "*gnus-uu-pre*")
899             (insert (format "   %s\n" subj)))))
900       (when (or (eq in-state 'last)
901                 (eq in-state 'first-and-last))
902         (if gnus-uu-digest-buffer
903             (with-current-buffer gnus-uu-digest-buffer
904               (erase-buffer)
905               (insert-buffer-substring "*gnus-uu-pre*")
906               (goto-char (point-max))
907               (insert-buffer-substring "*gnus-uu-body*"))
908           (save-excursion
909             (set-buffer "*gnus-uu-pre*")
910             (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
911             (if gnus-uu-digest-buffer
912                 (with-current-buffer gnus-uu-digest-buffer
913                   (erase-buffer)
914                   (insert-buffer-substring "*gnus-uu-pre*"))
915               (gnus-write-buffer-as-coding-system
916                nnheader-text-coding-system gnus-uu-saved-article-name)))
917           (save-excursion
918             (set-buffer "*gnus-uu-body*")
919             (goto-char (point-max))
920             (insert
921              (concat (setq end-string (format "End of %s Digest" name))
922                      "\n"))
923             (insert (concat (make-string (length end-string) ?*) "\n"))
924             (if gnus-uu-digest-buffer
925                 (with-current-buffer gnus-uu-digest-buffer
926                   (goto-char (point-max))
927                   (insert-buffer-substring "*gnus-uu-body*"))
928               (let ((file-name-coding-system nnmail-pathname-coding-system))
929                 (write-region-as-coding-system
930                  nnheader-text-coding-system
931                  (point-min) (point-max) gnus-uu-saved-article-name t)))))
932         (gnus-kill-buffer "*gnus-uu-pre*")
933         (gnus-kill-buffer "*gnus-uu-body*")
934         (push 'end state))
935       (if (memq 'begin state)
936           (cons gnus-uu-saved-article-name state)
937         state)))))
938
939 ;; Binhex treatment - not very advanced.
940
941 (defvar gnus-uu-binhex-body-line
942   "^[^:]...............................................................$")
943 (defvar gnus-uu-binhex-begin-line
944   "^:...............................................................$")
945 (defvar gnus-uu-binhex-end-line
946   ":$")
947
948 (defun gnus-uu-binhex-article (buffer in-state)
949   (let (state start-char)
950     (save-excursion
951       (set-buffer buffer)
952       (widen)
953       (goto-char (point-min))
954       (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
955         (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
956           (setq state (list 'wrong-type))))
957
958       (if (memq 'wrong-type state)
959           ()
960         (beginning-of-line)
961         (setq start-char (point))
962         (if (looking-at gnus-uu-binhex-begin-line)
963             (progn
964               (setq state (list 'begin))
965               (write-region (point-min) (point-min)
966                             gnus-uu-binhex-article-name))
967           (setq state (list 'middle)))
968         (goto-char (point-max))
969         (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
970                                     gnus-uu-binhex-end-line)
971                             nil t)
972         (when (looking-at gnus-uu-binhex-end-line)
973           (setq state (if (memq 'begin state)
974                           (cons 'end state)
975                         (list 'end))))
976         (beginning-of-line)
977         (forward-line 1)
978         (when (file-exists-p gnus-uu-binhex-article-name)
979           (write-region-as-binary start-char (point)
980                                   gnus-uu-binhex-article-name 'append))))
981     (if (memq 'begin state)
982         (cons gnus-uu-binhex-article-name state)
983       state)))
984
985 ;; PostScript
986
987 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
988   (let ((state (list 'ok))
989         start-char end-char file-name)
990     (save-excursion
991       (set-buffer process-buffer)
992       (goto-char (point-min))
993       (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
994           (setq state (list 'wrong-type))
995         (beginning-of-line)
996         (setq start-char (point))
997         (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
998             (setq state (list 'wrong-type))
999           (setq end-char (point))
1000           (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1001           (insert-buffer-substring process-buffer start-char end-char)
1002           (setq file-name (concat gnus-uu-work-dir
1003                                   (cdr gnus-article-current) ".ps"))
1004           (write-region (point-min) (point-max) file-name)
1005           (setq state (list file-name 'begin 'end)))))
1006     state))
1007
1008
1009 ;; Find actions.
1010
1011 (defun gnus-uu-get-actions (files)
1012   (let ((ofiles files)
1013         action name)
1014     (while files
1015       (setq name (cdr (assq 'name (car files))))
1016       (and
1017        (setq action (gnus-uu-get-action name))
1018        (setcar files (nconc (list (if (string= action "gnus-uu-archive")
1019                                       (cons 'action "file")
1020                                     (cons 'action action))
1021                                   (cons 'execute (gnus-uu-command
1022                                                   action name)))
1023                             (car files))))
1024       (setq files (cdr files)))
1025     ofiles))
1026
1027 (defun gnus-uu-get-action (file-name)
1028   (let (action)
1029     (setq action
1030           (gnus-uu-choose-action
1031            file-name
1032            (append
1033             gnus-uu-user-view-rules
1034             (if gnus-uu-ignore-default-view-rules
1035                 nil
1036               gnus-uu-default-view-rules)
1037             gnus-uu-user-view-rules-end)))
1038     (when (and (not (string= (or action "") "gnus-uu-archive"))
1039                gnus-uu-view-with-metamail)
1040       (when (setq action
1041                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
1042         (setq action (format "metamail -d -b -c \"%s\"" action))))
1043     action))
1044
1045
1046 ;; Functions for treating subjects and collecting series.
1047
1048 (defun gnus-uu-reginize-string (string)
1049   ;; Takes a string and puts a \ in front of every special character;
1050   ;; replaces the last thing that looks like "2/3" with "[0-9]+/3"
1051   ;; or, if it can't find something like that, tries "2 of 3", then
1052   ;; finally just replaces the next to last number with "[0-9]+".
1053   (save-excursion
1054     (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1055     (buffer-disable-undo)
1056     (erase-buffer)
1057     (insert (regexp-quote string))
1058
1059     (setq case-fold-search nil)
1060
1061     (end-of-line)
1062     (if (re-search-backward "\\([^0-9]\\)[0-9]+/\\([0-9]+\\)" nil t)
1063         (replace-match "\\1[0-9]+/\\2")
1064
1065       (end-of-line)
1066       (if (re-search-backward "\\([^0-9]\\)[0-9]+[ \t]*of[ \t]*\\([0-9]+\\)"
1067                               nil t)
1068           (replace-match "\\1[0-9]+ of \\2")
1069
1070         (end-of-line)
1071         (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1072                                 nil t)
1073             (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1074
1075     (goto-char 1)
1076     (while (re-search-forward "[ \t]+" nil t)
1077       (replace-match "[ \t]+" t t))
1078
1079     (buffer-string)))
1080
1081 (defun gnus-uu-get-list-of-articles (n)
1082   ;; If N is non-nil, the article numbers of the N next articles
1083   ;; will be returned.
1084   ;; If any articles have been marked as processable, they will be
1085   ;; returned.
1086   ;; Failing that, articles that have subjects that are part of the
1087   ;; same "series" as the current will be returned.
1088   (let (articles)
1089     (cond
1090      (n
1091       (setq n (prefix-numeric-value n))
1092       (let ((backward (< n 0))
1093             (n (abs n)))
1094         (save-excursion
1095           (while (and (> n 0)
1096                       (push (gnus-summary-article-number)
1097                             articles)
1098                       (gnus-summary-search-forward nil nil backward))
1099             (setq n (1- n))))
1100         (nreverse articles)))
1101      (gnus-newsgroup-processable
1102       (reverse gnus-newsgroup-processable))
1103      (t
1104       (gnus-uu-find-articles-matching)))))
1105
1106 (defun gnus-uu-string< (l1 l2)
1107   (string< (car l1) (car l2)))
1108
1109 (defun gnus-uu-find-articles-matching
1110   (&optional subject only-unread do-not-translate)
1111   ;; Finds all articles that matches the regexp SUBJECT.  If it is
1112   ;; nil, the current article name will be used.  If ONLY-UNREAD is
1113   ;; non-nil, only unread articles are chosen.  If DO-NOT-TRANSLATE is
1114   ;; non-nil, article names are not equalized before sorting.
1115   (let ((subject (or subject
1116                      (gnus-uu-reginize-string (gnus-summary-article-subject))))
1117         list-of-subjects)
1118     (save-excursion
1119       (when subject
1120         ;; Collect all subjects matching subject.
1121         (let ((case-fold-search t)
1122               (data gnus-newsgroup-data)
1123               subj mark d)
1124           (while data
1125             (setq d (pop data))
1126             (and (not (gnus-data-pseudo-p d))
1127                  (or (not only-unread)
1128                      (= (setq mark (gnus-data-mark d))
1129                         gnus-unread-mark)
1130                      (= mark gnus-ticked-mark)
1131                      (= mark gnus-dormant-mark))
1132                  (setq subj (mail-header-subject (gnus-data-header d)))
1133                  (string-match subject subj)
1134                  (push (cons subj (gnus-data-number d))
1135                        list-of-subjects))))
1136
1137         ;; Expand numbers, sort, and return the list of article
1138         ;; numbers.
1139         (mapcar 'cdr
1140                 (sort (gnus-uu-expand-numbers
1141                        list-of-subjects
1142                        (not do-not-translate))
1143                       'gnus-uu-string<))))))
1144
1145 (defun gnus-uu-expand-numbers (string-list &optional translate)
1146   ;; Takes a list of strings and "expands" all numbers in all the
1147   ;; strings.  That is, this function makes all numbers equal length by
1148   ;; prepending lots of zeroes before each number.  This is to ease later
1149   ;; sorting to find out what sequence the articles are supposed to be
1150   ;; decoded in.  Returns the list of expanded strings.
1151   (let ((out-list string-list)
1152         string)
1153     (save-excursion
1154       (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1155       (buffer-disable-undo)
1156       (while string-list
1157         (erase-buffer)
1158         (insert (caar string-list))
1159         ;; Translate multiple spaces to one space.
1160         (goto-char (point-min))
1161         (while (re-search-forward "[ \t]+" nil t)
1162           (replace-match " "))
1163         ;; Translate all characters to "a".
1164         (goto-char (point-min))
1165         (when translate
1166           (while (re-search-forward "[A-Za-z]" nil t)
1167             (replace-match "a" t t)))
1168         ;; Expand numbers.
1169         (goto-char (point-min))
1170         (while (re-search-forward "[0-9]+" nil t)
1171           (ignore-errors
1172             (replace-match
1173              (format "%06d"
1174                      (string-to-number (buffer-substring
1175                                      (match-beginning 0) (match-end 0)))))))
1176         (setq string (buffer-substring 1 (point-max)))
1177         (setcar (car string-list) string)
1178         (setq string-list (cdr string-list))))
1179     out-list))
1180
1181
1182 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1183 ;; function.  It takes a list of articles to be grabbed and a function
1184 ;; to apply to each article.
1185 ;;
1186 ;; The function to be called should take two parameters.  The first
1187 ;; parameter is the article buffer.  The function should leave the
1188 ;; result, if any, in this buffer.  Most treatment functions will just
1189 ;; generate files...
1190 ;;
1191 ;; The second parameter is the state of the list of articles, and can
1192 ;; have four values: `first', `middle', `last' and `first-and-last'.
1193 ;;
1194 ;; The function should return a list.  The list may contain the
1195 ;; following symbols:
1196 ;; `error' if an error occurred
1197 ;; `begin' if the beginning of an encoded file has been received
1198 ;;   If the list returned contains a `begin', the first element of
1199 ;;   the list *must* be a string with the file name of the decoded
1200 ;;   file.
1201 ;; `end' if the end of an encoded file has been received
1202 ;; `middle' if the article was a body part of an encoded file
1203 ;; `wrong-type' if the article was not a part of an encoded file
1204 ;; `ok', which can be used everything is ok
1205
1206 (defvar gnus-uu-has-been-grabbed nil)
1207
1208 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1209   (let (art)
1210     (if (not (and gnus-uu-has-been-grabbed
1211                   gnus-uu-unmark-articles-not-decoded))
1212         ()
1213       (when dont-unmark-last-article
1214         (setq art (car gnus-uu-has-been-grabbed))
1215         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1216       (while gnus-uu-has-been-grabbed
1217         (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1218         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1219       (when dont-unmark-last-article
1220         (setq gnus-uu-has-been-grabbed (list art))))))
1221
1222 ;; This function takes a list of articles and a function to apply to
1223 ;; each article grabbed.
1224 ;;
1225 ;; This function returns a list of files decoded if the grabbing and
1226 ;; the process-function has been successful and nil otherwise.
1227 (defun gnus-uu-grab-articles (articles process-function
1228                                        &optional sloppy limit no-errors)
1229   (let ((state 'first)
1230         (gnus-asynchronous nil)
1231         (gnus-inhibit-treatment t)
1232         has-been-begin article result-file result-files process-state
1233         gnus-summary-display-article-function
1234         gnus-article-prepare-hook gnus-display-mime-function
1235         article-series files)
1236
1237     (while (and articles
1238                 (not (memq 'error process-state))
1239                 (or sloppy
1240                     (not (memq 'end process-state))))
1241
1242       (setq article (pop articles))
1243       (when (vectorp (gnus-summary-article-header article))
1244         (push article article-series)
1245
1246         (unless articles
1247           (if (eq state 'first)
1248               (setq state 'first-and-last)
1249             (setq state 'last)))
1250
1251         (let ((part (gnus-uu-part-number article)))
1252           (gnus-message 6 "Getting article %d%s..."
1253                         article (if (string= part "") "" (concat ", " part))))
1254         (gnus-summary-display-article article)
1255
1256         ;; Push the article to the processing function.
1257         (save-excursion
1258           (set-buffer gnus-original-article-buffer)
1259           (let ((buffer-read-only nil))
1260             (save-excursion
1261               (set-buffer gnus-summary-buffer)
1262               (setq process-state
1263                     (funcall process-function
1264                              gnus-original-article-buffer state)))))
1265
1266         (gnus-summary-remove-process-mark article)
1267
1268         ;; If this is the beginning of a decoded file, we push it
1269         ;; on to a list.
1270         (when (or (memq 'begin process-state)
1271                   (and (or (eq state 'first)
1272                            (eq state 'first-and-last))
1273                        (memq 'ok process-state)))
1274           (when has-been-begin
1275             ;; If there is a `result-file' here, that means that the
1276             ;; file was unsuccessfully decoded, so we delete it.
1277             (when (and result-file
1278                        (file-exists-p result-file)
1279                        (not gnus-uu-be-dangerous)
1280                        (or (eq gnus-uu-be-dangerous t)
1281                            (gnus-y-or-n-p
1282                             (format "Delete unsuccessfully decoded file %s"
1283                                     result-file))))
1284               (delete-file result-file)))
1285           (when (memq 'begin process-state)
1286             (setq result-file (car process-state)))
1287           (setq has-been-begin t))
1288
1289         ;; Check whether we have decoded one complete file.
1290         (when (memq 'end process-state)
1291           (setq article-series nil)
1292           (setq has-been-begin nil)
1293           (if (stringp result-file)
1294               (setq files (list result-file))
1295             (setq files result-file))
1296           (setq result-file (car files))
1297           (while files
1298             (push (list (cons 'name (pop files))
1299                         (cons 'article article))
1300                   result-files))
1301           ;; Allow user-defined functions to be run on this file.
1302           (when gnus-uu-grabbed-file-functions
1303             (let ((funcs gnus-uu-grabbed-file-functions))
1304               (unless (listp funcs)
1305                 (setq funcs (list funcs)))
1306               (while funcs
1307                 (funcall (pop funcs) result-file))))
1308           (setq result-file nil)
1309           ;; Check whether we have decoded enough articles.
1310           (and limit (= (length result-files) limit)
1311                (setq articles nil)))
1312
1313         ;; If this is the last article to be decoded, and
1314         ;; we still haven't reached the end, then we delete
1315         ;; the partially decoded file.
1316         (and (or (eq state 'last) (eq state 'first-and-last))
1317              (not (memq 'end process-state))
1318              result-file
1319              (file-exists-p result-file)
1320              (not gnus-uu-be-dangerous)
1321              (or (eq gnus-uu-be-dangerous t)
1322                  (gnus-y-or-n-p
1323                   (format "Delete incomplete file %s? " result-file)))
1324              (delete-file result-file))
1325
1326         ;; If this was a file of the wrong sort, then
1327         (when (and (or (memq 'wrong-type process-state)
1328                        (memq 'error process-state))
1329                    gnus-uu-unmark-articles-not-decoded)
1330           (gnus-summary-tick-article article t))
1331
1332         ;; Set the new series state.
1333         (if (and (not has-been-begin)
1334                  (not sloppy)
1335                  (or (memq 'end process-state)
1336                      (memq 'middle process-state)))
1337             (progn
1338               (setq process-state (list 'error))
1339               (gnus-message 2 "No begin part at the beginning")
1340               (sleep-for 2))
1341           (setq state 'middle))))
1342
1343       ;; When there are no result-files, then something must be wrong.
1344     (if result-files
1345         (message "")
1346       (cond
1347        ((not has-been-begin)
1348         (gnus-message 2 "Wrong type file"))
1349        ((memq 'error process-state)
1350         (gnus-message 2 "An error occurred during decoding"))
1351        ((not (or (memq 'ok process-state)
1352                  (memq 'end process-state)))
1353         (gnus-message 2 "End of articles reached before end of file")))
1354       ;; Make unsuccessfully decoded articles unread.
1355       (when gnus-uu-unmark-articles-not-decoded
1356         (while article-series
1357           (gnus-summary-tick-article (pop article-series) t))))
1358
1359     ;; The original article buffer is hosed, shoot it down.
1360     (gnus-kill-buffer gnus-original-article-buffer)
1361     (setq gnus-current-article nil)
1362     result-files))
1363
1364 (defun gnus-uu-grab-view (file)
1365   "View FILE using the gnus-uu methods."
1366   (let ((action (gnus-uu-get-action file)))
1367     (gnus-execute-command
1368      (if (string-match "%" action)
1369          (format action file)
1370        (concat action " " file))
1371      (eq gnus-view-pseudos 'not-confirm))))
1372
1373 (defun gnus-uu-grab-move (file)
1374   "Move FILE to somewhere."
1375   (when gnus-uu-default-dir
1376     (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1377                            (file-name-nondirectory file))))
1378       (cond ((fboundp 'make-symbolic-link)
1379              (rename-file file to-file)
1380              (unless (file-exists-p file)
1381                (make-symbolic-link to-file file)))
1382             (t
1383              (copy-file file to-file))))))
1384
1385 (defun gnus-uu-part-number (article)
1386   (let* ((header (gnus-summary-article-header article))
1387          (subject (and header (mail-header-subject header)))
1388          (part nil))
1389     (if subject
1390         (while (string-match "[0-9]+/[0-9]+\\|[0-9]+[ \t]+of[ \t]+[0-9]+"
1391                              subject)
1392           (setq part (match-string 0 subject))
1393           (setq subject (substring subject (match-end 0)))))
1394     (or part
1395         (while (string-match "[0-9]+[^0-9]+[0-9]+" subject)
1396           (setq part (match-string 0 subject))
1397           (setq subject (substring subject (match-end 0)))))
1398     (or part "")))
1399
1400 (defun gnus-uu-uudecode-sentinel (process event)
1401   (delete-process (get-process process)))
1402
1403 (defun gnus-uu-uustrip-article (process-buffer in-state)
1404   ;; Uudecodes a file asynchronously.
1405   (save-excursion
1406     (set-buffer process-buffer)
1407     (let ((state (list 'wrong-type))
1408           process-connection-type case-fold-search buffer-read-only
1409           files start-char)
1410       (goto-char (point-min))
1411
1412       ;; Deal with ^M at the end of the lines.
1413       (when gnus-uu-kill-carriage-return
1414         (save-excursion
1415           (while (search-forward "\r" nil t)
1416             (delete-backward-char 1))))
1417
1418       (while (or (re-search-forward gnus-uu-begin-string nil t)
1419                  (re-search-forward gnus-uu-body-line nil t))
1420         (setq state (list 'ok))
1421         ;; Ok, we are at the first uucoded line.
1422         (beginning-of-line)
1423         (setq start-char (point))
1424
1425         (if (not (looking-at gnus-uu-begin-string))
1426             (setq state (list 'middle))
1427           ;; This is the beginning of a uuencoded article.
1428           ;; We replace certain characters that could make things messy.
1429           (setq gnus-uu-file-name
1430                 (let ((nnheader-file-name-translation-alist
1431                        '((?/ . ?,) (?  . ?_) (?* . ?_) (?$ . ?_))))
1432                   (nnheader-translate-file-chars (match-string 1))))
1433           (replace-match (concat "begin 644 " gnus-uu-file-name) t t)
1434
1435           ;; Remove any non gnus-uu-body-line right after start.
1436           (forward-line 1)
1437           (while (and (not (eobp))
1438                       (not (looking-at gnus-uu-body-line)))
1439             (gnus-delete-line))
1440
1441           ;; If a process is running, we kill it.
1442           (when (and gnus-uu-uudecode-process
1443                      (memq (process-status gnus-uu-uudecode-process)
1444                            '(run stop)))
1445             (delete-process gnus-uu-uudecode-process)
1446             (gnus-uu-unmark-list-of-grabbed t))
1447
1448           ;; Start a new uudecoding process.
1449           (let ((cdir default-directory))
1450             (unwind-protect
1451                 (progn
1452                   (cd gnus-uu-work-dir)
1453                   (setq gnus-uu-uudecode-process
1454                         (start-process
1455                          "*uudecode*"
1456                          (gnus-get-buffer-create gnus-uu-output-buffer-name)
1457                          shell-file-name shell-command-switch
1458                          (format "cd %s %s uudecode" gnus-uu-work-dir
1459                                  gnus-shell-command-separator))))
1460               (cd cdir)))
1461           (set-process-sentinel
1462            gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1463           (setq state (list 'begin))
1464           (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1465
1466         ;; We look for the end of the thing to be decoded.
1467         (if (re-search-forward gnus-uu-end-string nil t)
1468             (push 'end state)
1469           (goto-char (point-max))
1470           (re-search-backward gnus-uu-body-line nil t))
1471
1472         (forward-line 1)
1473
1474         (when gnus-uu-uudecode-process
1475           (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1476             ;; Try to correct mishandled uucode.
1477             (when gnus-uu-correct-stripped-uucode
1478               (gnus-uu-check-correct-stripped-uucode start-char (point)))
1479             (gnus-run-hooks 'gnus-uu-pre-uudecode-hook)
1480
1481             ;; Send the text to the process.
1482             (condition-case nil
1483                 (process-send-region
1484                  gnus-uu-uudecode-process start-char (point))
1485               (error
1486                (progn
1487                  (delete-process gnus-uu-uudecode-process)
1488                  (gnus-message 2 "gnus-uu: Couldn't uudecode")
1489                  (setq state (list 'wrong-type)))))
1490
1491             (if (memq 'end state)
1492                 (progn
1493                   ;; Send an EOF, just in case.
1494                   (ignore-errors
1495                     (process-send-eof gnus-uu-uudecode-process))
1496                   (while (memq (process-status gnus-uu-uudecode-process)
1497                                '(open run))
1498                     (accept-process-output gnus-uu-uudecode-process 1)))
1499               (when (or (not gnus-uu-uudecode-process)
1500                         (not (memq (process-status gnus-uu-uudecode-process)
1501                                    '(run stop))))
1502                 (setq state (list 'wrong-type)))))))
1503
1504       (if (memq 'begin state)
1505           (cons (if (= (length files) 1) (car files) files) state)
1506         state))))
1507
1508 (defvar gnus-uu-unshar-warning
1509   "*** WARNING ***
1510
1511 Shell archives are an archaic method of bundling files for distribution
1512 across computer networks.  During the unpacking process, arbitrary commands
1513 are executed on your system, and all kinds of nasty things can happen.
1514 Please examine the archive very carefully before you instruct Emacs to
1515 unpack it.  You can browse the archive buffer using \\[scroll-other-window].
1516
1517 If you are unsure what to do, please answer \"no\"."
1518   "Text of warning message displayed by `gnus-uu-unshar-article'.
1519 Make sure that this text consists only of few text lines.  Otherwise,
1520 Gnus might fail to display all of it.")
1521
1522
1523 ;; This function is used by `gnus-uu-grab-articles' to treat
1524 ;; a shared article.
1525 (defun gnus-uu-unshar-article (process-buffer in-state)
1526   (let ((state (list 'ok))
1527         start-char)
1528     (save-excursion
1529       (set-buffer process-buffer)
1530       (goto-char (point-min))
1531       (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1532           (setq state (list 'wrong-type))
1533         (save-window-excursion
1534           (save-excursion
1535             (switch-to-buffer (current-buffer))
1536             (delete-other-windows)
1537             (let ((buffer (get-buffer-create (generate-new-buffer-name
1538                                               "*Warning*"))))
1539               (unless
1540                   (unwind-protect
1541                       (with-current-buffer buffer
1542                         (insert (substitute-command-keys
1543                                  gnus-uu-unshar-warning))
1544                         (goto-char (point-min))
1545                         (display-buffer buffer)
1546                         (yes-or-no-p "This is a shell archive, unshar it? "))
1547                     (kill-buffer buffer))
1548                 (setq state (list 'error))))))
1549         (unless (memq 'error state)
1550           (beginning-of-line)
1551           (setq start-char (point))
1552           (call-process-region
1553            start-char (point-max) shell-file-name nil
1554            (gnus-get-buffer-create gnus-uu-output-buffer-name) nil
1555            shell-command-switch
1556            (concat "cd " gnus-uu-work-dir " "
1557                    gnus-shell-command-separator  " sh")))))
1558     state))
1559
1560 ;; Returns the name of what the shar file is going to unpack.
1561 (defun gnus-uu-find-name-in-shar ()
1562   (let ((oldpoint (point))
1563         res)
1564     (goto-char (point-min))
1565     (when (re-search-forward gnus-uu-shar-name-marker nil t)
1566       (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1567     (goto-char oldpoint)
1568     res))
1569
1570 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1571 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1572 ;; found, or the name of the command to run if such a rule is found.
1573 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1574   (let ((action-list (copy-sequence file-action-list))
1575         (case-fold-search t)
1576         rule action)
1577     (and
1578      (unless no-ignore
1579        (and (not
1580              (and gnus-uu-ignore-files-by-name
1581                   (string-match gnus-uu-ignore-files-by-name file-name)))
1582             (not
1583              (and gnus-uu-ignore-files-by-type
1584                   (string-match gnus-uu-ignore-files-by-type
1585                                 (or (gnus-uu-choose-action
1586                                      file-name gnus-uu-ext-to-mime-list t)
1587                                     ""))))))
1588      (while (not (or (eq action-list ()) action))
1589        (setq rule (car action-list))
1590        (setq action-list (cdr action-list))
1591        (when (string-match (car rule) file-name)
1592          (setq action (cadr rule)))))
1593     action))
1594
1595 (defun gnus-uu-treat-archive (file-path)
1596   ;; Unpacks an archive.  Returns t if unpacking is successful.
1597   (let ((did-unpack t)
1598         action command dir)
1599     (setq action (gnus-uu-choose-action
1600                   file-path (append gnus-uu-user-archive-rules
1601                                     (if gnus-uu-ignore-default-archive-rules
1602                                         nil
1603                                       gnus-uu-default-archive-rules))))
1604
1605     (when (not action)
1606       (error "No unpackers for the file %s" file-path))
1607
1608     (string-match "/[^/]*$" file-path)
1609     (setq dir (substring file-path 0 (match-beginning 0)))
1610
1611     (when (member action gnus-uu-destructive-archivers)
1612       (copy-file file-path (concat file-path "~") t))
1613
1614     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1615
1616     (save-excursion
1617       (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1618       (erase-buffer))
1619
1620     (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1621
1622     (if (eq 0 (call-process shell-file-name nil
1623                            (gnus-get-buffer-create gnus-uu-output-buffer-name)
1624                            nil shell-command-switch command))
1625         (message "")
1626       (gnus-message 2 "Error during unpacking of archive")
1627       (setq did-unpack nil))
1628
1629     (when (member action gnus-uu-destructive-archivers)
1630       (rename-file (concat file-path "~") file-path t))
1631
1632     did-unpack))
1633
1634 (defun gnus-uu-dir-files (dir)
1635   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1636         files file)
1637     (while dirs
1638       (if (file-directory-p (setq file (car dirs)))
1639           (setq files (append files (gnus-uu-dir-files file)))
1640         (push file files))
1641       (setq dirs (cdr dirs)))
1642     files))
1643
1644 (defun gnus-uu-unpack-files (files &optional ignore)
1645   ;; Go through FILES and look for files to unpack.
1646   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1647          (ofiles files)
1648          file did-unpack)
1649     (while files
1650       (setq file (cdr (assq 'name (car files))))
1651       (when (and (not (member file ignore))
1652                  (equal (gnus-uu-get-action (file-name-nondirectory file))
1653                         "gnus-uu-archive"))
1654         (push file did-unpack)
1655         (unless (gnus-uu-treat-archive file)
1656           (gnus-message 2 "Error during unpacking of %s" file))
1657         (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1658                (nfiles newfiles))
1659           (while nfiles
1660             (unless (member (car nfiles) totfiles)
1661               (push (list (cons 'name (car nfiles))
1662                           (cons 'original file))
1663                     ofiles))
1664             (setq nfiles (cdr nfiles)))
1665           (setq totfiles newfiles)))
1666       (setq files (cdr files)))
1667     (if did-unpack
1668         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1669       ofiles)))
1670
1671 (defun gnus-uu-ls-r (dir)
1672   (let* ((files (gnus-uu-directory-files dir t))
1673          (ofiles files))
1674     (while files
1675       (when (file-directory-p (car files))
1676         (setq ofiles (delete (car files) ofiles))
1677         (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1678       (setq files (cdr files)))
1679     ofiles))
1680
1681 ;; Various stuff
1682
1683 (defun gnus-uu-directory-files (dir &optional full)
1684   (let (files out file)
1685     (setq files (directory-files dir full))
1686     (while files
1687       (setq file (car files))
1688       (setq files (cdr files))
1689       (unless (member (file-name-nondirectory file) '("." ".."))
1690         (push file out)))
1691     (setq out (nreverse out))
1692     out))
1693
1694 (defun gnus-uu-check-correct-stripped-uucode (start end)
1695   (save-excursion
1696     (let (found beg length)
1697       (unless gnus-uu-correct-stripped-uucode
1698         (goto-char start)
1699
1700         (if (re-search-forward " \\|`" end t)
1701             (progn
1702               (goto-char start)
1703               (while (not (eobp))
1704                 (progn
1705                   (when (looking-at "\n")
1706                     (replace-match ""))
1707                   (forward-line 1))))
1708
1709           (while (not (eobp))
1710             (unless (looking-at (concat gnus-uu-begin-string "\\|"
1711                                         gnus-uu-end-string))
1712               (when (not found)
1713                 (setq length (- (point-at-eol) (point-at-bol))))
1714               (setq found t)
1715               (beginning-of-line)
1716               (setq beg (point))
1717               (end-of-line)
1718               (unless (= length (- (point) beg))
1719                 (insert (make-string (- length (- (point) beg)) ? ))))
1720             (forward-line 1)))))))
1721
1722 (defvar gnus-uu-tmp-alist nil)
1723
1724 (defun gnus-uu-initialize (&optional scan)
1725   (let (entry)
1726     (if (and (not scan)
1727              (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1728                (if (file-exists-p (cdr entry))
1729                    (setq gnus-uu-work-dir (cdr entry))
1730                  (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1731                  nil)))
1732         t
1733       (setq gnus-uu-tmp-dir (file-name-as-directory
1734                              (expand-file-name gnus-uu-tmp-dir)))
1735       (if (not (file-directory-p gnus-uu-tmp-dir))
1736           (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1737         (when (not (file-writable-p gnus-uu-tmp-dir))
1738           (error "Temp directory %s can't be written to"
1739                  gnus-uu-tmp-dir)))
1740
1741       (setq gnus-uu-work-dir
1742             (mm-make-temp-file (concat gnus-uu-tmp-dir "gnus") 'dir))
1743       (gnus-set-file-modes gnus-uu-work-dir 448)
1744       (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1745       (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1746             gnus-uu-tmp-alist))))
1747
1748
1749 ;; Kills the temporary uu buffers, kills any processes, etc.
1750 (defun gnus-uu-clean-up ()
1751   (let (buf)
1752     (and gnus-uu-uudecode-process
1753          (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1754                '(stop run))
1755          (delete-process gnus-uu-uudecode-process))
1756     (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1757       (kill-buffer buf))))
1758
1759 (defun gnus-quote-arg-for-sh-or-csh (arg)
1760   (let ((pos 0) new-pos accum)
1761     ;; *** bug: we don't handle newline characters properly
1762     (while (setq new-pos (string-match "[!`\"$\\& \t{}]" arg pos))
1763       (push (substring arg pos new-pos) accum)
1764       (push "\\" accum)
1765       (push (list (aref arg new-pos)) accum)
1766       (setq pos (1+ new-pos)))
1767     (if (= pos 0)
1768         arg
1769       (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
1770
1771 ;; Inputs an action and a filename and returns a full command, making sure
1772 ;; that the filename will be treated as a single argument when the shell
1773 ;; executes the command.
1774 (defun gnus-uu-command (action file)
1775   (let ((quoted-file (shell-quote-argument file)))
1776     (if (string-match "%s" action)
1777         (format action quoted-file)
1778       (concat action " " quoted-file))))
1779
1780 (defun gnus-uu-delete-work-dir (&optional dir)
1781   "Delete recursively all files and directories under `gnus-uu-work-dir'."
1782   (if dir
1783       (gnus-message 7 "Deleting directory %s..." dir)
1784     (setq dir gnus-uu-work-dir))
1785   (when (and dir
1786              (file-exists-p dir))
1787     (let ((files (directory-files dir t nil t))
1788           file)
1789       (while (setq file (pop files))
1790         (unless (member (file-name-nondirectory file) '("." ".."))
1791           (if (file-directory-p file)
1792               (gnus-uu-delete-work-dir file)
1793             (gnus-message 9 "Deleting file %s..." file)
1794             (condition-case err
1795                 (delete-file file)
1796               (error (gnus-message 3 "Deleting file %s failed... %s" file err))))))
1797       (condition-case err
1798           (delete-directory dir)
1799         (error (gnus-message 3 "Deleting directory %s failed... %s" file err))))
1800     (gnus-message 7 "")))
1801
1802 ;; Initializing
1803
1804 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1805 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1806
1807 \f
1808
1809 ;;;
1810 ;;; uuencoded posting
1811 ;;;
1812
1813 ;; Any function that is to be used as and encoding method will take two
1814 ;; parameters: PATH-NAME and FILE-NAME.  (E.g. "/home/gaga/spiral.jpg"
1815 ;; and "spiral.jpg", respectively.) The function should return nil if
1816 ;; the encoding wasn't successful.
1817 (defcustom gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1818   "Function used for encoding binary files.
1819 There are three functions supplied with gnus-uu for encoding files:
1820 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1821 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1822 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1823 uuencode and adds MIME headers."
1824   :group 'gnus-extract-post
1825   :type '(radio (function-item gnus-uu-post-encode-uuencode)
1826                 (function-item gnus-uu-post-encode-mime)
1827                 (function-item gnus-uu-post-encode-mime-uuencode)
1828                 (function :tag "Other")))
1829
1830 (defcustom gnus-uu-post-include-before-composing nil
1831   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1832 If this variable is t, you can either include an encoded file with
1833 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article."
1834   :group 'gnus-extract-post
1835   :type 'boolean)
1836
1837 (defcustom gnus-uu-post-length 990
1838   "Maximum length of an article.
1839 The encoded file will be split into how many articles it takes to
1840 post the entire file."
1841   :group 'gnus-extract-post
1842   :type 'integer)
1843
1844 (defcustom gnus-uu-post-threaded nil
1845   "Non-nil means that gnus-uu will post the encoded file in a thread.
1846 This may not be smart, as no other decoder I have seen are able to
1847 follow threads when collecting uuencoded articles.  (Well, I have seen
1848 one package that does that - gnus-uu, but somehow, I don't think that
1849 counts...)  The default is nil."
1850   :group 'gnus-extract-post
1851   :type 'boolean)
1852
1853 (defcustom gnus-uu-post-separate-description t
1854   "Non-nil means that the description will be posted in a separate article.
1855 The first article will typically be numbered (0/x).  If this variable
1856 is nil, the description the user enters will be included at the
1857 beginning of the first article, which will be numbered (1/x).  Default
1858 is t."
1859   :group 'gnus-extract-post
1860   :type 'boolean)
1861
1862 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1863 (defvar gnus-uu-post-message-id nil)
1864 (defvar gnus-uu-post-inserted-file-name nil)
1865 (defvar gnus-uu-winconf-post-news nil)
1866
1867 (defun gnus-uu-post-news ()
1868   "Compose an article and post an encoded file."
1869   (interactive)
1870   (setq gnus-uu-post-inserted-file-name nil)
1871   (setq gnus-uu-winconf-post-news (current-window-configuration))
1872
1873   (gnus-summary-post-news)
1874
1875   (let ((map (make-sparse-keymap)))
1876     (set-keymap-parent map (current-local-map))
1877     (use-local-map map))
1878   ;;(local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1879   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1880   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1881   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1882
1883   (when gnus-uu-post-include-before-composing
1884     (save-excursion (setq gnus-uu-post-inserted-file-name
1885                           (gnus-uu-post-insert-binary)))))
1886
1887 (defun gnus-uu-post-insert-binary-in-article ()
1888   "Inserts an encoded file in the buffer.
1889 The user will be asked for a file name."
1890   (interactive)
1891   (save-excursion
1892     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1893
1894 ;; Encodes with uuencode and substitutes all spaces with backticks.
1895 (defun gnus-uu-post-encode-uuencode (path file-name)
1896   (when (gnus-uu-post-encode-file "uuencode" path file-name)
1897     (goto-char (point-min))
1898     (forward-line 1)
1899     (while (search-forward " " nil t)
1900       (replace-match "`"))
1901     t))
1902
1903 ;; Encodes with uuencode and adds MIME headers.
1904 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1905   (when (gnus-uu-post-encode-uuencode path file-name)
1906     (gnus-uu-post-make-mime file-name "x-uue")
1907     t))
1908
1909 ;; Encodes with base64 and adds MIME headers
1910 (defun gnus-uu-post-encode-mime (path file-name)
1911   (when (eq 0 (call-process shell-file-name nil t nil shell-command-switch
1912                             (format "%s %s -o %s" "mmencode" path file-name)))
1913     (gnus-uu-post-make-mime file-name "base64")
1914     t))
1915
1916 ;; Adds MIME headers.
1917 (defun gnus-uu-post-make-mime (file-name encoding)
1918   (goto-char (point-min))
1919   (insert (format "Content-Type: %s; name=\"%s\"\n"
1920                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1921                   file-name))
1922   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1923   (save-restriction
1924     (set-buffer gnus-message-buffer)
1925     (goto-char (point-min))
1926     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1927     (forward-line -1)
1928     (narrow-to-region (point-min) (point))
1929     (unless (mail-fetch-field "mime-version")
1930       (widen)
1931       (insert "MIME-Version: 1.0\n"))
1932     (widen)))
1933
1934 ;; Encodes a file PATH with COMMAND, leaving the result in the
1935 ;; current buffer.
1936 (defun gnus-uu-post-encode-file (command path file-name)
1937   (eq 0 (call-process shell-file-name nil t nil shell-command-switch
1938                       (format "%s %s %s" command path file-name))))
1939
1940 (defun gnus-uu-post-news-inews ()
1941   "Posts the composed news article and encoded file.
1942 If no file has been included, the user will be asked for a file."
1943   (interactive)
1944
1945   (let (file-name)
1946
1947     (if gnus-uu-post-inserted-file-name
1948         (setq file-name gnus-uu-post-inserted-file-name)
1949       (setq file-name (gnus-uu-post-insert-binary)))
1950
1951     (gnus-uu-post-encoded file-name gnus-uu-post-threaded))
1952   (setq gnus-uu-post-inserted-file-name nil)
1953   (when gnus-uu-winconf-post-news
1954     (set-window-configuration gnus-uu-winconf-post-news)))
1955
1956 ;; Asks for a file to encode, encodes it and inserts the result in
1957 ;; the current buffer.  Returns the file name the user gave.
1958 (defun gnus-uu-post-insert-binary ()
1959   (let ((uuencode-buffer-name "*uuencode buffer*")
1960         file-path uubuf file-name)
1961
1962     (setq file-path (read-file-name
1963                      "What file do you want to encode? "))
1964     (when (not (file-exists-p file-path))
1965       (error "%s: No such file" file-path))
1966
1967     (goto-char (point-max))
1968     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1969
1970     ;; #### Unix-specific?
1971     (when (string-match "^~/" file-path)
1972       (setq file-path (concat "$HOME" (substring file-path 1))))
1973     ;; #### Unix-specific?
1974     (if (string-match "/[^/]*$" file-path)
1975         (setq file-name (substring file-path (1+ (match-beginning 0))))
1976       (setq file-name file-path))
1977
1978     (unwind-protect
1979         (if (save-excursion
1980               (set-buffer (setq uubuf
1981                                 (gnus-get-buffer-create uuencode-buffer-name)))
1982               (erase-buffer)
1983               (funcall gnus-uu-post-encode-method file-path file-name))
1984             (insert-buffer-substring uubuf)
1985           (error "Encoding unsuccessful"))
1986       (kill-buffer uubuf))
1987     file-name))
1988
1989 ;; Posts the article and all of the encoded file.
1990 (defun gnus-uu-post-encoded (file-name &optional threaded)
1991   (let ((send-buffer-name "*uuencode send buffer*")
1992         (encoded-buffer-name "*encoded buffer*")
1993         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1994         (separator (concat mail-header-separator "\n\n"))
1995         uubuf length parts header i end beg
1996         beg-line minlen post-buf whole-len beg-binary end-binary)
1997
1998     (setq post-buf (current-buffer))
1999
2000     (goto-char (point-min))
2001     (when (not (re-search-forward
2002                 (if gnus-uu-post-separate-description
2003                     (concat "^" (regexp-quote gnus-uu-post-binary-separator)
2004                             "$")
2005                   (concat "^" (regexp-quote mail-header-separator) "$"))
2006                 nil t))
2007       (error "Internal error: No binary/header separator"))
2008     (beginning-of-line)
2009     (forward-line 1)
2010     (setq beg-binary (point))
2011     (setq end-binary (point-max))
2012
2013     (save-excursion
2014       (set-buffer (setq uubuf (gnus-get-buffer-create encoded-buffer-name)))
2015       (erase-buffer)
2016       (insert-buffer-substring post-buf beg-binary end-binary)
2017       (goto-char (point-min))
2018       (setq length (count-lines (point-min) (point-max)))
2019       (setq parts (/ length gnus-uu-post-length))
2020       (unless (< (% length gnus-uu-post-length) 4)
2021         (incf parts)))
2022
2023     (when gnus-uu-post-separate-description
2024       (forward-line -1))
2025     (delete-region (point) (point-max))
2026
2027     (goto-char (point-min))
2028     (re-search-forward
2029      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2030     (setq header (buffer-substring (point-min) (point-at-bol)))
2031
2032     (goto-char (point-min))
2033     (when gnus-uu-post-separate-description
2034       (when (re-search-forward "^Subject: " nil t)
2035         (end-of-line)
2036         (insert (format " (0/%d)" parts)))
2037       (save-excursion
2038         (message-send))
2039       (setq gnus-uu-post-message-id (message-fetch-field "message-id")))
2040
2041     (save-excursion
2042       (setq i 1)
2043       (setq beg 1)
2044       (while (not (> i parts))
2045         (set-buffer (gnus-get-buffer-create send-buffer-name))
2046         (erase-buffer)
2047         (insert header)
2048         (when (and threaded gnus-uu-post-message-id)
2049           (insert "References: " gnus-uu-post-message-id "\n"))
2050         (insert separator)
2051         (setq whole-len
2052               (- 62 (length (format top-string "" file-name i parts ""))))
2053         (when (> 1 (setq minlen (/ whole-len 2)))
2054           (setq minlen 1))
2055         (setq
2056          beg-line
2057          (format top-string
2058                  (make-string minlen ?-)
2059                  file-name i parts
2060                  (make-string
2061                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
2062
2063         (goto-char (point-min))
2064         (when (re-search-forward "^Subject: " nil t)
2065           (end-of-line)
2066           (insert (format " (%d/%d)" i parts)))
2067
2068         (goto-char (point-max))
2069         (save-excursion
2070           (set-buffer uubuf)
2071           (goto-char beg)
2072           (if (= i parts)
2073               (goto-char (point-max))
2074             (forward-line gnus-uu-post-length))
2075           (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
2076             (forward-line -4))
2077           (setq end (point)))
2078         (insert-buffer-substring uubuf beg end)
2079         (insert beg-line "\n")
2080         (setq beg end)
2081         (incf i)
2082         (goto-char (point-min))
2083         (re-search-forward
2084          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2085         (beginning-of-line)
2086         (forward-line 2)
2087         (when (re-search-forward
2088                (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
2089                nil t)
2090           (replace-match "")
2091           (forward-line 1))
2092         (insert beg-line)
2093         (insert "\n")
2094         (let (message-sent-message-via)
2095           (save-excursion
2096             (message-send))
2097           (setq gnus-uu-post-message-id
2098                 (concat (message-fetch-field "references") " "
2099                         (message-fetch-field "message-id"))))))
2100
2101     (gnus-kill-buffer send-buffer-name)
2102     (gnus-kill-buffer encoded-buffer-name)
2103
2104     (when (not gnus-uu-post-separate-description)
2105       (set-buffer-modified-p nil)
2106       (bury-buffer))))
2107
2108 (provide 'gnus-uu)
2109
2110 ;;; gnus-uu.el ends here