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