*** 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)
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         (push (list (cons 'name result-file)
1189                     (cons 'article article))
1190               result-files)
1191         ;; Allow user-defined functions to be run on this file.
1192         (when gnus-uu-grabbed-file-functions
1193           (let ((funcs gnus-uu-grabbed-file-functions))
1194             (unless (listp funcs)
1195               (setq funcs (list funcs)))
1196             (while funcs
1197               (funcall (pop funcs) result-file))))
1198         ;; Check whether we have decoded enough articles.
1199         (and limit (= (length result-files) limit)
1200              (setq articles nil)))
1201
1202       ;; If this is the last article to be decoded, and
1203       ;; we still haven't reached the end, then we delete
1204       ;; the partially decoded file.
1205       (and (or (eq state 'last) (eq state 'first-and-last))
1206            (not (memq 'end process-state))
1207            result-file 
1208            (file-exists-p result-file)
1209            (delete-file result-file))
1210
1211       ;; If this was a file of the wrong sort, then 
1212       (when (and (or (memq 'wrong-type process-state)
1213                      (memq 'error process-state))
1214                  gnus-uu-unmark-articles-not-decoded)
1215         (gnus-summary-tick-article article t))
1216
1217       ;; Set the new series state.
1218       (if (and (not has-been-begin)
1219                (not sloppy)
1220                (or (memq 'end process-state)
1221                    (memq 'middle process-state)))
1222           (progn
1223             (setq process-state (list 'error))
1224             (message "No begin part at the beginning")
1225             (sleep-for 2))
1226         (setq state 'middle)))
1227
1228     ;; When there are no result-files, then something must be wrong.
1229     (if result-files
1230         (message "")
1231       (cond
1232        ((not has-been-begin)
1233         (message "Wrong type file"))
1234        ((memq 'error process-state)
1235         (message "An error occurred during decoding"))
1236        ((not (or (memq 'ok process-state) 
1237                  (memq 'end process-state)))
1238         (message "End of articles reached before end of file")))
1239       ;; Make unsuccessfully decoded articles unread.
1240       (when gnus-uu-unmark-articles-not-decoded
1241         (while article-series
1242           (gnus-summary-tick-article (pop article-series) t))))
1243
1244     result-files))
1245
1246 (defun gnus-uu-grab-view (file)
1247   "View FILE using the gnus-uu methods."
1248   (let ((action (gnus-uu-get-action file)))
1249     (gnus-execute-command
1250      (if (string-match "%" action)
1251          (format action file)
1252        (concat action " " file))
1253      (eq gnus-view-pseudos 'not-confirm))))
1254
1255 (defun gnus-uu-grab-move (file)
1256   "Move FILE to somewhere."
1257   (when gnus-uu-default-dir
1258     (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1259                            (file-name-nondirectory file))))
1260       (rename-file file to-file)
1261       (unless (file-exists-p file)
1262         (make-symbolic-link to-file file)))))
1263
1264 (defun gnus-uu-part-number (article)
1265   (let ((subject (mail-header-subject (gnus-summary-article-header article))))
1266     (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1267                       subject)
1268         (substring subject (match-beginning 0) (match-end 0))
1269       "")))
1270
1271 (defun gnus-uu-uudecode-sentinel (process event)
1272   (delete-process (get-process process)))
1273
1274 (defun gnus-uu-uustrip-article (process-buffer in-state)
1275   ;; Uudecodes a file asynchronously.
1276   (let ((state (list 'ok))
1277         (process-connection-type nil)
1278         start-char pst name-beg name-end)
1279     (save-excursion
1280       (set-buffer process-buffer)
1281       (let ((case-fold-search nil)
1282             (buffer-read-only nil))
1283
1284         (goto-char (point-min))
1285
1286         (if gnus-uu-kill-carriage-return
1287             (progn
1288               (while (search-forward "\r" nil t)
1289                 (delete-backward-char 1))
1290               (goto-char (point-min))))
1291
1292         (if (not (re-search-forward gnus-uu-begin-string nil t))
1293             (if (not (re-search-forward gnus-uu-body-line nil t))
1294                 (setq state (list 'wrong-type))))
1295      
1296         (if (memq 'wrong-type state)
1297             ()
1298           (beginning-of-line)
1299           (setq start-char (point))
1300
1301           (if (looking-at gnus-uu-begin-string)
1302               (progn 
1303                 (setq name-end (match-end 1)
1304                       name-beg (match-beginning 1))
1305                 ;; Remove any non gnus-uu-body-line right after start.
1306                 (forward-line 1)
1307                 (or (looking-at gnus-uu-body-line)
1308                     (gnus-delete-line))
1309  
1310                 ;; Replace any slashes and spaces in file names before decoding
1311                 (goto-char name-beg)
1312                 (while (re-search-forward "/" name-end t)
1313                   (replace-match ","))
1314                 (goto-char name-beg)
1315                 (while (re-search-forward " " name-end t)
1316                   (replace-match "_"))
1317                 (goto-char name-beg)
1318                 (if (re-search-forward "_*$" name-end t)
1319                     (replace-match ""))
1320
1321                 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1322                 (and gnus-uu-uudecode-process
1323                      (setq pst (process-status 
1324                                 (or gnus-uu-uudecode-process "nevair")))
1325                      (if (or (eq pst 'stop) (eq pst 'run))
1326                          (progn
1327                            (delete-process gnus-uu-uudecode-process)
1328                            (gnus-uu-unmark-list-of-grabbed t))))
1329                 (if (get-process "*uudecode*")
1330                     (delete-process "*uudecode*"))
1331                 (setq gnus-uu-uudecode-process
1332                       (start-process 
1333                        "*uudecode*" 
1334                        (get-buffer-create gnus-uu-output-buffer-name)
1335                        "sh" "-c" 
1336                        (format "cd %s ; uudecode" gnus-uu-work-dir)))
1337                 (set-process-sentinel 
1338                  gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1339                 (setq state (list 'begin))
1340                 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1341             (setq state (list 'middle)))
1342         
1343           (goto-char (point-max))
1344
1345           (re-search-backward 
1346            (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1347           (beginning-of-line)
1348
1349           (if (looking-at gnus-uu-end-string)
1350               (setq state (cons 'end state)))
1351           (forward-line 1)
1352
1353           (and gnus-uu-uudecode-process
1354                (setq pst (process-status 
1355                           (or gnus-uu-uudecode-process "nevair")))
1356                (if (or (eq pst 'run) (eq pst 'stop))
1357                    (progn
1358                      (if gnus-uu-correct-stripped-uucode
1359                          (progn
1360                            (gnus-uu-check-correct-stripped-uucode 
1361                             start-char (point))
1362                            (goto-char (point-max))
1363                            (re-search-backward 
1364                             (concat gnus-uu-body-line "\\|" 
1365                                     gnus-uu-end-string) 
1366                             nil t)
1367                            (forward-line 1)))
1368
1369                      (condition-case nil
1370                          (process-send-region gnus-uu-uudecode-process 
1371                                               start-char (point))
1372                        (error 
1373                         (progn 
1374                           (delete-process gnus-uu-uudecode-process)
1375                           (message "gnus-uu: Couldn't uudecode")
1376                                         ;                         (sleep-for 2)
1377                           (setq state (list 'wrong-type)))))
1378
1379                      (when (memq 'end state)
1380                        (while (memq (process-status gnus-uu-uudecode-process)
1381                                     '(open run))
1382                          (accept-process-output gnus-uu-uudecode-process 1))))
1383                  (setq state (list 'wrong-type))))
1384           (if (not gnus-uu-uudecode-process)
1385               (setq state (list 'wrong-type)))))
1386
1387       (if (memq 'begin state)
1388           (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1389         state))))
1390
1391 ;; This function is used by `gnus-uu-grab-articles' to treat
1392 ;; a shared article.
1393 (defun gnus-uu-unshar-article (process-buffer in-state)
1394   (let ((state (list 'ok))
1395         start-char)
1396     (save-excursion
1397       (set-buffer process-buffer)
1398       (goto-char (point-min))
1399       (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1400           (setq state (list 'wrong-type))
1401         (beginning-of-line)
1402         (setq start-char (point))
1403         (call-process-region 
1404          start-char (point-max) "sh" nil 
1405          (get-buffer-create gnus-uu-output-buffer-name) nil 
1406          "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1407     state))
1408
1409 ;; Returns the name of what the shar file is going to unpack.
1410 (defun gnus-uu-find-name-in-shar ()
1411   (let ((oldpoint (point))
1412         res)
1413     (goto-char (point-min))
1414     (if (re-search-forward gnus-uu-shar-name-marker nil t)
1415         (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1416     (goto-char oldpoint)
1417     res))
1418
1419 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1420 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1421 ;; found, or the name of the command to run if such a rule is found.
1422 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1423   (let ((action-list (copy-sequence file-action-list))
1424         (case-fold-search t)
1425         rule action)
1426     (and 
1427      (or no-ignore 
1428          (and (not 
1429                (and gnus-uu-ignore-files-by-name
1430                     (string-match gnus-uu-ignore-files-by-name file-name)))
1431               (not 
1432                (and gnus-uu-ignore-files-by-type
1433                     (string-match gnus-uu-ignore-files-by-type 
1434                                   (or (gnus-uu-choose-action 
1435                                        file-name gnus-uu-ext-to-mime-list t) 
1436                                       ""))))))
1437      (while (not (or (eq action-list ()) action))
1438        (setq rule (car action-list))
1439        (setq action-list (cdr action-list))
1440        (if (string-match (car rule) file-name)
1441            (setq action (cadr rule)))))
1442     action))
1443
1444 (defun gnus-uu-treat-archive (file-path)
1445   ;; Unpacks an archive. Returns t if unpacking is successful.
1446   (let ((did-unpack t)
1447         action command dir)
1448     (setq action (gnus-uu-choose-action 
1449                   file-path (append gnus-uu-user-archive-rules
1450                                     (if gnus-uu-ignore-default-archive-rules
1451                                         nil
1452                                       gnus-uu-default-archive-rules))))
1453
1454     (if (not action) (error "No unpackers for the file %s" file-path))
1455
1456     (string-match "/[^/]*$" file-path)
1457     (setq dir (substring file-path 0 (match-beginning 0)))
1458
1459     (if (member action gnus-uu-destructive-archivers)
1460         (copy-file file-path (concat file-path "~") t))
1461
1462     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1463
1464     (save-excursion
1465       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1466       (erase-buffer))
1467
1468     (message "Unpacking: %s..." (gnus-uu-command action file-path))
1469
1470     (if (= 0 (call-process "sh" nil 
1471                            (get-buffer-create gnus-uu-output-buffer-name)
1472                            nil "-c" command))
1473         (message "")
1474       (message "Error during unpacking of archive")
1475       (setq did-unpack nil))
1476
1477     (if (member action gnus-uu-destructive-archivers)
1478         (rename-file (concat file-path "~") file-path t))
1479
1480     did-unpack))
1481
1482 (defun gnus-uu-dir-files (dir)
1483   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1484         files file)
1485     (while dirs
1486       (if (file-directory-p (setq file (car dirs)))
1487           (setq files (append files (gnus-uu-dir-files file)))
1488         (setq files (cons file files)))
1489       (setq dirs (cdr dirs)))
1490     files))
1491
1492 (defun gnus-uu-unpack-files (files &optional ignore)
1493   ;; Go through FILES and look for files to unpack. 
1494   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1495          (ofiles files)
1496          file did-unpack)
1497     (gnus-uu-add-file totfiles) 
1498     (while files
1499       (setq file (cdr (assq 'name (car files))))
1500       (if (and (not (member file ignore))
1501                (equal (gnus-uu-get-action (file-name-nondirectory file))
1502                       "gnus-uu-archive"))
1503           (progn
1504             (setq did-unpack (cons file did-unpack))
1505             (or (gnus-uu-treat-archive file)
1506                 (message "Error during unpacking of %s" file))
1507             (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1508                    (nfiles newfiles))
1509               (gnus-uu-add-file newfiles)
1510               (while nfiles
1511                 (or (member (car nfiles) totfiles)
1512                     (setq ofiles (cons (list (cons 'name (car nfiles))
1513                                              (cons 'original file))
1514                                        ofiles)))
1515                 (setq nfiles (cdr nfiles)))
1516               (setq totfiles newfiles))))
1517       (setq files (cdr files)))
1518     (if did-unpack 
1519         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1520       ofiles)))
1521
1522 (defun gnus-uu-ls-r (dir)
1523   (let* ((files (gnus-uu-directory-files dir t))
1524          (ofiles files))
1525     (while files
1526       (if (file-directory-p (car files))
1527           (progn
1528             (setq ofiles (delete (car files) ofiles))
1529             (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1530       (setq files (cdr files)))
1531     ofiles))
1532
1533 ;; Various stuff
1534
1535 (defun gnus-uu-directory-files (dir &optional full)
1536   (let (files out file)
1537     (setq files (directory-files dir full))
1538     (while files
1539       (setq file (car files))
1540       (setq files (cdr files))
1541       (or (string-match "/\\.\\.?$" file)
1542           (setq out (cons file out))))
1543     (setq out (nreverse out))
1544     out))
1545
1546 (defun gnus-uu-check-correct-stripped-uucode (start end)
1547   (let (found beg length)
1548     (if (not gnus-uu-correct-stripped-uucode)
1549         ()
1550       (goto-char start)
1551
1552       (if (re-search-forward " \\|`" end t)
1553           (progn
1554             (goto-char start)
1555             (while (not (eobp))
1556               (progn
1557                 (if (looking-at "\n") (replace-match ""))
1558                 (forward-line 1))))
1559             
1560         (while (not (eobp))
1561           (if (looking-at (concat gnus-uu-begin-string "\\|" 
1562                                   gnus-uu-end-string))
1563               ()
1564             (if (not found)
1565                 (progn
1566                   (beginning-of-line)
1567                   (setq beg (point))
1568                   (end-of-line)
1569                   (setq length (- (point) beg))))
1570             (setq found t)
1571             (beginning-of-line)
1572             (setq beg (point))
1573             (end-of-line)
1574             (if (not (= length (- (point) beg)))
1575                 (insert (make-string (- length (- (point) beg)) ? ))))
1576           (forward-line 1))))))
1577
1578 (defvar gnus-uu-tmp-alist nil)
1579
1580 (defun gnus-uu-initialize (&optional scan)
1581   (let (entry)
1582     (if (and (not scan)
1583              (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1584                  (if (file-exists-p (cdr entry))
1585                      (setq gnus-uu-work-dir (cdr entry))
1586                    (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1587                    nil)))
1588         t
1589       (setq gnus-uu-tmp-dir (file-name-as-directory 
1590                              (expand-file-name gnus-uu-tmp-dir)))
1591       (if (not (file-directory-p gnus-uu-tmp-dir))
1592           (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1593         (if (not (file-writable-p gnus-uu-tmp-dir))
1594             (error "Temp directory %s can't be written to" 
1595                    gnus-uu-tmp-dir)))
1596
1597       (setq gnus-uu-work-dir 
1598             (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1599       (gnus-uu-add-file gnus-uu-work-dir)
1600       (if (not (file-directory-p gnus-uu-work-dir)) 
1601           (gnus-make-directory gnus-uu-work-dir))
1602       (set-file-modes gnus-uu-work-dir 448)
1603       (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1604       (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1605                                     gnus-uu-tmp-alist)))))
1606
1607
1608 ;; Kills the temporary uu buffers, kills any processes, etc.
1609 (defun gnus-uu-clean-up ()
1610   (let (buf pst)
1611     (and gnus-uu-uudecode-process
1612          (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1613          (if (or (eq pst 'stop) (eq pst 'run))
1614              (delete-process gnus-uu-uudecode-process)))
1615     (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1616          (kill-buffer buf))))
1617
1618 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1619 ;; hasn't been deleted, if, for instance, the user terminated decoding
1620 ;; with `C-g'.
1621 (defun gnus-uu-check-for-generated-files ()
1622   (let (file dirs)
1623     ;; First delete the generated files.
1624     (while (setq file (pop gnus-uu-generated-file-list))
1625       (unless (string-match "/\\.[\\.]?$" file)
1626         (if (file-directory-p file)
1627             (push file dirs)
1628           (when (file-exists-p file)
1629             (delete-file file)))))
1630     ;; Then delete the directories.
1631     (setq dirs (nreverse dirs))
1632     (while (setq file (pop dirs))
1633       (delete-directory (directory-file-name file)))))
1634
1635 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1636 ;; still exists upon exiting the newsgroup).
1637 (defun gnus-uu-add-file (file)
1638   (if (stringp file)
1639       (setq gnus-uu-generated-file-list 
1640             (cons file gnus-uu-generated-file-list))
1641     (setq gnus-uu-generated-file-list 
1642           (append file gnus-uu-generated-file-list))))
1643
1644 ;; Inputs an action and a file and returns a full command, putting
1645 ;; quotes round the file name and escaping any quotes in the file name.
1646 (defun gnus-uu-command (action file)
1647   (let ((ofile ""))
1648     (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1649       (progn
1650         (setq ofile
1651               (concat ofile (substring file 0 (match-beginning 0)) "\\"
1652                       (substring file (match-beginning 0) (match-end 0))))
1653         (setq file (substring file (1+ (match-beginning 0))))))
1654     (setq ofile (concat "\"" ofile file "\""))
1655     (if (string-match "%s" action)
1656         (format action ofile)
1657       (concat action " " ofile))))
1658
1659
1660 ;; Initializing
1661
1662 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1663 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1664
1665 \f
1666
1667 ;;;
1668 ;;; uuencoded posting
1669 ;;;
1670
1671 (require 'sendmail)
1672 (require 'rnews)
1673
1674 ;; Any function that is to be used as and encoding method will take two
1675 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1676 ;; and "spiral.jpg", respectively.) The function should return nil if
1677 ;; the encoding wasn't successful.
1678 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1679   "Function used for encoding binary files.
1680 There are three functions supplied with gnus-uu for encoding files:
1681 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1682 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
1683 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
1684 uuencode and adds MIME headers.")
1685
1686 (defvar gnus-uu-post-include-before-composing nil
1687   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1688 If this variable is t, you can either include an encoded file with
1689 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1690
1691 (defvar gnus-uu-post-length 990
1692   "Maximum length of an article.
1693 The encoded file will be split into how many articles it takes to
1694 post the entire file.")
1695
1696 (defvar gnus-uu-post-threaded nil
1697   "Non-nil means that gnus-uu will post the encoded file in a thread.
1698 This may not be smart, as no other decoder I have seen are able to
1699 follow threads when collecting uuencoded articles. (Well, I have seen
1700 one package that does that - gnus-uu, but somehow, I don't think that 
1701 counts...) Default is nil.")
1702
1703 (defvar gnus-uu-post-separate-description t
1704   "Non-nil means that the description will be posted in a separate article.
1705 The first article will typically be numbered (0/x). If this variable
1706 is nil, the description the user enters will be included at the 
1707 beginning of the first article, which will be numbered (1/x). Default 
1708 is t.")
1709
1710 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1711 (defvar gnus-uu-post-message-id nil)
1712 (defvar gnus-uu-post-inserted-file-name nil)
1713 (defvar gnus-uu-winconf-post-news nil)
1714
1715 (defun gnus-uu-post-news ()
1716   "Compose an article and post an encoded file."
1717   (interactive)
1718   (setq gnus-uu-post-inserted-file-name nil)
1719   (setq gnus-uu-winconf-post-news (current-window-configuration))
1720
1721   (gnus-summary-post-news)
1722
1723   (use-local-map (copy-keymap (current-local-map)))
1724   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1725   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1726   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1727   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1728       
1729   (if gnus-uu-post-include-before-composing
1730       (save-excursion (setq gnus-uu-post-inserted-file-name 
1731                             (gnus-uu-post-insert-binary)))))
1732
1733 (defun gnus-uu-post-insert-binary-in-article ()
1734   "Inserts an encoded file in the buffer.
1735 The user will be asked for a file name."
1736   (interactive)
1737   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1738       (error "Not in post-news buffer"))
1739   (save-excursion 
1740     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1741
1742 ;; Encodes with uuencode and substitutes all spaces with backticks.
1743 (defun gnus-uu-post-encode-uuencode (path file-name)
1744   (if (gnus-uu-post-encode-file "uuencode" path file-name)
1745       (progn
1746         (goto-char (point-min))
1747         (forward-line 1)
1748         (while (re-search-forward " " nil t)
1749           (replace-match "`"))
1750         t)))
1751
1752 ;; Encodes with uuencode and adds MIME headers.
1753 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1754   (if (gnus-uu-post-encode-uuencode path file-name)
1755       (progn
1756         (gnus-uu-post-make-mime file-name "x-uue")
1757         t)))
1758
1759 ;; Encodes with base64 and adds MIME headers
1760 (defun gnus-uu-post-encode-mime (path file-name)
1761   (if (gnus-uu-post-encode-file "mmencode" path file-name)
1762       (progn
1763         (gnus-uu-post-make-mime file-name "base64")
1764         t)))
1765
1766 ;; Adds MIME headers.
1767 (defun gnus-uu-post-make-mime (file-name encoding)
1768   (goto-char (point-min))
1769   (insert (format "Content-Type: %s; name=\"%s\"\n" 
1770                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list) 
1771                   file-name))
1772   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1773   (save-restriction
1774     (set-buffer gnus-post-news-buffer)
1775     (goto-char (point-min))
1776     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1777     (forward-line -1)
1778     (narrow-to-region 1 (point))
1779     (or (mail-fetch-field "mime-version")
1780         (progn
1781           (widen)
1782           (insert "MIME-Version: 1.0\n")))
1783     (widen)))
1784
1785 ;; Encodes a file PATH with COMMAND, leaving the result in the
1786 ;; current buffer.
1787 (defun gnus-uu-post-encode-file (command path file-name)
1788   (= 0 (call-process "sh" nil t nil "-c" 
1789                      (format "%s %s %s" command path file-name))))
1790
1791 (defun gnus-uu-post-news-inews ()
1792   "Posts the composed news article and encoded file.
1793 If no file has been included, the user will be asked for a file."
1794   (interactive)
1795   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1796       (error "Not in post news buffer"))
1797
1798   (let (file-name)
1799
1800     (if gnus-uu-post-inserted-file-name
1801         (setq file-name gnus-uu-post-inserted-file-name)
1802       (setq file-name (gnus-uu-post-insert-binary)))
1803   
1804     (if gnus-uu-post-threaded
1805         (let ((gnus-required-headers 
1806                (if (memq 'Message-ID gnus-required-headers)
1807                    gnus-required-headers
1808                  (cons 'Message-ID gnus-required-headers)))
1809               gnus-inews-article-hook)
1810
1811           (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1812                                             gnus-inews-article-hook
1813                                           (list gnus-inews-article-hook)))
1814           (setq gnus-inews-article-hook 
1815                 (cons
1816                  '(lambda ()
1817                     (save-excursion
1818                       (goto-char (point-min))
1819                       (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1820                           (setq gnus-uu-post-message-id 
1821                                 (buffer-substring 
1822                                  (match-beginning 1) (match-end 1)))
1823                         (setq gnus-uu-post-message-id nil))))
1824                  gnus-inews-article-hook))
1825           (gnus-uu-post-encoded file-name t))
1826       (gnus-uu-post-encoded file-name nil)))
1827   (setq gnus-uu-post-inserted-file-name nil)
1828   (and gnus-uu-winconf-post-news
1829        (set-window-configuration gnus-uu-winconf-post-news)))
1830       
1831 ;; Asks for a file to encode, encodes it and inserts the result in
1832 ;; the current buffer. Returns the file name the user gave.
1833 (defun gnus-uu-post-insert-binary ()
1834   (let ((uuencode-buffer-name "*uuencode buffer*")
1835         file-path uubuf file-name)
1836
1837     (setq file-path (read-file-name 
1838                      "What file do you want to encode? "))
1839     (if (not (file-exists-p file-path))
1840         (error "%s: No such file" file-path))
1841
1842     (goto-char (point-max))
1843     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1844     
1845     (if (string-match "^~/" file-path)
1846         (setq file-path (concat "$HOME" (substring file-path 1))))
1847     (if (string-match "/[^/]*$" file-path)
1848         (setq file-name (substring file-path (1+ (match-beginning 0))))
1849       (setq file-name file-path))
1850
1851     (unwind-protect
1852         (if (save-excursion
1853               (set-buffer (setq uubuf 
1854                                 (get-buffer-create uuencode-buffer-name)))
1855               (erase-buffer)
1856               (funcall gnus-uu-post-encode-method file-path file-name))
1857             (insert-buffer-substring uubuf)
1858           (error "Encoding unsuccessful"))
1859       (kill-buffer uubuf))
1860     file-name))
1861
1862 ;; Posts the article and all of the encoded file.
1863 (defun gnus-uu-post-encoded (file-name &optional threaded)
1864   (let ((send-buffer-name "*uuencode send buffer*")
1865         (encoded-buffer-name "*encoded buffer*")
1866         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1867         (separator (concat mail-header-separator "\n\n"))
1868         uubuf length parts header i end beg
1869         beg-line minlen buf post-buf whole-len beg-binary end-binary)
1870
1871     (setq post-buf (current-buffer))
1872
1873     (goto-char (point-min))
1874     (if (not (re-search-forward 
1875               (if gnus-uu-post-separate-description 
1876                   (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1877                           "$")
1878                 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1879         (error "Internal error: No binary/header separator"))
1880     (beginning-of-line)
1881     (forward-line 1)
1882     (setq beg-binary (point))
1883     (setq end-binary (point-max))
1884
1885     (save-excursion 
1886       (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1887       (erase-buffer)
1888       (insert-buffer-substring post-buf beg-binary end-binary)
1889       (goto-char (point-min))
1890       (setq length (count-lines 1 (point-max)))
1891       (setq parts (/ length gnus-uu-post-length))
1892       (if (not (< (% length gnus-uu-post-length) 4))
1893           (setq parts (1+ parts))))
1894
1895     (if gnus-uu-post-separate-description
1896         (forward-line -1))
1897     (kill-region (point) (point-max))
1898
1899     (goto-char (point-min))
1900     (re-search-forward 
1901      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1902     (beginning-of-line)
1903     (setq header (buffer-substring 1 (point)))
1904
1905     (goto-char (point-min))
1906     (if (not gnus-uu-post-separate-description)
1907         ()
1908       (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1909           (progn
1910             (end-of-line)
1911             (insert (format " (0/%d)" parts))))
1912       (gnus-inews-news))
1913
1914     (save-excursion
1915       (setq i 1)
1916       (setq beg 1)
1917       (while (not (> i parts))
1918         (set-buffer (get-buffer-create send-buffer-name))
1919         (erase-buffer)
1920         (insert header)
1921         (if (and threaded gnus-uu-post-message-id)
1922             (insert (format "References: %s\n" gnus-uu-post-message-id)))
1923         (insert separator)
1924         (setq whole-len
1925               (- 62 (length (format top-string "" file-name i parts ""))))
1926         (if (> 1 (setq minlen (/ whole-len 2)))
1927             (setq minlen 1))
1928         (setq 
1929          beg-line 
1930          (format top-string
1931                  (make-string minlen ?-) 
1932                  file-name i parts
1933                  (make-string 
1934                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1935
1936         (goto-char (point-min))
1937         (if (not (re-search-forward "^Subject: " nil t))
1938             ()
1939           (if (not threaded)
1940               (progn
1941                 (end-of-line)
1942                 (insert (format " (%d/%d)" i parts)))
1943             (if (or (and (= i 2) gnus-uu-post-separate-description)
1944                     (and (= i 1) (not gnus-uu-post-separate-description)))
1945                 (replace-match "Subject: Re: "))))
1946                   
1947         (goto-char (point-max))
1948         (save-excursion
1949           (set-buffer uubuf)
1950           (goto-char beg)
1951           (if (= i parts)
1952               (goto-char (point-max))
1953             (forward-line gnus-uu-post-length))
1954           (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1955               (forward-line -4))
1956           (setq end (point)))
1957         (insert-buffer-substring uubuf beg end)
1958         (insert beg-line)
1959         (insert "\n")
1960         (setq beg end)
1961         (setq i (1+ i))
1962         (goto-char (point-min))
1963         (re-search-forward
1964          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1965         (beginning-of-line)
1966         (forward-line 2)
1967         (if (re-search-forward 
1968              (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1969              nil t)
1970             (progn 
1971               (replace-match "")
1972               (forward-line 1)))
1973         (insert beg-line)
1974         (insert "\n")
1975         (gnus-inews-news)))
1976
1977     (and (setq buf (get-buffer send-buffer-name))
1978          (kill-buffer buf))
1979     (and (setq buf (get-buffer encoded-buffer-name))
1980          (kill-buffer buf))
1981
1982     (if (not gnus-uu-post-separate-description)
1983         (progn
1984           (set-buffer-modified-p nil)
1985           (and (fboundp 'bury-buffer) (bury-buffer))))))
1986
1987 (provide 'gnus-uu)
1988
1989 ;; gnus-uu.el ends here