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