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