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