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