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