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