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