1 ;;; gnus-uu.el --- extract (uu)encoded files in Gnus
2 ;; Copyright (C) 1985,86,87,93,94,95,96,97 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
34 (defgroup gnus-extract nil
35 "Extracting encoded files."
38 ;; Default viewing action rules
40 (defcustom gnus-uu-default-view-rules
41 '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed s/\r//g")
42 ("\\.pas$" "cat %s | sed s/\r//g")
43 ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
44 ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "xv")
45 ("\\.tga$" "tgatoppm %s | xv -")
46 ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
47 "sox -v .5 %s -t .au -u - > /dev/audio")
48 ("\\.au$" "cat %s > /dev/audio")
49 ("\\.midi?$" "playmidi -f")
51 ("\\.ps$" "ghostview")
53 ("\\.html$" "xmosaic")
54 ("\\.mpe?g$" "mpeg_play")
55 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
56 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
58 "Default actions to be taken when the user asks to view a file.
59 To change the behaviour, you can either edit this variable or set
60 `gnus-uu-user-view-rules' to something useful.
64 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
65 following in your .emacs file:
67 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
69 Both these variables are lists of lists with two string elements. The
70 first string is a regular expression. If the file name matches this
71 regular expression, the command in the second string is executed with
72 the file as an argument.
74 If the command string contains \"%s\", the file name will be inserted
75 at that point in the command string. If there's no \"%s\" in the
76 command string, the file name will be appended to the command string
79 There are several user variables to tailor the behaviour of gnus-uu to
80 your needs. First we have `gnus-uu-user-view-rules', which is the
81 variable gnus-uu first consults when trying to decide how to view a
82 file. If this variable contains no matches, gnus-uu examines the
83 default rule variable provided in this package. If gnus-uu finds no
84 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
87 :type '(repeat (group regexp (string :tag "Command"))))
89 (defcustom gnus-uu-user-view-rules nil
90 "What actions are to be taken to view a file.
91 See the documentation on the `gnus-uu-default-view-rules' variable for
94 :type '(repeat (group regexp (string :tag "Command"))))
96 (defcustom gnus-uu-user-view-rules-end
98 "What actions are to be taken if no rule matched the file name.
99 See the documentation on the `gnus-uu-default-view-rules' variable for
102 :type '(repeat (group regexp (string :tag "Command"))))
104 ;; Default unpacking commands
106 (defcustom gnus-uu-default-archive-rules
107 '(("\\.tar$" "tar xf")
108 ("\\.zip$" "unzip -o")
110 ("\\.arj$" "unarj x")
112 ("\\.\\(lzh\\|lha\\)$" "lha x")
113 ("\\.Z$" "uncompress")
115 ("\\.arc$" "arc -x"))
116 "See `gnus-uu-user-archive-rules'."
118 :type '(repeat (group regexp (string :tag "Command"))))
120 (defvar gnus-uu-destructive-archivers
121 (list "uncompress" "gunzip"))
123 (defcustom gnus-uu-user-archive-rules nil
124 "A list that can be set to override the default archive unpacking commands.
125 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
126 unpack zip files, say the following:
127 (setq gnus-uu-user-archive-rules
128 '((\"\\\\.tar$\" \"untar\")
129 (\"\\\\.zip$\" \"zip -x\")))"
131 :type '(repeat (group regexp (string :tag "Command"))))
133 (defcustom gnus-uu-ignore-files-by-name nil
134 "*A regular expression saying what files should not be viewed based on name.
135 If, for instance, you want gnus-uu to ignore all .au and .wav files,
136 you could say something like
138 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
140 Note that this variable can be used in conjunction with the
141 `gnus-uu-ignore-files-by-type' variable."
143 :type '(choice (const :tag "off" nil)
144 (regexp :format "%v")))
146 (defcustom gnus-uu-ignore-files-by-type nil
147 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
148 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
149 you could say something like
151 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
153 Note that this variable can be used in conjunction with the
154 `gnus-uu-ignore-files-by-name' variable."
156 :type '(choice (const :tag "off" nil)
157 (regexp :format "%v")))
159 ;; Pseudo-MIME support
161 (defconst gnus-uu-ext-to-mime-list
162 '(("\\.gif$" "image/gif")
163 ("\\.jpe?g$" "image/jpeg")
164 ("\\.tiff?$" "image/tiff")
165 ("\\.xwd$" "image/xwd")
166 ("\\.pbm$" "image/pbm")
167 ("\\.pgm$" "image/pgm")
168 ("\\.ppm$" "image/ppm")
169 ("\\.xbm$" "image/xbm")
170 ("\\.pcx$" "image/pcx")
171 ("\\.tga$" "image/tga")
172 ("\\.ps$" "image/postscript")
173 ("\\.fli$" "video/fli")
174 ("\\.wav$" "audio/wav")
175 ("\\.aiff$" "audio/aiff")
176 ("\\.hcom$" "audio/hcom")
177 ("\\.voc$" "audio/voc")
178 ("\\.smp$" "audio/smp")
179 ("\\.mod$" "audio/mod")
180 ("\\.dvi$" "image/dvi")
181 ("\\.mpe?g$" "video/mpeg")
182 ("\\.au$" "audio/basic")
183 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
184 ("\\.\\(c\\|h\\)$" "text/source")
185 ("read.*me" "text/plain")
186 ("\\.html$" "text/html")
187 ("\\.bat$" "text/bat")
188 ("\\.[1-6]$" "text/man")
189 ("\\.flc$" "video/flc")
190 ("\\.rle$" "video/rle")
191 ("\\.pfx$" "video/pfx")
192 ("\\.avi$" "video/avi")
193 ("\\.sme$" "video/sme")
194 ("\\.rpza$" "video/prza")
195 ("\\.dl$" "video/dl")
196 ("\\.qt$" "video/qt")
197 ("\\.rsrc$" "video/rsrc")
198 ("\\..*$" "unknown/unknown")))
200 ;; Various variables users may set
202 (defcustom gnus-uu-tmp-dir "/tmp/"
203 "*Variable saying where gnus-uu is to do its work.
204 Default is \"/tmp/\"."
208 (defcustom gnus-uu-do-not-unpack-archives nil
209 "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
214 (defcustom gnus-uu-ignore-default-view-rules nil
215 "*Non-nil means that gnus-uu will ignore the default viewing rules.
216 Only the user viewing rules will be consulted. Default is nil."
220 (defcustom gnus-uu-grabbed-file-functions nil
221 "Functions run on each file after successful decoding.
222 They will be called with the name of the file as the argument.
223 Likely functions you can use in this list are `gnus-uu-grab-view'
224 and `gnus-uu-grab-move'."
226 :options '(gnus-uu-grab-view gnus-uu-grab-move)
229 (defcustom gnus-uu-ignore-default-archive-rules nil
230 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
231 Only the user unpacking commands will be consulted. Default is nil."
235 (defcustom gnus-uu-kill-carriage-return t
236 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
241 (defcustom gnus-uu-view-with-metamail nil
242 "*Non-nil means that files will be viewed with metamail.
243 The gnus-uu viewing functions will be ignored and gnus-uu will try
244 to guess at a content-type based on file name suffixes. Default
249 (defcustom gnus-uu-unmark-articles-not-decoded nil
250 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
255 (defcustom gnus-uu-correct-stripped-uucode nil
256 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
261 (defcustom gnus-uu-save-in-digest nil
262 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
263 If this variable is nil, gnus-uu will just save everything in a
264 file without any embellishments. The digesting almost conforms to RFC1153 -
265 no easy way to specify any meaningful volume and issue numbers were found,
266 so I simply dropped them."
270 (defcustom gnus-uu-digest-headers
271 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
272 "^Summary:" "^References:")
273 "List of regexps to match headers included in digested messages.
274 The headers will be included in the sequence they are matched."
276 :type '(repeat regexp))
278 (defcustom gnus-uu-save-separate-articles nil
279 "*Non-nil means that gnus-uu will save articles in separate files."
283 (defcustom gnus-uu-be-dangerous 'ask
284 "*Specifies what to do if unusual situations arise during decoding.
285 If nil, be as conservative as possible. If t, ignore things that
286 didn't work, and overwrite existing files. Otherwise, ask each time."
288 :type '(choice (const :tag "conservative" nil)
289 (const :tag "ask" ask)
290 (const :tag "liberal" t)))
292 ;; Internal variables
294 (defvar gnus-uu-saved-article-name nil)
296 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
297 (defconst gnus-uu-end-string "^end[ \t]*$")
299 (defconst gnus-uu-body-line "^M")
301 (while (> (setq i (1- i)) 0)
302 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
303 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
305 ;"^M.............................................................?$"
307 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
309 (defvar gnus-uu-shar-file-name nil)
310 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
312 (defconst gnus-uu-postscript-begin-string "^%!PS-")
313 (defconst gnus-uu-postscript-end-string "^%%EOF$")
315 (defvar gnus-uu-file-name nil)
316 (defconst gnus-uu-uudecode-process nil)
317 (defvar gnus-uu-binhex-article-name nil)
319 (defvar gnus-uu-work-dir nil)
321 (defconst gnus-uu-output-buffer-name " *Gnus UU Output*")
323 (defvar gnus-uu-default-dir gnus-article-save-directory)
324 (defvar gnus-uu-digest-from-subject nil)
328 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
329 "p" gnus-summary-mark-as-processable
330 "u" gnus-summary-unmark-as-processable
331 "U" gnus-summary-unmark-all-processable
332 "v" gnus-uu-mark-over
333 "s" gnus-uu-mark-series
334 "r" gnus-uu-mark-region
335 "R" gnus-uu-mark-by-regexp
336 "t" gnus-uu-mark-thread
337 "T" gnus-uu-unmark-thread
339 "b" gnus-uu-mark-buffer
340 "S" gnus-uu-mark-sparse
341 "k" gnus-summary-kill-process-mark
342 "y" gnus-summary-yank-process-mark
343 "w" gnus-summary-save-process-mark
344 "i" gnus-uu-invert-processable)
346 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
347 ;;"x" gnus-uu-extract-any
348 ;;"m" gnus-uu-extract-mime
349 "u" gnus-uu-decode-uu
350 "U" gnus-uu-decode-uu-and-save
351 "s" gnus-uu-decode-unshar
352 "S" gnus-uu-decode-unshar-and-save
353 "o" gnus-uu-decode-save
354 "O" gnus-uu-decode-save
355 "b" gnus-uu-decode-binhex
356 "B" gnus-uu-decode-binhex
357 "p" gnus-uu-decode-postscript
358 "P" gnus-uu-decode-postscript-and-save)
361 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
362 "u" gnus-uu-decode-uu-view
363 "U" gnus-uu-decode-uu-and-save-view
364 "s" gnus-uu-decode-unshar-view
365 "S" gnus-uu-decode-unshar-and-save-view
366 "o" gnus-uu-decode-save-view
367 "O" gnus-uu-decode-save-view
368 "b" gnus-uu-decode-binhex-view
369 "B" gnus-uu-decode-binhex-view
370 "p" gnus-uu-decode-postscript-view
371 "P" gnus-uu-decode-postscript-and-save-view)
376 (defun gnus-uu-decode-uu (&optional n)
377 "Uudecodes the current article."
379 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
381 (defun gnus-uu-decode-uu-and-save (n dir)
382 "Decodes and saves the resulting file."
384 (list current-prefix-arg
385 (file-name-as-directory
386 (read-file-name "Uudecode and save in dir: "
388 gnus-uu-default-dir t))))
389 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
391 (defun gnus-uu-decode-unshar (&optional n)
392 "Unshars the current article."
394 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
396 (defun gnus-uu-decode-unshar-and-save (n dir)
397 "Unshars and saves the current article."
399 (list current-prefix-arg
400 (file-name-as-directory
401 (read-file-name "Unshar and save in dir: "
403 gnus-uu-default-dir t))))
404 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
406 (defun gnus-uu-decode-save (n file)
407 "Saves the current article."
409 (list current-prefix-arg
411 (if gnus-uu-save-separate-articles
412 "Save articles is dir: "
413 "Save articles in file: ")
415 gnus-uu-default-dir)))
416 (setq gnus-uu-saved-article-name file)
417 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
419 (defun gnus-uu-decode-binhex (n dir)
420 "Unbinhexes the current article."
422 (list current-prefix-arg
423 (file-name-as-directory
424 (read-file-name "Unbinhex and save in dir: "
426 gnus-uu-default-dir))))
427 (setq gnus-uu-binhex-article-name
428 (make-temp-name (concat gnus-uu-work-dir "binhex")))
429 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
431 (defun gnus-uu-decode-uu-view (&optional n)
432 "Uudecodes and views the current article."
434 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
435 (gnus-uu-decode-uu n)))
437 (defun gnus-uu-decode-uu-and-save-view (n dir)
438 "Decodes, views and saves the resulting file."
440 (list current-prefix-arg
441 (read-file-name "Uudecode, view and save in dir: "
443 gnus-uu-default-dir t)))
444 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
445 (gnus-uu-decode-uu-and-save n dir)))
447 (defun gnus-uu-decode-unshar-view (&optional n)
448 "Unshars and views the current article."
450 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
451 (gnus-uu-decode-unshar n)))
453 (defun gnus-uu-decode-unshar-and-save-view (n dir)
454 "Unshars and saves the current article."
456 (list current-prefix-arg
457 (read-file-name "Unshar, view and save in dir: "
459 gnus-uu-default-dir t)))
460 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
461 (gnus-uu-decode-unshar-and-save n dir)))
463 (defun gnus-uu-decode-save-view (n file)
464 "Saves and views the current article."
466 (list current-prefix-arg
467 (read-file-name (if gnus-uu-save-separate-articles
468 "Save articles is dir: "
469 "Save articles in file: ")
470 gnus-uu-default-dir gnus-uu-default-dir)))
471 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
472 (gnus-uu-decode-save n file)))
474 (defun gnus-uu-decode-binhex-view (n file)
475 "Unbinhexes and views the current article."
477 (list current-prefix-arg
478 (read-file-name "Unbinhex, view and save in dir: "
479 gnus-uu-default-dir gnus-uu-default-dir)))
480 (setq gnus-uu-binhex-article-name
481 (make-temp-name (concat gnus-uu-work-dir "binhex")))
482 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
483 (gnus-uu-decode-binhex n file)))
486 ;; Digest and forward articles
488 (defun gnus-uu-digest-mail-forward (&optional n post)
489 "Digests and forwards all articles in this series."
491 (let ((gnus-uu-save-in-digest t)
492 (file (make-temp-name (nnheader-concat gnus-uu-tmp-dir "forward")))
493 buf subject from newsgroups)
494 (setq gnus-uu-digest-from-subject nil)
495 (gnus-uu-decode-save n file)
496 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
497 (gnus-add-current-to-buffer-list)
499 (delete-other-windows)
501 (let ((fs gnus-uu-digest-from-subject))
504 subject (gnus-simplify-subject-fuzzy (cdar fs))
506 (while (and fs (or from subject))
508 (unless (string= from (caar fs))
511 (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
516 (setq subject "Digested Articles"))
519 (if (gnus-news-group-p gnus-newsgroup-name)
522 (goto-char (point-min))
523 (when (re-search-forward "^Subject: ")
524 (delete-region (point) (gnus-point-at-eol))
526 (goto-char (point-min))
527 (when (re-search-forward "^From: ")
528 (delete-region (point) (gnus-point-at-eol))
530 (message-forward post)
533 (setq gnus-uu-digest-from-subject nil)))
535 (defun gnus-uu-digest-post-forward (&optional n)
536 "Digest and forward to a newsgroup."
538 (gnus-uu-digest-mail-forward n t))
542 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
543 "Ask for a regular expression and set the process mark on all articles that match."
544 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
545 (gnus-set-global-variables)
546 (let ((articles (gnus-uu-find-articles-matching regexp)))
549 (gnus-summary-remove-process-mark (pop articles))
550 (gnus-summary-set-process-mark (pop articles))))
552 (gnus-summary-position-point))
554 (defun gnus-uu-unmark-by-regexp (regexp &optional unmark)
555 "Ask for a regular expression and remove the process mark on all articles that match."
556 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
557 (gnus-uu-mark-by-regexp regexp t))
559 (defun gnus-uu-mark-series ()
560 "Mark the current series with the process mark."
562 (gnus-set-global-variables)
563 (let ((articles (gnus-uu-find-articles-matching)))
565 (gnus-summary-set-process-mark (car articles))
566 (setq articles (cdr articles)))
568 (gnus-summary-position-point))
570 (defun gnus-uu-mark-region (beg end &optional unmark)
571 "Set the process mark on all articles between point and mark."
573 (gnus-set-global-variables)
576 (while (< (point) end)
578 (gnus-summary-remove-process-mark (gnus-summary-article-number))
579 (gnus-summary-set-process-mark (gnus-summary-article-number)))
581 (gnus-summary-position-point))
583 (defun gnus-uu-unmark-region (beg end)
584 "Remove the process mark from all articles between point and mark."
586 (gnus-uu-mark-region beg end t))
588 (defun gnus-uu-mark-buffer ()
589 "Set the process mark on all articles in the buffer."
591 (gnus-uu-mark-region (point-min) (point-max)))
593 (defun gnus-uu-unmark-buffer ()
594 "Remove the process mark on all articles in the buffer."
596 (gnus-uu-mark-region (point-min) (point-max) t))
598 (defun gnus-uu-mark-thread ()
599 "Marks all articles downwards in this thread."
601 (gnus-set-global-variables)
602 (let ((level (gnus-summary-thread-level)))
603 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
604 (zerop (gnus-summary-next-subject 1))
605 (> (gnus-summary-thread-level) level))))
606 (gnus-summary-position-point))
608 (defun gnus-uu-unmark-thread ()
609 "Unmarks all articles downwards in this thread."
611 (gnus-set-global-variables)
612 (let ((level (gnus-summary-thread-level)))
613 (while (and (gnus-summary-remove-process-mark
614 (gnus-summary-article-number))
615 (zerop (gnus-summary-next-subject 1))
616 (> (gnus-summary-thread-level) level))))
617 (gnus-summary-position-point))
619 (defun gnus-uu-invert-processable ()
620 "Invert the list of process-marked articles."
621 (let ((data gnus-newsgroup-data)
625 (if (memq (setq number (gnus-data-number (pop data)))
626 gnus-newsgroup-processable)
627 (gnus-summary-remove-process-mark number)
628 (gnus-summary-set-process-mark number)))))
629 (gnus-summary-position-point))
631 (defun gnus-uu-mark-over (&optional score)
632 "Mark all articles with a score over SCORE (the prefix.)"
634 (let ((score (gnus-score-default score))
635 (data gnus-newsgroup-data))
638 (when (> (or (cdr (assq (gnus-data-number (car data))
639 gnus-newsgroup-scored))
640 gnus-summary-default-score 0)
642 (gnus-summary-set-process-mark (caar data)))
643 (setq data (cdr data))))
644 (gnus-summary-position-point)))
646 (defun gnus-uu-mark-sparse ()
647 "Mark all series that have some articles marked."
649 (gnus-set-global-variables)
650 (let ((marked (nreverse gnus-newsgroup-processable))
651 subject articles total headers)
653 (error "No articles marked with the process mark"))
654 (setq gnus-newsgroup-processable nil)
657 (and (vectorp (setq headers
658 (gnus-summary-article-header (car marked))))
659 (setq subject (mail-header-subject headers)
660 articles (gnus-uu-find-articles-matching
661 (gnus-uu-reginize-string subject))
662 total (nconc total articles)))
664 (gnus-summary-set-process-mark (car articles))
665 (setcdr marked (delq (car articles) (cdr marked)))
666 (setq articles (cdr articles)))
667 (setq marked (cdr marked)))
668 (setq gnus-newsgroup-processable (nreverse total)))
669 (gnus-summary-position-point)))
671 (defun gnus-uu-mark-all ()
672 "Mark all articles in \"series\" order."
674 (gnus-set-global-variables)
675 (setq gnus-newsgroup-processable nil)
677 (let ((data gnus-newsgroup-data)
680 (when (and (not (memq (setq number (gnus-data-number (car data)))
681 gnus-newsgroup-processable))
682 (vectorp (gnus-data-header (car data))))
683 (gnus-summary-goto-subject number)
684 (gnus-uu-mark-series))
685 (setq data (cdr data)))))
686 (gnus-summary-position-point))
688 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
690 (defun gnus-uu-decode-postscript (&optional n)
691 "Gets postscript of the current article."
693 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
695 (defun gnus-uu-decode-postscript-view (&optional n)
696 "Gets and views the current article."
698 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
699 (gnus-uu-decode-postscript n)))
701 (defun gnus-uu-decode-postscript-and-save (n dir)
702 "Extracts postscript and saves the current article."
704 (list current-prefix-arg
705 (file-name-as-directory
706 (read-file-name "Save in dir: "
708 gnus-uu-default-dir t))))
709 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
712 (defun gnus-uu-decode-postscript-and-save-view (n dir)
713 "Decodes, views and saves the resulting file."
715 (list current-prefix-arg
716 (read-file-name "Where do you want to save the file(s)? "
718 gnus-uu-default-dir t)))
719 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
720 (gnus-uu-decode-postscript-and-save n dir)))
723 ;; Internal functions.
725 (defun gnus-uu-decode-with-method (method n &optional save not-insert
727 (gnus-uu-initialize scan)
729 (setq gnus-uu-default-dir save))
730 ;; Create the directory we save to.
731 (when (and scan cdir save
732 (not (file-exists-p save)))
733 (make-directory save t))
734 (let ((articles (gnus-uu-get-list-of-articles n))
736 (setq files (gnus-uu-grab-articles articles method t))
737 (let ((gnus-current-article (car articles)))
739 (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
741 (gnus-uu-save-files files save))
742 (when (eq gnus-uu-do-not-unpack-archives nil)
743 (setq files (gnus-uu-unpack-files files)))
744 (setq files (nreverse (gnus-uu-get-actions files)))
745 (or not-insert (not gnus-insert-pseudo-articles)
746 (gnus-summary-insert-pseudos files save))))
748 (defun gnus-uu-scan-directory (dir &optional rec)
749 "Return a list of all files under DIR."
750 (let ((files (directory-files dir t))
752 (while (setq file (pop files))
753 (unless (member (file-name-nondirectory file) '("." ".."))
754 (push (list (cons 'name file)
755 (cons 'article gnus-current-article))
757 (when (file-directory-p file)
758 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
763 (defun gnus-uu-save-files (files dir)
765 (let ((len (length files))
766 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
767 to-file file fromdir)
768 (while (setq file (cdr (assq 'name (pop files))))
769 (when (file-exists-p file)
770 (string-match reg file)
771 (setq fromdir (substring file (match-end 0)))
772 (if (file-directory-p file)
773 (gnus-make-directory (concat dir fromdir))
774 (setq to-file (concat dir fromdir))
775 (when (or (not (file-exists-p to-file))
776 (eq gnus-uu-be-dangerous t)
777 (and gnus-uu-be-dangerous
778 (gnus-y-or-n-p (format "%s exists; overwrite? "
780 (copy-file file to-file t t)))))
781 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
783 ;; Functions for saving and possibly digesting articles without
786 ;; Function called by gnus-uu-grab-articles to treat each article.
787 (defun gnus-uu-save-article (buffer in-state)
789 (gnus-uu-save-separate-articles
793 (concat gnus-uu-saved-article-name gnus-current-article))
794 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
795 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
797 ((eq in-state 'last) (list 'end))
798 (t (list 'middle)))))
799 ((not gnus-uu-save-in-digest)
802 (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
803 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
804 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
806 ((eq in-state 'last) (list 'end))
807 (t (list 'middle)))))
809 (let ((header (gnus-summary-article-header)))
810 (push (cons (mail-header-from header)
811 (mail-header-subject header))
812 gnus-uu-digest-from-subject))
813 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
814 (delim (concat "^" (make-string 30 ?-) "$"))
815 beg subj headers headline sorthead body end-string state)
816 (if (or (eq in-state 'first)
817 (eq in-state 'first-and-last))
819 (setq state (list 'begin))
820 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
823 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
826 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
827 (current-time-string) name name))))
828 (when (not (eq in-state 'end))
829 (setq state (list 'middle))))
831 (set-buffer (get-buffer "*gnus-uu-body*"))
832 (goto-char (setq beg (point-max)))
836 (let (buffer-read-only)
837 (gnus-set-text-properties (point-min) (point-max) nil)
838 ;; These two are necessary for XEmacs 19.12 fascism.
839 (put-text-property (point-min) (point-max) 'invisible nil)
840 (put-text-property (point-min) (point-max) 'intangible nil))
841 (goto-char (point-min))
842 (re-search-forward "\n\n")
843 ;; Quote all 30-dash lines.
845 (while (re-search-forward delim nil t)
849 (setq body (buffer-substring (1- (point)) (point-max)))
850 (narrow-to-region (point-min) (point))
851 (if (not (setq headers gnus-uu-digest-headers))
852 (setq sorthead (buffer-substring (point-min) (point-max)))
854 (setq headline (car headers))
855 (setq headers (cdr headers))
856 (goto-char (point-min))
857 (while (re-search-forward headline nil t)
862 (or (and (re-search-forward "^[^ \t]" nil t)
864 (progn (forward-line 1) (point)))))))))
866 (insert sorthead) (goto-char (point-max))
867 (insert body) (goto-char (point-max))
868 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
870 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
871 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
873 (set-buffer (get-buffer "*gnus-uu-pre*"))
874 (insert (format " %s\n" subj)))))
875 (when (or (eq in-state 'last)
876 (eq in-state 'first-and-last))
878 (set-buffer (get-buffer "*gnus-uu-pre*"))
879 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
880 (gnus-write-buffer gnus-uu-saved-article-name))
882 (set-buffer (get-buffer "*gnus-uu-body*"))
883 (goto-char (point-max))
885 (concat (setq end-string (format "End of %s Digest" name))
887 (insert (concat (make-string (length end-string) ?*) "\n"))
889 (point-min) (point-max) gnus-uu-saved-article-name t))
890 (kill-buffer (get-buffer "*gnus-uu-pre*"))
891 (kill-buffer (get-buffer "*gnus-uu-body*"))
893 (if (memq 'begin state)
894 (cons gnus-uu-saved-article-name state)
897 ;; Binhex treatment - not very advanced.
899 (defconst gnus-uu-binhex-body-line
900 "^[^:]...............................................................$")
901 (defconst gnus-uu-binhex-begin-line
902 "^:...............................................................$")
903 (defconst gnus-uu-binhex-end-line
906 (defun gnus-uu-binhex-article (buffer in-state)
907 (let (state start-char)
911 (goto-char (point-min))
912 (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
913 (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
914 (setq state (list 'wrong-type))))
916 (if (memq 'wrong-type state)
919 (setq start-char (point))
920 (if (looking-at gnus-uu-binhex-begin-line)
922 (setq state (list 'begin))
923 (write-region 1 1 gnus-uu-binhex-article-name))
924 (setq state (list 'middle)))
925 (goto-char (point-max))
926 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
927 gnus-uu-binhex-end-line)
929 (when (looking-at gnus-uu-binhex-end-line)
930 (setq state (if (memq 'begin state)
935 (when (file-exists-p gnus-uu-binhex-article-name)
936 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
937 (if (memq 'begin state)
938 (cons gnus-uu-binhex-article-name state)
943 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
944 (let ((state (list 'ok))
945 start-char end-char file-name)
947 (set-buffer process-buffer)
948 (goto-char (point-min))
949 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
950 (setq state (list 'wrong-type))
952 (setq start-char (point))
953 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
954 (setq state (list 'wrong-type))
955 (setq end-char (point))
956 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
957 (insert-buffer-substring process-buffer start-char end-char)
958 (setq file-name (concat gnus-uu-work-dir
959 (cdr gnus-article-current) ".ps"))
960 (write-region (point-min) (point-max) file-name)
961 (setq state (list file-name 'begin 'end)))))
967 (defun gnus-uu-get-actions (files)
971 (setq name (cdr (assq 'name (car files))))
973 (setq action (gnus-uu-get-action name))
974 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
975 (cons 'action "file")
976 (cons 'action action))
977 (cons 'execute (gnus-uu-command
980 (setq files (cdr files)))
983 (defun gnus-uu-get-action (file-name)
986 (gnus-uu-choose-action
989 gnus-uu-user-view-rules
990 (if gnus-uu-ignore-default-view-rules
992 gnus-uu-default-view-rules)
993 gnus-uu-user-view-rules-end)))
994 (when (and (not (string= (or action "") "gnus-uu-archive"))
995 gnus-uu-view-with-metamail)
997 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
998 (setq action (format "metamail -d -b -c \"%s\"" action))))
1002 ;; Functions for treating subjects and collecting series.
1004 (defun gnus-uu-reginize-string (string)
1005 ;; Takes a string and puts a \ in front of every special character;
1006 ;; ignores any leading "version numbers" thingies that they use in
1007 ;; the comp.binaries groups, and either replaces anything that looks
1008 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
1009 ;; like that, replaces the last two numbers with "[0-9]+". This, in
1010 ;; my experience, should get most postings of a series.
1012 (vernum "v[0-9]+[a-z][0-9]+:")
1015 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1016 (buffer-disable-undo (current-buffer))
1018 (insert (regexp-quote string))
1021 (setq case-fold-search nil)
1022 (goto-char (point-min))
1023 (when (looking-at vernum)
1024 (replace-match vernum t t)
1025 (setq beg (length vernum)))
1028 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
1029 (replace-match " [0-9]+/[0-9]+")
1032 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
1033 (replace-match "[0-9]+ of [0-9]+")
1036 (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1038 (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1041 (while (re-search-forward "[ \t]+" nil t)
1042 (replace-match "[ \t]*" t t))
1044 (buffer-substring 1 (point-max)))))
1046 (defun gnus-uu-get-list-of-articles (n)
1047 ;; If N is non-nil, the article numbers of the N next articles
1048 ;; will be returned.
1049 ;; If any articles have been marked as processable, they will be
1051 ;; Failing that, articles that have subjects that are part of the
1052 ;; same "series" as the current will be returned.
1056 (setq n (prefix-numeric-value n))
1057 (let ((backward (< n 0))
1061 (push (gnus-summary-article-number)
1063 (gnus-summary-search-forward nil nil backward))
1065 (nreverse articles)))
1066 (gnus-newsgroup-processable
1067 (reverse gnus-newsgroup-processable))
1069 (gnus-uu-find-articles-matching)))))
1071 (defun gnus-uu-string< (l1 l2)
1072 (string< (car l1) (car l2)))
1074 (defun gnus-uu-find-articles-matching
1075 (&optional subject only-unread do-not-translate)
1076 ;; Finds all articles that matches the regexp SUBJECT. If it is
1077 ;; nil, the current article name will be used. If ONLY-UNREAD is
1078 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1079 ;; non-nil, article names are not equalized before sorting.
1080 (let ((subject (or subject
1081 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1086 ;; Collect all subjects matching subject.
1087 (let ((case-fold-search t)
1088 (data gnus-newsgroup-data)
1092 (and (not (gnus-data-pseudo-p d))
1093 (or (not only-unread)
1094 (= (setq mark (gnus-data-mark d))
1096 (= mark gnus-ticked-mark)
1097 (= mark gnus-dormant-mark))
1098 (setq subj (mail-header-subject (gnus-data-header d)))
1099 (string-match subject subj)
1100 (push (cons subj (gnus-data-number d))
1101 list-of-subjects))))
1103 ;; Expand numbers, sort, and return the list of article
1105 (mapcar (lambda (sub) (cdr sub))
1106 (sort (gnus-uu-expand-numbers
1108 (not do-not-translate))
1109 'gnus-uu-string<))))))
1111 (defun gnus-uu-expand-numbers (string-list &optional translate)
1112 ;; Takes a list of strings and "expands" all numbers in all the
1113 ;; strings. That is, this function makes all numbers equal length by
1114 ;; prepending lots of zeroes before each number. This is to ease later
1115 ;; sorting to find out what sequence the articles are supposed to be
1116 ;; decoded in. Returns the list of expanded strings.
1117 (let ((out-list string-list)
1120 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1121 (buffer-disable-undo (current-buffer))
1124 (insert (caar string-list))
1125 ;; Translate multiple spaces to one space.
1126 (goto-char (point-min))
1127 (while (re-search-forward "[ \t]+" nil t)
1128 (replace-match " "))
1129 ;; Translate all characters to "a".
1130 (goto-char (point-min))
1132 (while (re-search-forward "[A-Za-z]" nil t)
1133 (replace-match "a" t t)))
1135 (goto-char (point-min))
1136 (while (re-search-forward "[0-9]+" nil t)
1139 (string-to-int (buffer-substring
1140 (match-beginning 0) (match-end 0))))))
1141 (setq string (buffer-substring 1 (point-max)))
1142 (setcar (car string-list) string)
1143 (setq string-list (cdr string-list))))
1147 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1148 ;; function. It takes a list of articles to be grabbed and a function
1149 ;; to apply to each article.
1151 ;; The function to be called should take two parameters. The first
1152 ;; parameter is the article buffer. The function should leave the
1153 ;; result, if any, in this buffer. Most treatment functions will just
1154 ;; generate files...
1156 ;; The second parameter is the state of the list of articles, and can
1157 ;; have four values: `first', `middle', `last' and `first-and-last'.
1159 ;; The function should return a list. The list may contain the
1160 ;; following symbols:
1161 ;; `error' if an error occurred
1162 ;; `begin' if the beginning of an encoded file has been received
1163 ;; If the list returned contains a `begin', the first element of
1164 ;; the list *must* be a string with the file name of the decoded
1166 ;; `end' if the end of an encoded file has been received
1167 ;; `middle' if the article was a body part of an encoded file
1168 ;; `wrong-type' if the article was not a part of an encoded file
1169 ;; `ok', which can be used everything is ok
1171 (defvar gnus-uu-has-been-grabbed nil)
1173 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1175 (if (not (and gnus-uu-has-been-grabbed
1176 gnus-uu-unmark-articles-not-decoded))
1178 (when dont-unmark-last-article
1179 (setq art (car gnus-uu-has-been-grabbed))
1180 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1181 (while gnus-uu-has-been-grabbed
1182 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1183 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1184 (when dont-unmark-last-article
1185 (setq gnus-uu-has-been-grabbed (list art))))))
1187 ;; This function takes a list of articles and a function to apply to
1188 ;; each article grabbed.
1190 ;; This function returns a list of files decoded if the grabbing and
1191 ;; the process-function has been successful and nil otherwise.
1192 (defun gnus-uu-grab-articles (articles process-function
1193 &optional sloppy limit no-errors)
1194 (let ((state 'first)
1195 (gnus-asynchronous nil)
1196 has-been-begin article result-file result-files process-state
1197 gnus-summary-display-article-function
1198 gnus-article-display-hook gnus-article-prepare-hook
1199 article-series files)
1201 (while (and articles
1202 (not (memq 'error process-state))
1204 (not (memq 'end process-state))))
1206 (setq article (pop articles))
1207 (push article article-series)
1210 (if (eq state 'first)
1211 (setq state 'first-and-last)
1212 (setq state 'last)))
1214 (let ((part (gnus-uu-part-number article)))
1215 (gnus-message 6 "Getting article %d%s..."
1216 article (if (string= part "") "" (concat ", " part))))
1217 (gnus-summary-display-article article)
1219 ;; Push the article to the processing function.
1221 (set-buffer gnus-original-article-buffer)
1222 (let ((buffer-read-only nil))
1224 (set-buffer gnus-summary-buffer)
1226 (funcall process-function
1227 gnus-original-article-buffer state)))))
1229 (gnus-summary-remove-process-mark article)
1231 ;; If this is the beginning of a decoded file, we push it
1233 (when (or (memq 'begin process-state)
1234 (and (or (eq state 'first)
1235 (eq state 'first-and-last))
1236 (memq 'ok process-state)))
1237 (when has-been-begin
1238 ;; If there is a `result-file' here, that means that the
1239 ;; file was unsuccessfully decoded, so we delete it.
1240 (when (and result-file
1241 (file-exists-p result-file)
1242 (not gnus-uu-be-dangerous)
1243 (or (eq gnus-uu-be-dangerous t)
1245 (format "Delete unsuccessfully decoded file %s"
1247 (delete-file result-file)))
1248 (when (memq 'begin process-state)
1249 (setq result-file (car process-state)))
1250 (setq has-been-begin t))
1252 ;; Check whether we have decoded one complete file.
1253 (when (memq 'end process-state)
1254 (setq article-series nil)
1255 (setq has-been-begin nil)
1256 (if (stringp result-file)
1257 (setq files (list result-file))
1258 (setq files result-file))
1259 (setq result-file (car files))
1261 (push (list (cons 'name (pop files))
1262 (cons 'article article))
1264 ;; Allow user-defined functions to be run on this file.
1265 (when gnus-uu-grabbed-file-functions
1266 (let ((funcs gnus-uu-grabbed-file-functions))
1267 (unless (listp funcs)
1268 (setq funcs (list funcs)))
1270 (funcall (pop funcs) result-file))))
1271 (setq result-file nil)
1272 ;; Check whether we have decoded enough articles.
1273 (and limit (= (length result-files) limit)
1274 (setq articles nil)))
1276 ;; If this is the last article to be decoded, and
1277 ;; we still haven't reached the end, then we delete
1278 ;; the partially decoded file.
1279 (and (or (eq state 'last) (eq state 'first-and-last))
1280 (not (memq 'end process-state))
1282 (file-exists-p result-file)
1283 (not gnus-uu-be-dangerous)
1284 (or (eq gnus-uu-be-dangerous t)
1285 (gnus-y-or-n-p (format "Delete incomplete file %s? " result-file)))
1286 (delete-file result-file))
1288 ;; If this was a file of the wrong sort, then
1289 (when (and (or (memq 'wrong-type process-state)
1290 (memq 'error process-state))
1291 gnus-uu-unmark-articles-not-decoded)
1292 (gnus-summary-tick-article article t))
1294 ;; Set the new series state.
1295 (if (and (not has-been-begin)
1297 (or (memq 'end process-state)
1298 (memq 'middle process-state)))
1300 (setq process-state (list 'error))
1301 (gnus-message 2 "No begin part at the beginning")
1303 (setq state 'middle)))
1305 ;; When there are no result-files, then something must be wrong.
1309 ((not has-been-begin)
1310 (gnus-message 2 "Wrong type file"))
1311 ((memq 'error process-state)
1312 (gnus-message 2 "An error occurred during decoding"))
1313 ((not (or (memq 'ok process-state)
1314 (memq 'end process-state)))
1315 (gnus-message 2 "End of articles reached before end of file")))
1316 ;; Make unsuccessfully decoded articles unread.
1317 (when gnus-uu-unmark-articles-not-decoded
1318 (while article-series
1319 (gnus-summary-tick-article (pop article-series) t))))
1323 (defun gnus-uu-grab-view (file)
1324 "View FILE using the gnus-uu methods."
1325 (let ((action (gnus-uu-get-action file)))
1326 (gnus-execute-command
1327 (if (string-match "%" action)
1328 (format action file)
1329 (concat action " " file))
1330 (eq gnus-view-pseudos 'not-confirm))))
1332 (defun gnus-uu-grab-move (file)
1333 "Move FILE to somewhere."
1334 (when gnus-uu-default-dir
1335 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1336 (file-name-nondirectory file))))
1337 (rename-file file to-file)
1338 (unless (file-exists-p file)
1339 (make-symbolic-link to-file file)))))
1341 (defun gnus-uu-part-number (article)
1342 (let* ((header (gnus-summary-article-header article))
1343 (subject (and header (mail-header-subject header))))
1345 (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+" subject))
1346 (match-string 0 subject)
1349 (defun gnus-uu-uudecode-sentinel (process event)
1350 (delete-process (get-process process)))
1352 (defun gnus-uu-uustrip-article (process-buffer in-state)
1353 ;; Uudecodes a file asynchronously.
1355 (set-buffer process-buffer)
1356 (let ((state (list 'wrong-type))
1357 process-connection-type case-fold-search buffer-read-only
1359 (goto-char (point-min))
1361 ;; Deal with ^M at the end of the lines.
1362 (when gnus-uu-kill-carriage-return
1364 (while (search-forward "\r" nil t)
1365 (delete-backward-char 1))))
1367 (while (or (re-search-forward gnus-uu-begin-string nil t)
1368 (re-search-forward gnus-uu-body-line nil t))
1369 (setq state (list 'ok))
1370 ;; Ok, we are at the first uucoded line.
1372 (setq start-char (point))
1374 (if (not (looking-at gnus-uu-begin-string))
1375 (setq state (list 'middle))
1376 ;; This is the beginning of an uuencoded article.
1377 ;; We replace certain characters that could make things messy.
1378 (setq gnus-uu-file-name
1379 (let ((nnheader-file-name-translation-alist
1380 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1381 (nnheader-translate-file-chars (match-string 1))))
1382 (replace-match (concat "begin 644 " gnus-uu-file-name) t t)
1384 ;; Remove any non gnus-uu-body-line right after start.
1386 (while (and (not (eobp))
1387 (not (looking-at gnus-uu-body-line)))
1390 ;; If a process is running, we kill it.
1391 (when (and gnus-uu-uudecode-process
1392 (memq (process-status gnus-uu-uudecode-process)
1394 (delete-process gnus-uu-uudecode-process)
1395 (gnus-uu-unmark-list-of-grabbed t))
1397 ;; Start a new uudecoding process.
1398 (let ((cdir default-directory))
1401 (cd gnus-uu-work-dir)
1402 (setq gnus-uu-uudecode-process
1405 (get-buffer-create gnus-uu-output-buffer-name)
1406 shell-file-name shell-command-switch
1407 (format "cd %s %s uudecode" gnus-uu-work-dir
1408 gnus-shell-command-separator))))
1410 (set-process-sentinel
1411 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1412 (setq state (list 'begin))
1413 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1415 ;; We look for the end of the thing to be decoded.
1416 (if (re-search-forward gnus-uu-end-string nil t)
1418 (goto-char (point-max))
1419 (re-search-backward gnus-uu-body-line nil t))
1423 (when gnus-uu-uudecode-process
1424 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1425 ;; Try to correct mishandled uucode.
1426 (when gnus-uu-correct-stripped-uucode
1427 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1429 ;; Send the text to the process.
1431 (process-send-region
1432 gnus-uu-uudecode-process start-char (point))
1435 (delete-process gnus-uu-uudecode-process)
1436 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1437 (setq state (list 'wrong-type)))))
1439 (if (memq 'end state)
1441 ;; Send an EOF, just in case.
1443 (process-send-eof gnus-uu-uudecode-process))
1444 (while (memq (process-status gnus-uu-uudecode-process)
1446 (accept-process-output gnus-uu-uudecode-process 1)))
1447 (when (or (not gnus-uu-uudecode-process)
1448 (not (memq (process-status gnus-uu-uudecode-process)
1450 (setq state (list 'wrong-type)))))))
1452 (if (memq 'begin state)
1453 (cons (if (= (length files) 1) (car files) files) state)
1456 ;; This function is used by `gnus-uu-grab-articles' to treat
1457 ;; a shared article.
1458 (defun gnus-uu-unshar-article (process-buffer in-state)
1459 (let ((state (list 'ok))
1462 (set-buffer process-buffer)
1463 (goto-char (point-min))
1464 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1465 (setq state (list 'wrong-type))
1467 (setq start-char (point))
1468 (call-process-region
1469 start-char (point-max) shell-file-name nil
1470 (get-buffer-create gnus-uu-output-buffer-name) nil
1471 shell-command-switch
1472 (concat "cd " gnus-uu-work-dir " "
1473 gnus-shell-command-separator " sh"))))
1476 ;; Returns the name of what the shar file is going to unpack.
1477 (defun gnus-uu-find-name-in-shar ()
1478 (let ((oldpoint (point))
1480 (goto-char (point-min))
1481 (when (re-search-forward gnus-uu-shar-name-marker nil t)
1482 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1483 (goto-char oldpoint)
1486 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1487 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1488 ;; found, or the name of the command to run if such a rule is found.
1489 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1490 (let ((action-list (copy-sequence file-action-list))
1491 (case-fold-search t)
1496 (and gnus-uu-ignore-files-by-name
1497 (string-match gnus-uu-ignore-files-by-name file-name)))
1499 (and gnus-uu-ignore-files-by-type
1500 (string-match gnus-uu-ignore-files-by-type
1501 (or (gnus-uu-choose-action
1502 file-name gnus-uu-ext-to-mime-list t)
1504 (while (not (or (eq action-list ()) action))
1505 (setq rule (car action-list))
1506 (setq action-list (cdr action-list))
1507 (when (string-match (car rule) file-name)
1508 (setq action (cadr rule)))))
1511 (defun gnus-uu-treat-archive (file-path)
1512 ;; Unpacks an archive. Returns t if unpacking is successful.
1513 (let ((did-unpack t)
1515 (setq action (gnus-uu-choose-action
1516 file-path (append gnus-uu-user-archive-rules
1517 (if gnus-uu-ignore-default-archive-rules
1519 gnus-uu-default-archive-rules))))
1522 (error "No unpackers for the file %s" file-path))
1524 (string-match "/[^/]*$" file-path)
1525 (setq dir (substring file-path 0 (match-beginning 0)))
1527 (when (member action gnus-uu-destructive-archivers)
1528 (copy-file file-path (concat file-path "~") t))
1530 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1533 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1536 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1538 (if (= 0 (call-process shell-file-name nil
1539 (get-buffer-create gnus-uu-output-buffer-name)
1540 nil shell-command-switch command))
1542 (gnus-message 2 "Error during unpacking of archive")
1543 (setq did-unpack nil))
1545 (when (member action gnus-uu-destructive-archivers)
1546 (rename-file (concat file-path "~") file-path t))
1550 (defun gnus-uu-dir-files (dir)
1551 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1554 (if (file-directory-p (setq file (car dirs)))
1555 (setq files (append files (gnus-uu-dir-files file)))
1557 (setq dirs (cdr dirs)))
1560 (defun gnus-uu-unpack-files (files &optional ignore)
1561 ;; Go through FILES and look for files to unpack.
1562 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1566 (setq file (cdr (assq 'name (car files))))
1567 (when (and (not (member file ignore))
1568 (equal (gnus-uu-get-action (file-name-nondirectory file))
1570 (push file did-unpack)
1571 (unless (gnus-uu-treat-archive file)
1572 (gnus-message 2 "Error during unpacking of %s" file))
1573 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1576 (unless (member (car nfiles) totfiles)
1577 (push (list (cons 'name (car nfiles))
1578 (cons 'original file))
1580 (setq nfiles (cdr nfiles)))
1581 (setq totfiles newfiles)))
1582 (setq files (cdr files)))
1584 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1587 (defun gnus-uu-ls-r (dir)
1588 (let* ((files (gnus-uu-directory-files dir t))
1591 (when (file-directory-p (car files))
1592 (setq ofiles (delete (car files) ofiles))
1593 (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1594 (setq files (cdr files)))
1599 (defun gnus-uu-directory-files (dir &optional full)
1600 (let (files out file)
1601 (setq files (directory-files dir full))
1603 (setq file (car files))
1604 (setq files (cdr files))
1605 (unless (member (file-name-nondirectory file) '("." ".."))
1607 (setq out (nreverse out))
1610 (defun gnus-uu-check-correct-stripped-uucode (start end)
1612 (let (found beg length)
1613 (if (not gnus-uu-correct-stripped-uucode)
1617 (if (re-search-forward " \\|`" end t)
1622 (when (looking-at "\n")
1627 (if (looking-at (concat gnus-uu-begin-string "\\|"
1628 gnus-uu-end-string))
1634 (setq length (- (point) beg)))
1639 (when (not (= length (- (point) beg)))
1640 (insert (make-string (- length (- (point) beg)) ? ))))
1641 (forward-line 1)))))))
1643 (defvar gnus-uu-tmp-alist nil)
1645 (defun gnus-uu-initialize (&optional scan)
1648 (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1649 (if (file-exists-p (cdr entry))
1650 (setq gnus-uu-work-dir (cdr entry))
1651 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1654 (setq gnus-uu-tmp-dir (file-name-as-directory
1655 (expand-file-name gnus-uu-tmp-dir)))
1656 (if (not (file-directory-p gnus-uu-tmp-dir))
1657 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1658 (when (not (file-writable-p gnus-uu-tmp-dir))
1659 (error "Temp directory %s can't be written to"
1662 (setq gnus-uu-work-dir
1663 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1664 (gnus-make-directory gnus-uu-work-dir)
1665 (set-file-modes gnus-uu-work-dir 448)
1666 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1667 (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1668 gnus-uu-tmp-alist))))
1671 ;; Kills the temporary uu buffers, kills any processes, etc.
1672 (defun gnus-uu-clean-up ()
1674 (and gnus-uu-uudecode-process
1675 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1677 (delete-process gnus-uu-uudecode-process))
1678 (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1679 (kill-buffer buf))))
1681 (defun gnus-quote-arg-for-sh-or-csh (arg)
1682 (let ((pos 0) new-pos accum)
1683 ;; *** bug: we don't handle newline characters properly
1684 (while (setq new-pos (string-match "[!`\"$\\& \t]" arg pos))
1685 (push (substring arg pos new-pos) accum)
1687 (push (list (aref arg new-pos)) accum)
1688 (setq pos (1+ new-pos)))
1691 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
1693 ;; Inputs an action and a filename and returns a full command, making sure
1694 ;; that the filename will be treated as a single argument when the shell
1695 ;; executes the command.
1696 (defun gnus-uu-command (action file)
1697 (let ((quoted-file (gnus-quote-arg-for-sh-or-csh file)))
1698 (if (string-match "%s" action)
1699 (format action quoted-file)
1700 (concat action " " quoted-file))))
1702 (defun gnus-uu-delete-work-dir (&optional dir)
1703 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1705 (gnus-message 7 "Deleting directory %s..." dir)
1706 (setq dir gnus-uu-work-dir))
1708 (file-exists-p dir))
1709 (let ((files (directory-files dir t nil t))
1711 (while (setq file (pop files))
1712 (unless (member (file-name-nondirectory file) '("." ".."))
1713 (if (file-directory-p file)
1714 (gnus-uu-delete-work-dir file)
1715 (gnus-message 9 "Deleting file %s..." file)
1716 (delete-file file))))
1717 (delete-directory dir)))
1718 (gnus-message 7 ""))
1722 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1723 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1728 ;;; uuencoded posting
1731 ;; Any function that is to be used as and encoding method will take two
1732 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1733 ;; and "spiral.jpg", respectively.) The function should return nil if
1734 ;; the encoding wasn't successful.
1735 (defcustom gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1736 "Function used for encoding binary files.
1737 There are three functions supplied with gnus-uu for encoding files:
1738 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1739 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1740 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1741 uuencode and adds MIME headers."
1742 :group 'gnus-extract
1743 :type '(radio (function-item gnus-uu-post-encode-uuencode)
1744 (function-item gnus-uu-post-encode-mime)
1745 (function-item gnus-uu-post-encode-mime-uuencode)
1746 (function :tag "Other")))
1748 (defcustom gnus-uu-post-include-before-composing nil
1749 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1750 If this variable is t, you can either include an encoded file with
1751 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article."
1752 :group 'gnus-extract
1755 (defcustom gnus-uu-post-length 990
1756 "Maximum length of an article.
1757 The encoded file will be split into how many articles it takes to
1758 post the entire file."
1759 :group 'gnus-extract
1762 (defcustom gnus-uu-post-threaded nil
1763 "Non-nil means that gnus-uu will post the encoded file in a thread.
1764 This may not be smart, as no other decoder I have seen are able to
1765 follow threads when collecting uuencoded articles. (Well, I have seen
1766 one package that does that - gnus-uu, but somehow, I don't think that
1767 counts...) Default is nil."
1768 :group 'gnus-extract
1771 (defcustom gnus-uu-post-separate-description t
1772 "Non-nil means that the description will be posted in a separate article.
1773 The first article will typically be numbered (0/x). If this variable
1774 is nil, the description the user enters will be included at the
1775 beginning of the first article, which will be numbered (1/x). Default
1777 :group 'gnus-extract
1780 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1781 (defvar gnus-uu-post-message-id nil)
1782 (defvar gnus-uu-post-inserted-file-name nil)
1783 (defvar gnus-uu-winconf-post-news nil)
1785 (defun gnus-uu-post-news ()
1786 "Compose an article and post an encoded file."
1788 (setq gnus-uu-post-inserted-file-name nil)
1789 (setq gnus-uu-winconf-post-news (current-window-configuration))
1791 (gnus-summary-post-news)
1793 (use-local-map (copy-keymap (current-local-map)))
1794 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1795 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1796 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1797 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1799 (when gnus-uu-post-include-before-composing
1800 (save-excursion (setq gnus-uu-post-inserted-file-name
1801 (gnus-uu-post-insert-binary)))))
1803 (defun gnus-uu-post-insert-binary-in-article ()
1804 "Inserts an encoded file in the buffer.
1805 The user will be asked for a file name."
1808 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1810 ;; Encodes with uuencode and substitutes all spaces with backticks.
1811 (defun gnus-uu-post-encode-uuencode (path file-name)
1812 (when (gnus-uu-post-encode-file "uuencode" path file-name)
1813 (goto-char (point-min))
1815 (while (re-search-forward " " nil t)
1816 (replace-match "`"))
1819 ;; Encodes with uuencode and adds MIME headers.
1820 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1821 (when (gnus-uu-post-encode-uuencode path file-name)
1822 (gnus-uu-post-make-mime file-name "x-uue")
1825 ;; Encodes with base64 and adds MIME headers
1826 (defun gnus-uu-post-encode-mime (path file-name)
1827 (when (gnus-uu-post-encode-file "mmencode" path file-name)
1828 (gnus-uu-post-make-mime file-name "base64")
1831 ;; Adds MIME headers.
1832 (defun gnus-uu-post-make-mime (file-name encoding)
1833 (goto-char (point-min))
1834 (insert (format "Content-Type: %s; name=\"%s\"\n"
1835 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1837 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1839 (set-buffer gnus-message-buffer)
1840 (goto-char (point-min))
1841 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1843 (narrow-to-region 1 (point))
1844 (unless (mail-fetch-field "mime-version")
1846 (insert "MIME-Version: 1.0\n"))
1849 ;; Encodes a file PATH with COMMAND, leaving the result in the
1851 (defun gnus-uu-post-encode-file (command path file-name)
1852 (= 0 (call-process shell-file-name nil t nil shell-command-switch
1853 (format "%s %s %s" command path file-name))))
1855 (defun gnus-uu-post-news-inews ()
1856 "Posts the composed news article and encoded file.
1857 If no file has been included, the user will be asked for a file."
1862 (if gnus-uu-post-inserted-file-name
1863 (setq file-name gnus-uu-post-inserted-file-name)
1864 (setq file-name (gnus-uu-post-insert-binary)))
1866 (if gnus-uu-post-threaded
1867 (let ((message-required-news-headers
1868 (if (memq 'Message-ID message-required-news-headers)
1869 message-required-news-headers
1870 (cons 'Message-ID message-required-news-headers)))
1871 gnus-inews-article-hook)
1873 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1874 gnus-inews-article-hook
1875 (list gnus-inews-article-hook)))
1879 (goto-char (point-min))
1880 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1881 (setq gnus-uu-post-message-id
1883 (match-beginning 1) (match-end 1)))
1884 (setq gnus-uu-post-message-id nil))))
1885 gnus-inews-article-hook)
1886 (gnus-uu-post-encoded file-name t))
1887 (gnus-uu-post-encoded file-name nil)))
1888 (setq gnus-uu-post-inserted-file-name nil)
1889 (when gnus-uu-winconf-post-news
1890 (set-window-configuration gnus-uu-winconf-post-news)))
1892 ;; Asks for a file to encode, encodes it and inserts the result in
1893 ;; the current buffer. Returns the file name the user gave.
1894 (defun gnus-uu-post-insert-binary ()
1895 (let ((uuencode-buffer-name "*uuencode buffer*")
1896 file-path uubuf file-name)
1898 (setq file-path (read-file-name
1899 "What file do you want to encode? "))
1900 (when (not (file-exists-p file-path))
1901 (error "%s: No such file" file-path))
1903 (goto-char (point-max))
1904 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1906 (when (string-match "^~/" file-path)
1907 (setq file-path (concat "$HOME" (substring file-path 1))))
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 (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 buf 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 (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 (when (not (< (% length gnus-uu-post-length) 4))
1955 (setq parts (1+ parts))))
1957 (when gnus-uu-post-separate-description
1959 (kill-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 (if (not gnus-uu-post-separate-description)
1970 (when (and (not threaded) (re-search-forward "^Subject: " nil t))
1972 (insert (format " (0/%d)" parts)))
1978 (while (not (> i parts))
1979 (set-buffer (get-buffer-create send-buffer-name))
1982 (when (and threaded gnus-uu-post-message-id)
1983 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1986 (- 62 (length (format top-string "" file-name i parts ""))))
1987 (when (> 1 (setq minlen (/ whole-len 2)))
1992 (make-string minlen ?-)
1995 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1997 (goto-char (point-min))
1998 (if (not (re-search-forward "^Subject: " nil t))
2003 (insert (format " (%d/%d)" i parts)))
2004 (when (or (and (= i 2) gnus-uu-post-separate-description)
2005 (and (= i 1) (not gnus-uu-post-separate-description)))
2006 (replace-match "Subject: Re: "))))
2008 (goto-char (point-max))
2013 (goto-char (point-max))
2014 (forward-line gnus-uu-post-length))
2015 (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
2018 (insert-buffer-substring uubuf beg end)
2023 (goto-char (point-min))
2025 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2028 (when (re-search-forward
2029 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
2035 (let (message-sent-message-via)
2038 (when (setq buf (get-buffer send-buffer-name))
2040 (when (setq buf (get-buffer encoded-buffer-name))
2043 (when (not gnus-uu-post-separate-description)
2044 (set-buffer-modified-p nil)
2045 (when (fboundp 'bury-buffer)
2050 ;; gnus-uu.el ends here