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