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