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")
44 ("\\.midi?$" "playmidi -f")
46 ("\\.ps$" "ghostview")
48 ("\\.html$" "xmosaic")
49 ("\\.mpe?g$" "mpeg_play")
50 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
51 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
53 "*Default actions to be taken when the user asks to view a file.
54 To change the behaviour, you can either edit this variable or set
55 `gnus-uu-user-view-rules' to something useful.
59 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
60 following in your .emacs file:
62 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
64 Both these variables are lists of lists with two string elements. The
65 first string is a regular expression. If the file name matches this
66 regular expression, the command in the second string is executed with
67 the file as an argument.
69 If the command string contains \"%s\", the file name will be inserted
70 at that point in the command string. If there's no \"%s\" in the
71 command string, the file name will be appended to the command string
74 There are several user variables to tailor the behaviour of gnus-uu to
75 your needs. First we have `gnus-uu-user-view-rules', which is the
76 variable gnus-uu first consults when trying to decide how to view a
77 file. If this variable contains no matches, gnus-uu examines the
78 default rule variable provided in this package. If gnus-uu finds no
79 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
82 (defvar gnus-uu-user-view-rules nil
83 "*Variable detailing what actions are to be taken to view a file.
84 See the documentation on the `gnus-uu-default-view-rules' variable for
87 (defvar gnus-uu-user-view-rules-end
89 "*Variable saying what actions are to be taken if no rule matched the file name.
90 See the documentation on the `gnus-uu-default-view-rules' variable for
93 ;; Default unpacking commands
95 (defvar gnus-uu-default-archive-rules
96 '(("\\.tar$" "tar xf")
97 ("\\.zip$" "unzip -o")
101 ("\\.\\(lzh\\|lha\\)$" "lha x")
102 ("\\.Z$" "uncompress")
104 ("\\.arc$" "arc -x")))
106 (defvar gnus-uu-destructive-archivers
107 (list "uncompress" "gunzip"))
109 (defvar gnus-uu-user-archive-rules nil
110 "*A list that can be set to override the default archive unpacking commands.
111 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
112 unpack zip files, say the following:
113 (setq gnus-uu-user-archive-rules
114 '((\"\\\\.tar$\" \"untar\")
115 (\"\\\\.zip$\" \"zip -x\")))")
117 (defvar gnus-uu-ignore-files-by-name nil
118 "*A regular expression saying what files should not be viewed based on name.
119 If, for instance, you want gnus-uu to ignore all .au and .wav files,
120 you could say something like
122 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
124 Note that this variable can be used in conjunction with the
125 `gnus-uu-ignore-files-by-type' variable.")
127 (defvar gnus-uu-ignore-files-by-type nil
128 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
129 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
130 you could say something like
132 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
134 Note that this variable can be used in conjunction with the
135 `gnus-uu-ignore-files-by-name' variable.")
137 ;; Pseudo-MIME support
139 (defconst gnus-uu-ext-to-mime-list
140 '(("\\.gif$" "image/gif")
141 ("\\.jpe?g$" "image/jpeg")
142 ("\\.tiff?$" "image/tiff")
143 ("\\.xwd$" "image/xwd")
144 ("\\.pbm$" "image/pbm")
145 ("\\.pgm$" "image/pgm")
146 ("\\.ppm$" "image/ppm")
147 ("\\.xbm$" "image/xbm")
148 ("\\.pcx$" "image/pcx")
149 ("\\.tga$" "image/tga")
150 ("\\.ps$" "image/postscript")
151 ("\\.fli$" "video/fli")
152 ("\\.wav$" "audio/wav")
153 ("\\.aiff$" "audio/aiff")
154 ("\\.hcom$" "audio/hcom")
155 ("\\.voc$" "audio/voc")
156 ("\\.smp$" "audio/smp")
157 ("\\.mod$" "audio/mod")
158 ("\\.dvi$" "image/dvi")
159 ("\\.mpe?g$" "video/mpeg")
160 ("\\.au$" "audio/basic")
161 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
162 ("\\.\\(c\\|h\\)$" "text/source")
163 ("read.*me" "text/plain")
164 ("\\.html$" "text/html")
165 ("\\.bat$" "text/bat")
166 ("\\.[1-6]$" "text/man")
167 ("\\.flc$" "video/flc")
168 ("\\.rle$" "video/rle")
169 ("\\.pfx$" "video/pfx")
170 ("\\.avi$" "video/avi")
171 ("\\.sme$" "video/sme")
172 ("\\.rpza$" "video/prza")
173 ("\\.dl$" "video/dl")
174 ("\\.qt$" "video/qt")
175 ("\\.rsrc$" "video/rsrc")
176 ("\\..*$" "unknown/unknown")))
178 ;; Various variables users may set
180 (defvar gnus-uu-tmp-dir "/tmp/"
181 "*Variable saying where gnus-uu is to do its work.
182 Default is \"/tmp/\".")
184 (defvar gnus-uu-do-not-unpack-archives nil
185 "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
188 (defvar gnus-uu-view-and-save nil
189 "*Non-nil means that the user will always be asked to save a file after viewing it.
190 If the variable is nil, the user will only be asked to save if the
191 viewing is unsuccessful. Default is nil.")
193 (defvar gnus-uu-ignore-default-view-rules nil
194 "*Non-nil means that gnus-uu will ignore the default viewing rules.
195 Only the user viewing rules will be consulted. Default is nil.")
197 (defvar gnus-uu-grabbed-file-functions nil
198 "*Functions run on each file after successful decoding.
199 They will be called with the name of the file as the argument.
200 Likely functions you can use in this list are `gnus-uu-grab-view'
201 and `gnus-uu-grab-move'.")
203 (defvar gnus-uu-ignore-default-archive-rules nil
204 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
205 Only the user unpacking commands will be consulted. Default is nil.")
207 (defvar gnus-uu-kill-carriage-return t
208 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
211 (defvar gnus-uu-view-with-metamail nil
212 "*Non-nil means that files will be viewed with metamail.
213 The gnus-uu viewing functions will be ignored and gnus-uu will try
214 to guess at a content-type based on file name suffixes. Default
217 (defvar gnus-uu-unmark-articles-not-decoded nil
218 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
221 (defvar gnus-uu-correct-stripped-uucode nil
222 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
225 (defvar gnus-uu-save-in-digest nil
226 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
227 If this variable is nil, gnus-uu will just save everything in a
228 file without any embellishments. The digesting almost conforms to RFC1153 -
229 no easy way to specify any meaningful volume and issue numbers were found,
230 so I simply dropped them.")
232 (defvar gnus-uu-digest-headers
233 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
234 "^Summary:" "^References:")
235 "*List of regexps to match headers included in digested messages.
236 The headers will be included in the sequence they are matched.")
238 (defvar gnus-uu-save-separate-articles nil
239 "*Non-nil means that gnus-uu will save articles in separate files.")
241 ;; Internal variables
243 (defvar gnus-uu-saved-article-name nil)
245 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
246 (defconst gnus-uu-end-string "^end[ \t]*$")
248 (defconst gnus-uu-body-line "^M")
250 (while (> (setq i (1- i)) 0)
251 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
252 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
254 ;"^M.............................................................?$"
256 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
258 (defvar gnus-uu-shar-file-name nil)
259 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
261 (defconst gnus-uu-postscript-begin-string "^%!PS-")
262 (defconst gnus-uu-postscript-end-string "^%%EOF$")
264 (defvar gnus-uu-file-name nil)
265 (defconst gnus-uu-uudecode-process nil)
266 (defvar gnus-uu-binhex-article-name nil)
268 (defvar gnus-uu-generated-file-list nil)
269 (defvar gnus-uu-work-dir nil)
271 (defconst gnus-uu-output-buffer-name " *Gnus UU Output*")
273 (defvar gnus-uu-default-dir default-directory)
274 (defvar gnus-uu-digest-from-subject nil)
279 (gnus-uu-mark-map "P" gnus-summary-mark-map)
280 "p" gnus-summary-mark-as-processable
281 "u" gnus-summary-unmark-as-processable
282 "U" gnus-summary-unmark-all-processable
283 "v" gnus-uu-mark-over
284 "s" gnus-uu-mark-series
285 "r" gnus-uu-mark-region
286 "R" gnus-uu-mark-by-regexp
287 "t" gnus-uu-mark-thread
288 "T" gnus-uu-unmark-thread
290 "b" gnus-uu-mark-buffer
291 "S" gnus-uu-mark-sparse)
294 (gnus-uu-extract-map "X" gnus-summary-mode-map)
295 ;;"x" gnus-uu-extract-any
296 ;;"m" gnus-uu-extract-mime
297 "u" gnus-uu-decode-uu
298 "U" gnus-uu-decode-uu-and-save
299 "s" gnus-uu-decode-unshar
300 "S" gnus-uu-decode-unshar-and-save
301 "o" gnus-uu-decode-save
302 "O" gnus-uu-decode-save
303 "b" gnus-uu-decode-binhex
304 "B" gnus-uu-decode-binhex
305 "p" gnus-uu-decode-postscript
306 "P" gnus-uu-decode-postscript-and-save)
309 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
310 "u" gnus-uu-decode-uu-view
311 "U" gnus-uu-decode-uu-and-save-view
312 "s" gnus-uu-decode-unshar-view
313 "S" gnus-uu-decode-unshar-and-save-view
314 "o" gnus-uu-decode-save-view
315 "O" gnus-uu-decode-save-view
316 "b" gnus-uu-decode-binhex-view
317 "B" gnus-uu-decode-binhex-view
318 "p" gnus-uu-decode-postscript-view
319 "P" gnus-uu-decode-postscript-and-save-view)
324 (defun gnus-uu-decode-uu (n)
325 "Uudecodes the current article."
327 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
329 (defun gnus-uu-decode-uu-and-save (n dir)
330 "Decodes and saves the resulting file."
332 (list current-prefix-arg
333 (file-name-as-directory
334 (read-file-name "Uudecode and save in dir: "
336 gnus-uu-default-dir t))))
337 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir))
339 (defun gnus-uu-decode-unshar (n)
340 "Unshars the current article."
342 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan))
344 (defun gnus-uu-decode-unshar-and-save (n dir)
345 "Unshars and saves the current article."
347 (list current-prefix-arg
348 (file-name-as-directory
349 (read-file-name "Unshar and save in dir: "
351 gnus-uu-default-dir t))))
352 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan))
354 (defun gnus-uu-decode-save (n file)
355 "Saves the current article."
357 (list current-prefix-arg
359 (if gnus-uu-save-separate-articles
360 "Save articles is dir: "
361 "Save articles in file: ")
363 gnus-uu-default-dir)))
364 (setq gnus-uu-saved-article-name file)
365 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t)
366 (setq gnus-uu-generated-file-list
367 (delete file gnus-uu-generated-file-list)))
369 (defun gnus-uu-decode-binhex (n dir)
370 "Unbinhexes the current article."
372 (list current-prefix-arg
373 (file-name-as-directory
374 (read-file-name "Unbinhex and save in dir: "
376 gnus-uu-default-dir))))
377 (setq gnus-uu-binhex-article-name
378 (make-temp-name (concat gnus-uu-work-dir "binhex")))
379 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
381 (defun gnus-uu-decode-uu-view (n)
382 "Uudecodes and views the current article."
384 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
385 (gnus-uu-decode-uu n)))
387 (defun gnus-uu-decode-uu-and-save-view (n dir)
388 "Decodes, views and saves the resulting file."
390 (list current-prefix-arg
391 (read-file-name "Uudecode, view and save in dir: "
393 gnus-uu-default-dir t)))
394 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
395 (gnus-uu-decode-uu-and-save n dir)))
397 (defun gnus-uu-decode-unshar-view (n)
398 "Unshars and views the current article."
400 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
401 (gnus-uu-decode-unshar n)))
403 (defun gnus-uu-decode-unshar-and-save-view (n dir)
404 "Unshars and saves the current article."
406 (list current-prefix-arg
407 (read-file-name "Unshar, view and save in dir: "
409 gnus-uu-default-dir t)))
410 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
411 (gnus-uu-decode-unshar-and-save n dir)))
413 (defun gnus-uu-decode-save-view (n file)
414 "Saves and views the current article."
416 (list current-prefix-arg
417 (read-file-name (if gnus-uu-save-separate-articles
418 "Save articles is dir: "
419 "Save articles in file: ")
420 gnus-uu-default-dir gnus-uu-default-dir)))
421 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
422 (gnus-uu-decode-save n file)))
424 (defun gnus-uu-decode-binhex-view (n file)
425 "Unbinhexes and views the current article."
427 (list current-prefix-arg
428 (read-file-name "Unbinhex, view and save in dir: "
429 gnus-uu-default-dir gnus-uu-default-dir)))
430 (setq gnus-uu-binhex-article-name
431 (make-temp-name (concat gnus-uu-work-dir "binhex")))
432 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
433 (gnus-uu-decode-binhex n file)))
436 ;; Digest and forward articles
438 (defun gnus-uu-digest-mail-forward (n &optional post)
439 "Digests and forwards all articles in this series."
441 (let ((gnus-uu-save-in-digest t)
442 (file (make-temp-name (concat gnus-uu-tmp-dir "forward")))
444 (setq gnus-uu-digest-from-subject nil)
445 (gnus-uu-decode-save n file)
446 (gnus-uu-add-file file)
447 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
448 (gnus-add-current-to-buffer-list)
450 (delete-other-windows)
452 (let ((fs gnus-uu-digest-from-subject))
455 (setq from (car (car fs))
456 subject (gnus-simplify-subject-fuzzy (cdr (car fs)))
458 (while (and fs (or from subject))
460 (or (string= from (car (car fs)))
463 (or (string= (gnus-simplify-subject-fuzzy (cdr (car fs)))
467 (or subject (setq subject "Digested Articles"))
468 (or from (setq from "Various")))
469 (goto-char (point-min))
470 (and (re-search-forward "^Subject: ")
472 (delete-region (point) (gnus-point-at-eol))
474 (goto-char (point-min))
475 (and (re-search-forward "^From: ")
477 (delete-region (point) (gnus-point-at-eol))
480 (gnus-forward-using-post)
484 (setq gnus-uu-digest-from-subject nil)))
486 (defun gnus-uu-digest-post-forward (n)
487 "Digest and forward to a newsgroup."
489 (gnus-uu-digest-mail-forward n t))
493 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
494 "Ask for a regular expression and set the process mark on all articles that match."
495 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
496 (gnus-set-global-variables)
497 (let ((articles (gnus-uu-find-articles-matching regexp)))
500 (gnus-summary-remove-process-mark (pop articles))
501 (gnus-summary-set-process-mark (pop articles))))
503 (gnus-summary-position-point))
505 (defun gnus-uu-unmark-by-regexp (regexp &optional unmark)
506 "Ask for a regular expression and remove the process mark on all articles that match."
507 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
508 (gnus-uu-mark-by-regexp regexp t))
510 (defun gnus-uu-mark-series ()
511 "Mark the current series with the process mark."
513 (gnus-set-global-variables)
514 (let ((articles (gnus-uu-find-articles-matching)))
516 (gnus-summary-set-process-mark (car articles))
517 (setq articles (cdr articles)))
519 (gnus-summary-position-point))
521 (defun gnus-uu-mark-region (beg end &optional unmark)
522 "Set the process mark on all articles between point and mark."
524 (gnus-set-global-variables)
527 (while (< (point) end)
529 (gnus-summary-remove-process-mark (gnus-summary-article-number))
530 (gnus-summary-set-process-mark (gnus-summary-article-number)))
532 (gnus-summary-position-point))
534 (defun gnus-uu-unmark-region (beg end)
535 "Remove the process mark from all articles between point and mark."
537 (gnus-uu-mark-region beg end t))
539 (defun gnus-uu-mark-buffer ()
540 "Set the process mark on all articles in the buffer."
542 (gnus-uu-mark-region (point-min) (point-max)))
544 (defun gnus-uu-unmark-buffer ()
545 "Remove the process mark on all articles in the buffer."
547 (gnus-uu-mark-region (point-min) (point-max) t))
549 (defun gnus-uu-mark-thread ()
550 "Marks all articles downwards in this thread."
552 (gnus-set-global-variables)
553 (let ((level (gnus-summary-thread-level)))
554 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
555 (zerop (gnus-summary-next-subject 1))
556 (> (gnus-summary-thread-level) level))))
557 (gnus-summary-position-point))
559 (defun gnus-uu-unmark-thread ()
560 "Unmarks all articles downwards in this thread."
562 (gnus-set-global-variables)
563 (let ((level (gnus-summary-thread-level)))
564 (while (and (gnus-summary-remove-process-mark
565 (gnus-summary-article-number))
566 (zerop (gnus-summary-next-subject 1))
567 (> (gnus-summary-thread-level) level))))
568 (gnus-summary-position-point))
570 (defun gnus-uu-mark-over (score)
571 "Mark all articles with a score over SCORE (the prefix.)"
573 (let ((score (gnus-score-default score))
574 (data gnus-newsgroup-data))
577 (when (> (or (cdr (assq (gnus-data-number (caar data))
578 gnus-newsgroup-scored))
579 gnus-summary-default-score 0)
581 (gnus-summary-set-process-mark (caar data)))
582 (setq data (cdr data))))
583 (gnus-summary-position-point)))
585 (defun gnus-uu-mark-sparse ()
586 "Mark all series that have some articles marked."
588 (gnus-set-global-variables)
589 (let ((marked (nreverse gnus-newsgroup-processable))
590 subject articles total headers)
591 (or marked (error "No articles marked with the process mark"))
592 (setq gnus-newsgroup-processable nil)
595 (and (setq headers (gnus-summary-article-header (car marked)))
596 (setq subject (mail-header-subject headers)
597 articles (gnus-uu-find-articles-matching
598 (gnus-uu-reginize-string subject))
599 total (nconc total articles)))
601 (gnus-summary-set-process-mark (car articles))
602 (setcdr marked (delq (car articles) (cdr marked)))
603 (setq articles (cdr articles)))
604 (setq marked (cdr marked)))
605 (setq gnus-newsgroup-processable (nreverse total)))
606 (gnus-summary-position-point)))
608 (defun gnus-uu-mark-all ()
609 "Mark all articles in \"series\" order."
611 (gnus-set-global-variables)
612 (setq gnus-newsgroup-processable nil)
614 (let ((data gnus-newsgroup-data)
617 (unless (memq (setq number (gnus-data-number (car data)))
618 gnus-newsgroup-processable)
619 (gnus-summary-goto-subject number)
620 (gnus-uu-mark-series))
621 (setq data (cdr data)))))
622 (gnus-summary-position-point))
624 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
626 (defun gnus-uu-decode-postscript (n)
627 "Gets postscript of the current article."
629 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
631 (defun gnus-uu-decode-postscript-view (n)
632 "Gets and views the current article."
634 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
635 (gnus-uu-decode-postscript n)))
637 (defun gnus-uu-decode-postscript-and-save (n dir)
638 "Extracts postscript and saves the current article."
640 (list current-prefix-arg
641 (file-name-as-directory
642 (read-file-name "Save in dir: "
644 gnus-uu-default-dir t))))
645 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n dir))
648 (defun gnus-uu-decode-postscript-and-save-view (n dir)
649 "Decodes, views and saves the resulting file."
651 (list current-prefix-arg
652 (read-file-name "Where do you want to save the file(s)? "
654 gnus-uu-default-dir t)))
655 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
656 (gnus-uu-decode-postscript-and-save n dir)))
659 ;; Internal functions.
661 (defun gnus-uu-decode-with-method (method n &optional save not-insert scan)
662 (gnus-uu-initialize scan)
663 (if save (setq gnus-uu-default-dir save))
664 (let ((articles (gnus-uu-get-list-of-articles n))
666 (setq files (gnus-uu-grab-articles articles method t))
667 (let ((gnus-current-article (car articles)))
668 (and scan (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
669 (and save (gnus-uu-save-files files save))
670 (if (eq gnus-uu-do-not-unpack-archives nil)
671 (setq files (gnus-uu-unpack-files files)))
672 (gnus-uu-add-file (mapcar (lambda (file) (cdr (assq 'name file))) files))
673 (setq files (nreverse (gnus-uu-get-actions files)))
674 (or not-insert (not gnus-insert-pseudo-articles)
675 (gnus-summary-insert-pseudos files save))))
677 ;; Return a list of files in dir.
678 (defun gnus-uu-scan-directory (dir)
679 (let ((files (directory-files dir t))
682 (cond ((string-match "/\\.\\.?$" (car files)))
683 ((file-directory-p (car files))
684 (setq dirs (cons (car files) dirs)))
685 (t (setq out (cons (list (cons 'name (car files))
686 (cons 'article gnus-current-article))
688 (setq files (cdr files)))
689 (apply 'nconc out (mapcar (lambda (d) (gnus-uu-scan-directory d))
692 (defun gnus-uu-save-files (files dir)
693 (let ((len (length files))
697 (setq file (cdr (assq 'name (car files))))
700 (setq to-file (if (file-directory-p dir)
701 (concat dir (file-name-nondirectory file))
703 (and (or (not (file-exists-p to-file))
704 (gnus-y-or-n-p (format "%s exists; overwrite? "
706 (copy-file file to-file t t))))
707 (setq files (cdr files)))
708 (message "Saved %d file%s" len (if (> len 1) "s" ""))))
710 ;; Functions for saving and possibly digesting articles without
713 ;; Function called by gnus-uu-grab-articles to treat each article.
714 (defun gnus-uu-save-article (buffer in-state)
716 (gnus-uu-save-separate-articles
719 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
720 gnus-current-article))
721 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
722 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
724 ((eq in-state 'last) (list 'end))
725 (t (list 'middle)))))
726 ((not gnus-uu-save-in-digest)
729 (write-region 1 (point-max) gnus-uu-saved-article-name t)
730 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
731 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
733 ((eq in-state 'last) (list 'end))
734 (t (list 'middle)))))
736 (let ((header (gnus-summary-article-header)))
737 (setq gnus-uu-digest-from-subject
738 (cons (cons (mail-header-from header)
739 (mail-header-subject header))
740 gnus-uu-digest-from-subject)))
741 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
742 (delim (concat "^" (make-string 30 ?-) "$"))
743 beg subj headers headline sorthead body end-string state)
744 (if (or (eq in-state 'first)
745 (eq in-state 'first-and-last))
747 (setq state (list 'begin))
748 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
751 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
754 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
755 (current-time-string) name name))))
756 (if (not (eq in-state 'end))
757 (setq state (list 'middle))))
759 (set-buffer (get-buffer "*gnus-uu-body*"))
760 (goto-char (setq beg (point-max)))
764 (let (buffer-read-only)
765 (set-text-properties (point-min) (point-max) nil)
766 ;; These two are necessary for XEmacs 19.12 fascism.
767 (put-text-property (point-min) (point-max) 'invisible nil)
768 (put-text-property (point-min) (point-max) 'intangible nil))
769 (goto-char (point-min))
770 (re-search-forward "\n\n")
771 ;; Quote all 30-dash lines.
773 (while (re-search-forward delim nil t)
777 (setq body (buffer-substring (1- (point)) (point-max)))
778 (narrow-to-region (point-min) (point))
779 (if (not (setq headers gnus-uu-digest-headers))
780 (setq sorthead (buffer-substring (point-min) (point-max)))
782 (setq headline (car headers))
783 (setq headers (cdr headers))
784 (goto-char (point-min))
785 (if (re-search-forward headline nil t)
790 (or (and (re-search-forward "^[^ \t]" nil t)
792 (progn (forward-line 1) (point)))))))))
794 (insert sorthead) (goto-char (point-max))
795 (insert body) (goto-char (point-max))
796 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
798 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
800 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
802 (set-buffer (get-buffer "*gnus-uu-pre*"))
803 (insert (format " %s\n" subj))))))
804 (if (or (eq in-state 'last)
805 (eq in-state 'first-and-last))
808 (set-buffer (get-buffer "*gnus-uu-pre*"))
809 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
810 (write-region 1 (point-max) gnus-uu-saved-article-name))
812 (set-buffer (get-buffer "*gnus-uu-body*"))
813 (goto-char (point-max))
815 (concat (setq end-string (format "End of %s Digest" name))
817 (insert (concat (make-string (length end-string) ?*) "\n"))
818 (write-region 1 (point-max) gnus-uu-saved-article-name t))
819 (kill-buffer (get-buffer "*gnus-uu-pre*"))
820 (kill-buffer (get-buffer "*gnus-uu-body*"))
821 (setq state (cons 'end state))))
822 (if (memq 'begin state)
823 (cons gnus-uu-saved-article-name state)
826 ;; Binhex treatment - not very advanced.
828 (defconst gnus-uu-binhex-body-line
829 "^[^:]...............................................................$")
830 (defconst gnus-uu-binhex-begin-line
831 "^:...............................................................$")
832 (defconst gnus-uu-binhex-end-line
835 (defun gnus-uu-binhex-article (buffer in-state)
836 (let (state start-char)
840 (goto-char (point-min))
841 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
842 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
843 (setq state (list 'wrong-type))))
845 (if (memq 'wrong-type state)
848 (setq start-char (point))
849 (if (looking-at gnus-uu-binhex-begin-line)
851 (setq state (list 'begin))
852 (write-region 1 1 gnus-uu-binhex-article-name))
853 (setq state (list 'middle)))
854 (goto-char (point-max))
855 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
856 gnus-uu-binhex-end-line) nil t)
857 (if (looking-at gnus-uu-binhex-end-line)
858 (setq state (if (memq 'begin state)
863 (if (file-exists-p gnus-uu-binhex-article-name)
864 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
865 (if (memq 'begin state)
866 (cons gnus-uu-binhex-article-name state)
871 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
872 (let ((state (list 'ok))
873 start-char end-char file-name)
875 (set-buffer process-buffer)
876 (goto-char (point-min))
877 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
878 (setq state (list 'wrong-type))
880 (setq start-char (point))
881 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
882 (setq state (list 'wrong-type))
883 (setq end-char (point))
884 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
885 (insert-buffer-substring process-buffer start-char end-char)
886 (setq file-name (concat gnus-uu-work-dir
887 (cdr gnus-article-current) ".ps"))
888 (write-region (point-min) (point-max) file-name)
889 (setq state (list file-name 'begin 'end)))))
895 (defun gnus-uu-get-actions (files)
899 (setq name (cdr (assq 'name (car files))))
901 (setq action (gnus-uu-get-action name))
902 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
903 (cons 'action "file")
904 (cons 'action action))
905 (cons 'execute (gnus-uu-command
908 (setq files (cdr files)))
911 (defun gnus-uu-get-action (file-name)
914 (gnus-uu-choose-action
917 gnus-uu-user-view-rules
918 (if gnus-uu-ignore-default-view-rules
920 gnus-uu-default-view-rules)
921 gnus-uu-user-view-rules-end)))
922 (if (and (not (string= (or action "") "gnus-uu-archive"))
923 gnus-uu-view-with-metamail)
925 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
926 (setq action (format "metamail -d -b -c \"%s\"" action))))
930 ;; Functions for treating subjects and collecting series.
932 (defun gnus-uu-reginize-string (string)
933 ;; Takes a string and puts a \ in front of every special character;
934 ;; ignores any leading "version numbers" thingies that they use in
935 ;; the comp.binaries groups, and either replaces anything that looks
936 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
937 ;; like that, replaces the last two numbers with "[0-9]+". This, in
938 ;; my experience, should get most postings of a series.
940 (vernum "v[0-9]+[a-z][0-9]+:")
943 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
944 (buffer-disable-undo (current-buffer))
946 (insert (regexp-quote string))
949 (setq case-fold-search nil)
950 (goto-char (point-min))
951 (if (looking-at vernum)
953 (replace-match vernum t t)
954 (setq beg (length vernum))))
957 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
958 (replace-match " [0-9]+/[0-9]+")
961 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
962 (replace-match "[0-9]+ of [0-9]+")
965 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
968 (< 1 (goto-char (1- (point))))))
969 (re-search-forward "[0-9]+" nil t)
970 (replace-match "[0-9]+")
972 (setq count (1- count)))))
975 (while (re-search-forward "[ \t]+" nil t)
976 (replace-match "[ \t]*" t t))
978 (buffer-substring 1 (point-max)))))
980 (defun gnus-uu-get-list-of-articles (n)
981 ;; If N is non-nil, the article numbers of the N next articles
983 ;; If any articles have been marked as processable, they will be
985 ;; Failing that, articles that have subjects that are part of the
986 ;; same "series" as the current will be returned.
990 (let ((backward (< n 0))
994 (setq articles (cons (gnus-summary-article-number)
996 (gnus-summary-search-forward nil nil backward))
998 (nreverse articles)))
999 (gnus-newsgroup-processable
1000 (reverse gnus-newsgroup-processable))
1002 (gnus-uu-find-articles-matching)))))
1004 (defun gnus-uu-string< (l1 l2)
1005 (string< (car l1) (car l2)))
1007 (defun gnus-uu-find-articles-matching
1008 (&optional subject only-unread do-not-translate)
1009 ;; Finds all articles that matches the regexp SUBJECT. If it is
1010 ;; nil, the current article name will be used. If ONLY-UNREAD is
1011 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1012 ;; non-nil, article names are not equalized before sorting.
1013 (let ((subject (or subject
1014 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1019 ;; Collect all subjects matching subject.
1020 (let ((case-fold-search t)
1021 (data gnus-newsgroup-data)
1025 (and (not (gnus-data-pseudo-p d))
1026 (or (not only-unread)
1027 (= (setq mark (gnus-data-mark d))
1029 (= mark gnus-ticked-mark)
1030 (= mark gnus-dormant-mark))
1031 (setq subj (mail-header-subject (gnus-data-header d)))
1032 (string-match subject subj)
1033 (setq list-of-subjects
1034 (cons (cons subj (gnus-data-number d))
1035 list-of-subjects)))))
1037 ;; Expand numbers, sort, and return the list of article
1039 (mapcar (lambda (sub) (cdr sub))
1040 (sort (gnus-uu-expand-numbers
1042 (not do-not-translate))
1043 'gnus-uu-string<))))))
1045 (defun gnus-uu-expand-numbers (string-list &optional translate)
1046 ;; Takes a list of strings and "expands" all numbers in all the
1047 ;; strings. That is, this function makes all numbers equal length by
1048 ;; prepending lots of zeroes before each number. This is to ease later
1049 ;; sorting to find out what sequence the articles are supposed to be
1050 ;; decoded in. Returns the list of expanded strings.
1051 (let ((out-list string-list)
1054 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1055 (buffer-disable-undo (current-buffer))
1058 (insert (car (car string-list)))
1059 ;; Translate multiple spaces to one space.
1060 (goto-char (point-min))
1061 (while (re-search-forward "[ \t]+" nil t)
1062 (replace-match " "))
1063 ;; Translate all characters to "a".
1064 (goto-char (point-min))
1066 (while (re-search-forward "[A-Za-z]" nil t)
1067 (replace-match "a" t t)))
1069 (goto-char (point-min))
1070 (while (re-search-forward "[0-9]+" nil t)
1073 (string-to-int (buffer-substring
1074 (match-beginning 0) (match-end 0))))))
1075 (setq string (buffer-substring 1 (point-max)))
1076 (setcar (car string-list) string)
1077 (setq string-list (cdr string-list))))
1081 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1082 ;; function. It takes a list of articles to be grabbed and a function
1083 ;; to apply to each article.
1085 ;; The function to be called should take two parameters. The first
1086 ;; parameter is the article buffer. The function should leave the
1087 ;; result, if any, in this buffer. Most treatment functions will just
1088 ;; generate files...
1090 ;; The second parameter is the state of the list of articles, and can
1091 ;; have four values: `first', `middle', `last' and `first-and-last'.
1093 ;; The function should return a list. The list may contain the
1094 ;; following symbols:
1095 ;; `error' if an error occurred
1096 ;; `begin' if the beginning of an encoded file has been received
1097 ;; If the list returned contains a `begin', the first element of
1098 ;; the list *must* be a string with the file name of the decoded
1100 ;; `end' if the the end of an encoded file has been received
1101 ;; `middle' if the article was a body part of an encoded file
1102 ;; `wrong-type' if the article was not a part of an encoded file
1103 ;; `ok', which can be used everything is ok
1105 (defvar gnus-uu-has-been-grabbed nil)
1107 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1109 (if (not (and gnus-uu-has-been-grabbed
1110 gnus-uu-unmark-articles-not-decoded))
1112 (if dont-unmark-last-article
1114 (setq art (car gnus-uu-has-been-grabbed))
1115 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
1116 (while gnus-uu-has-been-grabbed
1117 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1118 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1119 (if dont-unmark-last-article
1120 (setq gnus-uu-has-been-grabbed (list art))))))
1122 ;; This function takes a list of articles and a function to apply to
1123 ;; each article grabbed.
1125 ;; This function returns a list of files decoded if the grabbing and
1126 ;; the process-function has been successful and nil otherwise.
1127 (defun gnus-uu-grab-articles
1128 (articles process-function &optional sloppy limit no-errors)
1129 (let ((state 'first)
1130 has-been-begin article result-file result-files process-state
1133 (while (and articles
1134 (not (memq 'error process-state))
1136 (not (memq 'end process-state))))
1138 (setq article (pop articles))
1139 (push article article-series)
1142 (if (eq state 'first)
1143 (setq state 'first-and-last)
1144 (setq state 'last)))
1146 (let ((part (gnus-uu-part-number article)))
1147 (message "Getting article %d%s..."
1148 article (if (string= part "") "" (concat ", " part))))
1149 (gnus-summary-display-article article)
1151 ;; Push the article to the processing function.
1153 (set-buffer gnus-original-article-buffer)
1154 (let ((buffer-read-only nil))
1156 (set-buffer gnus-summary-buffer)
1158 (funcall process-function
1159 gnus-original-article-buffer state)))))
1161 (gnus-summary-remove-process-mark article)
1163 ;; If this is the beginning of a decoded file, we push it
1165 (when (or (memq 'begin process-state)
1166 (and (or (eq state 'first)
1167 (eq state 'first-and-last))
1168 (memq 'ok process-state)))
1170 ;; If there is a `result-file' here, that means that the
1171 ;; file was unsuccessfully decoded, so we delete it.
1172 (when (and result-file
1173 (file-exists-p result-file))
1174 (delete-file result-file)))
1175 (when (memq 'begin process-state)
1176 (setq result-file (car process-state)))
1177 (setq has-been-begin t))
1179 ;; Check whether we have decoded one complete file.
1180 (when (memq 'end process-state)
1181 (setq article-series nil)
1182 (setq has-been-begin nil)
1183 (push (list (cons 'name result-file)
1184 (cons 'article article))
1186 ;; Allow user-defined functions to be run on this file.
1187 (when gnus-uu-grabbed-file-functions
1188 (let ((funcs gnus-uu-grabbed-file-functions))
1189 (unless (listp funcs)
1190 (setq funcs (list funcs)))
1192 (funcall (pop funcs) result-file))))
1193 ;; Check whether we have decoded enough articles.
1194 (and limit (= (length result-files) limit)
1195 (setq articles nil)))
1197 ;; If this is the last article to be decoded, and
1198 ;; we still haven't reached the end, then we delete
1199 ;; the partially decoded file.
1200 (and (or (eq state 'last) (eq state 'first-and-last))
1201 (not (memq 'end process-state))
1203 (file-exists-p result-file)
1204 (delete-file result-file))
1206 ;; If this was a file of the wrong sort, then
1207 (when (and (or (memq 'wrong-type process-state)
1208 (memq 'error process-state))
1209 gnus-uu-unmark-articles-not-decoded)
1210 (gnus-summary-tick-article article t))
1212 ;; Set the new series state.
1213 (if (and (not has-been-begin)
1215 (or (memq 'end process-state)
1216 (memq 'middle process-state)))
1218 (setq process-state (list 'error))
1219 (message "No begin part at the beginning")
1221 (setq state 'middle)))
1223 ;; When there are no result-files, then something must be wrong.
1227 ((not has-been-begin)
1228 (message "Wrong type file"))
1229 ((memq 'error process-state)
1230 (message "An error occurred during decoding"))
1231 ((not (or (memq 'ok process-state)
1232 (memq 'end process-state)))
1233 (message "End of articles reached before end of file")))
1234 ;; Make unsuccessfully decoded articles unread.
1235 (when gnus-uu-unmark-articles-not-decoded
1236 (while article-series
1237 (gnus-summary-tick-article (pop article-series) t))))
1241 (defun gnus-uu-grab-view (file)
1242 "View FILE using the gnus-uu methods."
1243 (let ((action (gnus-uu-get-action file)))
1244 (gnus-execute-command
1245 (if (string-match "%" action)
1246 (format action file)
1247 (concat action " " file))
1248 (eq gnus-view-pseudos 'not-confirm))))
1250 (defun gnus-uu-grab-move (file)
1251 "Move FILE to somewhere."
1252 (when gnus-uu-default-dir
1253 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1254 (file-name-nondirectory file))))
1255 (rename-file file to-file)
1256 (unless (file-exists-p file)
1257 (make-symbolic-link to-file file)))))
1259 (defun gnus-uu-part-number (article)
1260 (let ((subject (mail-header-subject (gnus-summary-article-header article))))
1261 (if (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+"
1263 (substring subject (match-beginning 0) (match-end 0))
1266 (defun gnus-uu-uudecode-sentinel (process event)
1267 (delete-process (get-process process)))
1269 (defun gnus-uu-uustrip-article (process-buffer in-state)
1270 ;; Uudecodes a file asynchronously.
1271 (let ((state (list 'ok))
1272 (process-connection-type nil)
1273 start-char pst name-beg name-end)
1275 (set-buffer process-buffer)
1276 (let ((case-fold-search nil)
1277 (buffer-read-only nil))
1279 (goto-char (point-min))
1281 (if gnus-uu-kill-carriage-return
1283 (while (search-forward "\r" nil t)
1284 (delete-backward-char 1))
1285 (goto-char (point-min))))
1287 (if (not (re-search-forward gnus-uu-begin-string nil t))
1288 (if (not (re-search-forward gnus-uu-body-line nil t))
1289 (setq state (list 'wrong-type))))
1291 (if (memq 'wrong-type state)
1294 (setq start-char (point))
1296 (if (looking-at gnus-uu-begin-string)
1298 (setq name-end (match-end 1)
1299 name-beg (match-beginning 1))
1300 ;; Remove any non gnus-uu-body-line right after start.
1302 (or (looking-at gnus-uu-body-line)
1305 ;; Replace any slashes and spaces in file names before decoding
1306 (goto-char name-beg)
1307 (while (re-search-forward "/" name-end t)
1308 (replace-match ","))
1309 (goto-char name-beg)
1310 (while (re-search-forward " " name-end t)
1311 (replace-match "_"))
1312 (goto-char name-beg)
1313 (if (re-search-forward "_*$" name-end t)
1316 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
1317 (and gnus-uu-uudecode-process
1318 (setq pst (process-status
1319 (or gnus-uu-uudecode-process "nevair")))
1320 (if (or (eq pst 'stop) (eq pst 'run))
1322 (delete-process gnus-uu-uudecode-process)
1323 (gnus-uu-unmark-list-of-grabbed t))))
1324 (if (get-process "*uudecode*")
1325 (delete-process "*uudecode*"))
1326 (setq gnus-uu-uudecode-process
1329 (get-buffer-create gnus-uu-output-buffer-name)
1331 (format "cd %s ; uudecode" gnus-uu-work-dir)))
1332 (set-process-sentinel
1333 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1334 (setq state (list 'begin))
1335 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
1336 (setq state (list 'middle)))
1338 (goto-char (point-max))
1341 (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
1344 (if (looking-at gnus-uu-end-string)
1345 (setq state (cons 'end state)))
1348 (and gnus-uu-uudecode-process
1349 (setq pst (process-status
1350 (or gnus-uu-uudecode-process "nevair")))
1351 (if (or (eq pst 'run) (eq pst 'stop))
1353 (if gnus-uu-correct-stripped-uucode
1355 (gnus-uu-check-correct-stripped-uucode
1357 (goto-char (point-max))
1359 (concat gnus-uu-body-line "\\|"
1365 (process-send-region gnus-uu-uudecode-process
1369 (delete-process gnus-uu-uudecode-process)
1370 (message "gnus-uu: Couldn't uudecode")
1372 (setq state (list 'wrong-type)))))
1374 (if (memq 'end state)
1375 (accept-process-output gnus-uu-uudecode-process)))
1376 (setq state (list 'wrong-type))))
1377 (if (not gnus-uu-uudecode-process)
1378 (setq state (list 'wrong-type)))))
1380 (if (memq 'begin state)
1381 (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1384 ;; This function is used by `gnus-uu-grab-articles' to treat
1385 ;; a shared article.
1386 (defun gnus-uu-unshar-article (process-buffer in-state)
1387 (let ((state (list 'ok))
1390 (set-buffer process-buffer)
1391 (goto-char (point-min))
1392 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1393 (setq state (list 'wrong-type))
1395 (setq start-char (point))
1396 (call-process-region
1397 start-char (point-max) "sh" nil
1398 (get-buffer-create gnus-uu-output-buffer-name) nil
1399 "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1402 ;; Returns the name of what the shar file is going to unpack.
1403 (defun gnus-uu-find-name-in-shar ()
1404 (let ((oldpoint (point))
1406 (goto-char (point-min))
1407 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1408 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1409 (goto-char oldpoint)
1412 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1413 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1414 ;; found, or the name of the command to run if such a rule is found.
1415 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1416 (let ((action-list (copy-sequence file-action-list))
1417 (case-fold-search t)
1422 (and gnus-uu-ignore-files-by-name
1423 (string-match gnus-uu-ignore-files-by-name file-name)))
1425 (and gnus-uu-ignore-files-by-type
1426 (string-match gnus-uu-ignore-files-by-type
1427 (or (gnus-uu-choose-action
1428 file-name gnus-uu-ext-to-mime-list t)
1430 (while (not (or (eq action-list ()) action))
1431 (setq rule (car action-list))
1432 (setq action-list (cdr action-list))
1433 (if (string-match (car rule) file-name)
1434 (setq action (car (cdr rule))))))
1437 (defun gnus-uu-treat-archive (file-path)
1438 ;; Unpacks an archive. Returns t if unpacking is successful.
1439 (let ((did-unpack t)
1441 (setq action (gnus-uu-choose-action
1442 file-path (append gnus-uu-user-archive-rules
1443 (if gnus-uu-ignore-default-archive-rules
1445 gnus-uu-default-archive-rules))))
1447 (if (not action) (error "No unpackers for the file %s" file-path))
1449 (string-match "/[^/]*$" file-path)
1450 (setq dir (substring file-path 0 (match-beginning 0)))
1452 (if (member action gnus-uu-destructive-archivers)
1453 (copy-file file-path (concat file-path "~") t))
1455 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1458 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1461 (message "Unpacking: %s..." (gnus-uu-command action file-path))
1463 (if (= 0 (call-process "sh" nil
1464 (get-buffer-create gnus-uu-output-buffer-name)
1467 (message "Error during unpacking of archive")
1468 (setq did-unpack nil))
1470 (if (member action gnus-uu-destructive-archivers)
1471 (rename-file (concat file-path "~") file-path t))
1475 (defun gnus-uu-dir-files (dir)
1476 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1479 (if (file-directory-p (setq file (car dirs)))
1480 (setq files (append files (gnus-uu-dir-files file)))
1481 (setq files (cons file files)))
1482 (setq dirs (cdr dirs)))
1485 (defun gnus-uu-unpack-files (files &optional ignore)
1486 ;; Go through FILES and look for files to unpack.
1487 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1489 file did-unpack file-entry)
1490 (gnus-uu-add-file totfiles)
1492 (setq file (cdr (setq file-entry (assq 'name (car files)))))
1493 (if (and (not (member file ignore))
1494 (equal (gnus-uu-get-action (file-name-nondirectory file))
1497 (setq did-unpack (cons file did-unpack))
1498 (or (gnus-uu-treat-archive file)
1499 (message "Error during unpacking of %s" file))
1500 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1502 (gnus-uu-add-file newfiles)
1504 (or (member (car nfiles) totfiles)
1505 (setq ofiles (cons (list (cons 'name (car nfiles))
1506 (cons 'original file))
1508 (setq nfiles (cdr nfiles)))
1509 (setq totfiles newfiles))))
1510 (setq files (cdr files)))
1512 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1515 (defun gnus-uu-ls-r (dir)
1516 (let* ((files (gnus-uu-directory-files dir t))
1519 (if (file-directory-p (car files))
1521 (setq ofiles (delete (car files) ofiles))
1522 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1523 (setq files (cdr files)))
1528 (defun gnus-uu-directory-files (dir &optional full)
1529 (let (files out file)
1530 (setq files (directory-files dir full))
1532 (setq file (car files))
1533 (setq files (cdr files))
1534 (or (string-match "/\\.\\.?$" file)
1535 (setq out (cons file out))))
1536 (setq out (nreverse out))
1539 (defun gnus-uu-check-correct-stripped-uucode (start end)
1540 (let (found beg length)
1541 (if (not gnus-uu-correct-stripped-uucode)
1545 (if (re-search-forward " \\|`" end t)
1550 (if (looking-at "\n") (replace-match ""))
1554 (if (looking-at (concat gnus-uu-begin-string "\\|"
1555 gnus-uu-end-string))
1562 (setq length (- (point) beg))))
1567 (if (not (= length (- (point) beg)))
1568 (insert (make-string (- length (- (point) beg)) ? ))))
1569 (forward-line 1))))))
1571 (defvar gnus-uu-tmp-alist nil)
1573 (defun gnus-uu-initialize (&optional scan)
1576 (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1577 (if (file-exists-p (cdr entry))
1578 (setq gnus-uu-work-dir (cdr entry))
1579 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1582 (setq gnus-uu-tmp-dir (file-name-as-directory
1583 (expand-file-name gnus-uu-tmp-dir)))
1584 (if (not (file-directory-p gnus-uu-tmp-dir))
1585 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1586 (if (not (file-writable-p gnus-uu-tmp-dir))
1587 (error "Temp directory %s can't be written to"
1590 (setq gnus-uu-work-dir
1591 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1592 (gnus-uu-add-file gnus-uu-work-dir)
1593 (if (not (file-directory-p gnus-uu-work-dir))
1594 (gnus-make-directory gnus-uu-work-dir))
1595 (set-file-modes gnus-uu-work-dir 448)
1596 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1597 (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1598 gnus-uu-tmp-alist)))))
1601 ;; Kills the temporary uu buffers, kills any processes, etc.
1602 (defun gnus-uu-clean-up ()
1604 (and gnus-uu-uudecode-process
1605 (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1606 (if (or (eq pst 'stop) (eq pst 'run))
1607 (delete-process gnus-uu-uudecode-process)))
1608 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1609 (kill-buffer buf))))
1611 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1612 ;; hasn't been deleted, if, for instance, the user terminated decoding
1614 (defun gnus-uu-check-for-generated-files ()
1616 (while gnus-uu-generated-file-list
1617 (setq file (car gnus-uu-generated-file-list))
1618 (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1619 (if (not (string-match "/\\.[\\.]?$" file))
1621 (if (file-directory-p file)
1622 (setq dirs (cons file dirs))
1623 (if (file-exists-p file)
1624 (delete-file file))))))
1625 (setq dirs (nreverse dirs))
1627 (setq file (car dirs))
1628 (setq dirs (cdr dirs))
1629 (if (file-directory-p file)
1630 (if (string-match "/$" file)
1631 (delete-directory (substring file 0 (match-beginning 0)))
1632 (delete-directory file))))))
1634 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1635 ;; still exists upon exiting the newsgroup).
1636 (defun gnus-uu-add-file (file)
1638 (setq gnus-uu-generated-file-list
1639 (cons file gnus-uu-generated-file-list))
1640 (setq gnus-uu-generated-file-list
1641 (append file gnus-uu-generated-file-list))))
1643 ;; Inputs an action and a file and returns a full command, putting
1644 ;; quotes round the file name and escaping any quotes in the file name.
1645 (defun gnus-uu-command (action file)
1647 (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1650 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1651 (substring file (match-beginning 0) (match-end 0))))
1652 (setq file (substring file (1+ (match-beginning 0))))))
1653 (setq ofile (concat "\"" ofile file "\""))
1654 (if (string-match "%s" action)
1655 (format action ofile)
1656 (concat action " " ofile))))
1661 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1662 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1667 ;;; uuencoded posting
1673 ;; Any function that is to be used as and encoding method will take two
1674 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1675 ;; and "spiral.jpg", respectively.) The function should return nil if
1676 ;; the encoding wasn't successful.
1677 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1678 "Function used for encoding binary files.
1679 There are three functions supplied with gnus-uu for encoding files:
1680 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1681 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1682 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1683 uuencode and adds MIME headers.")
1685 (defvar gnus-uu-post-include-before-composing nil
1686 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1687 If this variable is t, you can either include an encoded file with
1688 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1690 (defvar gnus-uu-post-length 990
1691 "Maximum length of an article.
1692 The encoded file will be split into how many articles it takes to
1693 post the entire file.")
1695 (defvar gnus-uu-post-threaded nil
1696 "Non-nil means that gnus-uu will post the encoded file in a thread.
1697 This may not be smart, as no other decoder I have seen are able to
1698 follow threads when collecting uuencoded articles. (Well, I have seen
1699 one package that does that - gnus-uu, but somehow, I don't think that
1700 counts...) Default is nil.")
1702 (defvar gnus-uu-post-separate-description t
1703 "Non-nil means that the description will be posted in a separate article.
1704 The first article will typically be numbered (0/x). If this variable
1705 is nil, the description the user enters will be included at the
1706 beginning of the first article, which will be numbered (1/x). Default
1709 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1710 (defvar gnus-uu-post-message-id nil)
1711 (defvar gnus-uu-post-inserted-file-name nil)
1712 (defvar gnus-uu-winconf-post-news nil)
1714 (defun gnus-uu-post-news ()
1715 "Compose an article and post an encoded file."
1717 (setq gnus-uu-post-inserted-file-name nil)
1718 (setq gnus-uu-winconf-post-news (current-window-configuration))
1720 (gnus-summary-post-news)
1722 (use-local-map (copy-keymap (current-local-map)))
1723 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1724 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1725 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1726 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1728 (if gnus-uu-post-include-before-composing
1729 (save-excursion (setq gnus-uu-post-inserted-file-name
1730 (gnus-uu-post-insert-binary)))))
1732 (defun gnus-uu-post-insert-binary-in-article ()
1733 "Inserts an encoded file in the buffer.
1734 The user will be asked for a file name."
1736 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1737 (error "Not in post-news buffer"))
1739 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1741 ;; Encodes with uuencode and substitutes all spaces with backticks.
1742 (defun gnus-uu-post-encode-uuencode (path file-name)
1743 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1745 (goto-char (point-min))
1747 (while (re-search-forward " " nil t)
1748 (replace-match "`"))
1751 ;; Encodes with uuencode and adds MIME headers.
1752 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1753 (if (gnus-uu-post-encode-uuencode path file-name)
1755 (gnus-uu-post-make-mime file-name "x-uue")
1758 ;; Encodes with base64 and adds MIME headers
1759 (defun gnus-uu-post-encode-mime (path file-name)
1760 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1762 (gnus-uu-post-make-mime file-name "base64")
1765 ;; Adds MIME headers.
1766 (defun gnus-uu-post-make-mime (file-name encoding)
1767 (goto-char (point-min))
1768 (insert (format "Content-Type: %s; name=\"%s\"\n"
1769 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1771 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1773 (set-buffer gnus-post-news-buffer)
1774 (goto-char (point-min))
1775 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1777 (narrow-to-region 1 (point))
1778 (or (mail-fetch-field "mime-version")
1781 (insert "MIME-Version: 1.0\n")))
1784 ;; Encodes a file PATH with COMMAND, leaving the result in the
1786 (defun gnus-uu-post-encode-file (command path file-name)
1787 (= 0 (call-process "sh" nil t nil "-c"
1788 (format "%s %s %s" command path file-name))))
1790 (defun gnus-uu-post-news-inews ()
1791 "Posts the composed news article and encoded file.
1792 If no file has been included, the user will be asked for a file."
1794 (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1795 (error "Not in post news buffer"))
1799 (if gnus-uu-post-inserted-file-name
1800 (setq file-name gnus-uu-post-inserted-file-name)
1801 (setq file-name (gnus-uu-post-insert-binary)))
1803 (if gnus-uu-post-threaded
1804 (let ((gnus-required-headers
1805 (if (memq 'Message-ID gnus-required-headers)
1806 gnus-required-headers
1807 (cons 'Message-ID gnus-required-headers)))
1808 gnus-inews-article-hook)
1810 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1811 gnus-inews-article-hook
1812 (list gnus-inews-article-hook)))
1813 (setq gnus-inews-article-hook
1817 (goto-char (point-min))
1818 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1819 (setq gnus-uu-post-message-id
1821 (match-beginning 1) (match-end 1)))
1822 (setq gnus-uu-post-message-id nil))))
1823 gnus-inews-article-hook))
1824 (gnus-uu-post-encoded file-name t))
1825 (gnus-uu-post-encoded file-name nil)))
1826 (setq gnus-uu-post-inserted-file-name nil)
1827 (and gnus-uu-winconf-post-news
1828 (set-window-configuration gnus-uu-winconf-post-news)))
1830 ;; Asks for a file to encode, encodes it and inserts the result in
1831 ;; the current buffer. Returns the file name the user gave.
1832 (defun gnus-uu-post-insert-binary ()
1833 (let ((uuencode-buffer-name "*uuencode buffer*")
1834 file-path uubuf file-name)
1836 (setq file-path (read-file-name
1837 "What file do you want to encode? "))
1838 (if (not (file-exists-p file-path))
1839 (error "%s: No such file" file-path))
1841 (goto-char (point-max))
1842 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1844 (if (string-match "^~/" file-path)
1845 (setq file-path (concat "$HOME" (substring file-path 1))))
1846 (if (string-match "/[^/]*$" file-path)
1847 (setq file-name (substring file-path (1+ (match-beginning 0))))
1848 (setq file-name file-path))
1852 (set-buffer (setq uubuf
1853 (get-buffer-create uuencode-buffer-name)))
1855 (funcall gnus-uu-post-encode-method file-path file-name))
1856 (insert-buffer-substring uubuf)
1857 (error "Encoding unsuccessful"))
1858 (kill-buffer uubuf))
1861 ;; Posts the article and all of the encoded file.
1862 (defun gnus-uu-post-encoded (file-name &optional threaded)
1863 (let ((send-buffer-name "*uuencode send buffer*")
1864 (encoded-buffer-name "*encoded buffer*")
1865 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1866 (separator (concat mail-header-separator "\n\n"))
1867 uubuf length parts header i end beg
1868 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1870 (setq post-buf (current-buffer))
1872 (goto-char (point-min))
1873 (if (not (re-search-forward
1874 (if gnus-uu-post-separate-description
1875 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1877 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1878 (error "Internal error: No binary/header separator"))
1881 (setq beg-binary (point))
1882 (setq end-binary (point-max))
1885 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1887 (insert-buffer-substring post-buf beg-binary end-binary)
1888 (goto-char (point-min))
1889 (setq length (count-lines 1 (point-max)))
1890 (setq parts (/ length gnus-uu-post-length))
1891 (if (not (< (% length gnus-uu-post-length) 4))
1892 (setq parts (1+ parts))))
1894 (if gnus-uu-post-separate-description
1896 (kill-region (point) (point-max))
1898 (goto-char (point-min))
1900 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1902 (setq header (buffer-substring 1 (point)))
1904 (goto-char (point-min))
1905 (if (not gnus-uu-post-separate-description)
1907 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1910 (insert (format " (0/%d)" parts))))
1916 (while (not (> i parts))
1917 (set-buffer (get-buffer-create send-buffer-name))
1920 (if (and threaded gnus-uu-post-message-id)
1921 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1924 (- 62 (length (format top-string "" file-name i parts ""))))
1925 (if (> 1 (setq minlen (/ whole-len 2)))
1930 (make-string minlen ?-)
1933 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1935 (goto-char (point-min))
1936 (if (not (re-search-forward "^Subject: " nil t))
1941 (insert (format " (%d/%d)" i parts)))
1942 (if (or (and (= i 2) gnus-uu-post-separate-description)
1943 (and (= i 1) (not gnus-uu-post-separate-description)))
1944 (replace-match "Subject: Re: "))))
1946 (goto-char (point-max))
1951 (goto-char (point-max))
1952 (forward-line gnus-uu-post-length))
1953 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1956 (insert-buffer-substring uubuf beg end)
1961 (goto-char (point-min))
1963 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1966 (if (re-search-forward
1967 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1976 (and (setq buf (get-buffer send-buffer-name))
1978 (and (setq buf (get-buffer encoded-buffer-name))
1981 (if (not gnus-uu-post-separate-description)
1983 (set-buffer-modified-p nil)
1984 (and (fboundp 'bury-buffer) (bury-buffer))))))
1988 ;; gnus-uu.el ends here