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