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