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