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