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