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