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