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