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