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