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