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