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