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