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