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