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