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