1 ;;; gnus-uu.el --- extract (uu)encoded files in Gnus
2 ;; Copyright (C) 1985,86,87,93,94,95,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (require 'cl))
33 ;; Default viewing action rules
35 (defvar gnus-uu-default-view-rules
36 '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed s/\r//g")
37 ("\\.pas$" "cat %s | sed s/\r//g")
38 ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
39 ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "xv")
40 ("\\.tga$" "tgatoppm %s | xv -")
41 ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
42 "sox -v .5 %s -t .au -u - > /dev/audio")
43 ("\\.au$" "cat %s > /dev/audio")
45 ("\\.ps$" "ghostview")
47 ("\\.html$" "xmosaic")
48 ("\\.mpe?g$" "mpeg_play")
49 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
50 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
52 "*Default actions to be taken when the user asks to view a file.
53 To change the behaviour, you can either edit this variable or set
54 `gnus-uu-user-view-rules' to something useful.
58 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
59 following in your .emacs file:
61 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
63 Both these variables are lists of lists with two string elements. The
64 first string is a regular expression. If the file name matches this
65 regular expression, the command in the second string is executed with
66 the file as an argument.
68 If the command string contains \"%s\", the file name will be inserted
69 at that point in the command string. If there's no \"%s\" in the
70 command string, the file name will be appended to the command string
73 There are several user variables to tailor the behaviour of gnus-uu to
74 your needs. First we have `gnus-uu-user-view-rules', which is the
75 variable gnus-uu first consults when trying to decide how to view a
76 file. If this variable contains no matches, gnus-uu examines the
77 default rule variable provided in this package. If gnus-uu finds no
78 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
81 (defvar gnus-uu-user-view-rules nil
82 "*Variable detailing what actions are to be taken to view a file.
83 See the documentation on the `gnus-uu-default-view-rules' variable for
86 (defvar gnus-uu-user-view-rules-end
88 "*Variable saying what actions are to be taken if no rule matched the file name.
89 See the documentation on the `gnus-uu-default-view-rules' variable for
92 ;; Default unpacking commands
94 (defvar gnus-uu-default-archive-rules
95 '(("\\.tar$" "tar xf")
96 ("\\.zip$" "unzip -o")
100 ("\\.\\(lzh\\|lha\\)$" "lha x")
101 ("\\.Z$" "uncompress")
103 ("\\.arc$" "arc -x")))
105 (defvar gnus-uu-destructive-archivers
106 (list "uncompress" "gunzip"))
108 (defvar gnus-uu-user-archive-rules nil
109 "*A list that can be set to override the default archive unpacking commands.
110 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
111 unpack zip files, say the following:
112 (setq gnus-uu-user-archive-rules
113 '((\"\\\\.tar$\" \"untar\")
114 (\"\\\\.zip$\" \"zip -x\")))")
116 (defvar gnus-uu-ignore-files-by-name nil
117 "*A regular expression saying what files should not be viewed based on name.
118 If, for instance, you want gnus-uu to ignore all .au and .wav files,
119 you could say something like
121 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
123 Note that this variable can be used in conjunction with the
124 `gnus-uu-ignore-files-by-type' variable.")
126 (defvar gnus-uu-ignore-files-by-type nil
127 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
128 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
129 you could say something like
131 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
133 Note that this variable can be used in conjunction with the
134 `gnus-uu-ignore-files-by-name' variable.")
136 ;; Pseudo-MIME support
138 (defconst gnus-uu-ext-to-mime-list
139 '(("\\.gif$" "image/gif")
140 ("\\.jpe?g$" "image/jpeg")
141 ("\\.tiff?$" "image/tiff")
142 ("\\.xwd$" "image/xwd")
143 ("\\.pbm$" "image/pbm")
144 ("\\.pgm$" "image/pgm")
145 ("\\.ppm$" "image/ppm")
146 ("\\.xbm$" "image/xbm")
147 ("\\.pcx$" "image/pcx")
148 ("\\.tga$" "image/tga")
149 ("\\.ps$" "image/postscript")
150 ("\\.fli$" "video/fli")
151 ("\\.wav$" "audio/wav")
152 ("\\.aiff$" "audio/aiff")
153 ("\\.hcom$" "audio/hcom")
154 ("\\.voc$" "audio/voc")
155 ("\\.smp$" "audio/smp")
156 ("\\.mod$" "audio/mod")
157 ("\\.dvi$" "image/dvi")
158 ("\\.mpe?g$" "video/mpeg")
159 ("\\.au$" "audio/basic")
160 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
161 ("\\.\\(c\\|h\\)$" "text/source")
162 ("read.*me" "text/plain")
163 ("\\.html$" "text/html")
164 ("\\.bat$" "text/bat")
165 ("\\.[1-6]$" "text/man")
166 ("\\.flc$" "video/flc")
167 ("\\.rle$" "video/rle")
168 ("\\.pfx$" "video/pfx")
169 ("\\.avi$" "video/avi")
170 ("\\.sme$" "video/sme")
171 ("\\.rpza$" "video/prza")
172 ("\\.dl$" "video/dl")
173 ("\\.qt$" "video/qt")
174 ("\\.rsrc$" "video/rsrc")
175 ("\\..*$" "unknown/unknown")))
177 ;; Various variables users may set
179 (defvar gnus-uu-tmp-dir "/tmp/"
180 "*Variable saying where gnus-uu is to do its work.
181 Default is \"/tmp/\".")
183 (defvar gnus-uu-do-not-unpack-archives nil
184 "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
187 (defvar gnus-uu-view-and-save nil
188 "*Non-nil means that the user will always be asked to save a file after viewing it.
189 If the variable is nil, the user will only be asked to save if the
190 viewing is unsuccessful. Default is nil.")
192 (defvar gnus-uu-ignore-default-view-rules nil
193 "*Non-nil means that gnus-uu will ignore the default viewing rules.
194 Only the user viewing rules will be consulted. Default is nil.")
196 (defvar gnus-uu-grabbed-file-functions nil
197 "*Functions run on each file after successful decoding.
198 They will be called with the name of the file as the argument.
199 Likely functions you can use in this list are `gnus-uu-grab-view'
200 and `gnus-uu-grab-move'.")
202 (defvar gnus-uu-ignore-default-archive-rules nil
203 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
204 Only the user unpacking commands will be consulted. Default is nil.")
206 (defvar gnus-uu-kill-carriage-return t
207 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
210 (defvar gnus-uu-view-with-metamail nil
211 "*Non-nil means that files will be viewed with metamail.
212 The gnus-uu viewing functions will be ignored and gnus-uu will try
213 to guess at a content-type based on file name suffixes. Default
216 (defvar gnus-uu-unmark-articles-not-decoded nil
217 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
220 (defvar gnus-uu-correct-stripped-uucode nil
221 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
224 (defvar gnus-uu-save-in-digest nil
225 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
226 If this variable is nil, gnus-uu will just save everything in a
227 file without any embellishments. The digesting almost conforms to RFC1153 -
228 no easy way to specify any meaningful volume and issue numbers were found,
229 so I simply dropped them.")
231 (defvar gnus-uu-digest-headers
232 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
233 "^Summary:" "^References:")
234 "*List of regexps to match headers included in digested messages.
235 The headers will be included in the sequence they are matched.")
237 (defvar gnus-uu-save-separate-articles nil
238 "*Non-nil means that gnus-uu will save articles in separate files.")
240 ;; Internal variables
242 (defvar gnus-uu-saved-article-name nil)
244 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
245 (defconst gnus-uu-end-string "^end[ \t]*$")
247 (defconst gnus-uu-body-line "^M")
249 (while (> (setq i (1- i)) 0)
250 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
251 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
253 ;"^M.............................................................?$"
255 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
257 (defvar gnus-uu-shar-file-name nil)
258 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
260 (defconst gnus-uu-postscript-begin-string "^%!PS-")
261 (defconst gnus-uu-postscript-end-string "^%%EOF$")
263 (defvar gnus-uu-file-name nil)
264 (defconst gnus-uu-uudecode-process nil)
265 (defvar gnus-uu-binhex-article-name nil)
267 (defvar gnus-uu-generated-file-list nil)
268 (defvar gnus-uu-work-dir nil)
270 (defconst gnus-uu-output-buffer-name " *Gnus UU Output*")
272 (defvar gnus-uu-default-dir default-directory)
273 (defvar gnus-uu-digest-from-subject nil)
278 (gnus-uu-mark-map "P" gnus-summary-mark-map)
279 "p" gnus-summary-mark-as-processable
280 "u" gnus-summary-unmark-as-processable
281 "U" gnus-summary-unmark-all-processable
282 "v" gnus-uu-mark-over
283 "s" gnus-uu-mark-series
284 "r" gnus-uu-mark-region
285 "R" gnus-uu-mark-by-regexp
286 "t" gnus-uu-mark-thread
287 "T" gnus-uu-unmark-thread
289 "b" gnus-uu-mark-buffer
290 "S" gnus-uu-mark-sparse)
293 (gnus-uu-extract-map "X" gnus-summary-mode-map)
294 ;;"x" gnus-uu-extract-any
295 ;;"m" gnus-uu-extract-mime
296 "u" gnus-uu-decode-uu
297 "U" gnus-uu-decode-uu-and-save
298 "s" gnus-uu-decode-unshar
299 "S" gnus-uu-decode-unshar-and-save
300 "o" gnus-uu-decode-save
301 "O" gnus-uu-decode-save
302 "b" gnus-uu-decode-binhex
303 "B" gnus-uu-decode-binhex
304 "p" gnus-uu-decode-postscript
305 "P" gnus-uu-decode-postscript-and-save)
308 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
309 "u" gnus-uu-decode-uu-view
310 "U" gnus-uu-decode-uu-and-save-view
311 "s" gnus-uu-decode-unshar-view
312 "S" gnus-uu-decode-unshar-and-save-view
313 "o" gnus-uu-decode-save-view
314 "O" gnus-uu-decode-save-view
315 "b" gnus-uu-decode-binhex-view
316 "B" gnus-uu-decode-binhex-view
317 "p" gnus-uu-decode-postscript-view
318 "P" gnus-uu-decode-postscript-and-save-view)
323 (defun gnus-uu-decode-uu (n)
324 "Uudecodes the current article."
326 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
328 (defun gnus-uu-decode-uu-and-save (n dir)
329 "Decodes and saves the resulting file."
331 (list current-prefix-arg
332 (file-name-as-directory
333 (read-file-name "Uudecode and save in dir: "
335 gnus-uu-default-dir t))))
336 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir))
338 (defun gnus-uu-decode-unshar (n)
339 "Unshars the current article."
341 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan))
343 (defun gnus-uu-decode-unshar-and-save (n dir)
344 "Unshars and saves the current article."
346 (list current-prefix-arg
347 (file-name-as-directory
348 (read-file-name "Unshar and save in dir: "
350 gnus-uu-default-dir t))))
351 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan))
353 (defun gnus-uu-decode-save (n file)
354 "Saves the current article."
356 (list current-prefix-arg
358 (if gnus-uu-save-separate-articles
359 "Save articles is dir: "
360 "Save articles in file: ")
362 gnus-uu-default-dir)))
363 (setq gnus-uu-saved-article-name file)
364 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t)
365 (setq gnus-uu-generated-file-list
366 (delete file gnus-uu-generated-file-list)))
368 (defun gnus-uu-decode-binhex (n dir)
369 "Unbinhexes the current article."
371 (list current-prefix-arg
372 (file-name-as-directory
373 (read-file-name "Unbinhex and save in dir: "
375 gnus-uu-default-dir))))
376 (setq gnus-uu-binhex-article-name
377 (make-temp-name (concat gnus-uu-work-dir "binhex")))
378 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
380 (defun gnus-uu-decode-uu-view (n)
381 "Uudecodes and views the current article."
383 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
384 (gnus-uu-decode-uu n)))
386 (defun gnus-uu-decode-uu-and-save-view (n dir)
387 "Decodes, views and saves the resulting file."
389 (list current-prefix-arg
390 (read-file-name "Uudecode, view and save in dir: "
392 gnus-uu-default-dir t)))
393 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
394 (gnus-uu-decode-uu-and-save n dir)))
396 (defun gnus-uu-decode-unshar-view (n)
397 "Unshars and views the current article."
399 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
400 (gnus-uu-decode-unshar n)))
402 (defun gnus-uu-decode-unshar-and-save-view (n dir)
403 "Unshars and saves the current article."
405 (list current-prefix-arg
406 (read-file-name "Unshar, view and save in dir: "
408 gnus-uu-default-dir t)))
409 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
410 (gnus-uu-decode-unshar-and-save n dir)))
412 (defun gnus-uu-decode-save-view (n file)
413 "Saves and views the current article."
415 (list current-prefix-arg
416 (read-file-name (if gnus-uu-save-separate-articles
417 "Save articles is dir: "
418 "Save articles in file: ")
419 gnus-uu-default-dir gnus-uu-default-dir)))
420 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
421 (gnus-uu-decode-save n file)))
423 (defun gnus-uu-decode-binhex-view (n file)
424 "Unbinhexes and views the current article."
426 (list current-prefix-arg
427 (read-file-name "Unbinhex, view and save in dir: "
428 gnus-uu-default-dir gnus-uu-default-dir)))
429 (setq gnus-uu-binhex-article-name
430 (make-temp-name (concat gnus-uu-work-dir "binhex")))
431 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
432 (gnus-uu-decode-binhex n file)))
435 ;; Digest and forward articles
437 (defun gnus-uu-digest-mail-forward (n &optional post)
438 "Digests and forwards all articles in this series."
440 (let ((gnus-uu-save-in-digest t)
441 (file (make-temp-name (concat gnus-uu-tmp-dir "forward")))
443 (setq gnus-uu-digest-from-subject nil)
444 (gnus-uu-decode-save n file)
445 (gnus-uu-add-file file)
446 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
447 (gnus-add-current-to-buffer-list)
449 (delete-other-windows)
451 (let ((fs gnus-uu-digest-from-subject))
454 (setq from (car (car fs))
455 subject (gnus-simplify-subject-fuzzy (cdr (car fs)))
457 (while (and fs (or from subject))
459 (or (string= from (car (car fs)))
462 (or (string= (gnus-simplify-subject-fuzzy (cdr (car fs)))
466 (or subject (setq subject "Digested Articles"))
467 (or from (setq from "Various")))
468 (goto-char (point-min))
469 (and (re-search-forward "^Subject: ")
471 (delete-region (point) (gnus-point-at-eol))
473 (goto-char (point-min))
474 (and (re-search-forward "^From: ")
476 (delete-region (point) (gnus-point-at-eol))
479 (gnus-forward-using-post)
483 (setq gnus-uu-digest-from-subject nil)))
485 (defun gnus-uu-digest-post-forward (n)
486 "Digest and forward to a newsgroup."
488 (gnus-uu-digest-mail-forward n t))
492 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
493 "Ask for a regular expression and set the process mark on all articles that match."
494 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
495 (gnus-set-global-variables)
496 (let ((articles (gnus-uu-find-articles-matching regexp)))
499 (gnus-summary-remove-process-mark (pop articles))
500 (gnus-summary-set-process-mark (pop articles))))
502 (gnus-summary-position-point))
504 (defun gnus-uu-unmark-by-regexp (regexp &optional unmark)
505 "Ask for a regular expression and remove the process mark on all articles that match."
506 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
507 (gnus-uu-mark-by-regexp regexp t))
509 (defun gnus-uu-mark-series ()
510 "Mark the current series with the process mark."
512 (gnus-set-global-variables)
513 (let ((articles (gnus-uu-find-articles-matching)))
515 (gnus-summary-set-process-mark (car articles))
516 (setq articles (cdr articles)))
518 (gnus-summary-position-point))
520 (defun gnus-uu-mark-region (beg end &optional unmark)
521 "Set the process mark on all articles between point and mark."
523 (gnus-set-global-variables)
526 (while (< (point) end)
528 (gnus-summary-remove-process-mark (gnus-summary-article-number))
529 (gnus-summary-set-process-mark (gnus-summary-article-number)))
531 (gnus-summary-position-point))
533 (defun gnus-uu-unmark-region (beg end)
534 "Remove the process mark from all articles between point and mark."
536 (gnus-uu-mark-region beg end t))
538 (defun gnus-uu-mark-buffer ()
539 "Set the process mark on all articles in the buffer."
541 (gnus-uu-mark-region (point-min) (point-max)))
543 (defun gnus-uu-unmark-buffer ()
544 "Remove the process mark on all articles in the buffer."
546 (gnus-uu-mark-region (point-min) (point-max) t))
548 (defun gnus-uu-mark-thread ()
549 "Marks all articles downwards in this thread."
551 (gnus-set-global-variables)
552 (let ((level (gnus-summary-thread-level)))
553 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
554 (zerop (gnus-summary-next-subject 1))
555 (> (gnus-summary-thread-level) level))))
556 (gnus-summary-position-point))
558 (defun gnus-uu-unmark-thread ()
559 "Unmarks all articles downwards in this thread."
561 (gnus-set-global-variables)
562 (let ((level (gnus-summary-thread-level)))
563 (while (and (gnus-summary-remove-process-mark
564 (gnus-summary-article-number))
565 (zerop (gnus-summary-next-subject 1))
566 (> (gnus-summary-thread-level) level))))
567 (gnus-summary-position-point))
569 (defun gnus-uu-mark-over (score)
570 "Mark all articles with a score over SCORE (the prefix.)"
572 (let ((score (gnus-score-default score))
573 (data gnus-newsgroup-data))
576 (when (> (or (cdr (assq (gnus-data-number (caar data))
577 gnus-newsgroup-scored))
578 gnus-summary-default-score 0)
580 (gnus-summary-set-process-mark (caar data)))
581 (setq data (cdr data))))
582 (gnus-summary-position-point)))
584 (defun gnus-uu-mark-sparse ()
585 "Mark all series that have some articles marked."
587 (gnus-set-global-variables)
588 (let ((marked (nreverse gnus-newsgroup-processable))
589 subject articles total headers)
590 (or marked (error "No articles marked with the process mark"))
591 (setq gnus-newsgroup-processable nil)
594 (and (setq headers (gnus-summary-article-header (car marked)))
595 (setq subject (mail-header-subject headers)
596 articles (gnus-uu-find-articles-matching
597 (gnus-uu-reginize-string subject))
598 total (nconc total articles)))
600 (gnus-summary-set-process-mark (car articles))
601 (setcdr marked (delq (car articles) (cdr marked)))
602 (setq articles (cdr articles)))
603 (setq marked (cdr marked)))
604 (setq gnus-newsgroup-processable (nreverse total)))
605 (gnus-summary-position-point)))
607 (defun gnus-uu-mark-all ()
608 "Mark all articles in \"series\" order."
610 (gnus-set-global-variables)
611 (setq gnus-newsgroup-processable nil)
613 (let ((data gnus-newsgroup-data)
616 (unless (memq (setq number (gnus-data-number (car data)))
617 gnus-newsgroup-processable)
618 (gnus-summary-goto-subject number)
619 (gnus-uu-mark-series))
620 (setq data (cdr data)))))
621 (gnus-summary-position-point))
623 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
625 (defun gnus-uu-decode-postscript (n)
626 "Gets postscript of the current article."
628 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
630 (defun gnus-uu-decode-postscript-view (n)
631 "Gets and views the current article."
633 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
634 (gnus-uu-decode-postscript n)))
636 (defun gnus-uu-decode-postscript-and-save (n dir)
637 "Extracts postscript and saves the current article."
639 (list current-prefix-arg
640 (file-name-as-directory
641 (read-file-name "Save in dir: "
643 gnus-uu-default-dir t))))
644 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n dir))
647 (defun gnus-uu-decode-postscript-and-save-view (n dir)
648 "Decodes, views and saves the resulting file."
650 (list current-prefix-arg
651 (read-file-name "Where do you want to save the file(s)? "
653 gnus-uu-default-dir t)))
654 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
655 (gnus-uu-decode-postscript-and-save n dir)))
658 ;; Internal functions.
660 (defun gnus-uu-decode-with-method (method n &optional save not-insert scan)
661 (gnus-uu-initialize scan)
662 (if save (setq gnus-uu-default-dir save))
663 (let ((articles (gnus-uu-get-list-of-articles n))
665 (setq files (gnus-uu-grab-articles articles method t))
666 (let ((gnus-current-article (car articles)))
667 (and scan (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
668 (and save (gnus-uu-save-files files save))
669 (if (eq gnus-uu-do-not-unpack-archives nil)
670 (setq files (gnus-uu-unpack-files files)))
671 (gnus-uu-add-file (mapcar (lambda (file) (cdr (assq 'name file))) files))
672 (setq files (nreverse (gnus-uu-get-actions files)))
673 (or not-insert (not gnus-insert-pseudo-articles)
674 (gnus-summary-insert-pseudos files save))))
676 ;; Return a list of files in dir.
677 (defun gnus-uu-scan-directory (dir)
678 (let ((files (directory-files dir t))
681 (cond ((string-match "/\\.\\.?$" (car files)))
682 ((file-directory-p (car files))
683 (setq dirs (cons (car files) dirs)))
684 (t (setq out (cons (list (cons 'name (car files))
685 (cons 'article gnus-current-article))
687 (setq files (cdr files)))
688 (apply 'nconc out (mapcar (lambda (d) (gnus-uu-scan-directory d))
691 (defun gnus-uu-save-files (files dir)
692 (let ((len (length files))
696 (setq file (cdr (assq 'name (car files))))
699 (setq to-file (if (file-directory-p dir)
700 (concat dir (file-name-nondirectory file))
702 (and (or (not (file-exists-p to-file))
703 (gnus-y-or-n-p (format "%s exists; overwrite? "
705 (copy-file file to-file t t))))
706 (setq files (cdr files)))
707 (message "Saved %d file%s" len (if (> len 1) "s" ""))))
709 ;; Functions for saving and possibly digesting articles without
712 ;; Function called by gnus-uu-grab-articles to treat each article.
713 (defun gnus-uu-save-article (buffer in-state)
715 (gnus-uu-save-separate-articles
718 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
719 gnus-current-article))
720 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
721 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
723 ((eq in-state 'last) (list 'end))
724 (t (list 'middle)))))
725 ((not gnus-uu-save-in-digest)
728 (write-region 1 (point-max) gnus-uu-saved-article-name t)
729 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
730 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
732 ((eq in-state 'last) (list 'end))
733 (t (list 'middle)))))
735 (let ((header (gnus-summary-article-header)))
736 (setq gnus-uu-digest-from-subject
737 (cons (cons (mail-header-from header)
738 (mail-header-subject header))
739 gnus-uu-digest-from-subject)))
740 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
741 (delim (concat "^" (make-string 30 ?-) "$"))
742 beg subj headers headline sorthead body end-string state)
743 (if (or (eq in-state 'first)
744 (eq in-state 'first-and-last))
746 (setq state (list 'begin))
747 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
750 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
753 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
754 (current-time-string) name name))))
755 (if (not (eq in-state 'end))
756 (setq state (list 'middle))))
758 (set-buffer (get-buffer "*gnus-uu-body*"))
759 (goto-char (setq beg (point-max)))
763 (let (buffer-read-only)
764 (set-text-properties (point-min) (point-max) nil)
765 ;; These two are necessary for XEmacs 19.12 fascism.
766 (put-text-property (point-min) (point-max) 'invisible nil)
767 (put-text-property (point-min) (point-max) 'intangible nil))
768 (goto-char (point-min))
769 (re-search-forward "\n\n")
770 ;; Quote all 30-dash lines.
772 (while (re-search-forward delim nil t)
776 (setq body (buffer-substring (1- (point)) (point-max)))
777 (narrow-to-region (point-min) (point))
778 (if (not (setq headers gnus-uu-digest-headers))
779 (setq sorthead (buffer-substring (point-min) (point-max)))
781 (setq headline (car headers))
782 (setq headers (cdr headers))
783 (goto-char (point-min))
784 (if (re-search-forward headline nil t)
789 (or (and (re-search-forward "^[^ \t]" nil t)
791 (progn (forward-line 1) (point)))))))))
793 (insert sorthead) (goto-char (point-max))
794 (insert body) (goto-char (point-max))
795 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
797 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
799 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
801 (set-buffer (get-buffer "*gnus-uu-pre*"))
802 (insert (format " %s\n" subj))))))
803 (if (or (eq in-state 'last)
804 (eq in-state 'first-and-last))
807 (set-buffer (get-buffer "*gnus-uu-pre*"))
808 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
809 (write-region 1 (point-max) gnus-uu-saved-article-name))
811 (set-buffer (get-buffer "*gnus-uu-body*"))
812 (goto-char (point-max))
814 (concat (setq end-string (format "End of %s Digest" name))
816 (insert (concat (make-string (length end-string) ?*) "\n"))
817 (write-region 1 (point-max) gnus-uu-saved-article-name t))
818 (kill-buffer (get-buffer "*gnus-uu-pre*"))
819 (kill-buffer (get-buffer "*gnus-uu-body*"))
820 (setq state (cons 'end state))))
821 (if (memq 'begin state)
822 (cons gnus-uu-saved-article-name state)
825 ;; Binhex treatment - not very advanced.
827 (defconst gnus-uu-binhex-body-line
828 "^[^:]...............................................................$")
829 (defconst gnus-uu-binhex-begin-line
830 "^:...............................................................$")
831 (defconst gnus-uu-binhex-end-line
834 (defun gnus-uu-binhex-article (buffer in-state)
835 (let (state start-char)
839 (goto-char (point-min))
840 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
841 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
842 (setq state (list 'wrong-type))))
844 (if (memq 'wrong-type state)
847 (setq start-char (point))
848 (if (looking-at gnus-uu-binhex-begin-line)
850 (setq state (list 'begin))
851 (write-region 1 1 gnus-uu-binhex-article-name))
852 (setq state (list 'middle)))
853 (goto-char (point-max))
854 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
855 gnus-uu-binhex-end-line) nil t)
856 (if (looking-at gnus-uu-binhex-end-line)
857 (setq state (if (memq 'begin state)
862 (if (file-exists-p gnus-uu-binhex-article-name)
863 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
864 (if (memq 'begin state)
865 (cons gnus-uu-binhex-article-name state)
870 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
871 (let ((state (list 'ok))
872 start-char end-char file-name)
874 (set-buffer process-buffer)
875 (goto-char (point-min))
876 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
877 (setq state (list 'wrong-type))
879 (setq start-char (point))
880 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
881 (setq state (list 'wrong-type))
882 (setq end-char (point))
883 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
884 (insert-buffer-substring process-buffer start-char end-char)
885 (setq file-name (concat gnus-uu-work-dir
886 (cdr gnus-article-current) ".ps"))
887 (write-region (point-min) (point-max) file-name)
888 (setq state (list file-name 'begin 'end)))))
894 (defun gnus-uu-get-actions (files)
898 (setq name (cdr (assq 'name (car files))))
900 (setq action (gnus-uu-get-action name))
901 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
902 (cons 'action "file")
903 (cons 'action action))
904 (cons 'execute (gnus-uu-command
907 (setq files (cdr files)))
910 (defun gnus-uu-get-action (file-name)
913 (gnus-uu-choose-action
916 gnus-uu-user-view-rules
917 (if gnus-uu-ignore-default-view-rules
919 gnus-uu-default-view-rules)
920 gnus-uu-user-view-rules-end)))
921 (if (and (not (string= (or action "") "gnus-uu-archive"))
922 gnus-uu-view-with-metamail)
924 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
925 (setq action (format "metamail -d -b -c \"%s\"" action))))
929 ;; Functions for treating subjects and collecting series.
931 (defun gnus-uu-reginize-string (string)
932 ;; Takes a string and puts a \ in front of every special character;
933 ;; ignores any leading "version numbers" thingies that they use in
934 ;; the comp.binaries groups, and either replaces anything that looks
935 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
936 ;; like that, replaces the last two numbers with "[0-9]+". This, in
937 ;; my experience, should get most postings of a series.
939 (vernum "v[0-9]+[a-z][0-9]+:")
942 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
943 (buffer-disable-undo (current-buffer))
945 (insert (regexp-quote string))
948 (setq case-fold-search nil)
949 (goto-char (point-min))
950 (if (looking-at vernum)
952 (replace-match vernum t t)
953 (setq beg (length vernum))))
956 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
957 (replace-match " [0-9]+/[0-9]+")
960 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
961 (replace-match "[0-9]+ of [0-9]+")
964 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
967 (< 1 (goto-char (1- (point))))))
968 (re-search-forward "[0-9]+" nil t)
969 (replace-match "[0-9]+")
971 (setq count (1- count)))))
974 (while (re-search-forward "[ \t]+" nil t)
975 (replace-match "[ \t]*" t t))
977 (buffer-substring 1 (point-max)))))
979 (defun gnus-uu-get-list-of-articles (n)
980 ;; If N is non-nil, the article numbers of the N next articles
982 ;; If any articles have been marked as processable, they will be
984 ;; Failing that, articles that have subjects that are part of the
985 ;; same "series" as the current will be returned.
989 (let ((backward (< n 0))
993 (setq articles (cons (gnus-summary-article-number)
995 (gnus-summary-search-forward nil nil backward))
997 (nreverse articles)))
998 (gnus-newsgroup-processable
999 (reverse gnus-newsgroup-processable))
1001 (gnus-uu-find-articles-matching)))))
1003 (defun gnus-uu-string< (l1 l2)
1004 (string< (car l1) (car l2)))
1006 (defun gnus-uu-find-articles-matching
1007 (&optional subject only-unread do-not-translate)
1008 ;; Finds all articles that matches the regexp SUBJECT. If it is
1009 ;; nil, the current article name will be used. If ONLY-UNREAD is
1010 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1011 ;; non-nil, article names are not equalized before sorting.
1012 (let ((subject (or subject
1013 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1018 ;; Collect all subjects matching subject.
1019 (let ((case-fold-search t)
1020 (data gnus-newsgroup-data)
1024 (and (not (gnus-data-pseudo-p d))
1025 (or (not only-unread)
1026 (= (setq mark (gnus-data-mark d))
1028 (= mark gnus-ticked-mark)
1029 (= mark gnus-dormant-mark))
1030 (setq subj (mail-header-subject (gnus-data-header d)))
1031 (string-match subject subj)
1032 (setq list-of-subjects
1033 (cons (cons subj (gnus-data-number d))
1034 list-of-subjects)))))
1036 ;; Expand numbers, sort, and return the list of article
1038 (mapcar (lambda (sub) (cdr sub))
1039 (sort (gnus-uu-expand-numbers
1041 (not do-not-translate))
1042 'gnus-uu-string<))))))
1044 (defun gnus-uu-expand-numbers (string-list &optional translate)
1045 ;; Takes a list of strings and "expands" all numbers in all the
1046 ;; strings. That is, this function makes all numbers equal length by
1047 ;; prepending lots of zeroes before each number. This is to ease later
1048 ;; sorting to find out what sequence the articles are supposed to be
1049 ;; decoded in. Returns the list of expanded strings.
1050 (let ((out-list string-list)
1053 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1054 (buffer-disable-undo (current-buffer))
1057 (insert (car (car string-list)))
1058 ;; Translate multiple spaces to one space.
1059 (goto-char (point-min))
1060 (while (re-search-forward "[ \t]+" nil t)
1061 (replace-match " "))
1062 ;; Translate all characters to "a".
1063 (goto-char (point-min))
1065 (while (re-search-forward "[A-Za-z]" nil t)
1066 (replace-match "a" t t)))
1068 (goto-char (point-min))
1069 (while (re-search-forward "[0-9]+" nil t)
1072 (string-to-int (buffer-substring
1073 (match-beginning 0) (match-end 0))))))
1074 (setq string (buffer-substring 1 (point-max)))
1075 (setcar (car string-list) string)
1076 (setq string-list (cdr string-list))))
1080 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1081 ;; function. It takes a list of articles to be grabbed and a function
1082 ;; to apply to each article.
1084 ;; The function to be called should take two parameters. The first
1085 ;; parameter is the article buffer. The function should leave the
1086 ;; result, if any, in this buffer. Most treatment functions will just
1087 ;; generate files...
1089 ;; The second parameter is the state of the list of articles, and can
1090 ;; have four values: `first', `middle', `last' and `first-and-last'.
1092 ;; The function should return a list. The list may contain the
1093 ;; following symbols:
1094 ;; `error' if an error occurred
1095 ;; `begin' if the beginning of an encoded file has been received
1096 ;; If the list returned contains a `begin', the first element of
1097 ;; the list *must* be a string with the file name of the decoded
1099 ;; `end' if the the end of an encoded file has been received
1100 ;; `middle' if the article was a body part of an encoded file
1101 ;; `wrong-type' if the article was not a part of an encoded file
1102 ;; `ok', which can be used everything is ok
1104 (defvar gnus-uu-has-been-grabbed nil)
1106 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1108 (if (not (and gnus-uu-has-been-grabbed
1109 gnus-uu-unmark-articles-not-decoded))
1111 (if dont-unmark-last-article
1113 (setq art (car gnus-uu-has-been-grabbed))
1114 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
1115 (while gnus-uu-has-been-grabbed
1116 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1117 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1118 (if dont-unmark-last-article
1119 (setq gnus-uu-has-been-grabbed (list art))))))
1121 ;; This function takes a list of articles and a function to apply to
1122 ;; each article grabbed.
1124 ;; This function returns a list of files decoded if the grabbing and
1125 ;; the process-function has been successful and nil otherwise.
1126 (defun gnus-uu-grab-articles
1127 (articles process-function &optional sloppy limit no-errors)
1128 (let ((state 'first)
1129 has-been-begin article result-file result-files process-state
1132 (while (and articles
1133 (not (memq 'error process-state))
1135 (not (memq 'end process-state))))
1137 (setq article (pop articles))
1138 (push article article-series)
1141 (if (eq state 'first)
1142 (setq state 'first-and-last)
1143 (setq state 'last)))
1145 (let ((part (gnus-uu-part-number article)))
1146 (message "Getting article %d%s..."
1147 article (if (string= part "") "" (concat ", " part))))
1148 (gnus-summary-display-article article)
1150 ;; Push the article to the processing function.
1152 (set-buffer gnus-original-article-buffer)
1153 (let ((buffer-read-only nil))
1155 (set-buffer gnus-summary-buffer)
1157 (funcall process-function
1158 gnus-original-article-buffer state)))))
1160 (gnus-summary-remove-process-mark article)
1162 ;; If this is the beginning of a decoded file, we push it
1164 (when (or (memq 'begin process-state)
1165 (and (or (eq state 'first)
1166 (eq state 'first-and-last))
1167 (memq 'ok process-state)))
1169 ;; If there is a `result-file' here, that means that the
1170 ;; file was unsuccessfully decoded, so we delete it.
1171 (when (and result-file
1172 (file-exists-p result-file))
1173 (delete-file result-file)))
1174 (when (memq 'begin process-state)
1175 (setq result-file (car process-state)))
1176 (setq has-been-begin t))
1178 ;; Check whether we have decoded one complete file.
1179 (when (memq 'end process-state)
1180 (setq article-series nil)
1181 (setq has-been-begin nil)
1182 (push (list (cons 'name result-file)
1183 (cons 'article article))
1185 ;; Allow user-defined functions to be run on this file.
1186 (when gnus-uu-grabbed-file-functions
1187 (let ((funcs gnus-uu-grabbed-file-functions))
1188 (unless (listp funcs)
1189 (setq funcs (list funcs)))
1191 (funcall (pop funcs) result-file))))
1192 ;; Check whether we have decoded enough articles.
1193 (and limit (= (length result-files) limit)
1194 (setq articles nil)))
1196 ;; If this is the last article to be decoded, and
1197 ;; we still haven't reached the end, then we delete
1198 ;; the partially decoded file.
1199 (and (or (eq state 'last) (eq state 'first-and-last))
1200 (not (memq 'end process-state))
1202 (file-exists-p result-file)
1203 (delete-file result-file))
1205 ;; If this was a file of the wrong sort, then
1206 (when (and (or (memq 'wrong-type process-state)
1207 (memq 'error process-state))
1208 gnus-uu-unmark-articles-not-decoded)
1209 (gnus-summary-tick-article article t))
1211 ;; Set the new series state.
1212 (if (and (not has-been-begin)
1214 (or (memq 'end process-state)
1215 (memq 'middle process-state)))
1217 (setq process-state (list 'error))
1218 (message "No begin part at the beginning")
1220 (setq state 'middle)))
1222 ;; When there are no result-files, then something must be wrong.
1226 ((not has-been-begin)
1227 (message "Wrong type file"))
1228 ((memq 'error process-state)
1229 (message "An error occurred during decoding"))
1230 ((not (or (memq 'ok process-state)
1231 (memq 'end process-state)))
1232 (message "End of articles reached before end of file")))
1233 ;; Make unsuccessfully decoded articles unread.
1234 (when gnus-uu-unmark-articles-not-decoded
1235 (while article-series
1236 (gnus-summary-tick-article (pop article-series) t))))
1240 (defun gnus-uu-grab-view (file)
1241 "View FILE using the gnus-uu methods."
1242 (let ((action (gnus-uu-get-action file)))
1243 (gnus-execute-command
1244 (if (string-match "%" action)
1245 (format action file)
1246 (concat action " " file))
1247 (eq gnus-view-pseudos 'not-confirm))))
1249 (defun gnus-uu-grab-move (file)
1250 "Move FILE to somewhere."
1251 (when gnus-uu-default-dir
1252 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1253 (file-name-nondirectory file))))
1254 (rename-file file to-file)
1255 (unless (file-exists-p file)
1256 (make-symbolic-link to-file file)))))
1258 (defun gnus-uu-part-number (article)
1259 (let ((subject (mail-header-subject (gnus-summary-article-header article))))
1260 (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1262 (substring subject (match-beginning 0) (match-end 0))
1265 (defun gnus-uu-uudecode-sentinel (process event)
1266 (delete-process (get-process process)))
1268 (defun gnus-uu-uustrip-article (process-buffer in-state)
1269 ;; Uudecodes a file asynchronously.
1270 (let ((state (list 'ok))
1271 (process-connection-type nil)
1272 start-char pst name-beg name-end)
1274 (set-buffer process-buffer)
1275 (let ((case-fold-search nil)
1276 (buffer-read-only nil))
1278 (goto-char (point-min))
1280 (if gnus-uu-kill-carriage-return
1282 (while (search-forward "\r" nil t)
1283 (delete-backward-char 1))
1284 (goto-char (point-min))))
1286 (if (not (re-search-forward gnus-uu-begin-string nil t))
1287 (if (not (re-search-forward gnus-uu-body-line nil t))
1288 (setq state (list 'wrong-type))))
1290 (if (memq 'wrong-type state)
1293 (setq start-char (point))
1295 (if (looking-at gnus-uu-begin-string)
1297 (setq name-end (match-end 1)
1298 name-beg (match-beginning 1))
1299 ;; Remove any non gnus-uu-body-line right after start.
1301 (or (looking-at gnus-uu-body-line)
1304 ;; Replace any slashes and spaces in file names before decoding
1305 (goto-char name-beg)
1306 (while (re-search-forward "/" name-end t)
1307 (replace-match ","))
1308 (goto-char name-beg)
1309 (while (re-search-forward " " name-end t)
1310 (replace-match "_"))
1311 (goto-char name-beg)
1312 (if (re-search-forward "_*$" name-end t)
1315 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1316 (and gnus-uu-uudecode-process
1317 (setq pst (process-status
1318 (or gnus-uu-uudecode-process "nevair")))
1319 (if (or (eq pst 'stop) (eq pst 'run))
1321 (delete-process gnus-uu-uudecode-process)
1322 (gnus-uu-unmark-list-of-grabbed t))))
1323 (if (get-process "*uudecode*")
1324 (delete-process "*uudecode*"))
1325 (setq gnus-uu-uudecode-process
1328 (get-buffer-create gnus-uu-output-buffer-name)
1330 (format "cd %s ; uudecode" gnus-uu-work-dir)))
1331 (set-process-sentinel
1332 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1333 (setq state (list 'begin))
1334 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1335 (setq state (list 'middle)))
1337 (goto-char (point-max))
1340 (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1343 (if (looking-at gnus-uu-end-string)
1344 (setq state (cons 'end state)))
1347 (and gnus-uu-uudecode-process
1348 (setq pst (process-status
1349 (or gnus-uu-uudecode-process "nevair")))
1350 (if (or (eq pst 'run) (eq pst 'stop))
1352 (if gnus-uu-correct-stripped-uucode
1354 (gnus-uu-check-correct-stripped-uucode
1356 (goto-char (point-max))
1358 (concat gnus-uu-body-line "\\|"
1364 (process-send-region gnus-uu-uudecode-process
1368 (delete-process gnus-uu-uudecode-process)
1369 (message "gnus-uu: Couldn't uudecode")
1371 (setq state (list 'wrong-type)))))
1373 (if (memq 'end state)
1374 (accept-process-output gnus-uu-uudecode-process)))
1375 (setq state (list 'wrong-type))))
1376 (if (not gnus-uu-uudecode-process)
1377 (setq state (list 'wrong-type)))))
1379 (if (memq 'begin state)
1380 (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1383 ;; This function is used by `gnus-uu-grab-articles' to treat
1384 ;; a shared article.
1385 (defun gnus-uu-unshar-article (process-buffer in-state)
1386 (let ((state (list 'ok))
1389 (set-buffer process-buffer)
1390 (goto-char (point-min))
1391 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1392 (setq state (list 'wrong-type))
1394 (setq start-char (point))
1395 (call-process-region
1396 start-char (point-max) "sh" nil
1397 (get-buffer-create gnus-uu-output-buffer-name) nil
1398 "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1401 ;; Returns the name of what the shar file is going to unpack.
1402 (defun gnus-uu-find-name-in-shar ()
1403 (let ((oldpoint (point))
1405 (goto-char (point-min))
1406 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1407 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1408 (goto-char oldpoint)
1411 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1412 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1413 ;; found, or the name of the command to run if such a rule is found.
1414 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1415 (let ((action-list (copy-sequence file-action-list))
1416 (case-fold-search t)
1421 (and gnus-uu-ignore-files-by-name
1422 (string-match gnus-uu-ignore-files-by-name file-name)))
1424 (and gnus-uu-ignore-files-by-type
1425 (string-match gnus-uu-ignore-files-by-type
1426 (or (gnus-uu-choose-action
1427 file-name gnus-uu-ext-to-mime-list t)
1429 (while (not (or (eq action-list ()) action))
1430 (setq rule (car action-list))
1431 (setq action-list (cdr action-list))
1432 (if (string-match (car rule) file-name)
1433 (setq action (car (cdr rule))))))
1436 (defun gnus-uu-treat-archive (file-path)
1437 ;; Unpacks an archive. Returns t if unpacking is successful.
1438 (let ((did-unpack t)
1440 (setq action (gnus-uu-choose-action
1441 file-path (append gnus-uu-user-archive-rules
1442 (if gnus-uu-ignore-default-archive-rules
1444 gnus-uu-default-archive-rules))))
1446 (if (not action) (error "No unpackers for the file %s" file-path))
1448 (string-match "/[^/]*$" file-path)
1449 (setq dir (substring file-path 0 (match-beginning 0)))
1451 (if (member action gnus-uu-destructive-archivers)
1452 (copy-file file-path (concat file-path "~") t))
1454 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1457 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1460 (message "Unpacking: %s..." (gnus-uu-command action file-path))
1462 (if (= 0 (call-process "sh" nil
1463 (get-buffer-create gnus-uu-output-buffer-name)
1466 (message "Error during unpacking of archive")
1467 (setq did-unpack nil))
1469 (if (member action gnus-uu-destructive-archivers)
1470 (rename-file (concat file-path "~") file-path t))
1474 (defun gnus-uu-dir-files (dir)
1475 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1478 (if (file-directory-p (setq file (car dirs)))
1479 (setq files (append files (gnus-uu-dir-files file)))
1480 (setq files (cons file files)))
1481 (setq dirs (cdr dirs)))
1484 (defun gnus-uu-unpack-files (files &optional ignore)
1485 ;; Go through FILES and look for files to unpack.
1486 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1488 file did-unpack file-entry)
1489 (gnus-uu-add-file totfiles)
1491 (setq file (cdr (setq file-entry (assq 'name (car files)))))
1492 (if (and (not (member file ignore))
1493 (equal (gnus-uu-get-action (file-name-nondirectory file))
1496 (setq did-unpack (cons file did-unpack))
1497 (or (gnus-uu-treat-archive file)
1498 (message "Error during unpacking of %s" file))
1499 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1501 (gnus-uu-add-file newfiles)
1503 (or (member (car nfiles) totfiles)
1504 (setq ofiles (cons (list (cons 'name (car nfiles))
1505 (cons 'original file))
1507 (setq nfiles (cdr nfiles)))
1508 (setq totfiles newfiles))))
1509 (setq files (cdr files)))
1511 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1514 (defun gnus-uu-ls-r (dir)
1515 (let* ((files (gnus-uu-directory-files dir t))
1518 (if (file-directory-p (car files))
1520 (setq ofiles (delete (car files) ofiles))
1521 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1522 (setq files (cdr files)))
1527 (defun gnus-uu-directory-files (dir &optional full)
1528 (let (files out file)
1529 (setq files (directory-files dir full))
1531 (setq file (car files))
1532 (setq files (cdr files))
1533 (or (string-match "/\\.\\.?$" file)
1534 (setq out (cons file out))))
1535 (setq out (nreverse out))
1538 (defun gnus-uu-check-correct-stripped-uucode (start end)
1539 (let (found beg length)
1540 (if (not gnus-uu-correct-stripped-uucode)
1544 (if (re-search-forward " \\|`" end t)
1549 (if (looking-at "\n") (replace-match ""))
1553 (if (looking-at (concat gnus-uu-begin-string "\\|"
1554 gnus-uu-end-string))
1561 (setq length (- (point) beg))))
1566 (if (not (= length (- (point) beg)))
1567 (insert (make-string (- length (- (point) beg)) ? ))))
1568 (forward-line 1))))))
1570 (defvar gnus-uu-tmp-alist nil)
1572 (defun gnus-uu-initialize (&optional scan)
1575 (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1576 (if (file-exists-p (cdr entry))
1577 (setq gnus-uu-work-dir (cdr entry))
1578 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1581 (setq gnus-uu-tmp-dir (file-name-as-directory
1582 (expand-file-name gnus-uu-tmp-dir)))
1583 (if (not (file-directory-p gnus-uu-tmp-dir))
1584 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1585 (if (not (file-writable-p gnus-uu-tmp-dir))
1586 (error "Temp directory %s can't be written to"
1589 (setq gnus-uu-work-dir
1590 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1591 (gnus-uu-add-file gnus-uu-work-dir)
1592 (if (not (file-directory-p gnus-uu-work-dir))
1593 (gnus-make-directory gnus-uu-work-dir))
1594 (set-file-modes gnus-uu-work-dir 448)
1595 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1596 (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1597 gnus-uu-tmp-alist)))))
1600 ;; Kills the temporary uu buffers, kills any processes, etc.
1601 (defun gnus-uu-clean-up ()
1603 (and gnus-uu-uudecode-process
1604 (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1605 (if (or (eq pst 'stop) (eq pst 'run))
1606 (delete-process gnus-uu-uudecode-process)))
1607 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1608 (kill-buffer buf))))
1610 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1611 ;; hasn't been deleted, if, for instance, the user terminated decoding
1613 (defun gnus-uu-check-for-generated-files ()
1615 (while gnus-uu-generated-file-list
1616 (setq file (car gnus-uu-generated-file-list))
1617 (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1618 (if (not (string-match "/\\.[\\.]?$" file))
1620 (if (file-directory-p file)
1621 (setq dirs (cons file dirs))
1622 (if (file-exists-p file)
1623 (delete-file file))))))
1624 (setq dirs (nreverse dirs))
1626 (setq file (car dirs))
1627 (setq dirs (cdr dirs))
1628 (if (file-directory-p file)
1629 (if (string-match "/$" file)
1630 (delete-directory (substring file 0 (match-beginning 0)))
1631 (delete-directory file))))))
1633 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1634 ;; still exists upon exiting the newsgroup).
1635 (defun gnus-uu-add-file (file)
1637 (setq gnus-uu-generated-file-list
1638 (cons file gnus-uu-generated-file-list))
1639 (setq gnus-uu-generated-file-list
1640 (append file gnus-uu-generated-file-list))))
1642 ;; Inputs an action and a file and returns a full command, putting
1643 ;; quotes round the file name and escaping any quotes in the file name.
1644 (defun gnus-uu-command (action file)
1646 (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1649 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1650 (substring file (match-beginning 0) (match-end 0))))
1651 (setq file (substring file (1+ (match-beginning 0))))))
1652 (setq ofile (concat "\"" ofile file "\""))
1653 (if (string-match "%s" action)
1654 (format action ofile)
1655 (concat action " " ofile))))
1660 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1661 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1666 ;;; uuencoded posting
1672 ;; Any function that is to be used as and encoding method will take two
1673 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1674 ;; and "spiral.jpg", respectively.) The function should return nil if
1675 ;; the encoding wasn't successful.
1676 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1677 "Function used for encoding binary files.
1678 There are three functions supplied with gnus-uu for encoding files:
1679 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1680 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1681 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1682 uuencode and adds MIME headers.")
1684 (defvar gnus-uu-post-include-before-composing nil
1685 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1686 If this variable is t, you can either include an encoded file with
1687 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1689 (defvar gnus-uu-post-length 990
1690 "Maximum length of an article.
1691 The encoded file will be split into how many articles it takes to
1692 post the entire file.")
1694 (defvar gnus-uu-post-threaded nil
1695 "Non-nil means that gnus-uu will post the encoded file in a thread.
1696 This may not be smart, as no other decoder I have seen are able to
1697 follow threads when collecting uuencoded articles. (Well, I have seen
1698 one package that does that - gnus-uu, but somehow, I don't think that
1699 counts...) Default is nil.")
1701 (defvar gnus-uu-post-separate-description t
1702 "Non-nil means that the description will be posted in a separate article.
1703 The first article will typically be numbered (0/x). If this variable
1704 is nil, the description the user enters will be included at the
1705 beginning of the first article, which will be numbered (1/x). Default
1708 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1709 (defvar gnus-uu-post-message-id nil)
1710 (defvar gnus-uu-post-inserted-file-name nil)
1711 (defvar gnus-uu-winconf-post-news nil)
1713 (defun gnus-uu-post-news ()
1714 "Compose an article and post an encoded file."
1716 (setq gnus-uu-post-inserted-file-name nil)
1717 (setq gnus-uu-winconf-post-news (current-window-configuration))
1719 (gnus-summary-post-news)
1721 (use-local-map (copy-keymap (current-local-map)))
1722 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1723 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1724 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1725 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1727 (if gnus-uu-post-include-before-composing
1728 (save-excursion (setq gnus-uu-post-inserted-file-name
1729 (gnus-uu-post-insert-binary)))))
1731 (defun gnus-uu-post-insert-binary-in-article ()
1732 "Inserts an encoded file in the buffer.
1733 The user will be asked for a file name."
1735 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1736 (error "Not in post-news buffer"))
1738 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1740 ;; Encodes with uuencode and substitutes all spaces with backticks.
1741 (defun gnus-uu-post-encode-uuencode (path file-name)
1742 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1744 (goto-char (point-min))
1746 (while (re-search-forward " " nil t)
1747 (replace-match "`"))
1750 ;; Encodes with uuencode and adds MIME headers.
1751 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1752 (if (gnus-uu-post-encode-uuencode path file-name)
1754 (gnus-uu-post-make-mime file-name "x-uue")
1757 ;; Encodes with base64 and adds MIME headers
1758 (defun gnus-uu-post-encode-mime (path file-name)
1759 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1761 (gnus-uu-post-make-mime file-name "base64")
1764 ;; Adds MIME headers.
1765 (defun gnus-uu-post-make-mime (file-name encoding)
1766 (goto-char (point-min))
1767 (insert (format "Content-Type: %s; name=\"%s\"\n"
1768 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1770 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1772 (set-buffer gnus-post-news-buffer)
1773 (goto-char (point-min))
1774 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1776 (narrow-to-region 1 (point))
1777 (or (mail-fetch-field "mime-version")
1780 (insert "MIME-Version: 1.0\n")))
1783 ;; Encodes a file PATH with COMMAND, leaving the result in the
1785 (defun gnus-uu-post-encode-file (command path file-name)
1786 (= 0 (call-process "sh" nil t nil "-c"
1787 (format "%s %s %s" command path file-name))))
1789 (defun gnus-uu-post-news-inews ()
1790 "Posts the composed news article and encoded file.
1791 If no file has been included, the user will be asked for a file."
1793 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1794 (error "Not in post news buffer"))
1798 (if gnus-uu-post-inserted-file-name
1799 (setq file-name gnus-uu-post-inserted-file-name)
1800 (setq file-name (gnus-uu-post-insert-binary)))
1802 (if gnus-uu-post-threaded
1803 (let ((gnus-required-headers
1804 (if (memq 'Message-ID gnus-required-headers)
1805 gnus-required-headers
1806 (cons 'Message-ID gnus-required-headers)))
1807 gnus-inews-article-hook)
1809 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1810 gnus-inews-article-hook
1811 (list gnus-inews-article-hook)))
1812 (setq gnus-inews-article-hook
1816 (goto-char (point-min))
1817 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1818 (setq gnus-uu-post-message-id
1820 (match-beginning 1) (match-end 1)))
1821 (setq gnus-uu-post-message-id nil))))
1822 gnus-inews-article-hook))
1823 (gnus-uu-post-encoded file-name t))
1824 (gnus-uu-post-encoded file-name nil)))
1825 (setq gnus-uu-post-inserted-file-name nil)
1826 (and gnus-uu-winconf-post-news
1827 (set-window-configuration gnus-uu-winconf-post-news)))
1829 ;; Asks for a file to encode, encodes it and inserts the result in
1830 ;; the current buffer. Returns the file name the user gave.
1831 (defun gnus-uu-post-insert-binary ()
1832 (let ((uuencode-buffer-name "*uuencode buffer*")
1833 file-path uubuf file-name)
1835 (setq file-path (read-file-name
1836 "What file do you want to encode? "))
1837 (if (not (file-exists-p file-path))
1838 (error "%s: No such file" file-path))
1840 (goto-char (point-max))
1841 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1843 (if (string-match "^~/" file-path)
1844 (setq file-path (concat "$HOME" (substring file-path 1))))
1845 (if (string-match "/[^/]*$" file-path)
1846 (setq file-name (substring file-path (1+ (match-beginning 0))))
1847 (setq file-name file-path))
1851 (set-buffer (setq uubuf
1852 (get-buffer-create uuencode-buffer-name)))
1854 (funcall gnus-uu-post-encode-method file-path file-name))
1855 (insert-buffer-substring uubuf)
1856 (error "Encoding unsuccessful"))
1857 (kill-buffer uubuf))
1860 ;; Posts the article and all of the encoded file.
1861 (defun gnus-uu-post-encoded (file-name &optional threaded)
1862 (let ((send-buffer-name "*uuencode send buffer*")
1863 (encoded-buffer-name "*encoded buffer*")
1864 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1865 (separator (concat mail-header-separator "\n\n"))
1866 uubuf length parts header i end beg
1867 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1869 (setq post-buf (current-buffer))
1871 (goto-char (point-min))
1872 (if (not (re-search-forward
1873 (if gnus-uu-post-separate-description
1874 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1876 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1877 (error "Internal error: No binary/header separator"))
1880 (setq beg-binary (point))
1881 (setq end-binary (point-max))
1884 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1886 (insert-buffer-substring post-buf beg-binary end-binary)
1887 (goto-char (point-min))
1888 (setq length (count-lines 1 (point-max)))
1889 (setq parts (/ length gnus-uu-post-length))
1890 (if (not (< (% length gnus-uu-post-length) 4))
1891 (setq parts (1+ parts))))
1893 (if gnus-uu-post-separate-description
1895 (kill-region (point) (point-max))
1897 (goto-char (point-min))
1899 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1901 (setq header (buffer-substring 1 (point)))
1903 (goto-char (point-min))
1904 (if (not gnus-uu-post-separate-description)
1906 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1909 (insert (format " (0/%d)" parts))))
1915 (while (not (> i parts))
1916 (set-buffer (get-buffer-create send-buffer-name))
1919 (if (and threaded gnus-uu-post-message-id)
1920 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1923 (- 62 (length (format top-string "" file-name i parts ""))))
1924 (if (> 1 (setq minlen (/ whole-len 2)))
1929 (make-string minlen ?-)
1932 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1934 (goto-char (point-min))
1935 (if (not (re-search-forward "^Subject: " nil t))
1940 (insert (format " (%d/%d)" i parts)))
1941 (if (or (and (= i 2) gnus-uu-post-separate-description)
1942 (and (= i 1) (not gnus-uu-post-separate-description)))
1943 (replace-match "Subject: Re: "))))
1945 (goto-char (point-max))
1950 (goto-char (point-max))
1951 (forward-line gnus-uu-post-length))
1952 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1955 (insert-buffer-substring uubuf beg end)
1960 (goto-char (point-min))
1962 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1965 (if (re-search-forward
1966 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1975 (and (setq buf (get-buffer send-buffer-name))
1977 (and (setq buf (get-buffer encoded-buffer-name))
1980 (if (not gnus-uu-post-separate-description)
1982 (set-buffer-modified-p nil)
1983 (and (fboundp 'bury-buffer) (bury-buffer))))))
1987 ;; gnus-uu.el ends here