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