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 (defvar gnus-uu-be-dangerous 'ask
238 "*Specifies what to do if unusual situations arise during decoding.
239 If nil, be as conservative as possible. If t, ignore things that
240 didn't work, and overwrite existing files. Otherwise, ask each time.")
242 ;; Internal variables
244 (defvar gnus-uu-saved-article-name nil)
246 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
247 (defconst gnus-uu-end-string "^end[ \t]*$")
249 (defconst gnus-uu-body-line "^M")
251 (while (> (setq i (1- i)) 0)
252 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
253 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
255 ;"^M.............................................................?$"
257 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
259 (defvar gnus-uu-shar-file-name nil)
260 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
262 (defconst gnus-uu-postscript-begin-string "^%!PS-")
263 (defconst gnus-uu-postscript-end-string "^%%EOF$")
265 (defvar gnus-uu-file-name nil)
266 (defconst gnus-uu-uudecode-process nil)
267 (defvar gnus-uu-binhex-article-name nil)
269 (defvar gnus-uu-work-dir nil)
271 (defconst gnus-uu-output-buffer-name " *Gnus UU Output*")
273 (defvar gnus-uu-default-dir gnus-article-save-directory)
274 (defvar gnus-uu-digest-from-subject nil)
278 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
279 "p" gnus-summary-mark-as-processable
280 "u" gnus-summary-unmark-as-processable
281 "U" gnus-summary-unmark-all-processable
282 "v" gnus-uu-mark-over
283 "s" gnus-uu-mark-series
284 "r" gnus-uu-mark-region
285 "R" gnus-uu-mark-by-regexp
286 "t" gnus-uu-mark-thread
287 "T" gnus-uu-unmark-thread
289 "b" gnus-uu-mark-buffer
290 "S" gnus-uu-mark-sparse
291 "k" gnus-summary-kill-process-mark
292 "y" gnus-summary-yank-process-mark
293 "w" gnus-summary-save-process-mark
294 "i" gnus-uu-invert-processable)
296 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
297 ;;"x" gnus-uu-extract-any
298 ;;"m" gnus-uu-extract-mime
299 "u" gnus-uu-decode-uu
300 "U" gnus-uu-decode-uu-and-save
301 "s" gnus-uu-decode-unshar
302 "S" gnus-uu-decode-unshar-and-save
303 "o" gnus-uu-decode-save
304 "O" gnus-uu-decode-save
305 "b" gnus-uu-decode-binhex
306 "B" gnus-uu-decode-binhex
307 "p" gnus-uu-decode-postscript
308 "P" gnus-uu-decode-postscript-and-save)
311 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
312 "u" gnus-uu-decode-uu-view
313 "U" gnus-uu-decode-uu-and-save-view
314 "s" gnus-uu-decode-unshar-view
315 "S" gnus-uu-decode-unshar-and-save-view
316 "o" gnus-uu-decode-save-view
317 "O" gnus-uu-decode-save-view
318 "b" gnus-uu-decode-binhex-view
319 "B" gnus-uu-decode-binhex-view
320 "p" gnus-uu-decode-postscript-view
321 "P" gnus-uu-decode-postscript-and-save-view)
326 (defun gnus-uu-decode-uu (&optional n)
327 "Uudecodes the current article."
329 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
331 (defun gnus-uu-decode-uu-and-save (n dir)
332 "Decodes and saves the resulting file."
334 (list current-prefix-arg
335 (file-name-as-directory
336 (read-file-name "Uudecode and save in dir: "
338 gnus-uu-default-dir t))))
339 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
341 (defun gnus-uu-decode-unshar (&optional n)
342 "Unshars the current article."
344 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
346 (defun gnus-uu-decode-unshar-and-save (n dir)
347 "Unshars and saves the current article."
349 (list current-prefix-arg
350 (file-name-as-directory
351 (read-file-name "Unshar and save in dir: "
353 gnus-uu-default-dir t))))
354 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
356 (defun gnus-uu-decode-save (n file)
357 "Saves the current article."
359 (list current-prefix-arg
361 (if gnus-uu-save-separate-articles
362 "Save articles is dir: "
363 "Save articles in file: ")
365 gnus-uu-default-dir)))
366 (setq gnus-uu-saved-article-name file)
367 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
369 (defun gnus-uu-decode-binhex (n dir)
370 "Unbinhexes the current article."
372 (list current-prefix-arg
373 (file-name-as-directory
374 (read-file-name "Unbinhex and save in dir: "
376 gnus-uu-default-dir))))
377 (setq gnus-uu-binhex-article-name
378 (make-temp-name (concat gnus-uu-work-dir "binhex")))
379 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
381 (defun gnus-uu-decode-uu-view (&optional n)
382 "Uudecodes and views the current article."
384 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
385 (gnus-uu-decode-uu n)))
387 (defun gnus-uu-decode-uu-and-save-view (n dir)
388 "Decodes, views and saves the resulting file."
390 (list current-prefix-arg
391 (read-file-name "Uudecode, view and save in dir: "
393 gnus-uu-default-dir t)))
394 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
395 (gnus-uu-decode-uu-and-save n dir)))
397 (defun gnus-uu-decode-unshar-view (&optional n)
398 "Unshars and views the current article."
400 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
401 (gnus-uu-decode-unshar n)))
403 (defun gnus-uu-decode-unshar-and-save-view (n dir)
404 "Unshars and saves the current article."
406 (list current-prefix-arg
407 (read-file-name "Unshar, view and save in dir: "
409 gnus-uu-default-dir t)))
410 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
411 (gnus-uu-decode-unshar-and-save n dir)))
413 (defun gnus-uu-decode-save-view (n file)
414 "Saves and views the current article."
416 (list current-prefix-arg
417 (read-file-name (if gnus-uu-save-separate-articles
418 "Save articles is dir: "
419 "Save articles in file: ")
420 gnus-uu-default-dir gnus-uu-default-dir)))
421 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
422 (gnus-uu-decode-save n file)))
424 (defun gnus-uu-decode-binhex-view (n file)
425 "Unbinhexes and views the current article."
427 (list current-prefix-arg
428 (read-file-name "Unbinhex, view and save in dir: "
429 gnus-uu-default-dir gnus-uu-default-dir)))
430 (setq gnus-uu-binhex-article-name
431 (make-temp-name (concat gnus-uu-work-dir "binhex")))
432 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
433 (gnus-uu-decode-binhex n file)))
436 ;; Digest and forward articles
438 (defun gnus-uu-digest-mail-forward (&optional n post)
439 "Digests and forwards all articles in this series."
441 (let ((gnus-uu-save-in-digest t)
442 (file (make-temp-name (nnheader-concat gnus-uu-tmp-dir "forward")))
443 buf subject from newsgroups)
444 (setq gnus-uu-digest-from-subject nil)
445 (gnus-uu-decode-save n file)
446 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
447 (gnus-add-current-to-buffer-list)
449 (delete-other-windows)
451 (let ((fs gnus-uu-digest-from-subject))
454 subject (gnus-simplify-subject-fuzzy (cdar fs))
456 (while (and fs (or from subject))
458 (unless (string= from (caar fs))
461 (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
466 (setq subject "Digested Articles"))
469 (if (gnus-news-group-p gnus-newsgroup-name)
472 (goto-char (point-min))
473 (when (re-search-forward "^Subject: ")
474 (delete-region (point) (gnus-point-at-eol))
476 (goto-char (point-min))
477 (when (re-search-forward "^From: ")
478 (delete-region (point) (gnus-point-at-eol))
480 (message-forward post)
483 (setq gnus-uu-digest-from-subject nil)))
485 (defun gnus-uu-digest-post-forward (&optional n)
486 "Digest and forward to a newsgroup."
488 (gnus-uu-digest-mail-forward n t))
492 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
493 "Ask for a regular expression and set the process mark on all articles that match."
494 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
495 (gnus-set-global-variables)
496 (let ((articles (gnus-uu-find-articles-matching regexp)))
499 (gnus-summary-remove-process-mark (pop articles))
500 (gnus-summary-set-process-mark (pop articles))))
502 (gnus-summary-position-point))
504 (defun gnus-uu-unmark-by-regexp (regexp &optional unmark)
505 "Ask for a regular expression and remove the process mark on all articles that match."
506 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
507 (gnus-uu-mark-by-regexp regexp t))
509 (defun gnus-uu-mark-series ()
510 "Mark the current series with the process mark."
512 (gnus-set-global-variables)
513 (let ((articles (gnus-uu-find-articles-matching)))
515 (gnus-summary-set-process-mark (car articles))
516 (setq articles (cdr articles)))
518 (gnus-summary-position-point))
520 (defun gnus-uu-mark-region (beg end &optional unmark)
521 "Set the process mark on all articles between point and mark."
523 (gnus-set-global-variables)
526 (while (< (point) end)
528 (gnus-summary-remove-process-mark (gnus-summary-article-number))
529 (gnus-summary-set-process-mark (gnus-summary-article-number)))
531 (gnus-summary-position-point))
533 (defun gnus-uu-unmark-region (beg end)
534 "Remove the process mark from all articles between point and mark."
536 (gnus-uu-mark-region beg end t))
538 (defun gnus-uu-mark-buffer ()
539 "Set the process mark on all articles in the buffer."
541 (gnus-uu-mark-region (point-min) (point-max)))
543 (defun gnus-uu-unmark-buffer ()
544 "Remove the process mark on all articles in the buffer."
546 (gnus-uu-mark-region (point-min) (point-max) t))
548 (defun gnus-uu-mark-thread ()
549 "Marks all articles downwards in this thread."
551 (gnus-set-global-variables)
552 (let ((level (gnus-summary-thread-level)))
553 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
554 (zerop (gnus-summary-next-subject 1))
555 (> (gnus-summary-thread-level) level))))
556 (gnus-summary-position-point))
558 (defun gnus-uu-unmark-thread ()
559 "Unmarks all articles downwards in this thread."
561 (gnus-set-global-variables)
562 (let ((level (gnus-summary-thread-level)))
563 (while (and (gnus-summary-remove-process-mark
564 (gnus-summary-article-number))
565 (zerop (gnus-summary-next-subject 1))
566 (> (gnus-summary-thread-level) level))))
567 (gnus-summary-position-point))
569 (defun gnus-uu-invert-processable ()
570 "Invert the list of process-marked articles."
571 (let ((data gnus-newsgroup-data)
575 (if (memq (setq number (gnus-data-number (pop data)))
576 gnus-newsgroup-processable)
577 (gnus-summary-remove-process-mark number)
578 (gnus-summary-set-process-mark number)))))
579 (gnus-summary-position-point))
581 (defun gnus-uu-mark-over (&optional score)
582 "Mark all articles with a score over SCORE (the prefix.)"
584 (let ((score (gnus-score-default score))
585 (data gnus-newsgroup-data))
588 (when (> (or (cdr (assq (gnus-data-number (car data))
589 gnus-newsgroup-scored))
590 gnus-summary-default-score 0)
592 (gnus-summary-set-process-mark (caar data)))
593 (setq data (cdr data))))
594 (gnus-summary-position-point)))
596 (defun gnus-uu-mark-sparse ()
597 "Mark all series that have some articles marked."
599 (gnus-set-global-variables)
600 (let ((marked (nreverse gnus-newsgroup-processable))
601 subject articles total headers)
603 (error "No articles marked with the process mark"))
604 (setq gnus-newsgroup-processable nil)
607 (and (vectorp (setq headers
608 (gnus-summary-article-header (car marked))))
609 (setq subject (mail-header-subject headers)
610 articles (gnus-uu-find-articles-matching
611 (gnus-uu-reginize-string subject))
612 total (nconc total articles)))
614 (gnus-summary-set-process-mark (car articles))
615 (setcdr marked (delq (car articles) (cdr marked)))
616 (setq articles (cdr articles)))
617 (setq marked (cdr marked)))
618 (setq gnus-newsgroup-processable (nreverse total)))
619 (gnus-summary-position-point)))
621 (defun gnus-uu-mark-all ()
622 "Mark all articles in \"series\" order."
624 (gnus-set-global-variables)
625 (setq gnus-newsgroup-processable nil)
627 (let ((data gnus-newsgroup-data)
630 (when (and (not (memq (setq number (gnus-data-number (car data)))
631 gnus-newsgroup-processable))
632 (vectorp (gnus-data-header (car data))))
633 (gnus-summary-goto-subject number)
634 (gnus-uu-mark-series))
635 (setq data (cdr data)))))
636 (gnus-summary-position-point))
638 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
640 (defun gnus-uu-decode-postscript (&optional n)
641 "Gets postscript of the current article."
643 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
645 (defun gnus-uu-decode-postscript-view (&optional n)
646 "Gets and views the current article."
648 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
649 (gnus-uu-decode-postscript n)))
651 (defun gnus-uu-decode-postscript-and-save (n dir)
652 "Extracts postscript and saves the current article."
654 (list current-prefix-arg
655 (file-name-as-directory
656 (read-file-name "Save in dir: "
658 gnus-uu-default-dir t))))
659 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
662 (defun gnus-uu-decode-postscript-and-save-view (n dir)
663 "Decodes, views and saves the resulting file."
665 (list current-prefix-arg
666 (read-file-name "Where do you want to save the file(s)? "
668 gnus-uu-default-dir t)))
669 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
670 (gnus-uu-decode-postscript-and-save n dir)))
673 ;; Internal functions.
675 (defun gnus-uu-decode-with-method (method n &optional save not-insert
677 (gnus-uu-initialize scan)
679 (setq gnus-uu-default-dir save))
680 ;; Create the directory we save to.
681 (when (and scan cdir save
682 (not (file-exists-p save)))
683 (make-directory save t))
684 (let ((articles (gnus-uu-get-list-of-articles n))
686 (setq files (gnus-uu-grab-articles articles method t))
687 (let ((gnus-current-article (car articles)))
689 (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
691 (gnus-uu-save-files files save))
692 (when (eq gnus-uu-do-not-unpack-archives nil)
693 (setq files (gnus-uu-unpack-files files)))
694 (setq files (nreverse (gnus-uu-get-actions files)))
695 (or not-insert (not gnus-insert-pseudo-articles)
696 (gnus-summary-insert-pseudos files save))))
698 (defun gnus-uu-scan-directory (dir &optional rec)
699 "Return a list of all files under DIR."
700 (let ((files (directory-files dir t))
702 (while (setq file (pop files))
703 (unless (member (file-name-nondirectory file) '("." ".."))
704 (push (list (cons 'name file)
705 (cons 'article gnus-current-article))
707 (when (file-directory-p file)
708 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
713 (defun gnus-uu-save-files (files dir)
715 (let ((len (length files))
716 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
717 to-file file fromdir)
718 (while (setq file (cdr (assq 'name (pop files))))
719 (when (file-exists-p file)
720 (string-match reg file)
721 (setq fromdir (substring file (match-end 0)))
722 (if (file-directory-p file)
723 (gnus-make-directory (concat dir fromdir))
724 (setq to-file (concat dir fromdir))
725 (when (or (not (file-exists-p to-file))
726 (eq gnus-uu-be-dangerous t)
727 (and gnus-uu-be-dangerous
728 (gnus-y-or-n-p (format "%s exists; overwrite? "
730 (copy-file file to-file t t)))))
731 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
733 ;; Functions for saving and possibly digesting articles without
736 ;; Function called by gnus-uu-grab-articles to treat each article.
737 (defun gnus-uu-save-article (buffer in-state)
739 (gnus-uu-save-separate-articles
743 (concat gnus-uu-saved-article-name gnus-current-article))
744 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
745 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
747 ((eq in-state 'last) (list 'end))
748 (t (list 'middle)))))
749 ((not gnus-uu-save-in-digest)
752 (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
753 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
754 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
756 ((eq in-state 'last) (list 'end))
757 (t (list 'middle)))))
759 (let ((header (gnus-summary-article-header)))
760 (push (cons (mail-header-from header)
761 (mail-header-subject header))
762 gnus-uu-digest-from-subject))
763 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
764 (delim (concat "^" (make-string 30 ?-) "$"))
765 beg subj headers headline sorthead body end-string state)
766 (if (or (eq in-state 'first)
767 (eq in-state 'first-and-last))
769 (setq state (list 'begin))
770 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
773 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
776 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
777 (current-time-string) name name))))
778 (when (not (eq in-state 'end))
779 (setq state (list 'middle))))
781 (set-buffer (get-buffer "*gnus-uu-body*"))
782 (goto-char (setq beg (point-max)))
786 (let (buffer-read-only)
787 (gnus-set-text-properties (point-min) (point-max) nil)
788 ;; These two are necessary for XEmacs 19.12 fascism.
789 (put-text-property (point-min) (point-max) 'invisible nil)
790 (put-text-property (point-min) (point-max) 'intangible nil))
791 (goto-char (point-min))
792 (re-search-forward "\n\n")
793 ;; Quote all 30-dash lines.
795 (while (re-search-forward delim nil t)
799 (setq body (buffer-substring (1- (point)) (point-max)))
800 (narrow-to-region (point-min) (point))
801 (if (not (setq headers gnus-uu-digest-headers))
802 (setq sorthead (buffer-substring (point-min) (point-max)))
804 (setq headline (car headers))
805 (setq headers (cdr headers))
806 (goto-char (point-min))
807 (while (re-search-forward headline nil t)
812 (or (and (re-search-forward "^[^ \t]" nil t)
814 (progn (forward-line 1) (point)))))))))
816 (insert sorthead) (goto-char (point-max))
817 (insert body) (goto-char (point-max))
818 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
820 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
821 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
823 (set-buffer (get-buffer "*gnus-uu-pre*"))
824 (insert (format " %s\n" subj)))))
825 (when (or (eq in-state 'last)
826 (eq in-state 'first-and-last))
828 (set-buffer (get-buffer "*gnus-uu-pre*"))
829 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
830 (gnus-write-buffer gnus-uu-saved-article-name))
832 (set-buffer (get-buffer "*gnus-uu-body*"))
833 (goto-char (point-max))
835 (concat (setq end-string (format "End of %s Digest" name))
837 (insert (concat (make-string (length end-string) ?*) "\n"))
839 (point-min) (point-max) gnus-uu-saved-article-name t))
840 (kill-buffer (get-buffer "*gnus-uu-pre*"))
841 (kill-buffer (get-buffer "*gnus-uu-body*"))
843 (if (memq 'begin state)
844 (cons gnus-uu-saved-article-name state)
847 ;; Binhex treatment - not very advanced.
849 (defconst gnus-uu-binhex-body-line
850 "^[^:]...............................................................$")
851 (defconst gnus-uu-binhex-begin-line
852 "^:...............................................................$")
853 (defconst gnus-uu-binhex-end-line
856 (defun gnus-uu-binhex-article (buffer in-state)
857 (let (state start-char)
861 (goto-char (point-min))
862 (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
863 (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
864 (setq state (list 'wrong-type))))
866 (if (memq 'wrong-type state)
869 (setq start-char (point))
870 (if (looking-at gnus-uu-binhex-begin-line)
872 (setq state (list 'begin))
873 (write-region 1 1 gnus-uu-binhex-article-name))
874 (setq state (list 'middle)))
875 (goto-char (point-max))
876 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
877 gnus-uu-binhex-end-line)
879 (when (looking-at gnus-uu-binhex-end-line)
880 (setq state (if (memq 'begin state)
885 (when (file-exists-p gnus-uu-binhex-article-name)
886 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
887 (if (memq 'begin state)
888 (cons gnus-uu-binhex-article-name state)
893 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
894 (let ((state (list 'ok))
895 start-char end-char file-name)
897 (set-buffer process-buffer)
898 (goto-char (point-min))
899 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
900 (setq state (list 'wrong-type))
902 (setq start-char (point))
903 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
904 (setq state (list 'wrong-type))
905 (setq end-char (point))
906 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
907 (insert-buffer-substring process-buffer start-char end-char)
908 (setq file-name (concat gnus-uu-work-dir
909 (cdr gnus-article-current) ".ps"))
910 (write-region (point-min) (point-max) file-name)
911 (setq state (list file-name 'begin 'end)))))
917 (defun gnus-uu-get-actions (files)
921 (setq name (cdr (assq 'name (car files))))
923 (setq action (gnus-uu-get-action name))
924 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
925 (cons 'action "file")
926 (cons 'action action))
927 (cons 'execute (gnus-uu-command
930 (setq files (cdr files)))
933 (defun gnus-uu-get-action (file-name)
936 (gnus-uu-choose-action
939 gnus-uu-user-view-rules
940 (if gnus-uu-ignore-default-view-rules
942 gnus-uu-default-view-rules)
943 gnus-uu-user-view-rules-end)))
944 (when (and (not (string= (or action "") "gnus-uu-archive"))
945 gnus-uu-view-with-metamail)
947 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
948 (setq action (format "metamail -d -b -c \"%s\"" action))))
952 ;; Functions for treating subjects and collecting series.
954 (defun gnus-uu-reginize-string (string)
955 ;; Takes a string and puts a \ in front of every special character;
956 ;; ignores any leading "version numbers" thingies that they use in
957 ;; the comp.binaries groups, and either replaces anything that looks
958 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
959 ;; like that, replaces the last two numbers with "[0-9]+". This, in
960 ;; my experience, should get most postings of a series.
962 (vernum "v[0-9]+[a-z][0-9]+:")
965 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
966 (buffer-disable-undo (current-buffer))
968 (insert (regexp-quote string))
971 (setq case-fold-search nil)
972 (goto-char (point-min))
973 (when (looking-at vernum)
974 (replace-match vernum t t)
975 (setq beg (length vernum)))
978 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
979 (replace-match " [0-9]+/[0-9]+")
982 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
983 (replace-match "[0-9]+ of [0-9]+")
986 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
989 (< 1 (goto-char (1- (point))))))
990 (re-search-forward "[0-9]+" nil t)
991 (replace-match "[0-9]+")
993 (setq count (1- count)))))
996 (while (re-search-forward "[ \t]+" nil t)
997 (replace-match "[ \t]*" t t))
999 (buffer-substring 1 (point-max)))))
1001 (defun gnus-uu-get-list-of-articles (n)
1002 ;; If N is non-nil, the article numbers of the N next articles
1003 ;; will be returned.
1004 ;; If any articles have been marked as processable, they will be
1006 ;; Failing that, articles that have subjects that are part of the
1007 ;; same "series" as the current will be returned.
1011 (setq n (prefix-numeric-value n))
1012 (let ((backward (< n 0))
1016 (push (gnus-summary-article-number)
1018 (gnus-summary-search-forward nil nil backward))
1020 (nreverse articles)))
1021 (gnus-newsgroup-processable
1022 (reverse gnus-newsgroup-processable))
1024 (gnus-uu-find-articles-matching)))))
1026 (defun gnus-uu-string< (l1 l2)
1027 (string< (car l1) (car l2)))
1029 (defun gnus-uu-find-articles-matching
1030 (&optional subject only-unread do-not-translate)
1031 ;; Finds all articles that matches the regexp SUBJECT. If it is
1032 ;; nil, the current article name will be used. If ONLY-UNREAD is
1033 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1034 ;; non-nil, article names are not equalized before sorting.
1035 (let ((subject (or subject
1036 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1041 ;; Collect all subjects matching subject.
1042 (let ((case-fold-search t)
1043 (data gnus-newsgroup-data)
1047 (and (not (gnus-data-pseudo-p d))
1048 (or (not only-unread)
1049 (= (setq mark (gnus-data-mark d))
1051 (= mark gnus-ticked-mark)
1052 (= mark gnus-dormant-mark))
1053 (setq subj (mail-header-subject (gnus-data-header d)))
1054 (string-match subject subj)
1055 (push (cons subj (gnus-data-number d))
1056 list-of-subjects))))
1058 ;; Expand numbers, sort, and return the list of article
1060 (mapcar (lambda (sub) (cdr sub))
1061 (sort (gnus-uu-expand-numbers
1063 (not do-not-translate))
1064 'gnus-uu-string<))))))
1066 (defun gnus-uu-expand-numbers (string-list &optional translate)
1067 ;; Takes a list of strings and "expands" all numbers in all the
1068 ;; strings. That is, this function makes all numbers equal length by
1069 ;; prepending lots of zeroes before each number. This is to ease later
1070 ;; sorting to find out what sequence the articles are supposed to be
1071 ;; decoded in. Returns the list of expanded strings.
1072 (let ((out-list string-list)
1075 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1076 (buffer-disable-undo (current-buffer))
1079 (insert (caar string-list))
1080 ;; Translate multiple spaces to one space.
1081 (goto-char (point-min))
1082 (while (re-search-forward "[ \t]+" nil t)
1083 (replace-match " "))
1084 ;; Translate all characters to "a".
1085 (goto-char (point-min))
1087 (while (re-search-forward "[A-Za-z]" nil t)
1088 (replace-match "a" t t)))
1090 (goto-char (point-min))
1091 (while (re-search-forward "[0-9]+" nil t)
1094 (string-to-int (buffer-substring
1095 (match-beginning 0) (match-end 0))))))
1096 (setq string (buffer-substring 1 (point-max)))
1097 (setcar (car string-list) string)
1098 (setq string-list (cdr string-list))))
1102 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1103 ;; function. It takes a list of articles to be grabbed and a function
1104 ;; to apply to each article.
1106 ;; The function to be called should take two parameters. The first
1107 ;; parameter is the article buffer. The function should leave the
1108 ;; result, if any, in this buffer. Most treatment functions will just
1109 ;; generate files...
1111 ;; The second parameter is the state of the list of articles, and can
1112 ;; have four values: `first', `middle', `last' and `first-and-last'.
1114 ;; The function should return a list. The list may contain the
1115 ;; following symbols:
1116 ;; `error' if an error occurred
1117 ;; `begin' if the beginning of an encoded file has been received
1118 ;; If the list returned contains a `begin', the first element of
1119 ;; the list *must* be a string with the file name of the decoded
1121 ;; `end' if the end of an encoded file has been received
1122 ;; `middle' if the article was a body part of an encoded file
1123 ;; `wrong-type' if the article was not a part of an encoded file
1124 ;; `ok', which can be used everything is ok
1126 (defvar gnus-uu-has-been-grabbed nil)
1128 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1130 (if (not (and gnus-uu-has-been-grabbed
1131 gnus-uu-unmark-articles-not-decoded))
1133 (when dont-unmark-last-article
1134 (setq art (car gnus-uu-has-been-grabbed))
1135 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1136 (while gnus-uu-has-been-grabbed
1137 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1138 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1139 (when dont-unmark-last-article
1140 (setq gnus-uu-has-been-grabbed (list art))))))
1142 ;; This function takes a list of articles and a function to apply to
1143 ;; each article grabbed.
1145 ;; This function returns a list of files decoded if the grabbing and
1146 ;; the process-function has been successful and nil otherwise.
1147 (defun gnus-uu-grab-articles (articles process-function
1148 &optional sloppy limit no-errors)
1149 (let ((state 'first)
1150 (gnus-asynchronous nil)
1151 has-been-begin article result-file result-files process-state
1152 gnus-summary-display-article-function
1153 gnus-article-display-hook gnus-article-prepare-hook
1154 article-series files)
1156 (while (and articles
1157 (not (memq 'error process-state))
1159 (not (memq 'end process-state))))
1161 (setq article (pop articles))
1162 (push article article-series)
1165 (if (eq state 'first)
1166 (setq state 'first-and-last)
1167 (setq state 'last)))
1169 (let ((part (gnus-uu-part-number article)))
1170 (gnus-message 6 "Getting article %d%s..."
1171 article (if (string= part "") "" (concat ", " part))))
1172 (gnus-summary-display-article article)
1174 ;; Push the article to the processing function.
1176 (set-buffer gnus-original-article-buffer)
1177 (let ((buffer-read-only nil))
1179 (set-buffer gnus-summary-buffer)
1181 (funcall process-function
1182 gnus-original-article-buffer state)))))
1184 (gnus-summary-remove-process-mark article)
1186 ;; If this is the beginning of a decoded file, we push it
1188 (when (or (memq 'begin process-state)
1189 (and (or (eq state 'first)
1190 (eq state 'first-and-last))
1191 (memq 'ok process-state)))
1192 (when has-been-begin
1193 ;; If there is a `result-file' here, that means that the
1194 ;; file was unsuccessfully decoded, so we delete it.
1195 (when (and result-file
1196 (file-exists-p result-file)
1197 (not gnus-uu-be-dangerous)
1198 (or (eq gnus-uu-be-dangerous t)
1200 (format "Delete unsuccessfully decoded file %s"
1202 (delete-file result-file)))
1203 (when (memq 'begin process-state)
1204 (setq result-file (car process-state)))
1205 (setq has-been-begin t))
1207 ;; Check whether we have decoded one complete file.
1208 (when (memq 'end process-state)
1209 (setq article-series nil)
1210 (setq has-been-begin nil)
1211 (if (stringp result-file)
1212 (setq files (list result-file))
1213 (setq files result-file))
1214 (setq result-file (car files))
1216 (push (list (cons 'name (pop files))
1217 (cons 'article article))
1219 ;; Allow user-defined functions to be run on this file.
1220 (when gnus-uu-grabbed-file-functions
1221 (let ((funcs gnus-uu-grabbed-file-functions))
1222 (unless (listp funcs)
1223 (setq funcs (list funcs)))
1225 (funcall (pop funcs) result-file))))
1226 (setq result-file nil)
1227 ;; Check whether we have decoded enough articles.
1228 (and limit (= (length result-files) limit)
1229 (setq articles nil)))
1231 ;; If this is the last article to be decoded, and
1232 ;; we still haven't reached the end, then we delete
1233 ;; the partially decoded file.
1234 (and (or (eq state 'last) (eq state 'first-and-last))
1235 (not (memq 'end process-state))
1237 (file-exists-p result-file)
1238 (not gnus-uu-be-dangerous)
1239 (or (eq gnus-uu-be-dangerous t)
1240 (gnus-y-or-n-p (format "Delete incomplete file %s? " result-file)))
1241 (delete-file result-file))
1243 ;; If this was a file of the wrong sort, then
1244 (when (and (or (memq 'wrong-type process-state)
1245 (memq 'error process-state))
1246 gnus-uu-unmark-articles-not-decoded)
1247 (gnus-summary-tick-article article t))
1249 ;; Set the new series state.
1250 (if (and (not has-been-begin)
1252 (or (memq 'end process-state)
1253 (memq 'middle process-state)))
1255 (setq process-state (list 'error))
1256 (gnus-message 2 "No begin part at the beginning")
1258 (setq state 'middle)))
1260 ;; When there are no result-files, then something must be wrong.
1264 ((not has-been-begin)
1265 (gnus-message 2 "Wrong type file"))
1266 ((memq 'error process-state)
1267 (gnus-message 2 "An error occurred during decoding"))
1268 ((not (or (memq 'ok process-state)
1269 (memq 'end process-state)))
1270 (gnus-message 2 "End of articles reached before end of file")))
1271 ;; Make unsuccessfully decoded articles unread.
1272 (when gnus-uu-unmark-articles-not-decoded
1273 (while article-series
1274 (gnus-summary-tick-article (pop article-series) t))))
1278 (defun gnus-uu-grab-view (file)
1279 "View FILE using the gnus-uu methods."
1280 (let ((action (gnus-uu-get-action file)))
1281 (gnus-execute-command
1282 (if (string-match "%" action)
1283 (format action file)
1284 (concat action " " file))
1285 (eq gnus-view-pseudos 'not-confirm))))
1287 (defun gnus-uu-grab-move (file)
1288 "Move FILE to somewhere."
1289 (when gnus-uu-default-dir
1290 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1291 (file-name-nondirectory file))))
1292 (rename-file file to-file)
1293 (unless (file-exists-p file)
1294 (make-symbolic-link to-file file)))))
1296 (defun gnus-uu-part-number (article)
1297 (let* ((header (gnus-summary-article-header article))
1298 (subject (and header (mail-header-subject header))))
1300 (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+" subject))
1301 (match-string 0 subject)
1304 (defun gnus-uu-uudecode-sentinel (process event)
1305 (delete-process (get-process process)))
1307 (defun gnus-uu-uustrip-article (process-buffer in-state)
1308 ;; Uudecodes a file asynchronously.
1310 (set-buffer process-buffer)
1311 (let ((state (list 'wrong-type))
1312 process-connection-type case-fold-search buffer-read-only
1314 (goto-char (point-min))
1316 ;; Deal with ^M at the end of the lines.
1317 (when gnus-uu-kill-carriage-return
1319 (while (search-forward "\r" nil t)
1320 (delete-backward-char 1))))
1322 (while (or (re-search-forward gnus-uu-begin-string nil t)
1323 (re-search-forward gnus-uu-body-line nil t))
1324 (setq state (list 'ok))
1325 ;; Ok, we are at the first uucoded line.
1327 (setq start-char (point))
1329 (if (not (looking-at gnus-uu-begin-string))
1330 (setq state (list 'middle))
1331 ;; This is the beginning of an uuencoded article.
1332 ;; We replace certain characters that could make things messy.
1333 (setq gnus-uu-file-name
1334 (let ((nnheader-file-name-translation-alist
1335 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1336 (nnheader-translate-file-chars (match-string 1))))
1338 ;; Remove any non gnus-uu-body-line right after start.
1340 (while (and (not (eobp))
1341 (not (looking-at gnus-uu-body-line)))
1344 ;; If a process is running, we kill it.
1345 (when (and gnus-uu-uudecode-process
1346 (memq (process-status gnus-uu-uudecode-process)
1348 (delete-process gnus-uu-uudecode-process)
1349 (gnus-uu-unmark-list-of-grabbed t))
1351 ;; Start a new uudecoding process.
1352 (let ((cdir default-directory))
1355 (cd gnus-uu-work-dir)
1356 (setq gnus-uu-uudecode-process
1359 (get-buffer-create gnus-uu-output-buffer-name)
1360 shell-file-name shell-command-switch
1361 (format "cd %s %s uudecode" gnus-uu-work-dir
1362 gnus-shell-command-separator))))
1364 (set-process-sentinel
1365 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1366 (setq state (list 'begin))
1367 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1369 ;; We look for the end of the thing to be decoded.
1370 (if (re-search-forward gnus-uu-end-string nil t)
1372 (goto-char (point-max))
1373 (re-search-backward gnus-uu-body-line nil t))
1377 (when gnus-uu-uudecode-process
1378 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1379 ;; Try to correct mishandled uucode.
1380 (when gnus-uu-correct-stripped-uucode
1381 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1383 ;; Send the text to the process.
1385 (process-send-region
1386 gnus-uu-uudecode-process start-char (point))
1389 (delete-process gnus-uu-uudecode-process)
1390 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1391 (setq state (list 'wrong-type)))))
1393 (if (memq 'end state)
1395 ;; Send an EOF, just in case.
1397 (process-send-eof gnus-uu-uudecode-process)
1399 (while (memq (process-status gnus-uu-uudecode-process)
1401 (accept-process-output gnus-uu-uudecode-process 1)))
1402 (when (or (not gnus-uu-uudecode-process)
1403 (not (memq (process-status gnus-uu-uudecode-process)
1405 (setq state (list 'wrong-type)))))))
1407 (if (memq 'begin state)
1408 (cons (if (= (length files) 1) (car files) files) state)
1411 ;; This function is used by `gnus-uu-grab-articles' to treat
1412 ;; a shared article.
1413 (defun gnus-uu-unshar-article (process-buffer in-state)
1414 (let ((state (list 'ok))
1417 (set-buffer process-buffer)
1418 (goto-char (point-min))
1419 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1420 (setq state (list 'wrong-type))
1422 (setq start-char (point))
1423 (call-process-region
1424 start-char (point-max) shell-file-name nil
1425 (get-buffer-create gnus-uu-output-buffer-name) nil
1426 shell-command-switch
1427 (concat "cd " gnus-uu-work-dir " "
1428 gnus-shell-command-separator " sh"))))
1431 ;; Returns the name of what the shar file is going to unpack.
1432 (defun gnus-uu-find-name-in-shar ()
1433 (let ((oldpoint (point))
1435 (goto-char (point-min))
1436 (when (re-search-forward gnus-uu-shar-name-marker nil t)
1437 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1438 (goto-char oldpoint)
1441 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1442 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1443 ;; found, or the name of the command to run if such a rule is found.
1444 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1445 (let ((action-list (copy-sequence file-action-list))
1446 (case-fold-search t)
1451 (and gnus-uu-ignore-files-by-name
1452 (string-match gnus-uu-ignore-files-by-name file-name)))
1454 (and gnus-uu-ignore-files-by-type
1455 (string-match gnus-uu-ignore-files-by-type
1456 (or (gnus-uu-choose-action
1457 file-name gnus-uu-ext-to-mime-list t)
1459 (while (not (or (eq action-list ()) action))
1460 (setq rule (car action-list))
1461 (setq action-list (cdr action-list))
1462 (when (string-match (car rule) file-name)
1463 (setq action (cadr rule)))))
1466 (defun gnus-uu-treat-archive (file-path)
1467 ;; Unpacks an archive. Returns t if unpacking is successful.
1468 (let ((did-unpack t)
1470 (setq action (gnus-uu-choose-action
1471 file-path (append gnus-uu-user-archive-rules
1472 (if gnus-uu-ignore-default-archive-rules
1474 gnus-uu-default-archive-rules))))
1477 (error "No unpackers for the file %s" file-path))
1479 (string-match "/[^/]*$" file-path)
1480 (setq dir (substring file-path 0 (match-beginning 0)))
1482 (when (member action gnus-uu-destructive-archivers)
1483 (copy-file file-path (concat file-path "~") t))
1485 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1488 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1491 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1493 (if (= 0 (call-process shell-file-name nil
1494 (get-buffer-create gnus-uu-output-buffer-name)
1495 nil shell-command-switch command))
1497 (gnus-message 2 "Error during unpacking of archive")
1498 (setq did-unpack nil))
1500 (when (member action gnus-uu-destructive-archivers)
1501 (rename-file (concat file-path "~") file-path t))
1505 (defun gnus-uu-dir-files (dir)
1506 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1509 (if (file-directory-p (setq file (car dirs)))
1510 (setq files (append files (gnus-uu-dir-files file)))
1512 (setq dirs (cdr dirs)))
1515 (defun gnus-uu-unpack-files (files &optional ignore)
1516 ;; Go through FILES and look for files to unpack.
1517 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1521 (setq file (cdr (assq 'name (car files))))
1522 (when (and (not (member file ignore))
1523 (equal (gnus-uu-get-action (file-name-nondirectory file))
1525 (push file did-unpack)
1526 (unless (gnus-uu-treat-archive file)
1527 (gnus-message 2 "Error during unpacking of %s" file))
1528 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1531 (unless (member (car nfiles) totfiles)
1532 (push (list (cons 'name (car nfiles))
1533 (cons 'original file))
1535 (setq nfiles (cdr nfiles)))
1536 (setq totfiles newfiles)))
1537 (setq files (cdr files)))
1539 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1542 (defun gnus-uu-ls-r (dir)
1543 (let* ((files (gnus-uu-directory-files dir t))
1546 (when (file-directory-p (car files))
1547 (setq ofiles (delete (car files) ofiles))
1548 (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1549 (setq files (cdr files)))
1554 (defun gnus-uu-directory-files (dir &optional full)
1555 (let (files out file)
1556 (setq files (directory-files dir full))
1558 (setq file (car files))
1559 (setq files (cdr files))
1560 (unless (member (file-name-nondirectory file) '("." ".."))
1562 (setq out (nreverse out))
1565 (defun gnus-uu-check-correct-stripped-uucode (start end)
1567 (let (found beg length)
1568 (if (not gnus-uu-correct-stripped-uucode)
1572 (if (re-search-forward " \\|`" end t)
1577 (when (looking-at "\n")
1582 (if (looking-at (concat gnus-uu-begin-string "\\|"
1583 gnus-uu-end-string))
1589 (setq length (- (point) beg)))
1594 (when (not (= length (- (point) beg)))
1595 (insert (make-string (- length (- (point) beg)) ? ))))
1596 (forward-line 1)))))))
1598 (defvar gnus-uu-tmp-alist nil)
1600 (defun gnus-uu-initialize (&optional scan)
1603 (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1604 (if (file-exists-p (cdr entry))
1605 (setq gnus-uu-work-dir (cdr entry))
1606 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1609 (setq gnus-uu-tmp-dir (file-name-as-directory
1610 (expand-file-name gnus-uu-tmp-dir)))
1611 (if (not (file-directory-p gnus-uu-tmp-dir))
1612 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1613 (when (not (file-writable-p gnus-uu-tmp-dir))
1614 (error "Temp directory %s can't be written to"
1617 (setq gnus-uu-work-dir
1618 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1619 (gnus-make-directory gnus-uu-work-dir)
1620 (set-file-modes gnus-uu-work-dir 448)
1621 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1622 (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1623 gnus-uu-tmp-alist))))
1626 ;; Kills the temporary uu buffers, kills any processes, etc.
1627 (defun gnus-uu-clean-up ()
1629 (and gnus-uu-uudecode-process
1630 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1632 (delete-process gnus-uu-uudecode-process))
1633 (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1634 (kill-buffer buf))))
1636 ;; Inputs an action and a file and returns a full command, putting
1637 ;; quotes round the file name and escaping any quotes in the file name.
1638 (defun gnus-uu-command (action file)
1640 (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1643 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1644 (substring file (match-beginning 0) (match-end 0))))
1645 (setq file (substring file (1+ (match-beginning 0))))))
1646 (setq ofile (concat "\"" ofile file "\""))
1647 (if (string-match "%s" action)
1648 (format action ofile)
1649 (concat action " " ofile))))
1651 (defun gnus-uu-delete-work-dir (&optional dir)
1652 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1654 (gnus-message 7 "Deleting directory %s..." dir)
1655 (setq dir gnus-uu-work-dir))
1657 (file-exists-p dir))
1658 (let ((files (directory-files dir t nil t))
1660 (while (setq file (pop files))
1661 (unless (member (file-name-nondirectory file) '("." ".."))
1662 (if (file-directory-p file)
1663 (gnus-uu-delete-work-dir file)
1664 (gnus-message 9 "Deleting file %s..." file)
1665 (delete-file file))))
1666 (delete-directory dir)))
1667 (gnus-message 7 ""))
1671 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1672 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1677 ;;; uuencoded posting
1680 ;; Any function that is to be used as and encoding method will take two
1681 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1682 ;; and "spiral.jpg", respectively.) The function should return nil if
1683 ;; the encoding wasn't successful.
1684 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1685 "Function used for encoding binary files.
1686 There are three functions supplied with gnus-uu for encoding files:
1687 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1688 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1689 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1690 uuencode and adds MIME headers.")
1692 (defvar gnus-uu-post-include-before-composing nil
1693 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1694 If this variable is t, you can either include an encoded file with
1695 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1697 (defvar gnus-uu-post-length 990
1698 "Maximum length of an article.
1699 The encoded file will be split into how many articles it takes to
1700 post the entire file.")
1702 (defvar gnus-uu-post-threaded nil
1703 "Non-nil means that gnus-uu will post the encoded file in a thread.
1704 This may not be smart, as no other decoder I have seen are able to
1705 follow threads when collecting uuencoded articles. (Well, I have seen
1706 one package that does that - gnus-uu, but somehow, I don't think that
1707 counts...) Default is nil.")
1709 (defvar gnus-uu-post-separate-description t
1710 "Non-nil means that the description will be posted in a separate article.
1711 The first article will typically be numbered (0/x). If this variable
1712 is nil, the description the user enters will be included at the
1713 beginning of the first article, which will be numbered (1/x). Default
1716 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1717 (defvar gnus-uu-post-message-id nil)
1718 (defvar gnus-uu-post-inserted-file-name nil)
1719 (defvar gnus-uu-winconf-post-news nil)
1721 (defun gnus-uu-post-news ()
1722 "Compose an article and post an encoded file."
1724 (setq gnus-uu-post-inserted-file-name nil)
1725 (setq gnus-uu-winconf-post-news (current-window-configuration))
1727 (gnus-summary-post-news)
1729 (use-local-map (copy-keymap (current-local-map)))
1730 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1731 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1732 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1733 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1735 (when gnus-uu-post-include-before-composing
1736 (save-excursion (setq gnus-uu-post-inserted-file-name
1737 (gnus-uu-post-insert-binary)))))
1739 (defun gnus-uu-post-insert-binary-in-article ()
1740 "Inserts an encoded file in the buffer.
1741 The user will be asked for a file name."
1744 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1746 ;; Encodes with uuencode and substitutes all spaces with backticks.
1747 (defun gnus-uu-post-encode-uuencode (path file-name)
1748 (when (gnus-uu-post-encode-file "uuencode" path file-name)
1749 (goto-char (point-min))
1751 (while (re-search-forward " " nil t)
1752 (replace-match "`"))
1755 ;; Encodes with uuencode and adds MIME headers.
1756 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1757 (when (gnus-uu-post-encode-uuencode path file-name)
1758 (gnus-uu-post-make-mime file-name "x-uue")
1761 ;; Encodes with base64 and adds MIME headers
1762 (defun gnus-uu-post-encode-mime (path file-name)
1763 (when (gnus-uu-post-encode-file "mmencode" path file-name)
1764 (gnus-uu-post-make-mime file-name "base64")
1767 ;; Adds MIME headers.
1768 (defun gnus-uu-post-make-mime (file-name encoding)
1769 (goto-char (point-min))
1770 (insert (format "Content-Type: %s; name=\"%s\"\n"
1771 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1773 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1775 (set-buffer gnus-message-buffer)
1776 (goto-char (point-min))
1777 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1779 (narrow-to-region 1 (point))
1780 (unless (mail-fetch-field "mime-version")
1782 (insert "MIME-Version: 1.0\n"))
1785 ;; Encodes a file PATH with COMMAND, leaving the result in the
1787 (defun gnus-uu-post-encode-file (command path file-name)
1788 (= 0 (call-process shell-file-name nil t nil shell-command-switch
1789 (format "%s %s %s" command path file-name))))
1791 (defun gnus-uu-post-news-inews ()
1792 "Posts the composed news article and encoded file.
1793 If no file has been included, the user will be asked for a file."
1798 (if gnus-uu-post-inserted-file-name
1799 (setq file-name gnus-uu-post-inserted-file-name)
1800 (setq file-name (gnus-uu-post-insert-binary)))
1802 (if gnus-uu-post-threaded
1803 (let ((message-required-news-headers
1804 (if (memq 'Message-ID message-required-news-headers)
1805 message-required-news-headers
1806 (cons 'Message-ID message-required-news-headers)))
1807 gnus-inews-article-hook)
1809 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1810 gnus-inews-article-hook
1811 (list gnus-inews-article-hook)))
1815 (goto-char (point-min))
1816 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1817 (setq gnus-uu-post-message-id
1819 (match-beginning 1) (match-end 1)))
1820 (setq gnus-uu-post-message-id nil))))
1821 gnus-inews-article-hook)
1822 (gnus-uu-post-encoded file-name t))
1823 (gnus-uu-post-encoded file-name nil)))
1824 (setq gnus-uu-post-inserted-file-name nil)
1825 (when gnus-uu-winconf-post-news
1826 (set-window-configuration gnus-uu-winconf-post-news)))
1828 ;; Asks for a file to encode, encodes it and inserts the result in
1829 ;; the current buffer. Returns the file name the user gave.
1830 (defun gnus-uu-post-insert-binary ()
1831 (let ((uuencode-buffer-name "*uuencode buffer*")
1832 file-path uubuf file-name)
1834 (setq file-path (read-file-name
1835 "What file do you want to encode? "))
1836 (when (not (file-exists-p file-path))
1837 (error "%s: No such file" file-path))
1839 (goto-char (point-max))
1840 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1842 (when (string-match "^~/" file-path)
1843 (setq file-path (concat "$HOME" (substring file-path 1))))
1844 (if (string-match "/[^/]*$" file-path)
1845 (setq file-name (substring file-path (1+ (match-beginning 0))))
1846 (setq file-name file-path))
1850 (set-buffer (setq uubuf
1851 (get-buffer-create uuencode-buffer-name)))
1853 (funcall gnus-uu-post-encode-method file-path file-name))
1854 (insert-buffer-substring uubuf)
1855 (error "Encoding unsuccessful"))
1856 (kill-buffer uubuf))
1859 ;; Posts the article and all of the encoded file.
1860 (defun gnus-uu-post-encoded (file-name &optional threaded)
1861 (let ((send-buffer-name "*uuencode send buffer*")
1862 (encoded-buffer-name "*encoded buffer*")
1863 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1864 (separator (concat mail-header-separator "\n\n"))
1865 uubuf length parts header i end beg
1866 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1868 (setq post-buf (current-buffer))
1870 (goto-char (point-min))
1871 (when (not (re-search-forward
1872 (if gnus-uu-post-separate-description
1873 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1875 (concat "^" (regexp-quote mail-header-separator) "$"))
1877 (error "Internal error: No binary/header separator"))
1880 (setq beg-binary (point))
1881 (setq end-binary (point-max))
1884 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1886 (insert-buffer-substring post-buf beg-binary end-binary)
1887 (goto-char (point-min))
1888 (setq length (count-lines 1 (point-max)))
1889 (setq parts (/ length gnus-uu-post-length))
1890 (when (not (< (% length gnus-uu-post-length) 4))
1891 (setq parts (1+ parts))))
1893 (when gnus-uu-post-separate-description
1895 (kill-region (point) (point-max))
1897 (goto-char (point-min))
1899 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1901 (setq header (buffer-substring 1 (point)))
1903 (goto-char (point-min))
1904 (if (not gnus-uu-post-separate-description)
1906 (when (and (not threaded) (re-search-forward "^Subject: " nil t))
1908 (insert (format " (0/%d)" parts)))
1914 (while (not (> i parts))
1915 (set-buffer (get-buffer-create send-buffer-name))
1918 (when (and threaded gnus-uu-post-message-id)
1919 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1922 (- 62 (length (format top-string "" file-name i parts ""))))
1923 (when (> 1 (setq minlen (/ whole-len 2)))
1928 (make-string minlen ?-)
1931 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1933 (goto-char (point-min))
1934 (if (not (re-search-forward "^Subject: " nil t))
1939 (insert (format " (%d/%d)" i parts)))
1940 (when (or (and (= i 2) gnus-uu-post-separate-description)
1941 (and (= i 1) (not gnus-uu-post-separate-description)))
1942 (replace-match "Subject: Re: "))))
1944 (goto-char (point-max))
1949 (goto-char (point-max))
1950 (forward-line gnus-uu-post-length))
1951 (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1954 (insert-buffer-substring uubuf beg end)
1959 (goto-char (point-min))
1961 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1964 (when (re-search-forward
1965 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1971 (let (message-sent-message-via)
1974 (when (setq buf (get-buffer send-buffer-name))
1976 (when (setq buf (get-buffer encoded-buffer-name))
1979 (when (not gnus-uu-post-separate-description)
1980 (set-buffer-modified-p nil)
1981 (when (fboundp 'bury-buffer)
1986 ;; gnus-uu.el ends here