1 ;;; gnus-uu.el --- extract (uu)encoded files in Gnus
2 ;; Copyright (C) 1985,86,87,93,94,95 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 ;; Default viewing action rules
34 (defvar gnus-uu-default-view-rules
35 '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed s/\r//g")
36 ("\\.pas$" "cat %s | sed s/\r//g")
37 ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
38 ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "xv")
39 ("\\.tga$" "tgatoppm %s | xv -")
40 ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
41 "sox -v .5 %s -t .au -u - > /dev/audio")
42 ("\\.au$" "cat %s > /dev/audio")
44 ("\\.ps$" "ghostview")
46 ("\\.html$" "xmosaic")
47 ("\\.mpe?g$" "mpeg_play")
48 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
49 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
51 "*Default actions to be taken when the user asks to view a file.
52 To change the behaviour, you can either edit this variable or set
53 `gnus-uu-user-view-rules' to something useful.
57 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
58 following in your .emacs file:
60 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
62 Both these variables are lists of lists with two string elements. The
63 first string is a regular expression. If the file name matches this
64 regular expression, the command in the second string is executed with
65 the file as an argument.
67 If the command string contains \"%s\", the file name will be inserted
68 at that point in the command string. If there's no \"%s\" in the
69 command string, the file name will be appended to the command string
72 There are several user variables to tailor the behaviour of gnus-uu to
73 your needs. First we have `gnus-uu-user-view-rules', which is the
74 variable gnus-uu first consults when trying to decide how to view a
75 file. If this variable contains no matches, gnus-uu examines the
76 default rule vaiable provided in this package. If gnus-uu finds no
77 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
80 (defvar gnus-uu-user-view-rules nil
81 "*Variable detailing what actions are to be taken to view a file.
82 See the documentation on the `gnus-uu-default-view-rules' variable for
85 (defvar gnus-uu-user-view-rules-end
87 "*Variable saying what actions are to be taken if no rule matched the file name.
88 See the documentation on the `gnus-uu-default-view-rules' variable for
91 ;; Default unpacking commands
93 (defvar gnus-uu-default-archive-rules
94 '(("\\.tar$" "tar xf")
95 ("\\.zip$" "unzip -o")
99 ("\\.\\(lzh\\|lha\\)$" "lha x")
100 ("\\.Z$" "uncompress")
102 ("\\.arc$" "arc -x")))
104 (defvar gnus-uu-destructive-archivers
105 (list "uncompress" "gunzip"))
107 (defvar gnus-uu-user-archive-rules nil
108 "*A list that can be set to override the default archive unpacking commands.
109 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
110 unpack zip files, say the following:
111 (setq gnus-uu-user-archive-rules
112 '((\"\\\\.tar$\" \"untar\")
113 (\"\\\\.zip$\" \"zip -x\")))")
115 (defvar gnus-uu-ignore-files-by-name nil
116 "*A regular expression saying what files should not be viewed based on name.
117 If, for instance, you want gnus-uu to ignore all .au and .wav files,
118 you could say something like
120 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
122 Note that this variable can be used in conjunction with the
123 `gnus-uu-ignore-files-by-type' variable.")
125 (defvar gnus-uu-ignore-files-by-type nil
126 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
127 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
128 you could say something like
130 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
132 Note that this variable can be used in conjunction with the
133 `gnus-uu-ignore-files-by-name' variable.")
135 ;; Pseudo-MIME support
137 (defconst gnus-uu-ext-to-mime-list
138 '(("\\.gif$" "image/gif")
139 ("\\.jpe?g$" "image/jpeg")
140 ("\\.tiff?$" "image/tiff")
141 ("\\.xwd$" "image/xwd")
142 ("\\.pbm$" "image/pbm")
143 ("\\.pgm$" "image/pgm")
144 ("\\.ppm$" "image/ppm")
145 ("\\.xbm$" "image/xbm")
146 ("\\.pcx$" "image/pcx")
147 ("\\.tga$" "image/tga")
148 ("\\.ps$" "image/postscript")
149 ("\\.fli$" "video/fli")
150 ("\\.wav$" "audio/wav")
151 ("\\.aiff$" "audio/aiff")
152 ("\\.hcom$" "audio/hcom")
153 ("\\.voc$" "audio/voc")
154 ("\\.smp$" "audio/smp")
155 ("\\.mod$" "audio/mod")
156 ("\\.dvi$" "image/dvi")
157 ("\\.mpe?g$" "video/mpeg")
158 ("\\.au$" "audio/basic")
159 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
160 ("\\.\\(c\\|h\\)$" "text/source")
161 ("read.*me" "text/plain")
162 ("\\.html$" "text/html")
163 ("\\.bat$" "text/bat")
164 ("\\.[1-6]$" "text/man")
165 ("\\.flc$" "video/flc")
166 ("\\.rle$" "video/rle")
167 ("\\.pfx$" "video/pfx")
168 ("\\.avi$" "video/avi")
169 ("\\.sme$" "video/sme")
170 ("\\.rpza$" "video/prza")
171 ("\\.dl$" "video/dl")
172 ("\\.qt$" "video/qt")
173 ("\\.rsrc$" "video/rsrc")
174 ("\\..*$" "unknown/unknown")))
176 ;; Various variables users may set
178 (defvar gnus-uu-tmp-dir "/tmp/"
179 "*Variable saying where gnus-uu is to do its work.
180 Default is \"/tmp/\".")
182 (defvar gnus-uu-do-not-unpack-archives nil
183 "*Non-nil means that gnus-uu won't peek inside archives looking for files to dispay.
186 (defvar gnus-uu-view-and-save nil
187 "*Non-nil means that the user will always be asked to save a file after viewing it.
188 If the variable is nil, the suer will only be asked to save if the
189 viewing is unsuccessful. Default is nil.")
191 (defvar gnus-uu-ignore-default-view-rules nil
192 "*Non-nil means that gnus-uu will ignore the default viewing rules.
193 Only the user viewing rules will be consulted. Default is nil.")
195 (defvar gnus-uu-ignore-default-archive-rules nil
196 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
197 Only the user unpacking commands will be consulted. Default is nil.")
199 (defvar gnus-uu-kill-carriage-return t
200 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
203 (defvar gnus-uu-view-with-metamail nil
204 "*Non-nil means that files will be viewed with metamail.
205 The gnus-uu viewing functions will be ignored and gnus-uu will try
206 to guess at a content-type based on file name suffixes. Default
209 (defvar gnus-uu-unmark-articles-not-decoded nil
210 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
213 (defvar gnus-uu-correct-stripped-uucode nil
214 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had traling spaces deleted.
217 (defvar gnus-uu-save-in-digest nil
218 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
219 If this variable is nil, gnus-uu will just save everything in a
220 file without any embellishments. The digesting almost conforms to RFC1153 -
221 no easy way to specify any meaningful volume and issue numbers were found,
222 so I simply dropped them.")
224 (defvar gnus-uu-digest-headers
225 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
226 "^Summary:" "^References:")
227 "*List of regexps to match headers included in digested messages.
228 The headers will be included in the sequence they are matched.")
230 (defvar gnus-uu-save-separate-articles nil
231 "*Non-nil means that gnus-uu will save articles in separate files.")
233 ;; Internal variables
235 (defvar gnus-uu-saved-article-name nil)
237 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
238 (defconst gnus-uu-end-string "^end[ \t]*$")
240 (defconst gnus-uu-body-line "^M")
242 (while (> (setq i (1- i)) 0)
243 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
244 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
246 ;"^M.............................................................?$"
248 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
250 (defvar gnus-uu-shar-file-name nil)
251 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
253 (defconst gnus-uu-postscript-begin-string "^%!PS-")
254 (defconst gnus-uu-postscript-end-string "^%%EOF$")
256 (defvar gnus-uu-file-name nil)
257 (defconst gnus-uu-uudecode-process nil)
259 (defvar gnus-uu-generated-file-list nil)
260 (defvar gnus-uu-work-dir nil)
262 (defconst gnus-uu-output-buffer-name "*Gnus UU Output*")
264 (defconst gnus-uu-highest-article-number 1)
265 (defvar gnus-uu-default-dir default-directory)
269 (defvar gnus-uu-extract-map nil)
270 (defvar gnus-uu-extract-view-map nil)
271 (defvar gnus-uu-mark-map nil)
273 (define-prefix-command 'gnus-uu-mark-map)
274 (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
275 (define-key gnus-uu-mark-map "p" 'gnus-summary-mark-as-processable)
276 (define-key gnus-uu-mark-map "u" 'gnus-summary-unmark-as-processable)
277 (define-key gnus-uu-mark-map "U" 'gnus-summary-unmark-all-processable)
278 (define-key gnus-uu-mark-map "s" 'gnus-uu-mark-series)
279 (define-key gnus-uu-mark-map "r" 'gnus-uu-mark-region)
280 (define-key gnus-uu-mark-map "R" 'gnus-uu-mark-by-regexp)
281 (define-key gnus-uu-mark-map "t" 'gnus-uu-mark-thread)
282 (define-key gnus-uu-mark-map "a" 'gnus-uu-mark-all)
283 (define-key gnus-uu-mark-map "S" 'gnus-uu-mark-sparse)
285 (define-prefix-command 'gnus-uu-extract-map)
286 (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
287 ;;(define-key gnus-uu-extract-map "x" 'gnus-uu-extract-any)
288 ;;(define-key gnus-uu-extract-map "m" 'gnus-uu-extract-mime)
289 (define-key gnus-uu-extract-map "u" 'gnus-uu-decode-uu)
290 (define-key gnus-uu-extract-map "U" 'gnus-uu-decode-uu-and-save)
291 (define-key gnus-uu-extract-map "s" 'gnus-uu-decode-unshar)
292 (define-key gnus-uu-extract-map "S" 'gnus-uu-decode-unshar-and-save)
293 (define-key gnus-uu-extract-map "o" 'gnus-uu-decode-save)
294 (define-key gnus-uu-extract-map "O" 'gnus-uu-decode-save)
295 (define-key gnus-uu-extract-map "b" 'gnus-uu-decode-binhex)
296 (define-key gnus-uu-extract-map "B" 'gnus-uu-decode-binhex)
297 (define-key gnus-uu-extract-map "p" 'gnus-uu-decode-postscript)
298 (define-key gnus-uu-extract-map "P" 'gnus-uu-decode-postscript-and-save)
300 (define-prefix-command 'gnus-uu-extract-view-map)
301 (define-key gnus-uu-extract-map "v" 'gnus-uu-extract-view-map)
302 (define-key gnus-uu-extract-view-map "u" 'gnus-uu-decode-uu-view)
303 (define-key gnus-uu-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
304 (define-key gnus-uu-extract-view-map "s" 'gnus-uu-decode-unshar-view)
305 (define-key gnus-uu-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
306 (define-key gnus-uu-extract-view-map "o" 'gnus-uu-decode-save-view)
307 (define-key gnus-uu-extract-view-map "O" 'gnus-uu-decode-save-view)
308 (define-key gnus-uu-extract-view-map "b" 'gnus-uu-decode-binhex-view)
309 (define-key gnus-uu-extract-view-map "B" 'gnus-uu-decode-binhex-view)
310 (define-key gnus-uu-extract-view-map "p" 'gnus-uu-decode-postscript-view)
311 (define-key gnus-uu-extract-view-map "P" 'gnus-uu-decode-postscript-and-save-view)
317 (defun gnus-uu-decode-uu (n)
318 "Uudecodes the current article."
320 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
322 (defun gnus-uu-decode-uu-and-save (n dir)
323 "Decodes and saves the resulting file."
325 (list current-prefix-arg
326 (read-file-name "Uudecode and save in dir: "
328 gnus-uu-default-dir t)))
329 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir))
331 (defun gnus-uu-decode-unshar (n)
332 "Unshars the current article."
334 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n))
336 (defun gnus-uu-decode-unshar-and-save (n dir)
337 "Unshars and saves the current article."
339 (list current-prefix-arg
340 (read-file-name "Unshar and save in dir: "
342 gnus-uu-default-dir t)))
343 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir))
345 (defun gnus-uu-decode-save (n file)
346 "Saves the current article."
348 (list current-prefix-arg
350 (if gnus-uu-save-separate-articles
351 "Save articles is dir: "
352 "Save articles in file: ")
354 gnus-uu-default-dir)))
355 (setq gnus-uu-saved-article-name file)
356 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t)
357 (setq gnus-uu-generated-file-list
358 (delete file gnus-uu-generated-file-list)))
360 (defun gnus-uu-decode-binhex (n dir)
361 "Unbinhexes the current article."
363 (list current-prefix-arg
364 (read-file-name "Unbinhex and save in dir: "
366 gnus-uu-default-dir t)))
367 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
369 (defun gnus-uu-decode-uu-view (n)
370 "Uudecodes and views the current article."
372 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
373 (gnus-uu-decode-uu n)))
375 (defun gnus-uu-decode-uu-and-save-view (n dir)
376 "Decodes, views and saves the resulting file."
378 (list current-prefix-arg
379 (read-file-name "Uudecode, view and save in dir: "
381 gnus-uu-default-dir t)))
382 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
383 (gnus-uu-decode-uu-and-save n dir)))
385 (defun gnus-uu-decode-unshar-view (n)
386 "Unshars and views the current article."
388 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
389 (gnus-uu-decode-unshar n)))
391 (defun gnus-uu-decode-unshar-and-save-view (n dir)
392 "Unshars and saves the current article."
394 (list current-prefix-arg
395 (read-file-name "Unshar, view and save in dir: "
397 gnus-uu-default-dir t)))
398 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
399 (gnus-uu-decode-unshar-and-save n dir)))
401 (defun gnus-uu-decode-save-view (n file)
402 "Saves and views the current article."
404 (list current-prefix-arg
405 (read-file-name (if gnus-uu-save-separate-articles
406 "Save articles is dir: "
407 "Save articles in file: ")
408 gnus-uu-default-dir gnus-uu-default-dir)))
409 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
410 (gnus-uu-decode-save n file)))
412 (defun gnus-uu-decode-binhex-view (n file)
413 "Unbinhexes and views the current article."
415 (list current-prefix-arg
416 (read-file-name "Unbinhex, view and save in dir: "
417 gnus-uu-default-dir gnus-uu-default-dir)))
418 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
419 (gnus-uu-decode-binhex n file)))
422 ;; Digest and forward articles
424 (defun gnus-uu-digest-mail-forward (n &optional post)
425 "Digests and forwards all articles in this series."
428 (let ((gnus-uu-save-in-digest t)
429 (file (concat gnus-uu-work-dir (make-temp-name "forward")))
430 (winconf (current-window-configuration))
432 (gnus-uu-decode-save n file)
433 (gnus-uu-add-file file)
434 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
435 (gnus-add-current-to-buffer-list)
437 (delete-other-windows)
439 (goto-char (point-min))
440 (and (re-search-forward "^Subject: ")
442 (delete-region (point) (gnus-point-at-eol))
443 (insert "Digested Articles")))
444 (goto-char (point-min))
445 (and (re-search-forward "^From: ")
447 (delete-region (point) (gnus-point-at-eol))
450 (gnus-forward-using-post)
451 (funcall gnus-mail-forward-method))
454 (defun gnus-uu-digest-post-forward (n)
455 "Digest and forward to a newsgroup."
457 (gnus-uu-digest-mail-forward n t))
461 (defun gnus-uu-mark-by-regexp (regexp)
462 "Ask for a regular expression and set the process mark on all articles that match."
463 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
464 (gnus-set-global-variables)
465 (let ((articles (gnus-uu-find-articles-matching regexp)))
467 (gnus-summary-set-process-mark (car articles))
468 (setq articles (cdr articles)))
470 (gnus-summary-position-cursor))
472 (defun gnus-uu-mark-series ()
473 "Mark the current series with the process mark."
475 (gnus-set-global-variables)
476 (let ((articles (gnus-uu-find-articles-matching)))
478 (gnus-summary-set-process-mark (car articles))
479 (setq articles (cdr articles)))
481 (gnus-summary-position-cursor))
483 (defun gnus-uu-mark-region (beg end)
484 "Marks all articles between point and mark."
486 (gnus-set-global-variables)
489 (while (< (point) end)
490 (gnus-summary-set-process-mark (gnus-summary-article-number))
492 (gnus-summary-position-cursor))
494 (defun gnus-uu-mark-thread ()
495 "Marks all articles downwards in this thread."
497 (gnus-set-global-variables)
499 (let ((level (gnus-summary-thread-level)))
500 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
501 (zerop (gnus-summary-next-subject 1))
502 (> (gnus-summary-thread-level) level)))))
503 (gnus-summary-position-cursor))
505 (defun gnus-uu-mark-sparse ()
506 "Mark all series that have some articles marked."
508 (gnus-set-global-variables)
509 (let ((marked (nreverse gnus-newsgroup-processable))
510 subject articles total headers)
511 (or marked (error "No articles marked with the process mark"))
512 (setq gnus-newsgroup-processable nil)
515 (and (setq headers (gnus-get-header-by-number (car marked)))
516 (setq subject (header-subject headers)
517 articles (gnus-uu-find-articles-matching
518 (gnus-uu-reginize-string subject))
519 total (nconc total articles)))
521 (gnus-summary-set-process-mark (car articles))
522 (setcdr marked (delq (car articles) (cdr marked)))
523 (setq articles (cdr articles)))
524 (setq marked (cdr marked)))
525 (setq gnus-newsgroup-processable (nreverse total)))
526 (gnus-summary-position-cursor)))
528 (defun gnus-uu-mark-all ()
529 "Mark all articles in \"series\" order."
531 (gnus-set-global-variables)
532 (setq gnus-newsgroup-processable nil)
534 (goto-char (point-min))
536 (while (and (not (eobp))
537 (setq number (gnus-summary-article-number)))
538 (if (not (memq number gnus-newsgroup-processable))
539 (save-excursion (gnus-uu-mark-series)))
541 (gnus-summary-position-cursor))
543 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
545 (defun gnus-uu-decode-postscript (n)
546 "Gets postscript of the current article."
548 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
550 (defun gnus-uu-decode-postscript-view (n)
551 "Gets and views the current article."
553 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
554 (gnus-uu-decode-postscript n)))
556 (defun gnus-uu-decode-postscript-and-save (n dir)
557 "Extracts postscript and saves the current article."
559 (list current-prefix-arg
560 (read-file-name "Where do you want to save the file(s)? "
562 gnus-uu-default-dir t)))
563 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n dir))
566 (defun gnus-uu-decode-postscript-and-save-view (n dir)
567 "Decodes, views and saves the resulting file."
569 (list current-prefix-arg
570 (read-file-name "Where do you want to save the file(s)? "
572 gnus-uu-default-dir t)))
573 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
574 (gnus-uu-decode-postscript-and-save n dir)))
577 ;; Internal functions.
579 (defun gnus-uu-decode-with-method (method n &optional save not-insert)
581 (if save (setq gnus-uu-default-dir save))
582 (let ((articles (gnus-uu-get-list-of-articles n))
584 (setq files (gnus-uu-grab-articles articles method t))
585 (and save (gnus-uu-save-files files save))
586 (setq files (gnus-uu-unpack-files files))
587 (gnus-uu-add-file (mapcar (lambda (file) (cdr (assq 'name file))) files))
588 (setq files (nreverse (gnus-uu-get-actions files)))
589 (or not-insert (gnus-summary-insert-pseudos files))))
591 (defun gnus-uu-save-files (files dir)
592 (let ((len (length files))
595 (setq file (cdr (assq 'name (car files))))
596 (and (file-exists-p file)
598 (setq to-file (if (file-directory-p dir)
599 (concat dir (file-name-nondirectory file))
601 (and (or (not (file-exists-p to-file))
602 (gnus-y-or-n-p (format "%s exists; overwrite? " to-file)))
603 (copy-file file to-file 1 t))))
604 (setq files (cdr files)))
605 (message "Saved %d file%s" len (if (> len 1) "s" ""))))
607 ;; Functions for saving and possibly digesting articles without
610 ;; Function called by gnus-uu-grab-articles to treat each article.
611 (defun gnus-uu-save-article (buffer in-state)
613 (gnus-uu-save-separate-articles
616 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
617 gnus-current-article))
618 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
619 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
621 ((eq in-state 'last) (list 'end))
622 (t (list 'middle)))))
623 ((not gnus-uu-save-in-digest)
626 (write-region 1 (point-max) gnus-uu-saved-article-name t)
627 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
628 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
630 ((eq in-state 'last) (list 'end))
631 (t (list 'middle)))))
633 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
634 beg subj headers headline sorthead body end-string state)
635 (if (or (eq in-state 'first)
636 (eq in-state 'first-and-last))
638 (setq state (list 'begin))
639 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
642 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
645 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
646 (current-time-string) name name))))
647 (if (not (eq in-state 'end))
648 (setq state (list 'middle))))
650 (set-buffer (get-buffer "*gnus-uu-body*"))
651 (goto-char (setq beg (point-max)))
655 (goto-char (point-min))
656 (re-search-forward "\n\n")
657 (setq body (buffer-substring (1- (point)) (point-max)))
658 (narrow-to-region 1 (point))
659 (setq headers gnus-uu-digest-headers)
661 (setq headline (car headers))
662 (setq headers (cdr headers))
663 (goto-char (point-min))
664 (if (re-search-forward headline nil t)
669 (or (and (re-search-forward "^[^ \t]" nil t)
671 (progn (forward-line 1) (point))))))))
673 (insert sorthead)(goto-char (point-max))
674 (insert body)(goto-char (point-max))
675 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
677 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
679 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
681 (set-buffer (get-buffer "*gnus-uu-pre*"))
682 (insert (format " %s\n" subj))))))
683 (if (or (eq in-state 'last)
684 (eq in-state 'first-and-last))
687 (set-buffer (get-buffer "*gnus-uu-pre*"))
688 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
689 (write-region 1 (point-max) gnus-uu-saved-article-name))
691 (set-buffer (get-buffer "*gnus-uu-body*"))
692 (goto-char (point-max))
694 (concat (setq end-string (format "End of %s Digest" name))
696 (insert (concat (make-string (length end-string) ?*) "\n"))
697 (write-region 1 (point-max) gnus-uu-saved-article-name t))
698 (kill-buffer (get-buffer "*gnus-uu-pre*"))
699 (kill-buffer (get-buffer "*gnus-uu-body*"))
700 (setq state (cons 'end state))))
701 (if (memq 'begin state)
702 (cons gnus-uu-saved-article-name state)
705 ;; Binhex treatment - not very advanced.
707 (defconst gnus-uu-binhex-body-line
708 "^[^:]...............................................................$")
709 (defconst gnus-uu-binhex-begin-line
710 "^:...............................................................$")
711 (defconst gnus-uu-binhex-end-line
713 (defvar gnus-uu-binhex-article-name nil)
715 (defun gnus-uu-binhex-article (buffer in-state)
716 (let (state start-char)
720 (goto-char (point-min))
721 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
722 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
723 (setq state (list 'wrong-type))))
725 (if (memq 'wrong-type state)
728 (setq start-char (point))
729 (if (looking-at gnus-uu-binhex-begin-line)
731 (setq state (list 'begin))
732 (write-region 1 1 gnus-uu-binhex-article-name))
733 (setq state (list 'middle)))
734 (goto-char (point-max))
735 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
736 gnus-uu-binhex-end-line) nil t)
737 (if (looking-at gnus-uu-binhex-end-line)
738 (setq state (if (memq 'begin state)
743 (if (file-exists-p gnus-uu-binhex-article-name)
744 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
745 (if (memq 'begin state)
746 (cons gnus-uu-binhex-article-name state)
751 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
752 (let ((state (list 'ok))
753 start-char end-char file-name)
755 (set-buffer process-buffer)
756 (goto-char (point-min))
757 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
758 (setq state (list 'wrong-type))
760 (setq start-char (point))
761 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
762 (setq state (list 'wrong-type))
763 (setq end-char (point))
764 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
765 (insert-buffer-substring process-buffer start-char end-char)
766 (setq file-name (concat gnus-uu-work-dir (cdr gnus-article-current) ".ps"))
767 (write-region (point-min) (point-max) file-name)
768 (setq state (list file-name'begin 'end))
777 (defun gnus-uu-get-actions (files)
781 (setq name (cdr (assq 'name (car files))))
783 (setq action (gnus-uu-get-action name))
784 (setcar files (nconc (list (cons 'action action)
785 (cons 'execute (if (string-match "%" action)
787 (concat action " " name))))
789 (setq files (cdr files)))
792 (defun gnus-uu-get-action (file-name)
795 (gnus-uu-choose-action
798 gnus-uu-user-view-rules
799 (if gnus-uu-ignore-default-view-rules
801 gnus-uu-default-view-rules)
802 gnus-uu-user-view-rules-end)))
803 (if (and (not (string= (or action "") "gnus-uu-archive"))
804 gnus-uu-view-with-metamail)
806 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
807 (setq action (format "metamail -d -b -c \"%s\"" action))))
811 ;; Functions for treating subjects and collecting series.
813 (defun gnus-uu-reginize-string (string)
814 ;; Takes a string and puts a \ in front of every special character;
815 ;; ignores any leading "version numbers" thingies that they use in
816 ;; the comp.binaries groups, and either replaces anything that looks
817 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
818 ;; like that, replaces the last two numbers with "[0-9]+". This, in
819 ;; my experience, should get most postings of a series.
821 (vernum "v[0-9]+[a-z][0-9]+:")
824 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
825 (buffer-disable-undo (current-buffer))
827 (insert (regexp-quote string))
830 (setq case-fold-search nil)
831 (goto-char (point-min))
832 (if (looking-at vernum)
834 (replace-match vernum t t)
835 (setq beg (length vernum))))
838 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
839 (replace-match " [0-9]+/[0-9]+")
842 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
843 (replace-match "[0-9]+ of [0-9]+")
846 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
849 (< 1 (goto-char (1- (point))))))
850 (re-search-forward "[0-9]+" nil t)
851 (replace-match "[0-9]+")
853 (setq count (1- count)))))
856 (while (re-search-forward "[ \t]+" nil t)
857 (replace-match "[ \t]*" t t))
859 (buffer-substring 1 (point-max)))))
861 (defun gnus-uu-get-list-of-articles (n)
862 ;; If N is non-nil, the article numbers of the N next articles
864 ;; If any articles have been marked as processable, they will be
866 ;; Failing that, articles that have subjects that are part of the
867 ;; same "series" as the current will be returned.
868 (let (articles process)
871 (let ((backward (< n 0))
875 (setq articles (cons (gnus-summary-article-number)
877 (gnus-summary-search-forward nil nil backward))
879 (nreverse articles)))
880 (gnus-newsgroup-processable
881 (reverse gnus-newsgroup-processable))
883 (gnus-uu-find-articles-matching)))))
885 (defun gnus-uu-string< (l1 l2)
886 (string< (car l1) (car l2)))
888 (defun gnus-uu-find-articles-matching
889 (&optional subject only-unread do-not-translate)
890 ;; Finds all articles that matches the regexp SUBJECT. If it is
891 ;; nil, the current article name will be used. If ONLY-UNREAD is
892 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
893 ;; non-nil, article names are not equalized before sorting.
894 (let ((subject (or subject
895 (gnus-uu-reginize-string (gnus-summary-subject-string))))
896 beg end list-of-subjects)
900 ;; Collect all subjects matching subject.
901 (let ((case-fold-search t)
903 (goto-char (point-min))
905 (and (setq subj (gnus-summary-subject-string))
906 (string-match subject subj)
907 (or (not only-unread)
908 (= (setq mark (gnus-summary-article-mark))
910 (= mark gnus-ticked-mark)
911 (= mark gnus-dormant-mark))
912 (setq list-of-subjects
913 (cons (cons subj (gnus-summary-article-number))
917 ;; Expand numbers, sort, and return the list of article
919 (mapcar (lambda (sub) (cdr sub))
920 (sort (gnus-uu-expand-numbers
922 (not do-not-translate))
923 'gnus-uu-string<))))))
925 (defun gnus-uu-expand-numbers (string-list &optional translate)
926 ;; Takes a list of strings and "expands" all numbers in all the
927 ;; strings. That is, this function makes all numbers equal length by
928 ;; prepending lots of zeroes before each number. This is to ease later
929 ;; sorting to find out what sequence the articles are supposed to be
930 ;; decoded in. Returns the list of expanded strings.
931 (let ((out-list string-list)
934 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
935 (buffer-disable-undo (current-buffer))
938 (insert (car (car string-list)))
939 ;; Translate multiple spaces to one space.
940 (goto-char (point-min))
941 (while (re-search-forward "[ \t]+" nil t)
943 ;; Translate all characters to "a".
944 (goto-char (point-min))
946 (while (re-search-forward "[A-Za-z]" nil t)
947 (replace-match "a" t t)))
949 (goto-char (point-min))
950 (while (re-search-forward "[0-9]+" nil t)
953 (string-to-int (buffer-substring
954 (match-beginning 0) (match-end 0))))))
955 (setq string (buffer-substring 1 (point-max)))
956 (setcar (car string-list) string)
957 (setq string-list (cdr string-list))))
961 ;; `gnus-uu-grab-articles' is the general multi-article treatment
962 ;; function. It takes a list of articles to be grabbed and a function
963 ;; to apply to each article.
965 ;; The function to be called should take two parameters. The first
966 ;; parameter is the article buffer. The function should leave the
967 ;; result, if any, in this buffer. Most treatment functions will just
970 ;; The second parameter is the state of the list of articles, and can
971 ;; have four values: `first', `middle', `last' and `first-and-last'.
973 ;; The function should return a list. The list may contain the
974 ;; following symbols:
975 ;; `error' if an error occurred
976 ;; `begin' if the beginning of an encoded file has been received
977 ;; If the list returned contains a `begin', the first element of
978 ;; the list *must* be a string with the file name of the decoded
980 ;; `end' if the the end of an encoded file has been received
981 ;; `middle' if the article was a body part of an encoded file
982 ;; `wrong-type' if the article was not a part of an encoded file
983 ;; `ok', which can be used everything is ok
985 (defvar gnus-uu-has-been-grabbed nil)
987 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
989 (if (not (and gnus-uu-has-been-grabbed
990 gnus-uu-unmark-articles-not-decoded))
992 (if dont-unmark-last-article
994 (setq art (car gnus-uu-has-been-grabbed))
995 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
996 (while gnus-uu-has-been-grabbed
997 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
998 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
999 (if dont-unmark-last-article
1000 (setq gnus-uu-has-been-grabbed (list art))))))
1002 ;; This function takes a list of articles and a function to apply to
1003 ;; each article grabbed.
1005 ;; This function returns a list of files decoded if the grabbing and
1006 ;; the process-function has been successful and nil otherwise.
1007 (defun gnus-uu-grab-articles
1008 (articles process-function &optional sloppy limit no-errors)
1009 (let ((state 'first)
1011 has-been-begin has-been-end
1012 article result-file result-files process-state article-buffer
1015 (if (not (gnus-server-opened gnus-current-select-method))
1017 (gnus-start-news-server)
1018 (gnus-request-group gnus-newsgroup-name)))
1020 (setq gnus-uu-has-been-grabbed nil)
1022 (while (and articles
1023 (not (memq 'error process-state))
1025 (not (memq 'end process-state))))
1027 (setq article (car articles))
1028 (setq articles (cdr articles))
1029 (setq gnus-uu-has-been-grabbed (cons article gnus-uu-has-been-grabbed))
1031 (if (> article gnus-uu-highest-article-number)
1032 (setq gnus-uu-highest-article-number article))
1034 (if (eq articles ())
1035 (if (eq state 'first)
1036 (setq state 'first-and-last)
1037 (setq state 'last)))
1039 (message "Getting article %d, %s" article (gnus-uu-part-number article))
1041 (if (not (= (or gnus-current-article 0) article))
1043 (gnus-request-article article gnus-newsgroup-name
1045 (setq gnus-last-article gnus-current-article)
1046 (setq gnus-current-article article)
1047 (setq gnus-article-current (cons gnus-newsgroup-name article))
1048 (if (stringp nntp-server-buffer)
1049 (setq article-buffer nntp-server-buffer)
1050 (setq article-buffer (buffer-name nntp-server-buffer))))
1051 (setq article-buffer gnus-article-buffer))
1053 (buffer-disable-undo article-buffer)
1054 ;; Mark article as read.
1055 (run-hooks 'gnus-mark-article-hook)
1056 (and (memq article gnus-newsgroup-processable)
1057 (gnus-summary-remove-process-mark article))
1059 (setq process-state (funcall process-function article-buffer state))
1061 (if (or (memq 'begin process-state)
1062 (and (or (eq state 'first) (eq state 'first-and-last))
1063 (memq 'ok process-state)))
1066 (if (and result-file (file-exists-p result-file))
1067 (delete-file result-file)))
1068 (if (memq 'begin process-state)
1069 (setq result-file (car process-state)))
1070 (setq begin-article article)
1071 (setq has-been-begin t)
1072 (setq has-been-end nil)))
1074 (if (memq 'end process-state)
1076 (setq gnus-uu-has-been-grabbed nil)
1077 (setq result-files (cons (list (cons 'name result-file)
1078 (cons 'article article))
1080 (setq has-been-end t)
1081 (setq has-been-begin nil)
1082 (and limit (= (length result-files) limit)
1083 (setq articles nil))))
1085 (if (and (or (eq state 'last) (eq state 'first-and-last))
1086 (not (memq 'end process-state)))
1087 (if (and result-file (file-exists-p result-file))
1088 (delete-file result-file)))
1090 (if (not (memq 'wrong-type process-state))
1091 (setq wrong-type nil)
1092 (if gnus-uu-unmark-articles-not-decoded
1093 (gnus-summary-tick-article article t)))
1095 (if sloppy (setq wrong-type nil))
1097 (if (and (not has-been-begin)
1099 (or (memq 'end process-state)
1100 (memq 'middle process-state)))
1102 (setq process-state (list 'error))
1103 (message "No begin part at the beginning")
1105 (setq state 'middle)))
1107 ;; Make sure the last article is put in the article buffer & fix
1110 (if (not (string= article-buffer gnus-article-buffer))
1112 (set-buffer (get-buffer-create gnus-article-buffer))
1113 (let ((buffer-read-only nil))
1116 (insert-buffer-substring article-buffer)
1117 (goto-char (point-min)))))
1121 (if (not has-been-begin)
1122 (if (not no-errors) (message "Wrong type file"))
1123 (if (memq 'error process-state)
1124 (setq result-files nil)
1125 (if (not (or (memq 'ok process-state)
1126 (memq 'end process-state)))
1129 (message "End of articles reached before end of file"))
1130 (setq result-files nil))
1131 (gnus-uu-unmark-list-of-grabbed)))))
1134 (defun gnus-uu-part-number (article)
1135 (let ((subject (header-subject (gnus-get-header-by-number article))))
1136 (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1138 (substring subject (match-beginning 0) (match-end 0))
1141 (defun gnus-uu-uudecode-sentinel (process event)
1142 (delete-process (get-process process)))
1144 (defun gnus-uu-uustrip-article (process-buffer in-state)
1145 ;; Uudecodes a file asynchronously.
1146 (let ((state (list 'ok))
1147 (process-connection-type nil)
1148 start-char pst name-beg name-end)
1150 (set-buffer process-buffer)
1151 (let ((case-fold-search nil)
1152 (buffer-read-only nil))
1154 (goto-char (point-min))
1156 (if gnus-uu-kill-carriage-return
1158 (while (search-forward "\r" nil t)
1159 (delete-backward-char 1))
1160 (goto-char (point-min))))
1162 (if (not (re-search-forward gnus-uu-begin-string nil t))
1163 (if (not (re-search-forward gnus-uu-body-line nil t))
1164 (setq state (list 'wrong-type))))
1166 (if (memq 'wrong-type state)
1169 (setq start-char (point))
1171 (if (looking-at gnus-uu-begin-string)
1173 (setq name-end (match-end 1))
1175 ; Replace any slashes and spaces in file names before decoding
1176 (goto-char (setq name-beg (match-beginning 1)))
1177 (while (re-search-forward "/" name-end t)
1178 (replace-match ","))
1179 (goto-char name-beg)
1180 (while (re-search-forward " " name-end t)
1181 (replace-match "_"))
1182 (goto-char name-beg)
1183 (if (re-search-forward "_*$" name-end t)
1186 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1187 (and gnus-uu-uudecode-process
1188 (setq pst (process-status
1189 (or gnus-uu-uudecode-process "nevair")))
1190 (if (or (eq pst 'stop) (eq pst 'run))
1192 (delete-process gnus-uu-uudecode-process)
1193 (gnus-uu-unmark-list-of-grabbed t))))
1194 (if (get-process "*uudecode*")
1195 (delete-process "*uudecode*"))
1196 (setq gnus-uu-uudecode-process
1199 (get-buffer-create gnus-uu-output-buffer-name)
1201 (format "cd %s ; uudecode" gnus-uu-work-dir)))
1202 (set-process-sentinel
1203 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1204 (setq state (list 'begin))
1205 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1206 (setq state (list 'middle)))
1208 (goto-char (point-max))
1211 (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1214 (if (looking-at gnus-uu-end-string)
1215 (setq state (cons 'end state)))
1218 (and gnus-uu-uudecode-process
1219 (setq pst (process-status
1220 (or gnus-uu-uudecode-process "nevair")))
1221 (if (or (eq pst 'run) (eq pst 'stop))
1223 (if gnus-uu-correct-stripped-uucode
1225 (gnus-uu-check-correct-stripped-uucode
1227 (goto-char (point-max))
1229 (concat gnus-uu-body-line "\\|"
1235 (process-send-region gnus-uu-uudecode-process
1239 (delete-process gnus-uu-uudecode-process)
1240 (message "gnus-uu: Couldn't uudecode")
1242 (setq state (list 'wrong-type)))))
1244 (if (memq 'end state)
1245 (accept-process-output gnus-uu-uudecode-process)))
1246 (setq state (list 'wrong-type))))
1247 (if (not gnus-uu-uudecode-process)
1248 (setq state (list 'wrong-type)))))
1250 (if (memq 'begin state)
1251 (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1254 ;; This function is used by `gnus-uu-grab-articles' to treat
1255 ;; a shared article.
1256 (defun gnus-uu-unshar-article (process-buffer in-state)
1257 (let ((state (list 'ok))
1260 (set-buffer process-buffer)
1261 (goto-char (point-min))
1262 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1263 (setq state (list 'wrong-type))
1265 (setq start-char (point))
1266 (call-process-region
1267 start-char (point-max) "sh" nil
1268 (get-buffer-create gnus-uu-output-buffer-name) nil
1269 "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1272 ;; Returns the name of what the shar file is going to unpack.
1273 (defun gnus-uu-find-name-in-shar ()
1274 (let ((oldpoint (point))
1276 (goto-char (point-min))
1277 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1278 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1279 (goto-char oldpoint)
1282 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1283 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1284 ;; found, or the name of the command to run if such a rule is found.
1285 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1286 (let ((action-list (copy-sequence file-action-list))
1291 (and gnus-uu-ignore-files-by-name
1292 (string-match gnus-uu-ignore-files-by-name file-name)))
1294 (and gnus-uu-ignore-files-by-type
1295 (string-match gnus-uu-ignore-files-by-type
1296 (or (gnus-uu-choose-action
1297 file-name gnus-uu-ext-to-mime-list t)
1299 (while (not (or (eq action-list ()) action))
1300 (setq rule (car action-list))
1301 (setq action-list (cdr action-list))
1302 (if (string-match (car rule) file-name)
1303 (setq action (car (cdr rule))))))
1306 (defun gnus-uu-treat-archive (file-path)
1307 ;; Unpacks an archive. Returns t if unpacking is successful.
1308 (let ((did-unpack t)
1309 action command files file file-name dir)
1310 (setq action (gnus-uu-choose-action
1311 file-path (append gnus-uu-user-archive-rules
1312 (if gnus-uu-ignore-default-archive-rules
1314 gnus-uu-default-archive-rules))))
1316 (if (not action) (error "No unpackers for the file %s" file-path))
1318 (string-match "/[^/]*$" file-path)
1319 (setq file-name (substring file-path (1+ (match-beginning 0))))
1320 (setq dir (substring file-path 0 (match-beginning 0)))
1322 (if (member action gnus-uu-destructive-archivers)
1323 (copy-file file-path (concat file-path "~") t))
1325 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1328 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1331 (message "Unpacking: %s..." (gnus-uu-command action file-path))
1333 (if (= 0 (call-process "sh" nil
1334 (get-buffer-create gnus-uu-output-buffer-name)
1337 (message "Error during unpacking of archive")
1338 (setq did-unpack nil))
1340 (if (member action gnus-uu-destructive-archivers)
1341 (rename-file (concat file-path "~") file-path t))
1345 (defun gnus-uu-dir-files (dir)
1346 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1349 (if (file-directory-p (setq file (car dirs)))
1350 (setq files (append files (gnus-uu-dir-files file)))
1351 (setq files (cons file files)))
1352 (setq dirs (cdr dirs)))
1355 (defun gnus-uu-unpack-files (files &optional ignore)
1356 ;; Go through FILES and look for files to unpack.
1357 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1361 (setq file (cdr (assq 'name (car files))))
1362 (if (and (not (member file ignore))
1363 (equal (gnus-uu-get-action (file-name-nondirectory file))
1366 (setq did-unpack (cons file did-unpack))
1367 (or (gnus-uu-treat-archive file)
1368 (message "Error during unpacking of %s" file))
1369 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1372 (or (member (car nfiles) totfiles)
1373 (setq ofiles (cons (list (cons 'name (car nfiles))
1374 (cons 'original file))
1376 (setq nfiles (cdr nfiles)))
1377 (setq totfiles newfiles))))
1378 (setq files (cdr files)))
1380 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1383 (defun gnus-uu-ls-r (dir)
1384 (let* ((files (gnus-uu-directory-files dir t))
1387 (if (file-directory-p (car files))
1389 (setq ofiles (delete (car files) ofiles))
1390 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1391 (setq files (cdr files)))
1396 (defun gnus-uu-directory-files (dir &optional full)
1397 (let (files out file)
1398 (setq files (directory-files dir full))
1400 (setq file (car files))
1401 (setq files (cdr files))
1402 (or (string-match "/\\.\\.?$" file)
1403 (setq out (cons file out))))
1404 (setq out (nreverse out))
1407 (defun gnus-uu-check-correct-stripped-uucode (start end)
1408 (let (found beg length short)
1409 (if (not gnus-uu-correct-stripped-uucode)
1413 (if (re-search-forward " \\|`" end t)
1418 (if (looking-at "\n") (replace-match ""))
1422 (if (looking-at (concat gnus-uu-begin-string "\\|"
1423 gnus-uu-end-string))
1430 (setq length (- (point) beg))))
1435 (if (not (= length (- (point) beg)))
1436 (insert (make-string (- length (- (point) beg)) ? ))))
1437 (forward-line 1))))))
1439 (defun gnus-uu-initialize ()
1440 (setq gnus-uu-highest-article-number 1)
1441 (gnus-uu-check-for-generated-files)
1442 (setq gnus-uu-tmp-dir (expand-file-name gnus-uu-tmp-dir))
1443 (if (string-match "[^/]$" gnus-uu-tmp-dir)
1444 (setq gnus-uu-tmp-dir (concat gnus-uu-tmp-dir "/")))
1445 (if (not (file-directory-p gnus-uu-tmp-dir))
1446 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1447 (if (not (file-writable-p gnus-uu-tmp-dir))
1448 (error "Temp directory %s can't be written to" gnus-uu-tmp-dir)))
1449 (setq gnus-uu-work-dir
1450 (concat gnus-uu-tmp-dir (make-temp-name "gnus")))
1451 (gnus-uu-add-file gnus-uu-work-dir)
1452 (if (not (file-directory-p gnus-uu-work-dir))
1453 (make-directory gnus-uu-work-dir))
1454 (set-file-modes gnus-uu-work-dir 448)
1455 (setq gnus-uu-work-dir (concat gnus-uu-work-dir "/")))
1457 ;; Kills the temporary uu buffers, kills any processes, etc.
1458 (defun gnus-uu-clean-up ()
1460 (and gnus-uu-uudecode-process
1461 (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1462 (if (or (eq pst 'stop) (eq pst 'run))
1463 (delete-process gnus-uu-uudecode-process)))
1464 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1465 (kill-buffer buf))))
1467 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1468 ;; hasn't been deleted, if, for instance, the user terminated decoding
1470 (defun gnus-uu-check-for-generated-files ()
1472 (while gnus-uu-generated-file-list
1473 (setq file (car gnus-uu-generated-file-list))
1474 (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1475 (if (not (string-match "/\\.[\\.]?$" file))
1477 (if (file-directory-p file)
1478 (setq dirs (cons file dirs))
1479 (if (file-exists-p file)
1480 (delete-file file))))))
1481 (setq dirs (nreverse dirs))
1483 (setq file (car dirs))
1484 (setq dirs (cdr dirs))
1485 (if (file-directory-p file)
1486 (if (string-match "/$" file)
1487 (delete-directory (substring file 0 (match-beginning 0)))
1488 (delete-directory file))))))
1490 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1491 ;; still exists upon exiting the newsgroup).
1492 (defun gnus-uu-add-file (file)
1494 (setq gnus-uu-generated-file-list
1495 (cons file gnus-uu-generated-file-list))
1496 (setq gnus-uu-generated-file-list
1497 (append file gnus-uu-generated-file-list))))
1499 ;; Inputs an action and a file and returns a full command, putting
1500 ;; ticks round the file name and escaping any ticks in the file name.
1501 (defun gnus-uu-command (action file)
1503 (while (string-match "`\\|\"\\|\\$\\|\\\\" file)
1506 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1507 (substring file (match-beginning 0) (match-end 0))))
1508 (setq file (substring file (1+ (match-beginning 0))))))
1509 (setq ofile (concat "\"" ofile file "\""))
1510 (if (string-match "%s" action)
1511 (format action ofile)
1512 (concat action " " ofile))))
1517 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1518 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1523 ;;; uuencoded posting
1529 ;; Any function that is to be used as and encoding method will take two
1530 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1531 ;; and "spiral.jpg", respectively.) The function should return nil if
1532 ;; the encoding wasn't successful.
1533 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1534 "Function used for encoding binary files.
1535 There are three functions supplied with gnus-uu for encoding files:
1536 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1537 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1538 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1539 uuencode and adds MIME headers.")
1541 (defvar gnus-uu-post-include-before-composing nil
1542 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1543 If this variable is t, you can either include an encoded file with
1544 \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1546 (defvar gnus-uu-post-length 990
1547 "Maximum length of an article.
1548 The encoded file will be split into how many articles it takes to
1549 post the entire file.")
1551 (defvar gnus-uu-post-threaded nil
1552 "Non-nil means that gnus-uu will post the encoded file in a thread.
1553 This may not be smart, as no other decoder I have seen are able to
1554 follow threads when collecting uuencoded articles. (Well, I have seen
1555 one package that does that - gnus-uu, but somehow, I don't think that
1556 counts...) Default is nil.")
1558 (defvar gnus-uu-post-separate-description t
1559 "Non-nil means that the description will be posted in a separate article.
1560 The first article will typically be numbered (0/x). If this variable
1561 is nil, the description the user enters will be included at the
1562 beginning of the first article, which will be numbered (1/x). Default
1565 (defconst gnus-uu-post-binary-separator "--binary follows this line--")
1566 (defvar gnus-uu-post-message-id nil)
1567 (defvar gnus-uu-post-inserted-file-name nil)
1568 (defvar gnus-uu-winconf-post-news nil)
1570 (defun gnus-uu-post-news ()
1571 "Compose an article and post an encoded file."
1573 (setq gnus-uu-post-inserted-file-name nil)
1574 (setq gnus-uu-winconf-post-news (current-window-configuration))
1576 (gnus-summary-post-news)
1578 (use-local-map (copy-keymap (current-local-map)))
1579 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1580 (local-set-key "\C-c\C-f\C-a" 'gnus-uu-post-reply-summary)
1581 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1582 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1583 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1585 (if gnus-uu-post-include-before-composing
1586 (save-excursion (setq gnus-uu-post-inserted-file-name
1587 (gnus-uu-post-insert-binary)))))
1589 (defun gnus-uu-post-insert-binary-in-article ()
1590 "Inserts an encoded file in the buffer.
1591 The user will be asked for a file name."
1593 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1594 (error "Not in post-news buffer"))
1596 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1598 ;; Encodes with uuencode and substitutes all spaces with backticks.
1599 (defun gnus-uu-post-encode-uuencode (path file-name)
1600 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1602 (goto-char (point-min))
1604 (while (re-search-forward " " nil t)
1605 (replace-match "`"))
1608 ;; Encodes with uuencode and adds MIME headers.
1609 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1610 (if (gnus-uu-post-encode-uuencode path file-name)
1612 (gnus-uu-post-make-mime file-name "x-uue")
1615 ;; Encodes with base64 and adds MIME headers
1616 (defun gnus-uu-post-encode-mime (path file-name)
1617 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1619 (gnus-uu-post-make-mime file-name "base64")
1622 ;; Adds MIME headers.
1623 (defun gnus-uu-post-make-mime (file-name encoding)
1624 (goto-char (point-min))
1625 (insert (format "Content-Type: %s; name=\"%s\"\n"
1626 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1628 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1630 (set-buffer gnus-post-news-buffer)
1631 (goto-char (point-min))
1632 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1634 (narrow-to-region 1 (point))
1635 (or (mail-fetch-field "mime-version")
1638 (insert "MIME-Version: 1.0\n")))
1641 ;; Encodes a file PATH with COMMAND, leaving the result in the
1643 (defun gnus-uu-post-encode-file (command path file-name)
1644 (= 0 (call-process "sh" nil t nil "-c"
1645 (format "%s %s %s" command path file-name))))
1647 (defun gnus-uu-post-news-inews ()
1648 "Posts the composed news article and encoded file.
1649 If no file has been included, the user will be asked for a file."
1651 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1652 (error "Not in post news buffer"))
1656 (if gnus-uu-post-inserted-file-name
1657 (setq file-name gnus-uu-post-inserted-file-name)
1658 (setq file-name (gnus-uu-post-insert-binary)))
1660 (if gnus-uu-post-threaded
1661 (let ((gnus-required-headers
1662 (if (memq 'Message-ID gnus-required-headers)
1663 gnus-required-headers
1664 (cons 'Message-ID gnus-required-headers)))
1665 gnus-inews-article-hook elem)
1667 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1668 gnus-inews-article-hook
1669 (list gnus-inews-article-hook)))
1670 (setq gnus-inews-article-hook
1674 (goto-char (point-min))
1675 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1676 (setq gnus-uu-post-message-id
1678 (match-beginning 1) (match-end 1)))
1679 (setq gnus-uu-post-message-id nil))))
1680 gnus-inews-article-hook))
1681 (gnus-uu-post-encoded file-name t))
1682 (gnus-uu-post-encoded file-name nil)))
1683 (setq gnus-uu-post-inserted-file-name nil)
1684 (and gnus-uu-winconf-post-news
1685 (set-window-configuration gnus-uu-winconf-post-news)))
1687 ;; Asks for a file to encode, encodes it and inserts the result in
1688 ;; the current buffer. Returns the file name the user gave.
1689 (defun gnus-uu-post-insert-binary ()
1690 (let ((uuencode-buffer-name "*uuencode buffer*")
1691 file-path post-buf uubuf file-name)
1693 (setq file-path (read-file-name
1694 "What file do you want to encode? "))
1695 (if (not (file-exists-p file-path))
1696 (error "%s: No such file" file-path))
1698 (goto-char (point-max))
1699 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1701 (if (string-match "^~/" file-path)
1702 (setq file-path (concat "$HOME" (substring file-path 1))))
1703 (if (string-match "/[^/]*$" file-path)
1704 (setq file-name (substring file-path (1+ (match-beginning 0))))
1705 (setq file-name file-path))
1709 (set-buffer (setq uubuf
1710 (get-buffer-create uuencode-buffer-name)))
1712 (funcall gnus-uu-post-encode-method file-path file-name))
1713 (insert-buffer uubuf)
1714 (error "Encoding unsuccessful"))
1715 (kill-buffer uubuf))
1718 ;; Posts the article and all of the encoded file.
1719 (defun gnus-uu-post-encoded (file-name &optional threaded)
1720 (let ((send-buffer-name "*uuencode send buffer*")
1721 (encoded-buffer-name "*encoded buffer*")
1722 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1723 (separator (concat mail-header-separator "\n\n"))
1724 file uubuf length parts header i end beg
1725 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1727 (setq post-buf (current-buffer))
1729 (goto-char (point-min))
1730 (if (not (re-search-forward
1731 (if gnus-uu-post-separate-description
1732 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1734 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1735 (error "Internal error: No binary/header separator"))
1738 (setq beg-binary (point))
1739 (setq end-binary (point-max))
1742 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1744 (insert-buffer-substring post-buf beg-binary end-binary)
1745 (goto-char (point-min))
1746 (setq length (count-lines 1 (point-max)))
1747 (setq parts (/ length gnus-uu-post-length))
1748 (if (not (< (% length gnus-uu-post-length) 4))
1749 (setq parts (1+ parts))))
1751 (if gnus-uu-post-separate-description
1753 (kill-region (point) (point-max))
1755 (goto-char (point-min))
1757 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1759 (setq header (buffer-substring 1 (point)))
1761 (goto-char (point-min))
1762 (if (not gnus-uu-post-separate-description)
1764 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1767 (insert (format " (0/%d)" parts))))
1773 (while (not (> i parts))
1774 (set-buffer (get-buffer-create send-buffer-name))
1777 (if (and threaded gnus-uu-post-message-id)
1778 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1781 (- 62 (length (format top-string "" file-name i parts ""))))
1782 (if (> 1 (setq minlen (/ whole-len 2)))
1787 (make-string minlen ?-)
1790 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1792 (goto-char (point-min))
1793 (if (not (re-search-forward "^Subject: " nil t))
1798 (insert (format " (%d/%d)" i parts)))
1799 (if (or (and (= i 2) gnus-uu-post-separate-description)
1800 (and (= i 1) (not gnus-uu-post-separate-description)))
1801 (replace-match "Subject: Re: "))))
1803 (goto-char (point-max))
1808 (goto-char (point-max))
1809 (forward-line gnus-uu-post-length))
1810 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1813 (insert-buffer-substring uubuf beg end)
1818 (goto-char (point-min))
1820 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1823 (if (re-search-forward
1824 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1833 (and (setq buf (get-buffer send-buffer-name))
1835 (and (setq buf (get-buffer encoded-buffer-name))
1838 (if (not gnus-uu-post-separate-description)
1840 (set-buffer-modified-p nil)
1841 (and (fboundp 'bury-buffer) (bury-buffer))))))
1845 ;; gnus-uu.el ends here