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