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