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