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