*** 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         article-series files)
1130  
1131     (while (and articles 
1132                 (not (memq 'error process-state))
1133                 (or sloppy
1134                     (not (memq 'end process-state))))
1135
1136       (setq article (pop articles))
1137       (push article article-series)
1138
1139       (unless articles 
1140         (if (eq state 'first)
1141             (setq state 'first-and-last)
1142           (setq state 'last)))
1143
1144       (let ((part (gnus-uu-part-number article)))
1145         (gnus-message 6 "Getting article %d%s..." 
1146                       article (if (string= part "") "" (concat ", " part))))
1147       (gnus-summary-display-article article)
1148       
1149       ;; Push the article to the processing function.
1150       (save-excursion
1151         (set-buffer gnus-original-article-buffer)
1152         (let ((buffer-read-only nil))
1153           (save-excursion
1154             (set-buffer gnus-summary-buffer)
1155             (setq process-state 
1156                   (funcall process-function
1157                            gnus-original-article-buffer state)))))
1158
1159       (gnus-summary-remove-process-mark article)
1160
1161       ;; If this is the beginning of a decoded file, we push it 
1162       ;; on to a list.
1163       (when (or (memq 'begin process-state)
1164                 (and (or (eq state 'first) 
1165                          (eq state 'first-and-last))
1166                      (memq 'ok process-state)))
1167         (if has-been-begin
1168             ;; If there is a `result-file' here, that means that the
1169             ;; file was unsuccessfully decoded, so we delete it.
1170             (when (and result-file 
1171                        (file-exists-p result-file)) 
1172               (delete-file result-file)))
1173         (when (memq 'begin process-state)
1174           (setq result-file (car process-state)))
1175         (setq has-been-begin t))
1176
1177       ;; Check whether we have decoded one complete file.
1178       (when (memq 'end process-state)
1179         (setq article-series nil)
1180         (setq has-been-begin nil)
1181         (if (stringp result-file)
1182             (setq files (list result-file))
1183           (setq files result-file))
1184         (setq result-file (car files))
1185         (while files
1186           (push (list (cons 'name (pop files))
1187                       (cons 'article article))
1188                 result-files))
1189         ;; Allow user-defined functions to be run on this file.
1190         (when gnus-uu-grabbed-file-functions
1191           (let ((funcs gnus-uu-grabbed-file-functions))
1192             (unless (listp funcs)
1193               (setq funcs (list funcs)))
1194             (while funcs
1195               (funcall (pop funcs) result-file))))
1196         ;; Check whether we have decoded enough articles.
1197         (and limit (= (length result-files) limit)
1198              (setq articles nil)))
1199
1200       ;; If this is the last article to be decoded, and
1201       ;; we still haven't reached the end, then we delete
1202       ;; the partially decoded file.
1203       (and (or (eq state 'last) (eq state 'first-and-last))
1204            (not (memq 'end process-state))
1205            result-file 
1206            (file-exists-p result-file)
1207            (delete-file result-file))
1208
1209       ;; If this was a file of the wrong sort, then 
1210       (when (and (or (memq 'wrong-type process-state)
1211                      (memq 'error process-state))
1212                  gnus-uu-unmark-articles-not-decoded)
1213         (gnus-summary-tick-article article t))
1214
1215       ;; Set the new series state.
1216       (if (and (not has-been-begin)
1217                (not sloppy)
1218                (or (memq 'end process-state)
1219                    (memq 'middle process-state)))
1220           (progn
1221             (setq process-state (list 'error))
1222             (gnus-message 2 "No begin part at the beginning")
1223             (sleep-for 2))
1224         (setq state 'middle)))
1225
1226     ;; When there are no result-files, then something must be wrong.
1227     (if result-files
1228         (message "")
1229       (cond
1230        ((not has-been-begin)
1231         (gnus-message 2 "Wrong type file"))
1232        ((memq 'error process-state)
1233         (gnus-message 2 "An error occurred during decoding"))
1234        ((not (or (memq 'ok process-state) 
1235                  (memq 'end process-state)))
1236         (gnus-message 2 "End of articles reached before end of file")))
1237       ;; Make unsuccessfully decoded articles unread.
1238       (when gnus-uu-unmark-articles-not-decoded
1239         (while article-series
1240           (gnus-summary-tick-article (pop article-series) t))))
1241
1242     result-files))
1243
1244 (defun gnus-uu-grab-view (file)
1245   "View FILE using the gnus-uu methods."
1246   (let ((action (gnus-uu-get-action file)))
1247     (gnus-execute-command
1248      (if (string-match "%" action)
1249          (format action file)
1250        (concat action " " file))
1251      (eq gnus-view-pseudos 'not-confirm))))
1252
1253 (defun gnus-uu-grab-move (file)
1254   "Move FILE to somewhere."
1255   (when gnus-uu-default-dir
1256     (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1257                            (file-name-nondirectory file))))
1258       (rename-file file to-file)
1259       (unless (file-exists-p file)
1260         (make-symbolic-link to-file file)))))
1261
1262 (defun gnus-uu-part-number (article)
1263   (let ((subject (mail-header-subject (gnus-summary-article-header article))))
1264     (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1265                       subject)
1266         (substring subject (match-beginning 0) (match-end 0))
1267       "")))
1268
1269 (defun gnus-uu-uudecode-sentinel (process event)
1270   (delete-process (get-process process)))
1271
1272 (defun gnus-uu-uustrip-article (process-buffer in-state)
1273   ;; Uudecodes a file asynchronously.
1274   (save-excursion
1275     (set-buffer process-buffer)
1276     (let ((state (list 'wrong-type))
1277           process-connection-type case-fold-search buffer-read-only 
1278           files start-char)
1279       (goto-char (point-min))
1280
1281       ;; Deal with ^M at the end of the lines.
1282       (when gnus-uu-kill-carriage-return
1283         (save-excursion
1284           (while (search-forward "\r" nil t)
1285             (delete-backward-char 1))))
1286
1287       (while (or (re-search-forward gnus-uu-begin-string nil t)
1288                  (re-search-forward gnus-uu-body-line nil t))
1289         (setq state (list 'ok))
1290         ;; Ok, we are at the first uucoded line.
1291         (beginning-of-line)
1292         (setq start-char (point))
1293
1294         (if (not (looking-at gnus-uu-begin-string))
1295             (setq state (list 'middle))
1296           ;; This is the beginning of an uuencoded article.
1297           ;; We replace certain characters that could make things messy.
1298           (setq gnus-uu-file-name 
1299                 (let ((nnheader-file-name-translation-alist
1300                        '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1301                   (nnheader-translate-file-chars (match-string 1))))
1302
1303           ;; Remove any non gnus-uu-body-line right after start.
1304           (forward-line 1)
1305           (while (and (not (eobp))
1306                       (not (looking-at gnus-uu-body-line)))
1307             (gnus-delete-line))
1308
1309           ;; If a process is running, we kill it.
1310           (when (and gnus-uu-uudecode-process
1311                      (memq (process-status gnus-uu-uudecode-process) 
1312                            '(run stop)))
1313             (delete-process gnus-uu-uudecode-process)
1314             (gnus-uu-unmark-list-of-grabbed t))
1315
1316           ;; Start a new uudecoding process.
1317           (setq gnus-uu-uudecode-process
1318                 (start-process 
1319                  "*uudecode*" 
1320                  (get-buffer-create gnus-uu-output-buffer-name)
1321                  "sh" "-c" 
1322                  (format "cd %s ; uudecode" gnus-uu-work-dir)))
1323           (set-process-sentinel 
1324            gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1325           (setq state (list 'begin))
1326           (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1327         
1328         ;; We look for the end of the thing to be decoded.
1329         (if (re-search-forward gnus-uu-end-string nil t)
1330             (setq state (cons 'end state))
1331           (goto-char (point-max))
1332           (re-search-backward gnus-uu-body-line nil t))
1333          
1334         (forward-line 1)
1335
1336         (when gnus-uu-uudecode-process
1337           (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1338             ;; Try to correct mishandled uucode.
1339             (when gnus-uu-correct-stripped-uucode
1340               (gnus-uu-check-correct-stripped-uucode start-char (point)))
1341
1342             ;; Send the text to the process.
1343             (condition-case nil
1344                 (process-send-region
1345                  gnus-uu-uudecode-process start-char (point))
1346               (error 
1347                (progn 
1348                  (delete-process gnus-uu-uudecode-process)
1349                  (gnus-message 2 "gnus-uu: Couldn't uudecode")
1350                  (setq state (list 'wrong-type)))))
1351
1352             (if (memq 'end state)
1353                 (progn
1354                   ;; Send an EOF, just in case.
1355                   (condition-case ()
1356                       (process-send-eof gnus-uu-uudecode-process)
1357                     (error nil))
1358                   (while (memq (process-status gnus-uu-uudecode-process)
1359                                '(open run))
1360                     (accept-process-output gnus-uu-uudecode-process 1)))
1361               (when (or (not gnus-uu-uudecode-process)
1362                         (not (memq (process-status gnus-uu-uudecode-process)
1363                                    '(run stop))))
1364                 (setq state (list 'wrong-type)))))))
1365
1366       (if (memq 'begin state)
1367           (cons (if (= (length files) 1) (car files) files) state)
1368         state))))
1369
1370 ;; This function is used by `gnus-uu-grab-articles' to treat
1371 ;; a shared article.
1372 (defun gnus-uu-unshar-article (process-buffer in-state)
1373   (let ((state (list 'ok))
1374         start-char)
1375     (save-excursion
1376       (set-buffer process-buffer)
1377       (goto-char (point-min))
1378       (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1379           (setq state (list 'wrong-type))
1380         (beginning-of-line)
1381         (setq start-char (point))
1382         (call-process-region 
1383          start-char (point-max) "sh" nil 
1384          (get-buffer-create gnus-uu-output-buffer-name) nil 
1385          "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1386     state))
1387
1388 ;; Returns the name of what the shar file is going to unpack.
1389 (defun gnus-uu-find-name-in-shar ()
1390   (let ((oldpoint (point))
1391         res)
1392     (goto-char (point-min))
1393     (if (re-search-forward gnus-uu-shar-name-marker nil t)
1394         (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1395     (goto-char oldpoint)
1396     res))
1397
1398 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1399 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1400 ;; found, or the name of the command to run if such a rule is found.
1401 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1402   (let ((action-list (copy-sequence file-action-list))
1403         (case-fold-search t)
1404         rule action)
1405     (and 
1406      (or no-ignore 
1407          (and (not 
1408                (and gnus-uu-ignore-files-by-name
1409                     (string-match gnus-uu-ignore-files-by-name file-name)))
1410               (not 
1411                (and gnus-uu-ignore-files-by-type
1412                     (string-match gnus-uu-ignore-files-by-type 
1413                                   (or (gnus-uu-choose-action 
1414                                        file-name gnus-uu-ext-to-mime-list t) 
1415                                       ""))))))
1416      (while (not (or (eq action-list ()) action))
1417        (setq rule (car action-list))
1418        (setq action-list (cdr action-list))
1419        (if (string-match (car rule) file-name)
1420            (setq action (cadr rule)))))
1421     action))
1422
1423 (defun gnus-uu-treat-archive (file-path)
1424   ;; Unpacks an archive. Returns t if unpacking is successful.
1425   (let ((did-unpack t)
1426         action command dir)
1427     (setq action (gnus-uu-choose-action 
1428                   file-path (append gnus-uu-user-archive-rules
1429                                     (if gnus-uu-ignore-default-archive-rules
1430                                         nil
1431                                       gnus-uu-default-archive-rules))))
1432
1433     (if (not action) (error "No unpackers for the file %s" file-path))
1434
1435     (string-match "/[^/]*$" file-path)
1436     (setq dir (substring file-path 0 (match-beginning 0)))
1437
1438     (if (member action gnus-uu-destructive-archivers)
1439         (copy-file file-path (concat file-path "~") t))
1440
1441     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1442
1443     (save-excursion
1444       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1445       (erase-buffer))
1446
1447     (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1448
1449     (if (= 0 (call-process "sh" nil 
1450                            (get-buffer-create gnus-uu-output-buffer-name)
1451                            nil "-c" command))
1452         (message "")
1453       (gnus-message 2 "Error during unpacking of archive")
1454       (setq did-unpack nil))
1455
1456     (if (member action gnus-uu-destructive-archivers)
1457         (rename-file (concat file-path "~") file-path t))
1458
1459     did-unpack))
1460
1461 (defun gnus-uu-dir-files (dir)
1462   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1463         files file)
1464     (while dirs
1465       (if (file-directory-p (setq file (car dirs)))
1466           (setq files (append files (gnus-uu-dir-files file)))
1467         (setq files (cons file files)))
1468       (setq dirs (cdr dirs)))
1469     files))
1470
1471 (defun gnus-uu-unpack-files (files &optional ignore)
1472   ;; Go through FILES and look for files to unpack. 
1473   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1474          (ofiles files)
1475          file did-unpack)
1476     (while files
1477       (setq file (cdr (assq 'name (car files))))
1478       (if (and (not (member file ignore))
1479                (equal (gnus-uu-get-action (file-name-nondirectory file))
1480                       "gnus-uu-archive"))
1481           (progn
1482             (setq did-unpack (cons file did-unpack))
1483             (or (gnus-uu-treat-archive file)
1484                 (gnus-message 2 "Error during unpacking of %s" file))
1485             (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1486                    (nfiles newfiles))
1487               (while nfiles
1488                 (or (member (car nfiles) totfiles)
1489                     (setq ofiles (cons (list (cons 'name (car nfiles))
1490                                              (cons 'original file))
1491                                        ofiles)))
1492                 (setq nfiles (cdr nfiles)))
1493               (setq totfiles newfiles))))
1494       (setq files (cdr files)))
1495     (if did-unpack 
1496         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1497       ofiles)))
1498
1499 (defun gnus-uu-ls-r (dir)
1500   (let* ((files (gnus-uu-directory-files dir t))
1501          (ofiles files))
1502     (while files
1503       (if (file-directory-p (car files))
1504           (progn
1505             (setq ofiles (delete (car files) ofiles))
1506             (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1507       (setq files (cdr files)))
1508     ofiles))
1509
1510 ;; Various stuff
1511
1512 (defun gnus-uu-directory-files (dir &optional full)
1513   (let (files out file)
1514     (setq files (directory-files dir full))
1515     (while files
1516       (setq file (car files))
1517       (setq files (cdr files))
1518       (or (string-match "/\\.\\.?$" file)
1519           (setq out (cons file out))))
1520     (setq out (nreverse out))
1521     out))
1522
1523 (defun gnus-uu-check-correct-stripped-uucode (start end)
1524   (save-excursion
1525     (let (found beg length)
1526       (if (not gnus-uu-correct-stripped-uucode)
1527           ()
1528         (goto-char start)
1529
1530         (if (re-search-forward " \\|`" end t)
1531             (progn
1532               (goto-char start)
1533               (while (not (eobp))
1534                 (progn
1535                   (if (looking-at "\n") (replace-match ""))
1536                   (forward-line 1))))
1537             
1538           (while (not (eobp))
1539             (if (looking-at (concat gnus-uu-begin-string "\\|" 
1540                                     gnus-uu-end-string))
1541                 ()
1542               (if (not found)
1543                   (progn
1544                     (beginning-of-line)
1545                     (setq beg (point))
1546                     (end-of-line)
1547                     (setq length (- (point) beg))))
1548               (setq found t)
1549               (beginning-of-line)
1550               (setq beg (point))
1551               (end-of-line)
1552               (if (not (= length (- (point) beg)))
1553                   (insert (make-string (- length (- (point) beg)) ? ))))
1554             (forward-line 1)))))))
1555
1556 (defvar gnus-uu-tmp-alist nil)
1557
1558 (defun gnus-uu-initialize (&optional scan)
1559   (let (entry)
1560     (if (and (not scan)
1561              (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1562                  (if (file-exists-p (cdr entry))
1563                      (setq gnus-uu-work-dir (cdr entry))
1564                    (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1565                    nil)))
1566         t
1567       (setq gnus-uu-tmp-dir (file-name-as-directory 
1568                              (expand-file-name gnus-uu-tmp-dir)))
1569       (if (not (file-directory-p gnus-uu-tmp-dir))
1570           (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1571         (if (not (file-writable-p gnus-uu-tmp-dir))
1572             (error "Temp directory %s can't be written to" 
1573                    gnus-uu-tmp-dir)))
1574
1575       (setq gnus-uu-work-dir 
1576             (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1577       (if (not (file-directory-p gnus-uu-work-dir)) 
1578           (gnus-make-directory gnus-uu-work-dir))
1579       (set-file-modes gnus-uu-work-dir 448)
1580       (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1581       (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1582                                     gnus-uu-tmp-alist)))))
1583
1584
1585 ;; Kills the temporary uu buffers, kills any processes, etc.
1586 (defun gnus-uu-clean-up ()
1587   (let (buf pst)
1588     (and gnus-uu-uudecode-process
1589          (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1590                '(stop run))
1591          (delete-process gnus-uu-uudecode-process))
1592     (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1593          (kill-buffer buf))))
1594
1595 ;; Inputs an action and a file and returns a full command, putting
1596 ;; quotes round the file name and escaping any quotes in the file name.
1597 (defun gnus-uu-command (action file)
1598   (let ((ofile ""))
1599     (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1600       (progn
1601         (setq ofile
1602               (concat ofile (substring file 0 (match-beginning 0)) "\\"
1603                       (substring file (match-beginning 0) (match-end 0))))
1604         (setq file (substring file (1+ (match-beginning 0))))))
1605     (setq ofile (concat "\"" ofile file "\""))
1606     (if (string-match "%s" action)
1607         (format action ofile)
1608       (concat action " " ofile))))
1609
1610 (defun gnus-uu-delete-work-dir (&optional dir)
1611   "Delete recursively all files and directories under `gnus-uu-work-dir'."
1612   (unless dir
1613     (setq dir gnus-uu-work-dir))
1614   (gnus-message 7 "Deleting directory %s..." dir)
1615   (when (and dir
1616              (file-exists-p dir))
1617     (let ((files (directory-files dir t nil t))
1618           file)
1619       (while (setq file (pop files))
1620         (unless (string-match "/\\.\\.?$" file)
1621           (if (file-directory-p file)
1622               (gnus-uu-delete-work-dir file)
1623             (gnus-message 9 "Deleting file %s..." file)
1624             (delete-file file))))
1625       (delete-directory dir)))
1626   (gnus-message 7 ""))
1627
1628 ;; Initializing
1629
1630 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1631 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1632
1633 \f
1634
1635 ;;;
1636 ;;; uuencoded posting
1637 ;;;
1638
1639 (require 'sendmail)
1640 (require 'rnews)
1641
1642 ;; Any function that is to be used as and encoding method will take two
1643 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1644 ;; and "spiral.jpg", respectively.) The function should return nil if
1645 ;; the encoding wasn't successful.
1646 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1647   "Function used for encoding binary files.
1648 There are three functions supplied with gnus-uu for encoding files:
1649 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1650 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
1651 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
1652 uuencode and adds MIME headers.")
1653
1654 (defvar gnus-uu-post-include-before-composing nil
1655   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1656 If this variable is t, you can either include an encoded file with
1657 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1658
1659 (defvar gnus-uu-post-length 990
1660   "Maximum length of an article.
1661 The encoded file will be split into how many articles it takes to
1662 post the entire file.")
1663
1664 (defvar gnus-uu-post-threaded nil
1665   "Non-nil means that gnus-uu will post the encoded file in a thread.
1666 This may not be smart, as no other decoder I have seen are able to
1667 follow threads when collecting uuencoded articles. (Well, I have seen
1668 one package that does that - gnus-uu, but somehow, I don't think that 
1669 counts...) Default is nil.")
1670
1671 (defvar gnus-uu-post-separate-description t
1672   "Non-nil means that the description will be posted in a separate article.
1673 The first article will typically be numbered (0/x). If this variable
1674 is nil, the description the user enters will be included at the 
1675 beginning of the first article, which will be numbered (1/x). Default 
1676 is t.")
1677
1678 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1679 (defvar gnus-uu-post-message-id nil)
1680 (defvar gnus-uu-post-inserted-file-name nil)
1681 (defvar gnus-uu-winconf-post-news nil)
1682
1683 (defun gnus-uu-post-news ()
1684   "Compose an article and post an encoded file."
1685   (interactive)
1686   (setq gnus-uu-post-inserted-file-name nil)
1687   (setq gnus-uu-winconf-post-news (current-window-configuration))
1688
1689   (gnus-summary-post-news)
1690
1691   (use-local-map (copy-keymap (current-local-map)))
1692   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1693   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1694   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1695   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1696       
1697   (if gnus-uu-post-include-before-composing
1698       (save-excursion (setq gnus-uu-post-inserted-file-name 
1699                             (gnus-uu-post-insert-binary)))))
1700
1701 (defun gnus-uu-post-insert-binary-in-article ()
1702   "Inserts an encoded file in the buffer.
1703 The user will be asked for a file name."
1704   (interactive)
1705   (save-excursion 
1706     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1707
1708 ;; Encodes with uuencode and substitutes all spaces with backticks.
1709 (defun gnus-uu-post-encode-uuencode (path file-name)
1710   (if (gnus-uu-post-encode-file "uuencode" path file-name)
1711       (progn
1712         (goto-char (point-min))
1713         (forward-line 1)
1714         (while (re-search-forward " " nil t)
1715           (replace-match "`"))
1716         t)))
1717
1718 ;; Encodes with uuencode and adds MIME headers.
1719 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1720   (if (gnus-uu-post-encode-uuencode path file-name)
1721       (progn
1722         (gnus-uu-post-make-mime file-name "x-uue")
1723         t)))
1724
1725 ;; Encodes with base64 and adds MIME headers
1726 (defun gnus-uu-post-encode-mime (path file-name)
1727   (if (gnus-uu-post-encode-file "mmencode" path file-name)
1728       (progn
1729         (gnus-uu-post-make-mime file-name "base64")
1730         t)))
1731
1732 ;; Adds MIME headers.
1733 (defun gnus-uu-post-make-mime (file-name encoding)
1734   (goto-char (point-min))
1735   (insert (format "Content-Type: %s; name=\"%s\"\n" 
1736                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list) 
1737                   file-name))
1738   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1739   (save-restriction
1740     (set-buffer gnus-message-buffer)
1741     (goto-char (point-min))
1742     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1743     (forward-line -1)
1744     (narrow-to-region 1 (point))
1745     (or (mail-fetch-field "mime-version")
1746         (progn
1747           (widen)
1748           (insert "MIME-Version: 1.0\n")))
1749     (widen)))
1750
1751 ;; Encodes a file PATH with COMMAND, leaving the result in the
1752 ;; current buffer.
1753 (defun gnus-uu-post-encode-file (command path file-name)
1754   (= 0 (call-process "sh" nil t nil "-c" 
1755                      (format "%s %s %s" command path file-name))))
1756
1757 (defun gnus-uu-post-news-inews ()
1758   "Posts the composed news article and encoded file.
1759 If no file has been included, the user will be asked for a file."
1760   (interactive)
1761
1762   (let (file-name)
1763
1764     (if gnus-uu-post-inserted-file-name
1765         (setq file-name gnus-uu-post-inserted-file-name)
1766       (setq file-name (gnus-uu-post-insert-binary)))
1767   
1768     (if gnus-uu-post-threaded
1769         (let ((message-required-news-headers 
1770                (if (memq 'Message-ID message-required-news-headers)
1771                    message-required-news-headers
1772                  (cons 'Message-ID message-required-news-headers)))
1773               gnus-inews-article-hook)
1774
1775           (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1776                                             gnus-inews-article-hook
1777                                           (list gnus-inews-article-hook)))
1778           (setq gnus-inews-article-hook 
1779                 (cons
1780                  '(lambda ()
1781                     (save-excursion
1782                       (goto-char (point-min))
1783                       (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1784                           (setq gnus-uu-post-message-id 
1785                                 (buffer-substring 
1786                                  (match-beginning 1) (match-end 1)))
1787                         (setq gnus-uu-post-message-id nil))))
1788                  gnus-inews-article-hook))
1789           (gnus-uu-post-encoded file-name t))
1790       (gnus-uu-post-encoded file-name nil)))
1791   (setq gnus-uu-post-inserted-file-name nil)
1792   (and gnus-uu-winconf-post-news
1793        (set-window-configuration gnus-uu-winconf-post-news)))
1794       
1795 ;; Asks for a file to encode, encodes it and inserts the result in
1796 ;; the current buffer. Returns the file name the user gave.
1797 (defun gnus-uu-post-insert-binary ()
1798   (let ((uuencode-buffer-name "*uuencode buffer*")
1799         file-path uubuf file-name)
1800
1801     (setq file-path (read-file-name 
1802                      "What file do you want to encode? "))
1803     (if (not (file-exists-p file-path))
1804         (error "%s: No such file" file-path))
1805
1806     (goto-char (point-max))
1807     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1808     
1809     (if (string-match "^~/" file-path)
1810         (setq file-path (concat "$HOME" (substring file-path 1))))
1811     (if (string-match "/[^/]*$" file-path)
1812         (setq file-name (substring file-path (1+ (match-beginning 0))))
1813       (setq file-name file-path))
1814
1815     (unwind-protect
1816         (if (save-excursion
1817               (set-buffer (setq uubuf 
1818                                 (get-buffer-create uuencode-buffer-name)))
1819               (erase-buffer)
1820               (funcall gnus-uu-post-encode-method file-path file-name))
1821             (insert-buffer-substring uubuf)
1822           (error "Encoding unsuccessful"))
1823       (kill-buffer uubuf))
1824     file-name))
1825
1826 ;; Posts the article and all of the encoded file.
1827 (defun gnus-uu-post-encoded (file-name &optional threaded)
1828   (let ((send-buffer-name "*uuencode send buffer*")
1829         (encoded-buffer-name "*encoded buffer*")
1830         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1831         (separator (concat mail-header-separator "\n\n"))
1832         uubuf length parts header i end beg
1833         beg-line minlen buf post-buf whole-len beg-binary end-binary)
1834
1835     (setq post-buf (current-buffer))
1836
1837     (goto-char (point-min))
1838     (if (not (re-search-forward 
1839               (if gnus-uu-post-separate-description 
1840                   (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1841                           "$")
1842                 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1843         (error "Internal error: No binary/header separator"))
1844     (beginning-of-line)
1845     (forward-line 1)
1846     (setq beg-binary (point))
1847     (setq end-binary (point-max))
1848
1849     (save-excursion 
1850       (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1851       (erase-buffer)
1852       (insert-buffer-substring post-buf beg-binary end-binary)
1853       (goto-char (point-min))
1854       (setq length (count-lines 1 (point-max)))
1855       (setq parts (/ length gnus-uu-post-length))
1856       (if (not (< (% length gnus-uu-post-length) 4))
1857           (setq parts (1+ parts))))
1858
1859     (if gnus-uu-post-separate-description
1860         (forward-line -1))
1861     (kill-region (point) (point-max))
1862
1863     (goto-char (point-min))
1864     (re-search-forward 
1865      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1866     (beginning-of-line)
1867     (setq header (buffer-substring 1 (point)))
1868
1869     (goto-char (point-min))
1870     (if (not gnus-uu-post-separate-description)
1871         ()
1872       (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1873           (progn
1874             (end-of-line)
1875             (insert (format " (0/%d)" parts))))
1876       (gnus-inews-news))
1877
1878     (save-excursion
1879       (setq i 1)
1880       (setq beg 1)
1881       (while (not (> i parts))
1882         (set-buffer (get-buffer-create send-buffer-name))
1883         (erase-buffer)
1884         (insert header)
1885         (if (and threaded gnus-uu-post-message-id)
1886             (insert (format "References: %s\n" gnus-uu-post-message-id)))
1887         (insert separator)
1888         (setq whole-len
1889               (- 62 (length (format top-string "" file-name i parts ""))))
1890         (if (> 1 (setq minlen (/ whole-len 2)))
1891             (setq minlen 1))
1892         (setq 
1893          beg-line 
1894          (format top-string
1895                  (make-string minlen ?-) 
1896                  file-name i parts
1897                  (make-string 
1898                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1899
1900         (goto-char (point-min))
1901         (if (not (re-search-forward "^Subject: " nil t))
1902             ()
1903           (if (not threaded)
1904               (progn
1905                 (end-of-line)
1906                 (insert (format " (%d/%d)" i parts)))
1907             (if (or (and (= i 2) gnus-uu-post-separate-description)
1908                     (and (= i 1) (not gnus-uu-post-separate-description)))
1909                 (replace-match "Subject: Re: "))))
1910                   
1911         (goto-char (point-max))
1912         (save-excursion
1913           (set-buffer uubuf)
1914           (goto-char beg)
1915           (if (= i parts)
1916               (goto-char (point-max))
1917             (forward-line gnus-uu-post-length))
1918           (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1919               (forward-line -4))
1920           (setq end (point)))
1921         (insert-buffer-substring uubuf beg end)
1922         (insert beg-line)
1923         (insert "\n")
1924         (setq beg end)
1925         (setq i (1+ i))
1926         (goto-char (point-min))
1927         (re-search-forward
1928          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1929         (beginning-of-line)
1930         (forward-line 2)
1931         (if (re-search-forward 
1932              (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1933              nil t)
1934             (progn 
1935               (replace-match "")
1936               (forward-line 1)))
1937         (insert beg-line)
1938         (insert "\n")
1939         (gnus-inews-news)))
1940
1941     (and (setq buf (get-buffer send-buffer-name))
1942          (kill-buffer buf))
1943     (and (setq buf (get-buffer encoded-buffer-name))
1944          (kill-buffer buf))
1945
1946     (if (not gnus-uu-post-separate-description)
1947         (progn
1948           (set-buffer-modified-p nil)
1949           (and (fboundp 'bury-buffer) (bury-buffer))))))
1950
1951 (provide 'gnus-uu)
1952
1953 ;; gnus-uu.el ends here