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