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