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-work-dir nil)
270 (defconst gnus-uu-output-buffer-name " *Gnus UU Output*")
272 (defvar gnus-uu-default-dir gnus-article-save-directory)
273 (defvar gnus-uu-digest-from-subject nil)
278 (gnus-uu-mark-map "P" gnus-summary-mark-map)
279 "p" gnus-summary-mark-as-processable
280 "u" gnus-summary-unmark-as-processable
281 "U" gnus-summary-unmark-all-processable
282 "v" gnus-uu-mark-over
283 "s" gnus-uu-mark-series
284 "r" gnus-uu-mark-region
285 "R" gnus-uu-mark-by-regexp
286 "t" gnus-uu-mark-thread
287 "T" gnus-uu-unmark-thread
289 "b" gnus-uu-mark-buffer
290 "S" gnus-uu-mark-sparse)
293 (gnus-uu-extract-map "X" gnus-summary-mode-map)
294 ;;"x" gnus-uu-extract-any
295 ;;"m" gnus-uu-extract-mime
296 "u" gnus-uu-decode-uu
297 "U" gnus-uu-decode-uu-and-save
298 "s" gnus-uu-decode-unshar
299 "S" gnus-uu-decode-unshar-and-save
300 "o" gnus-uu-decode-save
301 "O" gnus-uu-decode-save
302 "b" gnus-uu-decode-binhex
303 "B" gnus-uu-decode-binhex
304 "p" gnus-uu-decode-postscript
305 "P" gnus-uu-decode-postscript-and-save)
308 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
309 "u" gnus-uu-decode-uu-view
310 "U" gnus-uu-decode-uu-and-save-view
311 "s" gnus-uu-decode-unshar-view
312 "S" gnus-uu-decode-unshar-and-save-view
313 "o" gnus-uu-decode-save-view
314 "O" gnus-uu-decode-save-view
315 "b" gnus-uu-decode-binhex-view
316 "B" gnus-uu-decode-binhex-view
317 "p" gnus-uu-decode-postscript-view
318 "P" gnus-uu-decode-postscript-and-save-view)
323 (defun gnus-uu-decode-uu (n)
324 "Uudecodes the current article."
326 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
328 (defun gnus-uu-decode-uu-and-save (n dir)
329 "Decodes and saves the resulting file."
331 (list current-prefix-arg
332 (file-name-as-directory
333 (read-file-name "Uudecode and save in dir: "
335 gnus-uu-default-dir t))))
336 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
338 (defun gnus-uu-decode-unshar (n)
339 "Unshars the current article."
341 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
343 (defun gnus-uu-decode-unshar-and-save (n dir)
344 "Unshars and saves the current article."
346 (list current-prefix-arg
347 (file-name-as-directory
348 (read-file-name "Unshar and save in dir: "
350 gnus-uu-default-dir t))))
351 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
353 (defun gnus-uu-decode-save (n file)
354 "Saves the current article."
356 (list current-prefix-arg
358 (if gnus-uu-save-separate-articles
359 "Save articles is dir: "
360 "Save articles in file: ")
362 gnus-uu-default-dir)))
363 (setq gnus-uu-saved-article-name file)
364 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
366 (defun gnus-uu-decode-binhex (n dir)
367 "Unbinhexes the current article."
369 (list current-prefix-arg
370 (file-name-as-directory
371 (read-file-name "Unbinhex and save in dir: "
373 gnus-uu-default-dir))))
374 (setq gnus-uu-binhex-article-name
375 (make-temp-name (concat gnus-uu-work-dir "binhex")))
376 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
378 (defun gnus-uu-decode-uu-view (n)
379 "Uudecodes and views the current article."
381 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
382 (gnus-uu-decode-uu n)))
384 (defun gnus-uu-decode-uu-and-save-view (n dir)
385 "Decodes, views and saves the resulting file."
387 (list current-prefix-arg
388 (read-file-name "Uudecode, view and save in dir: "
390 gnus-uu-default-dir t)))
391 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
392 (gnus-uu-decode-uu-and-save n dir)))
394 (defun gnus-uu-decode-unshar-view (n)
395 "Unshars and views the current article."
397 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
398 (gnus-uu-decode-unshar n)))
400 (defun gnus-uu-decode-unshar-and-save-view (n dir)
401 "Unshars and saves the current article."
403 (list current-prefix-arg
404 (read-file-name "Unshar, view and save in dir: "
406 gnus-uu-default-dir t)))
407 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
408 (gnus-uu-decode-unshar-and-save n dir)))
410 (defun gnus-uu-decode-save-view (n file)
411 "Saves and views the current article."
413 (list current-prefix-arg
414 (read-file-name (if gnus-uu-save-separate-articles
415 "Save articles is dir: "
416 "Save articles in file: ")
417 gnus-uu-default-dir gnus-uu-default-dir)))
418 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
419 (gnus-uu-decode-save n file)))
421 (defun gnus-uu-decode-binhex-view (n file)
422 "Unbinhexes and views the current article."
424 (list current-prefix-arg
425 (read-file-name "Unbinhex, view and save in dir: "
426 gnus-uu-default-dir gnus-uu-default-dir)))
427 (setq gnus-uu-binhex-article-name
428 (make-temp-name (concat gnus-uu-work-dir "binhex")))
429 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
430 (gnus-uu-decode-binhex n file)))
433 ;; Digest and forward articles
435 (defun gnus-uu-digest-mail-forward (n &optional post)
436 "Digests and forwards all articles in this series."
438 (let ((gnus-uu-save-in-digest t)
439 (file (make-temp-name (concat gnus-uu-tmp-dir "forward")))
441 (setq gnus-uu-digest-from-subject nil)
442 (gnus-uu-decode-save n file)
443 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
444 (gnus-add-current-to-buffer-list)
446 (delete-other-windows)
448 (let ((fs gnus-uu-digest-from-subject))
452 subject (gnus-simplify-subject-fuzzy (cdar fs))
454 (while (and fs (or from subject))
456 (or (string= from (caar fs))
459 (or (string= (gnus-simplify-subject-fuzzy (cdar fs))
463 (or subject (setq subject "Digested Articles"))
464 (or from (setq from "Various")))
465 (goto-char (point-min))
466 (and (re-search-forward "^Subject: ")
468 (delete-region (point) (gnus-point-at-eol))
470 (goto-char (point-min))
471 (and (re-search-forward "^From: ")
473 (delete-region (point) (gnus-point-at-eol))
475 (message-forward post)
478 (setq gnus-uu-digest-from-subject nil)))
480 (defun gnus-uu-digest-post-forward (n)
481 "Digest and forward to a newsgroup."
483 (gnus-uu-digest-mail-forward n t))
487 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
488 "Ask for a regular expression and set the process mark on all articles that match."
489 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
490 (gnus-set-global-variables)
491 (let ((articles (gnus-uu-find-articles-matching regexp)))
494 (gnus-summary-remove-process-mark (pop articles))
495 (gnus-summary-set-process-mark (pop articles))))
497 (gnus-summary-position-point))
499 (defun gnus-uu-unmark-by-regexp (regexp &optional unmark)
500 "Ask for a regular expression and remove the process mark on all articles that match."
501 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
502 (gnus-uu-mark-by-regexp regexp t))
504 (defun gnus-uu-mark-series ()
505 "Mark the current series with the process mark."
507 (gnus-set-global-variables)
508 (let ((articles (gnus-uu-find-articles-matching)))
510 (gnus-summary-set-process-mark (car articles))
511 (setq articles (cdr articles)))
513 (gnus-summary-position-point))
515 (defun gnus-uu-mark-region (beg end &optional unmark)
516 "Set the process mark on all articles between point and mark."
518 (gnus-set-global-variables)
521 (while (< (point) end)
523 (gnus-summary-remove-process-mark (gnus-summary-article-number))
524 (gnus-summary-set-process-mark (gnus-summary-article-number)))
526 (gnus-summary-position-point))
528 (defun gnus-uu-unmark-region (beg end)
529 "Remove the process mark from all articles between point and mark."
531 (gnus-uu-mark-region beg end t))
533 (defun gnus-uu-mark-buffer ()
534 "Set the process mark on all articles in the buffer."
536 (gnus-uu-mark-region (point-min) (point-max)))
538 (defun gnus-uu-unmark-buffer ()
539 "Remove the process mark on all articles in the buffer."
541 (gnus-uu-mark-region (point-min) (point-max) t))
543 (defun gnus-uu-mark-thread ()
544 "Marks all articles downwards in this thread."
546 (gnus-set-global-variables)
547 (let ((level (gnus-summary-thread-level)))
548 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
549 (zerop (gnus-summary-next-subject 1))
550 (> (gnus-summary-thread-level) level))))
551 (gnus-summary-position-point))
553 (defun gnus-uu-unmark-thread ()
554 "Unmarks all articles downwards in this thread."
556 (gnus-set-global-variables)
557 (let ((level (gnus-summary-thread-level)))
558 (while (and (gnus-summary-remove-process-mark
559 (gnus-summary-article-number))
560 (zerop (gnus-summary-next-subject 1))
561 (> (gnus-summary-thread-level) level))))
562 (gnus-summary-position-point))
564 (defun gnus-uu-mark-over (score)
565 "Mark all articles with a score over SCORE (the prefix.)"
567 (let ((score (gnus-score-default score))
568 (data gnus-newsgroup-data))
571 (when (> (or (cdr (assq (gnus-data-number (caar data))
572 gnus-newsgroup-scored))
573 gnus-summary-default-score 0)
575 (gnus-summary-set-process-mark (caar data)))
576 (setq data (cdr data))))
577 (gnus-summary-position-point)))
579 (defun gnus-uu-mark-sparse ()
580 "Mark all series that have some articles marked."
582 (gnus-set-global-variables)
583 (let ((marked (nreverse gnus-newsgroup-processable))
584 subject articles total headers)
585 (or marked (error "No articles marked with the process mark"))
586 (setq gnus-newsgroup-processable nil)
589 (and (setq headers (gnus-summary-article-header (car marked)))
590 (setq subject (mail-header-subject headers)
591 articles (gnus-uu-find-articles-matching
592 (gnus-uu-reginize-string subject))
593 total (nconc total articles)))
595 (gnus-summary-set-process-mark (car articles))
596 (setcdr marked (delq (car articles) (cdr marked)))
597 (setq articles (cdr articles)))
598 (setq marked (cdr marked)))
599 (setq gnus-newsgroup-processable (nreverse total)))
600 (gnus-summary-position-point)))
602 (defun gnus-uu-mark-all ()
603 "Mark all articles in \"series\" order."
605 (gnus-set-global-variables)
606 (setq gnus-newsgroup-processable nil)
608 (let ((data gnus-newsgroup-data)
611 (unless (memq (setq number (gnus-data-number (car data)))
612 gnus-newsgroup-processable)
613 (gnus-summary-goto-subject number)
614 (gnus-uu-mark-series))
615 (setq data (cdr data)))))
616 (gnus-summary-position-point))
618 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
620 (defun gnus-uu-decode-postscript (n)
621 "Gets postscript of the current article."
623 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
625 (defun gnus-uu-decode-postscript-view (n)
626 "Gets and views the current article."
628 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
629 (gnus-uu-decode-postscript n)))
631 (defun gnus-uu-decode-postscript-and-save (n dir)
632 "Extracts postscript and saves the current article."
634 (list current-prefix-arg
635 (file-name-as-directory
636 (read-file-name "Save in dir: "
638 gnus-uu-default-dir t))))
639 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
642 (defun gnus-uu-decode-postscript-and-save-view (n dir)
643 "Decodes, views and saves the resulting file."
645 (list current-prefix-arg
646 (read-file-name "Where do you want to save the file(s)? "
648 gnus-uu-default-dir t)))
649 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
650 (gnus-uu-decode-postscript-and-save n dir)))
653 ;; Internal functions.
655 (defun gnus-uu-decode-with-method (method n &optional save not-insert
657 (gnus-uu-initialize scan)
658 (if save (setq gnus-uu-default-dir save))
659 ;; Create the directory we save to.
660 (when (and scan cdir save
661 (not (file-exists-p save)))
662 (make-directory save t))
663 (let ((articles (gnus-uu-get-list-of-articles n))
665 (setq files (gnus-uu-grab-articles articles method t))
666 (let ((gnus-current-article (car articles)))
667 (and scan (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
668 (and save (gnus-uu-save-files files save))
669 (if (eq gnus-uu-do-not-unpack-archives nil)
670 (setq files (gnus-uu-unpack-files files)))
671 (setq files (nreverse (gnus-uu-get-actions files)))
672 (or not-insert (not gnus-insert-pseudo-articles)
673 (gnus-summary-insert-pseudos files save))))
675 (defun gnus-uu-scan-directory (dir &optional rec)
676 "Return a list of all files under DIR."
677 (let ((files (directory-files dir t))
679 (while (setq file (pop files))
680 (unless (string-match "/\\.\\.?$" file)
681 (push (list (cons 'name file)
682 (cons 'article gnus-current-article))
684 (when (file-directory-p file)
685 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
690 (defun gnus-uu-save-files (files dir)
692 (let ((len (length files))
693 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
694 to-file file fromdir)
695 (while (setq file (cdr (assq 'name (pop files))))
696 (when (file-exists-p file)
697 (string-match reg file)
698 (setq fromdir (substring file (match-end 0)))
699 (if (file-directory-p file)
700 (unless (file-exists-p (concat dir fromdir))
701 (make-directory (concat dir fromdir) t))
702 (setq to-file (concat dir fromdir))
703 (when (or (not (file-exists-p to-file))
704 (gnus-y-or-n-p (format "%s exists; overwrite? " to-file)))
705 (copy-file file to-file t t)))))
706 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
708 ;; Functions for saving and possibly digesting articles without
711 ;; Function called by gnus-uu-grab-articles to treat each article.
712 (defun gnus-uu-save-article (buffer in-state)
714 (gnus-uu-save-separate-articles
717 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
718 gnus-current-article))
719 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
720 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
722 ((eq in-state 'last) (list 'end))
723 (t (list 'middle)))))
724 ((not gnus-uu-save-in-digest)
727 (write-region 1 (point-max) gnus-uu-saved-article-name t)
728 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
729 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
731 ((eq in-state 'last) (list 'end))
732 (t (list 'middle)))))
734 (let ((header (gnus-summary-article-header)))
735 (setq gnus-uu-digest-from-subject
736 (cons (cons (mail-header-from header)
737 (mail-header-subject header))
738 gnus-uu-digest-from-subject)))
739 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
740 (delim (concat "^" (make-string 30 ?-) "$"))
741 beg subj headers headline sorthead body end-string state)
742 (if (or (eq in-state 'first)
743 (eq in-state 'first-and-last))
745 (setq state (list 'begin))
746 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
749 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
752 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
753 (current-time-string) name name))))
754 (if (not (eq in-state 'end))
755 (setq state (list 'middle))))
757 (set-buffer (get-buffer "*gnus-uu-body*"))
758 (goto-char (setq beg (point-max)))
762 (let (buffer-read-only)
763 (gnus-set-text-properties (point-min) (point-max) nil)
764 ;; These two are necessary for XEmacs 19.12 fascism.
765 (put-text-property (point-min) (point-max) 'invisible nil)
766 (put-text-property (point-min) (point-max) 'intangible nil))
767 (goto-char (point-min))
768 (re-search-forward "\n\n")
769 ;; Quote all 30-dash lines.
771 (while (re-search-forward delim nil t)
775 (setq body (buffer-substring (1- (point)) (point-max)))
776 (narrow-to-region (point-min) (point))
777 (if (not (setq headers gnus-uu-digest-headers))
778 (setq sorthead (buffer-substring (point-min) (point-max)))
780 (setq headline (car headers))
781 (setq headers (cdr headers))
782 (goto-char (point-min))
783 (while (re-search-forward headline nil t)
788 (or (and (re-search-forward "^[^ \t]" nil t)
790 (progn (forward-line 1) (point)))))))))
792 (insert sorthead) (goto-char (point-max))
793 (insert body) (goto-char (point-max))
794 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
796 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
798 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
800 (set-buffer (get-buffer "*gnus-uu-pre*"))
801 (insert (format " %s\n" subj))))))
802 (if (or (eq in-state 'last)
803 (eq in-state 'first-and-last))
806 (set-buffer (get-buffer "*gnus-uu-pre*"))
807 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
808 (write-region 1 (point-max) gnus-uu-saved-article-name))
810 (set-buffer (get-buffer "*gnus-uu-body*"))
811 (goto-char (point-max))
813 (concat (setq end-string (format "End of %s Digest" name))
815 (insert (concat (make-string (length end-string) ?*) "\n"))
816 (write-region 1 (point-max) gnus-uu-saved-article-name t))
817 (kill-buffer (get-buffer "*gnus-uu-pre*"))
818 (kill-buffer (get-buffer "*gnus-uu-body*"))
819 (setq state (cons 'end state))))
820 (if (memq 'begin state)
821 (cons gnus-uu-saved-article-name state)
824 ;; Binhex treatment - not very advanced.
826 (defconst gnus-uu-binhex-body-line
827 "^[^:]...............................................................$")
828 (defconst gnus-uu-binhex-begin-line
829 "^:...............................................................$")
830 (defconst gnus-uu-binhex-end-line
833 (defun gnus-uu-binhex-article (buffer in-state)
834 (let (state start-char)
838 (goto-char (point-min))
839 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
840 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
841 (setq state (list 'wrong-type))))
843 (if (memq 'wrong-type state)
846 (setq start-char (point))
847 (if (looking-at gnus-uu-binhex-begin-line)
849 (setq state (list 'begin))
850 (write-region 1 1 gnus-uu-binhex-article-name))
851 (setq state (list 'middle)))
852 (goto-char (point-max))
853 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
854 gnus-uu-binhex-end-line) nil t)
855 (if (looking-at gnus-uu-binhex-end-line)
856 (setq state (if (memq 'begin state)
861 (if (file-exists-p gnus-uu-binhex-article-name)
862 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
863 (if (memq 'begin state)
864 (cons gnus-uu-binhex-article-name state)
869 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
870 (let ((state (list 'ok))
871 start-char end-char file-name)
873 (set-buffer process-buffer)
874 (goto-char (point-min))
875 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
876 (setq state (list 'wrong-type))
878 (setq start-char (point))
879 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
880 (setq state (list 'wrong-type))
881 (setq end-char (point))
882 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
883 (insert-buffer-substring process-buffer start-char end-char)
884 (setq file-name (concat gnus-uu-work-dir
885 (cdr gnus-article-current) ".ps"))
886 (write-region (point-min) (point-max) file-name)
887 (setq state (list file-name 'begin 'end)))))
893 (defun gnus-uu-get-actions (files)
897 (setq name (cdr (assq 'name (car files))))
899 (setq action (gnus-uu-get-action name))
900 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
901 (cons 'action "file")
902 (cons 'action action))
903 (cons 'execute (gnus-uu-command
906 (setq files (cdr files)))
909 (defun gnus-uu-get-action (file-name)
912 (gnus-uu-choose-action
915 gnus-uu-user-view-rules
916 (if gnus-uu-ignore-default-view-rules
918 gnus-uu-default-view-rules)
919 gnus-uu-user-view-rules-end)))
920 (if (and (not (string= (or action "") "gnus-uu-archive"))
921 gnus-uu-view-with-metamail)
923 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
924 (setq action (format "metamail -d -b -c \"%s\"" action))))
928 ;; Functions for treating subjects and collecting series.
930 (defun gnus-uu-reginize-string (string)
931 ;; Takes a string and puts a \ in front of every special character;
932 ;; ignores any leading "version numbers" thingies that they use in
933 ;; the comp.binaries groups, and either replaces anything that looks
934 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
935 ;; like that, replaces the last two numbers with "[0-9]+". This, in
936 ;; my experience, should get most postings of a series.
938 (vernum "v[0-9]+[a-z][0-9]+:")
941 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
942 (buffer-disable-undo (current-buffer))
944 (insert (regexp-quote string))
947 (setq case-fold-search nil)
948 (goto-char (point-min))
949 (if (looking-at vernum)
951 (replace-match vernum t t)
952 (setq beg (length vernum))))
955 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
956 (replace-match " [0-9]+/[0-9]+")
959 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
960 (replace-match "[0-9]+ of [0-9]+")
963 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
966 (< 1 (goto-char (1- (point))))))
967 (re-search-forward "[0-9]+" nil t)
968 (replace-match "[0-9]+")
970 (setq count (1- count)))))
973 (while (re-search-forward "[ \t]+" nil t)
974 (replace-match "[ \t]*" t t))
976 (buffer-substring 1 (point-max)))))
978 (defun gnus-uu-get-list-of-articles (n)
979 ;; If N is non-nil, the article numbers of the N next articles
981 ;; If any articles have been marked as processable, they will be
983 ;; Failing that, articles that have subjects that are part of the
984 ;; same "series" as the current will be returned.
988 (let ((backward (< n 0))
992 (setq articles (cons (gnus-summary-article-number)
994 (gnus-summary-search-forward nil nil backward))
996 (nreverse articles)))
997 (gnus-newsgroup-processable
998 (reverse gnus-newsgroup-processable))
1000 (gnus-uu-find-articles-matching)))))
1002 (defun gnus-uu-string< (l1 l2)
1003 (string< (car l1) (car l2)))
1005 (defun gnus-uu-find-articles-matching
1006 (&optional subject only-unread do-not-translate)
1007 ;; Finds all articles that matches the regexp SUBJECT. If it is
1008 ;; nil, the current article name will be used. If ONLY-UNREAD is
1009 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1010 ;; non-nil, article names are not equalized before sorting.
1011 (let ((subject (or subject
1012 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1017 ;; Collect all subjects matching subject.
1018 (let ((case-fold-search t)
1019 (data gnus-newsgroup-data)
1023 (and (not (gnus-data-pseudo-p d))
1024 (or (not only-unread)
1025 (= (setq mark (gnus-data-mark d))
1027 (= mark gnus-ticked-mark)
1028 (= mark gnus-dormant-mark))
1029 (setq subj (mail-header-subject (gnus-data-header d)))
1030 (string-match subject subj)
1031 (setq list-of-subjects
1032 (cons (cons subj (gnus-data-number d))
1033 list-of-subjects)))))
1035 ;; Expand numbers, sort, and return the list of article
1037 (mapcar (lambda (sub) (cdr sub))
1038 (sort (gnus-uu-expand-numbers
1040 (not do-not-translate))
1041 'gnus-uu-string<))))))
1043 (defun gnus-uu-expand-numbers (string-list &optional translate)
1044 ;; Takes a list of strings and "expands" all numbers in all the
1045 ;; strings. That is, this function makes all numbers equal length by
1046 ;; prepending lots of zeroes before each number. This is to ease later
1047 ;; sorting to find out what sequence the articles are supposed to be
1048 ;; decoded in. Returns the list of expanded strings.
1049 (let ((out-list string-list)
1052 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1053 (buffer-disable-undo (current-buffer))
1056 (insert (caar string-list))
1057 ;; Translate multiple spaces to one space.
1058 (goto-char (point-min))
1059 (while (re-search-forward "[ \t]+" nil t)
1060 (replace-match " "))
1061 ;; Translate all characters to "a".
1062 (goto-char (point-min))
1064 (while (re-search-forward "[A-Za-z]" nil t)
1065 (replace-match "a" t t)))
1067 (goto-char (point-min))
1068 (while (re-search-forward "[0-9]+" nil t)
1071 (string-to-int (buffer-substring
1072 (match-beginning 0) (match-end 0))))))
1073 (setq string (buffer-substring 1 (point-max)))
1074 (setcar (car string-list) string)
1075 (setq string-list (cdr string-list))))
1079 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1080 ;; function. It takes a list of articles to be grabbed and a function
1081 ;; to apply to each article.
1083 ;; The function to be called should take two parameters. The first
1084 ;; parameter is the article buffer. The function should leave the
1085 ;; result, if any, in this buffer. Most treatment functions will just
1086 ;; generate files...
1088 ;; The second parameter is the state of the list of articles, and can
1089 ;; have four values: `first', `middle', `last' and `first-and-last'.
1091 ;; The function should return a list. The list may contain the
1092 ;; following symbols:
1093 ;; `error' if an error occurred
1094 ;; `begin' if the beginning of an encoded file has been received
1095 ;; If the list returned contains a `begin', the first element of
1096 ;; the list *must* be a string with the file name of the decoded
1098 ;; `end' if the the end of an encoded file has been received
1099 ;; `middle' if the article was a body part of an encoded file
1100 ;; `wrong-type' if the article was not a part of an encoded file
1101 ;; `ok', which can be used everything is ok
1103 (defvar gnus-uu-has-been-grabbed nil)
1105 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1107 (if (not (and gnus-uu-has-been-grabbed
1108 gnus-uu-unmark-articles-not-decoded))
1110 (if dont-unmark-last-article
1112 (setq art (car gnus-uu-has-been-grabbed))
1113 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
1114 (while gnus-uu-has-been-grabbed
1115 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1116 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1117 (if dont-unmark-last-article
1118 (setq gnus-uu-has-been-grabbed (list art))))))
1120 ;; This function takes a list of articles and a function to apply to
1121 ;; each article grabbed.
1123 ;; This function returns a list of files decoded if the grabbing and
1124 ;; the process-function has been successful and nil otherwise.
1125 (defun gnus-uu-grab-articles
1126 (articles process-function &optional sloppy limit no-errors)
1127 (let ((state 'first)
1128 has-been-begin article result-file result-files process-state
1129 gnus-summary-display-article-function
1130 article-series files)
1132 (while (and articles
1133 (not (memq 'error process-state))
1135 (not (memq 'end process-state))))
1137 (setq article (pop articles))
1138 (push article article-series)
1141 (if (eq state 'first)
1142 (setq state 'first-and-last)
1143 (setq state 'last)))
1145 (let ((part (gnus-uu-part-number article)))
1146 (gnus-message 6 "Getting article %d%s..."
1147 article (if (string= part "") "" (concat ", " part))))
1148 (gnus-summary-display-article article)
1150 ;; Push the article to the processing function.
1152 (set-buffer gnus-original-article-buffer)
1153 (let ((buffer-read-only nil))
1155 (set-buffer gnus-summary-buffer)
1157 (funcall process-function
1158 gnus-original-article-buffer state)))))
1160 (gnus-summary-remove-process-mark article)
1162 ;; If this is the beginning of a decoded file, we push it
1164 (when (or (memq 'begin process-state)
1165 (and (or (eq state 'first)
1166 (eq state 'first-and-last))
1167 (memq 'ok process-state)))
1169 ;; If there is a `result-file' here, that means that the
1170 ;; file was unsuccessfully decoded, so we delete it.
1171 (when (and result-file
1172 (file-exists-p result-file))
1173 (delete-file result-file)))
1174 (when (memq 'begin process-state)
1175 (setq result-file (car process-state)))
1176 (setq has-been-begin t))
1178 ;; Check whether we have decoded one complete file.
1179 (when (memq 'end process-state)
1180 (setq article-series nil)
1181 (setq has-been-begin nil)
1182 (if (stringp result-file)
1183 (setq files (list result-file))
1184 (setq files result-file))
1185 (setq result-file (car files))
1187 (push (list (cons 'name (pop files))
1188 (cons 'article article))
1190 ;; Allow user-defined functions to be run on this file.
1191 (when gnus-uu-grabbed-file-functions
1192 (let ((funcs gnus-uu-grabbed-file-functions))
1193 (unless (listp funcs)
1194 (setq funcs (list funcs)))
1196 (funcall (pop funcs) result-file))))
1197 ;; Check whether we have decoded enough articles.
1198 (and limit (= (length result-files) limit)
1199 (setq articles nil)))
1201 ;; If this is the last article to be decoded, and
1202 ;; we still haven't reached the end, then we delete
1203 ;; the partially decoded file.
1204 (and (or (eq state 'last) (eq state 'first-and-last))
1205 (not (memq 'end process-state))
1207 (file-exists-p result-file)
1208 (delete-file result-file))
1210 ;; If this was a file of the wrong sort, then
1211 (when (and (or (memq 'wrong-type process-state)
1212 (memq 'error process-state))
1213 gnus-uu-unmark-articles-not-decoded)
1214 (gnus-summary-tick-article article t))
1216 ;; Set the new series state.
1217 (if (and (not has-been-begin)
1219 (or (memq 'end process-state)
1220 (memq 'middle process-state)))
1222 (setq process-state (list 'error))
1223 (gnus-message 2 "No begin part at the beginning")
1225 (setq state 'middle)))
1227 ;; When there are no result-files, then something must be wrong.
1231 ((not has-been-begin)
1232 (gnus-message 2 "Wrong type file"))
1233 ((memq 'error process-state)
1234 (gnus-message 2 "An error occurred during decoding"))
1235 ((not (or (memq 'ok process-state)
1236 (memq 'end process-state)))
1237 (gnus-message 2 "End of articles reached before end of file")))
1238 ;; Make unsuccessfully decoded articles unread.
1239 (when gnus-uu-unmark-articles-not-decoded
1240 (while article-series
1241 (gnus-summary-tick-article (pop article-series) t))))
1245 (defun gnus-uu-grab-view (file)
1246 "View FILE using the gnus-uu methods."
1247 (let ((action (gnus-uu-get-action file)))
1248 (gnus-execute-command
1249 (if (string-match "%" action)
1250 (format action file)
1251 (concat action " " file))
1252 (eq gnus-view-pseudos 'not-confirm))))
1254 (defun gnus-uu-grab-move (file)
1255 "Move FILE to somewhere."
1256 (when gnus-uu-default-dir
1257 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1258 (file-name-nondirectory file))))
1259 (rename-file file to-file)
1260 (unless (file-exists-p file)
1261 (make-symbolic-link to-file file)))))
1263 (defun gnus-uu-part-number (article)
1264 (let* ((header (gnus-summary-article-header article))
1265 (subject (and header (mail-header-subject header))))
1267 (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+" subject))
1268 (match-string 0 subject)
1271 (defun gnus-uu-uudecode-sentinel (process event)
1272 (delete-process (get-process process)))
1274 (defun gnus-uu-uustrip-article (process-buffer in-state)
1275 ;; Uudecodes a file asynchronously.
1277 (set-buffer process-buffer)
1278 (let ((state (list 'wrong-type))
1279 process-connection-type case-fold-search buffer-read-only
1281 (goto-char (point-min))
1283 ;; Deal with ^M at the end of the lines.
1284 (when gnus-uu-kill-carriage-return
1286 (while (search-forward "\r" nil t)
1287 (delete-backward-char 1))))
1289 (while (or (re-search-forward gnus-uu-begin-string nil t)
1290 (re-search-forward gnus-uu-body-line nil t))
1291 (setq state (list 'ok))
1292 ;; Ok, we are at the first uucoded line.
1294 (setq start-char (point))
1296 (if (not (looking-at gnus-uu-begin-string))
1297 (setq state (list 'middle))
1298 ;; This is the beginning of an uuencoded article.
1299 ;; We replace certain characters that could make things messy.
1300 (setq gnus-uu-file-name
1301 (let ((nnheader-file-name-translation-alist
1302 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1303 (nnheader-translate-file-chars (match-string 1))))
1305 ;; Remove any non gnus-uu-body-line right after start.
1307 (while (and (not (eobp))
1308 (not (looking-at gnus-uu-body-line)))
1311 ;; If a process is running, we kill it.
1312 (when (and gnus-uu-uudecode-process
1313 (memq (process-status gnus-uu-uudecode-process)
1315 (delete-process gnus-uu-uudecode-process)
1316 (gnus-uu-unmark-list-of-grabbed t))
1318 ;; Start a new uudecoding process.
1319 (setq gnus-uu-uudecode-process
1322 (get-buffer-create gnus-uu-output-buffer-name)
1324 (format "cd %s ; uudecode" gnus-uu-work-dir)))
1325 (set-process-sentinel
1326 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1327 (setq state (list 'begin))
1328 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1330 ;; We look for the end of the thing to be decoded.
1331 (if (re-search-forward gnus-uu-end-string nil t)
1332 (setq state (cons 'end state))
1333 (goto-char (point-max))
1334 (re-search-backward gnus-uu-body-line nil t))
1338 (when gnus-uu-uudecode-process
1339 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1340 ;; Try to correct mishandled uucode.
1341 (when gnus-uu-correct-stripped-uucode
1342 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1344 ;; Send the text to the process.
1346 (process-send-region
1347 gnus-uu-uudecode-process start-char (point))
1350 (delete-process gnus-uu-uudecode-process)
1351 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1352 (setq state (list 'wrong-type)))))
1354 (if (memq 'end state)
1356 ;; Send an EOF, just in case.
1358 (process-send-eof gnus-uu-uudecode-process)
1360 (while (memq (process-status gnus-uu-uudecode-process)
1362 (accept-process-output gnus-uu-uudecode-process 1)))
1363 (when (or (not gnus-uu-uudecode-process)
1364 (not (memq (process-status gnus-uu-uudecode-process)
1366 (setq state (list 'wrong-type)))))))
1368 (if (memq 'begin state)
1369 (cons (if (= (length files) 1) (car files) files) state)
1372 ;; This function is used by `gnus-uu-grab-articles' to treat
1373 ;; a shared article.
1374 (defun gnus-uu-unshar-article (process-buffer in-state)
1375 (let ((state (list 'ok))
1378 (set-buffer process-buffer)
1379 (goto-char (point-min))
1380 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1381 (setq state (list 'wrong-type))
1383 (setq start-char (point))
1384 (call-process-region
1385 start-char (point-max) "sh" nil
1386 (get-buffer-create gnus-uu-output-buffer-name) nil
1387 "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1390 ;; Returns the name of what the shar file is going to unpack.
1391 (defun gnus-uu-find-name-in-shar ()
1392 (let ((oldpoint (point))
1394 (goto-char (point-min))
1395 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1396 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1397 (goto-char oldpoint)
1400 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1401 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1402 ;; found, or the name of the command to run if such a rule is found.
1403 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1404 (let ((action-list (copy-sequence file-action-list))
1405 (case-fold-search t)
1410 (and gnus-uu-ignore-files-by-name
1411 (string-match gnus-uu-ignore-files-by-name file-name)))
1413 (and gnus-uu-ignore-files-by-type
1414 (string-match gnus-uu-ignore-files-by-type
1415 (or (gnus-uu-choose-action
1416 file-name gnus-uu-ext-to-mime-list t)
1418 (while (not (or (eq action-list ()) action))
1419 (setq rule (car action-list))
1420 (setq action-list (cdr action-list))
1421 (if (string-match (car rule) file-name)
1422 (setq action (cadr rule)))))
1425 (defun gnus-uu-treat-archive (file-path)
1426 ;; Unpacks an archive. Returns t if unpacking is successful.
1427 (let ((did-unpack t)
1429 (setq action (gnus-uu-choose-action
1430 file-path (append gnus-uu-user-archive-rules
1431 (if gnus-uu-ignore-default-archive-rules
1433 gnus-uu-default-archive-rules))))
1435 (if (not action) (error "No unpackers for the file %s" file-path))
1437 (string-match "/[^/]*$" file-path)
1438 (setq dir (substring file-path 0 (match-beginning 0)))
1440 (if (member action gnus-uu-destructive-archivers)
1441 (copy-file file-path (concat file-path "~") t))
1443 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1446 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1449 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1451 (if (= 0 (call-process "sh" nil
1452 (get-buffer-create gnus-uu-output-buffer-name)
1455 (gnus-message 2 "Error during unpacking of archive")
1456 (setq did-unpack nil))
1458 (if (member action gnus-uu-destructive-archivers)
1459 (rename-file (concat file-path "~") file-path t))
1463 (defun gnus-uu-dir-files (dir)
1464 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1467 (if (file-directory-p (setq file (car dirs)))
1468 (setq files (append files (gnus-uu-dir-files file)))
1469 (setq files (cons file files)))
1470 (setq dirs (cdr dirs)))
1473 (defun gnus-uu-unpack-files (files &optional ignore)
1474 ;; Go through FILES and look for files to unpack.
1475 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1479 (setq file (cdr (assq 'name (car files))))
1480 (if (and (not (member file ignore))
1481 (equal (gnus-uu-get-action (file-name-nondirectory file))
1484 (setq did-unpack (cons file did-unpack))
1485 (or (gnus-uu-treat-archive file)
1486 (gnus-message 2 "Error during unpacking of %s" file))
1487 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1490 (or (member (car nfiles) totfiles)
1491 (setq ofiles (cons (list (cons 'name (car nfiles))
1492 (cons 'original file))
1494 (setq nfiles (cdr nfiles)))
1495 (setq totfiles newfiles))))
1496 (setq files (cdr files)))
1498 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1501 (defun gnus-uu-ls-r (dir)
1502 (let* ((files (gnus-uu-directory-files dir t))
1505 (if (file-directory-p (car files))
1507 (setq ofiles (delete (car files) ofiles))
1508 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1509 (setq files (cdr files)))
1514 (defun gnus-uu-directory-files (dir &optional full)
1515 (let (files out file)
1516 (setq files (directory-files dir full))
1518 (setq file (car files))
1519 (setq files (cdr files))
1520 (or (string-match "/\\.\\.?$" file)
1521 (setq out (cons file out))))
1522 (setq out (nreverse out))
1525 (defun gnus-uu-check-correct-stripped-uucode (start end)
1527 (let (found beg length)
1528 (if (not gnus-uu-correct-stripped-uucode)
1532 (if (re-search-forward " \\|`" end t)
1537 (if (looking-at "\n") (replace-match ""))
1541 (if (looking-at (concat gnus-uu-begin-string "\\|"
1542 gnus-uu-end-string))
1549 (setq length (- (point) beg))))
1554 (if (not (= length (- (point) beg)))
1555 (insert (make-string (- length (- (point) beg)) ? ))))
1556 (forward-line 1)))))))
1558 (defvar gnus-uu-tmp-alist nil)
1560 (defun gnus-uu-initialize (&optional scan)
1563 (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1564 (if (file-exists-p (cdr entry))
1565 (setq gnus-uu-work-dir (cdr entry))
1566 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1569 (setq gnus-uu-tmp-dir (file-name-as-directory
1570 (expand-file-name gnus-uu-tmp-dir)))
1571 (if (not (file-directory-p gnus-uu-tmp-dir))
1572 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1573 (if (not (file-writable-p gnus-uu-tmp-dir))
1574 (error "Temp directory %s can't be written to"
1577 (setq gnus-uu-work-dir
1578 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1579 (if (not (file-directory-p gnus-uu-work-dir))
1580 (gnus-make-directory gnus-uu-work-dir))
1581 (set-file-modes gnus-uu-work-dir 448)
1582 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1583 (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1584 gnus-uu-tmp-alist)))))
1587 ;; Kills the temporary uu buffers, kills any processes, etc.
1588 (defun gnus-uu-clean-up ()
1590 (and gnus-uu-uudecode-process
1591 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1593 (delete-process gnus-uu-uudecode-process))
1594 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1595 (kill-buffer buf))))
1597 ;; Inputs an action and a file and returns a full command, putting
1598 ;; quotes round the file name and escaping any quotes in the file name.
1599 (defun gnus-uu-command (action file)
1601 (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1604 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1605 (substring file (match-beginning 0) (match-end 0))))
1606 (setq file (substring file (1+ (match-beginning 0))))))
1607 (setq ofile (concat "\"" ofile file "\""))
1608 (if (string-match "%s" action)
1609 (format action ofile)
1610 (concat action " " ofile))))
1612 (defun gnus-uu-delete-work-dir (&optional dir)
1613 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1615 (setq dir gnus-uu-work-dir))
1616 (gnus-message 7 "Deleting directory %s..." dir)
1618 (file-exists-p dir))
1619 (let ((files (directory-files dir t nil t))
1621 (while (setq file (pop files))
1622 (unless (string-match "/\\.\\.?$" file)
1623 (if (file-directory-p file)
1624 (gnus-uu-delete-work-dir file)
1625 (gnus-message 9 "Deleting file %s..." file)
1626 (delete-file file))))
1627 (delete-directory dir)))
1628 (gnus-message 7 ""))
1632 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1633 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1638 ;;; uuencoded posting
1644 ;; Any function that is to be used as and encoding method will take two
1645 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1646 ;; and "spiral.jpg", respectively.) The function should return nil if
1647 ;; the encoding wasn't successful.
1648 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1649 "Function used for encoding binary files.
1650 There are three functions supplied with gnus-uu for encoding files:
1651 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1652 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1653 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1654 uuencode and adds MIME headers.")
1656 (defvar gnus-uu-post-include-before-composing nil
1657 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1658 If this variable is t, you can either include an encoded file with
1659 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1661 (defvar gnus-uu-post-length 990
1662 "Maximum length of an article.
1663 The encoded file will be split into how many articles it takes to
1664 post the entire file.")
1666 (defvar gnus-uu-post-threaded nil
1667 "Non-nil means that gnus-uu will post the encoded file in a thread.
1668 This may not be smart, as no other decoder I have seen are able to
1669 follow threads when collecting uuencoded articles. (Well, I have seen
1670 one package that does that - gnus-uu, but somehow, I don't think that
1671 counts...) Default is nil.")
1673 (defvar gnus-uu-post-separate-description t
1674 "Non-nil means that the description will be posted in a separate article.
1675 The first article will typically be numbered (0/x). If this variable
1676 is nil, the description the user enters will be included at the
1677 beginning of the first article, which will be numbered (1/x). Default
1680 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1681 (defvar gnus-uu-post-message-id nil)
1682 (defvar gnus-uu-post-inserted-file-name nil)
1683 (defvar gnus-uu-winconf-post-news nil)
1685 (defun gnus-uu-post-news ()
1686 "Compose an article and post an encoded file."
1688 (setq gnus-uu-post-inserted-file-name nil)
1689 (setq gnus-uu-winconf-post-news (current-window-configuration))
1691 (gnus-summary-post-news)
1693 (use-local-map (copy-keymap (current-local-map)))
1694 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1695 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1696 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1697 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1699 (if gnus-uu-post-include-before-composing
1700 (save-excursion (setq gnus-uu-post-inserted-file-name
1701 (gnus-uu-post-insert-binary)))))
1703 (defun gnus-uu-post-insert-binary-in-article ()
1704 "Inserts an encoded file in the buffer.
1705 The user will be asked for a file name."
1708 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1710 ;; Encodes with uuencode and substitutes all spaces with backticks.
1711 (defun gnus-uu-post-encode-uuencode (path file-name)
1712 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1714 (goto-char (point-min))
1716 (while (re-search-forward " " nil t)
1717 (replace-match "`"))
1720 ;; Encodes with uuencode and adds MIME headers.
1721 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1722 (if (gnus-uu-post-encode-uuencode path file-name)
1724 (gnus-uu-post-make-mime file-name "x-uue")
1727 ;; Encodes with base64 and adds MIME headers
1728 (defun gnus-uu-post-encode-mime (path file-name)
1729 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1731 (gnus-uu-post-make-mime file-name "base64")
1734 ;; Adds MIME headers.
1735 (defun gnus-uu-post-make-mime (file-name encoding)
1736 (goto-char (point-min))
1737 (insert (format "Content-Type: %s; name=\"%s\"\n"
1738 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1740 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1742 (set-buffer gnus-message-buffer)
1743 (goto-char (point-min))
1744 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1746 (narrow-to-region 1 (point))
1747 (or (mail-fetch-field "mime-version")
1750 (insert "MIME-Version: 1.0\n")))
1753 ;; Encodes a file PATH with COMMAND, leaving the result in the
1755 (defun gnus-uu-post-encode-file (command path file-name)
1756 (= 0 (call-process "sh" nil t nil "-c"
1757 (format "%s %s %s" command path file-name))))
1759 (defun gnus-uu-post-news-inews ()
1760 "Posts the composed news article and encoded file.
1761 If no file has been included, the user will be asked for a file."
1766 (if gnus-uu-post-inserted-file-name
1767 (setq file-name gnus-uu-post-inserted-file-name)
1768 (setq file-name (gnus-uu-post-insert-binary)))
1770 (if gnus-uu-post-threaded
1771 (let ((message-required-news-headers
1772 (if (memq 'Message-ID message-required-news-headers)
1773 message-required-news-headers
1774 (cons 'Message-ID message-required-news-headers)))
1775 gnus-inews-article-hook)
1777 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1778 gnus-inews-article-hook
1779 (list gnus-inews-article-hook)))
1780 (setq gnus-inews-article-hook
1784 (goto-char (point-min))
1785 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1786 (setq gnus-uu-post-message-id
1788 (match-beginning 1) (match-end 1)))
1789 (setq gnus-uu-post-message-id nil))))
1790 gnus-inews-article-hook))
1791 (gnus-uu-post-encoded file-name t))
1792 (gnus-uu-post-encoded file-name nil)))
1793 (setq gnus-uu-post-inserted-file-name nil)
1794 (and gnus-uu-winconf-post-news
1795 (set-window-configuration gnus-uu-winconf-post-news)))
1797 ;; Asks for a file to encode, encodes it and inserts the result in
1798 ;; the current buffer. Returns the file name the user gave.
1799 (defun gnus-uu-post-insert-binary ()
1800 (let ((uuencode-buffer-name "*uuencode buffer*")
1801 file-path uubuf file-name)
1803 (setq file-path (read-file-name
1804 "What file do you want to encode? "))
1805 (if (not (file-exists-p file-path))
1806 (error "%s: No such file" file-path))
1808 (goto-char (point-max))
1809 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1811 (if (string-match "^~/" file-path)
1812 (setq file-path (concat "$HOME" (substring file-path 1))))
1813 (if (string-match "/[^/]*$" file-path)
1814 (setq file-name (substring file-path (1+ (match-beginning 0))))
1815 (setq file-name file-path))
1819 (set-buffer (setq uubuf
1820 (get-buffer-create uuencode-buffer-name)))
1822 (funcall gnus-uu-post-encode-method file-path file-name))
1823 (insert-buffer-substring uubuf)
1824 (error "Encoding unsuccessful"))
1825 (kill-buffer uubuf))
1828 ;; Posts the article and all of the encoded file.
1829 (defun gnus-uu-post-encoded (file-name &optional threaded)
1830 (let ((send-buffer-name "*uuencode send buffer*")
1831 (encoded-buffer-name "*encoded buffer*")
1832 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1833 (separator (concat mail-header-separator "\n\n"))
1834 uubuf length parts header i end beg
1835 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1837 (setq post-buf (current-buffer))
1839 (goto-char (point-min))
1840 (if (not (re-search-forward
1841 (if gnus-uu-post-separate-description
1842 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1844 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1845 (error "Internal error: No binary/header separator"))
1848 (setq beg-binary (point))
1849 (setq end-binary (point-max))
1852 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1854 (insert-buffer-substring post-buf beg-binary end-binary)
1855 (goto-char (point-min))
1856 (setq length (count-lines 1 (point-max)))
1857 (setq parts (/ length gnus-uu-post-length))
1858 (if (not (< (% length gnus-uu-post-length) 4))
1859 (setq parts (1+ parts))))
1861 (if gnus-uu-post-separate-description
1863 (kill-region (point) (point-max))
1865 (goto-char (point-min))
1867 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1869 (setq header (buffer-substring 1 (point)))
1871 (goto-char (point-min))
1872 (if (not gnus-uu-post-separate-description)
1874 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1877 (insert (format " (0/%d)" parts))))
1883 (while (not (> i parts))
1884 (set-buffer (get-buffer-create send-buffer-name))
1887 (if (and threaded gnus-uu-post-message-id)
1888 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1891 (- 62 (length (format top-string "" file-name i parts ""))))
1892 (if (> 1 (setq minlen (/ whole-len 2)))
1897 (make-string minlen ?-)
1900 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1902 (goto-char (point-min))
1903 (if (not (re-search-forward "^Subject: " nil t))
1908 (insert (format " (%d/%d)" i parts)))
1909 (if (or (and (= i 2) gnus-uu-post-separate-description)
1910 (and (= i 1) (not gnus-uu-post-separate-description)))
1911 (replace-match "Subject: Re: "))))
1913 (goto-char (point-max))
1918 (goto-char (point-max))
1919 (forward-line gnus-uu-post-length))
1920 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1923 (insert-buffer-substring uubuf beg end)
1928 (goto-char (point-min))
1930 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1933 (if (re-search-forward
1934 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1943 (and (setq buf (get-buffer send-buffer-name))
1945 (and (setq buf (get-buffer encoded-buffer-name))
1948 (if (not gnus-uu-post-separate-description)
1950 (set-buffer-modified-p nil)
1951 (and (fboundp 'bury-buffer) (bury-buffer))))))
1955 ;; gnus-uu.el ends here