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