1 ;;; gnus-uu.el --- extract (uu)encoded files in Gnus
2 ;; Copyright (C) 198,995,86,87,93,94,95,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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.
29 (eval-when-compile (require 'cl))
37 (defgroup gnus-extract nil
38 "Extracting encoded files."
42 (defgroup gnus-extract-view nil
43 "Viewwing extracted files."
46 (defgroup gnus-extract-archive nil
47 "Extracting encoded archives."
50 (defgroup gnus-extract-post nil
51 "Extracting encoded archives."
52 :prefix "gnus-uu-post"
55 ;; Default viewing action rules
57 (defcustom gnus-uu-default-view-rules
58 '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed 's/\r$//'")
59 ("\\.pas$" "cat %s | sed 's/\r$//'")
60 ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
61 ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "display")
62 ("\\.tga$" "tgatoppm %s | ee -")
63 ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
64 "sox -v .5 %s -t .au -u - > /dev/audio")
65 ("\\.au$" "cat %s > /dev/audio")
66 ("\\.midi?$" "playmidi -f")
68 ("\\.ps$" "ghostview")
70 ("\\.html$" "xmosaic")
71 ("\\.mpe?g$" "mpeg_play")
72 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
73 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
75 "*Default actions to be taken when the user asks to view a file.
76 To change the behaviour, you can either edit this variable or set
77 `gnus-uu-user-view-rules' to something useful.
81 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
82 following in your .emacs file:
84 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
86 Both these variables are lists of lists with two string elements. The
87 first string is a regular expression. If the file name matches this
88 regular expression, the command in the second string is executed with
89 the file as an argument.
91 If the command string contains \"%s\", the file name will be inserted
92 at that point in the command string. If there's no \"%s\" in the
93 command string, the file name will be appended to the command string
96 There are several user variables to tailor the behaviour of gnus-uu to
97 your needs. First we have `gnus-uu-user-view-rules', which is the
98 variable gnus-uu first consults when trying to decide how to view a
99 file. If this variable contains no matches, gnus-uu examines the
100 default rule variable provided in this package. If gnus-uu finds no
101 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
103 :group 'gnus-extract-view
104 :type '(repeat (group regexp (string :tag "Command"))))
106 (defcustom gnus-uu-user-view-rules nil
107 "What actions are to be taken to view a file.
108 See the documentation on the `gnus-uu-default-view-rules' variable for
110 :group 'gnus-extract-view
111 :type '(repeat (group regexp (string :tag "Command"))))
113 (defcustom gnus-uu-user-view-rules-end
115 "*What actions are to be taken if no rule matched the file name.
116 See the documentation on the `gnus-uu-default-view-rules' variable for
118 :group 'gnus-extract-view
119 :type '(repeat (group regexp (string :tag "Command"))))
121 ;; Default unpacking commands
123 (defcustom gnus-uu-default-archive-rules
124 '(("\\.tar$" "tar xf")
125 ("\\.zip$" "unzip -o")
127 ("\\.arj$" "unarj x")
129 ("\\.\\(lzh\\|lha\\)$" "lha x")
130 ("\\.Z$" "uncompress")
132 ("\\.arc$" "arc -x"))
133 "*See `gnus-uu-user-archive-rules'."
134 :group 'gnus-extract-archive
135 :type '(repeat (group regexp (string :tag "Command"))))
137 (defvar gnus-uu-destructive-archivers
138 (list "uncompress" "gunzip"))
140 (defcustom gnus-uu-user-archive-rules nil
141 "A list that can be set to override the default archive unpacking commands.
142 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
143 unpack zip files, say the following:
144 (setq gnus-uu-user-archive-rules
145 '((\"\\\\.tar$\" \"untar\")
146 (\"\\\\.zip$\" \"zip -x\")))"
147 :group 'gnus-extract-archive
148 :type '(repeat (group regexp (string :tag "Command"))))
150 (defcustom gnus-uu-ignore-files-by-name nil
151 "*A regular expression saying what files should not be viewed based on name.
152 If, for instance, you want gnus-uu to ignore all .au and .wav files,
153 you could say something like
155 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
157 Note that this variable can be used in conjunction with the
158 `gnus-uu-ignore-files-by-type' variable."
160 :type '(choice (const :tag "off" nil)
161 (regexp :format "%v")))
163 (defcustom gnus-uu-ignore-files-by-type nil
164 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
165 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
166 you could say something like
168 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
170 Note that this variable can be used in conjunction with the
171 `gnus-uu-ignore-files-by-name' variable."
173 :type '(choice (const :tag "off" nil)
174 (regexp :format "%v")))
176 ;; Pseudo-MIME support
178 (defconst gnus-uu-ext-to-mime-list
179 '(("\\.gif$" "image/gif")
180 ("\\.jpe?g$" "image/jpeg")
181 ("\\.tiff?$" "image/tiff")
182 ("\\.xwd$" "image/xwd")
183 ("\\.pbm$" "image/pbm")
184 ("\\.pgm$" "image/pgm")
185 ("\\.ppm$" "image/ppm")
186 ("\\.xbm$" "image/xbm")
187 ("\\.pcx$" "image/pcx")
188 ("\\.tga$" "image/tga")
189 ("\\.ps$" "image/postscript")
190 ("\\.fli$" "video/fli")
191 ("\\.wav$" "audio/wav")
192 ("\\.aiff$" "audio/aiff")
193 ("\\.hcom$" "audio/hcom")
194 ("\\.voc$" "audio/voc")
195 ("\\.smp$" "audio/smp")
196 ("\\.mod$" "audio/mod")
197 ("\\.dvi$" "image/dvi")
198 ("\\.mpe?g$" "video/mpeg")
199 ("\\.au$" "audio/basic")
200 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
201 ("\\.\\(c\\|h\\)$" "text/source")
202 ("read.*me" "text/plain")
203 ("\\.html$" "text/html")
204 ("\\.bat$" "text/bat")
205 ("\\.[1-6]$" "text/man")
206 ("\\.flc$" "video/flc")
207 ("\\.rle$" "video/rle")
208 ("\\.pfx$" "video/pfx")
209 ("\\.avi$" "video/avi")
210 ("\\.sme$" "video/sme")
211 ("\\.rpza$" "video/prza")
212 ("\\.dl$" "video/dl")
213 ("\\.qt$" "video/qt")
214 ("\\.rsrc$" "video/rsrc")
215 ("\\..*$" "unknown/unknown")))
217 ;; Various variables users may set
219 (defcustom gnus-uu-tmp-dir
220 (cond ((fboundp 'temp-directory) (temp-directory))
221 ((boundp 'temporary-file-directory) temporary-file-directory)
223 "*Variable saying where gnus-uu is to do its work.
224 Default is \"/tmp/\"."
228 (defcustom gnus-uu-do-not-unpack-archives nil
229 "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
231 :group 'gnus-extract-archive
234 (defcustom gnus-uu-ignore-default-view-rules nil
235 "*Non-nil means that gnus-uu will ignore the default viewing rules.
236 Only the user viewing rules will be consulted. Default is nil."
237 :group 'gnus-extract-view
240 (defcustom gnus-uu-grabbed-file-functions nil
241 "Functions run on each file after successful decoding.
242 They will be called with the name of the file as the argument.
243 Likely functions you can use in this list are `gnus-uu-grab-view'
244 and `gnus-uu-grab-move'."
246 :options '(gnus-uu-grab-view gnus-uu-grab-move)
249 (defcustom gnus-uu-ignore-default-archive-rules nil
250 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
251 Only the user unpacking commands will be consulted. Default is nil."
252 :group 'gnus-extract-archive
255 (defcustom gnus-uu-kill-carriage-return t
256 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
261 (defcustom gnus-uu-view-with-metamail nil
262 "*Non-nil means that files will be viewed with metamail.
263 The gnus-uu viewing functions will be ignored and gnus-uu will try
264 to guess at a content-type based on file name suffixes. Default
269 (defcustom gnus-uu-unmark-articles-not-decoded nil
270 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
275 (defcustom gnus-uu-correct-stripped-uucode nil
276 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
281 (defcustom gnus-uu-save-in-digest nil
282 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
283 If this variable is nil, gnus-uu will just save everything in a
284 file without any embellishments. The digesting almost conforms to RFC1153 -
285 no easy way to specify any meaningful volume and issue numbers were found,
286 so I simply dropped them."
290 (defcustom gnus-uu-pre-uudecode-hook nil
291 "Hook run before sending a message to uudecode."
295 (defcustom gnus-uu-digest-headers
296 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
297 "^Summary:" "^References:" "^Content-Type:" "^Content-Transfer-Encoding:")
298 "*List of regexps to match headers included in digested messages.
299 The headers will be included in the sequence they are matched."
301 :type '(repeat regexp))
303 (defcustom gnus-uu-save-separate-articles nil
304 "*Non-nil means that gnus-uu will save articles in separate files."
308 (defcustom gnus-uu-be-dangerous 'ask
309 "*Specifies what to do if unusual situations arise during decoding.
310 If nil, be as conservative as possible. If t, ignore things that
311 didn't work, and overwrite existing files. Otherwise, ask each time."
313 :type '(choice (const :tag "conservative" nil)
314 (const :tag "ask" ask)
315 (const :tag "liberal" t)))
317 ;; Internal variables
319 (defvar gnus-uu-saved-article-name nil)
321 (defvar gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
322 (defvar gnus-uu-end-string "^end[ \t]*$")
324 (defvar gnus-uu-body-line "^M")
326 (while (> (setq i (1- i)) 0)
327 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
328 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
330 ;"^M.............................................................?$"
332 (defvar gnus-uu-shar-begin-string "^#! */bin/sh")
334 (defvar gnus-uu-shar-file-name nil)
335 (defvar gnus-uu-shar-name-marker
336 "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
338 (defvar gnus-uu-postscript-begin-string "^%!PS-")
339 (defvar gnus-uu-postscript-end-string "^%%EOF$")
341 (defvar gnus-uu-file-name nil)
342 (defvar gnus-uu-uudecode-process nil)
343 (defvar gnus-uu-binhex-article-name nil)
345 (defvar gnus-uu-work-dir nil)
347 (defvar gnus-uu-output-buffer-name " *Gnus UU Output*")
349 (defvar gnus-uu-default-dir gnus-article-save-directory)
350 (defvar gnus-uu-digest-from-subject nil)
354 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
355 "p" gnus-summary-mark-as-processable
356 "u" gnus-summary-unmark-as-processable
357 "U" gnus-summary-unmark-all-processable
358 "v" gnus-uu-mark-over
359 "s" gnus-uu-mark-series
360 "r" gnus-uu-mark-region
361 "g" gnus-uu-unmark-region
362 "R" gnus-uu-mark-by-regexp
363 "G" gnus-uu-unmark-by-regexp
364 "t" gnus-uu-mark-thread
365 "T" gnus-uu-unmark-thread
367 "b" gnus-uu-mark-buffer
368 "S" gnus-uu-mark-sparse
369 "k" gnus-summary-kill-process-mark
370 "y" gnus-summary-yank-process-mark
371 "w" gnus-summary-save-process-mark
372 "i" gnus-uu-invert-processable
373 "m" gnus-summary-save-parts)
375 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
376 ;;"x" gnus-uu-extract-any
377 ;;"m" gnus-uu-extract-mime
378 "u" gnus-uu-decode-uu
379 "U" gnus-uu-decode-uu-and-save
380 "s" gnus-uu-decode-unshar
381 "S" gnus-uu-decode-unshar-and-save
382 "o" gnus-uu-decode-save
383 "O" gnus-uu-decode-save
384 "b" gnus-uu-decode-binhex
385 "B" gnus-uu-decode-binhex
386 "p" gnus-uu-decode-postscript
387 "P" gnus-uu-decode-postscript-and-save)
390 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
391 "u" gnus-uu-decode-uu-view
392 "U" gnus-uu-decode-uu-and-save-view
393 "s" gnus-uu-decode-unshar-view
394 "S" gnus-uu-decode-unshar-and-save-view
395 "o" gnus-uu-decode-save-view
396 "O" gnus-uu-decode-save-view
397 "b" gnus-uu-decode-binhex-view
398 "B" gnus-uu-decode-binhex-view
399 "p" gnus-uu-decode-postscript-view
400 "P" gnus-uu-decode-postscript-and-save-view)
405 (defun gnus-uu-decode-uu (&optional n)
406 "Uudecodes the current article."
408 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
410 (defun gnus-uu-decode-uu-and-save (n dir)
411 "Decodes and saves the resulting file."
413 (list current-prefix-arg
414 (file-name-as-directory
415 (read-file-name "Uudecode and save in dir: "
417 gnus-uu-default-dir t))))
418 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
420 (defun gnus-uu-decode-unshar (&optional n)
421 "Unshars the current article."
423 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
425 (defun gnus-uu-decode-unshar-and-save (n dir)
426 "Unshars and saves the current article."
428 (list current-prefix-arg
429 (file-name-as-directory
430 (read-file-name "Unshar and save in dir: "
432 gnus-uu-default-dir t))))
433 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
435 (defun gnus-uu-decode-save (n file)
436 "Saves the current article."
438 (list current-prefix-arg
440 (if gnus-uu-save-separate-articles
441 "Save articles is dir: "
442 "Save articles in file: ")
444 gnus-uu-default-dir)))
445 (setq gnus-uu-saved-article-name file)
446 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
448 (defun gnus-uu-decode-binhex (n dir)
449 "Unbinhexes the current article."
451 (list current-prefix-arg
452 (file-name-as-directory
453 (read-file-name "Unbinhex and save in dir: "
455 gnus-uu-default-dir))))
456 (setq gnus-uu-binhex-article-name
457 (make-temp-name (concat gnus-uu-work-dir "binhex")))
458 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
460 (defun gnus-uu-decode-uu-view (&optional n)
461 "Uudecodes and views the current article."
463 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
464 (gnus-uu-decode-uu n)))
466 (defun gnus-uu-decode-uu-and-save-view (n dir)
467 "Decodes, views and saves the resulting file."
469 (list current-prefix-arg
470 (read-file-name "Uudecode, view and save in dir: "
472 gnus-uu-default-dir t)))
473 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
474 (gnus-uu-decode-uu-and-save n dir)))
476 (defun gnus-uu-decode-unshar-view (&optional n)
477 "Unshars and views the current article."
479 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
480 (gnus-uu-decode-unshar n)))
482 (defun gnus-uu-decode-unshar-and-save-view (n dir)
483 "Unshars and saves the current article."
485 (list current-prefix-arg
486 (read-file-name "Unshar, view and save in dir: "
488 gnus-uu-default-dir t)))
489 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
490 (gnus-uu-decode-unshar-and-save n dir)))
492 (defun gnus-uu-decode-save-view (n file)
493 "Saves and views the current article."
495 (list current-prefix-arg
496 (read-file-name (if gnus-uu-save-separate-articles
497 "Save articles is dir: "
498 "Save articles in file: ")
499 gnus-uu-default-dir gnus-uu-default-dir)))
500 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
501 (gnus-uu-decode-save n file)))
503 (defun gnus-uu-decode-binhex-view (n file)
504 "Unbinhexes and views the current article."
506 (list current-prefix-arg
507 (read-file-name "Unbinhex, view and save in dir: "
508 gnus-uu-default-dir gnus-uu-default-dir)))
509 (setq gnus-uu-binhex-article-name
510 (make-temp-name (concat gnus-uu-work-dir "binhex")))
511 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
512 (gnus-uu-decode-binhex n file)))
515 ;; Digest and forward articles
517 (defun gnus-uu-digest-mail-forward (&optional n post)
518 "Digests and forwards all articles in this series."
520 (let ((gnus-uu-save-in-digest t)
521 (file (make-temp-name (nnheader-concat gnus-uu-tmp-dir "forward")))
523 (gnus-setup-message 'forward
524 (setq gnus-uu-digest-from-subject nil)
525 (gnus-uu-decode-save n file)
526 (setq buf (switch-to-buffer
527 (gnus-get-buffer-create " *gnus-uu-forward*")))
531 (let ((fs gnus-uu-digest-from-subject))
534 subject (gnus-simplify-subject-fuzzy (cdar fs))
536 (while (and fs (or from subject))
538 (unless (string= from (caar fs))
541 (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
546 (setq subject "Digested Articles"))
549 (if (gnus-news-group-p gnus-newsgroup-name)
552 (goto-char (point-min))
553 (when (re-search-forward "^Subject: ")
554 (delete-region (point) (gnus-point-at-eol))
556 (goto-char (point-min))
557 (when (re-search-forward "^From: ")
558 (delete-region (point) (gnus-point-at-eol))
560 (message-forward post))
561 (setq gnus-uu-digest-from-subject nil)))
563 (defun gnus-uu-digest-post-forward (&optional n)
564 "Digest and forward to a newsgroup."
566 (gnus-uu-digest-mail-forward n t))
570 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
571 "Set the process mark on articles whose subjects match REGEXP.
572 When called interactively, prompt for REGEXP.
573 Optional UNMARK non-nil means unmark instead of mark."
574 (interactive "sMark (regexp): \nP")
575 (let ((articles (gnus-uu-find-articles-matching regexp)))
578 (gnus-summary-remove-process-mark (pop articles))
579 (gnus-summary-set-process-mark (pop articles))))
581 (gnus-summary-position-point))
583 (defun gnus-uu-unmark-by-regexp (regexp)
584 "Remove the process mark from articles whose subjects match REGEXP.
585 When called interactively, prompt for REGEXP."
586 (interactive "sUnmark (regexp): ")
587 (gnus-uu-mark-by-regexp regexp t))
589 (defun gnus-uu-mark-series ()
590 "Mark the current series with the process mark."
592 (let ((articles (gnus-uu-find-articles-matching)))
594 (gnus-summary-set-process-mark (car articles))
595 (setq articles (cdr articles)))
597 (gnus-summary-position-point))
599 (defun gnus-uu-mark-region (beg end &optional unmark)
600 "Set the process mark on all articles between point and mark."
604 (while (< (point) end)
606 (gnus-summary-remove-process-mark (gnus-summary-article-number))
607 (gnus-summary-set-process-mark (gnus-summary-article-number)))
609 (gnus-summary-position-point))
611 (defun gnus-uu-unmark-region (beg end)
612 "Remove the process mark from all articles between point and mark."
614 (gnus-uu-mark-region beg end t))
616 (defun gnus-uu-mark-buffer ()
617 "Set the process mark on all articles in the buffer."
619 (gnus-uu-mark-region (point-min) (point-max)))
621 (defun gnus-uu-unmark-buffer ()
622 "Remove the process mark on all articles in the buffer."
624 (gnus-uu-mark-region (point-min) (point-max) t))
626 (defun gnus-uu-mark-thread ()
627 "Marks all articles downwards in this thread."
629 (gnus-save-hidden-threads
630 (let ((level (gnus-summary-thread-level)))
631 (while (and (gnus-summary-set-process-mark
632 (gnus-summary-article-number))
633 (zerop (gnus-summary-next-subject 1 nil t))
634 (> (gnus-summary-thread-level) level)))))
635 (gnus-summary-position-point))
637 (defun gnus-uu-unmark-thread ()
638 "Unmarks all articles downwards in this thread."
640 (let ((level (gnus-summary-thread-level)))
641 (while (and (gnus-summary-remove-process-mark
642 (gnus-summary-article-number))
643 (zerop (gnus-summary-next-subject 1))
644 (> (gnus-summary-thread-level) level))))
645 (gnus-summary-position-point))
647 (defun gnus-uu-invert-processable ()
648 "Invert the list of process-marked articles."
650 (let ((data gnus-newsgroup-data)
654 (if (memq (setq number (gnus-data-number (pop data)))
655 gnus-newsgroup-processable)
656 (gnus-summary-remove-process-mark number)
657 (gnus-summary-set-process-mark number)))))
658 (gnus-summary-position-point))
660 (defun gnus-uu-mark-over (&optional score)
661 "Mark all articles with a score over SCORE (the prefix)."
663 (let ((score (or score gnus-summary-default-score 0))
664 (data gnus-newsgroup-data))
667 (when (> (or (cdr (assq (gnus-data-number (car data))
668 gnus-newsgroup-scored))
669 gnus-summary-default-score 0)
671 (gnus-summary-set-process-mark (caar data)))
672 (setq data (cdr data))))
673 (gnus-summary-position-point)))
675 (defun gnus-uu-mark-sparse ()
676 "Mark all series that have some articles marked."
678 (let ((marked (nreverse gnus-newsgroup-processable))
679 subject articles total headers)
681 (error "No articles marked with the process mark"))
682 (setq gnus-newsgroup-processable nil)
685 (and (vectorp (setq headers
686 (gnus-summary-article-header (car marked))))
687 (setq subject (mail-header-subject headers)
688 articles (gnus-uu-find-articles-matching
689 (gnus-uu-reginize-string subject))
690 total (nconc total articles)))
692 (gnus-summary-set-process-mark (car articles))
693 (setcdr marked (delq (car articles) (cdr marked)))
694 (setq articles (cdr articles)))
695 (setq marked (cdr marked)))
696 (setq gnus-newsgroup-processable (nreverse total)))
697 (gnus-summary-position-point)))
699 (defun gnus-uu-mark-all ()
700 "Mark all articles in \"series\" order."
702 (setq gnus-newsgroup-processable nil)
704 (let ((data gnus-newsgroup-data)
707 (when (and (not (memq (setq number (gnus-data-number (car data)))
708 gnus-newsgroup-processable))
709 (vectorp (gnus-data-header (car data))))
710 (gnus-summary-goto-subject number)
711 (gnus-uu-mark-series))
712 (setq data (cdr data)))))
713 (gnus-summary-position-point))
715 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
717 (defun gnus-uu-decode-postscript (&optional n)
718 "Gets postscript of the current article."
720 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
722 (defun gnus-uu-decode-postscript-view (&optional n)
723 "Gets and views the current article."
725 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
726 (gnus-uu-decode-postscript n)))
728 (defun gnus-uu-decode-postscript-and-save (n dir)
729 "Extracts postscript and saves the current article."
731 (list current-prefix-arg
732 (file-name-as-directory
733 (read-file-name "Save in dir: "
735 gnus-uu-default-dir t))))
736 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
739 (defun gnus-uu-decode-postscript-and-save-view (n dir)
740 "Decodes, views and saves the resulting file."
742 (list current-prefix-arg
743 (read-file-name "Where do you want to save the file(s)? "
745 gnus-uu-default-dir t)))
746 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
747 (gnus-uu-decode-postscript-and-save n dir)))
750 ;; Internal functions.
752 (defun gnus-uu-decode-with-method (method n &optional save not-insert
754 (gnus-uu-initialize scan)
756 (setq gnus-uu-default-dir save))
757 ;; Create the directory we save to.
758 (when (and scan cdir save
759 (not (file-exists-p save)))
760 (make-directory save t))
761 (let ((articles (gnus-uu-get-list-of-articles n))
763 (setq files (gnus-uu-grab-articles articles method t))
764 (let ((gnus-current-article (car articles)))
766 (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
768 (gnus-uu-save-files files save))
769 (when (eq gnus-uu-do-not-unpack-archives nil)
770 (setq files (gnus-uu-unpack-files files)))
771 (setq files (nreverse (gnus-uu-get-actions files)))
772 (or not-insert (not gnus-insert-pseudo-articles)
773 (gnus-summary-insert-pseudos files save))))
775 (defun gnus-uu-scan-directory (dir &optional rec)
776 "Return a list of all files under DIR."
777 (let ((files (directory-files dir t))
779 (while (setq file (pop files))
780 (unless (member (file-name-nondirectory file) '("." ".."))
781 (push (list (cons 'name file)
782 (cons 'article gnus-current-article))
784 (when (file-directory-p file)
785 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
790 (defun gnus-uu-save-files (files dir)
792 (let ((len (length files))
793 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
794 to-file file fromdir)
795 (while (setq file (cdr (assq 'name (pop files))))
796 (when (file-exists-p file)
797 (string-match reg file)
798 (setq fromdir (substring file (match-end 0)))
799 (if (file-directory-p file)
800 (gnus-make-directory (concat dir fromdir))
801 (setq to-file (concat dir fromdir))
802 (when (or (not (file-exists-p to-file))
803 (eq gnus-uu-be-dangerous t)
804 (and gnus-uu-be-dangerous
805 (gnus-y-or-n-p (format "%s exists; overwrite? "
807 (copy-file file to-file t t)))))
808 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
810 ;; Functions for saving and possibly digesting articles without
813 ;; Function called by gnus-uu-grab-articles to treat each article.
814 (defun gnus-uu-save-article (buffer in-state)
816 (gnus-uu-save-separate-articles
820 (concat gnus-uu-saved-article-name gnus-current-article))
821 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
822 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
824 ((eq in-state 'last) (list 'end))
825 (t (list 'middle)))))
826 ((not gnus-uu-save-in-digest)
829 (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
830 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
831 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
833 ((eq in-state 'last) (list 'end))
834 (t (list 'middle)))))
836 (let ((header (gnus-summary-article-header)))
837 (push (cons (mail-header-from header)
838 (mail-header-subject header))
839 gnus-uu-digest-from-subject))
840 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
841 beg subj headers headline sorthead body end-string state)
842 (if (or (eq in-state 'first)
843 (eq in-state 'first-and-last))
845 (setq state (list 'begin))
847 (set-buffer (gnus-get-buffer-create "*gnus-uu-body*"))
850 (set-buffer (gnus-get-buffer-create "*gnus-uu-pre*"))
853 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
854 (current-time-string) name name))))
855 (when (not (eq in-state 'end))
856 (setq state (list 'middle))))
858 (set-buffer "*gnus-uu-body*")
859 (goto-char (setq beg (point-max)))
863 (let (buffer-read-only)
864 (gnus-set-text-properties (point-min) (point-max) nil)
865 ;; These two are necessary for XEmacs 19.12 fascism.
866 (put-text-property (point-min) (point-max) 'invisible nil)
867 (put-text-property (point-min) (point-max) 'intangible nil))
868 (goto-char (point-min))
869 (re-search-forward "\n\n")
870 ;; Quote all 30-dash lines.
872 (while (re-search-forward "^-" nil t)
876 (setq body (buffer-substring (1- (point)) (point-max)))
877 (narrow-to-region (point-min) (point))
878 (if (not (setq headers gnus-uu-digest-headers))
879 (setq sorthead (buffer-substring (point-min) (point-max)))
881 (setq headline (car headers))
882 (setq headers (cdr headers))
883 (goto-char (point-min))
884 (while (re-search-forward headline nil t)
889 (or (and (re-search-forward "^[^ \t]" nil t)
891 (progn (forward-line 1) (point)))))))))
893 (insert sorthead) (goto-char (point-max))
894 (insert body) (goto-char (point-max))
895 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
897 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
898 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
900 (set-buffer "*gnus-uu-pre*")
901 (insert (format " %s\n" subj)))))
902 (when (or (eq in-state 'last)
903 (eq in-state 'first-and-last))
905 (set-buffer "*gnus-uu-pre*")
906 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
907 (gnus-write-buffer gnus-uu-saved-article-name))
909 (set-buffer "*gnus-uu-body*")
910 (goto-char (point-max))
912 (concat (setq end-string (format "End of %s Digest" name))
914 (insert (concat (make-string (length end-string) ?*) "\n"))
916 (point-min) (point-max) gnus-uu-saved-article-name t))
917 (gnus-kill-buffer "*gnus-uu-pre*")
918 (gnus-kill-buffer "*gnus-uu-body*")
920 (if (memq 'begin state)
921 (cons gnus-uu-saved-article-name state)
924 ;; Binhex treatment - not very advanced.
926 (defvar gnus-uu-binhex-body-line
927 "^[^:]...............................................................$")
928 (defvar gnus-uu-binhex-begin-line
929 "^:...............................................................$")
930 (defvar gnus-uu-binhex-end-line
933 (defun gnus-uu-binhex-article (buffer in-state)
934 (let (state start-char)
938 (goto-char (point-min))
939 (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
940 (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
941 (setq state (list 'wrong-type))))
943 (if (memq 'wrong-type state)
946 (setq start-char (point))
947 (if (looking-at gnus-uu-binhex-begin-line)
949 (setq state (list 'begin))
950 (write-region 1 1 gnus-uu-binhex-article-name))
951 (setq state (list 'middle)))
952 (goto-char (point-max))
953 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
954 gnus-uu-binhex-end-line)
956 (when (looking-at gnus-uu-binhex-end-line)
957 (setq state (if (memq 'begin state)
962 (when (file-exists-p gnus-uu-binhex-article-name)
963 (mm-append-to-file start-char (point) gnus-uu-binhex-article-name))))
964 (if (memq 'begin state)
965 (cons gnus-uu-binhex-article-name state)
970 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
971 (let ((state (list 'ok))
972 start-char end-char file-name)
974 (set-buffer process-buffer)
975 (goto-char (point-min))
976 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
977 (setq state (list 'wrong-type))
979 (setq start-char (point))
980 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
981 (setq state (list 'wrong-type))
982 (setq end-char (point))
983 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
984 (insert-buffer-substring process-buffer start-char end-char)
985 (setq file-name (concat gnus-uu-work-dir
986 (cdr gnus-article-current) ".ps"))
987 (write-region (point-min) (point-max) file-name)
988 (setq state (list file-name 'begin 'end)))))
994 (defun gnus-uu-get-actions (files)
998 (setq name (cdr (assq 'name (car files))))
1000 (setq action (gnus-uu-get-action name))
1001 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
1002 (cons 'action "file")
1003 (cons 'action action))
1004 (cons 'execute (gnus-uu-command
1007 (setq files (cdr files)))
1010 (defun gnus-uu-get-action (file-name)
1013 (gnus-uu-choose-action
1016 gnus-uu-user-view-rules
1017 (if gnus-uu-ignore-default-view-rules
1019 gnus-uu-default-view-rules)
1020 gnus-uu-user-view-rules-end)))
1021 (when (and (not (string= (or action "") "gnus-uu-archive"))
1022 gnus-uu-view-with-metamail)
1024 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
1025 (setq action (format "metamail -d -b -c \"%s\"" action))))
1029 ;; Functions for treating subjects and collecting series.
1031 (defun gnus-uu-reginize-string (string)
1032 ;; Takes a string and puts a \ in front of every special character;
1033 ;; replaces the last thing that looks like "2/3" with "[0-9]+/3"
1034 ;; or, if it can't find something like that, tries "2 of 3", then
1035 ;; finally just replaces the next to last number with "[0-9]+".
1037 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1038 (buffer-disable-undo)
1040 (insert (regexp-quote string))
1042 (setq case-fold-search nil)
1045 (if (re-search-backward "\\([^0-9]\\)[0-9]+/\\([0-9]+\\)" nil t)
1046 (replace-match "\\1[0-9]+/\\2")
1049 (if (re-search-backward "\\([^0-9]\\)[0-9]+[ \t]*of[ \t]*\\([0-9]+\\)"
1051 (replace-match "\\1[0-9]+ of \\2")
1054 (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1056 (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1059 (while (re-search-forward "[ \t]+" nil t)
1060 (replace-match "[ \t]+" t t))
1062 (buffer-substring 1 (point-max))))
1064 (defun gnus-uu-get-list-of-articles (n)
1065 ;; If N is non-nil, the article numbers of the N next articles
1066 ;; will be returned.
1067 ;; If any articles have been marked as processable, they will be
1069 ;; Failing that, articles that have subjects that are part of the
1070 ;; same "series" as the current will be returned.
1074 (setq n (prefix-numeric-value n))
1075 (let ((backward (< n 0))
1079 (push (gnus-summary-article-number)
1081 (gnus-summary-search-forward nil nil backward))
1083 (nreverse articles)))
1084 (gnus-newsgroup-processable
1085 (reverse gnus-newsgroup-processable))
1087 (gnus-uu-find-articles-matching)))))
1089 (defun gnus-uu-string< (l1 l2)
1090 (string< (car l1) (car l2)))
1092 (defun gnus-uu-find-articles-matching
1093 (&optional subject only-unread do-not-translate)
1094 ;; Finds all articles that matches the regexp SUBJECT. If it is
1095 ;; nil, the current article name will be used. If ONLY-UNREAD is
1096 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1097 ;; non-nil, article names are not equalized before sorting.
1098 (let ((subject (or subject
1099 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1103 ;; Collect all subjects matching subject.
1104 (let ((case-fold-search t)
1105 (data gnus-newsgroup-data)
1109 (and (not (gnus-data-pseudo-p d))
1110 (or (not only-unread)
1111 (= (setq mark (gnus-data-mark d))
1113 (= mark gnus-ticked-mark)
1114 (= mark gnus-dormant-mark))
1115 (setq subj (mail-header-subject (gnus-data-header d)))
1116 (string-match subject subj)
1117 (push (cons subj (gnus-data-number d))
1118 list-of-subjects))))
1120 ;; Expand numbers, sort, and return the list of article
1122 (mapcar (lambda (sub) (cdr sub))
1123 (sort (gnus-uu-expand-numbers
1125 (not do-not-translate))
1126 'gnus-uu-string<))))))
1128 (defun gnus-uu-expand-numbers (string-list &optional translate)
1129 ;; Takes a list of strings and "expands" all numbers in all the
1130 ;; strings. That is, this function makes all numbers equal length by
1131 ;; prepending lots of zeroes before each number. This is to ease later
1132 ;; sorting to find out what sequence the articles are supposed to be
1133 ;; decoded in. Returns the list of expanded strings.
1134 (let ((out-list string-list)
1137 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1138 (buffer-disable-undo)
1141 (insert (caar string-list))
1142 ;; Translate multiple spaces to one space.
1143 (goto-char (point-min))
1144 (while (re-search-forward "[ \t]+" nil t)
1145 (replace-match " "))
1146 ;; Translate all characters to "a".
1147 (goto-char (point-min))
1149 (while (re-search-forward "[A-Za-z]" nil t)
1150 (replace-match "a" t t)))
1152 (goto-char (point-min))
1153 (while (re-search-forward "[0-9]+" nil t)
1156 (string-to-int (buffer-substring
1157 (match-beginning 0) (match-end 0))))))
1158 (setq string (buffer-substring 1 (point-max)))
1159 (setcar (car string-list) string)
1160 (setq string-list (cdr string-list))))
1164 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1165 ;; function. It takes a list of articles to be grabbed and a function
1166 ;; to apply to each article.
1168 ;; The function to be called should take two parameters. The first
1169 ;; parameter is the article buffer. The function should leave the
1170 ;; result, if any, in this buffer. Most treatment functions will just
1171 ;; generate files...
1173 ;; The second parameter is the state of the list of articles, and can
1174 ;; have four values: `first', `middle', `last' and `first-and-last'.
1176 ;; The function should return a list. The list may contain the
1177 ;; following symbols:
1178 ;; `error' if an error occurred
1179 ;; `begin' if the beginning of an encoded file has been received
1180 ;; If the list returned contains a `begin', the first element of
1181 ;; the list *must* be a string with the file name of the decoded
1183 ;; `end' if the end of an encoded file has been received
1184 ;; `middle' if the article was a body part of an encoded file
1185 ;; `wrong-type' if the article was not a part of an encoded file
1186 ;; `ok', which can be used everything is ok
1188 (defvar gnus-uu-has-been-grabbed nil)
1190 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1192 (if (not (and gnus-uu-has-been-grabbed
1193 gnus-uu-unmark-articles-not-decoded))
1195 (when dont-unmark-last-article
1196 (setq art (car gnus-uu-has-been-grabbed))
1197 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1198 (while gnus-uu-has-been-grabbed
1199 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1200 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1201 (when dont-unmark-last-article
1202 (setq gnus-uu-has-been-grabbed (list art))))))
1204 ;; This function takes a list of articles and a function to apply to
1205 ;; each article grabbed.
1207 ;; This function returns a list of files decoded if the grabbing and
1208 ;; the process-function has been successful and nil otherwise.
1209 (defun gnus-uu-grab-articles (articles process-function
1210 &optional sloppy limit no-errors)
1211 (let ((state 'first)
1212 (gnus-asynchronous nil)
1213 (gnus-inhibit-treatment t)
1214 has-been-begin article result-file result-files process-state
1215 gnus-summary-display-article-function
1216 gnus-article-prepare-hook gnus-display-mime-function
1217 article-series files)
1219 (while (and articles
1220 (not (memq 'error process-state))
1222 (not (memq 'end process-state))))
1224 (setq article (pop articles))
1225 (when (vectorp (gnus-summary-article-header article))
1226 (push article article-series)
1229 (if (eq state 'first)
1230 (setq state 'first-and-last)
1231 (setq state 'last)))
1233 (let ((part (gnus-uu-part-number article)))
1234 (gnus-message 6 "Getting article %d%s..."
1235 article (if (string= part "") "" (concat ", " part))))
1236 (gnus-summary-display-article article)
1238 ;; Push the article to the processing function.
1240 (set-buffer gnus-original-article-buffer)
1241 (let ((buffer-read-only nil))
1243 (set-buffer gnus-summary-buffer)
1245 (funcall process-function
1246 gnus-original-article-buffer state)))))
1248 (gnus-summary-remove-process-mark article)
1250 ;; If this is the beginning of a decoded file, we push it
1252 (when (or (memq 'begin process-state)
1253 (and (or (eq state 'first)
1254 (eq state 'first-and-last))
1255 (memq 'ok process-state)))
1256 (when has-been-begin
1257 ;; If there is a `result-file' here, that means that the
1258 ;; file was unsuccessfully decoded, so we delete it.
1259 (when (and result-file
1260 (file-exists-p result-file)
1261 (not gnus-uu-be-dangerous)
1262 (or (eq gnus-uu-be-dangerous t)
1264 (format "Delete unsuccessfully decoded file %s"
1266 (delete-file result-file)))
1267 (when (memq 'begin process-state)
1268 (setq result-file (car process-state)))
1269 (setq has-been-begin t))
1271 ;; Check whether we have decoded one complete file.
1272 (when (memq 'end process-state)
1273 (setq article-series nil)
1274 (setq has-been-begin nil)
1275 (if (stringp result-file)
1276 (setq files (list result-file))
1277 (setq files result-file))
1278 (setq result-file (car files))
1280 (push (list (cons 'name (pop files))
1281 (cons 'article article))
1283 ;; Allow user-defined functions to be run on this file.
1284 (when gnus-uu-grabbed-file-functions
1285 (let ((funcs gnus-uu-grabbed-file-functions))
1286 (unless (listp funcs)
1287 (setq funcs (list funcs)))
1289 (funcall (pop funcs) result-file))))
1290 (setq result-file nil)
1291 ;; Check whether we have decoded enough articles.
1292 (and limit (= (length result-files) limit)
1293 (setq articles nil)))
1295 ;; If this is the last article to be decoded, and
1296 ;; we still haven't reached the end, then we delete
1297 ;; the partially decoded file.
1298 (and (or (eq state 'last) (eq state 'first-and-last))
1299 (not (memq 'end process-state))
1301 (file-exists-p result-file)
1302 (not gnus-uu-be-dangerous)
1303 (or (eq gnus-uu-be-dangerous t)
1305 (format "Delete incomplete file %s? " result-file)))
1306 (delete-file result-file))
1308 ;; If this was a file of the wrong sort, then
1309 (when (and (or (memq 'wrong-type process-state)
1310 (memq 'error process-state))
1311 gnus-uu-unmark-articles-not-decoded)
1312 (gnus-summary-tick-article article t))
1314 ;; Set the new series state.
1315 (if (and (not has-been-begin)
1317 (or (memq 'end process-state)
1318 (memq 'middle process-state)))
1320 (setq process-state (list 'error))
1321 (gnus-message 2 "No begin part at the beginning")
1323 (setq state 'middle)))
1325 ;; When there are no result-files, then something must be wrong.
1329 ((not has-been-begin)
1330 (gnus-message 2 "Wrong type file"))
1331 ((memq 'error process-state)
1332 (gnus-message 2 "An error occurred during decoding"))
1333 ((not (or (memq 'ok process-state)
1334 (memq 'end process-state)))
1335 (gnus-message 2 "End of articles reached before end of file")))
1336 ;; Make unsuccessfully decoded articles unread.
1337 (when gnus-uu-unmark-articles-not-decoded
1338 (while article-series
1339 (gnus-summary-tick-article (pop article-series) t)))))
1343 (defun gnus-uu-grab-view (file)
1344 "View FILE using the gnus-uu methods."
1345 (let ((action (gnus-uu-get-action file)))
1346 (gnus-execute-command
1347 (if (string-match "%" action)
1348 (format action file)
1349 (concat action " " file))
1350 (eq gnus-view-pseudos 'not-confirm))))
1352 (defun gnus-uu-grab-move (file)
1353 "Move FILE to somewhere."
1354 (when gnus-uu-default-dir
1355 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1356 (file-name-nondirectory file))))
1357 (rename-file file to-file)
1358 (unless (file-exists-p file)
1359 (make-symbolic-link to-file file)))))
1361 (defun gnus-uu-part-number (article)
1362 (let* ((header (gnus-summary-article-header article))
1363 (subject (and header (mail-header-subject header)))
1366 (while (string-match "[0-9]+/[0-9]+\\|[0-9]+[ \t]+of[ \t]+[0-9]+"
1368 (setq part (match-string 0 subject))
1369 (setq subject (substring subject (match-end 0)))))
1371 (while (string-match "\\([0-9]+\\)[^0-9]+\\([0-9]+\\)" subject)
1372 (setq part (match-string 0 subject))
1373 (setq subject (substring subject (match-end 0)))))
1376 (defun gnus-uu-uudecode-sentinel (process event)
1377 (delete-process (get-process process)))
1379 (defun gnus-uu-uustrip-article (process-buffer in-state)
1380 ;; Uudecodes a file asynchronously.
1382 (set-buffer process-buffer)
1383 (let ((state (list 'wrong-type))
1384 process-connection-type case-fold-search buffer-read-only
1386 (goto-char (point-min))
1388 ;; Deal with ^M at the end of the lines.
1389 (when gnus-uu-kill-carriage-return
1391 (while (search-forward "\r" nil t)
1392 (delete-backward-char 1))))
1394 (while (or (re-search-forward gnus-uu-begin-string nil t)
1395 (re-search-forward gnus-uu-body-line nil t))
1396 (setq state (list 'ok))
1397 ;; Ok, we are at the first uucoded line.
1399 (setq start-char (point))
1401 (if (not (looking-at gnus-uu-begin-string))
1402 (setq state (list 'middle))
1403 ;; This is the beginning of a uuencoded article.
1404 ;; We replace certain characters that could make things messy.
1405 (setq gnus-uu-file-name
1406 (let ((nnheader-file-name-translation-alist
1407 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1408 (nnheader-translate-file-chars (match-string 1))))
1409 (replace-match (concat "begin 644 " gnus-uu-file-name) t t)
1411 ;; Remove any non gnus-uu-body-line right after start.
1413 (while (and (not (eobp))
1414 (not (looking-at gnus-uu-body-line)))
1417 ;; If a process is running, we kill it.
1418 (when (and gnus-uu-uudecode-process
1419 (memq (process-status gnus-uu-uudecode-process)
1421 (delete-process gnus-uu-uudecode-process)
1422 (gnus-uu-unmark-list-of-grabbed t))
1424 ;; Start a new uudecoding process.
1425 (let ((cdir default-directory))
1428 (cd gnus-uu-work-dir)
1429 (setq gnus-uu-uudecode-process
1432 (gnus-get-buffer-create gnus-uu-output-buffer-name)
1433 shell-file-name shell-command-switch
1434 (format "cd %s %s uudecode" gnus-uu-work-dir
1435 gnus-shell-command-separator))))
1437 (set-process-sentinel
1438 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1439 (setq state (list 'begin))
1440 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1442 ;; We look for the end of the thing to be decoded.
1443 (if (re-search-forward gnus-uu-end-string nil t)
1445 (goto-char (point-max))
1446 (re-search-backward gnus-uu-body-line nil t))
1450 (when gnus-uu-uudecode-process
1451 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1452 ;; Try to correct mishandled uucode.
1453 (when gnus-uu-correct-stripped-uucode
1454 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1455 (gnus-run-hooks 'gnus-uu-pre-uudecode-hook)
1457 ;; Send the text to the process.
1459 (process-send-region
1460 gnus-uu-uudecode-process start-char (point))
1463 (delete-process gnus-uu-uudecode-process)
1464 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1465 (setq state (list 'wrong-type)))))
1467 (if (memq 'end state)
1469 ;; Send an EOF, just in case.
1471 (process-send-eof gnus-uu-uudecode-process))
1472 (while (memq (process-status gnus-uu-uudecode-process)
1474 (accept-process-output gnus-uu-uudecode-process 1)))
1475 (when (or (not gnus-uu-uudecode-process)
1476 (not (memq (process-status gnus-uu-uudecode-process)
1478 (setq state (list 'wrong-type)))))))
1480 (if (memq 'begin state)
1481 (cons (if (= (length files) 1) (car files) files) state)
1484 ;; This function is used by `gnus-uu-grab-articles' to treat
1485 ;; a shared article.
1486 (defun gnus-uu-unshar-article (process-buffer in-state)
1487 (let ((state (list 'ok))
1490 (set-buffer process-buffer)
1491 (goto-char (point-min))
1492 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1493 (setq state (list 'wrong-type))
1495 (setq start-char (point))
1496 (call-process-region
1497 start-char (point-max) shell-file-name nil
1498 (gnus-get-buffer-create gnus-uu-output-buffer-name) nil
1499 shell-command-switch
1500 (concat "cd " gnus-uu-work-dir " "
1501 gnus-shell-command-separator " sh"))))
1504 ;; Returns the name of what the shar file is going to unpack.
1505 (defun gnus-uu-find-name-in-shar ()
1506 (let ((oldpoint (point))
1508 (goto-char (point-min))
1509 (when (re-search-forward gnus-uu-shar-name-marker nil t)
1510 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1511 (goto-char oldpoint)
1514 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1515 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1516 ;; found, or the name of the command to run if such a rule is found.
1517 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1518 (let ((action-list (copy-sequence file-action-list))
1519 (case-fold-search t)
1524 (and gnus-uu-ignore-files-by-name
1525 (string-match gnus-uu-ignore-files-by-name file-name)))
1527 (and gnus-uu-ignore-files-by-type
1528 (string-match gnus-uu-ignore-files-by-type
1529 (or (gnus-uu-choose-action
1530 file-name gnus-uu-ext-to-mime-list t)
1532 (while (not (or (eq action-list ()) action))
1533 (setq rule (car action-list))
1534 (setq action-list (cdr action-list))
1535 (when (string-match (car rule) file-name)
1536 (setq action (cadr rule)))))
1539 (defun gnus-uu-treat-archive (file-path)
1540 ;; Unpacks an archive. Returns t if unpacking is successful.
1541 (let ((did-unpack t)
1543 (setq action (gnus-uu-choose-action
1544 file-path (append gnus-uu-user-archive-rules
1545 (if gnus-uu-ignore-default-archive-rules
1547 gnus-uu-default-archive-rules))))
1550 (error "No unpackers for the file %s" file-path))
1552 (string-match "/[^/]*$" file-path)
1553 (setq dir (substring file-path 0 (match-beginning 0)))
1555 (when (member action gnus-uu-destructive-archivers)
1556 (copy-file file-path (concat file-path "~") t))
1558 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1561 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1564 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1566 (if (= 0 (call-process shell-file-name nil
1567 (gnus-get-buffer-create gnus-uu-output-buffer-name)
1568 nil shell-command-switch command))
1570 (gnus-message 2 "Error during unpacking of archive")
1571 (setq did-unpack nil))
1573 (when (member action gnus-uu-destructive-archivers)
1574 (rename-file (concat file-path "~") file-path t))
1578 (defun gnus-uu-dir-files (dir)
1579 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1582 (if (file-directory-p (setq file (car dirs)))
1583 (setq files (append files (gnus-uu-dir-files file)))
1585 (setq dirs (cdr dirs)))
1588 (defun gnus-uu-unpack-files (files &optional ignore)
1589 ;; Go through FILES and look for files to unpack.
1590 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1594 (setq file (cdr (assq 'name (car files))))
1595 (when (and (not (member file ignore))
1596 (equal (gnus-uu-get-action (file-name-nondirectory file))
1598 (push file did-unpack)
1599 (unless (gnus-uu-treat-archive file)
1600 (gnus-message 2 "Error during unpacking of %s" file))
1601 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1604 (unless (member (car nfiles) totfiles)
1605 (push (list (cons 'name (car nfiles))
1606 (cons 'original file))
1608 (setq nfiles (cdr nfiles)))
1609 (setq totfiles newfiles)))
1610 (setq files (cdr files)))
1612 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1615 (defun gnus-uu-ls-r (dir)
1616 (let* ((files (gnus-uu-directory-files dir t))
1619 (when (file-directory-p (car files))
1620 (setq ofiles (delete (car files) ofiles))
1621 (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1622 (setq files (cdr files)))
1627 (defun gnus-uu-directory-files (dir &optional full)
1628 (let (files out file)
1629 (setq files (directory-files dir full))
1631 (setq file (car files))
1632 (setq files (cdr files))
1633 (unless (member (file-name-nondirectory file) '("." ".."))
1635 (setq out (nreverse out))
1638 (defun gnus-uu-check-correct-stripped-uucode (start end)
1640 (let (found beg length)
1641 (if (not gnus-uu-correct-stripped-uucode)
1645 (if (re-search-forward " \\|`" end t)
1650 (when (looking-at "\n")
1655 (if (looking-at (concat gnus-uu-begin-string "\\|"
1656 gnus-uu-end-string))
1662 (setq length (- (point) beg)))
1667 (when (not (= length (- (point) beg)))
1668 (insert (make-string (- length (- (point) beg)) ? ))))
1669 (forward-line 1)))))))
1671 (defvar gnus-uu-tmp-alist nil)
1673 (defun gnus-uu-initialize (&optional scan)
1676 (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1677 (if (file-exists-p (cdr entry))
1678 (setq gnus-uu-work-dir (cdr entry))
1679 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1682 (setq gnus-uu-tmp-dir (file-name-as-directory
1683 (expand-file-name gnus-uu-tmp-dir)))
1684 (if (not (file-directory-p gnus-uu-tmp-dir))
1685 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1686 (when (not (file-writable-p gnus-uu-tmp-dir))
1687 (error "Temp directory %s can't be written to"
1690 (setq gnus-uu-work-dir
1691 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1692 (gnus-make-directory gnus-uu-work-dir)
1693 (set-file-modes gnus-uu-work-dir 448)
1694 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1695 (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1696 gnus-uu-tmp-alist))))
1699 ;; Kills the temporary uu buffers, kills any processes, etc.
1700 (defun gnus-uu-clean-up ()
1702 (and gnus-uu-uudecode-process
1703 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1705 (delete-process gnus-uu-uudecode-process))
1706 (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1707 (kill-buffer buf))))
1709 ;; Inputs an action and a filename and returns a full command, making sure
1710 ;; that the filename will be treated as a single argument when the shell
1711 ;; executes the command.
1712 (defun gnus-uu-command (action file)
1713 (let ((quoted-file (mm-quote-arg file)))
1714 (if (string-match "%s" action)
1715 (format action quoted-file)
1716 (concat action " " quoted-file))))
1718 (defun gnus-uu-delete-work-dir (&optional dir)
1719 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1721 (gnus-message 7 "Deleting directory %s..." dir)
1722 (setq dir gnus-uu-work-dir))
1724 (file-exists-p dir))
1725 (let ((files (directory-files dir t nil t))
1727 (while (setq file (pop files))
1728 (unless (member (file-name-nondirectory file) '("." ".."))
1729 (if (file-directory-p file)
1730 (gnus-uu-delete-work-dir file)
1731 (gnus-message 9 "Deleting file %s..." file)
1732 (delete-file file))))
1733 (delete-directory dir)))
1734 (gnus-message 7 ""))
1738 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1739 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1744 ;;; uuencoded posting
1747 ;; Any function that is to be used as and encoding method will take two
1748 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1749 ;; and "spiral.jpg", respectively.) The function should return nil if
1750 ;; the encoding wasn't successful.
1751 (defcustom gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1752 "Function used for encoding binary files.
1753 There are three functions supplied with gnus-uu for encoding files:
1754 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1755 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1756 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1757 uuencode and adds MIME headers."
1758 :group 'gnus-extract-post
1759 :type '(radio (function-item gnus-uu-post-encode-uuencode)
1760 (function-item gnus-uu-post-encode-mime)
1761 (function-item gnus-uu-post-encode-mime-uuencode)
1762 (function :tag "Other")))
1764 (defcustom gnus-uu-post-include-before-composing nil
1765 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1766 If this variable is t, you can either include an encoded file with
1767 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article."
1768 :group 'gnus-extract-post
1771 (defcustom gnus-uu-post-length 990
1772 "Maximum length of an article.
1773 The encoded file will be split into how many articles it takes to
1774 post the entire file."
1775 :group 'gnus-extract-post
1778 (defcustom gnus-uu-post-threaded nil
1779 "Non-nil means that gnus-uu will post the encoded file in a thread.
1780 This may not be smart, as no other decoder I have seen are able to
1781 follow threads when collecting uuencoded articles. (Well, I have seen
1782 one package that does that - gnus-uu, but somehow, I don't think that
1783 counts...) The default is nil."
1784 :group 'gnus-extract-post
1787 (defcustom gnus-uu-post-separate-description t
1788 "Non-nil means that the description will be posted in a separate article.
1789 The first article will typically be numbered (0/x). If this variable
1790 is nil, the description the user enters will be included at the
1791 beginning of the first article, which will be numbered (1/x). Default
1793 :group 'gnus-extract-post
1796 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1797 (defvar gnus-uu-post-message-id nil)
1798 (defvar gnus-uu-post-inserted-file-name nil)
1799 (defvar gnus-uu-winconf-post-news nil)
1801 (defun gnus-uu-post-news ()
1802 "Compose an article and post an encoded file."
1804 (setq gnus-uu-post-inserted-file-name nil)
1805 (setq gnus-uu-winconf-post-news (current-window-configuration))
1807 (gnus-summary-post-news)
1809 (let ((map (make-sparse-keymap)))
1810 (set-keymap-parent map (current-local-map))
1811 (use-local-map map))
1812 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1813 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1814 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1815 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1817 (when gnus-uu-post-include-before-composing
1818 (save-excursion (setq gnus-uu-post-inserted-file-name
1819 (gnus-uu-post-insert-binary)))))
1821 (defun gnus-uu-post-insert-binary-in-article ()
1822 "Inserts an encoded file in the buffer.
1823 The user will be asked for a file name."
1826 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1828 ;; Encodes with uuencode and substitutes all spaces with backticks.
1829 (defun gnus-uu-post-encode-uuencode (path file-name)
1830 (when (gnus-uu-post-encode-file "uuencode" path file-name)
1831 (goto-char (point-min))
1833 (while (re-search-forward " " nil t)
1834 (replace-match "`"))
1837 ;; Encodes with uuencode and adds MIME headers.
1838 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1839 (when (gnus-uu-post-encode-uuencode path file-name)
1840 (gnus-uu-post-make-mime file-name "x-uue")
1843 ;; Encodes with base64 and adds MIME headers
1844 (defun gnus-uu-post-encode-mime (path file-name)
1845 (when (zerop (call-process shell-file-name nil t nil shell-command-switch
1846 (format "%s %s -o %s" "mmencode" path file-name)))
1847 (gnus-uu-post-make-mime file-name "base64")
1850 ;; Adds MIME headers.
1851 (defun gnus-uu-post-make-mime (file-name encoding)
1852 (goto-char (point-min))
1853 (insert (format "Content-Type: %s; name=\"%s\"\n"
1854 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1856 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1858 (set-buffer gnus-message-buffer)
1859 (goto-char (point-min))
1860 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1862 (narrow-to-region 1 (point))
1863 (unless (mail-fetch-field "mime-version")
1865 (insert "MIME-Version: 1.0\n"))
1868 ;; Encodes a file PATH with COMMAND, leaving the result in the
1870 (defun gnus-uu-post-encode-file (command path file-name)
1871 (= 0 (call-process shell-file-name nil t nil shell-command-switch
1872 (format "%s %s %s" command path file-name))))
1874 (defun gnus-uu-post-news-inews ()
1875 "Posts the composed news article and encoded file.
1876 If no file has been included, the user will be asked for a file."
1881 (if gnus-uu-post-inserted-file-name
1882 (setq file-name gnus-uu-post-inserted-file-name)
1883 (setq file-name (gnus-uu-post-insert-binary)))
1885 (gnus-uu-post-encoded file-name gnus-uu-post-threaded))
1886 (setq gnus-uu-post-inserted-file-name nil)
1887 (when gnus-uu-winconf-post-news
1888 (set-window-configuration gnus-uu-winconf-post-news)))
1890 ;; Asks for a file to encode, encodes it and inserts the result in
1891 ;; the current buffer. Returns the file name the user gave.
1892 (defun gnus-uu-post-insert-binary ()
1893 (let ((uuencode-buffer-name "*uuencode buffer*")
1894 file-path uubuf file-name)
1896 (setq file-path (read-file-name
1897 "What file do you want to encode? "))
1898 (when (not (file-exists-p file-path))
1899 (error "%s: No such file" file-path))
1901 (goto-char (point-max))
1902 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1904 ;; #### Unix-specific?
1905 (when (string-match "^~/" file-path)
1906 (setq file-path (concat "$HOME" (substring file-path 1))))
1907 ;; #### Unix-specific?
1908 (if (string-match "/[^/]*$" file-path)
1909 (setq file-name (substring file-path (1+ (match-beginning 0))))
1910 (setq file-name file-path))
1914 (set-buffer (setq uubuf
1915 (gnus-get-buffer-create uuencode-buffer-name)))
1917 (funcall gnus-uu-post-encode-method file-path file-name))
1918 (insert-buffer-substring uubuf)
1919 (error "Encoding unsuccessful"))
1920 (kill-buffer uubuf))
1923 ;; Posts the article and all of the encoded file.
1924 (defun gnus-uu-post-encoded (file-name &optional threaded)
1925 (let ((send-buffer-name "*uuencode send buffer*")
1926 (encoded-buffer-name "*encoded buffer*")
1927 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1928 (separator (concat mail-header-separator "\n\n"))
1929 uubuf length parts header i end beg
1930 beg-line minlen post-buf whole-len beg-binary end-binary)
1932 (setq post-buf (current-buffer))
1934 (goto-char (point-min))
1935 (when (not (re-search-forward
1936 (if gnus-uu-post-separate-description
1937 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1939 (concat "^" (regexp-quote mail-header-separator) "$"))
1941 (error "Internal error: No binary/header separator"))
1944 (setq beg-binary (point))
1945 (setq end-binary (point-max))
1948 (set-buffer (setq uubuf (gnus-get-buffer-create encoded-buffer-name)))
1950 (insert-buffer-substring post-buf beg-binary end-binary)
1951 (goto-char (point-min))
1952 (setq length (count-lines 1 (point-max)))
1953 (setq parts (/ length gnus-uu-post-length))
1954 (unless (< (% length gnus-uu-post-length) 4)
1957 (when gnus-uu-post-separate-description
1959 (delete-region (point) (point-max))
1961 (goto-char (point-min))
1963 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1965 (setq header (buffer-substring 1 (point)))
1967 (goto-char (point-min))
1968 (when gnus-uu-post-separate-description
1969 (when (re-search-forward "^Subject: " nil t)
1971 (insert (format " (0/%d)" parts)))
1974 (setq gnus-uu-post-message-id (message-fetch-field "message-id")))
1979 (while (not (> i parts))
1980 (set-buffer (gnus-get-buffer-create send-buffer-name))
1983 (when (and threaded gnus-uu-post-message-id)
1984 (insert "References: " gnus-uu-post-message-id "\n"))
1987 (- 62 (length (format top-string "" file-name i parts ""))))
1988 (when (> 1 (setq minlen (/ whole-len 2)))
1993 (make-string minlen ?-)
1996 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1998 (goto-char (point-min))
1999 (when (re-search-forward "^Subject: " nil t)
2001 (insert (format " (%d/%d)" i parts)))
2003 (goto-char (point-max))
2008 (goto-char (point-max))
2009 (forward-line gnus-uu-post-length))
2010 (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
2013 (insert-buffer-substring uubuf beg end)
2014 (insert beg-line "\n")
2017 (goto-char (point-min))
2019 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2022 (when (re-search-forward
2023 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
2029 (let (message-sent-message-via)
2032 (setq gnus-uu-post-message-id
2033 (concat (message-fetch-field "references") " "
2034 (message-fetch-field "message-id"))))))
2036 (gnus-kill-buffer send-buffer-name)
2037 (gnus-kill-buffer encoded-buffer-name)
2039 (when (not gnus-uu-post-separate-description)
2040 (set-buffer-modified-p nil)
2041 (when (fboundp 'bury-buffer)
2046 ;; gnus-uu.el ends here