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