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.
30 (require 'gnus-message)
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-save-separate-articles nil
225 "*Non-nil means that gnus-uu will save articles in separate files.")
227 ;; Internal variables
229 (defvar gnus-uu-saved-article-name nil)
231 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
232 (defconst gnus-uu-end-string "^end[ \t]*$")
234 (defconst gnus-uu-body-line "^M")
236 (while (> (setq i (1- i)) 0)
237 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
238 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
240 ;"^M.............................................................?$"
242 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
244 (defvar gnus-uu-shar-file-name nil)
245 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
247 (defconst gnus-uu-postscript-begin-string "^%!PS-")
248 (defconst gnus-uu-postscript-end-string "^%%EOF$")
250 (defvar gnus-uu-file-name nil)
251 (defconst gnus-uu-uudecode-process nil)
253 (defvar gnus-uu-generated-file-list nil)
254 (defvar gnus-uu-work-dir nil)
256 (defconst gnus-uu-output-buffer-name "*Gnus UU Output*")
258 (defconst gnus-uu-highest-article-number 1)
259 (defvar gnus-uu-default-dir default-directory)
263 (defun gnus-uu-decode-uu (n)
264 "Uudecodes the current article."
266 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
268 (defun gnus-uu-decode-uu-and-save (n dir)
269 "Decodes and saves the resulting file."
271 (list current-prefix-arg
272 (read-file-name "Where do you want to save the file(s)? "
274 gnus-uu-default-dir t)))
275 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir))
277 (defun gnus-uu-decode-unshar (n)
278 "Unshars the current article."
280 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n))
282 (defun gnus-uu-decode-unshar-and-save (n dir)
283 "Unshars and saves the current article."
285 (list current-prefix-arg
286 (read-file-name "Where do you want to save the file(s)? "
288 gnus-uu-default-dir t)))
289 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir))
291 (defun gnus-uu-decode-save (n file)
292 "Saves the current article."
294 (list current-prefix-arg
295 (read-file-name "Where do you want to save the file? "
297 gnus-uu-default-dir)))
298 (setq gnus-uu-saved-article-name file)
299 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t)
300 (setq gnus-uu-generated-file-list
301 (delete file gnus-uu-generated-file-list)))
303 (defun gnus-uu-decode-binhex (n dir)
304 "Unbinhexes the current article."
306 (list current-prefix-arg
307 (read-file-name "Where do you want to save the file(s)? "
309 gnus-uu-default-dir t)))
310 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
312 (defun gnus-uu-decode-uu-view (n)
313 "Uudecodes and views the current article."
315 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
316 (gnus-uu-decode-uu n)))
318 (defun gnus-uu-decode-uu-and-save-view (n dir)
319 "Decodes, views and saves the resulting file."
321 (list current-prefix-arg
322 (read-file-name "Where do you want to save the file(s)? "
324 gnus-uu-default-dir t)))
325 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
326 (gnus-uu-decode-uu-and-save n dir)))
328 (defun gnus-uu-decode-unshar-view (n)
329 "Unshars and views the current article."
331 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
332 (gnus-uu-decode-unshar n)))
334 (defun gnus-uu-decode-unshar-and-save-view (n dir)
335 "Unshars and saves the current article."
337 (list current-prefix-arg
338 (read-file-name "Where do you want to save the file(s)? "
340 gnus-uu-default-dir t)))
341 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
342 (gnus-uu-decode-unshar-and-save n dir)))
344 (defun gnus-uu-decode-save-view (n file)
345 "Saves and views the current article."
347 (list current-prefix-arg
348 (read-file-name "Where do you want to save the file? "
349 gnus-uu-default-dir gnus-uu-default-dir)))
350 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
351 (gnus-uu-decode-save n file)))
353 (defun gnus-uu-decode-binhex-view (n file)
354 "Unbinhexes and views the current article."
356 (list current-prefix-arg
357 (read-file-name "Where do you want to save the file(s)? "
358 gnus-uu-default-dir gnus-uu-default-dir)))
359 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
360 (gnus-uu-decode-binhex n file)))
363 ;; Digest and forward articles
365 (defun gnus-uu-digest-mail-forward (n &optional post)
366 "Digests and forwards all articles in this series."
369 (let ((gnus-uu-save-in-digest t)
370 (file (concat gnus-uu-work-dir (make-temp-name "forward")))
372 (setq gnus-winconf-post-news (current-window-configuration))
373 (gnus-uu-decode-save n file)
374 (gnus-uu-add-file file)
375 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
376 (gnus-add-current-to-buffer-list)
378 (delete-other-windows)
380 (goto-char (point-min))
381 (and (re-search-forward "^Subject: ")
383 (delete-region (point) (gnus-point-at-eol))
384 (insert "Digested Articles")))
385 (goto-char (point-min))
386 (and (re-search-forward "^From: ")
388 (delete-region (point) (gnus-point-at-eol))
391 (gnus-forward-using-post)
392 (funcall gnus-mail-forward-method))
395 (defun gnus-uu-digest-post-forward (n)
396 "Digest and forward to a newsgroup."
398 (gnus-uu-digest-mail-forward n t))
402 (defun gnus-uu-mark-by-regexp (regexp)
403 "Ask for a regular expression and set the process mark on all articles that match."
404 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
405 (gnus-set-global-variables)
406 (let ((articles (gnus-uu-find-articles-matching regexp)))
408 (gnus-summary-set-process-mark (car articles))
409 (setq articles (cdr articles)))
411 (gnus-summary-position-cursor))
413 (defun gnus-uu-mark-series ()
414 "Mark the current series with the process mark."
416 (gnus-set-global-variables)
417 (let ((articles (gnus-uu-find-articles-matching)))
419 (gnus-summary-set-process-mark (car articles))
420 (setq articles (cdr articles)))
422 (gnus-summary-position-cursor))
424 (defun gnus-uu-mark-region (beg end)
425 "Marks all articles between point and mark."
427 (gnus-set-global-variables)
430 (while (< (point) end)
431 (gnus-summary-set-process-mark (gnus-summary-article-number))
433 (gnus-summary-position-cursor))
435 (defun gnus-uu-mark-thread ()
436 "Marks all articles downwards in this thread."
438 (gnus-set-global-variables)
440 (let ((level (gnus-summary-thread-level)))
441 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
442 (zerop (gnus-summary-next-subject 1))
443 (> (gnus-summary-thread-level) level)))))
444 (gnus-summary-position-cursor))
446 (defun gnus-uu-mark-sparse ()
447 "Mark all series that have some articles marked."
449 (gnus-set-global-variables)
450 (let ((marked (nreverse gnus-newsgroup-processable))
451 subject articles total headers)
452 (or marked (error "No articles marked with the process mark"))
453 (setq gnus-newsgroup-processable nil)
456 (and (setq headers (gnus-get-header-by-number (car marked)))
457 (setq subject (header-subject headers)
458 articles (gnus-uu-find-articles-matching
459 (gnus-uu-reginize-string subject))
460 total (nconc total articles)))
462 (gnus-summary-set-process-mark (car articles))
463 (setcdr marked (delq (car articles) (cdr marked)))
464 (setq articles (cdr articles)))
465 (setq marked (cdr marked)))
466 (setq gnus-newsgroup-processable (nreverse total)))
467 (gnus-summary-position-cursor)))
469 (defun gnus-uu-mark-all ()
470 "Mark all articles in \"series\" order."
472 (gnus-set-global-variables)
473 (setq gnus-newsgroup-processable nil)
475 (goto-char (point-min))
477 (while (and (not (eobp))
478 (setq number (gnus-summary-article-number)))
479 (if (not (memq number gnus-newsgroup-processable))
480 (save-excursion (gnus-uu-mark-series)))
482 (gnus-summary-position-cursor))
484 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
486 (defun gnus-uu-decode-postscript (n)
487 "Gets postscript of the current article."
489 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
491 (defun gnus-uu-decode-postscript-view (n)
492 "Gets and views the current article."
494 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
495 (gnus-uu-decode-postscript n)))
497 (defun gnus-uu-decode-postscript-and-save (n dir)
498 "Extracts postscript and saves the current article."
500 (list current-prefix-arg
501 (read-file-name "Where do you want to save the file(s)? "
503 gnus-uu-default-dir t)))
504 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n dir))
507 (defun gnus-uu-decode-postscript-and-save-view (n dir)
508 "Decodes, views and saves the resulting file."
510 (list current-prefix-arg
511 (read-file-name "Where do you want to save the file(s)? "
513 gnus-uu-default-dir t)))
514 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
515 (gnus-uu-decode-postscript-and-save n dir)))
518 ;; Internal functions.
520 (defun gnus-uu-decode-with-method (method n &optional save not-insert)
522 (if save (setq gnus-uu-default-dir save))
523 (let ((articles (gnus-uu-get-list-of-articles n))
525 (setq files (gnus-uu-grab-articles articles method t))
526 (and save (gnus-uu-save-files files save))
527 (setq files (gnus-uu-unpack-files files))
528 (gnus-uu-add-file (mapcar (lambda (file) (cdr (assq 'name file))) files))
529 (setq files (nreverse (gnus-uu-get-actions files)))
530 (or not-insert (gnus-summary-insert-pseudos files))))
532 (defun gnus-uu-save-files (files dir)
533 (let ((len (length files))
536 (setq file (cdr (assq 'name (car files))))
537 (and (file-exists-p file)
539 (setq to-file (if (file-directory-p dir)
540 (concat dir (file-name-nondirectory file))
542 (and (or (not (file-exists-p to-file))
543 (gnus-y-or-n-p (format "%s exists; overwrite? " to-file)))
544 (copy-file file to-file 1 t))))
545 (setq files (cdr files)))
546 (message "Saved %d file%s" len (if (> len 1) "s" ""))))
548 ;; Functions for saving and possibly digesting articles without
551 ;; Function called by gnus-uu-grab-articles to treat each article.
552 (defun gnus-uu-save-article (buffer in-state)
554 (gnus-uu-save-separate-articles
557 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
558 gnus-current-article))
559 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
560 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
562 ((eq in-state 'last) (list 'end))
563 (t (list 'middle)))))
564 ((not gnus-uu-save-in-digest)
567 (write-region 1 (point-max) gnus-uu-saved-article-name t)
568 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
569 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
571 ((eq in-state 'last) (list 'end))
572 (t (list 'middle)))))
574 (let (beg subj name headers headline sorthead body end-string state)
575 (string-match "/\\([^/]*\\)$" gnus-uu-saved-article-name)
576 (setq name (substring gnus-uu-saved-article-name (match-beginning 1)
578 (if (or (eq in-state 'first)
579 (eq in-state 'first-and-last))
581 (setq state (list 'begin))
582 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
585 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
588 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
589 (current-time-string) name name))))
590 (if (not (eq in-state 'end))
591 (setq state (list 'middle))))
593 (set-buffer (get-buffer "*gnus-uu-body*"))
594 (goto-char (setq beg (point-max)))
599 (re-search-forward "\n\n")
600 (setq body (buffer-substring (1- (point)) (point-max)))
601 (narrow-to-region 1 (point))
602 (setq headers (list "Date:" "From:" "To:" "Cc:" "Subject:"
603 "Message-ID:" "Keywords:" "Summary:"))
605 (setq headline (car headers))
606 (setq headers (cdr headers))
608 (if (re-search-forward (concat "^" headline ".*$") nil t)
610 (concat sorthead (buffer-substring
612 (match-end 0)) "\n"))))
614 (insert sorthead)(goto-char (point-max))
615 (insert body)(goto-char (point-max))
616 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
618 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
620 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
622 (set-buffer (get-buffer "*gnus-uu-pre*"))
623 (insert (format " %s\n" subj))))))
624 (if (or (eq in-state 'last)
625 (eq in-state 'first-and-last))
628 (set-buffer (get-buffer "*gnus-uu-pre*"))
629 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
630 (write-region 1 (point-max) gnus-uu-saved-article-name))
632 (set-buffer (get-buffer "*gnus-uu-body*"))
633 (goto-char (point-max))
635 (concat (setq end-string (format "End of %s Digest" name))
637 (insert (concat (make-string (length end-string) ?*) "\n"))
638 (write-region 1 (point-max) gnus-uu-saved-article-name t))
639 (kill-buffer (get-buffer "*gnus-uu-pre*"))
640 (kill-buffer (get-buffer "*gnus-uu-body*"))
641 (setq state (cons 'end state))))
642 (if (memq 'begin state)
643 (cons gnus-uu-saved-article-name state)
646 ;; Binhex treatment - not very advanced.
648 (defconst gnus-uu-binhex-body-line
649 "^[^:]...............................................................$")
650 (defconst gnus-uu-binhex-begin-line
651 "^:...............................................................$")
652 (defconst gnus-uu-binhex-end-line
654 (defvar gnus-uu-binhex-article-name nil)
656 (defun gnus-uu-binhex-article (buffer in-state)
657 (let (state start-char)
662 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
663 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
664 (setq state (list 'wrong-type))))
666 (if (memq 'wrong-type state)
669 (setq start-char (point))
670 (if (looking-at gnus-uu-binhex-begin-line)
672 (setq state (list 'begin))
673 (write-region 1 1 gnus-uu-binhex-article-name))
674 (setq state (list 'middle)))
675 (goto-char (point-max))
676 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
677 gnus-uu-binhex-end-line) nil t)
678 (if (looking-at gnus-uu-binhex-end-line)
679 (setq state (if (memq 'begin state)
684 (if (file-exists-p gnus-uu-binhex-article-name)
685 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
686 (if (memq 'begin state)
687 (cons gnus-uu-binhex-article-name state)
692 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
693 (let ((state (list 'ok))
694 start-char end-char file-name)
696 (set-buffer process-buffer)
698 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
699 (setq state (list 'wrong-type))
701 (setq start-char (point))
702 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
703 (setq state (list 'wrong-type))
704 (setq end-char (point))
705 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
706 (insert-buffer-substring process-buffer start-char end-char)
707 (setq file-name (concat gnus-uu-work-dir (cdr gnus-article-current) ".ps"))
708 (write-region (point-min) (point-max) file-name)
709 (setq state (list file-name'begin 'end))
718 (defun gnus-uu-get-actions (files)
722 (setq name (cdr (assq 'name (car files))))
724 (setq action (gnus-uu-get-action name))
725 (setcar files (nconc (list (cons 'action action)
726 (cons 'execute (if (string-match "%" action)
728 (concat action " " name))))
730 (setq files (cdr files)))
733 (defun gnus-uu-get-action (file-name)
736 (gnus-uu-choose-action
739 gnus-uu-user-view-rules
740 (if gnus-uu-ignore-default-view-rules
742 gnus-uu-default-view-rules)
743 gnus-uu-user-view-rules-end)))
744 (if (and (not (string= (or action "") "gnus-uu-archive"))
745 gnus-uu-view-with-metamail)
747 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
748 (setq action (format "metamail -d -b -c \"%s\"" action))))
752 ;; Functions for treating subjects and collecting series.
754 (defun gnus-uu-reginize-string (string)
755 ;; Takes a string and puts a \ in front of every special character;
756 ;; ignores any leading "version numbers" thingies that they use in
757 ;; the comp.binaries groups, and either replaces anything that looks
758 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
759 ;; like that, replaces the last two numbers with "[0-9]+". This, in
760 ;; my experience, should get most postings of a series.
762 (vernum "v[0-9]+[a-z][0-9]+:")
765 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
766 (buffer-disable-undo (current-buffer))
768 (insert (regexp-quote string))
771 (setq case-fold-search nil)
773 (if (looking-at vernum)
775 (replace-match vernum t t)
776 (setq beg (length vernum))))
779 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
780 (replace-match " [0-9]+/[0-9]+")
783 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
784 (replace-match "[0-9]+ of [0-9]+")
787 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
790 (< 1 (goto-char (1- (point))))))
791 (re-search-forward "[0-9]+" nil t)
792 (replace-match "[0-9]+")
794 (setq count (1- count)))))
797 (while (re-search-forward "[ \t]+" nil t)
798 (replace-match "[ \t]*" t t))
800 (buffer-substring 1 (point-max)))))
802 (defun gnus-uu-get-list-of-articles (n)
803 ;; If N is non-nil, the article numbers of the N next articles
805 ;; If any articles have been marked as processable, they will be
807 ;; Failing that, articles that have subjects that are part of the
808 ;; same "series" as the current will be returned.
809 (let (articles process)
812 (let ((backward (< n 0))
816 (setq articles (cons (gnus-summary-article-number)
818 (gnus-summary-search-forward nil nil backward))
820 (nreverse articles)))
821 (gnus-newsgroup-processable
822 (reverse gnus-newsgroup-processable))
824 (gnus-uu-find-articles-matching)))))
826 (defun gnus-uu-string< (l1 l2)
827 (string< (car l1) (car l2)))
829 (defun gnus-uu-find-articles-matching
830 (&optional subject only-unread do-not-translate)
831 ;; Finds all articles that matches the regexp SUBJECT. If it is
832 ;; nil, the current article name will be used. If ONLY-UNREAD is
833 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
834 ;; non-nil, article names are not equalized before sorting.
835 (let ((subject (or subject
836 (gnus-uu-reginize-string (gnus-summary-subject-string))))
837 beg end list-of-subjects)
841 ;; Collect all subjects matching subject.
842 (let ((case-fold-search t)
844 (goto-char (point-min))
846 (and (setq subj (gnus-summary-subject-string))
847 (string-match subject subj)
848 (or (not only-unread)
849 (= (setq mark (gnus-summary-article-mark))
851 (= mark gnus-ticked-mark)
852 (= mark gnus-dormant-mark))
853 (setq list-of-subjects
854 (cons (cons subj (gnus-summary-article-number))
858 ;; Expand numbers, sort, and return the list of article
860 (mapcar (lambda (sub) (cdr sub))
861 (sort (gnus-uu-expand-numbers
863 (not do-not-translate))
864 'gnus-uu-string<))))))
866 (defun gnus-uu-expand-numbers (string-list &optional translate)
867 ;; Takes a list of strings and "expands" all numbers in all the
868 ;; strings. That is, this function makes all numbers equal length by
869 ;; prepending lots of zeroes before each number. This is to ease later
870 ;; sorting to find out what sequence the articles are supposed to be
871 ;; decoded in. Returns the list of expanded strings.
872 (let ((out-list string-list)
875 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
876 (buffer-disable-undo (current-buffer))
879 (insert (car (car string-list)))
880 ;; Translate multiple spaces to one space.
882 (while (re-search-forward "[ \t]+" nil t)
884 ;; Translate all characters to "a".
887 (while (re-search-forward "[A-Za-z]" nil t)
888 (replace-match "a" t t)))
891 (while (re-search-forward "[0-9]+" nil t)
894 (string-to-int (buffer-substring
895 (match-beginning 0) (match-end 0))))))
896 (setq string (buffer-substring 1 (point-max)))
897 (setcar (car string-list) string)
898 (setq string-list (cdr string-list))))
902 ;; `gnus-uu-grab-articles' is the general multi-article treatment
903 ;; function. It takes a list of articles to be grabbed and a function
904 ;; to apply to each article.
906 ;; The function to be called should take two parameters. The first
907 ;; parameter is the article buffer. The function should leave the
908 ;; result, if any, in this buffer. Most treatment functions will just
911 ;; The second parameter is the state of the list of articles, and can
912 ;; have four values: `first', `middle', `last' and `first-and-last'.
914 ;; The function should return a list. The list may contain the
915 ;; following symbols:
916 ;; `error' if an error occurred
917 ;; `begin' if the beginning of an encoded file has been received
918 ;; If the list returned contains a `begin', the first element of
919 ;; the list *must* be a string with the file name of the decoded
921 ;; `end' if the the end of an encoded file has been received
922 ;; `middle' if the article was a body part of an encoded file
923 ;; `wrong-type' if the article was not a part of an encoded file
924 ;; `ok', which can be used everything is ok
926 (defvar gnus-uu-has-been-grabbed nil)
928 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
930 (if (not (and gnus-uu-has-been-grabbed
931 gnus-uu-unmark-articles-not-decoded))
933 (if dont-unmark-last-article
935 (setq art (car gnus-uu-has-been-grabbed))
936 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
937 (while gnus-uu-has-been-grabbed
938 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
939 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
940 (if dont-unmark-last-article
941 (setq gnus-uu-has-been-grabbed (list art))))))
943 ;; This function takes a list of articles and a function to apply to
944 ;; each article grabbed.
946 ;; This function returns a list of files decoded if the grabbing and
947 ;; the process-function has been successful and nil otherwise.
948 (defun gnus-uu-grab-articles
949 (articles process-function &optional sloppy limit no-errors)
952 has-been-begin has-been-end
953 article result-file result-files process-state article-buffer
956 (if (not (gnus-server-opened gnus-current-select-method))
958 (gnus-start-news-server)
959 (gnus-request-group gnus-newsgroup-name)))
961 (setq gnus-uu-has-been-grabbed nil)
964 (not (memq 'error process-state))
966 (not (memq 'end process-state))))
968 (setq article (car articles))
969 (setq articles (cdr articles))
970 (setq gnus-uu-has-been-grabbed (cons article gnus-uu-has-been-grabbed))
972 (if (> article gnus-uu-highest-article-number)
973 (setq gnus-uu-highest-article-number article))
976 (if (eq state 'first)
977 (setq state 'first-and-last)
980 (message "Getting article %d, %s" article (gnus-uu-part-number article))
982 (if (not (= (or gnus-current-article 0) article))
984 (gnus-request-article article gnus-newsgroup-name
986 (setq gnus-last-article gnus-current-article)
987 (setq gnus-current-article article)
988 (setq gnus-article-current (cons gnus-newsgroup-name article))
989 (if (stringp nntp-server-buffer)
990 (setq article-buffer nntp-server-buffer)
991 (setq article-buffer (buffer-name nntp-server-buffer))))
992 (setq article-buffer gnus-article-buffer))
994 (buffer-disable-undo article-buffer)
995 ;; Mark article as read.
996 (run-hooks 'gnus-mark-article-hook)
997 (and (memq article gnus-newsgroup-processable)
998 (gnus-summary-remove-process-mark article))
1000 (setq process-state (funcall process-function article-buffer state))
1002 (if (or (memq 'begin process-state)
1003 (and (or (eq state 'first) (eq state 'first-and-last))
1004 (memq 'ok process-state)))
1007 (if (and result-file (file-exists-p result-file))
1008 (delete-file result-file)))
1009 (if (memq 'begin process-state)
1010 (setq result-file (car process-state)))
1011 (setq begin-article article)
1012 (setq has-been-begin t)
1013 (setq has-been-end nil)))
1015 (if (memq 'end process-state)
1017 (setq gnus-uu-has-been-grabbed nil)
1018 (setq result-files (cons (list (cons 'name result-file)
1019 (cons 'article article))
1021 (setq has-been-end t)
1022 (setq has-been-begin nil)
1023 (and limit (= (length result-files) limit)
1024 (setq articles nil))))
1026 (if (and (or (eq state 'last) (eq state 'first-and-last))
1027 (not (memq 'end process-state)))
1028 (if (and result-file (file-exists-p result-file))
1029 (delete-file result-file)))
1031 (if (not (memq 'wrong-type process-state))
1032 (setq wrong-type nil)
1033 (if gnus-uu-unmark-articles-not-decoded
1034 (gnus-summary-tick-article article t)))
1036 (if sloppy (setq wrong-type nil))
1038 (if (and (not has-been-begin)
1040 (or (memq 'end process-state)
1041 (memq 'middle process-state)))
1043 (setq process-state (list 'error))
1044 (message "No begin part at the beginning")
1046 (setq state 'middle)))
1048 ;; Make sure the last article is put in the article buffer & fix
1051 (if (not (string= article-buffer gnus-article-buffer))
1053 (set-buffer (get-buffer-create gnus-article-buffer))
1054 (let ((buffer-read-only nil))
1057 (insert-buffer-substring article-buffer)
1062 (if (not has-been-begin)
1063 (if (not no-errors) (message "Wrong type file"))
1064 (if (memq 'error process-state)
1065 (setq result-files nil)
1066 (if (not (or (memq 'ok process-state)
1067 (memq 'end process-state)))
1070 (message "End of articles reached before end of file"))
1071 (setq result-files nil))
1072 (gnus-uu-unmark-list-of-grabbed)))))
1075 (defun gnus-uu-part-number (article)
1076 (let ((subject (header-subject (gnus-get-header-by-number article))))
1077 (if (string-match "([0-9]+ */[0-9]+)\\|([0-9]+ * of *[0-9]+)"
1079 (substring subject (match-beginning 0) (match-end 0))
1082 (defun gnus-uu-uudecode-sentinel (process event)
1083 (delete-process (get-process process)))
1085 (defun gnus-uu-uustrip-article (process-buffer in-state)
1086 ;; Uudecodes a file asynchronously.
1087 (let ((state (list 'ok))
1088 (process-connection-type nil)
1089 start-char pst name-beg name-end)
1091 (set-buffer process-buffer)
1092 (let ((case-fold-search nil)
1093 (buffer-read-only nil))
1097 (if gnus-uu-kill-carriage-return
1099 (while (search-forward "\r" nil t)
1100 (delete-backward-char 1))
1103 (if (not (re-search-forward gnus-uu-begin-string nil t))
1104 (if (not (re-search-forward gnus-uu-body-line nil t))
1105 (setq state (list 'wrong-type))))
1107 (if (memq 'wrong-type state)
1110 (setq start-char (point))
1112 (if (looking-at gnus-uu-begin-string)
1114 (setq name-end (match-end 1))
1116 ; Replace any slashes and spaces in file names before decoding
1117 (goto-char (setq name-beg (match-beginning 1)))
1118 (while (re-search-forward "/" name-end t)
1119 (replace-match ","))
1120 (goto-char name-beg)
1121 (while (re-search-forward " " name-end t)
1122 (replace-match "_"))
1123 (goto-char name-beg)
1124 (if (re-search-forward "_*$" name-end t)
1127 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1128 (and gnus-uu-uudecode-process
1129 (setq pst (process-status
1130 (or gnus-uu-uudecode-process "nevair")))
1131 (if (or (eq pst 'stop) (eq pst 'run))
1133 (delete-process gnus-uu-uudecode-process)
1134 (gnus-uu-unmark-list-of-grabbed t))))
1135 (if (get-process "*uudecode*")
1136 (delete-process "*uudecode*"))
1137 (setq gnus-uu-uudecode-process
1140 (get-buffer-create gnus-uu-output-buffer-name)
1142 (format "cd %s ; uudecode" gnus-uu-work-dir)))
1143 (set-process-sentinel
1144 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1145 (setq state (list 'begin))
1146 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1147 (setq state (list 'middle)))
1149 (goto-char (point-max))
1152 (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1155 (if (looking-at gnus-uu-end-string)
1156 (setq state (cons 'end state)))
1159 (and gnus-uu-uudecode-process
1160 (setq pst (process-status
1161 (or gnus-uu-uudecode-process "nevair")))
1162 (if (or (eq pst 'run) (eq pst 'stop))
1164 (if gnus-uu-correct-stripped-uucode
1166 (gnus-uu-check-correct-stripped-uucode
1168 (goto-char (point-max))
1170 (concat gnus-uu-body-line "\\|"
1176 (process-send-region gnus-uu-uudecode-process
1180 (delete-process gnus-uu-uudecode-process)
1181 (message "gnus-uu: Couldn't uudecode")
1183 (setq state (list 'wrong-type)))))
1185 (if (memq 'end state)
1186 (accept-process-output gnus-uu-uudecode-process)))
1187 (setq state (list 'wrong-type))))
1188 (if (not gnus-uu-uudecode-process)
1189 (setq state (list 'wrong-type)))))
1191 (if (memq 'begin state)
1192 (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1195 ;; This function is used by `gnus-uu-grab-articles' to treat
1196 ;; a shared article.
1197 (defun gnus-uu-unshar-article (process-buffer in-state)
1198 (let ((state (list 'ok))
1201 (set-buffer process-buffer)
1203 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1204 (setq state (list 'wrong-type))
1206 (setq start-char (point))
1207 (call-process-region
1208 start-char (point-max) "sh" nil
1209 (get-buffer-create gnus-uu-output-buffer-name) nil
1210 "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1213 ;; Returns the name of what the shar file is going to unpack.
1214 (defun gnus-uu-find-name-in-shar ()
1215 (let ((oldpoint (point))
1218 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1219 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1220 (goto-char oldpoint)
1223 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1224 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1225 ;; found, or the name of the command to run if such a rule is found.
1226 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1227 (let ((action-list (copy-sequence file-action-list))
1232 (and gnus-uu-ignore-files-by-name
1233 (string-match gnus-uu-ignore-files-by-name file-name)))
1235 (and gnus-uu-ignore-files-by-type
1236 (string-match gnus-uu-ignore-files-by-type
1237 (or (gnus-uu-choose-action
1238 file-name gnus-uu-ext-to-mime-list t)
1240 (while (not (or (eq action-list ()) action))
1241 (setq rule (car action-list))
1242 (setq action-list (cdr action-list))
1243 (if (string-match (car rule) file-name)
1244 (setq action (car (cdr rule))))))
1247 (defun gnus-uu-treat-archive (file-path)
1248 ;; Unpacks an archive. Returns t if unpacking is successful.
1249 (let ((did-unpack t)
1250 action command files file file-name dir)
1251 (setq action (gnus-uu-choose-action
1252 file-path (append gnus-uu-user-archive-rules
1253 (if gnus-uu-ignore-default-archive-rules
1255 gnus-uu-default-archive-rules))))
1257 (if (not action) (error "No unpackers for the file %s" file-path))
1259 (string-match "/[^/]*$" file-path)
1260 (setq file-name (substring file-path (1+ (match-beginning 0))))
1261 (setq dir (substring file-path 0 (match-beginning 0)))
1263 (if (member action gnus-uu-destructive-archivers)
1264 (copy-file file-path (concat file-path "~") t))
1266 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1269 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1272 (message "Unpacking: %s..." (gnus-uu-command action file-path))
1274 (if (= 0 (call-process "sh" nil
1275 (get-buffer-create gnus-uu-output-buffer-name)
1278 (message "Error during unpacking of archive")
1279 (setq did-unpack nil))
1281 (if (member action gnus-uu-destructive-archivers)
1282 (rename-file (concat file-path "~") file-path t))
1286 (defun gnus-uu-dir-files (dir)
1287 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1290 (if (file-directory-p (setq file (car dirs)))
1291 (setq files (append files (gnus-uu-dir-files file)))
1292 (setq files (cons file files)))
1293 (setq dirs (cdr dirs)))
1296 (defun gnus-uu-unpack-files (files &optional ignore)
1297 ;; Go through FILES and look for files to unpack.
1298 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1302 (setq file (cdr (assq 'name (car files))))
1303 (if (and (not (member file ignore))
1304 (equal (gnus-uu-get-action (file-name-nondirectory file))
1307 (setq did-unpack (cons file did-unpack))
1308 (or (gnus-uu-treat-archive file)
1309 (message "Error during unpacking of %s" file))
1310 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1313 (or (member (car nfiles) totfiles)
1314 (setq ofiles (cons (list (cons 'name (car nfiles))
1315 (cons 'original file))
1317 (setq nfiles (cdr nfiles)))
1318 (setq totfiles newfiles))))
1319 (setq files (cdr files)))
1321 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1324 (defun gnus-uu-ls-r (dir)
1325 (let* ((files (gnus-uu-directory-files dir t))
1328 (if (file-directory-p (car files))
1330 (setq ofiles (delete (car files) ofiles))
1331 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1332 (setq files (cdr files)))
1337 (defun gnus-uu-directory-files (dir &optional full)
1338 (let (files out file)
1339 (setq files (directory-files dir full))
1341 (setq file (car files))
1342 (setq files (cdr files))
1343 (or (string-match "/\\.\\.?$" file)
1344 (setq out (cons file out))))
1345 (setq out (nreverse out))
1348 (defun gnus-uu-check-correct-stripped-uucode (start end)
1349 (let (found beg length short)
1350 (if (not gnus-uu-correct-stripped-uucode)
1354 (if (re-search-forward " \\|`" end t)
1359 (if (looking-at "\n") (replace-match ""))
1363 (if (looking-at (concat gnus-uu-begin-string "\\|"
1364 gnus-uu-end-string))
1371 (setq length (- (point) beg))))
1376 (if (not (= length (- (point) beg)))
1377 (insert (make-string (- length (- (point) beg)) ? ))))
1378 (forward-line 1))))))
1380 (defun gnus-uu-initialize ()
1381 (setq gnus-uu-highest-article-number 1)
1382 (gnus-uu-check-for-generated-files)
1383 (setq gnus-uu-tmp-dir (expand-file-name gnus-uu-tmp-dir))
1384 (if (string-match "[^/]$" gnus-uu-tmp-dir)
1385 (setq gnus-uu-tmp-dir (concat gnus-uu-tmp-dir "/")))
1386 (if (not (file-directory-p gnus-uu-tmp-dir))
1387 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1388 (if (not (file-writable-p gnus-uu-tmp-dir))
1389 (error "Temp directory %s can't be written to" gnus-uu-tmp-dir)))
1390 (setq gnus-uu-work-dir
1391 (concat gnus-uu-tmp-dir (make-temp-name "gnus")))
1392 (gnus-uu-add-file gnus-uu-work-dir)
1393 (if (not (file-directory-p gnus-uu-work-dir))
1394 (make-directory gnus-uu-work-dir))
1395 (setq gnus-uu-work-dir (concat gnus-uu-work-dir "/")))
1397 ;; Kills the temporary uu buffers, kills any processes, etc.
1398 (defun gnus-uu-clean-up ()
1400 (and gnus-uu-uudecode-process
1401 (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1402 (if (or (eq pst 'stop) (eq pst 'run))
1403 (delete-process gnus-uu-uudecode-process)))
1404 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1405 (kill-buffer buf))))
1407 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1408 ;; hasn't been deleted, if, for instance, the user terminated decoding
1410 (defun gnus-uu-check-for-generated-files ()
1412 (while gnus-uu-generated-file-list
1413 (setq file (car gnus-uu-generated-file-list))
1414 (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1415 (if (not (string-match "/\\.[\\.]?$" file))
1417 (if (file-directory-p file)
1418 (setq dirs (cons file dirs))
1419 (if (file-exists-p file)
1420 (delete-file file))))))
1421 (setq dirs (nreverse dirs))
1423 (setq file (car dirs))
1424 (setq dirs (cdr dirs))
1425 (if (file-directory-p file)
1426 (if (string-match "/$" file)
1427 (delete-directory (substring file 0 (match-beginning 0)))
1428 (delete-directory file))))))
1430 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1431 ;; still exists upon exiting the newsgroup).
1432 (defun gnus-uu-add-file (file)
1434 (setq gnus-uu-generated-file-list
1435 (cons file gnus-uu-generated-file-list))
1436 (setq gnus-uu-generated-file-list
1437 (append file gnus-uu-generated-file-list))))
1439 ;; Inputs an action and a file and returns a full command, putting
1440 ;; ticks round the file name and escaping any ticks in the file name.
1441 (defun gnus-uu-command (action file)
1443 (while (string-match "`\\|\"\\|\\$\\|\\\\" file)
1446 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1447 (substring file (match-beginning 0) (match-end 0))))
1448 (setq file (substring file (1+ (match-beginning 0))))))
1449 (setq ofile (concat "\"" ofile file "\""))
1450 (if (string-match "%s" action)
1451 (format action ofile)
1452 (concat action " " ofile))))
1457 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1458 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1461 ;; Major mode for posting encoded articles.
1466 ;; Any function that is to be used as and encoding method will take two
1467 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1468 ;; and "spiral.jpg", respectively.) The function should return nil if
1469 ;; the encoding wasn't successful.
1470 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1471 "Function used for encoding binary files.
1472 There are three functions supplied with gnus-uu for encoding files:
1473 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1474 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1475 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1476 uuencode and adds MIME headers.")
1478 (defvar gnus-uu-post-include-before-composing nil
1479 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1480 If this variable is t, you can either include an encoded file with
1481 \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1483 (defvar gnus-uu-post-length 990
1484 "Maximum length of an article.
1485 The encoded file will be split into how many articles it takes to
1486 post the entire file.")
1488 (defvar gnus-uu-post-threaded nil
1489 "Non-nil means that gnus-uu will post the encoded file in a thread.
1490 This may not be smart, as no other decoder I have seen are able to
1491 follow threads when collecting uuencoded articles. (Well, I have seen
1492 one package that does that - gnus-uu, but somehow, I don't think that
1493 counts...) Default is nil.")
1495 (defvar gnus-uu-post-separate-description t
1496 "Non-nil means that the description will be posted in a separate article.
1497 The first article will typically be numbered (0/x). If this variable
1498 is nil, the description the user enters will be included at the
1499 beginning of the first article, which will be numbered (1/x). Default
1502 (defconst gnus-uu-post-binary-separator "--binary follows this line--")
1503 (defvar gnus-uu-post-message-id nil)
1504 (defvar gnus-uu-post-inserted-file-name nil)
1505 (defvar gnus-uu-winconf-post-news nil)
1507 ;; The following map and mode was taken from rnewspost.el and edited
1509 (defvar gnus-uu-post-reply-mode-map nil)
1510 (if gnus-uu-post-reply-mode-map
1512 (setq gnus-uu-post-reply-mode-map (make-keymap))
1513 (define-key gnus-uu-post-reply-mode-map "\C-c?" 'describe-mode)
1514 (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-d"
1515 'news-reply-distribution)
1516 (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-k"
1517 'news-reply-keywords)
1518 (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-n"
1519 'news-reply-newsgroups)
1521 (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-f"
1522 'news-reply-followup-to)
1523 (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-s" 'mail-subject)
1524 (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-a"
1525 'gnus-uu-post-reply-summary)
1526 (define-key gnus-uu-post-reply-mode-map "\C-c\C-r"
1527 'news-caesar-buffer-body)
1528 (define-key gnus-uu-post-reply-mode-map "\C-c\C-w" 'news-reply-signature)
1529 (define-key gnus-uu-post-reply-mode-map "\C-c\C-y"
1530 'news-reply-yank-original)
1531 (define-key gnus-uu-post-reply-mode-map "\C-c\C-q"
1532 'mail-fill-yanked-message)
1533 (define-key gnus-uu-post-reply-mode-map "\C-c\C-c"
1534 'gnus-uu-post-news-inews)
1535 (define-key gnus-uu-post-reply-mode-map "\C-c\C-s"
1536 'gnus-uu-post-news-inews)
1537 (define-key gnus-uu-post-reply-mode-map "\C-c\C-i"
1538 'gnus-uu-post-insert-binary-in-article)
1541 ;; This mode was taken from rnewspost.el and modified slightly.
1542 (defun gnus-uu-post-reply-mode ()
1543 "Major mode for editing binary news to be posted on USENET.
1544 First-time posters are asked to please read the articles in newsgroup:
1545 news.announce.newusers .
1547 Like news-reply-mode, which is like Text Mode, but with these
1548 additional commands:
1550 \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-news-inews] post the message.
1551 C-c C-f move to a header field (and create it if there isn't):
1552 C-c C-f C-n move to Newsgroups: C-c C-f C-s move to Subj:
1553 C-c C-f C-f move to Followup-To: C-c C-f C-k move to Keywords:
1554 C-c C-f C-d move to Distribution: C-c C-f C-a move to Summary:
1555 C-c C-y news-reply-yank-original (insert current message, in NEWS).
1556 C-c C-q mail-fill-yanked-message (fill what was yanked).
1557 C-c C-r caesar rotate all letters by 13 places in the article's body (rot13).
1558 \\[gnus-uu-post-insert-binary-in-article] encode and include a file in this article.
1560 This mode is almost identical to news-reply-mode, but has some
1561 additional commands for treating encoded binary articles. In
1562 particular, \\[gnus-uu-post-news-inews] will ask for a file to include, if
1563 one hasn't been included already. It will post, first, the message
1564 composed, and then it will post as many additional articles it takes
1565 to post the entire encoded files.
1569 `gnus-uu-post-encode-method'
1570 There are three functions supplied with gnus-uu for encoding files:
1571 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1572 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1573 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1574 uuencode and adds MIME headers.
1576 `gnus-uu-post-include-before-composing'
1577 Non-nil means that gnus-uu will ask for a file to encode before you
1578 compose the article. If this variable is t, you can either include
1579 an encoded file with `C-c C-i' or have one included for you when you
1582 `gnus-uu-post-length'
1583 Maximum length of an article. The encoded file will be split into how
1584 many articles it takes to post the entire file.
1586 `gnus-uu-post-separate-description'
1587 Non-nil means that the description will be posted in a separate
1588 article. The first article will typically be numbered (0/x). If
1589 this variable is nil, the description the user enters will be
1590 included at the beginning of the first article, which will be
1591 numbered (1/x). Default is t.
1593 `gnus-uu-post-threaded'
1594 Non-nil means that gnus-uu will post the encoded file in a thread.
1595 This may not be smart, as no other decoder I have seen are able to
1596 follow threads when collecting uuencoded articles. (Well, I have seen
1597 one package that does that - gnus-uu, but somehow, I don't think that
1598 counts...) Default is nil.
1602 (or (fboundp 'mail-setup) (load "sendmail"))
1603 (kill-all-local-variables)
1604 (make-local-variable 'mail-reply-buffer)
1605 (setq mail-reply-buffer nil)
1606 (set-syntax-table text-mode-syntax-table)
1607 (use-local-map gnus-uu-post-reply-mode-map)
1608 (setq local-abbrev-table text-mode-abbrev-table)
1609 (setq major-mode 'gnus-uu-post-reply-mode)
1610 (setq mode-name "Gnus UU News")
1611 (make-local-variable 'paragraph-separate)
1612 (make-local-variable 'paragraph-start)
1613 (setq paragraph-start (concat "^" (regexp-quote mail-header-separator)
1614 "$\\|" paragraph-start))
1615 (setq paragraph-separate (concat "^" (regexp-quote mail-header-separator)
1616 "$\\|" paragraph-separate))
1617 (run-hooks 'text-mode-hook 'gnus-uu-post-reply-mode-hook))
1619 (defun gnus-uu-post-news ()
1620 "Compose an article and post an encoded file."
1622 (setq gnus-uu-post-inserted-file-name nil)
1623 (setq gnus-uu-winconf-post-news (current-window-configuration))
1624 (let (news-reply-mode)
1625 (fset 'news-reply-mode 'gnus-uu-post-reply-mode)
1626 (gnus-summary-post-news)
1627 (if gnus-uu-post-include-before-composing
1628 (save-excursion (setq gnus-uu-post-inserted-file-name
1629 (gnus-uu-post-insert-binary))))))
1631 (defun gnus-uu-post-insert-binary-in-article ()
1632 "Inserts an encoded file in the buffer.
1633 The user will be asked for a file name."
1635 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1636 (error "Not in post-news buffer"))
1638 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1640 ;; Encodes with uuencode and substitutes all spaces with backticks.
1641 (defun gnus-uu-post-encode-uuencode (path file-name)
1642 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1646 (while (re-search-forward " " nil t)
1647 (replace-match "`"))
1650 ;; Encodes with uuencode and adds MIME headers.
1651 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1652 (if (gnus-uu-post-encode-uuencode path file-name)
1654 (gnus-uu-post-make-mime file-name "x-uue")
1657 ;; Encodes with base64 and adds MIME headers
1658 (defun gnus-uu-post-encode-mime (path file-name)
1659 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1661 (gnus-uu-post-make-mime file-name "base64")
1664 ;; Adds MIME headers.
1665 (defun gnus-uu-post-make-mime (file-name encoding)
1667 (insert (format "Content-Type: %s; name=\"%s\"\n"
1668 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1670 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1672 (set-buffer gnus-post-news-buffer)
1674 (re-search-forward (regexp-quote mail-header-separator))
1677 (narrow-to-region 1 (point))
1678 (or (mail-fetch-field "mime-version")
1681 (insert "MIME-Version: 1.0\n")))
1684 ;; Encodes a file PATH with COMMAND, leaving the result in the
1686 (defun gnus-uu-post-encode-file (command path file-name)
1687 (= 0 (call-process "sh" nil t nil "-c"
1688 (format "%s %s %s" command path file-name))))
1690 (defun gnus-uu-post-news-inews ()
1691 "Posts the composed news article and encoded file.
1692 If no file has been included, the user will be asked for a file."
1694 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1695 (error "Not in post news buffer"))
1699 (if gnus-uu-post-inserted-file-name
1700 (setq file-name gnus-uu-post-inserted-file-name)
1701 (setq file-name (gnus-uu-post-insert-binary)))
1703 (if gnus-uu-post-threaded
1704 (let ((gnus-required-headers
1705 (if (memq 'Message-ID gnus-required-headers)
1706 gnus-required-headers
1707 (cons 'Message-ID gnus-required-headers)))
1708 gnus-inews-article-hook elem)
1710 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1711 gnus-inews-article-hook
1712 (list gnus-inews-article-hook)))
1713 (setq gnus-inews-article-hook
1718 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1719 (setq gnus-uu-post-message-id
1721 (match-beginning 1) (match-end 1)))
1722 (setq gnus-uu-post-message-id nil))))
1723 gnus-inews-article-hook))
1724 (gnus-uu-post-encoded file-name t))
1725 (gnus-uu-post-encoded file-name nil)))
1726 (setq gnus-uu-post-inserted-file-name nil)
1727 (and gnus-uu-winconf-post-news
1728 (set-window-configuration gnus-uu-winconf-post-news)))
1730 ;; Asks for a file to encode, encodes it and inserts the result in
1731 ;; the current buffer. Returns the file name the user gave.
1732 (defun gnus-uu-post-insert-binary ()
1733 (let ((uuencode-buffer-name "*uuencode buffer*")
1734 file-path post-buf uubuf file-name)
1736 (setq file-path (read-file-name
1737 "What file do you want to encode? "))
1738 (if (not (file-exists-p file-path))
1739 (error "%s: No such file" file-path))
1741 (goto-char (point-max))
1742 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1744 (if (string-match "^~/" file-path)
1745 (setq file-path (concat "$HOME" (substring file-path 1))))
1746 (if (string-match "/[^/]*$" file-path)
1747 (setq file-name (substring file-path (1+ (match-beginning 0))))
1748 (setq file-name file-path))
1752 (set-buffer (setq uubuf
1753 (get-buffer-create uuencode-buffer-name)))
1755 (funcall gnus-uu-post-encode-method file-path file-name))
1756 (insert-buffer uubuf)
1757 (error "Encoding unsuccessful"))
1758 (kill-buffer uubuf))
1761 ;; Posts the article and all of the encoded file.
1762 (defun gnus-uu-post-encoded (file-name &optional threaded)
1763 (let ((send-buffer-name "*uuencode send buffer*")
1764 (encoded-buffer-name "*encoded buffer*")
1765 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1766 (separator (concat mail-header-separator "\n\n"))
1767 file uubuf length parts header i end beg
1768 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1770 (setq post-buf (current-buffer))
1773 (if (not (re-search-forward
1774 (if gnus-uu-post-separate-description
1775 gnus-uu-post-binary-separator
1776 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1777 (error "Internal error: No binary/header separator"))
1780 (setq beg-binary (point))
1781 (setq end-binary (point-max))
1784 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1786 (insert-buffer-substring post-buf beg-binary end-binary)
1788 (setq length (count-lines 1 (point-max)))
1789 (setq parts (/ length gnus-uu-post-length))
1790 (if (not (< (% length gnus-uu-post-length) 4))
1791 (setq parts (1+ parts))))
1793 (if gnus-uu-post-separate-description
1795 (kill-region (point) (point-max))
1799 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1801 (setq header (buffer-substring 1 (point)))
1804 (if (not gnus-uu-post-separate-description)
1806 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1809 (insert (format " (0/%d)" parts))))
1815 (while (not (> i parts))
1816 (set-buffer (get-buffer-create send-buffer-name))
1819 (if (and threaded gnus-uu-post-message-id)
1820 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1823 (- 62 (length (format top-string "" file-name i parts ""))))
1824 (if (> 1 (setq minlen (/ whole-len 2)))
1829 (make-string minlen ?-)
1832 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1835 (if (not (re-search-forward "^Subject: " nil t))
1840 (insert (format " (%d/%d)" i parts)))
1841 (if (or (and (= i 2) gnus-uu-post-separate-description)
1842 (and (= i 1) (not gnus-uu-post-separate-description)))
1843 (replace-match "Subject: Re: "))))
1845 (goto-char (point-max))
1850 (goto-char (point-max))
1851 (forward-line gnus-uu-post-length))
1852 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1855 (insert-buffer-substring uubuf beg end)
1862 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1865 (if (re-search-forward gnus-uu-post-binary-separator nil t)
1873 (and (setq buf (get-buffer send-buffer-name))
1875 (and (setq buf (get-buffer encoded-buffer-name))
1878 (if (not gnus-uu-post-separate-description)
1880 (set-buffer-modified-p nil)
1881 (and (fboundp 'bury-buffer) (bury-buffer))))))
1885 ;; gnus-uu.el ends here