*** 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 ;; Version: v3.0
7 ;; Keyword: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;;; Code: 
28
29 (require 'gnus)
30 (require 'gnus-msg)
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               subj mark)
978           (goto-char (point-min))
979           (while (not (eobp))
980             (and (setq subj (gnus-summary-article-subject))
981                  (string-match subject subj)
982                  (or (not only-unread)
983                      (= (setq mark (gnus-summary-article-mark)) 
984                         gnus-unread-mark)
985                      (= mark gnus-ticked-mark)
986                      (= mark gnus-dormant-mark))
987                  (setq list-of-subjects 
988                        (cons (cons subj (gnus-summary-article-number))
989                              list-of-subjects)))
990             (forward-line 1)))
991
992         ;; Expand numbers, sort, and return the list of article
993         ;; numbers.
994         (mapcar (lambda (sub) (cdr sub)) 
995                 (sort (gnus-uu-expand-numbers 
996                        list-of-subjects
997                        (not do-not-translate)) 
998                       'gnus-uu-string<))))))
999
1000 (defun gnus-uu-expand-numbers (string-list &optional translate)
1001   ;; Takes a list of strings and "expands" all numbers in all the
1002   ;; strings.  That is, this function makes all numbers equal length by
1003   ;; prepending lots of zeroes before each number. This is to ease later
1004   ;; sorting to find out what sequence the articles are supposed to be
1005   ;; decoded in. Returns the list of expanded strings.
1006   (let ((out-list string-list)
1007         string)
1008     (save-excursion
1009       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1010       (buffer-disable-undo (current-buffer))
1011       (while string-list
1012         (erase-buffer)
1013         (insert (car (car string-list)))
1014         ;; Translate multiple spaces to one space.
1015         (goto-char (point-min))
1016         (while (re-search-forward "[ \t]+" nil t)
1017           (replace-match " "))
1018         ;; Translate all characters to "a".
1019         (goto-char (point-min))
1020         (if translate 
1021             (while (re-search-forward "[A-Za-z]" nil t)
1022               (replace-match "a" t t)))
1023         ;; Expand numbers.
1024         (goto-char (point-min))
1025         (while (re-search-forward "[0-9]+" nil t)
1026           (replace-match  
1027            (format "%06d" 
1028                    (string-to-int (buffer-substring 
1029                                    (match-beginning 0) (match-end 0))))))
1030         (setq string (buffer-substring 1 (point-max)))
1031         (setcar (car string-list) string)
1032         (setq string-list (cdr string-list))))
1033     out-list))
1034
1035
1036 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1037 ;; function.  It takes a list of articles to be grabbed and a function
1038 ;; to apply to each article.
1039 ;;
1040 ;; The function to be called should take two parameters.  The first
1041 ;; parameter is the article buffer. The function should leave the
1042 ;; result, if any, in this buffer. Most treatment functions will just
1043 ;; generate files...
1044 ;;
1045 ;; The second parameter is the state of the list of articles, and can
1046 ;; have four values: `first', `middle', `last' and `first-and-last'.
1047 ;;
1048 ;; The function should return a list. The list may contain the
1049 ;; following symbols:
1050 ;; `error' if an error occurred
1051 ;; `begin' if the beginning of an encoded file has been received
1052 ;;   If the list returned contains a `begin', the first element of
1053 ;;   the list *must* be a string with the file name of the decoded
1054 ;;   file.
1055 ;; `end' if the the end of an encoded file has been received
1056 ;; `middle' if the article was a body part of an encoded file
1057 ;; `wrong-type' if the article was not a part of an encoded file
1058 ;; `ok', which can be used everything is ok
1059
1060 (defvar gnus-uu-has-been-grabbed nil)
1061
1062 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1063   (let (art)
1064     (if (not (and gnus-uu-has-been-grabbed
1065                   gnus-uu-unmark-articles-not-decoded))
1066         ()
1067       (if dont-unmark-last-article
1068           (progn
1069             (setq art (car gnus-uu-has-been-grabbed))
1070             (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
1071       (while gnus-uu-has-been-grabbed
1072         (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1073         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1074       (if dont-unmark-last-article
1075           (setq gnus-uu-has-been-grabbed (list art))))))
1076
1077 ;; This function takes a list of articles and a function to apply to
1078 ;; each article grabbed. 
1079 ;; 
1080 ;; This function returns a list of files decoded if the grabbing and
1081 ;; the process-function has been successful and nil otherwise.
1082 (defun gnus-uu-grab-articles 
1083   (articles process-function &optional sloppy limit no-errors)
1084   (let ((state 'first) 
1085         has-been-begin article result-file result-files process-state 
1086         article-buffer)
1087  
1088     (if (not (gnus-server-opened gnus-current-select-method))
1089         (progn
1090           (gnus-start-news-server)
1091           (gnus-request-group gnus-newsgroup-name)))
1092
1093     (setq gnus-uu-has-been-grabbed nil)
1094
1095     (while (and articles 
1096                 (not (memq 'error process-state))
1097                 (or sloppy
1098                     (not (memq 'end process-state))))
1099
1100       (setq article (car articles))
1101       (setq articles (cdr articles))
1102       (setq gnus-uu-has-been-grabbed (cons article gnus-uu-has-been-grabbed))
1103
1104       (if (eq articles ()) 
1105           (if (eq state 'first)
1106               (setq state 'first-and-last)
1107             (setq state 'last)))
1108
1109       (message "Getting article %d, %s" article (gnus-uu-part-number article))
1110
1111       (if (not (= (or gnus-current-article 0) article))
1112           (let ((nntp-async-number nil))
1113             (gnus-request-article article gnus-newsgroup-name
1114                                   nntp-server-buffer)
1115             (setq gnus-last-article gnus-current-article)
1116             (setq gnus-current-article article)
1117             (setq gnus-article-current (cons gnus-newsgroup-name article))
1118             (if (stringp nntp-server-buffer)
1119                 (setq article-buffer nntp-server-buffer)
1120               (setq article-buffer (buffer-name nntp-server-buffer))))
1121         (gnus-summary-stop-page-breaking)
1122         (setq article-buffer gnus-article-buffer))
1123
1124       (buffer-disable-undo article-buffer)
1125       ;; Mark article as read.
1126       (and (memq article gnus-newsgroup-processable)
1127            (gnus-summary-remove-process-mark article))
1128       (run-hooks 'gnus-mark-article-hook)
1129
1130       (setq process-state (funcall process-function article-buffer state))
1131
1132       (if (or (memq 'begin process-state)
1133               (and (or (eq state 'first) (eq state 'first-and-last))
1134                    (memq 'ok process-state)))
1135           (progn
1136             (if has-been-begin
1137                 (if (and result-file (file-exists-p result-file)) 
1138                     (delete-file result-file)))
1139             (if (memq 'begin process-state)
1140                 (setq result-file (car process-state)))
1141             (setq has-been-begin t)))
1142
1143       (if (memq 'end process-state)
1144           (progn
1145             (setq gnus-uu-has-been-grabbed nil)
1146             (setq result-files (cons (list (cons 'name result-file)
1147                                            (cons 'article article))
1148                                      result-files))
1149             (setq has-been-begin nil)
1150             (and limit (= (length result-files) limit)
1151                  (setq articles nil))))
1152
1153       (if (and (or (eq state 'last) (eq state 'first-and-last))
1154                (not (memq 'end process-state)))
1155           (if (and result-file (file-exists-p result-file))
1156               (delete-file result-file)))
1157
1158       (if (not (memq 'wrong-type process-state))
1159           ()
1160         (if gnus-uu-unmark-articles-not-decoded
1161             (gnus-summary-tick-article article t)))
1162
1163       (if (and (not has-been-begin)
1164                (not sloppy)
1165                (or (memq 'end process-state)
1166                    (memq 'middle process-state)))
1167           (progn
1168             (setq process-state (list 'error))
1169             (message "No begin part at the beginning")
1170             (sleep-for 2))
1171         (setq state 'middle)))
1172
1173     ;; Make sure the last article is put in the article buffer & fix
1174     ;; windows etc.
1175
1176     (if (not (string= article-buffer gnus-article-buffer))
1177         (save-excursion
1178           (set-buffer (get-buffer-create gnus-article-buffer))
1179           (let ((buffer-read-only nil))
1180             (widen)
1181             (erase-buffer)
1182             (insert-buffer-substring article-buffer)
1183             (gnus-set-mode-line 'article)
1184             (goto-char (point-min)))))
1185
1186     (gnus-set-mode-line 'summary)
1187
1188     (if result-files
1189         ()
1190       (if (not has-been-begin)
1191           (if (not no-errors) (message "Wrong type file"))
1192         (if (memq 'error process-state)
1193             (setq result-files nil)
1194           (if (not (or (memq 'ok process-state) 
1195                        (memq 'end process-state)))
1196               (progn
1197                 (if (not no-errors)
1198                     (message "End of articles reached before end of file"))
1199                 (setq result-files nil))
1200             (gnus-uu-unmark-list-of-grabbed)))))
1201     result-files))
1202
1203 (defun gnus-uu-part-number (article)
1204   (let ((subject (mail-header-subject (gnus-summary-article-header article))))
1205     (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1206                       subject)
1207         (substring subject (match-beginning 0) (match-end 0))
1208       "")))
1209
1210 (defun gnus-uu-uudecode-sentinel (process event)
1211   (delete-process (get-process process)))
1212
1213 (defun gnus-uu-uustrip-article (process-buffer in-state)
1214   ;; Uudecodes a file asynchronously.
1215   (let ((state (list 'ok))
1216         (process-connection-type nil)
1217         start-char pst name-beg name-end)
1218     (save-excursion
1219       (set-buffer process-buffer)
1220       (let ((case-fold-search nil)
1221             (buffer-read-only nil))
1222
1223         (goto-char (point-min))
1224
1225         (if gnus-uu-kill-carriage-return
1226             (progn
1227               (while (search-forward "\r" nil t)
1228                 (delete-backward-char 1))
1229               (goto-char (point-min))))
1230
1231         (if (not (re-search-forward gnus-uu-begin-string nil t))
1232             (if (not (re-search-forward gnus-uu-body-line nil t))
1233                 (setq state (list 'wrong-type))))
1234      
1235         (if (memq 'wrong-type state)
1236             ()
1237           (beginning-of-line)
1238           (setq start-char (point))
1239
1240           (if (looking-at gnus-uu-begin-string)
1241               (progn 
1242                 (setq name-end (match-end 1)
1243                       name-beg (match-beginning 1))
1244                 ;; Remove any non gnus-uu-body-line right after start.
1245                 (forward-line 1)
1246                 (or (looking-at gnus-uu-body-line)
1247                     (gnus-delete-line))
1248  
1249                                         ; Replace any slashes and spaces in file names before decoding
1250                 (goto-char name-beg)
1251                 (while (re-search-forward "/" name-end t)
1252                   (replace-match ","))
1253                 (goto-char name-beg)
1254                 (while (re-search-forward " " name-end t)
1255                   (replace-match "_"))
1256                 (goto-char name-beg)
1257                 (if (re-search-forward "_*$" name-end t)
1258                     (replace-match ""))
1259
1260                 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1261                 (and gnus-uu-uudecode-process
1262                      (setq pst (process-status 
1263                                 (or gnus-uu-uudecode-process "nevair")))
1264                      (if (or (eq pst 'stop) (eq pst 'run))
1265                          (progn
1266                            (delete-process gnus-uu-uudecode-process)
1267                            (gnus-uu-unmark-list-of-grabbed t))))
1268                 (if (get-process "*uudecode*")
1269                     (delete-process "*uudecode*"))
1270                 (setq gnus-uu-uudecode-process
1271                       (start-process 
1272                        "*uudecode*" 
1273                        (get-buffer-create gnus-uu-output-buffer-name)
1274                        "sh" "-c" 
1275                        (format "cd %s ; uudecode" gnus-uu-work-dir)))
1276                 (set-process-sentinel 
1277                  gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1278                 (setq state (list 'begin))
1279                 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1280             (setq state (list 'middle)))
1281         
1282           (goto-char (point-max))
1283
1284           (re-search-backward 
1285            (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1286           (beginning-of-line)
1287
1288           (if (looking-at gnus-uu-end-string)
1289               (setq state (cons 'end state)))
1290           (forward-line 1)
1291
1292           (and gnus-uu-uudecode-process
1293                (setq pst (process-status 
1294                           (or gnus-uu-uudecode-process "nevair")))
1295                (if (or (eq pst 'run) (eq pst 'stop))
1296                    (progn
1297                      (if gnus-uu-correct-stripped-uucode
1298                          (progn
1299                            (gnus-uu-check-correct-stripped-uucode 
1300                             start-char (point))
1301                            (goto-char (point-max))
1302                            (re-search-backward 
1303                             (concat gnus-uu-body-line "\\|" 
1304                                     gnus-uu-end-string) 
1305                             nil t)
1306                            (forward-line 1)))
1307
1308                      (condition-case nil
1309                          (process-send-region gnus-uu-uudecode-process 
1310                                               start-char (point))
1311                        (error 
1312                         (progn 
1313                           (delete-process gnus-uu-uudecode-process)
1314                           (message "gnus-uu: Couldn't uudecode")
1315                                         ;                         (sleep-for 2)
1316                           (setq state (list 'wrong-type)))))
1317
1318                      (if (memq 'end state)
1319                          (accept-process-output gnus-uu-uudecode-process)))
1320                  (setq state (list 'wrong-type))))
1321           (if (not gnus-uu-uudecode-process)
1322               (setq state (list 'wrong-type)))))
1323
1324       (if (memq 'begin state)
1325           (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1326         state))))
1327
1328 ;; This function is used by `gnus-uu-grab-articles' to treat
1329 ;; a shared article.
1330 (defun gnus-uu-unshar-article (process-buffer in-state)
1331   (let ((state (list 'ok))
1332         start-char)
1333     (save-excursion
1334       (set-buffer process-buffer)
1335       (goto-char (point-min))
1336       (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1337           (setq state (list 'wrong-type))
1338         (beginning-of-line)
1339         (setq start-char (point))
1340         (call-process-region 
1341          start-char (point-max) "sh" nil 
1342          (get-buffer-create gnus-uu-output-buffer-name) nil 
1343          "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1344     state))
1345
1346 ;; Returns the name of what the shar file is going to unpack.
1347 (defun gnus-uu-find-name-in-shar ()
1348   (let ((oldpoint (point))
1349         res)
1350     (goto-char (point-min))
1351     (if (re-search-forward gnus-uu-shar-name-marker nil t)
1352         (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1353     (goto-char oldpoint)
1354     res))
1355
1356 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1357 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1358 ;; found, or the name of the command to run if such a rule is found.
1359 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1360   (let ((action-list (copy-sequence file-action-list))
1361         (case-fold-search t)
1362         rule action)
1363     (and 
1364      (or no-ignore 
1365          (and (not 
1366                (and gnus-uu-ignore-files-by-name
1367                     (string-match gnus-uu-ignore-files-by-name file-name)))
1368               (not 
1369                (and gnus-uu-ignore-files-by-type
1370                     (string-match gnus-uu-ignore-files-by-type 
1371                                   (or (gnus-uu-choose-action 
1372                                        file-name gnus-uu-ext-to-mime-list t) 
1373                                       ""))))))
1374      (while (not (or (eq action-list ()) action))
1375        (setq rule (car action-list))
1376        (setq action-list (cdr action-list))
1377        (if (string-match (car rule) file-name)
1378            (setq action (car (cdr rule))))))
1379     action))
1380
1381 (defun gnus-uu-treat-archive (file-path)
1382   ;; Unpacks an archive. Returns t if unpacking is successful.
1383   (let ((did-unpack t)
1384         action command dir)
1385     (setq action (gnus-uu-choose-action 
1386                   file-path (append gnus-uu-user-archive-rules
1387                                     (if gnus-uu-ignore-default-archive-rules
1388                                         nil
1389                                       gnus-uu-default-archive-rules))))
1390
1391     (if (not action) (error "No unpackers for the file %s" file-path))
1392
1393     (string-match "/[^/]*$" file-path)
1394     (setq dir (substring file-path 0 (match-beginning 0)))
1395
1396     (if (member action gnus-uu-destructive-archivers)
1397         (copy-file file-path (concat file-path "~") t))
1398
1399     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1400
1401     (save-excursion
1402       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1403       (erase-buffer))
1404
1405     (message "Unpacking: %s..." (gnus-uu-command action file-path))
1406
1407     (if (= 0 (call-process "sh" nil 
1408                            (get-buffer-create gnus-uu-output-buffer-name)
1409                            nil "-c" command))
1410         (message "")
1411       (message "Error during unpacking of archive")
1412       (setq did-unpack nil))
1413
1414     (if (member action gnus-uu-destructive-archivers)
1415         (rename-file (concat file-path "~") file-path t))
1416
1417     did-unpack))
1418
1419 (defun gnus-uu-dir-files (dir)
1420   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1421         files file)
1422     (while dirs
1423       (if (file-directory-p (setq file (car dirs)))
1424           (setq files (append files (gnus-uu-dir-files file)))
1425         (setq files (cons file files)))
1426       (setq dirs (cdr dirs)))
1427     files))
1428
1429 (defun gnus-uu-unpack-files (files &optional ignore)
1430   ;; Go through FILES and look for files to unpack. 
1431   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1432          (ofiles files)
1433          file did-unpack file-entry)
1434     (gnus-uu-add-file totfiles) 
1435     (while files
1436       (setq file (cdr (setq file-entry (assq 'name (car files)))))
1437       (if (and (not (member file ignore))
1438                (equal (gnus-uu-get-action (file-name-nondirectory file))
1439                       "gnus-uu-archive"))
1440           (progn
1441             (setq did-unpack (cons file did-unpack))
1442             (or (gnus-uu-treat-archive file)
1443                 (message "Error during unpacking of %s" file))
1444             (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1445                    (nfiles newfiles))
1446               (gnus-uu-add-file newfiles)
1447               (while nfiles
1448                 (or (member (car nfiles) totfiles)
1449                     (setq ofiles (cons (list (cons 'name (car nfiles))
1450                                              (cons 'original file))
1451                                        ofiles)))
1452                 (setq nfiles (cdr nfiles)))
1453               (setq totfiles newfiles))))
1454       (setq files (cdr files)))
1455     (if did-unpack 
1456         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1457       ofiles)))
1458
1459 (defun gnus-uu-ls-r (dir)
1460   (let* ((files (gnus-uu-directory-files dir t))
1461          (ofiles files))
1462     (while files
1463       (if (file-directory-p (car files))
1464           (progn
1465             (setq ofiles (delete (car files) ofiles))
1466             (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1467       (setq files (cdr files)))
1468     ofiles))
1469
1470 ;; Various stuff
1471
1472 (defun gnus-uu-directory-files (dir &optional full)
1473   (let (files out file)
1474     (setq files (directory-files dir full))
1475     (while files
1476       (setq file (car files))
1477       (setq files (cdr files))
1478       (or (string-match "/\\.\\.?$" file)
1479           (setq out (cons file out))))
1480     (setq out (nreverse out))
1481     out))
1482
1483 (defun gnus-uu-check-correct-stripped-uucode (start end)
1484   (let (found beg length)
1485     (if (not gnus-uu-correct-stripped-uucode)
1486         ()
1487       (goto-char start)
1488
1489       (if (re-search-forward " \\|`" end t)
1490           (progn
1491             (goto-char start)
1492             (while (not (eobp))
1493               (progn
1494                 (if (looking-at "\n") (replace-match ""))
1495                 (forward-line 1))))
1496             
1497         (while (not (eobp))
1498           (if (looking-at (concat gnus-uu-begin-string "\\|" 
1499                                   gnus-uu-end-string))
1500               ()
1501             (if (not found)
1502                 (progn
1503                   (beginning-of-line)
1504                   (setq beg (point))
1505                   (end-of-line)
1506                   (setq length (- (point) beg))))
1507             (setq found t)
1508             (beginning-of-line)
1509             (setq beg (point))
1510             (end-of-line)
1511             (if (not (= length (- (point) beg)))
1512                 (insert (make-string (- length (- (point) beg)) ? ))))
1513           (forward-line 1))))))
1514
1515 (defvar gnus-uu-tmp-alist nil)
1516
1517 (defun gnus-uu-initialize (&optional scan)
1518   (let (entry)
1519     (if (and (not scan)
1520              (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1521                  (if (file-exists-p (cdr entry))
1522                      (setq gnus-uu-work-dir (cdr entry))
1523                    (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1524                    nil)))
1525         t
1526       (setq gnus-uu-tmp-dir (file-name-as-directory 
1527                              (expand-file-name gnus-uu-tmp-dir)))
1528       (if (not (file-directory-p gnus-uu-tmp-dir))
1529           (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1530         (if (not (file-writable-p gnus-uu-tmp-dir))
1531             (error "Temp directory %s can't be written to" 
1532                    gnus-uu-tmp-dir)))
1533
1534       (setq gnus-uu-work-dir 
1535             (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1536       (gnus-uu-add-file gnus-uu-work-dir)
1537       (if (not (file-directory-p gnus-uu-work-dir)) 
1538           (gnus-make-directory gnus-uu-work-dir))
1539       (set-file-modes gnus-uu-work-dir 448)
1540       (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1541       (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1542                                     gnus-uu-tmp-alist)))))
1543
1544
1545 ;; Kills the temporary uu buffers, kills any processes, etc.
1546 (defun gnus-uu-clean-up ()
1547   (let (buf pst)
1548     (and gnus-uu-uudecode-process
1549          (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1550          (if (or (eq pst 'stop) (eq pst 'run))
1551              (delete-process gnus-uu-uudecode-process)))
1552     (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1553          (kill-buffer buf))))
1554
1555 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1556 ;; hasn't been deleted, if, for instance, the user terminated decoding
1557 ;; with `C-g'.
1558 (defun gnus-uu-check-for-generated-files ()
1559   (let (file dirs)
1560     (while gnus-uu-generated-file-list
1561       (setq file (car gnus-uu-generated-file-list))
1562       (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1563       (if (not (string-match "/\\.[\\.]?$" file))
1564           (progn
1565             (if (file-directory-p file)
1566                 (setq dirs (cons file dirs))
1567               (if (file-exists-p file)
1568                   (delete-file file))))))
1569     (setq dirs (nreverse dirs))
1570     (while dirs
1571       (setq file (car dirs))
1572       (setq dirs (cdr dirs))
1573       (if (file-directory-p file)
1574           (if (string-match "/$" file)
1575               (delete-directory (substring file 0 (match-beginning 0)))
1576             (delete-directory file))))))
1577
1578 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1579 ;; still exists upon exiting the newsgroup).
1580 (defun gnus-uu-add-file (file)
1581   (if (stringp file)
1582       (setq gnus-uu-generated-file-list 
1583             (cons file gnus-uu-generated-file-list))
1584     (setq gnus-uu-generated-file-list 
1585           (append file gnus-uu-generated-file-list))))
1586
1587 ;; Inputs an action and a file and returns a full command, putting
1588 ;; quotes round the file name and escaping any quotes in the file name.
1589 (defun gnus-uu-command (action file)
1590   (let ((ofile ""))
1591     (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1592       (progn
1593         (setq ofile
1594               (concat ofile (substring file 0 (match-beginning 0)) "\\"
1595                       (substring file (match-beginning 0) (match-end 0))))
1596         (setq file (substring file (1+ (match-beginning 0))))))
1597     (setq ofile (concat "\"" ofile file "\""))
1598     (if (string-match "%s" action)
1599         (format action ofile)
1600       (concat action " " ofile))))
1601
1602
1603 ;; Initializing
1604
1605 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1606 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1607
1608 \f
1609
1610 ;;;
1611 ;;; uuencoded posting
1612 ;;;
1613
1614 (require 'sendmail)
1615 (require 'rnews)
1616
1617 ;; Any function that is to be used as and encoding method will take two
1618 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1619 ;; and "spiral.jpg", respectively.) The function should return nil if
1620 ;; the encoding wasn't successful.
1621 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1622   "Function used for encoding binary files.
1623 There are three functions supplied with gnus-uu for encoding files:
1624 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1625 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
1626 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
1627 uuencode and adds MIME headers.")
1628
1629 (defvar gnus-uu-post-include-before-composing nil
1630   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1631 If this variable is t, you can either include an encoded file with
1632 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1633
1634 (defvar gnus-uu-post-length 990
1635   "Maximum length of an article.
1636 The encoded file will be split into how many articles it takes to
1637 post the entire file.")
1638
1639 (defvar gnus-uu-post-threaded nil
1640   "Non-nil means that gnus-uu will post the encoded file in a thread.
1641 This may not be smart, as no other decoder I have seen are able to
1642 follow threads when collecting uuencoded articles. (Well, I have seen
1643 one package that does that - gnus-uu, but somehow, I don't think that 
1644 counts...) Default is nil.")
1645
1646 (defvar gnus-uu-post-separate-description t
1647   "Non-nil means that the description will be posted in a separate article.
1648 The first article will typically be numbered (0/x). If this variable
1649 is nil, the description the user enters will be included at the 
1650 beginning of the first article, which will be numbered (1/x). Default 
1651 is t.")
1652
1653 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1654 (defvar gnus-uu-post-message-id nil)
1655 (defvar gnus-uu-post-inserted-file-name nil)
1656 (defvar gnus-uu-winconf-post-news nil)
1657
1658 (defun gnus-uu-post-news ()
1659   "Compose an article and post an encoded file."
1660   (interactive)
1661   (setq gnus-uu-post-inserted-file-name nil)
1662   (setq gnus-uu-winconf-post-news (current-window-configuration))
1663
1664   (gnus-summary-post-news)
1665
1666   (use-local-map (copy-keymap (current-local-map)))
1667   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1668   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1669   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1670   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1671       
1672   (if gnus-uu-post-include-before-composing
1673       (save-excursion (setq gnus-uu-post-inserted-file-name 
1674                             (gnus-uu-post-insert-binary)))))
1675
1676 (defun gnus-uu-post-insert-binary-in-article ()
1677   "Inserts an encoded file in the buffer.
1678 The user will be asked for a file name."
1679   (interactive)
1680   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1681       (error "Not in post-news buffer"))
1682   (save-excursion 
1683     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1684
1685 ;; Encodes with uuencode and substitutes all spaces with backticks.
1686 (defun gnus-uu-post-encode-uuencode (path file-name)
1687   (if (gnus-uu-post-encode-file "uuencode" path file-name)
1688       (progn
1689         (goto-char (point-min))
1690         (forward-line 1)
1691         (while (re-search-forward " " nil t)
1692           (replace-match "`"))
1693         t)))
1694
1695 ;; Encodes with uuencode and adds MIME headers.
1696 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1697   (if (gnus-uu-post-encode-uuencode path file-name)
1698       (progn
1699         (gnus-uu-post-make-mime file-name "x-uue")
1700         t)))
1701
1702 ;; Encodes with base64 and adds MIME headers
1703 (defun gnus-uu-post-encode-mime (path file-name)
1704   (if (gnus-uu-post-encode-file "mmencode" path file-name)
1705       (progn
1706         (gnus-uu-post-make-mime file-name "base64")
1707         t)))
1708
1709 ;; Adds MIME headers.
1710 (defun gnus-uu-post-make-mime (file-name encoding)
1711   (goto-char (point-min))
1712   (insert (format "Content-Type: %s; name=\"%s\"\n" 
1713                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list) 
1714                   file-name))
1715   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1716   (save-restriction
1717     (set-buffer gnus-post-news-buffer)
1718     (goto-char (point-min))
1719     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1720     (forward-line -1)
1721     (narrow-to-region 1 (point))
1722     (or (mail-fetch-field "mime-version")
1723         (progn
1724           (widen)
1725           (insert "MIME-Version: 1.0\n")))
1726     (widen)))
1727
1728 ;; Encodes a file PATH with COMMAND, leaving the result in the
1729 ;; current buffer.
1730 (defun gnus-uu-post-encode-file (command path file-name)
1731   (= 0 (call-process "sh" nil t nil "-c" 
1732                      (format "%s %s %s" command path file-name))))
1733
1734 (defun gnus-uu-post-news-inews ()
1735   "Posts the composed news article and encoded file.
1736 If no file has been included, the user will be asked for a file."
1737   (interactive)
1738   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1739       (error "Not in post news buffer"))
1740
1741   (let (file-name)
1742
1743     (if gnus-uu-post-inserted-file-name
1744         (setq file-name gnus-uu-post-inserted-file-name)
1745       (setq file-name (gnus-uu-post-insert-binary)))
1746   
1747     (if gnus-uu-post-threaded
1748         (let ((gnus-required-headers 
1749                (if (memq 'Message-ID gnus-required-headers)
1750                    gnus-required-headers
1751                  (cons 'Message-ID gnus-required-headers)))
1752               gnus-inews-article-hook)
1753
1754           (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1755                                             gnus-inews-article-hook
1756                                           (list gnus-inews-article-hook)))
1757           (setq gnus-inews-article-hook 
1758                 (cons
1759                  '(lambda ()
1760                     (save-excursion
1761                       (goto-char (point-min))
1762                       (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1763                           (setq gnus-uu-post-message-id 
1764                                 (buffer-substring 
1765                                  (match-beginning 1) (match-end 1)))
1766                         (setq gnus-uu-post-message-id nil))))
1767                  gnus-inews-article-hook))
1768           (gnus-uu-post-encoded file-name t))
1769       (gnus-uu-post-encoded file-name nil)))
1770   (setq gnus-uu-post-inserted-file-name nil)
1771   (and gnus-uu-winconf-post-news
1772        (set-window-configuration gnus-uu-winconf-post-news)))
1773       
1774 ;; Asks for a file to encode, encodes it and inserts the result in
1775 ;; the current buffer. Returns the file name the user gave.
1776 (defun gnus-uu-post-insert-binary ()
1777   (let ((uuencode-buffer-name "*uuencode buffer*")
1778         file-path uubuf file-name)
1779
1780     (setq file-path (read-file-name 
1781                      "What file do you want to encode? "))
1782     (if (not (file-exists-p file-path))
1783         (error "%s: No such file" file-path))
1784
1785     (goto-char (point-max))
1786     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1787     
1788     (if (string-match "^~/" file-path)
1789         (setq file-path (concat "$HOME" (substring file-path 1))))
1790     (if (string-match "/[^/]*$" file-path)
1791         (setq file-name (substring file-path (1+ (match-beginning 0))))
1792       (setq file-name file-path))
1793
1794     (unwind-protect
1795         (if (save-excursion
1796               (set-buffer (setq uubuf 
1797                                 (get-buffer-create uuencode-buffer-name)))
1798               (erase-buffer)
1799               (funcall gnus-uu-post-encode-method file-path file-name))
1800             (insert-buffer uubuf)
1801           (error "Encoding unsuccessful"))
1802       (kill-buffer uubuf))
1803     file-name))
1804
1805 ;; Posts the article and all of the encoded file.
1806 (defun gnus-uu-post-encoded (file-name &optional threaded)
1807   (let ((send-buffer-name "*uuencode send buffer*")
1808         (encoded-buffer-name "*encoded buffer*")
1809         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1810         (separator (concat mail-header-separator "\n\n"))
1811         uubuf length parts header i end beg
1812         beg-line minlen buf post-buf whole-len beg-binary end-binary)
1813
1814     (setq post-buf (current-buffer))
1815
1816     (goto-char (point-min))
1817     (if (not (re-search-forward 
1818               (if gnus-uu-post-separate-description 
1819                   (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1820                           "$")
1821                 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1822         (error "Internal error: No binary/header separator"))
1823     (beginning-of-line)
1824     (forward-line 1)
1825     (setq beg-binary (point))
1826     (setq end-binary (point-max))
1827
1828     (save-excursion 
1829       (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1830       (erase-buffer)
1831       (insert-buffer-substring post-buf beg-binary end-binary)
1832       (goto-char (point-min))
1833       (setq length (count-lines 1 (point-max)))
1834       (setq parts (/ length gnus-uu-post-length))
1835       (if (not (< (% length gnus-uu-post-length) 4))
1836           (setq parts (1+ parts))))
1837
1838     (if gnus-uu-post-separate-description
1839         (forward-line -1))
1840     (kill-region (point) (point-max))
1841
1842     (goto-char (point-min))
1843     (re-search-forward 
1844      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1845     (beginning-of-line)
1846     (setq header (buffer-substring 1 (point)))
1847
1848     (goto-char (point-min))
1849     (if (not gnus-uu-post-separate-description)
1850         ()
1851       (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1852           (progn
1853             (end-of-line)
1854             (insert (format " (0/%d)" parts))))
1855       (gnus-inews-news))
1856
1857     (save-excursion
1858       (setq i 1)
1859       (setq beg 1)
1860       (while (not (> i parts))
1861         (set-buffer (get-buffer-create send-buffer-name))
1862         (erase-buffer)
1863         (insert header)
1864         (if (and threaded gnus-uu-post-message-id)
1865             (insert (format "References: %s\n" gnus-uu-post-message-id)))
1866         (insert separator)
1867         (setq whole-len
1868               (- 62 (length (format top-string "" file-name i parts ""))))
1869         (if (> 1 (setq minlen (/ whole-len 2)))
1870             (setq minlen 1))
1871         (setq 
1872          beg-line 
1873          (format top-string
1874                  (make-string minlen ?-) 
1875                  file-name i parts
1876                  (make-string 
1877                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1878
1879         (goto-char (point-min))
1880         (if (not (re-search-forward "^Subject: " nil t))
1881             ()
1882           (if (not threaded)
1883               (progn
1884                 (end-of-line)
1885                 (insert (format " (%d/%d)" i parts)))
1886             (if (or (and (= i 2) gnus-uu-post-separate-description)
1887                     (and (= i 1) (not gnus-uu-post-separate-description)))
1888                 (replace-match "Subject: Re: "))))
1889                   
1890         (goto-char (point-max))
1891         (save-excursion
1892           (set-buffer uubuf)
1893           (goto-char beg)
1894           (if (= i parts)
1895               (goto-char (point-max))
1896             (forward-line gnus-uu-post-length))
1897           (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1898               (forward-line -4))
1899           (setq end (point)))
1900         (insert-buffer-substring uubuf beg end)
1901         (insert beg-line)
1902         (insert "\n")
1903         (setq beg end)
1904         (setq i (1+ i))
1905         (goto-char (point-min))
1906         (re-search-forward
1907          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1908         (beginning-of-line)
1909         (forward-line 2)
1910         (if (re-search-forward 
1911              (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1912              nil t)
1913             (progn 
1914               (replace-match "")
1915               (forward-line 1)))
1916         (insert beg-line)
1917         (insert "\n")
1918         (gnus-inews-news)))
1919
1920     (and (setq buf (get-buffer send-buffer-name))
1921          (kill-buffer buf))
1922     (and (setq buf (get-buffer encoded-buffer-name))
1923          (kill-buffer buf))
1924
1925     (if (not gnus-uu-post-separate-description)
1926         (progn
1927           (set-buffer-modified-p nil)
1928           (and (fboundp 'bury-buffer) (bury-buffer))))))
1929
1930 (provide 'gnus-uu)
1931
1932 ;; gnus-uu.el ends here