2002-01-29 Karl Kleinpaste <karl@charcoal.com>
[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, 2002 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?[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?[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     (let ((gnus-article-reply (gnus-summary-work-articles n)))
533       (gnus-setup-message 'forward
534         (setq gnus-uu-digest-from-subject nil)
535         (setq gnus-uu-digest-buffer
536               (gnus-get-buffer-create " *gnus-uu-forward*"))
537         (gnus-uu-decode-save n file)
538         (switch-to-buffer gnus-uu-digest-buffer)
539         (let ((fs gnus-uu-digest-from-subject))
540           (when fs
541             (setq from (caar fs)
542                   subject (gnus-simplify-subject-fuzzy (cdar fs))
543                   fs (cdr fs))
544             (while (and fs (or from subject))
545               (when from
546                 (unless (string= from (caar fs))
547                   (setq from nil)))
548               (when subject
549                 (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
550                                  subject)
551                   (setq subject nil)))
552               (setq fs (cdr fs))))
553           (unless subject
554             (setq subject "Digested Articles"))
555           (unless from
556             (setq from
557                   (if (gnus-news-group-p gnus-newsgroup-name)
558                       gnus-newsgroup-name
559                     "Various"))))
560         (goto-char (point-min))
561         (when (re-search-forward "^Subject: ")
562           (delete-region (point) (gnus-point-at-eol))
563           (insert subject))
564         (goto-char (point-min))
565         (when (re-search-forward "^From:")
566           (delete-region (point) (gnus-point-at-eol))
567           (insert " " from))
568         (let ((message-forward-decoded-p t))
569           (message-forward post t))))
570     (setq gnus-uu-digest-from-subject nil)))
571
572 (defun gnus-uu-digest-post-forward (&optional n)
573   "Digest and forward to a newsgroup."
574   (interactive "P")
575   (gnus-uu-digest-mail-forward n t))
576
577 ;; Process marking.
578
579 (defun gnus-message-process-mark (unmarkp new-marked)
580   (let ((old (- (length gnus-newsgroup-processable) (length new-marked))))
581     (message "%d mark%s %s%s"
582              (length new-marked)
583              (if (= (length new-marked) 1) "" "s")
584              (if unmarkp "removed" "added")
585              (cond
586               ((and (zerop old)
587                     (not unmarkp))
588                "")
589               (unmarkp
590                (format ", %d remain marked"
591                        (length gnus-newsgroup-processable)))
592               (t
593                (format ", %d already marked" old))))))
594
595 (defun gnus-new-processable (unmarkp articles)
596   (if unmarkp
597       (gnus-intersection gnus-newsgroup-processable articles)
598     (gnus-set-difference articles gnus-newsgroup-processable)))
599
600 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
601   "Set the process mark on articles whose subjects match REGEXP.
602 When called interactively, prompt for REGEXP.
603 Optional UNMARK non-nil means unmark instead of mark."
604   (interactive "sMark (regexp): \nP")
605   (save-excursion
606     (let* ((articles (gnus-uu-find-articles-matching regexp))
607            (new-marked (gnus-new-processable unmark articles)))
608       (while articles
609         (if unmark
610             (gnus-summary-remove-process-mark (pop articles))
611           (gnus-summary-set-process-mark (pop articles))))
612       (gnus-message-process-mark unmark new-marked)))
613   (gnus-summary-position-point))
614
615 (defun gnus-uu-unmark-by-regexp (regexp)
616   "Remove the process mark from articles whose subjects match REGEXP.
617 When called interactively, prompt for REGEXP."
618   (interactive "sUnmark (regexp): ")
619   (gnus-uu-mark-by-regexp regexp t))
620
621 (defun gnus-uu-mark-series ()
622   "Mark the current series with the process mark."
623   (interactive)
624   (let ((articles (gnus-uu-find-articles-matching)))
625     (while articles
626       (gnus-summary-set-process-mark (car articles))
627       (setq articles (cdr articles)))
628     (message ""))
629   (gnus-summary-position-point))
630
631 (defun gnus-uu-mark-region (beg end &optional unmark)
632   "Set the process mark on all articles between point and mark."
633   (interactive "r")
634   (save-excursion
635     (goto-char beg)
636     (while (< (point) end)
637       (if unmark
638           (gnus-summary-remove-process-mark (gnus-summary-article-number))
639         (gnus-summary-set-process-mark (gnus-summary-article-number)))
640       (forward-line 1)))
641   (gnus-summary-position-point))
642
643 (defun gnus-uu-unmark-region (beg end)
644   "Remove the process mark from all articles between point and mark."
645   (interactive "r")
646   (gnus-uu-mark-region beg end t))
647
648 (defun gnus-uu-mark-buffer ()
649   "Set the process mark on all articles in the buffer."
650   (interactive)
651   (gnus-uu-mark-region (point-min) (point-max)))
652
653 (defun gnus-uu-unmark-buffer ()
654   "Remove the process mark on all articles in the buffer."
655   (interactive)
656   (gnus-uu-mark-region (point-min) (point-max) t))
657
658 (defun gnus-uu-mark-thread ()
659   "Marks all articles downwards in this thread."
660   (interactive)
661   (gnus-save-hidden-threads
662     (let ((level (gnus-summary-thread-level)))
663       (while (and (gnus-summary-set-process-mark
664                    (gnus-summary-article-number))
665                   (zerop (gnus-summary-next-subject 1 nil t))
666                   (> (gnus-summary-thread-level) level)))))
667   (gnus-summary-position-point))
668
669 (defun gnus-uu-unmark-thread ()
670   "Unmarks all articles downwards in this thread."
671   (interactive)
672   (let ((level (gnus-summary-thread-level)))
673     (while (and (gnus-summary-remove-process-mark
674                  (gnus-summary-article-number))
675                 (zerop (gnus-summary-next-subject 1))
676                 (> (gnus-summary-thread-level) level))))
677   (gnus-summary-position-point))
678
679 (defun gnus-uu-invert-processable ()
680   "Invert the list of process-marked articles."
681   (interactive)
682   (let ((data gnus-newsgroup-data)
683         number)
684     (save-excursion
685       (while data
686         (if (memq (setq number (gnus-data-number (pop data)))
687                   gnus-newsgroup-processable)
688             (gnus-summary-remove-process-mark number)
689           (gnus-summary-set-process-mark number)))))
690   (gnus-summary-position-point))
691
692 (defun gnus-uu-mark-over (&optional score)
693   "Mark all articles with a score over SCORE (the prefix)."
694   (interactive "P")
695   (let ((score (or score gnus-summary-default-score 0))
696         (data gnus-newsgroup-data))
697     (save-excursion
698       (while data
699         (when (> (or (cdr (assq (gnus-data-number (car data))
700                                 gnus-newsgroup-scored))
701                      gnus-summary-default-score 0)
702                  score)
703           (gnus-summary-set-process-mark (caar data)))
704         (setq data (cdr data))))
705     (gnus-summary-position-point)))
706
707 (defun gnus-uu-mark-sparse ()
708   "Mark all series that have some articles marked."
709   (interactive)
710   (let ((marked (nreverse gnus-newsgroup-processable))
711         subject articles total headers)
712     (unless marked
713       (error "No articles marked with the process mark"))
714     (setq gnus-newsgroup-processable nil)
715     (save-excursion
716       (while marked
717         (and (vectorp (setq headers
718                             (gnus-summary-article-header (car marked))))
719              (setq subject (mail-header-subject headers)
720                    articles (gnus-uu-find-articles-matching
721                              (gnus-uu-reginize-string subject))
722                    total (nconc total articles)))
723         (while articles
724           (gnus-summary-set-process-mark (car articles))
725           (setcdr marked (delq (car articles) (cdr marked)))
726           (setq articles (cdr articles)))
727         (setq marked (cdr marked)))
728       (setq gnus-newsgroup-processable (nreverse total)))
729     (gnus-summary-position-point)))
730
731 (defun gnus-uu-mark-all ()
732   "Mark all articles in \"series\" order."
733   (interactive)
734   (setq gnus-newsgroup-processable nil)
735   (save-excursion
736     (let ((data gnus-newsgroup-data)
737           number)
738       (while data
739         (when (and (not (memq (setq number (gnus-data-number (car data)))
740                               gnus-newsgroup-processable))
741                    (vectorp (gnus-data-header (car data))))
742           (gnus-summary-goto-subject number)
743           (gnus-uu-mark-series))
744         (setq data (cdr data)))))
745   (gnus-summary-position-point))
746
747 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
748
749 (defun gnus-uu-decode-postscript (&optional n)
750   "Gets postscript of the current article."
751   (interactive "P")
752   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
753
754 (defun gnus-uu-decode-postscript-view (&optional n)
755   "Gets and views the current article."
756   (interactive "P")
757   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
758     (gnus-uu-decode-postscript n)))
759
760 (defun gnus-uu-decode-postscript-and-save (n dir)
761   "Extracts postscript and saves the current article."
762   (interactive
763    (list current-prefix-arg
764          (file-name-as-directory
765           (read-file-name "Save in dir: "
766                           gnus-uu-default-dir
767                           gnus-uu-default-dir t))))
768   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
769                               n dir nil nil t))
770
771 (defun gnus-uu-decode-postscript-and-save-view (n dir)
772   "Decodes, views and saves the resulting file."
773   (interactive
774    (list current-prefix-arg
775          (read-file-name "Where do you want to save the file(s)? "
776                          gnus-uu-default-dir
777                          gnus-uu-default-dir t)))
778   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
779     (gnus-uu-decode-postscript-and-save n dir)))
780
781
782 ;; Internal functions.
783
784 (defun gnus-uu-decode-with-method (method n &optional save not-insert
785                                           scan cdir)
786   (gnus-uu-initialize scan)
787   (when save
788     (setq gnus-uu-default-dir save))
789   ;; Create the directory we save to.
790   (when (and scan cdir save
791              (not (file-exists-p save)))
792     (make-directory save t))
793   (let ((articles (gnus-uu-get-list-of-articles n))
794         files)
795     (setq files (gnus-uu-grab-articles articles method t))
796     (let ((gnus-current-article (car articles)))
797       (when scan
798         (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
799     (when save
800       (gnus-uu-save-files files save))
801     (when (eq gnus-uu-do-not-unpack-archives nil)
802       (setq files (gnus-uu-unpack-files files)))
803     (setq files (nreverse (gnus-uu-get-actions files)))
804     (or not-insert (not gnus-insert-pseudo-articles)
805         (gnus-summary-insert-pseudos files save))))
806
807 (defun gnus-uu-scan-directory (dir &optional rec)
808   "Return a list of all files under DIR."
809   (let ((files (directory-files dir t))
810         out file)
811     (while (setq file (pop files))
812       (unless (member (file-name-nondirectory file) '("." ".."))
813         (push (list (cons 'name file)
814                     (cons 'article gnus-current-article))
815               out)
816         (when (file-directory-p file)
817           (setq out (nconc (gnus-uu-scan-directory file t) out)))))
818     (if rec
819         out
820       (nreverse out))))
821
822 (defun gnus-uu-save-files (files dir)
823   "Save FILES in DIR."
824   (let ((len (length files))
825         (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
826         to-file file fromdir)
827     (while (setq file (cdr (assq 'name (pop files))))
828       (when (file-exists-p file)
829         (string-match reg file)
830         (setq fromdir (substring file (match-end 0)))
831         (if (file-directory-p file)
832             (gnus-make-directory (concat dir fromdir))
833           (setq to-file (concat dir fromdir))
834           (when (or (not (file-exists-p to-file))
835                     (eq gnus-uu-be-dangerous t)
836                     (and gnus-uu-be-dangerous
837                          (gnus-y-or-n-p (format "%s exists; overwrite? "
838                                                 to-file))))
839             (copy-file file to-file t t)))))
840     (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
841
842 ;; Functions for saving and possibly digesting articles without
843 ;; any decoding.
844
845 ;; Function called by gnus-uu-grab-articles to treat each article.
846 (defun gnus-uu-save-article (buffer in-state)
847   (cond
848    (gnus-uu-save-separate-articles
849     (save-excursion
850       (set-buffer buffer)
851       (let ((coding-system-for-write mm-text-coding-system))
852         (gnus-write-buffer
853          (concat gnus-uu-saved-article-name gnus-current-article)))
854       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
855             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
856                                                  'begin 'end))
857             ((eq in-state 'last) (list 'end))
858             (t (list 'middle)))))
859    ((not gnus-uu-save-in-digest)
860     (save-excursion
861       (set-buffer buffer)
862       (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
863       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
864             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
865                                                  'begin 'end))
866             ((eq in-state 'last) (list 'end))
867             (t (list 'middle)))))
868    (t
869     (let ((header (gnus-summary-article-header)))
870       (push (cons (mail-header-from header)
871                   (mail-header-subject header))
872             gnus-uu-digest-from-subject))
873     (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
874           beg subj headers headline sorthead body end-string state)
875       (if (or (eq in-state 'first)
876               (eq in-state 'first-and-last))
877           (progn
878             (setq state (list 'begin))
879             (save-excursion
880               (set-buffer (gnus-get-buffer-create "*gnus-uu-body*"))
881               (erase-buffer))
882             (save-excursion
883               (set-buffer (gnus-get-buffer-create "*gnus-uu-pre*"))
884               (erase-buffer)
885               (insert (format
886                        "Date: %s\nFrom: %s\nSubject: %s Digest\n\n"
887                        (current-time-string) name name))
888               (when (and message-forward-as-mime gnus-uu-digest-buffer)
889                 (insert "<#part type=message/rfc822>\nSubject: Topics\n\n"))
890               (insert "Topics:\n")))
891         (when (not (eq in-state 'end))
892           (setq state (list 'middle))))
893       (save-excursion
894         (set-buffer "*gnus-uu-body*")
895         (goto-char (setq beg (point-max)))
896         (save-excursion
897           (save-restriction
898             (set-buffer buffer)
899             (let (buffer-read-only)
900               (gnus-set-text-properties (point-min) (point-max) nil)
901               ;; These two are necessary for XEmacs 19.12 fascism.
902               (put-text-property (point-min) (point-max) 'invisible nil)
903               (put-text-property (point-min) (point-max) 'intangible nil))
904             (when (and message-forward-as-mime
905                        message-forward-show-mml
906                        gnus-uu-digest-buffer)
907               (mm-enable-multibyte)
908               (mime-to-mml))
909             (goto-char (point-min))
910             (re-search-forward "\n\n")
911             (unless (and message-forward-as-mime gnus-uu-digest-buffer)
912               ;; Quote all 30-dash lines.
913               (save-excursion
914                 (while (re-search-forward "^-" nil t)
915                   (beginning-of-line)
916                   (delete-char 1)
917                   (insert "- "))))
918             (setq body (buffer-substring (1- (point)) (point-max)))
919             (narrow-to-region (point-min) (point))
920             (if (not (setq headers gnus-uu-digest-headers))
921                 (setq sorthead (buffer-substring (point-min) (point-max)))
922               (while headers
923                 (setq headline (car headers))
924                 (setq headers (cdr headers))
925                 (goto-char (point-min))
926                 (while (re-search-forward headline nil t)
927                   (setq sorthead
928                         (concat sorthead
929                                 (buffer-substring
930                                  (match-beginning 0)
931                                  (or (and (re-search-forward "^[^ \t]" nil t)
932                                           (1- (point)))
933                                      (progn (forward-line 1) (point)))))))))
934             (widen)))
935         (if (and message-forward-as-mime gnus-uu-digest-buffer)
936           (if message-forward-show-mml
937               (progn
938                 (insert "\n<#mml type=message/rfc822>\n")
939                 (insert sorthead) (goto-char (point-max))
940                 (insert body) (goto-char (point-max))
941                 (insert "\n<#/mml>\n"))
942             (let ((buf (mml-generate-new-buffer " *mml*")))
943               (with-current-buffer buf
944                 (insert sorthead)
945                 (goto-char (point-min))
946                 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
947                   (setq subj (buffer-substring (match-beginning 1)
948                                                (match-end 1))))
949                 (goto-char (point-max))
950                 (insert body))
951               (insert "\n<#part type=message/rfc822"
952                       " buffer=\"" (buffer-name buf) "\">\n")))
953           (insert sorthead) (goto-char (point-max))
954           (insert body) (goto-char (point-max))
955           (insert (concat "\n" (make-string 30 ?-) "\n\n")))
956         (goto-char beg)
957         (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
958           (setq subj (buffer-substring (match-beginning 1) (match-end 1))))
959         (when subj
960           (save-excursion
961             (set-buffer "*gnus-uu-pre*")
962             (insert (format "   %s\n" subj)))))
963       (when (or (eq in-state 'last)
964                 (eq in-state 'first-and-last))
965         (if (and message-forward-as-mime gnus-uu-digest-buffer)
966             (with-current-buffer gnus-uu-digest-buffer
967               (erase-buffer)
968               (insert-buffer "*gnus-uu-pre*")
969               (goto-char (point-max))
970               (insert-buffer "*gnus-uu-body*"))
971           (save-excursion
972             (set-buffer "*gnus-uu-pre*")
973             (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
974             (if gnus-uu-digest-buffer
975                 (with-current-buffer gnus-uu-digest-buffer
976                   (erase-buffer)
977                   (insert-buffer "*gnus-uu-pre*"))
978               (let ((coding-system-for-write mm-text-coding-system))
979                 (gnus-write-buffer gnus-uu-saved-article-name))))
980           (save-excursion
981             (set-buffer "*gnus-uu-body*")
982             (goto-char (point-max))
983             (insert
984              (concat (setq end-string (format "End of %s Digest" name))
985                      "\n"))
986             (insert (concat (make-string (length end-string) ?*) "\n"))
987             (if gnus-uu-digest-buffer
988                 (with-current-buffer gnus-uu-digest-buffer
989                   (goto-char (point-max))
990                   (insert-buffer "*gnus-uu-body*"))
991               (let ((coding-system-for-write mm-text-coding-system)
992                     (file-name-coding-system nnmail-pathname-coding-system))
993                 (write-region
994                  (point-min) (point-max) gnus-uu-saved-article-name t)))))
995         (gnus-kill-buffer "*gnus-uu-pre*")
996         (gnus-kill-buffer "*gnus-uu-body*")
997         (push 'end state))
998       (if (memq 'begin state)
999           (cons gnus-uu-saved-article-name state)
1000         state)))))
1001
1002 ;; Binhex treatment - not very advanced.
1003
1004 (defvar gnus-uu-binhex-body-line
1005   "^[^:]...............................................................$")
1006 (defvar gnus-uu-binhex-begin-line
1007   "^:...............................................................$")
1008 (defvar gnus-uu-binhex-end-line
1009   ":$")
1010
1011 (defun gnus-uu-binhex-article (buffer in-state)
1012   (let (state start-char)
1013     (save-excursion
1014       (set-buffer buffer)
1015       (widen)
1016       (goto-char (point-min))
1017       (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
1018         (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
1019           (setq state (list 'wrong-type))))
1020
1021       (if (memq 'wrong-type state)
1022           ()
1023         (beginning-of-line)
1024         (setq start-char (point))
1025         (if (looking-at gnus-uu-binhex-begin-line)
1026             (progn
1027               (setq state (list 'begin))
1028               (write-region 1 1 gnus-uu-binhex-article-name))
1029           (setq state (list 'middle)))
1030         (goto-char (point-max))
1031         (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
1032                                     gnus-uu-binhex-end-line)
1033                             nil t)
1034         (when (looking-at gnus-uu-binhex-end-line)
1035           (setq state (if (memq 'begin state)
1036                           (cons 'end state)
1037                         (list 'end))))
1038         (beginning-of-line)
1039         (forward-line 1)
1040         (when (file-exists-p gnus-uu-binhex-article-name)
1041           (mm-append-to-file start-char (point) gnus-uu-binhex-article-name))))
1042     (if (memq 'begin state)
1043         (cons gnus-uu-binhex-article-name state)
1044       state)))
1045
1046 ;; PostScript
1047
1048 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
1049   (let ((state (list 'ok))
1050         start-char end-char file-name)
1051     (save-excursion
1052       (set-buffer process-buffer)
1053       (goto-char (point-min))
1054       (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
1055           (setq state (list 'wrong-type))
1056         (beginning-of-line)
1057         (setq start-char (point))
1058         (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
1059             (setq state (list 'wrong-type))
1060           (setq end-char (point))
1061           (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1062           (insert-buffer-substring process-buffer start-char end-char)
1063           (setq file-name (concat gnus-uu-work-dir
1064                                   (cdr gnus-article-current) ".ps"))
1065           (write-region (point-min) (point-max) file-name)
1066           (setq state (list file-name 'begin 'end)))))
1067     state))
1068
1069
1070 ;; Find actions.
1071
1072 (defun gnus-uu-get-actions (files)
1073   (let ((ofiles files)
1074         action name)
1075     (while files
1076       (setq name (cdr (assq 'name (car files))))
1077       (and
1078        (setq action (gnus-uu-get-action name))
1079        (setcar files (nconc (list (if (string= action "gnus-uu-archive")
1080                                       (cons 'action "file")
1081                                     (cons 'action action))
1082                                   (cons 'execute (gnus-uu-command
1083                                                   action name)))
1084                             (car files))))
1085       (setq files (cdr files)))
1086     ofiles))
1087
1088 (defun gnus-uu-get-action (file-name)
1089   (let (action)
1090     (setq action
1091           (gnus-uu-choose-action
1092            file-name
1093            (append
1094             gnus-uu-user-view-rules
1095             (if gnus-uu-ignore-default-view-rules
1096                 nil
1097               gnus-uu-default-view-rules)
1098             gnus-uu-user-view-rules-end)))
1099     (when (and (not (string= (or action "") "gnus-uu-archive"))
1100                gnus-uu-view-with-metamail)
1101       (when (setq action
1102                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
1103         (setq action (format "metamail -d -b -c \"%s\"" action))))
1104     action))
1105
1106
1107 ;; Functions for treating subjects and collecting series.
1108
1109 (defun gnus-uu-reginize-string (string)
1110   ;; Takes a string and puts a \ in front of every special character;
1111   ;; replaces the last thing that looks like "2/3" with "[0-9]+/3"
1112   ;; or, if it can't find something like that, tries "2 of 3", then
1113   ;; finally just replaces the next to last number with "[0-9]+".
1114   (save-excursion
1115     (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1116     (buffer-disable-undo)
1117     (erase-buffer)
1118     (insert (regexp-quote string))
1119
1120     (setq case-fold-search nil)
1121
1122     (end-of-line)
1123     (if (re-search-backward "\\([^0-9]\\)[0-9]+/\\([0-9]+\\)" nil t)
1124         (replace-match "\\1[0-9]+/\\2")
1125
1126       (end-of-line)
1127       (if (re-search-backward "\\([^0-9]\\)[0-9]+[ \t]*of[ \t]*\\([0-9]+\\)"
1128                               nil t)
1129           (replace-match "\\1[0-9]+ of \\2")
1130
1131         (end-of-line)
1132         (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1133                                 nil t)
1134             (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1135
1136     (goto-char 1)
1137     (while (re-search-forward "[ \t]+" nil t)
1138       (replace-match "[ \t]+" t t))
1139
1140     (buffer-substring 1 (point-max))))
1141
1142 (defun gnus-uu-get-list-of-articles (n)
1143   ;; If N is non-nil, the article numbers of the N next articles
1144   ;; will be returned.
1145   ;; If any articles have been marked as processable, they will be
1146   ;; returned.
1147   ;; Failing that, articles that have subjects that are part of the
1148   ;; same "series" as the current will be returned.
1149   (let (articles)
1150     (cond
1151      (n
1152       (setq n (prefix-numeric-value n))
1153       (let ((backward (< n 0))
1154             (n (abs n)))
1155         (save-excursion
1156           (while (and (> n 0)
1157                       (push (gnus-summary-article-number)
1158                             articles)
1159                       (gnus-summary-search-forward nil nil backward))
1160             (setq n (1- n))))
1161         (nreverse articles)))
1162      (gnus-newsgroup-processable
1163       (reverse gnus-newsgroup-processable))
1164      (t
1165       (gnus-uu-find-articles-matching)))))
1166
1167 (defun gnus-uu-string< (l1 l2)
1168   (string< (car l1) (car l2)))
1169
1170 (defun gnus-uu-find-articles-matching
1171   (&optional subject only-unread do-not-translate)
1172   ;; Finds all articles that matches the regexp SUBJECT.  If it is
1173   ;; nil, the current article name will be used.  If ONLY-UNREAD is
1174   ;; non-nil, only unread articles are chosen.  If DO-NOT-TRANSLATE is
1175   ;; non-nil, article names are not equalized before sorting.
1176   (let ((subject (or subject
1177                      (gnus-uu-reginize-string (gnus-summary-article-subject))))
1178         list-of-subjects)
1179     (save-excursion
1180       (when subject
1181         ;; Collect all subjects matching subject.
1182         (let ((case-fold-search t)
1183               (data gnus-newsgroup-data)
1184               subj mark d)
1185           (while data
1186             (setq d (pop data))
1187             (and (not (gnus-data-pseudo-p d))
1188                  (or (not only-unread)
1189                      (= (setq mark (gnus-data-mark d))
1190                         gnus-unread-mark)
1191                      (= mark gnus-ticked-mark)
1192                      (= mark gnus-dormant-mark))
1193                  (setq subj (mail-header-subject (gnus-data-header d)))
1194                  (string-match subject subj)
1195                  (push (cons subj (gnus-data-number d))
1196                        list-of-subjects))))
1197
1198         ;; Expand numbers, sort, and return the list of article
1199         ;; numbers.
1200         (mapcar (lambda (sub) (cdr sub))
1201                 (sort (gnus-uu-expand-numbers
1202                        list-of-subjects
1203                        (not do-not-translate))
1204                       'gnus-uu-string<))))))
1205
1206 (defun gnus-uu-expand-numbers (string-list &optional translate)
1207   ;; Takes a list of strings and "expands" all numbers in all the
1208   ;; strings.  That is, this function makes all numbers equal length by
1209   ;; prepending lots of zeroes before each number.  This is to ease later
1210   ;; sorting to find out what sequence the articles are supposed to be
1211   ;; decoded in.  Returns the list of expanded strings.
1212   (let ((out-list string-list)
1213         string)
1214     (save-excursion
1215       (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1216       (buffer-disable-undo)
1217       (while string-list
1218         (erase-buffer)
1219         (insert (caar string-list))
1220         ;; Translate multiple spaces to one space.
1221         (goto-char (point-min))
1222         (while (re-search-forward "[ \t]+" nil t)
1223           (replace-match " "))
1224         ;; Translate all characters to "a".
1225         (goto-char (point-min))
1226         (when translate
1227           (while (re-search-forward "[A-Za-z]" nil t)
1228             (replace-match "a" t t)))
1229         ;; Expand numbers.
1230         (goto-char (point-min))
1231         (while (re-search-forward "[0-9]+" nil t)
1232           (ignore-errors
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     (setq gnus-current-article nil)
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