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