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