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