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