1 ;;; gnus-uu.el --- extract (uu)encoded files in Gnus
2 ;; Copyright (C) 1985,86,87,93,94,95,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
34 ;; Default viewing action rules
36 (defvar gnus-uu-default-view-rules
37 '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed s/\r//g")
38 ("\\.pas$" "cat %s | sed s/\r//g")
39 ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
40 ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "xv")
41 ("\\.tga$" "tgatoppm %s | xv -")
42 ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
43 "sox -v .5 %s -t .au -u - > /dev/audio")
44 ("\\.au$" "cat %s > /dev/audio")
45 ("\\.midi?$" "playmidi -f")
47 ("\\.ps$" "ghostview")
49 ("\\.html$" "xmosaic")
50 ("\\.mpe?g$" "mpeg_play")
51 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
52 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
54 "*Default actions to be taken when the user asks to view a file.
55 To change the behaviour, you can either edit this variable or set
56 `gnus-uu-user-view-rules' to something useful.
60 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
61 following in your .emacs file:
63 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
65 Both these variables are lists of lists with two string elements. The
66 first string is a regular expression. If the file name matches this
67 regular expression, the command in the second string is executed with
68 the file as an argument.
70 If the command string contains \"%s\", the file name will be inserted
71 at that point in the command string. If there's no \"%s\" in the
72 command string, the file name will be appended to the command string
75 There are several user variables to tailor the behaviour of gnus-uu to
76 your needs. First we have `gnus-uu-user-view-rules', which is the
77 variable gnus-uu first consults when trying to decide how to view a
78 file. If this variable contains no matches, gnus-uu examines the
79 default rule variable provided in this package. If gnus-uu finds no
80 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
83 (defvar gnus-uu-user-view-rules nil
84 "*Variable detailing what actions are to be taken to view a file.
85 See the documentation on the `gnus-uu-default-view-rules' variable for
88 (defvar gnus-uu-user-view-rules-end
90 "*Variable saying what actions are to be taken if no rule matched the file name.
91 See the documentation on the `gnus-uu-default-view-rules' variable for
94 ;; Default unpacking commands
96 (defvar gnus-uu-default-archive-rules
97 '(("\\.tar$" "tar xf")
98 ("\\.zip$" "unzip -o")
100 ("\\.arj$" "unarj x")
102 ("\\.\\(lzh\\|lha\\)$" "lha x")
103 ("\\.Z$" "uncompress")
105 ("\\.arc$" "arc -x")))
107 (defvar gnus-uu-destructive-archivers
108 (list "uncompress" "gunzip"))
110 (defvar gnus-uu-user-archive-rules nil
111 "*A list that can be set to override the default archive unpacking commands.
112 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
113 unpack zip files, say the following:
114 (setq gnus-uu-user-archive-rules
115 '((\"\\\\.tar$\" \"untar\")
116 (\"\\\\.zip$\" \"zip -x\")))")
118 (defvar gnus-uu-ignore-files-by-name nil
119 "*A regular expression saying what files should not be viewed based on name.
120 If, for instance, you want gnus-uu to ignore all .au and .wav files,
121 you could say something like
123 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
125 Note that this variable can be used in conjunction with the
126 `gnus-uu-ignore-files-by-type' variable.")
128 (defvar gnus-uu-ignore-files-by-type nil
129 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
130 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
131 you could say something like
133 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
135 Note that this variable can be used in conjunction with the
136 `gnus-uu-ignore-files-by-name' variable.")
138 ;; Pseudo-MIME support
140 (defconst gnus-uu-ext-to-mime-list
141 '(("\\.gif$" "image/gif")
142 ("\\.jpe?g$" "image/jpeg")
143 ("\\.tiff?$" "image/tiff")
144 ("\\.xwd$" "image/xwd")
145 ("\\.pbm$" "image/pbm")
146 ("\\.pgm$" "image/pgm")
147 ("\\.ppm$" "image/ppm")
148 ("\\.xbm$" "image/xbm")
149 ("\\.pcx$" "image/pcx")
150 ("\\.tga$" "image/tga")
151 ("\\.ps$" "image/postscript")
152 ("\\.fli$" "video/fli")
153 ("\\.wav$" "audio/wav")
154 ("\\.aiff$" "audio/aiff")
155 ("\\.hcom$" "audio/hcom")
156 ("\\.voc$" "audio/voc")
157 ("\\.smp$" "audio/smp")
158 ("\\.mod$" "audio/mod")
159 ("\\.dvi$" "image/dvi")
160 ("\\.mpe?g$" "video/mpeg")
161 ("\\.au$" "audio/basic")
162 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
163 ("\\.\\(c\\|h\\)$" "text/source")
164 ("read.*me" "text/plain")
165 ("\\.html$" "text/html")
166 ("\\.bat$" "text/bat")
167 ("\\.[1-6]$" "text/man")
168 ("\\.flc$" "video/flc")
169 ("\\.rle$" "video/rle")
170 ("\\.pfx$" "video/pfx")
171 ("\\.avi$" "video/avi")
172 ("\\.sme$" "video/sme")
173 ("\\.rpza$" "video/prza")
174 ("\\.dl$" "video/dl")
175 ("\\.qt$" "video/qt")
176 ("\\.rsrc$" "video/rsrc")
177 ("\\..*$" "unknown/unknown")))
179 ;; Various variables users may set
181 (defvar gnus-uu-tmp-dir "/tmp/"
182 "*Variable saying where gnus-uu is to do its work.
183 Default is \"/tmp/\".")
185 (defvar gnus-uu-do-not-unpack-archives nil
186 "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
189 (defvar gnus-uu-ignore-default-view-rules nil
190 "*Non-nil means that gnus-uu will ignore the default viewing rules.
191 Only the user viewing rules will be consulted. Default is nil.")
193 (defvar gnus-uu-grabbed-file-functions nil
194 "*Functions run on each file after successful decoding.
195 They will be called with the name of the file as the argument.
196 Likely functions you can use in this list are `gnus-uu-grab-view'
197 and `gnus-uu-grab-move'.")
199 (defvar gnus-uu-ignore-default-archive-rules nil
200 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
201 Only the user unpacking commands will be consulted. Default is nil.")
203 (defvar gnus-uu-kill-carriage-return t
204 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
207 (defvar gnus-uu-view-with-metamail nil
208 "*Non-nil means that files will be viewed with metamail.
209 The gnus-uu viewing functions will be ignored and gnus-uu will try
210 to guess at a content-type based on file name suffixes. Default
213 (defvar gnus-uu-unmark-articles-not-decoded nil
214 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
217 (defvar gnus-uu-correct-stripped-uucode nil
218 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
221 (defvar gnus-uu-save-in-digest nil
222 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
223 If this variable is nil, gnus-uu will just save everything in a
224 file without any embellishments. The digesting almost conforms to RFC1153 -
225 no easy way to specify any meaningful volume and issue numbers were found,
226 so I simply dropped them.")
228 (defvar gnus-uu-digest-headers
229 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
230 "^Summary:" "^References:")
231 "*List of regexps to match headers included in digested messages.
232 The headers will be included in the sequence they are matched.")
234 (defvar gnus-uu-save-separate-articles nil
235 "*Non-nil means that gnus-uu will save articles in separate files.")
237 ;; Internal variables
239 (defvar gnus-uu-saved-article-name nil)
241 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
242 (defconst gnus-uu-end-string "^end[ \t]*$")
244 (defconst gnus-uu-body-line "^M")
246 (while (> (setq i (1- i)) 0)
247 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
248 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
250 ;"^M.............................................................?$"
252 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
254 (defvar gnus-uu-shar-file-name nil)
255 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
257 (defconst gnus-uu-postscript-begin-string "^%!PS-")
258 (defconst gnus-uu-postscript-end-string "^%%EOF$")
260 (defvar gnus-uu-file-name nil)
261 (defconst gnus-uu-uudecode-process nil)
262 (defvar gnus-uu-binhex-article-name nil)
264 (defvar gnus-uu-work-dir nil)
266 (defconst gnus-uu-output-buffer-name " *Gnus UU Output*")
268 (defvar gnus-uu-default-dir gnus-article-save-directory)
269 (defvar gnus-uu-digest-from-subject nil)
273 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
274 "p" gnus-summary-mark-as-processable
275 "u" gnus-summary-unmark-as-processable
276 "U" gnus-summary-unmark-all-processable
277 "v" gnus-uu-mark-over
278 "s" gnus-uu-mark-series
279 "r" gnus-uu-mark-region
280 "R" gnus-uu-mark-by-regexp
281 "t" gnus-uu-mark-thread
282 "T" gnus-uu-unmark-thread
284 "b" gnus-uu-mark-buffer
285 "S" gnus-uu-mark-sparse
286 "k" gnus-summary-kill-process-mark
287 "y" gnus-summary-yank-process-mark
288 "w" gnus-summary-save-process-mark
289 "i" gnus-uu-invert-processable)
291 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
292 ;;"x" gnus-uu-extract-any
293 ;;"m" gnus-uu-extract-mime
294 "u" gnus-uu-decode-uu
295 "U" gnus-uu-decode-uu-and-save
296 "s" gnus-uu-decode-unshar
297 "S" gnus-uu-decode-unshar-and-save
298 "o" gnus-uu-decode-save
299 "O" gnus-uu-decode-save
300 "b" gnus-uu-decode-binhex
301 "B" gnus-uu-decode-binhex
302 "p" gnus-uu-decode-postscript
303 "P" gnus-uu-decode-postscript-and-save)
306 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
307 "u" gnus-uu-decode-uu-view
308 "U" gnus-uu-decode-uu-and-save-view
309 "s" gnus-uu-decode-unshar-view
310 "S" gnus-uu-decode-unshar-and-save-view
311 "o" gnus-uu-decode-save-view
312 "O" gnus-uu-decode-save-view
313 "b" gnus-uu-decode-binhex-view
314 "B" gnus-uu-decode-binhex-view
315 "p" gnus-uu-decode-postscript-view
316 "P" gnus-uu-decode-postscript-and-save-view)
321 (defun gnus-uu-decode-uu (&optional n)
322 "Uudecodes the current article."
324 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
326 (defun gnus-uu-decode-uu-and-save (n dir)
327 "Decodes and saves the resulting file."
329 (list current-prefix-arg
330 (file-name-as-directory
331 (read-file-name "Uudecode and save in dir: "
333 gnus-uu-default-dir t))))
334 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
336 (defun gnus-uu-decode-unshar (&optional n)
337 "Unshars the current article."
339 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
341 (defun gnus-uu-decode-unshar-and-save (n dir)
342 "Unshars and saves the current article."
344 (list current-prefix-arg
345 (file-name-as-directory
346 (read-file-name "Unshar and save in dir: "
348 gnus-uu-default-dir t))))
349 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
351 (defun gnus-uu-decode-save (n file)
352 "Saves the current article."
354 (list current-prefix-arg
356 (if gnus-uu-save-separate-articles
357 "Save articles is dir: "
358 "Save articles in file: ")
360 gnus-uu-default-dir)))
361 (setq gnus-uu-saved-article-name file)
362 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
364 (defun gnus-uu-decode-binhex (n dir)
365 "Unbinhexes the current article."
367 (list current-prefix-arg
368 (file-name-as-directory
369 (read-file-name "Unbinhex and save in dir: "
371 gnus-uu-default-dir))))
372 (setq gnus-uu-binhex-article-name
373 (make-temp-name (concat gnus-uu-work-dir "binhex")))
374 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
376 (defun gnus-uu-decode-uu-view (&optional n)
377 "Uudecodes and views the current article."
379 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
380 (gnus-uu-decode-uu n)))
382 (defun gnus-uu-decode-uu-and-save-view (n dir)
383 "Decodes, views and saves the resulting file."
385 (list current-prefix-arg
386 (read-file-name "Uudecode, view and save in dir: "
388 gnus-uu-default-dir t)))
389 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
390 (gnus-uu-decode-uu-and-save n dir)))
392 (defun gnus-uu-decode-unshar-view (&optional n)
393 "Unshars and views the current article."
395 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
396 (gnus-uu-decode-unshar n)))
398 (defun gnus-uu-decode-unshar-and-save-view (n dir)
399 "Unshars and saves the current article."
401 (list current-prefix-arg
402 (read-file-name "Unshar, view and save in dir: "
404 gnus-uu-default-dir t)))
405 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
406 (gnus-uu-decode-unshar-and-save n dir)))
408 (defun gnus-uu-decode-save-view (n file)
409 "Saves and views the current article."
411 (list current-prefix-arg
412 (read-file-name (if gnus-uu-save-separate-articles
413 "Save articles is dir: "
414 "Save articles in file: ")
415 gnus-uu-default-dir gnus-uu-default-dir)))
416 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
417 (gnus-uu-decode-save n file)))
419 (defun gnus-uu-decode-binhex-view (n file)
420 "Unbinhexes and views the current article."
422 (list current-prefix-arg
423 (read-file-name "Unbinhex, view and save in dir: "
424 gnus-uu-default-dir gnus-uu-default-dir)))
425 (setq gnus-uu-binhex-article-name
426 (make-temp-name (concat gnus-uu-work-dir "binhex")))
427 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
428 (gnus-uu-decode-binhex n file)))
431 ;; Digest and forward articles
433 (defun gnus-uu-digest-mail-forward (&optional n post)
434 "Digests and forwards all articles in this series."
436 (let ((gnus-uu-save-in-digest t)
437 (file (make-temp-name (concat gnus-uu-tmp-dir "forward")))
438 buf subject from newsgroups)
439 (setq gnus-uu-digest-from-subject nil)
440 (gnus-uu-decode-save n file)
441 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
442 (gnus-add-current-to-buffer-list)
444 (delete-other-windows)
446 (let ((fs gnus-uu-digest-from-subject))
449 subject (gnus-simplify-subject-fuzzy (cdar fs))
451 (while (and fs (or from subject))
453 (or (string= from (caar fs))
456 (or (string= (gnus-simplify-subject-fuzzy (cdar fs))
461 (setq subject "Digested Articles"))
464 (if (gnus-news-group-p gnus-newsgroup-name)
467 (goto-char (point-min))
468 (when (re-search-forward "^Subject: ")
469 (delete-region (point) (gnus-point-at-eol))
471 (goto-char (point-min))
472 (when (re-search-forward "^From: ")
473 (delete-region (point) (gnus-point-at-eol))
475 (message-forward post)
478 (setq gnus-uu-digest-from-subject nil)))
480 (defun gnus-uu-digest-post-forward (&optional n)
481 "Digest and forward to a newsgroup."
483 (gnus-uu-digest-mail-forward n t))
487 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
488 "Ask for a regular expression and set the process mark on all articles that match."
489 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
490 (gnus-set-global-variables)
491 (let ((articles (gnus-uu-find-articles-matching regexp)))
494 (gnus-summary-remove-process-mark (pop articles))
495 (gnus-summary-set-process-mark (pop articles))))
497 (gnus-summary-position-point))
499 (defun gnus-uu-unmark-by-regexp (regexp &optional unmark)
500 "Ask for a regular expression and remove the process mark on all articles that match."
501 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
502 (gnus-uu-mark-by-regexp regexp t))
504 (defun gnus-uu-mark-series ()
505 "Mark the current series with the process mark."
507 (gnus-set-global-variables)
508 (let ((articles (gnus-uu-find-articles-matching)))
510 (gnus-summary-set-process-mark (car articles))
511 (setq articles (cdr articles)))
513 (gnus-summary-position-point))
515 (defun gnus-uu-mark-region (beg end &optional unmark)
516 "Set the process mark on all articles between point and mark."
518 (gnus-set-global-variables)
521 (while (< (point) end)
523 (gnus-summary-remove-process-mark (gnus-summary-article-number))
524 (gnus-summary-set-process-mark (gnus-summary-article-number)))
526 (gnus-summary-position-point))
528 (defun gnus-uu-unmark-region (beg end)
529 "Remove the process mark from all articles between point and mark."
531 (gnus-uu-mark-region beg end t))
533 (defun gnus-uu-mark-buffer ()
534 "Set the process mark on all articles in the buffer."
536 (gnus-uu-mark-region (point-min) (point-max)))
538 (defun gnus-uu-unmark-buffer ()
539 "Remove the process mark on all articles in the buffer."
541 (gnus-uu-mark-region (point-min) (point-max) t))
543 (defun gnus-uu-mark-thread ()
544 "Marks all articles downwards in this thread."
546 (gnus-set-global-variables)
547 (let ((level (gnus-summary-thread-level)))
548 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
549 (zerop (gnus-summary-next-subject 1))
550 (> (gnus-summary-thread-level) level))))
551 (gnus-summary-position-point))
553 (defun gnus-uu-unmark-thread ()
554 "Unmarks all articles downwards in this thread."
556 (gnus-set-global-variables)
557 (let ((level (gnus-summary-thread-level)))
558 (while (and (gnus-summary-remove-process-mark
559 (gnus-summary-article-number))
560 (zerop (gnus-summary-next-subject 1))
561 (> (gnus-summary-thread-level) level))))
562 (gnus-summary-position-point))
564 (defun gnus-uu-invert-processable ()
565 "Invert the list of process-marked articles."
566 (let ((data gnus-newsgroup-data)
570 (if (memq (setq number (gnus-data-number (pop data)))
571 gnus-newsgroup-processable)
572 (gnus-summary-remove-process-mark number)
573 (gnus-summary-set-process-mark number)))))
574 (gnus-summary-position-point))
576 (defun gnus-uu-mark-over (&optional score)
577 "Mark all articles with a score over SCORE (the prefix.)"
579 (let ((score (gnus-score-default score))
580 (data gnus-newsgroup-data))
583 (when (> (or (cdr (assq (gnus-data-number (car data))
584 gnus-newsgroup-scored))
585 gnus-summary-default-score 0)
587 (gnus-summary-set-process-mark (caar data)))
588 (setq data (cdr data))))
589 (gnus-summary-position-point)))
591 (defun gnus-uu-mark-sparse ()
592 "Mark all series that have some articles marked."
594 (gnus-set-global-variables)
595 (let ((marked (nreverse gnus-newsgroup-processable))
596 subject articles total headers)
597 (or marked (error "No articles marked with the process mark"))
598 (setq gnus-newsgroup-processable nil)
601 (and (vectorp (setq headers
602 (gnus-summary-article-header (car marked))))
603 (setq subject (mail-header-subject headers)
604 articles (gnus-uu-find-articles-matching
605 (gnus-uu-reginize-string subject))
606 total (nconc total articles)))
608 (gnus-summary-set-process-mark (car articles))
609 (setcdr marked (delq (car articles) (cdr marked)))
610 (setq articles (cdr articles)))
611 (setq marked (cdr marked)))
612 (setq gnus-newsgroup-processable (nreverse total)))
613 (gnus-summary-position-point)))
615 (defun gnus-uu-mark-all ()
616 "Mark all articles in \"series\" order."
618 (gnus-set-global-variables)
619 (setq gnus-newsgroup-processable nil)
621 (let ((data gnus-newsgroup-data)
624 (when (and (not (memq (setq number (gnus-data-number (car data)))
625 gnus-newsgroup-processable))
626 (vectorp (gnus-data-header (car data))))
627 (gnus-summary-goto-subject number)
628 (gnus-uu-mark-series))
629 (setq data (cdr data)))))
630 (gnus-summary-position-point))
632 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
634 (defun gnus-uu-decode-postscript (&optional n)
635 "Gets postscript of the current article."
637 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
639 (defun gnus-uu-decode-postscript-view (&optional n)
640 "Gets and views the current article."
642 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
643 (gnus-uu-decode-postscript n)))
645 (defun gnus-uu-decode-postscript-and-save (n dir)
646 "Extracts postscript and saves the current article."
648 (list current-prefix-arg
649 (file-name-as-directory
650 (read-file-name "Save in dir: "
652 gnus-uu-default-dir t))))
653 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
656 (defun gnus-uu-decode-postscript-and-save-view (n dir)
657 "Decodes, views and saves the resulting file."
659 (list current-prefix-arg
660 (read-file-name "Where do you want to save the file(s)? "
662 gnus-uu-default-dir t)))
663 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
664 (gnus-uu-decode-postscript-and-save n dir)))
667 ;; Internal functions.
669 (defun gnus-uu-decode-with-method (method n &optional save not-insert
671 (gnus-uu-initialize scan)
672 (if save (setq gnus-uu-default-dir save))
673 ;; Create the directory we save to.
674 (when (and scan cdir save
675 (not (file-exists-p save)))
676 (make-directory save t))
677 (let ((articles (gnus-uu-get-list-of-articles n))
679 (setq files (gnus-uu-grab-articles articles method t))
680 (let ((gnus-current-article (car articles)))
681 (and scan (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
682 (and save (gnus-uu-save-files files save))
683 (if (eq gnus-uu-do-not-unpack-archives nil)
684 (setq files (gnus-uu-unpack-files files)))
685 (setq files (nreverse (gnus-uu-get-actions files)))
686 (or not-insert (not gnus-insert-pseudo-articles)
687 (gnus-summary-insert-pseudos files save))))
689 (defun gnus-uu-scan-directory (dir &optional rec)
690 "Return a list of all files under DIR."
691 (let ((files (directory-files dir t))
693 (while (setq file (pop files))
694 (unless (member (file-name-nondirectory file) '("." ".."))
695 (push (list (cons 'name file)
696 (cons 'article gnus-current-article))
698 (when (file-directory-p file)
699 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
704 (defun gnus-uu-save-files (files dir)
706 (let ((len (length files))
707 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
708 to-file file fromdir)
709 (while (setq file (cdr (assq 'name (pop files))))
710 (when (file-exists-p file)
711 (string-match reg file)
712 (setq fromdir (substring file (match-end 0)))
713 (if (file-directory-p file)
714 (gnus-make-directory (concat dir fromdir))
715 (setq to-file (concat dir fromdir))
716 (when (or (not (file-exists-p to-file))
717 (gnus-y-or-n-p (format "%s exists; overwrite? " to-file)))
718 (copy-file file to-file t t)))))
719 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
721 ;; Functions for saving and possibly digesting articles without
724 ;; Function called by gnus-uu-grab-articles to treat each article.
725 (defun gnus-uu-save-article (buffer in-state)
727 (gnus-uu-save-separate-articles
730 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
731 gnus-current-article))
732 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
733 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
735 ((eq in-state 'last) (list 'end))
736 (t (list 'middle)))))
737 ((not gnus-uu-save-in-digest)
740 (write-region 1 (point-max) gnus-uu-saved-article-name t)
741 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
742 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
744 ((eq in-state 'last) (list 'end))
745 (t (list 'middle)))))
747 (let ((header (gnus-summary-article-header)))
748 (setq gnus-uu-digest-from-subject
749 (cons (cons (mail-header-from header)
750 (mail-header-subject header))
751 gnus-uu-digest-from-subject)))
752 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
753 (delim (concat "^" (make-string 30 ?-) "$"))
754 beg subj headers headline sorthead body end-string state)
755 (if (or (eq in-state 'first)
756 (eq in-state 'first-and-last))
758 (setq state (list 'begin))
759 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
762 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
765 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
766 (current-time-string) name name))))
767 (if (not (eq in-state 'end))
768 (setq state (list 'middle))))
770 (set-buffer (get-buffer "*gnus-uu-body*"))
771 (goto-char (setq beg (point-max)))
775 (let (buffer-read-only)
776 (gnus-set-text-properties (point-min) (point-max) nil)
777 ;; These two are necessary for XEmacs 19.12 fascism.
778 (put-text-property (point-min) (point-max) 'invisible nil)
779 (put-text-property (point-min) (point-max) 'intangible nil))
780 (goto-char (point-min))
781 (re-search-forward "\n\n")
782 ;; Quote all 30-dash lines.
784 (while (re-search-forward delim nil t)
788 (setq body (buffer-substring (1- (point)) (point-max)))
789 (narrow-to-region (point-min) (point))
790 (if (not (setq headers gnus-uu-digest-headers))
791 (setq sorthead (buffer-substring (point-min) (point-max)))
793 (setq headline (car headers))
794 (setq headers (cdr headers))
795 (goto-char (point-min))
796 (while (re-search-forward headline nil t)
801 (or (and (re-search-forward "^[^ \t]" nil t)
803 (progn (forward-line 1) (point)))))))))
805 (insert sorthead) (goto-char (point-max))
806 (insert body) (goto-char (point-max))
807 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
809 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
811 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
813 (set-buffer (get-buffer "*gnus-uu-pre*"))
814 (insert (format " %s\n" subj))))))
815 (if (or (eq in-state 'last)
816 (eq in-state 'first-and-last))
819 (set-buffer (get-buffer "*gnus-uu-pre*"))
820 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
821 (write-region 1 (point-max) gnus-uu-saved-article-name))
823 (set-buffer (get-buffer "*gnus-uu-body*"))
824 (goto-char (point-max))
826 (concat (setq end-string (format "End of %s Digest" name))
828 (insert (concat (make-string (length end-string) ?*) "\n"))
829 (write-region 1 (point-max) gnus-uu-saved-article-name t))
830 (kill-buffer (get-buffer "*gnus-uu-pre*"))
831 (kill-buffer (get-buffer "*gnus-uu-body*"))
832 (setq state (cons 'end state))))
833 (if (memq 'begin state)
834 (cons gnus-uu-saved-article-name state)
837 ;; Binhex treatment - not very advanced.
839 (defconst gnus-uu-binhex-body-line
840 "^[^:]...............................................................$")
841 (defconst gnus-uu-binhex-begin-line
842 "^:...............................................................$")
843 (defconst gnus-uu-binhex-end-line
846 (defun gnus-uu-binhex-article (buffer in-state)
847 (let (state start-char)
851 (goto-char (point-min))
852 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
853 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
854 (setq state (list 'wrong-type))))
856 (if (memq 'wrong-type state)
859 (setq start-char (point))
860 (if (looking-at gnus-uu-binhex-begin-line)
862 (setq state (list 'begin))
863 (write-region 1 1 gnus-uu-binhex-article-name))
864 (setq state (list 'middle)))
865 (goto-char (point-max))
866 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
867 gnus-uu-binhex-end-line) nil t)
868 (if (looking-at gnus-uu-binhex-end-line)
869 (setq state (if (memq 'begin state)
874 (if (file-exists-p gnus-uu-binhex-article-name)
875 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
876 (if (memq 'begin state)
877 (cons gnus-uu-binhex-article-name state)
882 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
883 (let ((state (list 'ok))
884 start-char end-char file-name)
886 (set-buffer process-buffer)
887 (goto-char (point-min))
888 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
889 (setq state (list 'wrong-type))
891 (setq start-char (point))
892 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
893 (setq state (list 'wrong-type))
894 (setq end-char (point))
895 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
896 (insert-buffer-substring process-buffer start-char end-char)
897 (setq file-name (concat gnus-uu-work-dir
898 (cdr gnus-article-current) ".ps"))
899 (write-region (point-min) (point-max) file-name)
900 (setq state (list file-name 'begin 'end)))))
906 (defun gnus-uu-get-actions (files)
910 (setq name (cdr (assq 'name (car files))))
912 (setq action (gnus-uu-get-action name))
913 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
914 (cons 'action "file")
915 (cons 'action action))
916 (cons 'execute (gnus-uu-command
919 (setq files (cdr files)))
922 (defun gnus-uu-get-action (file-name)
925 (gnus-uu-choose-action
928 gnus-uu-user-view-rules
929 (if gnus-uu-ignore-default-view-rules
931 gnus-uu-default-view-rules)
932 gnus-uu-user-view-rules-end)))
933 (if (and (not (string= (or action "") "gnus-uu-archive"))
934 gnus-uu-view-with-metamail)
936 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
937 (setq action (format "metamail -d -b -c \"%s\"" action))))
941 ;; Functions for treating subjects and collecting series.
943 (defun gnus-uu-reginize-string (string)
944 ;; Takes a string and puts a \ in front of every special character;
945 ;; ignores any leading "version numbers" thingies that they use in
946 ;; the comp.binaries groups, and either replaces anything that looks
947 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
948 ;; like that, replaces the last two numbers with "[0-9]+". This, in
949 ;; my experience, should get most postings of a series.
951 (vernum "v[0-9]+[a-z][0-9]+:")
954 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
955 (buffer-disable-undo (current-buffer))
957 (insert (regexp-quote string))
960 (setq case-fold-search nil)
961 (goto-char (point-min))
962 (if (looking-at vernum)
964 (replace-match vernum t t)
965 (setq beg (length vernum))))
968 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
969 (replace-match " [0-9]+/[0-9]+")
972 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
973 (replace-match "[0-9]+ of [0-9]+")
976 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
979 (< 1 (goto-char (1- (point))))))
980 (re-search-forward "[0-9]+" nil t)
981 (replace-match "[0-9]+")
983 (setq count (1- count)))))
986 (while (re-search-forward "[ \t]+" nil t)
987 (replace-match "[ \t]*" t t))
989 (buffer-substring 1 (point-max)))))
991 (defun gnus-uu-get-list-of-articles (n)
992 ;; If N is non-nil, the article numbers of the N next articles
994 ;; If any articles have been marked as processable, they will be
996 ;; Failing that, articles that have subjects that are part of the
997 ;; same "series" as the current will be returned.
1001 (let ((backward (< n 0))
1005 (setq articles (cons (gnus-summary-article-number)
1007 (gnus-summary-search-forward nil nil backward))
1009 (nreverse articles)))
1010 (gnus-newsgroup-processable
1011 (reverse gnus-newsgroup-processable))
1013 (gnus-uu-find-articles-matching)))))
1015 (defun gnus-uu-string< (l1 l2)
1016 (string< (car l1) (car l2)))
1018 (defun gnus-uu-find-articles-matching
1019 (&optional subject only-unread do-not-translate)
1020 ;; Finds all articles that matches the regexp SUBJECT. If it is
1021 ;; nil, the current article name will be used. If ONLY-UNREAD is
1022 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1023 ;; non-nil, article names are not equalized before sorting.
1024 (let ((subject (or subject
1025 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1030 ;; Collect all subjects matching subject.
1031 (let ((case-fold-search t)
1032 (data gnus-newsgroup-data)
1036 (and (not (gnus-data-pseudo-p d))
1037 (or (not only-unread)
1038 (= (setq mark (gnus-data-mark d))
1040 (= mark gnus-ticked-mark)
1041 (= mark gnus-dormant-mark))
1042 (setq subj (mail-header-subject (gnus-data-header d)))
1043 (string-match subject subj)
1044 (setq list-of-subjects
1045 (cons (cons subj (gnus-data-number d))
1046 list-of-subjects)))))
1048 ;; Expand numbers, sort, and return the list of article
1050 (mapcar (lambda (sub) (cdr sub))
1051 (sort (gnus-uu-expand-numbers
1053 (not do-not-translate))
1054 'gnus-uu-string<))))))
1056 (defun gnus-uu-expand-numbers (string-list &optional translate)
1057 ;; Takes a list of strings and "expands" all numbers in all the
1058 ;; strings. That is, this function makes all numbers equal length by
1059 ;; prepending lots of zeroes before each number. This is to ease later
1060 ;; sorting to find out what sequence the articles are supposed to be
1061 ;; decoded in. Returns the list of expanded strings.
1062 (let ((out-list string-list)
1065 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1066 (buffer-disable-undo (current-buffer))
1069 (insert (caar string-list))
1070 ;; Translate multiple spaces to one space.
1071 (goto-char (point-min))
1072 (while (re-search-forward "[ \t]+" nil t)
1073 (replace-match " "))
1074 ;; Translate all characters to "a".
1075 (goto-char (point-min))
1077 (while (re-search-forward "[A-Za-z]" nil t)
1078 (replace-match "a" t t)))
1080 (goto-char (point-min))
1081 (while (re-search-forward "[0-9]+" nil t)
1084 (string-to-int (buffer-substring
1085 (match-beginning 0) (match-end 0))))))
1086 (setq string (buffer-substring 1 (point-max)))
1087 (setcar (car string-list) string)
1088 (setq string-list (cdr string-list))))
1092 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1093 ;; function. It takes a list of articles to be grabbed and a function
1094 ;; to apply to each article.
1096 ;; The function to be called should take two parameters. The first
1097 ;; parameter is the article buffer. The function should leave the
1098 ;; result, if any, in this buffer. Most treatment functions will just
1099 ;; generate files...
1101 ;; The second parameter is the state of the list of articles, and can
1102 ;; have four values: `first', `middle', `last' and `first-and-last'.
1104 ;; The function should return a list. The list may contain the
1105 ;; following symbols:
1106 ;; `error' if an error occurred
1107 ;; `begin' if the beginning of an encoded file has been received
1108 ;; If the list returned contains a `begin', the first element of
1109 ;; the list *must* be a string with the file name of the decoded
1111 ;; `end' if the end of an encoded file has been received
1112 ;; `middle' if the article was a body part of an encoded file
1113 ;; `wrong-type' if the article was not a part of an encoded file
1114 ;; `ok', which can be used everything is ok
1116 (defvar gnus-uu-has-been-grabbed nil)
1118 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1120 (if (not (and gnus-uu-has-been-grabbed
1121 gnus-uu-unmark-articles-not-decoded))
1123 (if dont-unmark-last-article
1125 (setq art (car gnus-uu-has-been-grabbed))
1126 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
1127 (while gnus-uu-has-been-grabbed
1128 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1129 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1130 (if dont-unmark-last-article
1131 (setq gnus-uu-has-been-grabbed (list art))))))
1133 ;; This function takes a list of articles and a function to apply to
1134 ;; each article grabbed.
1136 ;; This function returns a list of files decoded if the grabbing and
1137 ;; the process-function has been successful and nil otherwise.
1138 (defun gnus-uu-grab-articles (articles process-function
1139 &optional sloppy limit no-errors)
1140 (let ((state 'first)
1141 (gnus-asynchronous nil)
1142 has-been-begin article result-file result-files process-state
1143 gnus-summary-display-article-function
1144 gnus-article-display-hook gnus-article-prepare-hook
1145 article-series files)
1147 (while (and articles
1148 (not (memq 'error process-state))
1150 (not (memq 'end process-state))))
1152 (setq article (pop articles))
1153 (push article article-series)
1156 (if (eq state 'first)
1157 (setq state 'first-and-last)
1158 (setq state 'last)))
1160 (let ((part (gnus-uu-part-number article)))
1161 (gnus-message 6 "Getting article %d%s..."
1162 article (if (string= part "") "" (concat ", " part))))
1163 (gnus-summary-display-article article)
1165 ;; Push the article to the processing function.
1167 (set-buffer gnus-original-article-buffer)
1168 (let ((buffer-read-only nil))
1170 (set-buffer gnus-summary-buffer)
1172 (funcall process-function
1173 gnus-original-article-buffer state)))))
1175 (gnus-summary-remove-process-mark article)
1177 ;; If this is the beginning of a decoded file, we push it
1179 (when (or (memq 'begin process-state)
1180 (and (or (eq state 'first)
1181 (eq state 'first-and-last))
1182 (memq 'ok process-state)))
1184 ;; If there is a `result-file' here, that means that the
1185 ;; file was unsuccessfully decoded, so we delete it.
1186 (when (and result-file
1187 (file-exists-p result-file)
1189 (format "Delete unsuccessfully decoded file %s"
1191 (delete-file result-file)))
1192 (when (memq 'begin process-state)
1193 (setq result-file (car process-state)))
1194 (setq has-been-begin t))
1196 ;; Check whether we have decoded one complete file.
1197 (when (memq 'end process-state)
1198 (setq article-series nil)
1199 (setq has-been-begin nil)
1200 (if (stringp result-file)
1201 (setq files (list result-file))
1202 (setq files result-file))
1203 (setq result-file (car files))
1205 (push (list (cons 'name (pop files))
1206 (cons 'article article))
1208 ;; Allow user-defined functions to be run on this file.
1209 (when gnus-uu-grabbed-file-functions
1210 (let ((funcs gnus-uu-grabbed-file-functions))
1211 (unless (listp funcs)
1212 (setq funcs (list funcs)))
1214 (funcall (pop funcs) result-file))))
1215 (setq result-file nil)
1216 ;; Check whether we have decoded enough articles.
1217 (and limit (= (length result-files) limit)
1218 (setq articles nil)))
1220 ;; If this is the last article to be decoded, and
1221 ;; we still haven't reached the end, then we delete
1222 ;; the partially decoded file.
1223 (and (or (eq state 'last) (eq state 'first-and-last))
1224 (not (memq 'end process-state))
1226 (file-exists-p result-file)
1227 (gnus-y-or-n-p (format "Delete incomplete file %s? " result-file))
1228 (delete-file result-file))
1230 ;; If this was a file of the wrong sort, then
1231 (when (and (or (memq 'wrong-type process-state)
1232 (memq 'error process-state))
1233 gnus-uu-unmark-articles-not-decoded)
1234 (gnus-summary-tick-article article t))
1236 ;; Set the new series state.
1237 (if (and (not has-been-begin)
1239 (or (memq 'end process-state)
1240 (memq 'middle process-state)))
1242 (setq process-state (list 'error))
1243 (gnus-message 2 "No begin part at the beginning")
1245 (setq state 'middle)))
1247 ;; When there are no result-files, then something must be wrong.
1251 ((not has-been-begin)
1252 (gnus-message 2 "Wrong type file"))
1253 ((memq 'error process-state)
1254 (gnus-message 2 "An error occurred during decoding"))
1255 ((not (or (memq 'ok process-state)
1256 (memq 'end process-state)))
1257 (gnus-message 2 "End of articles reached before end of file")))
1258 ;; Make unsuccessfully decoded articles unread.
1259 (when gnus-uu-unmark-articles-not-decoded
1260 (while article-series
1261 (gnus-summary-tick-article (pop article-series) t))))
1265 (defun gnus-uu-grab-view (file)
1266 "View FILE using the gnus-uu methods."
1267 (let ((action (gnus-uu-get-action file)))
1268 (gnus-execute-command
1269 (if (string-match "%" action)
1270 (format action file)
1271 (concat action " " file))
1272 (eq gnus-view-pseudos 'not-confirm))))
1274 (defun gnus-uu-grab-move (file)
1275 "Move FILE to somewhere."
1276 (when gnus-uu-default-dir
1277 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1278 (file-name-nondirectory file))))
1279 (rename-file file to-file)
1280 (unless (file-exists-p file)
1281 (make-symbolic-link to-file file)))))
1283 (defun gnus-uu-part-number (article)
1284 (let* ((header (gnus-summary-article-header article))
1285 (subject (and header (mail-header-subject header))))
1287 (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+" subject))
1288 (match-string 0 subject)
1291 (defun gnus-uu-uudecode-sentinel (process event)
1292 (delete-process (get-process process)))
1294 (defun gnus-uu-uustrip-article (process-buffer in-state)
1295 ;; Uudecodes a file asynchronously.
1297 (set-buffer process-buffer)
1298 (let ((state (list 'wrong-type))
1299 process-connection-type case-fold-search buffer-read-only
1301 (goto-char (point-min))
1303 ;; Deal with ^M at the end of the lines.
1304 (when gnus-uu-kill-carriage-return
1306 (while (search-forward "\r" nil t)
1307 (delete-backward-char 1))))
1309 (while (or (re-search-forward gnus-uu-begin-string nil t)
1310 (re-search-forward gnus-uu-body-line nil t))
1311 (setq state (list 'ok))
1312 ;; Ok, we are at the first uucoded line.
1314 (setq start-char (point))
1316 (if (not (looking-at gnus-uu-begin-string))
1317 (setq state (list 'middle))
1318 ;; This is the beginning of an uuencoded article.
1319 ;; We replace certain characters that could make things messy.
1320 (setq gnus-uu-file-name
1321 (let ((nnheader-file-name-translation-alist
1322 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1323 (nnheader-translate-file-chars (match-string 1))))
1325 ;; Remove any non gnus-uu-body-line right after start.
1327 (while (and (not (eobp))
1328 (not (looking-at gnus-uu-body-line)))
1331 ;; If a process is running, we kill it.
1332 (when (and gnus-uu-uudecode-process
1333 (memq (process-status gnus-uu-uudecode-process)
1335 (delete-process gnus-uu-uudecode-process)
1336 (gnus-uu-unmark-list-of-grabbed t))
1338 ;; Start a new uudecoding process.
1339 (let ((cdir default-directory))
1342 (cd gnus-uu-work-dir)
1343 (setq gnus-uu-uudecode-process
1346 (get-buffer-create gnus-uu-output-buffer-name)
1347 shell-file-name shell-command-switch
1348 (format "cd %s %s uudecode" gnus-uu-work-dir
1349 gnus-shell-command-separator))))
1351 (set-process-sentinel
1352 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1353 (setq state (list 'begin))
1354 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1356 ;; We look for the end of the thing to be decoded.
1357 (if (re-search-forward gnus-uu-end-string nil t)
1358 (setq state (cons 'end state))
1359 (goto-char (point-max))
1360 (re-search-backward gnus-uu-body-line nil t))
1364 (when gnus-uu-uudecode-process
1365 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1366 ;; Try to correct mishandled uucode.
1367 (when gnus-uu-correct-stripped-uucode
1368 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1370 ;; Send the text to the process.
1372 (process-send-region
1373 gnus-uu-uudecode-process start-char (point))
1376 (delete-process gnus-uu-uudecode-process)
1377 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1378 (setq state (list 'wrong-type)))))
1380 (if (memq 'end state)
1382 ;; Send an EOF, just in case.
1384 (process-send-eof gnus-uu-uudecode-process)
1386 (while (memq (process-status gnus-uu-uudecode-process)
1388 (accept-process-output gnus-uu-uudecode-process 1)))
1389 (when (or (not gnus-uu-uudecode-process)
1390 (not (memq (process-status gnus-uu-uudecode-process)
1392 (setq state (list 'wrong-type)))))))
1394 (if (memq 'begin state)
1395 (cons (if (= (length files) 1) (car files) files) state)
1398 ;; This function is used by `gnus-uu-grab-articles' to treat
1399 ;; a shared article.
1400 (defun gnus-uu-unshar-article (process-buffer in-state)
1401 (let ((state (list 'ok))
1404 (set-buffer process-buffer)
1405 (goto-char (point-min))
1406 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1407 (setq state (list 'wrong-type))
1409 (setq start-char (point))
1410 (call-process-region
1411 start-char (point-max) shell-file-name nil
1412 (get-buffer-create gnus-uu-output-buffer-name) nil
1413 shell-command-switch
1414 (concat "cd " gnus-uu-work-dir " "
1415 gnus-shell-command-separator " sh"))))
1418 ;; Returns the name of what the shar file is going to unpack.
1419 (defun gnus-uu-find-name-in-shar ()
1420 (let ((oldpoint (point))
1422 (goto-char (point-min))
1423 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1424 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1425 (goto-char oldpoint)
1428 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1429 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1430 ;; found, or the name of the command to run if such a rule is found.
1431 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1432 (let ((action-list (copy-sequence file-action-list))
1433 (case-fold-search t)
1438 (and gnus-uu-ignore-files-by-name
1439 (string-match gnus-uu-ignore-files-by-name file-name)))
1441 (and gnus-uu-ignore-files-by-type
1442 (string-match gnus-uu-ignore-files-by-type
1443 (or (gnus-uu-choose-action
1444 file-name gnus-uu-ext-to-mime-list t)
1446 (while (not (or (eq action-list ()) action))
1447 (setq rule (car action-list))
1448 (setq action-list (cdr action-list))
1449 (if (string-match (car rule) file-name)
1450 (setq action (cadr rule)))))
1453 (defun gnus-uu-treat-archive (file-path)
1454 ;; Unpacks an archive. Returns t if unpacking is successful.
1455 (let ((did-unpack t)
1457 (setq action (gnus-uu-choose-action
1458 file-path (append gnus-uu-user-archive-rules
1459 (if gnus-uu-ignore-default-archive-rules
1461 gnus-uu-default-archive-rules))))
1463 (if (not action) (error "No unpackers for the file %s" file-path))
1465 (string-match "/[^/]*$" file-path)
1466 (setq dir (substring file-path 0 (match-beginning 0)))
1468 (if (member action gnus-uu-destructive-archivers)
1469 (copy-file file-path (concat file-path "~") t))
1471 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1474 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1477 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1479 (if (= 0 (call-process shell-file-name nil
1480 (get-buffer-create gnus-uu-output-buffer-name)
1481 nil shell-command-switch command))
1483 (gnus-message 2 "Error during unpacking of archive")
1484 (setq did-unpack nil))
1486 (if (member action gnus-uu-destructive-archivers)
1487 (rename-file (concat file-path "~") file-path t))
1491 (defun gnus-uu-dir-files (dir)
1492 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1495 (if (file-directory-p (setq file (car dirs)))
1496 (setq files (append files (gnus-uu-dir-files file)))
1497 (setq files (cons file files)))
1498 (setq dirs (cdr dirs)))
1501 (defun gnus-uu-unpack-files (files &optional ignore)
1502 ;; Go through FILES and look for files to unpack.
1503 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1507 (setq file (cdr (assq 'name (car files))))
1508 (if (and (not (member file ignore))
1509 (equal (gnus-uu-get-action (file-name-nondirectory file))
1512 (setq did-unpack (cons file did-unpack))
1513 (or (gnus-uu-treat-archive file)
1514 (gnus-message 2 "Error during unpacking of %s" file))
1515 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1518 (or (member (car nfiles) totfiles)
1519 (setq ofiles (cons (list (cons 'name (car nfiles))
1520 (cons 'original file))
1522 (setq nfiles (cdr nfiles)))
1523 (setq totfiles newfiles))))
1524 (setq files (cdr files)))
1526 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1529 (defun gnus-uu-ls-r (dir)
1530 (let* ((files (gnus-uu-directory-files dir t))
1533 (if (file-directory-p (car files))
1535 (setq ofiles (delete (car files) ofiles))
1536 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1537 (setq files (cdr files)))
1542 (defun gnus-uu-directory-files (dir &optional full)
1543 (let (files out file)
1544 (setq files (directory-files dir full))
1546 (setq file (car files))
1547 (setq files (cdr files))
1548 (or (member (file-name-nondirectory file) '("." ".."))
1549 (setq out (cons file out))))
1550 (setq out (nreverse out))
1553 (defun gnus-uu-check-correct-stripped-uucode (start end)
1555 (let (found beg length)
1556 (if (not gnus-uu-correct-stripped-uucode)
1560 (if (re-search-forward " \\|`" end t)
1565 (if (looking-at "\n") (replace-match ""))
1569 (if (looking-at (concat gnus-uu-begin-string "\\|"
1570 gnus-uu-end-string))
1577 (setq length (- (point) beg))))
1582 (if (not (= length (- (point) beg)))
1583 (insert (make-string (- length (- (point) beg)) ? ))))
1584 (forward-line 1)))))))
1586 (defvar gnus-uu-tmp-alist nil)
1588 (defun gnus-uu-initialize (&optional scan)
1591 (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1592 (if (file-exists-p (cdr entry))
1593 (setq gnus-uu-work-dir (cdr entry))
1594 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1597 (setq gnus-uu-tmp-dir (file-name-as-directory
1598 (expand-file-name gnus-uu-tmp-dir)))
1599 (if (not (file-directory-p gnus-uu-tmp-dir))
1600 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1601 (if (not (file-writable-p gnus-uu-tmp-dir))
1602 (error "Temp directory %s can't be written to"
1605 (setq gnus-uu-work-dir
1606 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1607 (gnus-make-directory gnus-uu-work-dir)
1608 (set-file-modes gnus-uu-work-dir 448)
1609 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1610 (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1611 gnus-uu-tmp-alist)))))
1614 ;; Kills the temporary uu buffers, kills any processes, etc.
1615 (defun gnus-uu-clean-up ()
1617 (and gnus-uu-uudecode-process
1618 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1620 (delete-process gnus-uu-uudecode-process))
1621 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1622 (kill-buffer buf))))
1624 ;; Inputs an action and a file and returns a full command, putting
1625 ;; quotes round the file name and escaping any quotes in the file name.
1626 (defun gnus-uu-command (action file)
1628 (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1631 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1632 (substring file (match-beginning 0) (match-end 0))))
1633 (setq file (substring file (1+ (match-beginning 0))))))
1634 (setq ofile (concat "\"" ofile file "\""))
1635 (if (string-match "%s" action)
1636 (format action ofile)
1637 (concat action " " ofile))))
1639 (defun gnus-uu-delete-work-dir (&optional dir)
1640 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1642 (gnus-message 7 "Deleting directory %s..." dir)
1643 (setq dir gnus-uu-work-dir))
1645 (file-exists-p dir))
1646 (let ((files (directory-files dir t nil t))
1648 (while (setq file (pop files))
1649 (unless (member (file-name-nondirectory file) '("." ".."))
1650 (if (file-directory-p file)
1651 (gnus-uu-delete-work-dir file)
1652 (gnus-message 9 "Deleting file %s..." file)
1653 (delete-file file))))
1654 (delete-directory dir)))
1655 (gnus-message 7 ""))
1659 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1660 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1665 ;;; uuencoded posting
1668 ;; Any function that is to be used as and encoding method will take two
1669 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1670 ;; and "spiral.jpg", respectively.) The function should return nil if
1671 ;; the encoding wasn't successful.
1672 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1673 "Function used for encoding binary files.
1674 There are three functions supplied with gnus-uu for encoding files:
1675 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1676 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1677 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1678 uuencode and adds MIME headers.")
1680 (defvar gnus-uu-post-include-before-composing nil
1681 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1682 If this variable is t, you can either include an encoded file with
1683 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1685 (defvar gnus-uu-post-length 990
1686 "Maximum length of an article.
1687 The encoded file will be split into how many articles it takes to
1688 post the entire file.")
1690 (defvar gnus-uu-post-threaded nil
1691 "Non-nil means that gnus-uu will post the encoded file in a thread.
1692 This may not be smart, as no other decoder I have seen are able to
1693 follow threads when collecting uuencoded articles. (Well, I have seen
1694 one package that does that - gnus-uu, but somehow, I don't think that
1695 counts...) Default is nil.")
1697 (defvar gnus-uu-post-separate-description t
1698 "Non-nil means that the description will be posted in a separate article.
1699 The first article will typically be numbered (0/x). If this variable
1700 is nil, the description the user enters will be included at the
1701 beginning of the first article, which will be numbered (1/x). Default
1704 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1705 (defvar gnus-uu-post-message-id nil)
1706 (defvar gnus-uu-post-inserted-file-name nil)
1707 (defvar gnus-uu-winconf-post-news nil)
1709 (defun gnus-uu-post-news ()
1710 "Compose an article and post an encoded file."
1712 (setq gnus-uu-post-inserted-file-name nil)
1713 (setq gnus-uu-winconf-post-news (current-window-configuration))
1715 (gnus-summary-post-news)
1717 (use-local-map (copy-keymap (current-local-map)))
1718 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1719 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1720 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1721 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1723 (if gnus-uu-post-include-before-composing
1724 (save-excursion (setq gnus-uu-post-inserted-file-name
1725 (gnus-uu-post-insert-binary)))))
1727 (defun gnus-uu-post-insert-binary-in-article ()
1728 "Inserts an encoded file in the buffer.
1729 The user will be asked for a file name."
1732 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1734 ;; Encodes with uuencode and substitutes all spaces with backticks.
1735 (defun gnus-uu-post-encode-uuencode (path file-name)
1736 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1738 (goto-char (point-min))
1740 (while (re-search-forward " " nil t)
1741 (replace-match "`"))
1744 ;; Encodes with uuencode and adds MIME headers.
1745 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1746 (if (gnus-uu-post-encode-uuencode path file-name)
1748 (gnus-uu-post-make-mime file-name "x-uue")
1751 ;; Encodes with base64 and adds MIME headers
1752 (defun gnus-uu-post-encode-mime (path file-name)
1753 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1755 (gnus-uu-post-make-mime file-name "base64")
1758 ;; Adds MIME headers.
1759 (defun gnus-uu-post-make-mime (file-name encoding)
1760 (goto-char (point-min))
1761 (insert (format "Content-Type: %s; name=\"%s\"\n"
1762 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1764 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1766 (set-buffer gnus-message-buffer)
1767 (goto-char (point-min))
1768 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1770 (narrow-to-region 1 (point))
1771 (or (mail-fetch-field "mime-version")
1774 (insert "MIME-Version: 1.0\n")))
1777 ;; Encodes a file PATH with COMMAND, leaving the result in the
1779 (defun gnus-uu-post-encode-file (command path file-name)
1780 (= 0 (call-process shell-file-name nil t nil shell-command-switch
1781 (format "%s %s %s" command path file-name))))
1783 (defun gnus-uu-post-news-inews ()
1784 "Posts the composed news article and encoded file.
1785 If no file has been included, the user will be asked for a file."
1790 (if gnus-uu-post-inserted-file-name
1791 (setq file-name gnus-uu-post-inserted-file-name)
1792 (setq file-name (gnus-uu-post-insert-binary)))
1794 (if gnus-uu-post-threaded
1795 (let ((message-required-news-headers
1796 (if (memq 'Message-ID message-required-news-headers)
1797 message-required-news-headers
1798 (cons 'Message-ID message-required-news-headers)))
1799 gnus-inews-article-hook)
1801 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1802 gnus-inews-article-hook
1803 (list gnus-inews-article-hook)))
1804 (setq gnus-inews-article-hook
1808 (goto-char (point-min))
1809 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1810 (setq gnus-uu-post-message-id
1812 (match-beginning 1) (match-end 1)))
1813 (setq gnus-uu-post-message-id nil))))
1814 gnus-inews-article-hook))
1815 (gnus-uu-post-encoded file-name t))
1816 (gnus-uu-post-encoded file-name nil)))
1817 (setq gnus-uu-post-inserted-file-name nil)
1818 (and gnus-uu-winconf-post-news
1819 (set-window-configuration gnus-uu-winconf-post-news)))
1821 ;; Asks for a file to encode, encodes it and inserts the result in
1822 ;; the current buffer. Returns the file name the user gave.
1823 (defun gnus-uu-post-insert-binary ()
1824 (let ((uuencode-buffer-name "*uuencode buffer*")
1825 file-path uubuf file-name)
1827 (setq file-path (read-file-name
1828 "What file do you want to encode? "))
1829 (if (not (file-exists-p file-path))
1830 (error "%s: No such file" file-path))
1832 (goto-char (point-max))
1833 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1835 (if (string-match "^~/" file-path)
1836 (setq file-path (concat "$HOME" (substring file-path 1))))
1837 (if (string-match "/[^/]*$" file-path)
1838 (setq file-name (substring file-path (1+ (match-beginning 0))))
1839 (setq file-name file-path))
1843 (set-buffer (setq uubuf
1844 (get-buffer-create uuencode-buffer-name)))
1846 (funcall gnus-uu-post-encode-method file-path file-name))
1847 (insert-buffer-substring uubuf)
1848 (error "Encoding unsuccessful"))
1849 (kill-buffer uubuf))
1852 ;; Posts the article and all of the encoded file.
1853 (defun gnus-uu-post-encoded (file-name &optional threaded)
1854 (let ((send-buffer-name "*uuencode send buffer*")
1855 (encoded-buffer-name "*encoded buffer*")
1856 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1857 (separator (concat mail-header-separator "\n\n"))
1858 uubuf length parts header i end beg
1859 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1861 (setq post-buf (current-buffer))
1863 (goto-char (point-min))
1864 (if (not (re-search-forward
1865 (if gnus-uu-post-separate-description
1866 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1868 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1869 (error "Internal error: No binary/header separator"))
1872 (setq beg-binary (point))
1873 (setq end-binary (point-max))
1876 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1878 (insert-buffer-substring post-buf beg-binary end-binary)
1879 (goto-char (point-min))
1880 (setq length (count-lines 1 (point-max)))
1881 (setq parts (/ length gnus-uu-post-length))
1882 (if (not (< (% length gnus-uu-post-length) 4))
1883 (setq parts (1+ parts))))
1885 (if gnus-uu-post-separate-description
1887 (kill-region (point) (point-max))
1889 (goto-char (point-min))
1891 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1893 (setq header (buffer-substring 1 (point)))
1895 (goto-char (point-min))
1896 (if (not gnus-uu-post-separate-description)
1898 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1901 (insert (format " (0/%d)" parts))))
1907 (while (not (> i parts))
1908 (set-buffer (get-buffer-create send-buffer-name))
1911 (if (and threaded gnus-uu-post-message-id)
1912 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1915 (- 62 (length (format top-string "" file-name i parts ""))))
1916 (if (> 1 (setq minlen (/ whole-len 2)))
1921 (make-string minlen ?-)
1924 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1926 (goto-char (point-min))
1927 (if (not (re-search-forward "^Subject: " nil t))
1932 (insert (format " (%d/%d)" i parts)))
1933 (if (or (and (= i 2) gnus-uu-post-separate-description)
1934 (and (= i 1) (not gnus-uu-post-separate-description)))
1935 (replace-match "Subject: Re: "))))
1937 (goto-char (point-max))
1942 (goto-char (point-max))
1943 (forward-line gnus-uu-post-length))
1944 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1947 (insert-buffer-substring uubuf beg end)
1952 (goto-char (point-min))
1954 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1957 (if (re-search-forward
1958 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1965 (let (message-sent-message-via)
1968 (and (setq buf (get-buffer send-buffer-name))
1970 (and (setq buf (get-buffer encoded-buffer-name))
1973 (if (not gnus-uu-post-separate-description)
1975 (set-buffer-modified-p nil)
1976 (and (fboundp 'bury-buffer) (bury-buffer))))))
1980 ;; gnus-uu.el ends here