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))
36 (defgroup gnus-extract nil
37 "Extracting encoded files."
41 (defgroup gnus-extract-view nil
42 "Viewwing extracted files."
45 (defgroup gnus-extract-archive nil
46 "Extracting encoded archives."
49 (defgroup gnus-extract-post nil
50 "Extracting encoded archives."
51 :prefix "gnus-uu-post"
54 ;; Default viewing action rules
56 (defcustom gnus-uu-default-view-rules
57 '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed 's/\r$//'")
58 ("\\.pas$" "cat %s | sed 's/\r$//'")
59 ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
60 ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "display")
61 ("\\.tga$" "tgatoppm %s | ee -")
62 ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
63 "sox -v .5 %s -t .au -u - > /dev/audio")
64 ("\\.au$" "cat %s > /dev/audio")
65 ("\\.midi?$" "playmidi -f")
67 ("\\.ps$" "ghostview")
69 ("\\.html$" "xmosaic")
70 ("\\.mpe?g$" "mpeg_play")
71 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
72 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
74 "*Default actions to be taken when the user asks to view a file.
75 To change the behaviour, you can either edit this variable or set
76 `gnus-uu-user-view-rules' to something useful.
80 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
81 following in your .emacs file:
83 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
85 Both these variables are lists of lists with two string elements. The
86 first string is a regular expression. If the file name matches this
87 regular expression, the command in the second string is executed with
88 the file as an argument.
90 If the command string contains \"%s\", the file name will be inserted
91 at that point in the command string. If there's no \"%s\" in the
92 command string, the file name will be appended to the command string
95 There are several user variables to tailor the behaviour of gnus-uu to
96 your needs. First we have `gnus-uu-user-view-rules', which is the
97 variable gnus-uu first consults when trying to decide how to view a
98 file. If this variable contains no matches, gnus-uu examines the
99 default rule variable provided in this package. If gnus-uu finds no
100 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
102 :group 'gnus-extract-view
103 :type '(repeat (group regexp (string :tag "Command"))))
105 (defcustom gnus-uu-user-view-rules nil
106 "What actions are to be taken to view a file.
107 See the documentation on the `gnus-uu-default-view-rules' variable for
109 :group 'gnus-extract-view
110 :type '(repeat (group regexp (string :tag "Command"))))
112 (defcustom gnus-uu-user-view-rules-end
114 "*What actions are to be taken if no rule matched the file name.
115 See the documentation on the `gnus-uu-default-view-rules' variable for
117 :group 'gnus-extract-view
118 :type '(repeat (group regexp (string :tag "Command"))))
120 ;; Default unpacking commands
122 (defcustom gnus-uu-default-archive-rules
123 '(("\\.tar$" "tar xf")
124 ("\\.zip$" "unzip -o")
126 ("\\.arj$" "unarj x")
128 ("\\.\\(lzh\\|lha\\)$" "lha x")
129 ("\\.Z$" "uncompress")
131 ("\\.arc$" "arc -x"))
132 "*See `gnus-uu-user-archive-rules'."
133 :group 'gnus-extract-archive
134 :type '(repeat (group regexp (string :tag "Command"))))
136 (defvar gnus-uu-destructive-archivers
137 (list "uncompress" "gunzip"))
139 (defcustom gnus-uu-user-archive-rules nil
140 "A list that can be set to override the default archive unpacking commands.
141 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
142 unpack zip files, say the following:
143 (setq gnus-uu-user-archive-rules
144 '((\"\\\\.tar$\" \"untar\")
145 (\"\\\\.zip$\" \"zip -x\")))"
146 :group 'gnus-extract-archive
147 :type '(repeat (group regexp (string :tag "Command"))))
149 (defcustom gnus-uu-ignore-files-by-name nil
150 "*A regular expression saying what files should not be viewed based on name.
151 If, for instance, you want gnus-uu to ignore all .au and .wav files,
152 you could say something like
154 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
156 Note that this variable can be used in conjunction with the
157 `gnus-uu-ignore-files-by-type' variable."
159 :type '(choice (const :tag "off" nil)
160 (regexp :format "%v")))
162 (defcustom gnus-uu-ignore-files-by-type nil
163 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
164 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
165 you could say something like
167 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
169 Note that this variable can be used in conjunction with the
170 `gnus-uu-ignore-files-by-name' variable."
172 :type '(choice (const :tag "off" nil)
173 (regexp :format "%v")))
175 ;; Pseudo-MIME support
177 (defconst gnus-uu-ext-to-mime-list
178 '(("\\.gif$" "image/gif")
179 ("\\.jpe?g$" "image/jpeg")
180 ("\\.tiff?$" "image/tiff")
181 ("\\.xwd$" "image/xwd")
182 ("\\.pbm$" "image/pbm")
183 ("\\.pgm$" "image/pgm")
184 ("\\.ppm$" "image/ppm")
185 ("\\.xbm$" "image/xbm")
186 ("\\.pcx$" "image/pcx")
187 ("\\.tga$" "image/tga")
188 ("\\.ps$" "image/postscript")
189 ("\\.fli$" "video/fli")
190 ("\\.wav$" "audio/wav")
191 ("\\.aiff$" "audio/aiff")
192 ("\\.hcom$" "audio/hcom")
193 ("\\.voc$" "audio/voc")
194 ("\\.smp$" "audio/smp")
195 ("\\.mod$" "audio/mod")
196 ("\\.dvi$" "image/dvi")
197 ("\\.mpe?g$" "video/mpeg")
198 ("\\.au$" "audio/basic")
199 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
200 ("\\.\\(c\\|h\\)$" "text/source")
201 ("read.*me" "text/plain")
202 ("\\.html$" "text/html")
203 ("\\.bat$" "text/bat")
204 ("\\.[1-6]$" "text/man")
205 ("\\.flc$" "video/flc")
206 ("\\.rle$" "video/rle")
207 ("\\.pfx$" "video/pfx")
208 ("\\.avi$" "video/avi")
209 ("\\.sme$" "video/sme")
210 ("\\.rpza$" "video/prza")
211 ("\\.dl$" "video/dl")
212 ("\\.qt$" "video/qt")
213 ("\\.rsrc$" "video/rsrc")
214 ("\\..*$" "unknown/unknown")))
216 ;; Various variables users may set
218 (defcustom gnus-uu-tmp-dir
219 (cond ((fboundp 'temp-directory) (temp-directory))
220 ((boundp 'temporary-file-directory) temporary-file-directory)
222 "*Variable saying where gnus-uu is to do its work.
223 Default is \"/tmp/\"."
227 (defcustom gnus-uu-do-not-unpack-archives nil
228 "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
230 :group 'gnus-extract-archive
233 (defcustom gnus-uu-ignore-default-view-rules nil
234 "*Non-nil means that gnus-uu will ignore the default viewing rules.
235 Only the user viewing rules will be consulted. Default is nil."
236 :group 'gnus-extract-view
239 (defcustom gnus-uu-grabbed-file-functions nil
240 "Functions run on each file after successful decoding.
241 They will be called with the name of the file as the argument.
242 Likely functions you can use in this list are `gnus-uu-grab-view'
243 and `gnus-uu-grab-move'."
245 :options '(gnus-uu-grab-view gnus-uu-grab-move)
248 (defcustom gnus-uu-ignore-default-archive-rules nil
249 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
250 Only the user unpacking commands will be consulted. Default is nil."
251 :group 'gnus-extract-archive
254 (defcustom gnus-uu-kill-carriage-return t
255 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
260 (defcustom gnus-uu-view-with-metamail nil
261 "*Non-nil means that files will be viewed with metamail.
262 The gnus-uu viewing functions will be ignored and gnus-uu will try
263 to guess at a content-type based on file name suffixes. Default
268 (defcustom gnus-uu-unmark-articles-not-decoded nil
269 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
274 (defcustom gnus-uu-correct-stripped-uucode nil
275 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
280 (defcustom gnus-uu-save-in-digest nil
281 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
282 If this variable is nil, gnus-uu will just save everything in a
283 file without any embellishments. The digesting almost conforms to RFC1153 -
284 no easy way to specify any meaningful volume and issue numbers were found,
285 so I simply dropped them."
289 (defcustom gnus-uu-pre-uudecode-hook nil
290 "Hook run before sending a message to uudecode."
294 (defcustom gnus-uu-digest-headers
295 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
296 "^Summary:" "^References:" "^Content-Type:" "^Content-Transfer-Encoding:")
297 "*List of regexps to match headers included in digested messages.
298 The headers will be included in the sequence they are matched."
300 :type '(repeat regexp))
302 (defcustom gnus-uu-save-separate-articles nil
303 "*Non-nil means that gnus-uu will save articles in separate files."
307 (defcustom gnus-uu-be-dangerous 'ask
308 "*Specifies what to do if unusual situations arise during decoding.
309 If nil, be as conservative as possible. If t, ignore things that
310 didn't work, and overwrite existing files. Otherwise, ask each time."
312 :type '(choice (const :tag "conservative" nil)
313 (const :tag "ask" ask)
314 (const :tag "liberal" t)))
316 ;; Internal variables
318 (defvar gnus-uu-saved-article-name nil)
320 (defvar gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
321 (defvar gnus-uu-end-string "^end[ \t]*$")
323 (defvar gnus-uu-body-line "^M")
325 (while (> (setq i (1- i)) 0)
326 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
327 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
329 ;"^M.............................................................?$"
331 (defvar gnus-uu-shar-begin-string "^#! */bin/sh")
333 (defvar gnus-uu-shar-file-name nil)
334 (defvar gnus-uu-shar-name-marker
335 "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
337 (defvar gnus-uu-postscript-begin-string "^%!PS-")
338 (defvar gnus-uu-postscript-end-string "^%%EOF$")
340 (defvar gnus-uu-file-name nil)
341 (defvar gnus-uu-uudecode-process nil)
342 (defvar gnus-uu-binhex-article-name nil)
344 (defvar gnus-uu-work-dir nil)
346 (defvar gnus-uu-output-buffer-name " *Gnus UU Output*")
348 (defvar gnus-uu-default-dir gnus-article-save-directory)
349 (defvar gnus-uu-digest-from-subject nil)
353 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
354 "p" gnus-summary-mark-as-processable
355 "u" gnus-summary-unmark-as-processable
356 "U" gnus-summary-unmark-all-processable
357 "v" gnus-uu-mark-over
358 "s" gnus-uu-mark-series
359 "r" gnus-uu-mark-region
360 "g" gnus-uu-unmark-region
361 "R" gnus-uu-mark-by-regexp
362 "G" gnus-uu-unmark-by-regexp
363 "t" gnus-uu-mark-thread
364 "T" gnus-uu-unmark-thread
366 "b" gnus-uu-mark-buffer
367 "S" gnus-uu-mark-sparse
368 "k" gnus-summary-kill-process-mark
369 "y" gnus-summary-yank-process-mark
370 "w" gnus-summary-save-process-mark
371 "i" gnus-uu-invert-processable)
373 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
374 ;;"x" gnus-uu-extract-any
375 "m" gnus-summary-save-parts
376 "u" gnus-uu-decode-uu
377 "U" gnus-uu-decode-uu-and-save
378 "s" gnus-uu-decode-unshar
379 "S" gnus-uu-decode-unshar-and-save
380 "o" gnus-uu-decode-save
381 "O" gnus-uu-decode-save
382 "b" gnus-uu-decode-binhex
383 "B" gnus-uu-decode-binhex
384 "p" gnus-uu-decode-postscript
385 "P" gnus-uu-decode-postscript-and-save)
388 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
389 "u" gnus-uu-decode-uu-view
390 "U" gnus-uu-decode-uu-and-save-view
391 "s" gnus-uu-decode-unshar-view
392 "S" gnus-uu-decode-unshar-and-save-view
393 "o" gnus-uu-decode-save-view
394 "O" gnus-uu-decode-save-view
395 "b" gnus-uu-decode-binhex-view
396 "B" gnus-uu-decode-binhex-view
397 "p" gnus-uu-decode-postscript-view
398 "P" gnus-uu-decode-postscript-and-save-view)
403 (defun gnus-uu-decode-uu (&optional n)
404 "Uudecodes the current article."
406 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
408 (defun gnus-uu-decode-uu-and-save (n dir)
409 "Decodes and saves the resulting file."
411 (list current-prefix-arg
412 (file-name-as-directory
413 (read-file-name "Uudecode and save in dir: "
415 gnus-uu-default-dir t))))
416 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
418 (defun gnus-uu-decode-unshar (&optional n)
419 "Unshars the current article."
421 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
423 (defun gnus-uu-decode-unshar-and-save (n dir)
424 "Unshars and saves the current article."
426 (list current-prefix-arg
427 (file-name-as-directory
428 (read-file-name "Unshar and save in dir: "
430 gnus-uu-default-dir t))))
431 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
433 (defun gnus-uu-decode-save (n file)
434 "Saves the current article."
436 (list current-prefix-arg
438 (if gnus-uu-save-separate-articles
439 "Save articles is dir: "
440 "Save articles in file: ")
442 gnus-uu-default-dir)))
443 (setq gnus-uu-saved-article-name file)
444 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
446 (defun gnus-uu-decode-binhex (n dir)
447 "Unbinhexes the current article."
449 (list current-prefix-arg
450 (file-name-as-directory
451 (read-file-name "Unbinhex and save in dir: "
453 gnus-uu-default-dir))))
454 (setq gnus-uu-binhex-article-name
455 (make-temp-name (concat gnus-uu-work-dir "binhex")))
456 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
458 (defun gnus-uu-decode-uu-view (&optional n)
459 "Uudecodes and views the current article."
461 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
462 (gnus-uu-decode-uu n)))
464 (defun gnus-uu-decode-uu-and-save-view (n dir)
465 "Decodes, views and saves the resulting file."
467 (list current-prefix-arg
468 (read-file-name "Uudecode, view and save in dir: "
470 gnus-uu-default-dir t)))
471 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
472 (gnus-uu-decode-uu-and-save n dir)))
474 (defun gnus-uu-decode-unshar-view (&optional n)
475 "Unshars and views the current article."
477 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
478 (gnus-uu-decode-unshar n)))
480 (defun gnus-uu-decode-unshar-and-save-view (n dir)
481 "Unshars and saves the current article."
483 (list current-prefix-arg
484 (read-file-name "Unshar, view and save in dir: "
486 gnus-uu-default-dir t)))
487 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
488 (gnus-uu-decode-unshar-and-save n dir)))
490 (defun gnus-uu-decode-save-view (n file)
491 "Saves and views the current article."
493 (list current-prefix-arg
494 (read-file-name (if gnus-uu-save-separate-articles
495 "Save articles is dir: "
496 "Save articles in file: ")
497 gnus-uu-default-dir gnus-uu-default-dir)))
498 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
499 (gnus-uu-decode-save n file)))
501 (defun gnus-uu-decode-binhex-view (n file)
502 "Unbinhexes and views the current article."
504 (list current-prefix-arg
505 (read-file-name "Unbinhex, view and save in dir: "
506 gnus-uu-default-dir gnus-uu-default-dir)))
507 (setq gnus-uu-binhex-article-name
508 (make-temp-name (concat gnus-uu-work-dir "binhex")))
509 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
510 (gnus-uu-decode-binhex n file)))
513 ;; Digest and forward articles
515 (defun gnus-uu-digest-mail-forward (&optional n post)
516 "Digests and forwards all articles in this series."
518 (let ((gnus-uu-save-in-digest t)
519 (file (make-temp-name (nnheader-concat gnus-uu-tmp-dir "forward")))
521 (gnus-setup-message 'forward
522 (setq gnus-uu-digest-from-subject nil)
523 (gnus-uu-decode-save n file)
524 (setq buf (switch-to-buffer
525 (gnus-get-buffer-create " *gnus-uu-forward*")))
529 (let ((fs gnus-uu-digest-from-subject))
532 subject (gnus-simplify-subject-fuzzy (cdar fs))
534 (while (and fs (or from subject))
536 (unless (string= from (caar fs))
539 (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
544 (setq subject "Digested Articles"))
547 (if (gnus-news-group-p gnus-newsgroup-name)
550 (goto-char (point-min))
551 (when (re-search-forward "^Subject: ")
552 (delete-region (point) (gnus-point-at-eol))
554 (goto-char (point-min))
555 (when (re-search-forward "^From: ")
556 (delete-region (point) (gnus-point-at-eol))
558 (message-forward post))
559 (setq gnus-uu-digest-from-subject nil)))
561 (defun gnus-uu-digest-post-forward (&optional n)
562 "Digest and forward to a newsgroup."
564 (gnus-uu-digest-mail-forward n t))
568 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
569 "Set the process mark on articles whose subjects match REGEXP.
570 When called interactively, prompt for REGEXP.
571 Optional UNMARK non-nil means unmark instead of mark."
572 (interactive "sMark (regexp): \nP")
573 (let ((articles (gnus-uu-find-articles-matching regexp)))
576 (gnus-summary-remove-process-mark (pop articles))
577 (gnus-summary-set-process-mark (pop articles))))
579 (gnus-summary-position-point))
581 (defun gnus-uu-unmark-by-regexp (regexp)
582 "Remove the process mark from articles whose subjects match REGEXP.
583 When called interactively, prompt for REGEXP."
584 (interactive "sUnmark (regexp): ")
585 (gnus-uu-mark-by-regexp regexp t))
587 (defun gnus-uu-mark-series ()
588 "Mark the current series with the process mark."
590 (let ((articles (gnus-uu-find-articles-matching)))
592 (gnus-summary-set-process-mark (car articles))
593 (setq articles (cdr articles)))
595 (gnus-summary-position-point))
597 (defun gnus-uu-mark-region (beg end &optional unmark)
598 "Set the process mark on all articles between point and mark."
602 (while (< (point) end)
604 (gnus-summary-remove-process-mark (gnus-summary-article-number))
605 (gnus-summary-set-process-mark (gnus-summary-article-number)))
607 (gnus-summary-position-point))
609 (defun gnus-uu-unmark-region (beg end)
610 "Remove the process mark from all articles between point and mark."
612 (gnus-uu-mark-region beg end t))
614 (defun gnus-uu-mark-buffer ()
615 "Set the process mark on all articles in the buffer."
617 (gnus-uu-mark-region (point-min) (point-max)))
619 (defun gnus-uu-unmark-buffer ()
620 "Remove the process mark on all articles in the buffer."
622 (gnus-uu-mark-region (point-min) (point-max) t))
624 (defun gnus-uu-mark-thread ()
625 "Marks all articles downwards in this thread."
627 (gnus-save-hidden-threads
628 (let ((level (gnus-summary-thread-level)))
629 (while (and (gnus-summary-set-process-mark
630 (gnus-summary-article-number))
631 (zerop (gnus-summary-next-subject 1 nil t))
632 (> (gnus-summary-thread-level) level)))))
633 (gnus-summary-position-point))
635 (defun gnus-uu-unmark-thread ()
636 "Unmarks all articles downwards in this thread."
638 (let ((level (gnus-summary-thread-level)))
639 (while (and (gnus-summary-remove-process-mark
640 (gnus-summary-article-number))
641 (zerop (gnus-summary-next-subject 1))
642 (> (gnus-summary-thread-level) level))))
643 (gnus-summary-position-point))
645 (defun gnus-uu-invert-processable ()
646 "Invert the list of process-marked articles."
648 (let ((data gnus-newsgroup-data)
652 (if (memq (setq number (gnus-data-number (pop data)))
653 gnus-newsgroup-processable)
654 (gnus-summary-remove-process-mark number)
655 (gnus-summary-set-process-mark number)))))
656 (gnus-summary-position-point))
658 (defun gnus-uu-mark-over (&optional score)
659 "Mark all articles with a score over SCORE (the prefix)."
661 (let ((score (or score gnus-summary-default-score 0))
662 (data gnus-newsgroup-data))
665 (when (> (or (cdr (assq (gnus-data-number (car data))
666 gnus-newsgroup-scored))
667 gnus-summary-default-score 0)
669 (gnus-summary-set-process-mark (caar data)))
670 (setq data (cdr data))))
671 (gnus-summary-position-point)))
673 (defun gnus-uu-mark-sparse ()
674 "Mark all series that have some articles marked."
676 (let ((marked (nreverse gnus-newsgroup-processable))
677 subject articles total headers)
679 (error "No articles marked with the process mark"))
680 (setq gnus-newsgroup-processable nil)
683 (and (vectorp (setq headers
684 (gnus-summary-article-header (car marked))))
685 (setq subject (mail-header-subject headers)
686 articles (gnus-uu-find-articles-matching
687 (gnus-uu-reginize-string subject))
688 total (nconc total articles)))
690 (gnus-summary-set-process-mark (car articles))
691 (setcdr marked (delq (car articles) (cdr marked)))
692 (setq articles (cdr articles)))
693 (setq marked (cdr marked)))
694 (setq gnus-newsgroup-processable (nreverse total)))
695 (gnus-summary-position-point)))
697 (defun gnus-uu-mark-all ()
698 "Mark all articles in \"series\" order."
700 (setq gnus-newsgroup-processable nil)
702 (let ((data gnus-newsgroup-data)
705 (when (and (not (memq (setq number (gnus-data-number (car data)))
706 gnus-newsgroup-processable))
707 (vectorp (gnus-data-header (car data))))
708 (gnus-summary-goto-subject number)
709 (gnus-uu-mark-series))
710 (setq data (cdr data)))))
711 (gnus-summary-position-point))
713 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
715 (defun gnus-uu-decode-postscript (&optional n)
716 "Gets postscript of the current article."
718 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
720 (defun gnus-uu-decode-postscript-view (&optional n)
721 "Gets and views the current article."
723 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
724 (gnus-uu-decode-postscript n)))
726 (defun gnus-uu-decode-postscript-and-save (n dir)
727 "Extracts postscript and saves the current article."
729 (list current-prefix-arg
730 (file-name-as-directory
731 (read-file-name "Save in dir: "
733 gnus-uu-default-dir t))))
734 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
737 (defun gnus-uu-decode-postscript-and-save-view (n dir)
738 "Decodes, views and saves the resulting file."
740 (list current-prefix-arg
741 (read-file-name "Where do you want to save the file(s)? "
743 gnus-uu-default-dir t)))
744 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
745 (gnus-uu-decode-postscript-and-save n dir)))
748 ;; Internal functions.
750 (defun gnus-uu-decode-with-method (method n &optional save not-insert
752 (gnus-uu-initialize scan)
754 (setq gnus-uu-default-dir save))
755 ;; Create the directory we save to.
756 (when (and scan cdir save
757 (not (file-exists-p save)))
758 (make-directory save t))
759 (let ((articles (gnus-uu-get-list-of-articles n))
761 (setq files (gnus-uu-grab-articles articles method t))
762 (let ((gnus-current-article (car articles)))
764 (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
766 (gnus-uu-save-files files save))
767 (when (eq gnus-uu-do-not-unpack-archives nil)
768 (setq files (gnus-uu-unpack-files files)))
769 (setq files (nreverse (gnus-uu-get-actions files)))
770 (or not-insert (not gnus-insert-pseudo-articles)
771 (gnus-summary-insert-pseudos files save))))
773 (defun gnus-uu-scan-directory (dir &optional rec)
774 "Return a list of all files under DIR."
775 (let ((files (directory-files dir t))
777 (while (setq file (pop files))
778 (unless (member (file-name-nondirectory file) '("." ".."))
779 (push (list (cons 'name file)
780 (cons 'article gnus-current-article))
782 (when (file-directory-p file)
783 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
788 (defun gnus-uu-save-files (files dir)
790 (let ((len (length files))
791 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
792 to-file file fromdir)
793 (while (setq file (cdr (assq 'name (pop files))))
794 (when (file-exists-p file)
795 (string-match reg file)
796 (setq fromdir (substring file (match-end 0)))
797 (if (file-directory-p file)
798 (gnus-make-directory (concat dir fromdir))
799 (setq to-file (concat dir fromdir))
800 (when (or (not (file-exists-p to-file))
801 (eq gnus-uu-be-dangerous t)
802 (and gnus-uu-be-dangerous
803 (gnus-y-or-n-p (format "%s exists; overwrite? "
805 (copy-file file to-file t t)))))
806 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
808 ;; Functions for saving and possibly digesting articles without
811 ;; Function called by gnus-uu-grab-articles to treat each article.
812 (defun gnus-uu-save-article (buffer in-state)
814 (gnus-uu-save-separate-articles
818 (concat gnus-uu-saved-article-name gnus-current-article))
819 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
820 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
822 ((eq in-state 'last) (list 'end))
823 (t (list 'middle)))))
824 ((not gnus-uu-save-in-digest)
827 (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
828 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
829 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
831 ((eq in-state 'last) (list 'end))
832 (t (list 'middle)))))
834 (let ((header (gnus-summary-article-header)))
835 (push (cons (mail-header-from header)
836 (mail-header-subject header))
837 gnus-uu-digest-from-subject))
838 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
839 beg subj headers headline sorthead body end-string state)
840 (if (or (eq in-state 'first)
841 (eq in-state 'first-and-last))
843 (setq state (list 'begin))
845 (set-buffer (gnus-get-buffer-create "*gnus-uu-body*"))
848 (set-buffer (gnus-get-buffer-create "*gnus-uu-pre*"))
851 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
852 (current-time-string) name name))))
853 (when (not (eq in-state 'end))
854 (setq state (list 'middle))))
856 (set-buffer "*gnus-uu-body*")
857 (goto-char (setq beg (point-max)))
861 (let (buffer-read-only)
862 (gnus-set-text-properties (point-min) (point-max) nil)
863 ;; These two are necessary for XEmacs 19.12 fascism.
864 (put-text-property (point-min) (point-max) 'invisible nil)
865 (put-text-property (point-min) (point-max) 'intangible nil))
866 (goto-char (point-min))
867 (re-search-forward "\n\n")
868 ;; Quote all 30-dash lines.
870 (while (re-search-forward "^-" nil t)
874 (setq body (buffer-substring (1- (point)) (point-max)))
875 (narrow-to-region (point-min) (point))
876 (if (not (setq headers gnus-uu-digest-headers))
877 (setq sorthead (buffer-substring (point-min) (point-max)))
879 (setq headline (car headers))
880 (setq headers (cdr headers))
881 (goto-char (point-min))
882 (while (re-search-forward headline nil t)
887 (or (and (re-search-forward "^[^ \t]" nil t)
889 (progn (forward-line 1) (point)))))))))
891 (insert sorthead) (goto-char (point-max))
892 (insert body) (goto-char (point-max))
893 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
895 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
896 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
898 (set-buffer "*gnus-uu-pre*")
899 (insert (format " %s\n" subj)))))
900 (when (or (eq in-state 'last)
901 (eq in-state 'first-and-last))
903 (set-buffer "*gnus-uu-pre*")
904 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
905 (gnus-write-buffer gnus-uu-saved-article-name))
907 (set-buffer "*gnus-uu-body*")
908 (goto-char (point-max))
910 (concat (setq end-string (format "End of %s Digest" name))
912 (insert (concat (make-string (length end-string) ?*) "\n"))
914 (point-min) (point-max) gnus-uu-saved-article-name t))
915 (gnus-kill-buffer "*gnus-uu-pre*")
916 (gnus-kill-buffer "*gnus-uu-body*")
918 (if (memq 'begin state)
919 (cons gnus-uu-saved-article-name state)
922 ;; Binhex treatment - not very advanced.
924 (defvar gnus-uu-binhex-body-line
925 "^[^:]...............................................................$")
926 (defvar gnus-uu-binhex-begin-line
927 "^:...............................................................$")
928 (defvar gnus-uu-binhex-end-line
931 (defun gnus-uu-binhex-article (buffer in-state)
932 (let (state start-char)
936 (goto-char (point-min))
937 (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
938 (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
939 (setq state (list 'wrong-type))))
941 (if (memq 'wrong-type state)
944 (setq start-char (point))
945 (if (looking-at gnus-uu-binhex-begin-line)
947 (setq state (list 'begin))
948 (write-region 1 1 gnus-uu-binhex-article-name))
949 (setq state (list 'middle)))
950 (goto-char (point-max))
951 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
952 gnus-uu-binhex-end-line)
954 (when (looking-at gnus-uu-binhex-end-line)
955 (setq state (if (memq 'begin state)
960 (when (file-exists-p gnus-uu-binhex-article-name)
961 (write-region-as-binary start-char (point)
962 gnus-uu-binhex-article-name 'append))))
963 (if (memq 'begin state)
964 (cons gnus-uu-binhex-article-name state)
969 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
970 (let ((state (list 'ok))
971 start-char end-char file-name)
973 (set-buffer process-buffer)
974 (goto-char (point-min))
975 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
976 (setq state (list 'wrong-type))
978 (setq start-char (point))
979 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
980 (setq state (list 'wrong-type))
981 (setq end-char (point))
982 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
983 (insert-buffer-substring process-buffer start-char end-char)
984 (setq file-name (concat gnus-uu-work-dir
985 (cdr gnus-article-current) ".ps"))
986 (write-region (point-min) (point-max) file-name)
987 (setq state (list file-name 'begin 'end)))))
993 (defun gnus-uu-get-actions (files)
997 (setq name (cdr (assq 'name (car files))))
999 (setq action (gnus-uu-get-action name))
1000 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
1001 (cons 'action "file")
1002 (cons 'action action))
1003 (cons 'execute (gnus-uu-command
1006 (setq files (cdr files)))
1009 (defun gnus-uu-get-action (file-name)
1012 (gnus-uu-choose-action
1015 gnus-uu-user-view-rules
1016 (if gnus-uu-ignore-default-view-rules
1018 gnus-uu-default-view-rules)
1019 gnus-uu-user-view-rules-end)))
1020 (when (and (not (string= (or action "") "gnus-uu-archive"))
1021 gnus-uu-view-with-metamail)
1023 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
1024 (setq action (format "metamail -d -b -c \"%s\"" action))))
1028 ;; Functions for treating subjects and collecting series.
1030 (defun gnus-uu-reginize-string (string)
1031 ;; Takes a string and puts a \ in front of every special character;
1032 ;; replaces the last thing that looks like "2/3" with "[0-9]+/3"
1033 ;; or, if it can't find something like that, tries "2 of 3", then
1034 ;; finally just replaces the next to last number with "[0-9]+".
1036 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1037 (buffer-disable-undo)
1039 (insert (regexp-quote string))
1041 (setq case-fold-search nil)
1044 (if (re-search-backward "\\([^0-9]\\)[0-9]+/\\([0-9]+\\)" nil t)
1045 (replace-match "\\1[0-9]+/\\2")
1048 (if (re-search-backward "\\([^0-9]\\)[0-9]+[ \t]*of[ \t]*\\([0-9]+\\)"
1050 (replace-match "\\1[0-9]+ of \\2")
1053 (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1055 (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1058 (while (re-search-forward "[ \t]+" nil t)
1059 (replace-match "[ \t]+" t t))
1061 (buffer-substring 1 (point-max))))
1063 (defun gnus-uu-get-list-of-articles (n)
1064 ;; If N is non-nil, the article numbers of the N next articles
1065 ;; will be returned.
1066 ;; If any articles have been marked as processable, they will be
1068 ;; Failing that, articles that have subjects that are part of the
1069 ;; same "series" as the current will be returned.
1073 (setq n (prefix-numeric-value n))
1074 (let ((backward (< n 0))
1078 (push (gnus-summary-article-number)
1080 (gnus-summary-search-forward nil nil backward))
1082 (nreverse articles)))
1083 (gnus-newsgroup-processable
1084 (reverse gnus-newsgroup-processable))
1086 (gnus-uu-find-articles-matching)))))
1088 (defun gnus-uu-string< (l1 l2)
1089 (string< (car l1) (car l2)))
1091 (defun gnus-uu-find-articles-matching
1092 (&optional subject only-unread do-not-translate)
1093 ;; Finds all articles that matches the regexp SUBJECT. If it is
1094 ;; nil, the current article name will be used. If ONLY-UNREAD is
1095 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1096 ;; non-nil, article names are not equalized before sorting.
1097 (let ((subject (or subject
1098 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1102 ;; Collect all subjects matching subject.
1103 (let ((case-fold-search t)
1104 (data gnus-newsgroup-data)
1108 (and (not (gnus-data-pseudo-p d))
1109 (or (not only-unread)
1110 (= (setq mark (gnus-data-mark d))
1112 (= mark gnus-ticked-mark)
1113 (= mark gnus-dormant-mark))
1114 (setq subj (mail-header-subject (gnus-data-header d)))
1115 (string-match subject subj)
1116 (push (cons subj (gnus-data-number d))
1117 list-of-subjects))))
1119 ;; Expand numbers, sort, and return the list of article
1121 (mapcar (lambda (sub) (cdr sub))
1122 (sort (gnus-uu-expand-numbers
1124 (not do-not-translate))
1125 'gnus-uu-string<))))))
1127 (defun gnus-uu-expand-numbers (string-list &optional translate)
1128 ;; Takes a list of strings and "expands" all numbers in all the
1129 ;; strings. That is, this function makes all numbers equal length by
1130 ;; prepending lots of zeroes before each number. This is to ease later
1131 ;; sorting to find out what sequence the articles are supposed to be
1132 ;; decoded in. Returns the list of expanded strings.
1133 (let ((out-list string-list)
1136 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1137 (buffer-disable-undo)
1140 (insert (caar string-list))
1141 ;; Translate multiple spaces to one space.
1142 (goto-char (point-min))
1143 (while (re-search-forward "[ \t]+" nil t)
1144 (replace-match " "))
1145 ;; Translate all characters to "a".
1146 (goto-char (point-min))
1148 (while (re-search-forward "[A-Za-z]" nil t)
1149 (replace-match "a" t t)))
1151 (goto-char (point-min))
1152 (while (re-search-forward "[0-9]+" nil t)
1155 (string-to-int (buffer-substring
1156 (match-beginning 0) (match-end 0))))))
1157 (setq string (buffer-substring 1 (point-max)))
1158 (setcar (car string-list) string)
1159 (setq string-list (cdr string-list))))
1163 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1164 ;; function. It takes a list of articles to be grabbed and a function
1165 ;; to apply to each article.
1167 ;; The function to be called should take two parameters. The first
1168 ;; parameter is the article buffer. The function should leave the
1169 ;; result, if any, in this buffer. Most treatment functions will just
1170 ;; generate files...
1172 ;; The second parameter is the state of the list of articles, and can
1173 ;; have four values: `first', `middle', `last' and `first-and-last'.
1175 ;; The function should return a list. The list may contain the
1176 ;; following symbols:
1177 ;; `error' if an error occurred
1178 ;; `begin' if the beginning of an encoded file has been received
1179 ;; If the list returned contains a `begin', the first element of
1180 ;; the list *must* be a string with the file name of the decoded
1182 ;; `end' if the end of an encoded file has been received
1183 ;; `middle' if the article was a body part of an encoded file
1184 ;; `wrong-type' if the article was not a part of an encoded file
1185 ;; `ok', which can be used everything is ok
1187 (defvar gnus-uu-has-been-grabbed nil)
1189 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1191 (if (not (and gnus-uu-has-been-grabbed
1192 gnus-uu-unmark-articles-not-decoded))
1194 (when dont-unmark-last-article
1195 (setq art (car gnus-uu-has-been-grabbed))
1196 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1197 (while gnus-uu-has-been-grabbed
1198 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1199 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1200 (when dont-unmark-last-article
1201 (setq gnus-uu-has-been-grabbed (list art))))))
1203 ;; This function takes a list of articles and a function to apply to
1204 ;; each article grabbed.
1206 ;; This function returns a list of files decoded if the grabbing and
1207 ;; the process-function has been successful and nil otherwise.
1208 (defun gnus-uu-grab-articles (articles process-function
1209 &optional sloppy limit no-errors)
1210 (let ((state 'first)
1211 (gnus-asynchronous nil)
1212 (gnus-inhibit-treatment t)
1213 has-been-begin article result-file result-files process-state
1214 gnus-summary-display-article-function
1215 gnus-article-display-hook gnus-article-prepare-hook gnus-display-mime-function
1216 article-series files)
1218 (while (and articles
1219 (not (memq 'error process-state))
1221 (not (memq 'end process-state))))
1223 (setq article (pop articles))
1224 (when (vectorp (gnus-summary-article-header article))
1225 (push article article-series)
1228 (if (eq state 'first)
1229 (setq state 'first-and-last)
1230 (setq state 'last)))
1232 (let ((part (gnus-uu-part-number article)))
1233 (gnus-message 6 "Getting article %d%s..."
1234 article (if (string= part "") "" (concat ", " part))))
1235 (gnus-summary-display-article article)
1237 ;; Push the article to the processing function.
1239 (set-buffer gnus-original-article-buffer)
1240 (let ((buffer-read-only nil))
1242 (set-buffer gnus-summary-buffer)
1244 (funcall process-function
1245 gnus-original-article-buffer state)))))
1247 (gnus-summary-remove-process-mark article)
1249 ;; If this is the beginning of a decoded file, we push it
1251 (when (or (memq 'begin process-state)
1252 (and (or (eq state 'first)
1253 (eq state 'first-and-last))
1254 (memq 'ok process-state)))
1255 (when has-been-begin
1256 ;; If there is a `result-file' here, that means that the
1257 ;; file was unsuccessfully decoded, so we delete it.
1258 (when (and result-file
1259 (file-exists-p result-file)
1260 (not gnus-uu-be-dangerous)
1261 (or (eq gnus-uu-be-dangerous t)
1263 (format "Delete unsuccessfully decoded file %s"
1265 (delete-file result-file)))
1266 (when (memq 'begin process-state)
1267 (setq result-file (car process-state)))
1268 (setq has-been-begin t))
1270 ;; Check whether we have decoded one complete file.
1271 (when (memq 'end process-state)
1272 (setq article-series nil)
1273 (setq has-been-begin nil)
1274 (if (stringp result-file)
1275 (setq files (list result-file))
1276 (setq files result-file))
1277 (setq result-file (car files))
1279 (push (list (cons 'name (pop files))
1280 (cons 'article article))
1282 ;; Allow user-defined functions to be run on this file.
1283 (when gnus-uu-grabbed-file-functions
1284 (let ((funcs gnus-uu-grabbed-file-functions))
1285 (unless (listp funcs)
1286 (setq funcs (list funcs)))
1288 (funcall (pop funcs) result-file))))
1289 (setq result-file nil)
1290 ;; Check whether we have decoded enough articles.
1291 (and limit (= (length result-files) limit)
1292 (setq articles nil)))
1294 ;; If this is the last article to be decoded, and
1295 ;; we still haven't reached the end, then we delete
1296 ;; the partially decoded file.
1297 (and (or (eq state 'last) (eq state 'first-and-last))
1298 (not (memq 'end process-state))
1300 (file-exists-p result-file)
1301 (not gnus-uu-be-dangerous)
1302 (or (eq gnus-uu-be-dangerous t)
1304 (format "Delete incomplete file %s? " result-file)))
1305 (delete-file result-file))
1307 ;; If this was a file of the wrong sort, then
1308 (when (and (or (memq 'wrong-type process-state)
1309 (memq 'error process-state))
1310 gnus-uu-unmark-articles-not-decoded)
1311 (gnus-summary-tick-article article t))
1313 ;; Set the new series state.
1314 (if (and (not has-been-begin)
1316 (or (memq 'end process-state)
1317 (memq 'middle process-state)))
1319 (setq process-state (list 'error))
1320 (gnus-message 2 "No begin part at the beginning")
1322 (setq state 'middle)))
1324 ;; When there are no result-files, then something must be wrong.
1328 ((not has-been-begin)
1329 (gnus-message 2 "Wrong type file"))
1330 ((memq 'error process-state)
1331 (gnus-message 2 "An error occurred during decoding"))
1332 ((not (or (memq 'ok process-state)
1333 (memq 'end process-state)))
1334 (gnus-message 2 "End of articles reached before end of file")))
1335 ;; Make unsuccessfully decoded articles unread.
1336 (when gnus-uu-unmark-articles-not-decoded
1337 (while article-series
1338 (gnus-summary-tick-article (pop article-series) t)))))
1342 (defun gnus-uu-grab-view (file)
1343 "View FILE using the gnus-uu methods."
1344 (let ((action (gnus-uu-get-action file)))
1345 (gnus-execute-command
1346 (if (string-match "%" action)
1347 (format action file)
1348 (concat action " " file))
1349 (eq gnus-view-pseudos 'not-confirm))))
1351 (defun gnus-uu-grab-move (file)
1352 "Move FILE to somewhere."
1353 (when gnus-uu-default-dir
1354 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1355 (file-name-nondirectory file))))
1356 (cond ((fboundp 'make-symbolic-link)
1357 (rename-file file to-file)
1358 (unless (file-exists-p file)
1359 (make-symbolic-link to-file file)))
1361 (copy-file file to-file))))))
1363 (defun gnus-uu-part-number (article)
1364 (let* ((header (gnus-summary-article-header article))
1365 (subject (and header (mail-header-subject header)))
1368 (while (string-match "[0-9]+/[0-9]+\\|[0-9]+[ \t]+of[ \t]+[0-9]+"
1370 (setq part (match-string 0 subject))
1371 (setq subject (substring subject (match-end 0)))))
1373 (while (string-match "\\([0-9]+\\)[^0-9]+\\([0-9]+\\)" subject)
1374 (setq part (match-string 0 subject))
1375 (setq subject (substring subject (match-end 0)))))
1378 (defun gnus-uu-uudecode-sentinel (process event)
1379 (delete-process (get-process process)))
1381 (defun gnus-uu-uustrip-article (process-buffer in-state)
1382 ;; Uudecodes a file asynchronously.
1384 (set-buffer process-buffer)
1385 (let ((state (list 'wrong-type))
1386 process-connection-type case-fold-search buffer-read-only
1388 (goto-char (point-min))
1390 ;; Deal with ^M at the end of the lines.
1391 (when gnus-uu-kill-carriage-return
1393 (while (search-forward "\r" nil t)
1394 (delete-backward-char 1))))
1396 (while (or (re-search-forward gnus-uu-begin-string nil t)
1397 (re-search-forward gnus-uu-body-line nil t))
1398 (setq state (list 'ok))
1399 ;; Ok, we are at the first uucoded line.
1401 (setq start-char (point))
1403 (if (not (looking-at gnus-uu-begin-string))
1404 (setq state (list 'middle))
1405 ;; This is the beginning of a uuencoded article.
1406 ;; We replace certain characters that could make things messy.
1407 (setq gnus-uu-file-name
1408 (let ((nnheader-file-name-translation-alist
1409 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1410 (nnheader-translate-file-chars (match-string 1))))
1411 (replace-match (concat "begin 644 " gnus-uu-file-name) t t)
1413 ;; Remove any non gnus-uu-body-line right after start.
1415 (while (and (not (eobp))
1416 (not (looking-at gnus-uu-body-line)))
1419 ;; If a process is running, we kill it.
1420 (when (and gnus-uu-uudecode-process
1421 (memq (process-status gnus-uu-uudecode-process)
1423 (delete-process gnus-uu-uudecode-process)
1424 (gnus-uu-unmark-list-of-grabbed t))
1426 ;; Start a new uudecoding process.
1427 (let ((cdir default-directory))
1430 (cd gnus-uu-work-dir)
1431 (setq gnus-uu-uudecode-process
1434 (gnus-get-buffer-create gnus-uu-output-buffer-name)
1435 shell-file-name shell-command-switch
1436 (format "cd %s %s uudecode" gnus-uu-work-dir
1437 gnus-shell-command-separator))))
1439 (set-process-sentinel
1440 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1441 (setq state (list 'begin))
1442 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1444 ;; We look for the end of the thing to be decoded.
1445 (if (re-search-forward gnus-uu-end-string nil t)
1447 (goto-char (point-max))
1448 (re-search-backward gnus-uu-body-line nil t))
1452 (when gnus-uu-uudecode-process
1453 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1454 ;; Try to correct mishandled uucode.
1455 (when gnus-uu-correct-stripped-uucode
1456 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1457 (gnus-run-hooks 'gnus-uu-pre-uudecode-hook)
1459 ;; Send the text to the process.
1461 (process-send-region
1462 gnus-uu-uudecode-process start-char (point))
1465 (delete-process gnus-uu-uudecode-process)
1466 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1467 (setq state (list 'wrong-type)))))
1469 (if (memq 'end state)
1471 ;; Send an EOF, just in case.
1473 (process-send-eof gnus-uu-uudecode-process))
1474 (while (memq (process-status gnus-uu-uudecode-process)
1476 (accept-process-output gnus-uu-uudecode-process 1)))
1477 (when (or (not gnus-uu-uudecode-process)
1478 (not (memq (process-status gnus-uu-uudecode-process)
1480 (setq state (list 'wrong-type)))))))
1482 (if (memq 'begin state)
1483 (cons (if (= (length files) 1) (car files) files) state)
1486 ;; This function is used by `gnus-uu-grab-articles' to treat
1487 ;; a shared article.
1488 (defun gnus-uu-unshar-article (process-buffer in-state)
1489 (let ((state (list 'ok))
1492 (set-buffer process-buffer)
1493 (goto-char (point-min))
1494 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1495 (setq state (list 'wrong-type))
1497 (setq start-char (point))
1498 (call-process-region
1499 start-char (point-max) shell-file-name nil
1500 (gnus-get-buffer-create gnus-uu-output-buffer-name) nil
1501 shell-command-switch
1502 (concat "cd " gnus-uu-work-dir " "
1503 gnus-shell-command-separator " sh"))))
1506 ;; Returns the name of what the shar file is going to unpack.
1507 (defun gnus-uu-find-name-in-shar ()
1508 (let ((oldpoint (point))
1510 (goto-char (point-min))
1511 (when (re-search-forward gnus-uu-shar-name-marker nil t)
1512 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1513 (goto-char oldpoint)
1516 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1517 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1518 ;; found, or the name of the command to run if such a rule is found.
1519 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1520 (let ((action-list (copy-sequence file-action-list))
1521 (case-fold-search t)
1526 (and gnus-uu-ignore-files-by-name
1527 (string-match gnus-uu-ignore-files-by-name file-name)))
1529 (and gnus-uu-ignore-files-by-type
1530 (string-match gnus-uu-ignore-files-by-type
1531 (or (gnus-uu-choose-action
1532 file-name gnus-uu-ext-to-mime-list t)
1534 (while (not (or (eq action-list ()) action))
1535 (setq rule (car action-list))
1536 (setq action-list (cdr action-list))
1537 (when (string-match (car rule) file-name)
1538 (setq action (cadr rule)))))
1541 (defun gnus-uu-treat-archive (file-path)
1542 ;; Unpacks an archive. Returns t if unpacking is successful.
1543 (let ((did-unpack t)
1545 (setq action (gnus-uu-choose-action
1546 file-path (append gnus-uu-user-archive-rules
1547 (if gnus-uu-ignore-default-archive-rules
1549 gnus-uu-default-archive-rules))))
1552 (error "No unpackers for the file %s" file-path))
1554 (string-match "/[^/]*$" file-path)
1555 (setq dir (substring file-path 0 (match-beginning 0)))
1557 (when (member action gnus-uu-destructive-archivers)
1558 (copy-file file-path (concat file-path "~") t))
1560 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1563 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1566 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1568 (if (= 0 (call-process shell-file-name nil
1569 (gnus-get-buffer-create gnus-uu-output-buffer-name)
1570 nil shell-command-switch command))
1572 (gnus-message 2 "Error during unpacking of archive")
1573 (setq did-unpack nil))
1575 (when (member action gnus-uu-destructive-archivers)
1576 (rename-file (concat file-path "~") file-path t))
1580 (defun gnus-uu-dir-files (dir)
1581 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1584 (if (file-directory-p (setq file (car dirs)))
1585 (setq files (append files (gnus-uu-dir-files file)))
1587 (setq dirs (cdr dirs)))
1590 (defun gnus-uu-unpack-files (files &optional ignore)
1591 ;; Go through FILES and look for files to unpack.
1592 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1596 (setq file (cdr (assq 'name (car files))))
1597 (when (and (not (member file ignore))
1598 (equal (gnus-uu-get-action (file-name-nondirectory file))
1600 (push file did-unpack)
1601 (unless (gnus-uu-treat-archive file)
1602 (gnus-message 2 "Error during unpacking of %s" file))
1603 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1606 (unless (member (car nfiles) totfiles)
1607 (push (list (cons 'name (car nfiles))
1608 (cons 'original file))
1610 (setq nfiles (cdr nfiles)))
1611 (setq totfiles newfiles)))
1612 (setq files (cdr files)))
1614 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1617 (defun gnus-uu-ls-r (dir)
1618 (let* ((files (gnus-uu-directory-files dir t))
1621 (when (file-directory-p (car files))
1622 (setq ofiles (delete (car files) ofiles))
1623 (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1624 (setq files (cdr files)))
1629 (defun gnus-uu-directory-files (dir &optional full)
1630 (let (files out file)
1631 (setq files (directory-files dir full))
1633 (setq file (car files))
1634 (setq files (cdr files))
1635 (unless (member (file-name-nondirectory file) '("." ".."))
1637 (setq out (nreverse out))
1640 (defun gnus-uu-check-correct-stripped-uucode (start end)
1642 (let (found beg length)
1643 (if (not gnus-uu-correct-stripped-uucode)
1647 (if (re-search-forward " \\|`" end t)
1652 (when (looking-at "\n")
1657 (if (looking-at (concat gnus-uu-begin-string "\\|"
1658 gnus-uu-end-string))
1664 (setq length (- (point) beg)))
1669 (when (not (= length (- (point) beg)))
1670 (insert (make-string (- length (- (point) beg)) ? ))))
1671 (forward-line 1)))))))
1673 (defvar gnus-uu-tmp-alist nil)
1675 (defun gnus-uu-initialize (&optional scan)
1678 (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1679 (if (file-exists-p (cdr entry))
1680 (setq gnus-uu-work-dir (cdr entry))
1681 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1684 (setq gnus-uu-tmp-dir (file-name-as-directory
1685 (expand-file-name gnus-uu-tmp-dir)))
1686 (if (not (file-directory-p gnus-uu-tmp-dir))
1687 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1688 (when (not (file-writable-p gnus-uu-tmp-dir))
1689 (error "Temp directory %s can't be written to"
1692 (setq gnus-uu-work-dir
1693 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1694 (gnus-make-directory gnus-uu-work-dir)
1695 (set-file-modes gnus-uu-work-dir 448)
1696 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1697 (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1698 gnus-uu-tmp-alist))))
1701 ;; Kills the temporary uu buffers, kills any processes, etc.
1702 (defun gnus-uu-clean-up ()
1704 (and gnus-uu-uudecode-process
1705 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1707 (delete-process gnus-uu-uudecode-process))
1708 (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1709 (kill-buffer buf))))
1711 (defun gnus-quote-arg-for-sh-or-csh (arg)
1712 (let ((pos 0) new-pos accum)
1713 ;; *** bug: we don't handle newline characters properly
1714 (while (setq new-pos (string-match "[!`\"$\\& \t{}]" arg pos))
1715 (push (substring arg pos new-pos) accum)
1717 (push (list (aref arg new-pos)) accum)
1718 (setq pos (1+ new-pos)))
1721 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
1723 ;; Inputs an action and a filename and returns a full command, making sure
1724 ;; that the filename will be treated as a single argument when the shell
1725 ;; executes the command.
1726 (defun gnus-uu-command (action file)
1727 (let ((quoted-file (gnus-quote-arg-for-sh-or-csh file)))
1728 (if (string-match "%s" action)
1729 (format action quoted-file)
1730 (concat action " " quoted-file))))
1732 (defun gnus-uu-delete-work-dir (&optional dir)
1733 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1735 (gnus-message 7 "Deleting directory %s..." dir)
1736 (setq dir gnus-uu-work-dir))
1738 (file-exists-p dir))
1739 (let ((files (directory-files dir t nil t))
1741 (while (setq file (pop files))
1742 (unless (member (file-name-nondirectory file) '("." ".."))
1743 (if (file-directory-p file)
1744 (gnus-uu-delete-work-dir file)
1745 (gnus-message 9 "Deleting file %s..." file)
1746 (delete-file file))))
1747 (delete-directory dir)))
1748 (gnus-message 7 ""))
1752 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1753 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1758 ;;; uuencoded posting
1761 ;; Any function that is to be used as and encoding method will take two
1762 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1763 ;; and "spiral.jpg", respectively.) The function should return nil if
1764 ;; the encoding wasn't successful.
1765 (defcustom gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1766 "Function used for encoding binary files.
1767 There are three functions supplied with gnus-uu for encoding files:
1768 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1769 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1770 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1771 uuencode and adds MIME headers."
1772 :group 'gnus-extract-post
1773 :type '(radio (function-item gnus-uu-post-encode-uuencode)
1774 (function-item gnus-uu-post-encode-mime)
1775 (function-item gnus-uu-post-encode-mime-uuencode)
1776 (function :tag "Other")))
1778 (defcustom gnus-uu-post-include-before-composing nil
1779 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1780 If this variable is t, you can either include an encoded file with
1781 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article."
1782 :group 'gnus-extract-post
1785 (defcustom gnus-uu-post-length 990
1786 "Maximum length of an article.
1787 The encoded file will be split into how many articles it takes to
1788 post the entire file."
1789 :group 'gnus-extract-post
1792 (defcustom gnus-uu-post-threaded nil
1793 "Non-nil means that gnus-uu will post the encoded file in a thread.
1794 This may not be smart, as no other decoder I have seen are able to
1795 follow threads when collecting uuencoded articles. (Well, I have seen
1796 one package that does that - gnus-uu, but somehow, I don't think that
1797 counts...) The default is nil."
1798 :group 'gnus-extract-post
1801 (defcustom gnus-uu-post-separate-description t
1802 "Non-nil means that the description will be posted in a separate article.
1803 The first article will typically be numbered (0/x). If this variable
1804 is nil, the description the user enters will be included at the
1805 beginning of the first article, which will be numbered (1/x). Default
1807 :group 'gnus-extract-post
1810 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1811 (defvar gnus-uu-post-message-id nil)
1812 (defvar gnus-uu-post-inserted-file-name nil)
1813 (defvar gnus-uu-winconf-post-news nil)
1815 (defun gnus-uu-post-news ()
1816 "Compose an article and post an encoded file."
1818 (setq gnus-uu-post-inserted-file-name nil)
1819 (setq gnus-uu-winconf-post-news (current-window-configuration))
1821 (gnus-summary-post-news)
1823 (let ((map (make-sparse-keymap)))
1824 (set-keymap-parent map (current-local-map))
1825 (use-local-map map))
1826 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1827 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1828 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1829 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1831 (when gnus-uu-post-include-before-composing
1832 (save-excursion (setq gnus-uu-post-inserted-file-name
1833 (gnus-uu-post-insert-binary)))))
1835 (defun gnus-uu-post-insert-binary-in-article ()
1836 "Inserts an encoded file in the buffer.
1837 The user will be asked for a file name."
1840 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1842 ;; Encodes with uuencode and substitutes all spaces with backticks.
1843 (defun gnus-uu-post-encode-uuencode (path file-name)
1844 (when (gnus-uu-post-encode-file "uuencode" path file-name)
1845 (goto-char (point-min))
1847 (while (re-search-forward " " nil t)
1848 (replace-match "`"))
1851 ;; Encodes with uuencode and adds MIME headers.
1852 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1853 (when (gnus-uu-post-encode-uuencode path file-name)
1854 (gnus-uu-post-make-mime file-name "x-uue")
1857 ;; Encodes with base64 and adds MIME headers
1858 (defun gnus-uu-post-encode-mime (path file-name)
1859 (when (zerop (call-process shell-file-name nil t nil shell-command-switch
1860 (format "%s %s -o %s" "mmencode" path file-name)))
1861 (gnus-uu-post-make-mime file-name "base64")
1864 ;; Adds MIME headers.
1865 (defun gnus-uu-post-make-mime (file-name encoding)
1866 (goto-char (point-min))
1867 (insert (format "Content-Type: %s; name=\"%s\"\n"
1868 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1870 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1872 (set-buffer gnus-message-buffer)
1873 (goto-char (point-min))
1874 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1876 (narrow-to-region 1 (point))
1877 (unless (mail-fetch-field "mime-version")
1879 (insert "MIME-Version: 1.0\n"))
1882 ;; Encodes a file PATH with COMMAND, leaving the result in the
1884 (defun gnus-uu-post-encode-file (command path file-name)
1885 (= 0 (call-process shell-file-name nil t nil shell-command-switch
1886 (format "%s %s %s" command path file-name))))
1888 (defun gnus-uu-post-news-inews ()
1889 "Posts the composed news article and encoded file.
1890 If no file has been included, the user will be asked for a file."
1895 (if gnus-uu-post-inserted-file-name
1896 (setq file-name gnus-uu-post-inserted-file-name)
1897 (setq file-name (gnus-uu-post-insert-binary)))
1899 (gnus-uu-post-encoded file-name gnus-uu-post-threaded))
1900 (setq gnus-uu-post-inserted-file-name nil)
1901 (when gnus-uu-winconf-post-news
1902 (set-window-configuration gnus-uu-winconf-post-news)))
1904 ;; Asks for a file to encode, encodes it and inserts the result in
1905 ;; the current buffer. Returns the file name the user gave.
1906 (defun gnus-uu-post-insert-binary ()
1907 (let ((uuencode-buffer-name "*uuencode buffer*")
1908 file-path uubuf file-name)
1910 (setq file-path (read-file-name
1911 "What file do you want to encode? "))
1912 (when (not (file-exists-p file-path))
1913 (error "%s: No such file" file-path))
1915 (goto-char (point-max))
1916 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1918 ;; #### Unix-specific?
1919 (when (string-match "^~/" file-path)
1920 (setq file-path (concat "$HOME" (substring file-path 1))))
1921 ;; #### Unix-specific?
1922 (if (string-match "/[^/]*$" file-path)
1923 (setq file-name (substring file-path (1+ (match-beginning 0))))
1924 (setq file-name file-path))
1928 (set-buffer (setq uubuf
1929 (gnus-get-buffer-create uuencode-buffer-name)))
1931 (funcall gnus-uu-post-encode-method file-path file-name))
1932 (insert-buffer-substring uubuf)
1933 (error "Encoding unsuccessful"))
1934 (kill-buffer uubuf))
1937 ;; Posts the article and all of the encoded file.
1938 (defun gnus-uu-post-encoded (file-name &optional threaded)
1939 (let ((send-buffer-name "*uuencode send buffer*")
1940 (encoded-buffer-name "*encoded buffer*")
1941 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1942 (separator (concat mail-header-separator "\n\n"))
1943 uubuf length parts header i end beg
1944 beg-line minlen post-buf whole-len beg-binary end-binary)
1946 (setq post-buf (current-buffer))
1948 (goto-char (point-min))
1949 (when (not (re-search-forward
1950 (if gnus-uu-post-separate-description
1951 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1953 (concat "^" (regexp-quote mail-header-separator) "$"))
1955 (error "Internal error: No binary/header separator"))
1958 (setq beg-binary (point))
1959 (setq end-binary (point-max))
1962 (set-buffer (setq uubuf (gnus-get-buffer-create encoded-buffer-name)))
1964 (insert-buffer-substring post-buf beg-binary end-binary)
1965 (goto-char (point-min))
1966 (setq length (count-lines 1 (point-max)))
1967 (setq parts (/ length gnus-uu-post-length))
1968 (unless (< (% length gnus-uu-post-length) 4)
1971 (when gnus-uu-post-separate-description
1973 (delete-region (point) (point-max))
1975 (goto-char (point-min))
1977 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1979 (setq header (buffer-substring 1 (point)))
1981 (goto-char (point-min))
1982 (when gnus-uu-post-separate-description
1983 (when (re-search-forward "^Subject: " nil t)
1985 (insert (format " (0/%d)" parts)))
1988 (setq gnus-uu-post-message-id (message-fetch-field "message-id")))
1993 (while (not (> i parts))
1994 (set-buffer (gnus-get-buffer-create send-buffer-name))
1997 (when (and threaded gnus-uu-post-message-id)
1998 (insert "References: " gnus-uu-post-message-id "\n"))
2001 (- 62 (length (format top-string "" file-name i parts ""))))
2002 (when (> 1 (setq minlen (/ whole-len 2)))
2007 (make-string minlen ?-)
2010 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
2012 (goto-char (point-min))
2013 (when (re-search-forward "^Subject: " nil t)
2015 (insert (format " (%d/%d)" i parts)))
2017 (goto-char (point-max))
2022 (goto-char (point-max))
2023 (forward-line gnus-uu-post-length))
2024 (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
2027 (insert-buffer-substring uubuf beg end)
2028 (insert beg-line "\n")
2031 (goto-char (point-min))
2033 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2036 (when (re-search-forward
2037 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
2043 (let (message-sent-message-via)
2046 (setq gnus-uu-post-message-id
2047 (concat (message-fetch-field "references") " "
2048 (message-fetch-field "message-id"))))))
2050 (gnus-kill-buffer send-buffer-name)
2051 (gnus-kill-buffer encoded-buffer-name)
2053 (when (not gnus-uu-post-separate-description)
2054 (set-buffer-modified-p nil)
2055 (when (fboundp 'bury-buffer)
2060 ;; gnus-uu.el ends here