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