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