*** 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 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 (re-search-forward "^[^ \t]" nil t)
665                                       (progn (forward-line 1) (point))))))))
666              (widen)))
667          (insert sorthead)(goto-char (point-max))
668          (insert body)(goto-char (point-max))
669          (insert (concat "\n" (make-string 30 ?-) "\n\n"))
670          (goto-char beg)
671          (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
672              (progn
673                (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
674                (save-excursion 
675                 (set-buffer (get-buffer "*gnus-uu-pre*"))
676                 (insert (format "   %s\n" subj))))))
677       (if (or (eq in-state 'last)
678               (eq in-state 'first-and-last))
679           (progn
680             (save-excursion
681               (set-buffer (get-buffer "*gnus-uu-pre*"))
682               (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
683               (write-region 1 (point-max) gnus-uu-saved-article-name))
684             (save-excursion
685               (set-buffer (get-buffer "*gnus-uu-body*"))
686               (goto-char (point-max))
687               (insert 
688                (concat (setq end-string (format "End of %s Digest" name)) 
689                        "\n"))
690               (insert (concat (make-string (length end-string) ?*) "\n"))
691               (write-region 1 (point-max) gnus-uu-saved-article-name t))
692             (kill-buffer (get-buffer "*gnus-uu-pre*"))
693             (kill-buffer (get-buffer "*gnus-uu-body*"))
694             (setq state (cons 'end state))))
695       (if (memq 'begin state)
696           (cons gnus-uu-saved-article-name state)
697         state)))))
698
699 ;; Binhex treatment - not very advanced. 
700
701 (defconst gnus-uu-binhex-body-line 
702   "^[^:]...............................................................$")
703 (defconst gnus-uu-binhex-begin-line 
704   "^:...............................................................$")
705 (defconst gnus-uu-binhex-end-line
706   ":$")
707 (defvar gnus-uu-binhex-article-name nil)
708
709 (defun gnus-uu-binhex-article (buffer in-state)
710   (let (state start-char)
711     (save-excursion
712       (set-buffer buffer)
713       (widen)
714       (goto-char (point-min))
715       (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
716           (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
717               (setq state (list 'wrong-type))))
718
719       (if (memq 'wrong-type state)
720           ()
721         (beginning-of-line)
722         (setq start-char (point))
723         (if (looking-at gnus-uu-binhex-begin-line)
724             (progn
725               (setq state (list 'begin))
726               (write-region 1 1 gnus-uu-binhex-article-name))
727           (setq state (list 'middle)))
728         (goto-char (point-max))
729         (re-search-backward (concat gnus-uu-binhex-body-line "\\|" 
730                                     gnus-uu-binhex-end-line) nil t)
731         (if (looking-at gnus-uu-binhex-end-line)
732             (setq state (if (memq 'begin state)
733                             (cons 'end state)
734                           (list 'end))))
735         (beginning-of-line)
736         (forward-line 1)
737         (if (file-exists-p gnus-uu-binhex-article-name)
738             (append-to-file start-char (point) gnus-uu-binhex-article-name))))
739     (if (memq 'begin state)
740         (cons gnus-uu-binhex-article-name state)
741       state)))
742
743 ;; PostScript
744
745 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
746   (let ((state (list 'ok))
747         start-char end-char file-name)
748     (save-excursion
749      (set-buffer process-buffer)
750      (goto-char (point-min))
751      (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
752          (setq state (list 'wrong-type))
753        (beginning-of-line)
754        (setq start-char (point))
755        (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
756            (setq state (list 'wrong-type))
757          (setq end-char (point))
758          (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
759          (insert-buffer-substring process-buffer start-char end-char)
760          (setq file-name (concat gnus-uu-work-dir (cdr gnus-article-current) ".ps"))
761          (write-region (point-min) (point-max) file-name)
762          (setq state (list file-name'begin 'end))
763
764          ))
765      )
766     state))
767       
768
769 ;; Find actions.
770
771 (defun gnus-uu-get-actions (files)
772   (let ((ofiles files)
773         action name)
774     (while files
775       (setq name (cdr (assq 'name (car files))))
776       (and 
777        (setq action (gnus-uu-get-action name))
778        (setcar files (nconc (list (cons 'action action)
779                                   (cons 'execute (if (string-match "%" action)
780                                                      (format action name)
781                                                    (concat action " " name))))
782                             (car files))))
783       (setq files (cdr files)))
784     ofiles))
785
786 (defun gnus-uu-get-action (file-name)
787   (let (action)
788     (setq action 
789           (gnus-uu-choose-action 
790            file-name
791            (append 
792             gnus-uu-user-view-rules
793             (if gnus-uu-ignore-default-view-rules 
794                 nil 
795               gnus-uu-default-view-rules)
796             gnus-uu-user-view-rules-end)))
797     (if (and (not (string= (or action "") "gnus-uu-archive")) 
798              gnus-uu-view-with-metamail)
799         (if (setq action 
800                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
801             (setq action (format "metamail -d -b -c \"%s\"" action))))
802     action))
803
804
805 ;; Functions for treating subjects and collecting series.
806
807 (defun gnus-uu-reginize-string (string)
808   ;; Takes a string and puts a \ in front of every special character;
809   ;; ignores any leading "version numbers" thingies that they use in
810   ;; the comp.binaries groups, and either replaces anything that looks
811   ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
812   ;; like that, replaces the last two numbers with "[0-9]+". This, in
813   ;; my experience, should get most postings of a series.
814   (let ((count 2)
815         (vernum "v[0-9]+[a-z][0-9]+:")
816         reg beg)
817     (save-excursion
818       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
819       (buffer-disable-undo (current-buffer))
820       (erase-buffer)
821       (insert (regexp-quote string))
822       (setq beg 1)
823
824       (setq case-fold-search nil)
825       (goto-char (point-min))
826       (if (looking-at vernum)
827           (progn
828             (replace-match vernum t t)
829             (setq beg (length vernum))))
830
831       (goto-char beg)
832       (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
833           (replace-match " [0-9]+/[0-9]+")
834
835         (goto-char beg)
836         (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
837             (replace-match "[0-9]+ of [0-9]+")
838
839           (end-of-line)
840           (while (and (re-search-backward "[0-9]" nil t) (> count 0))
841             (while (and 
842                     (looking-at "[0-9]") 
843                     (< 1 (goto-char (1- (point))))))
844             (re-search-forward "[0-9]+" nil t)
845             (replace-match "[0-9]+")
846             (backward-char 5)
847             (setq count (1- count)))))
848
849       (goto-char beg)
850       (while (re-search-forward "[ \t]+" nil t)
851         (replace-match "[ \t]*" t t))
852
853       (buffer-substring 1 (point-max)))))
854
855 (defun gnus-uu-get-list-of-articles (n)
856   ;; If N is non-nil, the article numbers of the N next articles
857   ;; will be returned.
858   ;; If any articles have been marked as processable, they will be
859   ;; returned. 
860   ;; Failing that, articles that have subjects that are part of the
861   ;; same "series" as the current will be returned.
862   (let (articles process)
863   (cond 
864    (n
865     (let ((backward (< n 0))
866           (n (abs n)))
867       (save-excursion
868         (while (and (> n 0)
869                     (setq articles (cons (gnus-summary-article-number) 
870                                          articles))
871                     (gnus-summary-search-forward nil nil backward))
872           (setq n (1- n))))
873       (nreverse articles)))
874    (gnus-newsgroup-processable
875     (reverse gnus-newsgroup-processable))
876    (t
877     (gnus-uu-find-articles-matching)))))
878
879 (defun gnus-uu-string< (l1 l2)
880   (string< (car l1) (car l2)))
881
882 (defun gnus-uu-find-articles-matching 
883   (&optional subject only-unread do-not-translate)
884   ;; Finds all articles that matches the regexp SUBJECT.  If it is
885   ;; nil, the current article name will be used. If ONLY-UNREAD is
886   ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
887   ;; non-nil, article names are not equalized before sorting.
888   (let ((subject (or subject 
889                      (gnus-uu-reginize-string (gnus-summary-subject-string))))
890         beg end list-of-subjects)
891     (save-excursion
892       (if (not subject)
893           ()
894         ;; Collect all subjects matching subject.
895         (let ((case-fold-search t)
896               subj mark)
897           (goto-char (point-min))
898           (while (not (eobp))
899             (and (setq subj (gnus-summary-subject-string))
900                  (string-match subject subj)
901                  (or (not only-unread)
902                      (= (setq mark (gnus-summary-article-mark)) 
903                         gnus-unread-mark)
904                      (= mark gnus-ticked-mark)
905                      (= mark gnus-dormant-mark))
906                  (setq list-of-subjects 
907                        (cons (cons subj (gnus-summary-article-number))
908                              list-of-subjects)))
909             (forward-line 1)))
910
911         ;; Expand numbers, sort, and return the list of article
912         ;; numbers.
913         (mapcar (lambda (sub) (cdr sub)) 
914                 (sort (gnus-uu-expand-numbers 
915                        list-of-subjects
916                        (not do-not-translate)) 
917                       'gnus-uu-string<))))))
918
919 (defun gnus-uu-expand-numbers (string-list &optional translate)
920   ;; Takes a list of strings and "expands" all numbers in all the
921   ;; strings.  That is, this function makes all numbers equal length by
922   ;; prepending lots of zeroes before each number. This is to ease later
923   ;; sorting to find out what sequence the articles are supposed to be
924   ;; decoded in. Returns the list of expanded strings.
925   (let ((out-list string-list)
926         string pos num)
927     (save-excursion
928       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
929       (buffer-disable-undo (current-buffer))
930       (while string-list
931         (erase-buffer)
932         (insert (car (car string-list)))
933         ;; Translate multiple spaces to one space.
934         (goto-char (point-min))
935         (while (re-search-forward "[ \t]+" nil t)
936           (replace-match " "))
937         ;; Translate all characters to "a".
938         (goto-char (point-min))
939         (if translate 
940             (while (re-search-forward "[A-Za-z]" nil t)
941               (replace-match "a" t t)))
942         ;; Expand numbers.
943         (goto-char (point-min))
944         (while (re-search-forward "[0-9]+" nil t)
945           (replace-match  
946            (format "%06d" 
947                    (string-to-int (buffer-substring 
948                                    (match-beginning 0) (match-end 0))))))
949         (setq string (buffer-substring 1 (point-max)))
950         (setcar (car string-list) string)
951         (setq string-list (cdr string-list))))
952     out-list))
953
954
955 ;; `gnus-uu-grab-articles' is the general multi-article treatment
956 ;; function.  It takes a list of articles to be grabbed and a function
957 ;; to apply to each article.
958 ;;
959 ;; The function to be called should take two parameters.  The first
960 ;; parameter is the article buffer. The function should leave the
961 ;; result, if any, in this buffer. Most treatment functions will just
962 ;; generate files...
963 ;;
964 ;; The second parameter is the state of the list of articles, and can
965 ;; have four values: `first', `middle', `last' and `first-and-last'.
966 ;;
967 ;; The function should return a list. The list may contain the
968 ;; following symbols:
969 ;; `error' if an error occurred
970 ;; `begin' if the beginning of an encoded file has been received
971 ;;   If the list returned contains a `begin', the first element of
972 ;;   the list *must* be a string with the file name of the decoded
973 ;;   file.
974 ;; `end' if the the end of an encoded file has been received
975 ;; `middle' if the article was a body part of an encoded file
976 ;; `wrong-type' if the article was not a part of an encoded file
977 ;; `ok', which can be used everything is ok
978
979 (defvar gnus-uu-has-been-grabbed nil)
980
981 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
982   (let (art)
983     (if (not (and gnus-uu-has-been-grabbed
984                   gnus-uu-unmark-articles-not-decoded))
985         ()
986       (if dont-unmark-last-article
987           (progn
988             (setq art (car gnus-uu-has-been-grabbed))
989             (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
990       (while gnus-uu-has-been-grabbed
991         (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
992         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
993       (if dont-unmark-last-article
994           (setq gnus-uu-has-been-grabbed (list art))))))
995
996 ;; This function takes a list of articles and a function to apply to
997 ;; each article grabbed. 
998 ;; 
999 ;; This function returns a list of files decoded if the grabbing and
1000 ;; the process-function has been successful and nil otherwise.
1001 (defun gnus-uu-grab-articles 
1002   (articles process-function &optional sloppy limit no-errors)
1003   (let ((state 'first) 
1004         (wrong-type t)
1005         has-been-begin has-been-end 
1006         article result-file result-files process-state article-buffer
1007         begin-article)
1008  
1009     (if (not (gnus-server-opened gnus-current-select-method))
1010         (progn
1011           (gnus-start-news-server)
1012           (gnus-request-group gnus-newsgroup-name)))
1013
1014     (setq gnus-uu-has-been-grabbed nil)
1015
1016     (while (and articles 
1017                 (not (memq 'error process-state))
1018                 (or sloppy
1019                     (not (memq 'end process-state))))
1020
1021       (setq article (car articles))
1022       (setq articles (cdr articles))
1023       (setq gnus-uu-has-been-grabbed (cons article gnus-uu-has-been-grabbed))
1024
1025       (if (> article gnus-uu-highest-article-number) 
1026           (setq gnus-uu-highest-article-number article))
1027
1028       (if (eq articles ()) 
1029           (if (eq state 'first)
1030               (setq state 'first-and-last)
1031             (setq state 'last)))
1032
1033       (message "Getting article %d, %s" article (gnus-uu-part-number article))
1034
1035       (if (not (= (or gnus-current-article 0) article))
1036           (progn
1037             (gnus-request-article article gnus-newsgroup-name
1038                                   nntp-server-buffer)
1039             (setq gnus-last-article gnus-current-article)
1040             (setq gnus-current-article article)
1041             (setq gnus-article-current (cons gnus-newsgroup-name article))
1042             (if (stringp nntp-server-buffer)
1043                 (setq article-buffer nntp-server-buffer)
1044               (setq article-buffer (buffer-name nntp-server-buffer))))
1045         (setq article-buffer gnus-article-buffer))
1046
1047       (buffer-disable-undo article-buffer)
1048       ;; Mark article as read.
1049       (run-hooks 'gnus-mark-article-hook)
1050       (and (memq article gnus-newsgroup-processable)
1051            (gnus-summary-remove-process-mark article))
1052
1053       (setq process-state (funcall process-function article-buffer state))
1054
1055       (if (or (memq 'begin process-state)
1056               (and (or (eq state 'first) (eq state 'first-and-last))
1057                    (memq 'ok process-state)))
1058           (progn
1059             (if has-been-begin
1060                 (if (and result-file (file-exists-p result-file)) 
1061                     (delete-file result-file)))
1062             (if (memq 'begin process-state)
1063                 (setq result-file (car process-state)))
1064             (setq begin-article article)
1065             (setq has-been-begin t)
1066             (setq has-been-end nil)))
1067
1068       (if (memq 'end process-state)
1069           (progn
1070             (setq gnus-uu-has-been-grabbed nil)
1071             (setq result-files (cons (list (cons 'name result-file)
1072                                            (cons 'article article))
1073                                      result-files))
1074             (setq has-been-end t)
1075             (setq has-been-begin nil)
1076             (and limit (= (length result-files) limit)
1077                  (setq articles nil))))
1078
1079       (if (and (or (eq state 'last) (eq state 'first-and-last))
1080                (not (memq 'end process-state)))
1081                 (if (and result-file (file-exists-p result-file))
1082                     (delete-file result-file)))
1083
1084       (if (not (memq 'wrong-type process-state))
1085           (setq wrong-type nil)
1086         (if gnus-uu-unmark-articles-not-decoded
1087             (gnus-summary-tick-article article t)))
1088
1089       (if sloppy (setq wrong-type nil))
1090
1091       (if (and (not has-been-begin)
1092                (not sloppy)
1093                (or (memq 'end process-state)
1094                    (memq 'middle process-state)))
1095           (progn
1096             (setq process-state (list 'error))
1097             (message "No begin part at the beginning")
1098             (sleep-for 2))
1099         (setq state 'middle)))
1100
1101     ;; Make sure the last article is put in the article buffer & fix
1102     ;; windows etc.
1103
1104     (if (not (string= article-buffer gnus-article-buffer))
1105         (save-excursion
1106           (set-buffer (get-buffer-create gnus-article-buffer))
1107           (let ((buffer-read-only nil))
1108             (widen)
1109             (erase-buffer)
1110             (insert-buffer-substring article-buffer)
1111             (goto-char (point-min)))))
1112
1113     (if result-files
1114         ()
1115       (if (not has-been-begin)
1116           (if (not no-errors) (message "Wrong type file"))
1117         (if (memq 'error process-state)
1118             (setq result-files nil)
1119           (if (not (or (memq 'ok process-state) 
1120                        (memq 'end process-state)))
1121               (progn
1122                 (if (not no-errors)
1123                     (message "End of articles reached before end of file"))
1124                 (setq result-files nil))
1125             (gnus-uu-unmark-list-of-grabbed)))))
1126     result-files))
1127
1128 (defun gnus-uu-part-number (article)
1129   (let ((subject (header-subject (gnus-get-header-by-number article))))
1130     (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1131                       subject)
1132         (substring subject (match-beginning 0) (match-end 0))
1133       "")))
1134
1135 (defun gnus-uu-uudecode-sentinel (process event)
1136   (delete-process (get-process process)))
1137
1138 (defun gnus-uu-uustrip-article (process-buffer in-state)
1139   ;; Uudecodes a file asynchronously.
1140   (let ((state (list 'ok))
1141         (process-connection-type nil)
1142         start-char pst name-beg name-end)
1143     (save-excursion
1144       (set-buffer process-buffer)
1145       (let ((case-fold-search nil)
1146             (buffer-read-only nil))
1147
1148         (goto-char (point-min))
1149
1150         (if gnus-uu-kill-carriage-return
1151             (progn
1152               (while (search-forward "\r" nil t)
1153                 (delete-backward-char 1))
1154               (goto-char (point-min))))
1155
1156         (if (not (re-search-forward gnus-uu-begin-string nil t))
1157             (if (not (re-search-forward gnus-uu-body-line nil t))
1158                 (setq state (list 'wrong-type))))
1159      
1160         (if (memq 'wrong-type state)
1161             ()
1162           (beginning-of-line)
1163           (setq start-char (point))
1164
1165           (if (looking-at gnus-uu-begin-string)
1166               (progn 
1167                 (setq name-end (match-end 1))
1168
1169                 ; Replace any slashes and spaces in file names before decoding
1170                 (goto-char (setq name-beg (match-beginning 1)))
1171                 (while (re-search-forward "/" name-end t)
1172                   (replace-match ","))
1173                 (goto-char name-beg)
1174                 (while (re-search-forward " " name-end t)
1175                   (replace-match "_"))
1176                 (goto-char name-beg)
1177                 (if (re-search-forward "_*$" name-end t)
1178                   (replace-match ""))
1179
1180                 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1181                 (and gnus-uu-uudecode-process
1182                      (setq pst (process-status 
1183                                 (or gnus-uu-uudecode-process "nevair")))
1184                      (if (or (eq pst 'stop) (eq pst 'run))
1185                          (progn
1186                            (delete-process gnus-uu-uudecode-process)
1187                            (gnus-uu-unmark-list-of-grabbed t))))
1188                 (if (get-process "*uudecode*")
1189                     (delete-process "*uudecode*"))
1190                 (setq gnus-uu-uudecode-process
1191                       (start-process 
1192                        "*uudecode*" 
1193                        (get-buffer-create gnus-uu-output-buffer-name)
1194                        "sh" "-c" 
1195                        (format "cd %s ; uudecode" gnus-uu-work-dir)))
1196                 (set-process-sentinel 
1197                  gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1198                 (setq state (list 'begin))
1199                 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1200             (setq state (list 'middle)))
1201         
1202           (goto-char (point-max))
1203
1204           (re-search-backward 
1205            (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1206           (beginning-of-line)
1207
1208           (if (looking-at gnus-uu-end-string)
1209               (setq state (cons 'end state)))
1210           (forward-line 1)
1211
1212           (and gnus-uu-uudecode-process
1213                (setq pst (process-status 
1214                           (or gnus-uu-uudecode-process "nevair")))
1215                (if (or (eq pst 'run) (eq pst 'stop))
1216                    (progn
1217                      (if gnus-uu-correct-stripped-uucode
1218                          (progn
1219                            (gnus-uu-check-correct-stripped-uucode 
1220                             start-char (point))
1221                            (goto-char (point-max))
1222                            (re-search-backward 
1223                             (concat gnus-uu-body-line "\\|" 
1224                                     gnus-uu-end-string) 
1225                             nil t)
1226                            (forward-line 1)))
1227
1228                      (condition-case err
1229                          (process-send-region gnus-uu-uudecode-process 
1230                                               start-char (point))
1231                        (error 
1232                         (progn 
1233                           (delete-process gnus-uu-uudecode-process)
1234                           (message "gnus-uu: Couldn't uudecode")
1235 ;                         (sleep-for 2)
1236                           (setq state (list 'wrong-type)))))
1237
1238                      (if (memq 'end state)
1239                          (accept-process-output gnus-uu-uudecode-process)))
1240                  (setq state (list 'wrong-type))))
1241           (if (not gnus-uu-uudecode-process)
1242               (setq state (list 'wrong-type)))))
1243
1244       (if (memq 'begin state)
1245           (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1246         state))))
1247
1248 ;; This function is used by `gnus-uu-grab-articles' to treat
1249 ;; a shared article.
1250 (defun gnus-uu-unshar-article (process-buffer in-state)
1251   (let ((state (list 'ok))
1252         start-char)
1253     (save-excursion
1254      (set-buffer process-buffer)
1255      (goto-char (point-min))
1256      (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1257          (setq state (list 'wrong-type))
1258        (beginning-of-line)
1259        (setq start-char (point))
1260        (call-process-region 
1261         start-char (point-max) "sh" nil 
1262         (get-buffer-create gnus-uu-output-buffer-name) nil 
1263         "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1264     state))
1265
1266 ;; Returns the name of what the shar file is going to unpack.
1267 (defun gnus-uu-find-name-in-shar ()
1268   (let ((oldpoint (point))
1269         res)
1270     (goto-char (point-min))
1271     (if (re-search-forward gnus-uu-shar-name-marker nil t)
1272         (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1273     (goto-char oldpoint)
1274     res))
1275
1276 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1277 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1278 ;; found, or the name of the command to run if such a rule is found.
1279 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1280   (let ((action-list (copy-sequence file-action-list))
1281         rule action)
1282     (and 
1283      (or no-ignore 
1284          (and (not 
1285                (and gnus-uu-ignore-files-by-name
1286                     (string-match gnus-uu-ignore-files-by-name file-name)))
1287               (not 
1288                (and gnus-uu-ignore-files-by-type
1289                     (string-match gnus-uu-ignore-files-by-type 
1290                                   (or (gnus-uu-choose-action 
1291                                        file-name gnus-uu-ext-to-mime-list t) 
1292                                       ""))))))
1293      (while (not (or (eq action-list ()) action))
1294        (setq rule (car action-list))
1295        (setq action-list (cdr action-list))
1296        (if (string-match (car rule) file-name)
1297            (setq action (car (cdr rule))))))
1298     action))
1299
1300 (defun gnus-uu-treat-archive (file-path)
1301   ;; Unpacks an archive. Returns t if unpacking is successful.
1302   (let ((did-unpack t)
1303         action command files file file-name dir)
1304     (setq action (gnus-uu-choose-action 
1305                   file-path (append gnus-uu-user-archive-rules
1306                                     (if gnus-uu-ignore-default-archive-rules
1307                                         nil
1308                                       gnus-uu-default-archive-rules))))
1309
1310     (if (not action) (error "No unpackers for the file %s" file-path))
1311
1312     (string-match "/[^/]*$" file-path)
1313     (setq file-name (substring file-path (1+ (match-beginning 0))))
1314     (setq dir (substring file-path 0 (match-beginning 0)))
1315
1316     (if (member action gnus-uu-destructive-archivers)
1317         (copy-file file-path (concat file-path "~") t))
1318
1319     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1320
1321     (save-excursion
1322       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1323       (erase-buffer))
1324
1325     (message "Unpacking: %s..." (gnus-uu-command action file-path))
1326
1327     (if (= 0 (call-process "sh" nil 
1328                            (get-buffer-create gnus-uu-output-buffer-name)
1329                            nil "-c" command))
1330         (message "")
1331       (message "Error during unpacking of archive")
1332       (setq did-unpack nil))
1333
1334     (if (member action gnus-uu-destructive-archivers)
1335         (rename-file (concat file-path "~") file-path t))
1336
1337     did-unpack))
1338
1339 (defun gnus-uu-dir-files (dir)
1340   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1341         files file)
1342     (while dirs
1343       (if (file-directory-p (setq file (car dirs)))
1344           (setq files (append files (gnus-uu-dir-files file)))
1345         (setq files (cons file files)))
1346       (setq dirs (cdr dirs)))
1347     files))
1348
1349 (defun gnus-uu-unpack-files (files &optional ignore)
1350   ;; Go through FILES and look for files to unpack. 
1351   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1352          (ofiles files)
1353          file did-unpack)
1354     (while files
1355       (setq file (cdr (assq 'name (car files))))
1356       (if (and (not (member file ignore))
1357                (equal (gnus-uu-get-action (file-name-nondirectory file))
1358                       "gnus-uu-archive"))
1359           (progn
1360             (setq did-unpack (cons file did-unpack))
1361             (or (gnus-uu-treat-archive file)
1362                 (message "Error during unpacking of %s" file))
1363             (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1364                    (nfiles newfiles))
1365               (while nfiles
1366                 (or (member (car nfiles) totfiles)
1367                     (setq ofiles (cons (list (cons 'name (car nfiles))
1368                                              (cons 'original file))
1369                                        ofiles)))
1370                 (setq nfiles (cdr nfiles)))
1371               (setq totfiles newfiles))))
1372       (setq files (cdr files)))
1373     (if did-unpack 
1374         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1375       ofiles)))
1376
1377 (defun gnus-uu-ls-r (dir)
1378   (let* ((files (gnus-uu-directory-files dir t))
1379          (ofiles files))
1380     (while files
1381       (if (file-directory-p (car files))
1382           (progn
1383             (setq ofiles (delete (car files) ofiles))
1384             (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1385       (setq files (cdr files)))
1386     ofiles))
1387
1388 ;; Various stuff
1389
1390 (defun gnus-uu-directory-files (dir &optional full)
1391   (let (files out file)
1392     (setq files (directory-files dir full))
1393     (while files
1394       (setq file (car files))
1395       (setq files (cdr files))
1396       (or (string-match "/\\.\\.?$" file)
1397           (setq out (cons file out))))
1398     (setq out (nreverse out))
1399     out))
1400
1401 (defun gnus-uu-check-correct-stripped-uucode (start end)
1402   (let (found beg length short)
1403     (if (not gnus-uu-correct-stripped-uucode)
1404         ()
1405       (goto-char start)
1406
1407       (if (re-search-forward " \\|`" end t)
1408           (progn
1409             (goto-char start)
1410             (while (not (eobp))
1411               (progn
1412                 (if (looking-at "\n") (replace-match ""))
1413                 (forward-line 1))))
1414             
1415         (while (not (eobp))
1416           (if (looking-at (concat gnus-uu-begin-string "\\|" 
1417                                   gnus-uu-end-string))
1418               ()
1419             (if (not found)
1420                 (progn
1421                   (beginning-of-line)
1422                   (setq beg (point))
1423                   (end-of-line)
1424                   (setq length (- (point) beg))))
1425             (setq found t)
1426             (beginning-of-line)
1427             (setq beg (point))
1428             (end-of-line)
1429             (if (not (= length (- (point) beg)))
1430                 (insert (make-string (- length (- (point) beg)) ? ))))
1431           (forward-line 1))))))
1432
1433 (defun gnus-uu-initialize ()
1434   (setq gnus-uu-highest-article-number 1)
1435   (gnus-uu-check-for-generated-files)
1436   (setq gnus-uu-tmp-dir (expand-file-name gnus-uu-tmp-dir))
1437   (if (string-match "[^/]$" gnus-uu-tmp-dir) 
1438       (setq gnus-uu-tmp-dir (concat gnus-uu-tmp-dir "/")))
1439   (if (not (file-directory-p gnus-uu-tmp-dir))
1440       (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1441     (if (not (file-writable-p gnus-uu-tmp-dir))
1442         (error "Temp directory %s can't be written to" gnus-uu-tmp-dir)))
1443   (setq gnus-uu-work-dir 
1444         (concat gnus-uu-tmp-dir (make-temp-name "gnus")))
1445   (gnus-uu-add-file gnus-uu-work-dir)
1446   (if (not (file-directory-p gnus-uu-work-dir)) 
1447       (make-directory gnus-uu-work-dir))
1448   (set-file-modes gnus-uu-work-dir 448)
1449   (setq gnus-uu-work-dir (concat gnus-uu-work-dir "/")))
1450
1451 ;; Kills the temporary uu buffers, kills any processes, etc.
1452 (defun gnus-uu-clean-up ()
1453   (let (buf pst)
1454     (and gnus-uu-uudecode-process
1455          (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1456          (if (or (eq pst 'stop) (eq pst 'run))
1457              (delete-process gnus-uu-uudecode-process)))
1458     (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1459          (kill-buffer buf))))
1460
1461 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1462 ;; hasn't been deleted, if, for instance, the user terminated decoding
1463 ;; with `C-g'.
1464 (defun gnus-uu-check-for-generated-files ()
1465   (let (file dirs)
1466     (while gnus-uu-generated-file-list
1467       (setq file (car gnus-uu-generated-file-list))
1468       (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1469       (if (not (string-match "/\\.[\\.]?$" file))
1470           (progn
1471             (if (file-directory-p file)
1472                 (setq dirs (cons file dirs))
1473               (if (file-exists-p file)
1474                   (delete-file file))))))
1475     (setq dirs (nreverse dirs))
1476     (while dirs
1477       (setq file (car dirs))
1478       (setq dirs (cdr dirs))
1479       (if (file-directory-p file)
1480           (if (string-match "/$" file)
1481               (delete-directory (substring file 0 (match-beginning 0)))
1482             (delete-directory file))))))
1483
1484 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1485 ;; still exists upon exiting the newsgroup).
1486 (defun gnus-uu-add-file (file)
1487   (if (stringp file)
1488       (setq gnus-uu-generated-file-list 
1489             (cons file gnus-uu-generated-file-list))
1490     (setq gnus-uu-generated-file-list 
1491           (append file gnus-uu-generated-file-list))))
1492
1493 ;; Inputs an action and a file and returns a full command, putting
1494 ;; ticks round the file name and escaping any ticks in the file name.
1495 (defun gnus-uu-command (action file)
1496   (let ((ofile ""))
1497     (while (string-match "`\\|\"\\|\\$\\|\\\\" file)
1498       (progn
1499         (setq ofile
1500               (concat ofile (substring file 0 (match-beginning 0)) "\\"
1501                       (substring file (match-beginning 0) (match-end 0))))
1502         (setq file (substring file (1+ (match-beginning 0))))))
1503     (setq ofile (concat "\"" ofile file "\""))
1504     (if (string-match "%s" action)
1505         (format action ofile)
1506       (concat action " " ofile))))
1507
1508
1509 ;; Initializing
1510
1511 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1512 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1513
1514 \f
1515
1516 ;;;
1517 ;;; uuencoded posting
1518 ;;;
1519
1520 (require 'sendmail)
1521 (require 'rnews)
1522
1523 ;; Any function that is to be used as and encoding method will take two
1524 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1525 ;; and "spiral.jpg", respectively.) The function should return nil if
1526 ;; the encoding wasn't successful.
1527 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1528   "Function used for encoding binary files.
1529 There are three functions supplied with gnus-uu for encoding files:
1530 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1531 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
1532 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
1533 uuencode and adds MIME headers.")
1534
1535 (defvar gnus-uu-post-include-before-composing nil
1536   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1537 If this variable is t, you can either include an encoded file with
1538 \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1539
1540 (defvar gnus-uu-post-length 990
1541   "Maximum length of an article.
1542 The encoded file will be split into how many articles it takes to
1543 post the entire file.")
1544
1545 (defvar gnus-uu-post-threaded nil
1546   "Non-nil means that gnus-uu will post the encoded file in a thread.
1547 This may not be smart, as no other decoder I have seen are able to
1548 follow threads when collecting uuencoded articles. (Well, I have seen
1549 one package that does that - gnus-uu, but somehow, I don't think that 
1550 counts...) Default is nil.")
1551
1552 (defvar gnus-uu-post-separate-description t
1553   "Non-nil means that the description will be posted in a separate article.
1554 The first article will typically be numbered (0/x). If this variable
1555 is nil, the description the user enters will be included at the 
1556 beginning of the first article, which will be numbered (1/x). Default 
1557 is t.")
1558
1559 (defconst gnus-uu-post-binary-separator "--binary follows this line--")
1560 (defvar gnus-uu-post-message-id nil)
1561 (defvar gnus-uu-post-inserted-file-name nil)
1562 (defvar gnus-uu-winconf-post-news nil)
1563
1564 (defun gnus-uu-post-news ()
1565   "Compose an article and post an encoded file."
1566   (interactive)
1567   (setq gnus-uu-post-inserted-file-name nil)
1568   (setq gnus-uu-winconf-post-news (current-window-configuration))
1569
1570   (gnus-summary-post-news)
1571
1572   (use-local-map (copy-keymap (current-local-map)))
1573   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1574   (local-set-key "\C-c\C-f\C-a" 'gnus-uu-post-reply-summary)
1575   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1576   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1577   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1578       
1579   (if gnus-uu-post-include-before-composing
1580       (save-excursion (setq gnus-uu-post-inserted-file-name 
1581                             (gnus-uu-post-insert-binary)))))
1582
1583 (defun gnus-uu-post-insert-binary-in-article ()
1584   "Inserts an encoded file in the buffer.
1585 The user will be asked for a file name."
1586   (interactive)
1587   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1588       (error "Not in post-news buffer"))
1589   (save-excursion 
1590     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1591
1592 ;; Encodes with uuencode and substitutes all spaces with backticks.
1593 (defun gnus-uu-post-encode-uuencode (path file-name)
1594   (if (gnus-uu-post-encode-file "uuencode" path file-name)
1595       (progn
1596         (goto-char (point-min))
1597         (forward-line 1)
1598         (while (re-search-forward " " nil t)
1599           (replace-match "`"))
1600         t)))
1601
1602 ;; Encodes with uuencode and adds MIME headers.
1603 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1604   (if (gnus-uu-post-encode-uuencode path file-name)
1605       (progn
1606         (gnus-uu-post-make-mime file-name "x-uue")
1607         t)))
1608
1609 ;; Encodes with base64 and adds MIME headers
1610 (defun gnus-uu-post-encode-mime (path file-name)
1611   (if (gnus-uu-post-encode-file "mmencode" path file-name)
1612       (progn
1613         (gnus-uu-post-make-mime file-name "base64")
1614         t)))
1615
1616 ;; Adds MIME headers.
1617 (defun gnus-uu-post-make-mime (file-name encoding)
1618   (goto-char (point-min))
1619   (insert (format "Content-Type: %s; name=\"%s\"\n" 
1620                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list) 
1621                   file-name))
1622   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1623   (save-restriction
1624     (set-buffer gnus-post-news-buffer)
1625     (goto-char (point-min))
1626     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1627     (forward-line -1)
1628     (narrow-to-region 1 (point))
1629     (or (mail-fetch-field "mime-version")
1630         (progn
1631           (widen)
1632           (insert "MIME-Version: 1.0\n")))
1633     (widen)))
1634
1635 ;; Encodes a file PATH with COMMAND, leaving the result in the
1636 ;; current buffer.
1637 (defun gnus-uu-post-encode-file (command path file-name)
1638   (= 0 (call-process "sh" nil t nil "-c" 
1639                      (format "%s %s %s" command path file-name))))
1640
1641 (defun gnus-uu-post-news-inews ()
1642   "Posts the composed news article and encoded file.
1643 If no file has been included, the user will be asked for a file."
1644   (interactive)
1645   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1646       (error "Not in post news buffer"))
1647
1648   (let (file-name)
1649
1650     (if gnus-uu-post-inserted-file-name
1651         (setq file-name gnus-uu-post-inserted-file-name)
1652       (setq file-name (gnus-uu-post-insert-binary)))
1653   
1654     (if gnus-uu-post-threaded
1655         (let ((gnus-required-headers 
1656                (if (memq 'Message-ID gnus-required-headers)
1657                    gnus-required-headers
1658                  (cons 'Message-ID gnus-required-headers)))
1659               gnus-inews-article-hook elem)
1660
1661           (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1662                                             gnus-inews-article-hook
1663                                           (list gnus-inews-article-hook)))
1664           (setq gnus-inews-article-hook 
1665                 (cons
1666                  '(lambda ()
1667                     (save-excursion
1668                       (goto-char (point-min))
1669                       (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1670                           (setq gnus-uu-post-message-id 
1671                                 (buffer-substring 
1672                                  (match-beginning 1) (match-end 1)))
1673                         (setq gnus-uu-post-message-id nil))))
1674                  gnus-inews-article-hook))
1675           (gnus-uu-post-encoded file-name t))
1676       (gnus-uu-post-encoded file-name nil)))
1677   (setq gnus-uu-post-inserted-file-name nil)
1678   (and gnus-uu-winconf-post-news
1679        (set-window-configuration gnus-uu-winconf-post-news)))
1680       
1681 ;; Asks for a file to encode, encodes it and inserts the result in
1682 ;; the current buffer. Returns the file name the user gave.
1683 (defun gnus-uu-post-insert-binary ()
1684   (let ((uuencode-buffer-name "*uuencode buffer*")
1685         file-path post-buf uubuf file-name)
1686
1687     (setq file-path (read-file-name 
1688                      "What file do you want to encode? "))
1689     (if (not (file-exists-p file-path))
1690         (error "%s: No such file" file-path))
1691
1692     (goto-char (point-max))
1693     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1694     
1695     (if (string-match "^~/" file-path)
1696         (setq file-path (concat "$HOME" (substring file-path 1))))
1697     (if (string-match "/[^/]*$" file-path)
1698         (setq file-name (substring file-path (1+ (match-beginning 0))))
1699       (setq file-name file-path))
1700
1701     (unwind-protect
1702         (if (save-excursion
1703               (set-buffer (setq uubuf 
1704                                 (get-buffer-create uuencode-buffer-name)))
1705               (erase-buffer)
1706               (funcall gnus-uu-post-encode-method file-path file-name))
1707             (insert-buffer uubuf)
1708           (error "Encoding unsuccessful"))
1709       (kill-buffer uubuf))
1710     file-name))
1711
1712 ;; Posts the article and all of the encoded file.
1713 (defun gnus-uu-post-encoded (file-name &optional threaded)
1714   (let ((send-buffer-name "*uuencode send buffer*")
1715         (encoded-buffer-name "*encoded buffer*")
1716         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1717         (separator (concat mail-header-separator "\n\n"))
1718         file uubuf length parts header i end beg
1719         beg-line minlen buf post-buf whole-len beg-binary end-binary)
1720
1721     (setq post-buf (current-buffer))
1722
1723     (goto-char (point-min))
1724     (if (not (re-search-forward 
1725               (if gnus-uu-post-separate-description 
1726                   (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1727                           "$")
1728                 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1729         (error "Internal error: No binary/header separator"))
1730     (beginning-of-line)
1731     (forward-line 1)
1732     (setq beg-binary (point))
1733     (setq end-binary (point-max))
1734
1735     (save-excursion 
1736       (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1737       (erase-buffer)
1738       (insert-buffer-substring post-buf beg-binary end-binary)
1739       (goto-char (point-min))
1740       (setq length (count-lines 1 (point-max)))
1741       (setq parts (/ length gnus-uu-post-length))
1742       (if (not (< (% length gnus-uu-post-length) 4))
1743           (setq parts (1+ parts))))
1744
1745     (if gnus-uu-post-separate-description
1746         (forward-line -1))
1747     (kill-region (point) (point-max))
1748
1749     (goto-char (point-min))
1750     (re-search-forward 
1751      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1752     (beginning-of-line)
1753     (setq header (buffer-substring 1 (point)))
1754
1755     (goto-char (point-min))
1756     (if (not gnus-uu-post-separate-description)
1757         ()
1758       (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1759           (progn
1760             (end-of-line)
1761             (insert (format " (0/%d)" parts))))
1762       (gnus-inews-news))
1763
1764     (save-excursion
1765       (setq i 1)
1766       (setq beg 1)
1767       (while (not (> i parts))
1768         (set-buffer (get-buffer-create send-buffer-name))
1769         (erase-buffer)
1770         (insert header)
1771         (if (and threaded gnus-uu-post-message-id)
1772             (insert (format "References: %s\n" gnus-uu-post-message-id)))
1773         (insert separator)
1774         (setq whole-len
1775               (- 62 (length (format top-string "" file-name i parts ""))))
1776         (if (> 1 (setq minlen (/ whole-len 2)))
1777             (setq minlen 1))
1778         (setq 
1779          beg-line 
1780          (format top-string
1781                  (make-string minlen ?-) 
1782                  file-name i parts
1783                  (make-string 
1784                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1785
1786         (goto-char (point-min))
1787         (if (not (re-search-forward "^Subject: " nil t))
1788             ()
1789           (if (not threaded)
1790               (progn
1791                 (end-of-line)
1792                 (insert (format " (%d/%d)" i parts)))
1793             (if (or (and (= i 2) gnus-uu-post-separate-description)
1794                     (and (= i 1) (not gnus-uu-post-separate-description)))
1795                 (replace-match "Subject: Re: "))))
1796                   
1797         (goto-char (point-max))
1798         (save-excursion
1799           (set-buffer uubuf)
1800           (goto-char beg)
1801           (if (= i parts)
1802               (goto-char (point-max))
1803             (forward-line gnus-uu-post-length))
1804           (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1805               (forward-line -4))
1806           (setq end (point)))
1807         (insert-buffer-substring uubuf beg end)
1808         (insert beg-line)
1809         (insert "\n")
1810         (setq beg end)
1811         (setq i (1+ i))
1812         (goto-char (point-min))
1813         (re-search-forward
1814          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1815         (beginning-of-line)
1816         (forward-line 2)
1817         (if (re-search-forward 
1818              (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1819              nil t)
1820             (progn 
1821               (replace-match "")
1822               (forward-line 1)))
1823         (insert beg-line)
1824         (insert "\n")
1825         (gnus-inews-news)))
1826
1827     (and (setq buf (get-buffer send-buffer-name))
1828          (kill-buffer buf))
1829     (and (setq buf (get-buffer encoded-buffer-name))
1830          (kill-buffer buf))
1831
1832     (if (not gnus-uu-post-separate-description)
1833         (progn
1834           (set-buffer-modified-p nil)
1835           (and (fboundp 'bury-buffer) (bury-buffer))))))
1836
1837 (provide 'gnus-uu)
1838
1839 ;; gnus-uu.el ends here