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