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