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