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