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