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