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