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