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