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