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>
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 (eval-when-compile (require 'cl))
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 variable 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 user 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-grabbed-file-functions nil
196 "*Functions run on each file after successful decoding.
197 They will be called with the name of the file as the argument.
198 Likely functions you can use in this list are `gnus-uu-grab-view'
199 and `gnus-uu-grab-move'.")
201 (defvar gnus-uu-ignore-default-archive-rules nil
202 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
203 Only the user unpacking commands will be consulted. Default is nil.")
205 (defvar gnus-uu-kill-carriage-return t
206 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
209 (defvar gnus-uu-view-with-metamail nil
210 "*Non-nil means that files will be viewed with metamail.
211 The gnus-uu viewing functions will be ignored and gnus-uu will try
212 to guess at a content-type based on file name suffixes. Default
215 (defvar gnus-uu-unmark-articles-not-decoded nil
216 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
219 (defvar gnus-uu-correct-stripped-uucode nil
220 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
223 (defvar gnus-uu-save-in-digest nil
224 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
225 If this variable is nil, gnus-uu will just save everything in a
226 file without any embellishments. The digesting almost conforms to RFC1153 -
227 no easy way to specify any meaningful volume and issue numbers were found,
228 so I simply dropped them.")
230 (defvar gnus-uu-digest-headers
231 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
232 "^Summary:" "^References:")
233 "*List of regexps to match headers included in digested messages.
234 The headers will be included in the sequence they are matched.")
236 (defvar gnus-uu-save-separate-articles nil
237 "*Non-nil means that gnus-uu will save articles in separate files.")
239 ;; Internal variables
241 (defvar gnus-uu-saved-article-name nil)
243 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
244 (defconst gnus-uu-end-string "^end[ \t]*$")
246 (defconst gnus-uu-body-line "^M")
248 (while (> (setq i (1- i)) 0)
249 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
250 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
252 ;"^M.............................................................?$"
254 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
256 (defvar gnus-uu-shar-file-name nil)
257 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
259 (defconst gnus-uu-postscript-begin-string "^%!PS-")
260 (defconst gnus-uu-postscript-end-string "^%%EOF$")
262 (defvar gnus-uu-file-name nil)
263 (defconst gnus-uu-uudecode-process nil)
264 (defvar gnus-uu-binhex-article-name nil)
266 (defvar gnus-uu-generated-file-list nil)
267 (defvar gnus-uu-work-dir nil)
269 (defconst gnus-uu-output-buffer-name " *Gnus UU Output*")
271 (defvar gnus-uu-default-dir default-directory)
272 (defvar gnus-uu-digest-from-subject nil)
276 (defvar gnus-uu-extract-map nil)
277 (defvar gnus-uu-extract-view-map nil)
278 (defvar gnus-uu-mark-map nil)
280 (define-prefix-command 'gnus-uu-mark-map)
281 (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
282 (define-key gnus-uu-mark-map "p" 'gnus-summary-mark-as-processable)
283 (define-key gnus-uu-mark-map "u" 'gnus-summary-unmark-as-processable)
284 (define-key gnus-uu-mark-map "U" 'gnus-summary-unmark-all-processable)
285 (define-key gnus-uu-mark-map "s" 'gnus-uu-mark-series)
286 (define-key gnus-uu-mark-map "r" 'gnus-uu-mark-region)
287 (define-key gnus-uu-mark-map "R" 'gnus-uu-mark-by-regexp)
288 (define-key gnus-uu-mark-map "t" 'gnus-uu-mark-thread)
289 (define-key gnus-uu-mark-map "T" 'gnus-uu-unmark-thread)
290 (define-key gnus-uu-mark-map "a" 'gnus-uu-mark-all)
291 (define-key gnus-uu-mark-map "b" 'gnus-uu-mark-buffer)
292 (define-key gnus-uu-mark-map "S" 'gnus-uu-mark-sparse)
294 (define-prefix-command 'gnus-uu-extract-map)
295 (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
296 ;;(define-key gnus-uu-extract-map "x" 'gnus-uu-extract-any)
297 ;;(define-key gnus-uu-extract-map "m" 'gnus-uu-extract-mime)
298 (define-key gnus-uu-extract-map "u" 'gnus-uu-decode-uu)
299 (define-key gnus-uu-extract-map "U" 'gnus-uu-decode-uu-and-save)
300 (define-key gnus-uu-extract-map "s" 'gnus-uu-decode-unshar)
301 (define-key gnus-uu-extract-map "S" 'gnus-uu-decode-unshar-and-save)
302 (define-key gnus-uu-extract-map "o" 'gnus-uu-decode-save)
303 (define-key gnus-uu-extract-map "O" 'gnus-uu-decode-save)
304 (define-key gnus-uu-extract-map "b" 'gnus-uu-decode-binhex)
305 (define-key gnus-uu-extract-map "B" 'gnus-uu-decode-binhex)
306 (define-key gnus-uu-extract-map "p" 'gnus-uu-decode-postscript)
307 (define-key gnus-uu-extract-map "P" 'gnus-uu-decode-postscript-and-save)
309 (define-prefix-command 'gnus-uu-extract-view-map)
310 (define-key gnus-uu-extract-map "v" 'gnus-uu-extract-view-map)
311 (define-key gnus-uu-extract-view-map "u" 'gnus-uu-decode-uu-view)
312 (define-key gnus-uu-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
313 (define-key gnus-uu-extract-view-map "s" 'gnus-uu-decode-unshar-view)
314 (define-key gnus-uu-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
315 (define-key gnus-uu-extract-view-map "o" 'gnus-uu-decode-save-view)
316 (define-key gnus-uu-extract-view-map "O" 'gnus-uu-decode-save-view)
317 (define-key gnus-uu-extract-view-map "b" 'gnus-uu-decode-binhex-view)
318 (define-key gnus-uu-extract-view-map "B" 'gnus-uu-decode-binhex-view)
319 (define-key gnus-uu-extract-view-map "p" 'gnus-uu-decode-postscript-view)
320 (define-key gnus-uu-extract-view-map "P" 'gnus-uu-decode-postscript-and-save-view)
326 (defun gnus-uu-decode-uu (n)
327 "Uudecodes the current article."
329 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
331 (defun gnus-uu-decode-uu-and-save (n dir)
332 "Decodes and saves the resulting file."
334 (list current-prefix-arg
335 (file-name-as-directory
336 (read-file-name "Uudecode and save in dir: "
338 gnus-uu-default-dir t))))
339 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir))
341 (defun gnus-uu-decode-unshar (n)
342 "Unshars the current article."
344 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan))
346 (defun gnus-uu-decode-unshar-and-save (n dir)
347 "Unshars and saves the current article."
349 (list current-prefix-arg
350 (file-name-as-directory
351 (read-file-name "Unshar and save in dir: "
353 gnus-uu-default-dir t))))
354 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan))
356 (defun gnus-uu-decode-save (n file)
357 "Saves the current article."
359 (list current-prefix-arg
361 (if gnus-uu-save-separate-articles
362 "Save articles is dir: "
363 "Save articles in file: ")
365 gnus-uu-default-dir)))
366 (setq gnus-uu-saved-article-name file)
367 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t)
368 (setq gnus-uu-generated-file-list
369 (delete file gnus-uu-generated-file-list)))
371 (defun gnus-uu-decode-binhex (n dir)
372 "Unbinhexes the current article."
374 (list current-prefix-arg
375 (file-name-as-directory
376 (read-file-name "Unbinhex and save in dir: "
378 gnus-uu-default-dir))))
379 (setq gnus-uu-binhex-article-name
380 (make-temp-name (concat gnus-uu-work-dir "binhex")))
381 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
383 (defun gnus-uu-decode-uu-view (n)
384 "Uudecodes and views the current article."
386 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
387 (gnus-uu-decode-uu n)))
389 (defun gnus-uu-decode-uu-and-save-view (n dir)
390 "Decodes, views and saves the resulting file."
392 (list current-prefix-arg
393 (read-file-name "Uudecode, view and save in dir: "
395 gnus-uu-default-dir t)))
396 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
397 (gnus-uu-decode-uu-and-save n dir)))
399 (defun gnus-uu-decode-unshar-view (n)
400 "Unshars and views the current article."
402 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
403 (gnus-uu-decode-unshar n)))
405 (defun gnus-uu-decode-unshar-and-save-view (n dir)
406 "Unshars and saves the current article."
408 (list current-prefix-arg
409 (read-file-name "Unshar, view and save in dir: "
411 gnus-uu-default-dir t)))
412 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
413 (gnus-uu-decode-unshar-and-save n dir)))
415 (defun gnus-uu-decode-save-view (n file)
416 "Saves and views the current article."
418 (list current-prefix-arg
419 (read-file-name (if gnus-uu-save-separate-articles
420 "Save articles is dir: "
421 "Save articles in file: ")
422 gnus-uu-default-dir gnus-uu-default-dir)))
423 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
424 (gnus-uu-decode-save n file)))
426 (defun gnus-uu-decode-binhex-view (n file)
427 "Unbinhexes and views the current article."
429 (list current-prefix-arg
430 (read-file-name "Unbinhex, view and save in dir: "
431 gnus-uu-default-dir gnus-uu-default-dir)))
432 (setq gnus-uu-binhex-article-name
433 (make-temp-name (concat gnus-uu-work-dir "binhex")))
434 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
435 (gnus-uu-decode-binhex n file)))
438 ;; Digest and forward articles
440 (defun gnus-uu-digest-mail-forward (n &optional post)
441 "Digests and forwards all articles in this series."
443 (let ((gnus-uu-save-in-digest t)
444 (file (make-temp-name (concat gnus-uu-tmp-dir "forward")))
446 (setq gnus-uu-digest-from-subject nil)
447 (gnus-uu-decode-save n file)
448 (gnus-uu-add-file file)
449 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
450 (gnus-add-current-to-buffer-list)
452 (delete-other-windows)
454 (let ((fs gnus-uu-digest-from-subject))
457 (setq from (car (car fs))
458 subject (gnus-simplify-subject-fuzzy (cdr (car fs)))
460 (while (and fs (or from subject))
462 (or (string= from (car (car fs)))
465 (or (string= (gnus-simplify-subject-fuzzy (cdr (car fs)))
469 (or subject (setq subject "Digested Articles"))
470 (or from (setq from "Various")))
471 (goto-char (point-min))
472 (and (re-search-forward "^Subject: ")
474 (delete-region (point) (gnus-point-at-eol))
476 (goto-char (point-min))
477 (and (re-search-forward "^From: ")
479 (delete-region (point) (gnus-point-at-eol))
482 (gnus-forward-using-post)
486 (setq gnus-uu-digest-from-subject nil)))
488 (defun gnus-uu-digest-post-forward (n)
489 "Digest and forward to a newsgroup."
491 (gnus-uu-digest-mail-forward n t))
495 (defun gnus-uu-mark-by-regexp (regexp)
496 "Ask for a regular expression and set the process mark on all articles that match."
497 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
498 (gnus-set-global-variables)
499 (let ((articles (gnus-uu-find-articles-matching regexp)))
501 (gnus-summary-set-process-mark (car articles))
502 (setq articles (cdr articles)))
504 (gnus-summary-position-point))
506 (defun gnus-uu-mark-series ()
507 "Mark the current series with the process mark."
509 (gnus-set-global-variables)
510 (let ((articles (gnus-uu-find-articles-matching)))
512 (gnus-summary-set-process-mark (car articles))
513 (setq articles (cdr articles)))
515 (gnus-summary-position-point))
517 (defun gnus-uu-mark-region (beg end)
518 "Set the process mark on all articles between point and mark."
520 (gnus-set-global-variables)
523 (while (< (point) end)
524 (gnus-summary-set-process-mark (gnus-summary-article-number))
526 (gnus-summary-position-point))
528 (defun gnus-uu-mark-buffer ()
529 "Set the process mark on all articles in the buffer."
531 (gnus-uu-mark-region (point-min) (point-max)))
533 (defun gnus-uu-mark-thread ()
534 "Marks all articles downwards in this thread."
536 (gnus-set-global-variables)
537 (let ((level (gnus-summary-thread-level)))
538 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
539 (zerop (gnus-summary-next-subject 1))
540 (> (gnus-summary-thread-level) level))))
541 (gnus-summary-position-point))
543 (defun gnus-uu-unmark-thread ()
544 "Unmarks all articles downwards in this thread."
546 (gnus-set-global-variables)
547 (let ((level (gnus-summary-thread-level)))
548 (while (and (gnus-summary-remove-process-mark
549 (gnus-summary-article-number))
550 (zerop (gnus-summary-next-subject 1))
551 (> (gnus-summary-thread-level) level))))
552 (gnus-summary-position-point))
554 (defun gnus-uu-mark-sparse ()
555 "Mark all series that have some articles marked."
557 (gnus-set-global-variables)
558 (let ((marked (nreverse gnus-newsgroup-processable))
559 subject articles total headers)
560 (or marked (error "No articles marked with the process mark"))
561 (setq gnus-newsgroup-processable nil)
564 (and (setq headers (gnus-summary-article-header (car marked)))
565 (setq subject (mail-header-subject headers)
566 articles (gnus-uu-find-articles-matching
567 (gnus-uu-reginize-string subject))
568 total (nconc total articles)))
570 (gnus-summary-set-process-mark (car articles))
571 (setcdr marked (delq (car articles) (cdr marked)))
572 (setq articles (cdr articles)))
573 (setq marked (cdr marked)))
574 (setq gnus-newsgroup-processable (nreverse total)))
575 (gnus-summary-position-point)))
577 (defun gnus-uu-mark-all ()
578 "Mark all articles in \"series\" order."
580 (gnus-set-global-variables)
581 (setq gnus-newsgroup-processable nil)
583 (goto-char (point-min))
585 (while (and (not (eobp))
586 (setq number (gnus-summary-article-number)))
587 (if (not (memq number gnus-newsgroup-processable))
588 (save-excursion (gnus-uu-mark-series)))
590 (gnus-summary-position-point))
592 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
594 (defun gnus-uu-decode-postscript (n)
595 "Gets postscript of the current article."
597 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
599 (defun gnus-uu-decode-postscript-view (n)
600 "Gets and views the current article."
602 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
603 (gnus-uu-decode-postscript n)))
605 (defun gnus-uu-decode-postscript-and-save (n dir)
606 "Extracts postscript and saves the current article."
608 (list current-prefix-arg
609 (file-name-as-directory
610 (read-file-name "Save in dir: "
612 gnus-uu-default-dir t))))
613 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n dir))
616 (defun gnus-uu-decode-postscript-and-save-view (n dir)
617 "Decodes, views and saves the resulting file."
619 (list current-prefix-arg
620 (read-file-name "Where do you want to save the file(s)? "
622 gnus-uu-default-dir t)))
623 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
624 (gnus-uu-decode-postscript-and-save n dir)))
627 ;; Internal functions.
629 (defun gnus-uu-decode-with-method (method n &optional save not-insert scan)
630 (gnus-uu-initialize scan)
631 (if save (setq gnus-uu-default-dir save))
632 (let ((articles (gnus-uu-get-list-of-articles n))
634 (setq files (gnus-uu-grab-articles articles method t))
635 (let ((gnus-current-article (car articles)))
636 (and scan (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
637 (and save (gnus-uu-save-files files save))
638 (if (eq gnus-uu-do-not-unpack-archives nil)
639 (setq files (gnus-uu-unpack-files files)))
640 (gnus-uu-add-file (mapcar (lambda (file) (cdr (assq 'name file))) files))
641 (setq files (nreverse (gnus-uu-get-actions files)))
642 (or not-insert (not gnus-insert-pseudo-articles)
643 (gnus-summary-insert-pseudos files save))))
645 ;; Return a list of files in dir.
646 (defun gnus-uu-scan-directory (dir)
647 (let ((files (directory-files dir t))
650 (cond ((string-match "/\\.\\.?$" (car files)))
651 ((file-directory-p (car files))
652 (setq dirs (cons (car files) dirs)))
653 (t (setq out (cons (list (cons 'name (car files))
654 (cons 'article gnus-current-article))
656 (setq files (cdr files)))
657 (apply 'nconc out (mapcar (lambda (d) (gnus-uu-scan-directory d))
660 (defun gnus-uu-save-files (files dir)
661 (let ((len (length files))
665 (setq file (cdr (assq 'name (car files))))
668 (setq to-file (if (file-directory-p dir)
669 (concat dir (file-name-nondirectory file))
671 (and (or (not (file-exists-p to-file))
672 (gnus-y-or-n-p (format "%s exists; overwrite? "
674 (copy-file file to-file t t))))
675 (setq files (cdr files)))
676 (message "Saved %d file%s" len (if (> len 1) "s" ""))))
678 ;; Functions for saving and possibly digesting articles without
681 ;; Function called by gnus-uu-grab-articles to treat each article.
682 (defun gnus-uu-save-article (buffer in-state)
684 (gnus-uu-save-separate-articles
687 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
688 gnus-current-article))
689 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
690 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
692 ((eq in-state 'last) (list 'end))
693 (t (list 'middle)))))
694 ((not gnus-uu-save-in-digest)
697 (write-region 1 (point-max) gnus-uu-saved-article-name t)
698 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
699 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
701 ((eq in-state 'last) (list 'end))
702 (t (list 'middle)))))
704 (let ((header (gnus-summary-article-header)))
705 (setq gnus-uu-digest-from-subject
706 (cons (cons (mail-header-from header)
707 (mail-header-subject header))
708 gnus-uu-digest-from-subject)))
709 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
710 (delim (concat "^" (make-string 30 ?-) "$"))
711 beg subj headers headline sorthead body end-string state)
712 (if (or (eq in-state 'first)
713 (eq in-state 'first-and-last))
715 (setq state (list 'begin))
716 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
719 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
722 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
723 (current-time-string) name name))))
724 (if (not (eq in-state 'end))
725 (setq state (list 'middle))))
727 (set-buffer (get-buffer "*gnus-uu-body*"))
728 (goto-char (setq beg (point-max)))
732 (let (buffer-read-only)
733 (set-text-properties (point-min) (point-max) nil)
734 ;; These two are necessary for XEmacs 19.12 fascism.
735 (put-text-property (point-min) (point-max) 'invisible nil)
736 (put-text-property (point-min) (point-max) 'intangible nil))
737 (goto-char (point-min))
738 (re-search-forward "\n\n")
739 ;; Quote all 30-dash lines.
741 (while (re-search-forward delim nil t)
745 (setq body (buffer-substring (1- (point)) (point-max)))
746 (narrow-to-region (point-min) (point))
747 (if (not (setq headers gnus-uu-digest-headers))
748 (setq sorthead (buffer-substring (point-min) (point-max)))
750 (setq headline (car headers))
751 (setq headers (cdr headers))
752 (goto-char (point-min))
753 (if (re-search-forward headline nil t)
758 (or (and (re-search-forward "^[^ \t]" nil t)
760 (progn (forward-line 1) (point)))))))))
762 (insert sorthead) (goto-char (point-max))
763 (insert body) (goto-char (point-max))
764 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
766 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
768 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
770 (set-buffer (get-buffer "*gnus-uu-pre*"))
771 (insert (format " %s\n" subj))))))
772 (if (or (eq in-state 'last)
773 (eq in-state 'first-and-last))
776 (set-buffer (get-buffer "*gnus-uu-pre*"))
777 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
778 (write-region 1 (point-max) gnus-uu-saved-article-name))
780 (set-buffer (get-buffer "*gnus-uu-body*"))
781 (goto-char (point-max))
783 (concat (setq end-string (format "End of %s Digest" name))
785 (insert (concat (make-string (length end-string) ?*) "\n"))
786 (write-region 1 (point-max) gnus-uu-saved-article-name t))
787 (kill-buffer (get-buffer "*gnus-uu-pre*"))
788 (kill-buffer (get-buffer "*gnus-uu-body*"))
789 (setq state (cons 'end state))))
790 (if (memq 'begin state)
791 (cons gnus-uu-saved-article-name state)
794 ;; Binhex treatment - not very advanced.
796 (defconst gnus-uu-binhex-body-line
797 "^[^:]...............................................................$")
798 (defconst gnus-uu-binhex-begin-line
799 "^:...............................................................$")
800 (defconst gnus-uu-binhex-end-line
803 (defun gnus-uu-binhex-article (buffer in-state)
804 (let (state start-char)
808 (goto-char (point-min))
809 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
810 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
811 (setq state (list 'wrong-type))))
813 (if (memq 'wrong-type state)
816 (setq start-char (point))
817 (if (looking-at gnus-uu-binhex-begin-line)
819 (setq state (list 'begin))
820 (write-region 1 1 gnus-uu-binhex-article-name))
821 (setq state (list 'middle)))
822 (goto-char (point-max))
823 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
824 gnus-uu-binhex-end-line) nil t)
825 (if (looking-at gnus-uu-binhex-end-line)
826 (setq state (if (memq 'begin state)
831 (if (file-exists-p gnus-uu-binhex-article-name)
832 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
833 (if (memq 'begin state)
834 (cons gnus-uu-binhex-article-name state)
839 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
840 (let ((state (list 'ok))
841 start-char end-char file-name)
843 (set-buffer process-buffer)
844 (goto-char (point-min))
845 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
846 (setq state (list 'wrong-type))
848 (setq start-char (point))
849 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
850 (setq state (list 'wrong-type))
851 (setq end-char (point))
852 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
853 (insert-buffer-substring process-buffer start-char end-char)
854 (setq file-name (concat gnus-uu-work-dir
855 (cdr gnus-article-current) ".ps"))
856 (write-region (point-min) (point-max) file-name)
857 (setq state (list file-name 'begin 'end)))))
863 (defun gnus-uu-get-actions (files)
867 (setq name (cdr (assq 'name (car files))))
869 (setq action (gnus-uu-get-action name))
870 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
871 (cons 'action "file")
872 (cons 'action action))
873 (cons 'execute (if (string-match "%" action)
875 (concat action " " name))))
877 (setq files (cdr files)))
880 (defun gnus-uu-get-action (file-name)
883 (gnus-uu-choose-action
886 gnus-uu-user-view-rules
887 (if gnus-uu-ignore-default-view-rules
889 gnus-uu-default-view-rules)
890 gnus-uu-user-view-rules-end)))
891 (if (and (not (string= (or action "") "gnus-uu-archive"))
892 gnus-uu-view-with-metamail)
894 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
895 (setq action (format "metamail -d -b -c \"%s\"" action))))
899 ;; Functions for treating subjects and collecting series.
901 (defun gnus-uu-reginize-string (string)
902 ;; Takes a string and puts a \ in front of every special character;
903 ;; ignores any leading "version numbers" thingies that they use in
904 ;; the comp.binaries groups, and either replaces anything that looks
905 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
906 ;; like that, replaces the last two numbers with "[0-9]+". This, in
907 ;; my experience, should get most postings of a series.
909 (vernum "v[0-9]+[a-z][0-9]+:")
912 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
913 (buffer-disable-undo (current-buffer))
915 (insert (regexp-quote string))
918 (setq case-fold-search nil)
919 (goto-char (point-min))
920 (if (looking-at vernum)
922 (replace-match vernum t t)
923 (setq beg (length vernum))))
926 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
927 (replace-match " [0-9]+/[0-9]+")
930 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
931 (replace-match "[0-9]+ of [0-9]+")
934 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
937 (< 1 (goto-char (1- (point))))))
938 (re-search-forward "[0-9]+" nil t)
939 (replace-match "[0-9]+")
941 (setq count (1- count)))))
944 (while (re-search-forward "[ \t]+" nil t)
945 (replace-match "[ \t]*" t t))
947 (buffer-substring 1 (point-max)))))
949 (defun gnus-uu-get-list-of-articles (n)
950 ;; If N is non-nil, the article numbers of the N next articles
952 ;; If any articles have been marked as processable, they will be
954 ;; Failing that, articles that have subjects that are part of the
955 ;; same "series" as the current will be returned.
959 (let ((backward (< n 0))
963 (setq articles (cons (gnus-summary-article-number)
965 (gnus-summary-search-forward nil nil backward))
967 (nreverse articles)))
968 (gnus-newsgroup-processable
969 (reverse gnus-newsgroup-processable))
971 (gnus-uu-find-articles-matching)))))
973 (defun gnus-uu-string< (l1 l2)
974 (string< (car l1) (car l2)))
976 (defun gnus-uu-find-articles-matching
977 (&optional subject only-unread do-not-translate)
978 ;; Finds all articles that matches the regexp SUBJECT. If it is
979 ;; nil, the current article name will be used. If ONLY-UNREAD is
980 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
981 ;; non-nil, article names are not equalized before sorting.
982 (let ((subject (or subject
983 (gnus-uu-reginize-string (gnus-summary-article-subject))))
988 ;; Collect all subjects matching subject.
989 (let ((case-fold-search t)
990 (data gnus-newsgroup-data)
994 (and (not (gnus-data-pseudo-p d))
995 (or (not only-unread)
996 (= (setq mark (gnus-data-mark d))
998 (= mark gnus-ticked-mark)
999 (= mark gnus-dormant-mark))
1000 (setq subj (mail-header-subject (gnus-data-header d)))
1001 (string-match subject subj)
1002 (setq list-of-subjects
1003 (cons (cons subj (gnus-data-number d))
1004 list-of-subjects)))))
1006 ;; Expand numbers, sort, and return the list of article
1008 (mapcar (lambda (sub) (cdr sub))
1009 (sort (gnus-uu-expand-numbers
1011 (not do-not-translate))
1012 'gnus-uu-string<))))))
1014 (defun gnus-uu-expand-numbers (string-list &optional translate)
1015 ;; Takes a list of strings and "expands" all numbers in all the
1016 ;; strings. That is, this function makes all numbers equal length by
1017 ;; prepending lots of zeroes before each number. This is to ease later
1018 ;; sorting to find out what sequence the articles are supposed to be
1019 ;; decoded in. Returns the list of expanded strings.
1020 (let ((out-list string-list)
1023 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1024 (buffer-disable-undo (current-buffer))
1027 (insert (car (car string-list)))
1028 ;; Translate multiple spaces to one space.
1029 (goto-char (point-min))
1030 (while (re-search-forward "[ \t]+" nil t)
1031 (replace-match " "))
1032 ;; Translate all characters to "a".
1033 (goto-char (point-min))
1035 (while (re-search-forward "[A-Za-z]" nil t)
1036 (replace-match "a" t t)))
1038 (goto-char (point-min))
1039 (while (re-search-forward "[0-9]+" nil t)
1042 (string-to-int (buffer-substring
1043 (match-beginning 0) (match-end 0))))))
1044 (setq string (buffer-substring 1 (point-max)))
1045 (setcar (car string-list) string)
1046 (setq string-list (cdr string-list))))
1050 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1051 ;; function. It takes a list of articles to be grabbed and a function
1052 ;; to apply to each article.
1054 ;; The function to be called should take two parameters. The first
1055 ;; parameter is the article buffer. The function should leave the
1056 ;; result, if any, in this buffer. Most treatment functions will just
1057 ;; generate files...
1059 ;; The second parameter is the state of the list of articles, and can
1060 ;; have four values: `first', `middle', `last' and `first-and-last'.
1062 ;; The function should return a list. The list may contain the
1063 ;; following symbols:
1064 ;; `error' if an error occurred
1065 ;; `begin' if the beginning of an encoded file has been received
1066 ;; If the list returned contains a `begin', the first element of
1067 ;; the list *must* be a string with the file name of the decoded
1069 ;; `end' if the the end of an encoded file has been received
1070 ;; `middle' if the article was a body part of an encoded file
1071 ;; `wrong-type' if the article was not a part of an encoded file
1072 ;; `ok', which can be used everything is ok
1074 (defvar gnus-uu-has-been-grabbed nil)
1076 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1078 (if (not (and gnus-uu-has-been-grabbed
1079 gnus-uu-unmark-articles-not-decoded))
1081 (if dont-unmark-last-article
1083 (setq art (car gnus-uu-has-been-grabbed))
1084 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
1085 (while gnus-uu-has-been-grabbed
1086 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1087 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1088 (if dont-unmark-last-article
1089 (setq gnus-uu-has-been-grabbed (list art))))))
1091 ;; This function takes a list of articles and a function to apply to
1092 ;; each article grabbed.
1094 ;; This function returns a list of files decoded if the grabbing and
1095 ;; the process-function has been successful and nil otherwise.
1096 (defun gnus-uu-grab-articles
1097 (articles process-function &optional sloppy limit no-errors)
1098 (let ((state 'first)
1099 has-been-begin article result-file result-files process-state
1102 (while (and articles
1103 (not (memq 'error process-state))
1105 (not (memq 'end process-state))))
1107 (setq article (pop articles))
1108 (push article article-series)
1111 (if (eq state 'first)
1112 (setq state 'first-and-last)
1113 (setq state 'last)))
1115 (message "Getting article %d, %s" article (gnus-uu-part-number article))
1116 (gnus-summary-display-article article)
1118 ;; Push the article to the processing function.
1120 (set-buffer gnus-original-article-buffer)
1121 (let ((buffer-read-only nil))
1123 (set-buffer gnus-summary-buffer)
1125 (funcall process-function
1126 gnus-original-article-buffer state)))))
1128 (gnus-summary-remove-process-mark article)
1130 ;; If this is the beginning of a decoded file, we push it
1132 (when (or (memq 'begin process-state)
1133 (and (or (eq state 'first)
1134 (eq state 'first-and-last))
1135 (memq 'ok process-state)))
1137 ;; If there is a `result-file' here, that means that the
1138 ;; file was unsuccessfully decoded, so we delete it.
1139 (when (and result-file
1140 (file-exists-p result-file))
1141 (delete-file result-file)))
1142 (when (memq 'begin process-state)
1143 (setq result-file (car process-state)))
1144 (setq has-been-begin t))
1146 ;; Check whether we have decoded one complete file.
1147 (when (memq 'end process-state)
1148 (setq article-series nil)
1149 (setq has-been-begin nil)
1150 (push (list (cons 'name result-file)
1151 (cons 'article article))
1153 ;; Allow user-defined functions to be run on this file.
1154 (when gnus-uu-grabbed-file-functions
1155 (let ((funcs gnus-uu-grabbed-file-functions))
1156 (unless (listp funcs)
1157 (setq funcs (list funcs)))
1159 (funcall (pop funcs) result-file))))
1160 ;; Check whether we have decoded enough articles.
1161 (and limit (= (length result-files) limit)
1162 (setq articles nil)))
1164 ;; If this is the last article to be decoded, and
1165 ;; we still haven't reached the end, then we delete
1166 ;; the partially decoded file.
1167 (and (or (eq state 'last) (eq state 'first-and-last))
1168 (not (memq 'end process-state))
1170 (file-exists-p result-file)
1171 (delete-file result-file))
1173 ;; If this was a file of the wrong sort, then
1174 (when (and (or (memq 'wrong-type process-state)
1175 (memq 'error process-state))
1176 gnus-uu-unmark-articles-not-decoded)
1177 (gnus-summary-tick-article article t))
1179 ;; Set the new series state.
1180 (if (and (not has-been-begin)
1182 (or (memq 'end process-state)
1183 (memq 'middle process-state)))
1185 (setq process-state (list 'error))
1186 (message "No begin part at the beginning")
1188 (setq state 'middle)))
1190 ;; When there are no result-files, then something must be wrong.
1191 (unless result-files
1193 ((not has-been-begin)
1194 (message "Wrong type file"))
1195 ((memq 'error process-state)
1196 (message "An error occurred during decoding"))
1197 ((not (or (memq 'ok process-state)
1198 (memq 'end process-state)))
1199 (message "End of articles reached before end of file")))
1200 ;; Make unsuccessfully decoded articles unread.
1201 (when gnus-uu-unmark-articles-not-decoded
1202 (while article-series
1203 (gnus-summary-tick-article (pop article-series) t))))
1207 (defun gnus-uu-grab-view (file)
1208 "View FILE using the gnus-uu methods."
1209 (let ((action (gnus-uu-get-action file)))
1210 (gnus-execute-command
1211 (if (string-match "%" action)
1212 (format action file)
1213 (concat action " " file))
1214 (eq gnus-view-pseudos 'not-confirm))))
1216 (defun gnus-uu-grab-move (file)
1217 "Move FILE to somewhere."
1218 (when gnus-uu-default-dir
1219 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1220 (file-name-nondirectory file))))
1221 (rename-file file to-file)
1222 (unless (file-exists-p file)
1223 (make-symbolic-link to-file file)))))
1225 (defun gnus-uu-part-number (article)
1226 (let ((subject (mail-header-subject (gnus-summary-article-header article))))
1227 (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1229 (substring subject (match-beginning 0) (match-end 0))
1232 (defun gnus-uu-uudecode-sentinel (process event)
1233 (delete-process (get-process process)))
1235 (defun gnus-uu-uustrip-article (process-buffer in-state)
1236 ;; Uudecodes a file asynchronously.
1237 (let ((state (list 'ok))
1238 (process-connection-type nil)
1239 start-char pst name-beg name-end)
1241 (set-buffer process-buffer)
1242 (let ((case-fold-search nil)
1243 (buffer-read-only nil))
1245 (goto-char (point-min))
1247 (if gnus-uu-kill-carriage-return
1249 (while (search-forward "\r" nil t)
1250 (delete-backward-char 1))
1251 (goto-char (point-min))))
1253 (if (not (re-search-forward gnus-uu-begin-string nil t))
1254 (if (not (re-search-forward gnus-uu-body-line nil t))
1255 (setq state (list 'wrong-type))))
1257 (if (memq 'wrong-type state)
1260 (setq start-char (point))
1262 (if (looking-at gnus-uu-begin-string)
1264 (setq name-end (match-end 1)
1265 name-beg (match-beginning 1))
1266 ;; Remove any non gnus-uu-body-line right after start.
1268 (or (looking-at gnus-uu-body-line)
1271 ; Replace any slashes and spaces in file names before decoding
1272 (goto-char name-beg)
1273 (while (re-search-forward "/" name-end t)
1274 (replace-match ","))
1275 (goto-char name-beg)
1276 (while (re-search-forward " " name-end t)
1277 (replace-match "_"))
1278 (goto-char name-beg)
1279 (if (re-search-forward "_*$" name-end t)
1282 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1283 (and gnus-uu-uudecode-process
1284 (setq pst (process-status
1285 (or gnus-uu-uudecode-process "nevair")))
1286 (if (or (eq pst 'stop) (eq pst 'run))
1288 (delete-process gnus-uu-uudecode-process)
1289 (gnus-uu-unmark-list-of-grabbed t))))
1290 (if (get-process "*uudecode*")
1291 (delete-process "*uudecode*"))
1292 (setq gnus-uu-uudecode-process
1295 (get-buffer-create gnus-uu-output-buffer-name)
1297 (format "cd %s ; uudecode" gnus-uu-work-dir)))
1298 (set-process-sentinel
1299 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1300 (setq state (list 'begin))
1301 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1302 (setq state (list 'middle)))
1304 (goto-char (point-max))
1307 (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1310 (if (looking-at gnus-uu-end-string)
1311 (setq state (cons 'end state)))
1314 (and gnus-uu-uudecode-process
1315 (setq pst (process-status
1316 (or gnus-uu-uudecode-process "nevair")))
1317 (if (or (eq pst 'run) (eq pst 'stop))
1319 (if gnus-uu-correct-stripped-uucode
1321 (gnus-uu-check-correct-stripped-uucode
1323 (goto-char (point-max))
1325 (concat gnus-uu-body-line "\\|"
1331 (process-send-region gnus-uu-uudecode-process
1335 (delete-process gnus-uu-uudecode-process)
1336 (message "gnus-uu: Couldn't uudecode")
1338 (setq state (list 'wrong-type)))))
1340 (if (memq 'end state)
1341 (accept-process-output gnus-uu-uudecode-process)))
1342 (setq state (list 'wrong-type))))
1343 (if (not gnus-uu-uudecode-process)
1344 (setq state (list 'wrong-type)))))
1346 (if (memq 'begin state)
1347 (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1350 ;; This function is used by `gnus-uu-grab-articles' to treat
1351 ;; a shared article.
1352 (defun gnus-uu-unshar-article (process-buffer in-state)
1353 (let ((state (list 'ok))
1356 (set-buffer process-buffer)
1357 (goto-char (point-min))
1358 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1359 (setq state (list 'wrong-type))
1361 (setq start-char (point))
1362 (call-process-region
1363 start-char (point-max) "sh" nil
1364 (get-buffer-create gnus-uu-output-buffer-name) nil
1365 "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1368 ;; Returns the name of what the shar file is going to unpack.
1369 (defun gnus-uu-find-name-in-shar ()
1370 (let ((oldpoint (point))
1372 (goto-char (point-min))
1373 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1374 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1375 (goto-char oldpoint)
1378 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1379 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1380 ;; found, or the name of the command to run if such a rule is found.
1381 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1382 (let ((action-list (copy-sequence file-action-list))
1383 (case-fold-search t)
1388 (and gnus-uu-ignore-files-by-name
1389 (string-match gnus-uu-ignore-files-by-name file-name)))
1391 (and gnus-uu-ignore-files-by-type
1392 (string-match gnus-uu-ignore-files-by-type
1393 (or (gnus-uu-choose-action
1394 file-name gnus-uu-ext-to-mime-list t)
1396 (while (not (or (eq action-list ()) action))
1397 (setq rule (car action-list))
1398 (setq action-list (cdr action-list))
1399 (if (string-match (car rule) file-name)
1400 (setq action (car (cdr rule))))))
1403 (defun gnus-uu-treat-archive (file-path)
1404 ;; Unpacks an archive. Returns t if unpacking is successful.
1405 (let ((did-unpack t)
1407 (setq action (gnus-uu-choose-action
1408 file-path (append gnus-uu-user-archive-rules
1409 (if gnus-uu-ignore-default-archive-rules
1411 gnus-uu-default-archive-rules))))
1413 (if (not action) (error "No unpackers for the file %s" file-path))
1415 (string-match "/[^/]*$" file-path)
1416 (setq dir (substring file-path 0 (match-beginning 0)))
1418 (if (member action gnus-uu-destructive-archivers)
1419 (copy-file file-path (concat file-path "~") t))
1421 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1424 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1427 (message "Unpacking: %s..." (gnus-uu-command action file-path))
1429 (if (= 0 (call-process "sh" nil
1430 (get-buffer-create gnus-uu-output-buffer-name)
1433 (message "Error during unpacking of archive")
1434 (setq did-unpack nil))
1436 (if (member action gnus-uu-destructive-archivers)
1437 (rename-file (concat file-path "~") file-path t))
1441 (defun gnus-uu-dir-files (dir)
1442 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1445 (if (file-directory-p (setq file (car dirs)))
1446 (setq files (append files (gnus-uu-dir-files file)))
1447 (setq files (cons file files)))
1448 (setq dirs (cdr dirs)))
1451 (defun gnus-uu-unpack-files (files &optional ignore)
1452 ;; Go through FILES and look for files to unpack.
1453 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1455 file did-unpack file-entry)
1456 (gnus-uu-add-file totfiles)
1458 (setq file (cdr (setq file-entry (assq 'name (car files)))))
1459 (if (and (not (member file ignore))
1460 (equal (gnus-uu-get-action (file-name-nondirectory file))
1463 (setq did-unpack (cons file did-unpack))
1464 (or (gnus-uu-treat-archive file)
1465 (message "Error during unpacking of %s" file))
1466 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1468 (gnus-uu-add-file newfiles)
1470 (or (member (car nfiles) totfiles)
1471 (setq ofiles (cons (list (cons 'name (car nfiles))
1472 (cons 'original file))
1474 (setq nfiles (cdr nfiles)))
1475 (setq totfiles newfiles))))
1476 (setq files (cdr files)))
1478 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1481 (defun gnus-uu-ls-r (dir)
1482 (let* ((files (gnus-uu-directory-files dir t))
1485 (if (file-directory-p (car files))
1487 (setq ofiles (delete (car files) ofiles))
1488 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1489 (setq files (cdr files)))
1494 (defun gnus-uu-directory-files (dir &optional full)
1495 (let (files out file)
1496 (setq files (directory-files dir full))
1498 (setq file (car files))
1499 (setq files (cdr files))
1500 (or (string-match "/\\.\\.?$" file)
1501 (setq out (cons file out))))
1502 (setq out (nreverse out))
1505 (defun gnus-uu-check-correct-stripped-uucode (start end)
1506 (let (found beg length)
1507 (if (not gnus-uu-correct-stripped-uucode)
1511 (if (re-search-forward " \\|`" end t)
1516 (if (looking-at "\n") (replace-match ""))
1520 (if (looking-at (concat gnus-uu-begin-string "\\|"
1521 gnus-uu-end-string))
1528 (setq length (- (point) beg))))
1533 (if (not (= length (- (point) beg)))
1534 (insert (make-string (- length (- (point) beg)) ? ))))
1535 (forward-line 1))))))
1537 (defvar gnus-uu-tmp-alist nil)
1539 (defun gnus-uu-initialize (&optional scan)
1542 (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1543 (if (file-exists-p (cdr entry))
1544 (setq gnus-uu-work-dir (cdr entry))
1545 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1548 (setq gnus-uu-tmp-dir (file-name-as-directory
1549 (expand-file-name gnus-uu-tmp-dir)))
1550 (if (not (file-directory-p gnus-uu-tmp-dir))
1551 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1552 (if (not (file-writable-p gnus-uu-tmp-dir))
1553 (error "Temp directory %s can't be written to"
1556 (setq gnus-uu-work-dir
1557 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1558 (gnus-uu-add-file gnus-uu-work-dir)
1559 (if (not (file-directory-p gnus-uu-work-dir))
1560 (gnus-make-directory gnus-uu-work-dir))
1561 (set-file-modes gnus-uu-work-dir 448)
1562 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1563 (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1564 gnus-uu-tmp-alist)))))
1567 ;; Kills the temporary uu buffers, kills any processes, etc.
1568 (defun gnus-uu-clean-up ()
1570 (and gnus-uu-uudecode-process
1571 (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1572 (if (or (eq pst 'stop) (eq pst 'run))
1573 (delete-process gnus-uu-uudecode-process)))
1574 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1575 (kill-buffer buf))))
1577 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1578 ;; hasn't been deleted, if, for instance, the user terminated decoding
1580 (defun gnus-uu-check-for-generated-files ()
1582 (while gnus-uu-generated-file-list
1583 (setq file (car gnus-uu-generated-file-list))
1584 (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1585 (if (not (string-match "/\\.[\\.]?$" file))
1587 (if (file-directory-p file)
1588 (setq dirs (cons file dirs))
1589 (if (file-exists-p file)
1590 (delete-file file))))))
1591 (setq dirs (nreverse dirs))
1593 (setq file (car dirs))
1594 (setq dirs (cdr dirs))
1595 (if (file-directory-p file)
1596 (if (string-match "/$" file)
1597 (delete-directory (substring file 0 (match-beginning 0)))
1598 (delete-directory file))))))
1600 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1601 ;; still exists upon exiting the newsgroup).
1602 (defun gnus-uu-add-file (file)
1604 (setq gnus-uu-generated-file-list
1605 (cons file gnus-uu-generated-file-list))
1606 (setq gnus-uu-generated-file-list
1607 (append file gnus-uu-generated-file-list))))
1609 ;; Inputs an action and a file and returns a full command, putting
1610 ;; quotes round the file name and escaping any quotes in the file name.
1611 (defun gnus-uu-command (action file)
1613 (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1616 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1617 (substring file (match-beginning 0) (match-end 0))))
1618 (setq file (substring file (1+ (match-beginning 0))))))
1619 (setq ofile (concat "\"" ofile file "\""))
1620 (if (string-match "%s" action)
1621 (format action ofile)
1622 (concat action " " ofile))))
1627 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1628 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1633 ;;; uuencoded posting
1639 ;; Any function that is to be used as and encoding method will take two
1640 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1641 ;; and "spiral.jpg", respectively.) The function should return nil if
1642 ;; the encoding wasn't successful.
1643 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1644 "Function used for encoding binary files.
1645 There are three functions supplied with gnus-uu for encoding files:
1646 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1647 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1648 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1649 uuencode and adds MIME headers.")
1651 (defvar gnus-uu-post-include-before-composing nil
1652 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1653 If this variable is t, you can either include an encoded file with
1654 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1656 (defvar gnus-uu-post-length 990
1657 "Maximum length of an article.
1658 The encoded file will be split into how many articles it takes to
1659 post the entire file.")
1661 (defvar gnus-uu-post-threaded nil
1662 "Non-nil means that gnus-uu will post the encoded file in a thread.
1663 This may not be smart, as no other decoder I have seen are able to
1664 follow threads when collecting uuencoded articles. (Well, I have seen
1665 one package that does that - gnus-uu, but somehow, I don't think that
1666 counts...) Default is nil.")
1668 (defvar gnus-uu-post-separate-description t
1669 "Non-nil means that the description will be posted in a separate article.
1670 The first article will typically be numbered (0/x). If this variable
1671 is nil, the description the user enters will be included at the
1672 beginning of the first article, which will be numbered (1/x). Default
1675 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1676 (defvar gnus-uu-post-message-id nil)
1677 (defvar gnus-uu-post-inserted-file-name nil)
1678 (defvar gnus-uu-winconf-post-news nil)
1680 (defun gnus-uu-post-news ()
1681 "Compose an article and post an encoded file."
1683 (setq gnus-uu-post-inserted-file-name nil)
1684 (setq gnus-uu-winconf-post-news (current-window-configuration))
1686 (gnus-summary-post-news)
1688 (use-local-map (copy-keymap (current-local-map)))
1689 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1690 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1691 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1692 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1694 (if gnus-uu-post-include-before-composing
1695 (save-excursion (setq gnus-uu-post-inserted-file-name
1696 (gnus-uu-post-insert-binary)))))
1698 (defun gnus-uu-post-insert-binary-in-article ()
1699 "Inserts an encoded file in the buffer.
1700 The user will be asked for a file name."
1702 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1703 (error "Not in post-news buffer"))
1705 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1707 ;; Encodes with uuencode and substitutes all spaces with backticks.
1708 (defun gnus-uu-post-encode-uuencode (path file-name)
1709 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1711 (goto-char (point-min))
1713 (while (re-search-forward " " nil t)
1714 (replace-match "`"))
1717 ;; Encodes with uuencode and adds MIME headers.
1718 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1719 (if (gnus-uu-post-encode-uuencode path file-name)
1721 (gnus-uu-post-make-mime file-name "x-uue")
1724 ;; Encodes with base64 and adds MIME headers
1725 (defun gnus-uu-post-encode-mime (path file-name)
1726 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1728 (gnus-uu-post-make-mime file-name "base64")
1731 ;; Adds MIME headers.
1732 (defun gnus-uu-post-make-mime (file-name encoding)
1733 (goto-char (point-min))
1734 (insert (format "Content-Type: %s; name=\"%s\"\n"
1735 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1737 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1739 (set-buffer gnus-post-news-buffer)
1740 (goto-char (point-min))
1741 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1743 (narrow-to-region 1 (point))
1744 (or (mail-fetch-field "mime-version")
1747 (insert "MIME-Version: 1.0\n")))
1750 ;; Encodes a file PATH with COMMAND, leaving the result in the
1752 (defun gnus-uu-post-encode-file (command path file-name)
1753 (= 0 (call-process "sh" nil t nil "-c"
1754 (format "%s %s %s" command path file-name))))
1756 (defun gnus-uu-post-news-inews ()
1757 "Posts the composed news article and encoded file.
1758 If no file has been included, the user will be asked for a file."
1760 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1761 (error "Not in post news buffer"))
1765 (if gnus-uu-post-inserted-file-name
1766 (setq file-name gnus-uu-post-inserted-file-name)
1767 (setq file-name (gnus-uu-post-insert-binary)))
1769 (if gnus-uu-post-threaded
1770 (let ((gnus-required-headers
1771 (if (memq 'Message-ID gnus-required-headers)
1772 gnus-required-headers
1773 (cons 'Message-ID gnus-required-headers)))
1774 gnus-inews-article-hook)
1776 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1777 gnus-inews-article-hook
1778 (list gnus-inews-article-hook)))
1779 (setq gnus-inews-article-hook
1783 (goto-char (point-min))
1784 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1785 (setq gnus-uu-post-message-id
1787 (match-beginning 1) (match-end 1)))
1788 (setq gnus-uu-post-message-id nil))))
1789 gnus-inews-article-hook))
1790 (gnus-uu-post-encoded file-name t))
1791 (gnus-uu-post-encoded file-name nil)))
1792 (setq gnus-uu-post-inserted-file-name nil)
1793 (and gnus-uu-winconf-post-news
1794 (set-window-configuration gnus-uu-winconf-post-news)))
1796 ;; Asks for a file to encode, encodes it and inserts the result in
1797 ;; the current buffer. Returns the file name the user gave.
1798 (defun gnus-uu-post-insert-binary ()
1799 (let ((uuencode-buffer-name "*uuencode buffer*")
1800 file-path uubuf file-name)
1802 (setq file-path (read-file-name
1803 "What file do you want to encode? "))
1804 (if (not (file-exists-p file-path))
1805 (error "%s: No such file" file-path))
1807 (goto-char (point-max))
1808 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1810 (if (string-match "^~/" file-path)
1811 (setq file-path (concat "$HOME" (substring file-path 1))))
1812 (if (string-match "/[^/]*$" file-path)
1813 (setq file-name (substring file-path (1+ (match-beginning 0))))
1814 (setq file-name file-path))
1818 (set-buffer (setq uubuf
1819 (get-buffer-create uuencode-buffer-name)))
1821 (funcall gnus-uu-post-encode-method file-path file-name))
1822 (insert-buffer-substring uubuf)
1823 (error "Encoding unsuccessful"))
1824 (kill-buffer uubuf))
1827 ;; Posts the article and all of the encoded file.
1828 (defun gnus-uu-post-encoded (file-name &optional threaded)
1829 (let ((send-buffer-name "*uuencode send buffer*")
1830 (encoded-buffer-name "*encoded buffer*")
1831 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1832 (separator (concat mail-header-separator "\n\n"))
1833 uubuf length parts header i end beg
1834 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1836 (setq post-buf (current-buffer))
1838 (goto-char (point-min))
1839 (if (not (re-search-forward
1840 (if gnus-uu-post-separate-description
1841 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1843 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1844 (error "Internal error: No binary/header separator"))
1847 (setq beg-binary (point))
1848 (setq end-binary (point-max))
1851 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1853 (insert-buffer-substring post-buf beg-binary end-binary)
1854 (goto-char (point-min))
1855 (setq length (count-lines 1 (point-max)))
1856 (setq parts (/ length gnus-uu-post-length))
1857 (if (not (< (% length gnus-uu-post-length) 4))
1858 (setq parts (1+ parts))))
1860 (if gnus-uu-post-separate-description
1862 (kill-region (point) (point-max))
1864 (goto-char (point-min))
1866 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1868 (setq header (buffer-substring 1 (point)))
1870 (goto-char (point-min))
1871 (if (not gnus-uu-post-separate-description)
1873 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1876 (insert (format " (0/%d)" parts))))
1882 (while (not (> i parts))
1883 (set-buffer (get-buffer-create send-buffer-name))
1886 (if (and threaded gnus-uu-post-message-id)
1887 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1890 (- 62 (length (format top-string "" file-name i parts ""))))
1891 (if (> 1 (setq minlen (/ whole-len 2)))
1896 (make-string minlen ?-)
1899 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1901 (goto-char (point-min))
1902 (if (not (re-search-forward "^Subject: " nil t))
1907 (insert (format " (%d/%d)" i parts)))
1908 (if (or (and (= i 2) gnus-uu-post-separate-description)
1909 (and (= i 1) (not gnus-uu-post-separate-description)))
1910 (replace-match "Subject: Re: "))))
1912 (goto-char (point-max))
1917 (goto-char (point-max))
1918 (forward-line gnus-uu-post-length))
1919 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1922 (insert-buffer-substring uubuf beg end)
1927 (goto-char (point-min))
1929 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1932 (if (re-search-forward
1933 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1942 (and (setq buf (get-buffer send-buffer-name))
1944 (and (setq buf (get-buffer encoded-buffer-name))
1947 (if (not gnus-uu-post-separate-description)
1949 (set-buffer-modified-p nil)
1950 (and (fboundp 'bury-buffer) (bury-buffer))))))
1954 ;; gnus-uu.el ends here