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