*** 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             (or (string= from (caar fs))
454                 (setq from nil)))
455           (when subject
456             (or (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     (or marked (error "No articles marked with the process mark"))
598     (setq gnus-newsgroup-processable nil)
599     (save-excursion
600       (while marked
601         (and (vectorp (setq headers 
602                             (gnus-summary-article-header (car marked))))
603              (setq subject (mail-header-subject headers)
604                    articles (gnus-uu-find-articles-matching 
605                              (gnus-uu-reginize-string subject))
606                    total (nconc total articles)))
607         (while articles
608           (gnus-summary-set-process-mark (car articles))
609           (setcdr marked (delq (car articles) (cdr marked)))
610           (setq articles (cdr articles)))
611         (setq marked (cdr marked)))
612       (setq gnus-newsgroup-processable (nreverse total)))
613     (gnus-summary-position-point)))
614
615 (defun gnus-uu-mark-all ()
616   "Mark all articles in \"series\" order."
617   (interactive)
618   (gnus-set-global-variables)
619   (setq gnus-newsgroup-processable nil)
620   (save-excursion
621     (let ((data gnus-newsgroup-data)
622           number)
623       (while data
624         (when (and (not (memq (setq number (gnus-data-number (car data)))
625                               gnus-newsgroup-processable))
626                    (vectorp (gnus-data-header (car data))))
627           (gnus-summary-goto-subject number)
628           (gnus-uu-mark-series))
629         (setq data (cdr data)))))
630   (gnus-summary-position-point))
631
632 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>. 
633
634 (defun gnus-uu-decode-postscript (&optional n)
635   "Gets postscript of the current article."
636   (interactive "P")
637   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
638
639 (defun gnus-uu-decode-postscript-view (&optional n)
640   "Gets and views the current article."
641   (interactive "P")
642   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
643     (gnus-uu-decode-postscript n)))
644
645 (defun gnus-uu-decode-postscript-and-save (n dir)
646   "Extracts postscript and saves the current article."
647   (interactive
648    (list current-prefix-arg
649          (file-name-as-directory
650           (read-file-name "Save in dir: "
651                           gnus-uu-default-dir
652                           gnus-uu-default-dir t))))
653   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article 
654                               n dir nil nil t))
655
656 (defun gnus-uu-decode-postscript-and-save-view (n dir)
657   "Decodes, views and saves the resulting file."
658   (interactive
659    (list current-prefix-arg
660          (read-file-name "Where do you want to save the file(s)? "
661                          gnus-uu-default-dir
662                          gnus-uu-default-dir t)))
663   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
664     (gnus-uu-decode-postscript-and-save n dir)))
665
666
667 ;; Internal functions.
668
669 (defun gnus-uu-decode-with-method (method n &optional save not-insert 
670                                           scan cdir)
671   (gnus-uu-initialize scan)
672   (if save (setq gnus-uu-default-dir save))
673   ;; Create the directory we save to.
674   (when (and scan cdir save
675              (not (file-exists-p save)))
676     (make-directory save t))
677   (let ((articles (gnus-uu-get-list-of-articles n))
678         files)
679     (setq files (gnus-uu-grab-articles articles method t))
680     (let ((gnus-current-article (car articles)))
681       (and scan (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
682     (and save (gnus-uu-save-files files save))
683     (if (eq gnus-uu-do-not-unpack-archives nil)
684         (setq files (gnus-uu-unpack-files files)))
685     (setq files (nreverse (gnus-uu-get-actions files)))
686     (or not-insert (not gnus-insert-pseudo-articles)
687         (gnus-summary-insert-pseudos files save))))
688
689 (defun gnus-uu-scan-directory (dir &optional rec)
690   "Return a list of all files under DIR."
691   (let ((files (directory-files dir t))
692         out file)
693     (while (setq file (pop files))
694       (unless (member (file-name-nondirectory file) '("." ".."))
695         (push (list (cons 'name file)
696                     (cons 'article gnus-current-article))
697               out)
698         (when (file-directory-p file)
699           (setq out (nconc (gnus-uu-scan-directory file t) out)))))
700     (if rec 
701         out
702       (nreverse out))))
703
704 (defun gnus-uu-save-files (files dir)
705   "Save FILES in DIR."
706   (let ((len (length files))
707         (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
708         to-file file fromdir)
709     (while (setq file (cdr (assq 'name (pop files))))
710       (when (file-exists-p file)
711         (string-match reg file)
712         (setq fromdir (substring file (match-end 0)))
713         (if (file-directory-p file)
714             (gnus-make-directory (concat dir fromdir))
715           (setq to-file (concat dir fromdir))
716           (when (or (not (file-exists-p to-file))
717                     (gnus-y-or-n-p (format "%s exists; overwrite? " to-file)))
718             (copy-file file to-file t t)))))
719     (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
720
721 ;; Functions for saving and possibly digesting articles without
722 ;; any decoding.
723
724 ;; Function called by gnus-uu-grab-articles to treat each article.
725 (defun gnus-uu-save-article (buffer in-state)
726   (cond 
727    (gnus-uu-save-separate-articles
728     (save-excursion
729       (set-buffer buffer)
730       (write-region 1 (point-max) (concat gnus-uu-saved-article-name 
731                                           gnus-current-article))
732       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
733             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name 
734                                                  'begin 'end))
735             ((eq in-state 'last) (list 'end))
736             (t (list 'middle)))))
737    ((not gnus-uu-save-in-digest)
738     (save-excursion
739       (set-buffer buffer)
740       (write-region 1 (point-max) gnus-uu-saved-article-name t)
741       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
742             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name 
743                                                  'begin 'end))
744             ((eq in-state 'last) (list 'end))
745             (t (list 'middle)))))
746    (t
747     (let ((header (gnus-summary-article-header)))
748       (setq gnus-uu-digest-from-subject
749             (cons (cons (mail-header-from header)
750                         (mail-header-subject header))
751                   gnus-uu-digest-from-subject)))
752     (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
753           (delim (concat "^" (make-string 30 ?-) "$"))
754           beg subj headers headline sorthead body end-string state)
755       (if (or (eq in-state 'first) 
756               (eq in-state 'first-and-last))
757           (progn 
758             (setq state (list 'begin))
759             (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
760                             (erase-buffer))
761             (save-excursion 
762               (set-buffer (get-buffer-create "*gnus-uu-pre*"))
763               (erase-buffer)
764               (insert (format 
765                        "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
766                        (current-time-string) name name))))
767         (if (not (eq in-state 'end))
768             (setq state (list 'middle))))
769       (save-excursion
770         (set-buffer (get-buffer "*gnus-uu-body*"))
771         (goto-char (setq beg (point-max)))
772         (save-excursion
773           (save-restriction
774             (set-buffer buffer)
775             (let (buffer-read-only)
776               (gnus-set-text-properties (point-min) (point-max) nil)
777               ;; These two are necessary for XEmacs 19.12 fascism.
778               (put-text-property (point-min) (point-max) 'invisible nil)
779               (put-text-property (point-min) (point-max) 'intangible nil))
780             (goto-char (point-min))
781             (re-search-forward "\n\n")
782             ;; Quote all 30-dash lines.
783             (save-excursion
784               (while (re-search-forward delim nil t)
785                 (beginning-of-line)
786                 (delete-char 1)
787                 (insert " ")))
788             (setq body (buffer-substring (1- (point)) (point-max)))
789             (narrow-to-region (point-min) (point))
790             (if (not (setq headers gnus-uu-digest-headers))
791                 (setq sorthead (buffer-substring (point-min) (point-max)))
792               (while headers
793                 (setq headline (car headers))
794                 (setq headers (cdr headers))
795                 (goto-char (point-min))
796                 (while (re-search-forward headline nil t)
797                   (setq sorthead 
798                         (concat sorthead
799                                 (buffer-substring 
800                                  (match-beginning 0)
801                                  (or (and (re-search-forward "^[^ \t]" nil t)
802                                           (1- (point)))
803                                      (progn (forward-line 1) (point)))))))))
804             (widen)))
805         (insert sorthead) (goto-char (point-max))
806         (insert body) (goto-char (point-max))
807         (insert (concat "\n" (make-string 30 ?-) "\n\n"))
808         (goto-char beg)
809         (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
810             (progn
811               (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
812               (save-excursion 
813                 (set-buffer (get-buffer "*gnus-uu-pre*"))
814                 (insert (format "   %s\n" subj))))))
815       (if (or (eq in-state 'last)
816               (eq in-state 'first-and-last))
817           (progn
818             (save-excursion
819               (set-buffer (get-buffer "*gnus-uu-pre*"))
820               (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
821               (write-region 1 (point-max) gnus-uu-saved-article-name))
822             (save-excursion
823               (set-buffer (get-buffer "*gnus-uu-body*"))
824               (goto-char (point-max))
825               (insert 
826                (concat (setq end-string (format "End of %s Digest" name)) 
827                        "\n"))
828               (insert (concat (make-string (length end-string) ?*) "\n"))
829               (write-region 1 (point-max) gnus-uu-saved-article-name t))
830             (kill-buffer (get-buffer "*gnus-uu-pre*"))
831             (kill-buffer (get-buffer "*gnus-uu-body*"))
832             (setq state (cons 'end state))))
833       (if (memq 'begin state)
834           (cons gnus-uu-saved-article-name state)
835         state)))))
836
837 ;; Binhex treatment - not very advanced. 
838
839 (defconst gnus-uu-binhex-body-line 
840   "^[^:]...............................................................$")
841 (defconst gnus-uu-binhex-begin-line 
842   "^:...............................................................$")
843 (defconst gnus-uu-binhex-end-line
844   ":$")
845
846 (defun gnus-uu-binhex-article (buffer in-state)
847   (let (state start-char)
848     (save-excursion
849       (set-buffer buffer)
850       (widen)
851       (goto-char (point-min))
852       (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
853           (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
854               (setq state (list 'wrong-type))))
855
856       (if (memq 'wrong-type state)
857           ()
858         (beginning-of-line)
859         (setq start-char (point))
860         (if (looking-at gnus-uu-binhex-begin-line)
861             (progn
862               (setq state (list 'begin))
863               (write-region 1 1 gnus-uu-binhex-article-name))
864           (setq state (list 'middle)))
865         (goto-char (point-max))
866         (re-search-backward (concat gnus-uu-binhex-body-line "\\|" 
867                                     gnus-uu-binhex-end-line) nil t)
868         (if (looking-at gnus-uu-binhex-end-line)
869             (setq state (if (memq 'begin state)
870                             (cons 'end state)
871                           (list 'end))))
872         (beginning-of-line)
873         (forward-line 1)
874         (if (file-exists-p gnus-uu-binhex-article-name)
875             (append-to-file start-char (point) gnus-uu-binhex-article-name))))
876     (if (memq 'begin state)
877         (cons gnus-uu-binhex-article-name state)
878       state)))
879
880 ;; PostScript
881
882 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
883   (let ((state (list 'ok))
884         start-char end-char file-name)
885     (save-excursion
886       (set-buffer process-buffer)
887       (goto-char (point-min))
888       (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
889           (setq state (list 'wrong-type))
890         (beginning-of-line)
891         (setq start-char (point))
892         (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
893             (setq state (list 'wrong-type))
894           (setq end-char (point))
895           (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
896           (insert-buffer-substring process-buffer start-char end-char)
897           (setq file-name (concat gnus-uu-work-dir
898                                   (cdr gnus-article-current) ".ps"))
899           (write-region (point-min) (point-max) file-name)
900           (setq state (list file-name 'begin 'end)))))
901     state))
902       
903
904 ;; Find actions.
905
906 (defun gnus-uu-get-actions (files)
907   (let ((ofiles files)
908         action name)
909     (while files
910       (setq name (cdr (assq 'name (car files))))
911       (and 
912        (setq action (gnus-uu-get-action name))
913        (setcar files (nconc (list (if (string= action "gnus-uu-archive")
914                                       (cons 'action "file")
915                                     (cons 'action action))
916                                   (cons 'execute (gnus-uu-command
917                                                   action name)))
918                             (car files))))
919       (setq files (cdr files)))
920     ofiles))
921
922 (defun gnus-uu-get-action (file-name)
923   (let (action)
924     (setq action 
925           (gnus-uu-choose-action 
926            file-name
927            (append 
928             gnus-uu-user-view-rules
929             (if gnus-uu-ignore-default-view-rules 
930                 nil 
931               gnus-uu-default-view-rules)
932             gnus-uu-user-view-rules-end)))
933     (if (and (not (string= (or action "") "gnus-uu-archive")) 
934              gnus-uu-view-with-metamail)
935         (if (setq action 
936                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
937             (setq action (format "metamail -d -b -c \"%s\"" action))))
938     action))
939
940
941 ;; Functions for treating subjects and collecting series.
942
943 (defun gnus-uu-reginize-string (string)
944   ;; Takes a string and puts a \ in front of every special character;
945   ;; ignores any leading "version numbers" thingies that they use in
946   ;; the comp.binaries groups, and either replaces anything that looks
947   ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
948   ;; like that, replaces the last two numbers with "[0-9]+".  This, in
949   ;; my experience, should get most postings of a series.
950   (let ((count 2)
951         (vernum "v[0-9]+[a-z][0-9]+:")
952         beg)
953     (save-excursion
954       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
955       (buffer-disable-undo (current-buffer))
956       (erase-buffer)
957       (insert (regexp-quote string))
958       (setq beg 1)
959
960       (setq case-fold-search nil)
961       (goto-char (point-min))
962       (if (looking-at vernum)
963           (progn
964             (replace-match vernum t t)
965             (setq beg (length vernum))))
966
967       (goto-char beg)
968       (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
969           (replace-match " [0-9]+/[0-9]+")
970
971         (goto-char beg)
972         (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
973             (replace-match "[0-9]+ of [0-9]+")
974
975           (end-of-line)
976           (while (and (re-search-backward "[0-9]" nil t) (> count 0))
977             (while (and 
978                     (looking-at "[0-9]") 
979                     (< 1 (goto-char (1- (point))))))
980             (re-search-forward "[0-9]+" nil t)
981             (replace-match "[0-9]+")
982             (backward-char 5)
983             (setq count (1- count)))))
984
985       (goto-char beg)
986       (while (re-search-forward "[ \t]+" nil t)
987         (replace-match "[ \t]*" t t))
988
989       (buffer-substring 1 (point-max)))))
990
991 (defun gnus-uu-get-list-of-articles (n)
992   ;; If N is non-nil, the article numbers of the N next articles
993   ;; will be returned.
994   ;; If any articles have been marked as processable, they will be
995   ;; returned. 
996   ;; Failing that, articles that have subjects that are part of the
997   ;; same "series" as the current will be returned.
998   (let (articles)
999     (cond 
1000      (n
1001       (let ((backward (< n 0))
1002             (n (abs n)))
1003         (save-excursion
1004           (while (and (> n 0)
1005                       (setq articles (cons (gnus-summary-article-number) 
1006                                            articles))
1007                       (gnus-summary-search-forward nil nil backward))
1008             (setq n (1- n))))
1009         (nreverse articles)))
1010      (gnus-newsgroup-processable
1011       (reverse gnus-newsgroup-processable))
1012      (t
1013       (gnus-uu-find-articles-matching)))))
1014
1015 (defun gnus-uu-string< (l1 l2)
1016   (string< (car l1) (car l2)))
1017
1018 (defun gnus-uu-find-articles-matching 
1019   (&optional subject only-unread do-not-translate)
1020   ;; Finds all articles that matches the regexp SUBJECT.  If it is
1021   ;; nil, the current article name will be used.  If ONLY-UNREAD is
1022   ;; non-nil, only unread articles are chosen.  If DO-NOT-TRANSLATE is
1023   ;; non-nil, article names are not equalized before sorting.
1024   (let ((subject (or subject 
1025                      (gnus-uu-reginize-string (gnus-summary-article-subject))))
1026         list-of-subjects)
1027     (save-excursion
1028       (if (not subject)
1029           ()
1030         ;; Collect all subjects matching subject.
1031         (let ((case-fold-search t)
1032               (data gnus-newsgroup-data)
1033               subj mark d)
1034           (while data
1035             (setq d (pop data))
1036             (and (not (gnus-data-pseudo-p d))
1037                  (or (not only-unread)
1038                      (= (setq mark (gnus-data-mark d))
1039                         gnus-unread-mark)
1040                      (= mark gnus-ticked-mark)
1041                      (= mark gnus-dormant-mark))
1042                  (setq subj (mail-header-subject (gnus-data-header d)))
1043                  (string-match subject subj)
1044                  (setq list-of-subjects 
1045                        (cons (cons subj (gnus-data-number d))
1046                              list-of-subjects)))))
1047
1048         ;; Expand numbers, sort, and return the list of article
1049         ;; numbers.
1050         (mapcar (lambda (sub) (cdr sub)) 
1051                 (sort (gnus-uu-expand-numbers 
1052                        list-of-subjects
1053                        (not do-not-translate)) 
1054                       'gnus-uu-string<))))))
1055
1056 (defun gnus-uu-expand-numbers (string-list &optional translate)
1057   ;; Takes a list of strings and "expands" all numbers in all the
1058   ;; strings.  That is, this function makes all numbers equal length by
1059   ;; prepending lots of zeroes before each number.  This is to ease later
1060   ;; sorting to find out what sequence the articles are supposed to be
1061   ;; decoded in.  Returns the list of expanded strings.
1062   (let ((out-list string-list)
1063         string)
1064     (save-excursion
1065       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1066       (buffer-disable-undo (current-buffer))
1067       (while string-list
1068         (erase-buffer)
1069         (insert (caar string-list))
1070         ;; Translate multiple spaces to one space.
1071         (goto-char (point-min))
1072         (while (re-search-forward "[ \t]+" nil t)
1073           (replace-match " "))
1074         ;; Translate all characters to "a".
1075         (goto-char (point-min))
1076         (if translate 
1077             (while (re-search-forward "[A-Za-z]" nil t)
1078               (replace-match "a" t t)))
1079         ;; Expand numbers.
1080         (goto-char (point-min))
1081         (while (re-search-forward "[0-9]+" nil t)
1082           (replace-match  
1083            (format "%06d" 
1084                    (string-to-int (buffer-substring 
1085                                    (match-beginning 0) (match-end 0))))))
1086         (setq string (buffer-substring 1 (point-max)))
1087         (setcar (car string-list) string)
1088         (setq string-list (cdr string-list))))
1089     out-list))
1090
1091
1092 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1093 ;; function.  It takes a list of articles to be grabbed and a function
1094 ;; to apply to each article.
1095 ;;
1096 ;; The function to be called should take two parameters.  The first
1097 ;; parameter is the article buffer.  The function should leave the
1098 ;; result, if any, in this buffer.  Most treatment functions will just
1099 ;; generate files...
1100 ;;
1101 ;; The second parameter is the state of the list of articles, and can
1102 ;; have four values: `first', `middle', `last' and `first-and-last'.
1103 ;;
1104 ;; The function should return a list.  The list may contain the
1105 ;; following symbols:
1106 ;; `error' if an error occurred
1107 ;; `begin' if the beginning of an encoded file has been received
1108 ;;   If the list returned contains a `begin', the first element of
1109 ;;   the list *must* be a string with the file name of the decoded
1110 ;;   file.
1111 ;; `end' if the end of an encoded file has been received
1112 ;; `middle' if the article was a body part of an encoded file
1113 ;; `wrong-type' if the article was not a part of an encoded file
1114 ;; `ok', which can be used everything is ok
1115
1116 (defvar gnus-uu-has-been-grabbed nil)
1117
1118 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1119   (let (art)
1120     (if (not (and gnus-uu-has-been-grabbed
1121                   gnus-uu-unmark-articles-not-decoded))
1122         ()
1123       (if dont-unmark-last-article
1124           (progn
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       (if 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         (if 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             (setq state (cons '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     (if (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      (or 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        (if (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     (if (not action) (error "No unpackers for the file %s" file-path))
1464
1465     (string-match "/[^/]*$" file-path)
1466     (setq dir (substring file-path 0 (match-beginning 0)))
1467
1468     (if (member action gnus-uu-destructive-archivers)
1469         (copy-file file-path (concat file-path "~") t))
1470
1471     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1472
1473     (save-excursion
1474       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1475       (erase-buffer))
1476
1477     (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1478
1479     (if (= 0 (call-process shell-file-name nil 
1480                            (get-buffer-create gnus-uu-output-buffer-name)
1481                            nil shell-command-switch command))
1482         (message "")
1483       (gnus-message 2 "Error during unpacking of archive")
1484       (setq did-unpack nil))
1485
1486     (if (member action gnus-uu-destructive-archivers)
1487         (rename-file (concat file-path "~") file-path t))
1488
1489     did-unpack))
1490
1491 (defun gnus-uu-dir-files (dir)
1492   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1493         files file)
1494     (while dirs
1495       (if (file-directory-p (setq file (car dirs)))
1496           (setq files (append files (gnus-uu-dir-files file)))
1497         (setq files (cons file files)))
1498       (setq dirs (cdr dirs)))
1499     files))
1500
1501 (defun gnus-uu-unpack-files (files &optional ignore)
1502   ;; Go through FILES and look for files to unpack. 
1503   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1504          (ofiles files)
1505          file did-unpack)
1506     (while files
1507       (setq file (cdr (assq 'name (car files))))
1508       (if (and (not (member file ignore))
1509                (equal (gnus-uu-get-action (file-name-nondirectory file))
1510                       "gnus-uu-archive"))
1511           (progn
1512             (setq did-unpack (cons file did-unpack))
1513             (or (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                 (or (member (car nfiles) totfiles)
1519                     (setq ofiles (cons (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       (if (file-directory-p (car files))
1534           (progn
1535             (setq ofiles (delete (car files) ofiles))
1536             (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1537       (setq files (cdr files)))
1538     ofiles))
1539
1540 ;; Various stuff
1541
1542 (defun gnus-uu-directory-files (dir &optional full)
1543   (let (files out file)
1544     (setq files (directory-files dir full))
1545     (while files
1546       (setq file (car files))
1547       (setq files (cdr files))
1548       (or (member (file-name-nondirectory file) '("." ".."))
1549           (setq out (cons file out))))
1550     (setq out (nreverse out))
1551     out))
1552
1553 (defun gnus-uu-check-correct-stripped-uucode (start end)
1554   (save-excursion
1555     (let (found beg length)
1556       (if (not gnus-uu-correct-stripped-uucode)
1557           ()
1558         (goto-char start)
1559
1560         (if (re-search-forward " \\|`" end t)
1561             (progn
1562               (goto-char start)
1563               (while (not (eobp))
1564                 (progn
1565                   (if (looking-at "\n") (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               (if (not found)
1573                   (progn
1574                     (beginning-of-line)
1575                     (setq beg (point))
1576                     (end-of-line)
1577                     (setq length (- (point) beg))))
1578               (setq found t)
1579               (beginning-of-line)
1580               (setq beg (point))
1581               (end-of-line)
1582               (if (not (= length (- (point) beg)))
1583                   (insert (make-string (- length (- (point) beg)) ? ))))
1584             (forward-line 1)))))))
1585
1586 (defvar gnus-uu-tmp-alist nil)
1587
1588 (defun gnus-uu-initialize (&optional scan)
1589   (let (entry)
1590     (if (and (not scan)
1591              (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1592                  (if (file-exists-p (cdr entry))
1593                      (setq gnus-uu-work-dir (cdr entry))
1594                    (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1595                    nil)))
1596         t
1597       (setq gnus-uu-tmp-dir (file-name-as-directory 
1598                              (expand-file-name gnus-uu-tmp-dir)))
1599       (if (not (file-directory-p gnus-uu-tmp-dir))
1600           (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1601         (if (not (file-writable-p gnus-uu-tmp-dir))
1602             (error "Temp directory %s can't be written to" 
1603                    gnus-uu-tmp-dir)))
1604
1605       (setq gnus-uu-work-dir 
1606             (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1607       (gnus-make-directory gnus-uu-work-dir)
1608       (set-file-modes gnus-uu-work-dir 448)
1609       (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1610       (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1611                                     gnus-uu-tmp-alist)))))
1612
1613
1614 ;; Kills the temporary uu buffers, kills any processes, etc.
1615 (defun gnus-uu-clean-up ()
1616   (let (buf)
1617     (and gnus-uu-uudecode-process
1618          (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1619                '(stop run))
1620          (delete-process gnus-uu-uudecode-process))
1621     (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1622          (kill-buffer buf))))
1623
1624 ;; Inputs an action and a file and returns a full command, putting
1625 ;; quotes round the file name and escaping any quotes in the file name.
1626 (defun gnus-uu-command (action file)
1627   (let ((ofile ""))
1628     (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1629       (progn
1630         (setq ofile
1631               (concat ofile (substring file 0 (match-beginning 0)) "\\"
1632                       (substring file (match-beginning 0) (match-end 0))))
1633         (setq file (substring file (1+ (match-beginning 0))))))
1634     (setq ofile (concat "\"" ofile file "\""))
1635     (if (string-match "%s" action)
1636         (format action ofile)
1637       (concat action " " ofile))))
1638
1639 (defun gnus-uu-delete-work-dir (&optional dir)
1640   "Delete recursively all files and directories under `gnus-uu-work-dir'."
1641   (if dir
1642       (gnus-message 7 "Deleting directory %s..." dir)
1643     (setq dir gnus-uu-work-dir))
1644   (when (and dir
1645              (file-exists-p dir))
1646     (let ((files (directory-files dir t nil t))
1647           file)
1648       (while (setq file (pop files))
1649         (unless (member (file-name-nondirectory file) '("." ".."))
1650           (if (file-directory-p file)
1651               (gnus-uu-delete-work-dir file)
1652             (gnus-message 9 "Deleting file %s..." file)
1653             (delete-file file))))
1654       (delete-directory dir)))
1655   (gnus-message 7 ""))
1656
1657 ;; Initializing
1658
1659 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1660 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1661
1662 \f
1663
1664 ;;;
1665 ;;; uuencoded posting
1666 ;;;
1667
1668 ;; Any function that is to be used as and encoding method will take two
1669 ;; parameters: PATH-NAME and FILE-NAME.  (E.g. "/home/gaga/spiral.jpg"
1670 ;; and "spiral.jpg", respectively.) The function should return nil if
1671 ;; the encoding wasn't successful.
1672 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1673   "Function used for encoding binary files.
1674 There are three functions supplied with gnus-uu for encoding files:
1675 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1676 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
1677 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
1678 uuencode and adds MIME headers.")
1679
1680 (defvar gnus-uu-post-include-before-composing nil
1681   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1682 If this variable is t, you can either include an encoded file with
1683 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1684
1685 (defvar gnus-uu-post-length 990
1686   "Maximum length of an article.
1687 The encoded file will be split into how many articles it takes to
1688 post the entire file.")
1689
1690 (defvar gnus-uu-post-threaded nil
1691   "Non-nil means that gnus-uu will post the encoded file in a thread.
1692 This may not be smart, as no other decoder I have seen are able to
1693 follow threads when collecting uuencoded articles.  (Well, I have seen
1694 one package that does that - gnus-uu, but somehow, I don't think that 
1695 counts...) Default is nil.")
1696
1697 (defvar gnus-uu-post-separate-description t
1698   "Non-nil means that the description will be posted in a separate article.
1699 The first article will typically be numbered (0/x).  If this variable
1700 is nil, the description the user enters will be included at the 
1701 beginning of the first article, which will be numbered (1/x).  Default 
1702 is t.")
1703
1704 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1705 (defvar gnus-uu-post-message-id nil)
1706 (defvar gnus-uu-post-inserted-file-name nil)
1707 (defvar gnus-uu-winconf-post-news nil)
1708
1709 (defun gnus-uu-post-news ()
1710   "Compose an article and post an encoded file."
1711   (interactive)
1712   (setq gnus-uu-post-inserted-file-name nil)
1713   (setq gnus-uu-winconf-post-news (current-window-configuration))
1714
1715   (gnus-summary-post-news)
1716
1717   (use-local-map (copy-keymap (current-local-map)))
1718   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1719   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1720   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1721   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1722       
1723   (if gnus-uu-post-include-before-composing
1724       (save-excursion (setq gnus-uu-post-inserted-file-name 
1725                             (gnus-uu-post-insert-binary)))))
1726
1727 (defun gnus-uu-post-insert-binary-in-article ()
1728   "Inserts an encoded file in the buffer.
1729 The user will be asked for a file name."
1730   (interactive)
1731   (save-excursion 
1732     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1733
1734 ;; Encodes with uuencode and substitutes all spaces with backticks.
1735 (defun gnus-uu-post-encode-uuencode (path file-name)
1736   (if (gnus-uu-post-encode-file "uuencode" path file-name)
1737       (progn
1738         (goto-char (point-min))
1739         (forward-line 1)
1740         (while (re-search-forward " " nil t)
1741           (replace-match "`"))
1742         t)))
1743
1744 ;; Encodes with uuencode and adds MIME headers.
1745 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1746   (if (gnus-uu-post-encode-uuencode path file-name)
1747       (progn
1748         (gnus-uu-post-make-mime file-name "x-uue")
1749         t)))
1750
1751 ;; Encodes with base64 and adds MIME headers
1752 (defun gnus-uu-post-encode-mime (path file-name)
1753   (if (gnus-uu-post-encode-file "mmencode" path file-name)
1754       (progn
1755         (gnus-uu-post-make-mime file-name "base64")
1756         t)))
1757
1758 ;; Adds MIME headers.
1759 (defun gnus-uu-post-make-mime (file-name encoding)
1760   (goto-char (point-min))
1761   (insert (format "Content-Type: %s; name=\"%s\"\n" 
1762                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list) 
1763                   file-name))
1764   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1765   (save-restriction
1766     (set-buffer gnus-message-buffer)
1767     (goto-char (point-min))
1768     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1769     (forward-line -1)
1770     (narrow-to-region 1 (point))
1771     (or (mail-fetch-field "mime-version")
1772         (progn
1773           (widen)
1774           (insert "MIME-Version: 1.0\n")))
1775     (widen)))
1776
1777 ;; Encodes a file PATH with COMMAND, leaving the result in the
1778 ;; current buffer.
1779 (defun gnus-uu-post-encode-file (command path file-name)
1780   (= 0 (call-process shell-file-name nil t nil shell-command-switch 
1781                      (format "%s %s %s" command path file-name))))
1782
1783 (defun gnus-uu-post-news-inews ()
1784   "Posts the composed news article and encoded file.
1785 If no file has been included, the user will be asked for a file."
1786   (interactive)
1787
1788   (let (file-name)
1789
1790     (if gnus-uu-post-inserted-file-name
1791         (setq file-name gnus-uu-post-inserted-file-name)
1792       (setq file-name (gnus-uu-post-insert-binary)))
1793   
1794     (if gnus-uu-post-threaded
1795         (let ((message-required-news-headers 
1796                (if (memq 'Message-ID message-required-news-headers)
1797                    message-required-news-headers
1798                  (cons 'Message-ID message-required-news-headers)))
1799               gnus-inews-article-hook)
1800
1801           (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1802                                             gnus-inews-article-hook
1803                                           (list gnus-inews-article-hook)))
1804           (setq gnus-inews-article-hook 
1805                 (cons
1806                  '(lambda ()
1807                     (save-excursion
1808                       (goto-char (point-min))
1809                       (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1810                           (setq gnus-uu-post-message-id 
1811                                 (buffer-substring 
1812                                  (match-beginning 1) (match-end 1)))
1813                         (setq gnus-uu-post-message-id nil))))
1814                  gnus-inews-article-hook))
1815           (gnus-uu-post-encoded file-name t))
1816       (gnus-uu-post-encoded file-name nil)))
1817   (setq gnus-uu-post-inserted-file-name nil)
1818   (and gnus-uu-winconf-post-news
1819        (set-window-configuration gnus-uu-winconf-post-news)))
1820       
1821 ;; Asks for a file to encode, encodes it and inserts the result in
1822 ;; the current buffer.  Returns the file name the user gave.
1823 (defun gnus-uu-post-insert-binary ()
1824   (let ((uuencode-buffer-name "*uuencode buffer*")
1825         file-path uubuf file-name)
1826
1827     (setq file-path (read-file-name 
1828                      "What file do you want to encode? "))
1829     (if (not (file-exists-p file-path))
1830         (error "%s: No such file" file-path))
1831
1832     (goto-char (point-max))
1833     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1834     
1835     (if (string-match "^~/" file-path)
1836         (setq file-path (concat "$HOME" (substring file-path 1))))
1837     (if (string-match "/[^/]*$" file-path)
1838         (setq file-name (substring file-path (1+ (match-beginning 0))))
1839       (setq file-name file-path))
1840
1841     (unwind-protect
1842         (if (save-excursion
1843               (set-buffer (setq uubuf 
1844                                 (get-buffer-create uuencode-buffer-name)))
1845               (erase-buffer)
1846               (funcall gnus-uu-post-encode-method file-path file-name))
1847             (insert-buffer-substring uubuf)
1848           (error "Encoding unsuccessful"))
1849       (kill-buffer uubuf))
1850     file-name))
1851
1852 ;; Posts the article and all of the encoded file.
1853 (defun gnus-uu-post-encoded (file-name &optional threaded)
1854   (let ((send-buffer-name "*uuencode send buffer*")
1855         (encoded-buffer-name "*encoded buffer*")
1856         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1857         (separator (concat mail-header-separator "\n\n"))
1858         uubuf length parts header i end beg
1859         beg-line minlen buf post-buf whole-len beg-binary end-binary)
1860
1861     (setq post-buf (current-buffer))
1862
1863     (goto-char (point-min))
1864     (if (not (re-search-forward 
1865               (if gnus-uu-post-separate-description 
1866                   (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1867                           "$")
1868                 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1869         (error "Internal error: No binary/header separator"))
1870     (beginning-of-line)
1871     (forward-line 1)
1872     (setq beg-binary (point))
1873     (setq end-binary (point-max))
1874
1875     (save-excursion 
1876       (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1877       (erase-buffer)
1878       (insert-buffer-substring post-buf beg-binary end-binary)
1879       (goto-char (point-min))
1880       (setq length (count-lines 1 (point-max)))
1881       (setq parts (/ length gnus-uu-post-length))
1882       (if (not (< (% length gnus-uu-post-length) 4))
1883           (setq parts (1+ parts))))
1884
1885     (if gnus-uu-post-separate-description
1886         (forward-line -1))
1887     (kill-region (point) (point-max))
1888
1889     (goto-char (point-min))
1890     (re-search-forward 
1891      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1892     (beginning-of-line)
1893     (setq header (buffer-substring 1 (point)))
1894
1895     (goto-char (point-min))
1896     (if (not gnus-uu-post-separate-description)
1897         ()
1898       (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1899           (progn
1900             (end-of-line)
1901             (insert (format " (0/%d)" parts))))
1902       (message-send))
1903
1904     (save-excursion
1905       (setq i 1)
1906       (setq beg 1)
1907       (while (not (> i parts))
1908         (set-buffer (get-buffer-create send-buffer-name))
1909         (erase-buffer)
1910         (insert header)
1911         (if (and threaded gnus-uu-post-message-id)
1912             (insert (format "References: %s\n" gnus-uu-post-message-id)))
1913         (insert separator)
1914         (setq whole-len
1915               (- 62 (length (format top-string "" file-name i parts ""))))
1916         (if (> 1 (setq minlen (/ whole-len 2)))
1917             (setq minlen 1))
1918         (setq 
1919          beg-line 
1920          (format top-string
1921                  (make-string minlen ?-) 
1922                  file-name i parts
1923                  (make-string 
1924                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1925
1926         (goto-char (point-min))
1927         (if (not (re-search-forward "^Subject: " nil t))
1928             ()
1929           (if (not threaded)
1930               (progn
1931                 (end-of-line)
1932                 (insert (format " (%d/%d)" i parts)))
1933             (if (or (and (= i 2) gnus-uu-post-separate-description)
1934                     (and (= i 1) (not gnus-uu-post-separate-description)))
1935                 (replace-match "Subject: Re: "))))
1936                   
1937         (goto-char (point-max))
1938         (save-excursion
1939           (set-buffer uubuf)
1940           (goto-char beg)
1941           (if (= i parts)
1942               (goto-char (point-max))
1943             (forward-line gnus-uu-post-length))
1944           (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1945               (forward-line -4))
1946           (setq end (point)))
1947         (insert-buffer-substring uubuf beg end)
1948         (insert beg-line)
1949         (insert "\n")
1950         (setq beg end)
1951         (setq i (1+ i))
1952         (goto-char (point-min))
1953         (re-search-forward
1954          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1955         (beginning-of-line)
1956         (forward-line 2)
1957         (if (re-search-forward 
1958              (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1959              nil t)
1960             (progn 
1961               (replace-match "")
1962               (forward-line 1)))
1963         (insert beg-line)
1964         (insert "\n")
1965         (let (message-sent-message-via)
1966           (message-send))))
1967
1968     (and (setq buf (get-buffer send-buffer-name))
1969          (kill-buffer buf))
1970     (and (setq buf (get-buffer encoded-buffer-name))
1971          (kill-buffer buf))
1972
1973     (if (not gnus-uu-post-separate-description)
1974         (progn
1975           (set-buffer-modified-p nil)
1976           (and (fboundp 'bury-buffer) (bury-buffer))))))
1977
1978 (provide 'gnus-uu)
1979
1980 ;; gnus-uu.el ends here