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