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