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