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)
274 (gnus-uu-mark-map "P" gnus-summary-mark-map)
275 "p" gnus-summary-mark-as-processable
276 "u" gnus-summary-unmark-as-processable
277 "U" gnus-summary-unmark-all-processable
278 "v" gnus-uu-mark-over
279 "s" gnus-uu-mark-series
280 "r" gnus-uu-mark-region
281 "R" gnus-uu-mark-by-regexp
282 "t" gnus-uu-mark-thread
283 "T" gnus-uu-unmark-thread
285 "b" gnus-uu-mark-buffer
286 "S" gnus-uu-mark-sparse)
289 (gnus-uu-extract-map "X" gnus-summary-mode-map)
290 ;;"x" gnus-uu-extract-any
291 ;;"m" gnus-uu-extract-mime
292 "u" gnus-uu-decode-uu
293 "U" gnus-uu-decode-uu-and-save
294 "s" gnus-uu-decode-unshar
295 "S" gnus-uu-decode-unshar-and-save
296 "o" gnus-uu-decode-save
297 "O" gnus-uu-decode-save
298 "b" gnus-uu-decode-binhex
299 "B" gnus-uu-decode-binhex
300 "p" gnus-uu-decode-postscript
301 "P" gnus-uu-decode-postscript-and-save)
304 (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
305 "u" gnus-uu-decode-uu-view
306 "U" gnus-uu-decode-uu-and-save-view
307 "s" gnus-uu-decode-unshar-view
308 "S" gnus-uu-decode-unshar-and-save-view
309 "o" gnus-uu-decode-save-view
310 "O" gnus-uu-decode-save-view
311 "b" gnus-uu-decode-binhex-view
312 "B" gnus-uu-decode-binhex-view
313 "p" gnus-uu-decode-postscript-view
314 "P" gnus-uu-decode-postscript-and-save-view)
319 (defun gnus-uu-decode-uu (&optional n)
320 "Uudecodes the current article."
322 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
324 (defun gnus-uu-decode-uu-and-save (n dir)
325 "Decodes and saves the resulting file."
327 (list current-prefix-arg
328 (file-name-as-directory
329 (read-file-name "Uudecode and save in dir: "
331 gnus-uu-default-dir t))))
332 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
334 (defun gnus-uu-decode-unshar (&optional n)
335 "Unshars the current article."
337 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
339 (defun gnus-uu-decode-unshar-and-save (n dir)
340 "Unshars and saves the current article."
342 (list current-prefix-arg
343 (file-name-as-directory
344 (read-file-name "Unshar and save in dir: "
346 gnus-uu-default-dir t))))
347 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
349 (defun gnus-uu-decode-save (n file)
350 "Saves the current article."
352 (list current-prefix-arg
354 (if gnus-uu-save-separate-articles
355 "Save articles is dir: "
356 "Save articles in file: ")
358 gnus-uu-default-dir)))
359 (setq gnus-uu-saved-article-name file)
360 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
362 (defun gnus-uu-decode-binhex (n dir)
363 "Unbinhexes the current article."
365 (list current-prefix-arg
366 (file-name-as-directory
367 (read-file-name "Unbinhex and save in dir: "
369 gnus-uu-default-dir))))
370 (setq gnus-uu-binhex-article-name
371 (make-temp-name (concat gnus-uu-work-dir "binhex")))
372 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
374 (defun gnus-uu-decode-uu-view (&optional n)
375 "Uudecodes and views the current article."
377 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
378 (gnus-uu-decode-uu n)))
380 (defun gnus-uu-decode-uu-and-save-view (n dir)
381 "Decodes, views and saves the resulting file."
383 (list current-prefix-arg
384 (read-file-name "Uudecode, view and save in dir: "
386 gnus-uu-default-dir t)))
387 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
388 (gnus-uu-decode-uu-and-save n dir)))
390 (defun gnus-uu-decode-unshar-view (&optional n)
391 "Unshars and views the current article."
393 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
394 (gnus-uu-decode-unshar n)))
396 (defun gnus-uu-decode-unshar-and-save-view (n dir)
397 "Unshars and saves the current article."
399 (list current-prefix-arg
400 (read-file-name "Unshar, view and save in dir: "
402 gnus-uu-default-dir t)))
403 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
404 (gnus-uu-decode-unshar-and-save n dir)))
406 (defun gnus-uu-decode-save-view (n file)
407 "Saves and views the current article."
409 (list current-prefix-arg
410 (read-file-name (if gnus-uu-save-separate-articles
411 "Save articles is dir: "
412 "Save articles in file: ")
413 gnus-uu-default-dir gnus-uu-default-dir)))
414 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
415 (gnus-uu-decode-save n file)))
417 (defun gnus-uu-decode-binhex-view (n file)
418 "Unbinhexes and views the current article."
420 (list current-prefix-arg
421 (read-file-name "Unbinhex, view and save in dir: "
422 gnus-uu-default-dir gnus-uu-default-dir)))
423 (setq gnus-uu-binhex-article-name
424 (make-temp-name (concat gnus-uu-work-dir "binhex")))
425 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
426 (gnus-uu-decode-binhex n file)))
429 ;; Digest and forward articles
431 (defun gnus-uu-digest-mail-forward (&optional n post)
432 "Digests and forwards all articles in this series."
434 (let ((gnus-uu-save-in-digest t)
435 (file (make-temp-name (concat gnus-uu-tmp-dir "forward")))
437 (setq gnus-uu-digest-from-subject nil)
438 (gnus-uu-decode-save n file)
439 (setq buf (switch-to-buffer (get-buffer-create " *gnus-uu-forward*")))
440 (gnus-add-current-to-buffer-list)
442 (delete-other-windows)
444 (let ((fs gnus-uu-digest-from-subject))
448 subject (gnus-simplify-subject-fuzzy (cdar fs))
450 (while (and fs (or from subject))
452 (or (string= from (caar fs))
455 (or (string= (gnus-simplify-subject-fuzzy (cdar fs))
459 (or subject (setq subject "Digested Articles"))
460 (or from (setq from "Various")))
461 (goto-char (point-min))
462 (and (re-search-forward "^Subject: ")
464 (delete-region (point) (gnus-point-at-eol))
466 (goto-char (point-min))
467 (and (re-search-forward "^From: ")
469 (delete-region (point) (gnus-point-at-eol))
471 (message-forward post)
474 (setq gnus-uu-digest-from-subject nil)))
476 (defun gnus-uu-digest-post-forward (&optional n)
477 "Digest and forward to a newsgroup."
479 (gnus-uu-digest-mail-forward n t))
483 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
484 "Ask for a regular expression and set the process mark on all articles that match."
485 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
486 (gnus-set-global-variables)
487 (let ((articles (gnus-uu-find-articles-matching regexp)))
490 (gnus-summary-remove-process-mark (pop articles))
491 (gnus-summary-set-process-mark (pop articles))))
493 (gnus-summary-position-point))
495 (defun gnus-uu-unmark-by-regexp (regexp &optional unmark)
496 "Ask for a regular expression and remove the process mark on all articles that match."
497 (interactive (list (read-from-minibuffer "Mark (regexp): ")))
498 (gnus-uu-mark-by-regexp regexp t))
500 (defun gnus-uu-mark-series ()
501 "Mark the current series with the process mark."
503 (gnus-set-global-variables)
504 (let ((articles (gnus-uu-find-articles-matching)))
506 (gnus-summary-set-process-mark (car articles))
507 (setq articles (cdr articles)))
509 (gnus-summary-position-point))
511 (defun gnus-uu-mark-region (beg end &optional unmark)
512 "Set the process mark on all articles between point and mark."
514 (gnus-set-global-variables)
517 (while (< (point) end)
519 (gnus-summary-remove-process-mark (gnus-summary-article-number))
520 (gnus-summary-set-process-mark (gnus-summary-article-number)))
522 (gnus-summary-position-point))
524 (defun gnus-uu-unmark-region (beg end)
525 "Remove the process mark from all articles between point and mark."
527 (gnus-uu-mark-region beg end t))
529 (defun gnus-uu-mark-buffer ()
530 "Set the process mark on all articles in the buffer."
532 (gnus-uu-mark-region (point-min) (point-max)))
534 (defun gnus-uu-unmark-buffer ()
535 "Remove the process mark on all articles in the buffer."
537 (gnus-uu-mark-region (point-min) (point-max) t))
539 (defun gnus-uu-mark-thread ()
540 "Marks all articles downwards in this thread."
542 (gnus-set-global-variables)
543 (let ((level (gnus-summary-thread-level)))
544 (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
545 (zerop (gnus-summary-next-subject 1))
546 (> (gnus-summary-thread-level) level))))
547 (gnus-summary-position-point))
549 (defun gnus-uu-unmark-thread ()
550 "Unmarks all articles downwards in this thread."
552 (gnus-set-global-variables)
553 (let ((level (gnus-summary-thread-level)))
554 (while (and (gnus-summary-remove-process-mark
555 (gnus-summary-article-number))
556 (zerop (gnus-summary-next-subject 1))
557 (> (gnus-summary-thread-level) level))))
558 (gnus-summary-position-point))
560 (defun gnus-uu-mark-over (&optional score)
561 "Mark all articles with a score over SCORE (the prefix.)"
563 (let ((score (gnus-score-default score))
564 (data gnus-newsgroup-data))
567 (when (> (or (cdr (assq (gnus-data-number (car data))
568 gnus-newsgroup-scored))
569 gnus-summary-default-score 0)
571 (gnus-summary-set-process-mark (caar data)))
572 (setq data (cdr data))))
573 (gnus-summary-position-point)))
575 (defun gnus-uu-mark-sparse ()
576 "Mark all series that have some articles marked."
578 (gnus-set-global-variables)
579 (let ((marked (nreverse gnus-newsgroup-processable))
580 subject articles total headers)
581 (or marked (error "No articles marked with the process mark"))
582 (setq gnus-newsgroup-processable nil)
585 (and (vectorp (setq headers
586 (gnus-summary-article-header (car marked))))
587 (setq subject (mail-header-subject headers)
588 articles (gnus-uu-find-articles-matching
589 (gnus-uu-reginize-string subject))
590 total (nconc total articles)))
592 (gnus-summary-set-process-mark (car articles))
593 (setcdr marked (delq (car articles) (cdr marked)))
594 (setq articles (cdr articles)))
595 (setq marked (cdr marked)))
596 (setq gnus-newsgroup-processable (nreverse total)))
597 (gnus-summary-position-point)))
599 (defun gnus-uu-mark-all ()
600 "Mark all articles in \"series\" order."
602 (gnus-set-global-variables)
603 (setq gnus-newsgroup-processable nil)
605 (let ((data gnus-newsgroup-data)
608 (when (and (not (memq (setq number (gnus-data-number (car data)))
609 gnus-newsgroup-processable))
610 (vectorp (gnus-data-header (car data))))
611 (gnus-summary-goto-subject number)
612 (gnus-uu-mark-series))
613 (setq data (cdr data)))))
614 (gnus-summary-position-point))
616 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
618 (defun gnus-uu-decode-postscript (&optional n)
619 "Gets postscript of the current article."
621 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
623 (defun gnus-uu-decode-postscript-view (&optional n)
624 "Gets and views the current article."
626 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
627 (gnus-uu-decode-postscript n)))
629 (defun gnus-uu-decode-postscript-and-save (n dir)
630 "Extracts postscript and saves the current article."
632 (list current-prefix-arg
633 (file-name-as-directory
634 (read-file-name "Save in dir: "
636 gnus-uu-default-dir t))))
637 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
640 (defun gnus-uu-decode-postscript-and-save-view (n dir)
641 "Decodes, views and saves the resulting file."
643 (list current-prefix-arg
644 (read-file-name "Where do you want to save the file(s)? "
646 gnus-uu-default-dir t)))
647 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
648 (gnus-uu-decode-postscript-and-save n dir)))
651 ;; Internal functions.
653 (defun gnus-uu-decode-with-method (method n &optional save not-insert
655 (gnus-uu-initialize scan)
656 (if save (setq gnus-uu-default-dir save))
657 ;; Create the directory we save to.
658 (when (and scan cdir save
659 (not (file-exists-p save)))
660 (make-directory save t))
661 (let ((articles (gnus-uu-get-list-of-articles n))
663 (setq files (gnus-uu-grab-articles articles method t))
664 (let ((gnus-current-article (car articles)))
665 (and scan (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
666 (and save (gnus-uu-save-files files save))
667 (if (eq gnus-uu-do-not-unpack-archives nil)
668 (setq files (gnus-uu-unpack-files files)))
669 (setq files (nreverse (gnus-uu-get-actions files)))
670 (or not-insert (not gnus-insert-pseudo-articles)
671 (gnus-summary-insert-pseudos files save))))
673 (defun gnus-uu-scan-directory (dir &optional rec)
674 "Return a list of all files under DIR."
675 (let ((files (directory-files dir t))
677 (while (setq file (pop files))
678 (unless (member (file-name-nondirectory file) '("." ".."))
679 (push (list (cons 'name file)
680 (cons 'article gnus-current-article))
682 (when (file-directory-p file)
683 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
688 (defun gnus-uu-save-files (files dir)
690 (let ((len (length files))
691 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
692 to-file file fromdir)
693 (while (setq file (cdr (assq 'name (pop files))))
694 (when (file-exists-p file)
695 (string-match reg file)
696 (setq fromdir (substring file (match-end 0)))
697 (if (file-directory-p file)
698 (unless (file-exists-p (concat dir fromdir))
699 (make-directory (concat dir fromdir) t))
700 (setq to-file (concat dir fromdir))
701 (when (or (not (file-exists-p to-file))
702 (gnus-y-or-n-p (format "%s exists; overwrite? " to-file)))
703 (copy-file file to-file t t)))))
704 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
706 ;; Functions for saving and possibly digesting articles without
709 ;; Function called by gnus-uu-grab-articles to treat each article.
710 (defun gnus-uu-save-article (buffer in-state)
712 (gnus-uu-save-separate-articles
715 (write-region 1 (point-max) (concat gnus-uu-saved-article-name
716 gnus-current-article))
717 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
718 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
720 ((eq in-state 'last) (list 'end))
721 (t (list 'middle)))))
722 ((not gnus-uu-save-in-digest)
725 (write-region 1 (point-max) gnus-uu-saved-article-name t)
726 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
727 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
729 ((eq in-state 'last) (list 'end))
730 (t (list 'middle)))))
732 (let ((header (gnus-summary-article-header)))
733 (setq gnus-uu-digest-from-subject
734 (cons (cons (mail-header-from header)
735 (mail-header-subject header))
736 gnus-uu-digest-from-subject)))
737 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
738 (delim (concat "^" (make-string 30 ?-) "$"))
739 beg subj headers headline sorthead body end-string state)
740 (if (or (eq in-state 'first)
741 (eq in-state 'first-and-last))
743 (setq state (list 'begin))
744 (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
747 (set-buffer (get-buffer-create "*gnus-uu-pre*"))
750 "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
751 (current-time-string) name name))))
752 (if (not (eq in-state 'end))
753 (setq state (list 'middle))))
755 (set-buffer (get-buffer "*gnus-uu-body*"))
756 (goto-char (setq beg (point-max)))
760 (let (buffer-read-only)
761 (gnus-set-text-properties (point-min) (point-max) nil)
762 ;; These two are necessary for XEmacs 19.12 fascism.
763 (put-text-property (point-min) (point-max) 'invisible nil)
764 (put-text-property (point-min) (point-max) 'intangible nil))
765 (goto-char (point-min))
766 (re-search-forward "\n\n")
767 ;; Quote all 30-dash lines.
769 (while (re-search-forward delim nil t)
773 (setq body (buffer-substring (1- (point)) (point-max)))
774 (narrow-to-region (point-min) (point))
775 (if (not (setq headers gnus-uu-digest-headers))
776 (setq sorthead (buffer-substring (point-min) (point-max)))
778 (setq headline (car headers))
779 (setq headers (cdr headers))
780 (goto-char (point-min))
781 (while (re-search-forward headline nil t)
786 (or (and (re-search-forward "^[^ \t]" nil t)
788 (progn (forward-line 1) (point)))))))))
790 (insert sorthead) (goto-char (point-max))
791 (insert body) (goto-char (point-max))
792 (insert (concat "\n" (make-string 30 ?-) "\n\n"))
794 (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
796 (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
798 (set-buffer (get-buffer "*gnus-uu-pre*"))
799 (insert (format " %s\n" subj))))))
800 (if (or (eq in-state 'last)
801 (eq in-state 'first-and-last))
804 (set-buffer (get-buffer "*gnus-uu-pre*"))
805 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
806 (write-region 1 (point-max) gnus-uu-saved-article-name))
808 (set-buffer (get-buffer "*gnus-uu-body*"))
809 (goto-char (point-max))
811 (concat (setq end-string (format "End of %s Digest" name))
813 (insert (concat (make-string (length end-string) ?*) "\n"))
814 (write-region 1 (point-max) gnus-uu-saved-article-name t))
815 (kill-buffer (get-buffer "*gnus-uu-pre*"))
816 (kill-buffer (get-buffer "*gnus-uu-body*"))
817 (setq state (cons 'end state))))
818 (if (memq 'begin state)
819 (cons gnus-uu-saved-article-name state)
822 ;; Binhex treatment - not very advanced.
824 (defconst gnus-uu-binhex-body-line
825 "^[^:]...............................................................$")
826 (defconst gnus-uu-binhex-begin-line
827 "^:...............................................................$")
828 (defconst gnus-uu-binhex-end-line
831 (defun gnus-uu-binhex-article (buffer in-state)
832 (let (state start-char)
836 (goto-char (point-min))
837 (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
838 (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
839 (setq state (list 'wrong-type))))
841 (if (memq 'wrong-type state)
844 (setq start-char (point))
845 (if (looking-at gnus-uu-binhex-begin-line)
847 (setq state (list 'begin))
848 (write-region 1 1 gnus-uu-binhex-article-name))
849 (setq state (list 'middle)))
850 (goto-char (point-max))
851 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
852 gnus-uu-binhex-end-line) nil t)
853 (if (looking-at gnus-uu-binhex-end-line)
854 (setq state (if (memq 'begin state)
859 (if (file-exists-p gnus-uu-binhex-article-name)
860 (append-to-file start-char (point) gnus-uu-binhex-article-name))))
861 (if (memq 'begin state)
862 (cons gnus-uu-binhex-article-name state)
867 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
868 (let ((state (list 'ok))
869 start-char end-char file-name)
871 (set-buffer process-buffer)
872 (goto-char (point-min))
873 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
874 (setq state (list 'wrong-type))
876 (setq start-char (point))
877 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
878 (setq state (list 'wrong-type))
879 (setq end-char (point))
880 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
881 (insert-buffer-substring process-buffer start-char end-char)
882 (setq file-name (concat gnus-uu-work-dir
883 (cdr gnus-article-current) ".ps"))
884 (write-region (point-min) (point-max) file-name)
885 (setq state (list file-name 'begin 'end)))))
891 (defun gnus-uu-get-actions (files)
895 (setq name (cdr (assq 'name (car files))))
897 (setq action (gnus-uu-get-action name))
898 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
899 (cons 'action "file")
900 (cons 'action action))
901 (cons 'execute (gnus-uu-command
904 (setq files (cdr files)))
907 (defun gnus-uu-get-action (file-name)
910 (gnus-uu-choose-action
913 gnus-uu-user-view-rules
914 (if gnus-uu-ignore-default-view-rules
916 gnus-uu-default-view-rules)
917 gnus-uu-user-view-rules-end)))
918 (if (and (not (string= (or action "") "gnus-uu-archive"))
919 gnus-uu-view-with-metamail)
921 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
922 (setq action (format "metamail -d -b -c \"%s\"" action))))
926 ;; Functions for treating subjects and collecting series.
928 (defun gnus-uu-reginize-string (string)
929 ;; Takes a string and puts a \ in front of every special character;
930 ;; ignores any leading "version numbers" thingies that they use in
931 ;; the comp.binaries groups, and either replaces anything that looks
932 ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
933 ;; like that, replaces the last two numbers with "[0-9]+". This, in
934 ;; my experience, should get most postings of a series.
936 (vernum "v[0-9]+[a-z][0-9]+:")
939 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
940 (buffer-disable-undo (current-buffer))
942 (insert (regexp-quote string))
945 (setq case-fold-search nil)
946 (goto-char (point-min))
947 (if (looking-at vernum)
949 (replace-match vernum t t)
950 (setq beg (length vernum))))
953 (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
954 (replace-match " [0-9]+/[0-9]+")
957 (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
958 (replace-match "[0-9]+ of [0-9]+")
961 (while (and (re-search-backward "[0-9]" nil t) (> count 0))
964 (< 1 (goto-char (1- (point))))))
965 (re-search-forward "[0-9]+" nil t)
966 (replace-match "[0-9]+")
968 (setq count (1- count)))))
971 (while (re-search-forward "[ \t]+" nil t)
972 (replace-match "[ \t]*" t t))
974 (buffer-substring 1 (point-max)))))
976 (defun gnus-uu-get-list-of-articles (n)
977 ;; If N is non-nil, the article numbers of the N next articles
979 ;; If any articles have been marked as processable, they will be
981 ;; Failing that, articles that have subjects that are part of the
982 ;; same "series" as the current will be returned.
986 (let ((backward (< n 0))
990 (setq articles (cons (gnus-summary-article-number)
992 (gnus-summary-search-forward nil nil backward))
994 (nreverse articles)))
995 (gnus-newsgroup-processable
996 (reverse gnus-newsgroup-processable))
998 (gnus-uu-find-articles-matching)))))
1000 (defun gnus-uu-string< (l1 l2)
1001 (string< (car l1) (car l2)))
1003 (defun gnus-uu-find-articles-matching
1004 (&optional subject only-unread do-not-translate)
1005 ;; Finds all articles that matches the regexp SUBJECT. If it is
1006 ;; nil, the current article name will be used. If ONLY-UNREAD is
1007 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1008 ;; non-nil, article names are not equalized before sorting.
1009 (let ((subject (or subject
1010 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1015 ;; Collect all subjects matching subject.
1016 (let ((case-fold-search t)
1017 (data gnus-newsgroup-data)
1021 (and (not (gnus-data-pseudo-p d))
1022 (or (not only-unread)
1023 (= (setq mark (gnus-data-mark d))
1025 (= mark gnus-ticked-mark)
1026 (= mark gnus-dormant-mark))
1027 (setq subj (mail-header-subject (gnus-data-header d)))
1028 (string-match subject subj)
1029 (setq list-of-subjects
1030 (cons (cons subj (gnus-data-number d))
1031 list-of-subjects)))))
1033 ;; Expand numbers, sort, and return the list of article
1035 (mapcar (lambda (sub) (cdr sub))
1036 (sort (gnus-uu-expand-numbers
1038 (not do-not-translate))
1039 'gnus-uu-string<))))))
1041 (defun gnus-uu-expand-numbers (string-list &optional translate)
1042 ;; Takes a list of strings and "expands" all numbers in all the
1043 ;; strings. That is, this function makes all numbers equal length by
1044 ;; prepending lots of zeroes before each number. This is to ease later
1045 ;; sorting to find out what sequence the articles are supposed to be
1046 ;; decoded in. Returns the list of expanded strings.
1047 (let ((out-list string-list)
1050 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1051 (buffer-disable-undo (current-buffer))
1054 (insert (caar string-list))
1055 ;; Translate multiple spaces to one space.
1056 (goto-char (point-min))
1057 (while (re-search-forward "[ \t]+" nil t)
1058 (replace-match " "))
1059 ;; Translate all characters to "a".
1060 (goto-char (point-min))
1062 (while (re-search-forward "[A-Za-z]" nil t)
1063 (replace-match "a" t t)))
1065 (goto-char (point-min))
1066 (while (re-search-forward "[0-9]+" nil t)
1069 (string-to-int (buffer-substring
1070 (match-beginning 0) (match-end 0))))))
1071 (setq string (buffer-substring 1 (point-max)))
1072 (setcar (car string-list) string)
1073 (setq string-list (cdr string-list))))
1077 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1078 ;; function. It takes a list of articles to be grabbed and a function
1079 ;; to apply to each article.
1081 ;; The function to be called should take two parameters. The first
1082 ;; parameter is the article buffer. The function should leave the
1083 ;; result, if any, in this buffer. Most treatment functions will just
1084 ;; generate files...
1086 ;; The second parameter is the state of the list of articles, and can
1087 ;; have four values: `first', `middle', `last' and `first-and-last'.
1089 ;; The function should return a list. The list may contain the
1090 ;; following symbols:
1091 ;; `error' if an error occurred
1092 ;; `begin' if the beginning of an encoded file has been received
1093 ;; If the list returned contains a `begin', the first element of
1094 ;; the list *must* be a string with the file name of the decoded
1096 ;; `end' if the end of an encoded file has been received
1097 ;; `middle' if the article was a body part of an encoded file
1098 ;; `wrong-type' if the article was not a part of an encoded file
1099 ;; `ok', which can be used everything is ok
1101 (defvar gnus-uu-has-been-grabbed nil)
1103 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1105 (if (not (and gnus-uu-has-been-grabbed
1106 gnus-uu-unmark-articles-not-decoded))
1108 (if dont-unmark-last-article
1110 (setq art (car gnus-uu-has-been-grabbed))
1111 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
1112 (while gnus-uu-has-been-grabbed
1113 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1114 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1115 (if dont-unmark-last-article
1116 (setq gnus-uu-has-been-grabbed (list art))))))
1118 ;; This function takes a list of articles and a function to apply to
1119 ;; each article grabbed.
1121 ;; This function returns a list of files decoded if the grabbing and
1122 ;; the process-function has been successful and nil otherwise.
1123 (defun gnus-uu-grab-articles (articles process-function
1124 &optional sloppy limit no-errors)
1125 (let ((state 'first)
1126 has-been-begin article result-file result-files process-state
1127 gnus-summary-display-article-function
1128 gnus-article-display-hook gnus-article-prepare-hook
1129 article-series files)
1131 (while (and articles
1132 (not (memq 'error process-state))
1134 (not (memq 'end process-state))))
1136 (setq article (pop articles))
1137 (push article article-series)
1140 (if (eq state 'first)
1141 (setq state 'first-and-last)
1142 (setq state 'last)))
1144 (let ((part (gnus-uu-part-number article)))
1145 (gnus-message 6 "Getting article %d%s..."
1146 article (if (string= part "") "" (concat ", " part))))
1147 (gnus-summary-display-article article)
1149 ;; Push the article to the processing function.
1151 (set-buffer gnus-original-article-buffer)
1152 (let ((buffer-read-only nil))
1154 (set-buffer gnus-summary-buffer)
1156 (funcall process-function
1157 gnus-original-article-buffer state)))))
1159 (gnus-summary-remove-process-mark article)
1161 ;; If this is the beginning of a decoded file, we push it
1163 (when (or (memq 'begin process-state)
1164 (and (or (eq state 'first)
1165 (eq state 'first-and-last))
1166 (memq 'ok process-state)))
1168 ;; If there is a `result-file' here, that means that the
1169 ;; file was unsuccessfully decoded, so we delete it.
1170 (when (and result-file
1171 (file-exists-p result-file))
1172 (delete-file result-file)))
1173 (when (memq 'begin process-state)
1174 (setq result-file (car process-state)))
1175 (setq has-been-begin t))
1177 ;; Check whether we have decoded one complete file.
1178 (when (memq 'end process-state)
1179 (setq article-series nil)
1180 (setq has-been-begin nil)
1181 (if (stringp result-file)
1182 (setq files (list result-file))
1183 (setq files result-file))
1184 (setq result-file (car files))
1186 (push (list (cons 'name (pop files))
1187 (cons 'article article))
1189 ;; Allow user-defined functions to be run on this file.
1190 (when gnus-uu-grabbed-file-functions
1191 (let ((funcs gnus-uu-grabbed-file-functions))
1192 (unless (listp funcs)
1193 (setq funcs (list funcs)))
1195 (funcall (pop funcs) result-file))))
1196 ;; Check whether we have decoded enough articles.
1197 (and limit (= (length result-files) limit)
1198 (setq articles nil)))
1200 ;; If this is the last article to be decoded, and
1201 ;; we still haven't reached the end, then we delete
1202 ;; the partially decoded file.
1203 (and (or (eq state 'last) (eq state 'first-and-last))
1204 (not (memq 'end process-state))
1206 (file-exists-p result-file)
1207 (delete-file result-file))
1209 ;; If this was a file of the wrong sort, then
1210 (when (and (or (memq 'wrong-type process-state)
1211 (memq 'error process-state))
1212 gnus-uu-unmark-articles-not-decoded)
1213 (gnus-summary-tick-article article t))
1215 ;; Set the new series state.
1216 (if (and (not has-been-begin)
1218 (or (memq 'end process-state)
1219 (memq 'middle process-state)))
1221 (setq process-state (list 'error))
1222 (gnus-message 2 "No begin part at the beginning")
1224 (setq state 'middle)))
1226 ;; When there are no result-files, then something must be wrong.
1230 ((not has-been-begin)
1231 (gnus-message 2 "Wrong type file"))
1232 ((memq 'error process-state)
1233 (gnus-message 2 "An error occurred during decoding"))
1234 ((not (or (memq 'ok process-state)
1235 (memq 'end process-state)))
1236 (gnus-message 2 "End of articles reached before end of file")))
1237 ;; Make unsuccessfully decoded articles unread.
1238 (when gnus-uu-unmark-articles-not-decoded
1239 (while article-series
1240 (gnus-summary-tick-article (pop article-series) t))))
1244 (defun gnus-uu-grab-view (file)
1245 "View FILE using the gnus-uu methods."
1246 (let ((action (gnus-uu-get-action file)))
1247 (gnus-execute-command
1248 (if (string-match "%" action)
1249 (format action file)
1250 (concat action " " file))
1251 (eq gnus-view-pseudos 'not-confirm))))
1253 (defun gnus-uu-grab-move (file)
1254 "Move FILE to somewhere."
1255 (when gnus-uu-default-dir
1256 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1257 (file-name-nondirectory file))))
1258 (rename-file file to-file)
1259 (unless (file-exists-p file)
1260 (make-symbolic-link to-file file)))))
1262 (defun gnus-uu-part-number (article)
1263 (let* ((header (gnus-summary-article-header article))
1264 (subject (and header (mail-header-subject header))))
1266 (string-match "[0-9]+ */[0-9]+\\|[0-9]+ * of *[0-9]+" subject))
1267 (match-string 0 subject)
1270 (defun gnus-uu-uudecode-sentinel (process event)
1271 (delete-process (get-process process)))
1273 (defun gnus-uu-uustrip-article (process-buffer in-state)
1274 ;; Uudecodes a file asynchronously.
1276 (set-buffer process-buffer)
1277 (let ((state (list 'wrong-type))
1278 process-connection-type case-fold-search buffer-read-only
1280 (goto-char (point-min))
1282 ;; Deal with ^M at the end of the lines.
1283 (when gnus-uu-kill-carriage-return
1285 (while (search-forward "\r" nil t)
1286 (delete-backward-char 1))))
1288 (while (or (re-search-forward gnus-uu-begin-string nil t)
1289 (re-search-forward gnus-uu-body-line nil t))
1290 (setq state (list 'ok))
1291 ;; Ok, we are at the first uucoded line.
1293 (setq start-char (point))
1295 (if (not (looking-at gnus-uu-begin-string))
1296 (setq state (list 'middle))
1297 ;; This is the beginning of an uuencoded article.
1298 ;; We replace certain characters that could make things messy.
1299 (setq gnus-uu-file-name
1300 (let ((nnheader-file-name-translation-alist
1301 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
1302 (nnheader-translate-file-chars (match-string 1))))
1304 ;; Remove any non gnus-uu-body-line right after start.
1306 (while (and (not (eobp))
1307 (not (looking-at gnus-uu-body-line)))
1310 ;; If a process is running, we kill it.
1311 (when (and gnus-uu-uudecode-process
1312 (memq (process-status gnus-uu-uudecode-process)
1314 (delete-process gnus-uu-uudecode-process)
1315 (gnus-uu-unmark-list-of-grabbed t))
1317 ;; Start a new uudecoding process.
1318 (let ((cdir default-directory))
1321 (cd gnus-uu-work-dir)
1322 (setq gnus-uu-uudecode-process
1325 (get-buffer-create gnus-uu-output-buffer-name)
1326 shell-file-name shell-command-switch
1327 (format "cd %s %s uudecode" gnus-uu-work-dir
1328 gnus-shell-command-separator))))
1330 (set-process-sentinel
1331 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1332 (setq state (list 'begin))
1333 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1335 ;; We look for the end of the thing to be decoded.
1336 (if (re-search-forward gnus-uu-end-string nil t)
1337 (setq state (cons 'end state))
1338 (goto-char (point-max))
1339 (re-search-backward gnus-uu-body-line nil t))
1343 (when gnus-uu-uudecode-process
1344 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1345 ;; Try to correct mishandled uucode.
1346 (when gnus-uu-correct-stripped-uucode
1347 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1349 ;; Send the text to the process.
1351 (process-send-region
1352 gnus-uu-uudecode-process start-char (point))
1355 (delete-process gnus-uu-uudecode-process)
1356 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1357 (setq state (list 'wrong-type)))))
1359 (if (memq 'end state)
1361 ;; Send an EOF, just in case.
1363 (process-send-eof gnus-uu-uudecode-process)
1365 (while (memq (process-status gnus-uu-uudecode-process)
1367 (accept-process-output gnus-uu-uudecode-process 1)))
1368 (when (or (not gnus-uu-uudecode-process)
1369 (not (memq (process-status gnus-uu-uudecode-process)
1371 (setq state (list 'wrong-type)))))))
1373 (if (memq 'begin state)
1374 (cons (if (= (length files) 1) (car files) files) state)
1377 ;; This function is used by `gnus-uu-grab-articles' to treat
1378 ;; a shared article.
1379 (defun gnus-uu-unshar-article (process-buffer in-state)
1380 (let ((state (list 'ok))
1383 (set-buffer process-buffer)
1384 (goto-char (point-min))
1385 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1386 (setq state (list 'wrong-type))
1388 (setq start-char (point))
1389 (call-process-region
1390 start-char (point-max) shell-file-name nil
1391 (get-buffer-create gnus-uu-output-buffer-name) nil
1392 shell-command-switch (concat "cd " gnus-uu-work-dir " ; sh"))))
1395 ;; Returns the name of what the shar file is going to unpack.
1396 (defun gnus-uu-find-name-in-shar ()
1397 (let ((oldpoint (point))
1399 (goto-char (point-min))
1400 (if (re-search-forward gnus-uu-shar-name-marker nil t)
1401 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1402 (goto-char oldpoint)
1405 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1406 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1407 ;; found, or the name of the command to run if such a rule is found.
1408 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1409 (let ((action-list (copy-sequence file-action-list))
1410 (case-fold-search t)
1415 (and gnus-uu-ignore-files-by-name
1416 (string-match gnus-uu-ignore-files-by-name file-name)))
1418 (and gnus-uu-ignore-files-by-type
1419 (string-match gnus-uu-ignore-files-by-type
1420 (or (gnus-uu-choose-action
1421 file-name gnus-uu-ext-to-mime-list t)
1423 (while (not (or (eq action-list ()) action))
1424 (setq rule (car action-list))
1425 (setq action-list (cdr action-list))
1426 (if (string-match (car rule) file-name)
1427 (setq action (cadr rule)))))
1430 (defun gnus-uu-treat-archive (file-path)
1431 ;; Unpacks an archive. Returns t if unpacking is successful.
1432 (let ((did-unpack t)
1434 (setq action (gnus-uu-choose-action
1435 file-path (append gnus-uu-user-archive-rules
1436 (if gnus-uu-ignore-default-archive-rules
1438 gnus-uu-default-archive-rules))))
1440 (if (not action) (error "No unpackers for the file %s" file-path))
1442 (string-match "/[^/]*$" file-path)
1443 (setq dir (substring file-path 0 (match-beginning 0)))
1445 (if (member action gnus-uu-destructive-archivers)
1446 (copy-file file-path (concat file-path "~") t))
1448 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1451 (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1454 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1456 (if (= 0 (call-process shell-file-name nil
1457 (get-buffer-create gnus-uu-output-buffer-name)
1458 nil shell-command-switch command))
1460 (gnus-message 2 "Error during unpacking of archive")
1461 (setq did-unpack nil))
1463 (if (member action gnus-uu-destructive-archivers)
1464 (rename-file (concat file-path "~") file-path t))
1468 (defun gnus-uu-dir-files (dir)
1469 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1472 (if (file-directory-p (setq file (car dirs)))
1473 (setq files (append files (gnus-uu-dir-files file)))
1474 (setq files (cons file files)))
1475 (setq dirs (cdr dirs)))
1478 (defun gnus-uu-unpack-files (files &optional ignore)
1479 ;; Go through FILES and look for files to unpack.
1480 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1484 (setq file (cdr (assq 'name (car files))))
1485 (if (and (not (member file ignore))
1486 (equal (gnus-uu-get-action (file-name-nondirectory file))
1489 (setq did-unpack (cons file did-unpack))
1490 (or (gnus-uu-treat-archive file)
1491 (gnus-message 2 "Error during unpacking of %s" file))
1492 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1495 (or (member (car nfiles) totfiles)
1496 (setq ofiles (cons (list (cons 'name (car nfiles))
1497 (cons 'original file))
1499 (setq nfiles (cdr nfiles)))
1500 (setq totfiles newfiles))))
1501 (setq files (cdr files)))
1503 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1506 (defun gnus-uu-ls-r (dir)
1507 (let* ((files (gnus-uu-directory-files dir t))
1510 (if (file-directory-p (car files))
1512 (setq ofiles (delete (car files) ofiles))
1513 (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1514 (setq files (cdr files)))
1519 (defun gnus-uu-directory-files (dir &optional full)
1520 (let (files out file)
1521 (setq files (directory-files dir full))
1523 (setq file (car files))
1524 (setq files (cdr files))
1525 (or (member (file-name-nondirectory file) '("." ".."))
1526 (setq out (cons file out))))
1527 (setq out (nreverse out))
1530 (defun gnus-uu-check-correct-stripped-uucode (start end)
1532 (let (found beg length)
1533 (if (not gnus-uu-correct-stripped-uucode)
1537 (if (re-search-forward " \\|`" end t)
1542 (if (looking-at "\n") (replace-match ""))
1546 (if (looking-at (concat gnus-uu-begin-string "\\|"
1547 gnus-uu-end-string))
1554 (setq length (- (point) beg))))
1559 (if (not (= length (- (point) beg)))
1560 (insert (make-string (- length (- (point) beg)) ? ))))
1561 (forward-line 1)))))))
1563 (defvar gnus-uu-tmp-alist nil)
1565 (defun gnus-uu-initialize (&optional scan)
1568 (if (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1569 (if (file-exists-p (cdr entry))
1570 (setq gnus-uu-work-dir (cdr entry))
1571 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1574 (setq gnus-uu-tmp-dir (file-name-as-directory
1575 (expand-file-name gnus-uu-tmp-dir)))
1576 (if (not (file-directory-p gnus-uu-tmp-dir))
1577 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1578 (if (not (file-writable-p gnus-uu-tmp-dir))
1579 (error "Temp directory %s can't be written to"
1582 (setq gnus-uu-work-dir
1583 (make-temp-name (concat gnus-uu-tmp-dir "gnus")))
1584 (if (not (file-directory-p gnus-uu-work-dir))
1585 (make-directory gnus-uu-work-dir t))
1586 (set-file-modes gnus-uu-work-dir 448)
1587 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1588 (setq gnus-uu-tmp-alist (cons (cons gnus-newsgroup-name gnus-uu-work-dir)
1589 gnus-uu-tmp-alist)))))
1592 ;; Kills the temporary uu buffers, kills any processes, etc.
1593 (defun gnus-uu-clean-up ()
1595 (and gnus-uu-uudecode-process
1596 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1598 (delete-process gnus-uu-uudecode-process))
1599 (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1600 (kill-buffer buf))))
1602 ;; Inputs an action and a file and returns a full command, putting
1603 ;; quotes round the file name and escaping any quotes in the file name.
1604 (defun gnus-uu-command (action file)
1606 (while (string-match "!\\|`\\|\"\\|\\$\\|\\\\\\|&" file)
1609 (concat ofile (substring file 0 (match-beginning 0)) "\\"
1610 (substring file (match-beginning 0) (match-end 0))))
1611 (setq file (substring file (1+ (match-beginning 0))))))
1612 (setq ofile (concat "\"" ofile file "\""))
1613 (if (string-match "%s" action)
1614 (format action ofile)
1615 (concat action " " ofile))))
1617 (defun gnus-uu-delete-work-dir (&optional dir)
1618 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1620 (gnus-message 7 "Deleting directory %s..." dir)
1621 (setq dir gnus-uu-work-dir))
1623 (file-exists-p dir))
1624 (let ((files (directory-files dir t nil t))
1626 (while (setq file (pop files))
1627 (unless (member (file-name-nondirectory file) '("." ".."))
1628 (if (file-directory-p file)
1629 (gnus-uu-delete-work-dir file)
1630 (gnus-message 9 "Deleting file %s..." file)
1631 (delete-file file))))
1632 (delete-directory dir)))
1633 (gnus-message 7 ""))
1637 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1638 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1643 ;;; uuencoded posting
1646 ;; Any function that is to be used as and encoding method will take two
1647 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1648 ;; and "spiral.jpg", respectively.) The function should return nil if
1649 ;; the encoding wasn't successful.
1650 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1651 "Function used for encoding binary files.
1652 There are three functions supplied with gnus-uu for encoding files:
1653 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1654 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1655 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1656 uuencode and adds MIME headers.")
1658 (defvar gnus-uu-post-include-before-composing nil
1659 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1660 If this variable is t, you can either include an encoded file with
1661 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1663 (defvar gnus-uu-post-length 990
1664 "Maximum length of an article.
1665 The encoded file will be split into how many articles it takes to
1666 post the entire file.")
1668 (defvar gnus-uu-post-threaded nil
1669 "Non-nil means that gnus-uu will post the encoded file in a thread.
1670 This may not be smart, as no other decoder I have seen are able to
1671 follow threads when collecting uuencoded articles. (Well, I have seen
1672 one package that does that - gnus-uu, but somehow, I don't think that
1673 counts...) Default is nil.")
1675 (defvar gnus-uu-post-separate-description t
1676 "Non-nil means that the description will be posted in a separate article.
1677 The first article will typically be numbered (0/x). If this variable
1678 is nil, the description the user enters will be included at the
1679 beginning of the first article, which will be numbered (1/x). Default
1682 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1683 (defvar gnus-uu-post-message-id nil)
1684 (defvar gnus-uu-post-inserted-file-name nil)
1685 (defvar gnus-uu-winconf-post-news nil)
1687 (defun gnus-uu-post-news ()
1688 "Compose an article and post an encoded file."
1690 (setq gnus-uu-post-inserted-file-name nil)
1691 (setq gnus-uu-winconf-post-news (current-window-configuration))
1693 (gnus-summary-post-news)
1695 (use-local-map (copy-keymap (current-local-map)))
1696 (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1697 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1698 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1699 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1701 (if gnus-uu-post-include-before-composing
1702 (save-excursion (setq gnus-uu-post-inserted-file-name
1703 (gnus-uu-post-insert-binary)))))
1705 (defun gnus-uu-post-insert-binary-in-article ()
1706 "Inserts an encoded file in the buffer.
1707 The user will be asked for a file name."
1710 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1712 ;; Encodes with uuencode and substitutes all spaces with backticks.
1713 (defun gnus-uu-post-encode-uuencode (path file-name)
1714 (if (gnus-uu-post-encode-file "uuencode" path file-name)
1716 (goto-char (point-min))
1718 (while (re-search-forward " " nil t)
1719 (replace-match "`"))
1722 ;; Encodes with uuencode and adds MIME headers.
1723 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1724 (if (gnus-uu-post-encode-uuencode path file-name)
1726 (gnus-uu-post-make-mime file-name "x-uue")
1729 ;; Encodes with base64 and adds MIME headers
1730 (defun gnus-uu-post-encode-mime (path file-name)
1731 (if (gnus-uu-post-encode-file "mmencode" path file-name)
1733 (gnus-uu-post-make-mime file-name "base64")
1736 ;; Adds MIME headers.
1737 (defun gnus-uu-post-make-mime (file-name encoding)
1738 (goto-char (point-min))
1739 (insert (format "Content-Type: %s; name=\"%s\"\n"
1740 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1742 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1744 (set-buffer gnus-message-buffer)
1745 (goto-char (point-min))
1746 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1748 (narrow-to-region 1 (point))
1749 (or (mail-fetch-field "mime-version")
1752 (insert "MIME-Version: 1.0\n")))
1755 ;; Encodes a file PATH with COMMAND, leaving the result in the
1757 (defun gnus-uu-post-encode-file (command path file-name)
1758 (= 0 (call-process shell-file-name nil t nil shell-command-switch
1759 (format "%s %s %s" command path file-name))))
1761 (defun gnus-uu-post-news-inews ()
1762 "Posts the composed news article and encoded file.
1763 If no file has been included, the user will be asked for a file."
1768 (if gnus-uu-post-inserted-file-name
1769 (setq file-name gnus-uu-post-inserted-file-name)
1770 (setq file-name (gnus-uu-post-insert-binary)))
1772 (if gnus-uu-post-threaded
1773 (let ((message-required-news-headers
1774 (if (memq 'Message-ID message-required-news-headers)
1775 message-required-news-headers
1776 (cons 'Message-ID message-required-news-headers)))
1777 gnus-inews-article-hook)
1779 (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1780 gnus-inews-article-hook
1781 (list gnus-inews-article-hook)))
1782 (setq gnus-inews-article-hook
1786 (goto-char (point-min))
1787 (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1788 (setq gnus-uu-post-message-id
1790 (match-beginning 1) (match-end 1)))
1791 (setq gnus-uu-post-message-id nil))))
1792 gnus-inews-article-hook))
1793 (gnus-uu-post-encoded file-name t))
1794 (gnus-uu-post-encoded file-name nil)))
1795 (setq gnus-uu-post-inserted-file-name nil)
1796 (and gnus-uu-winconf-post-news
1797 (set-window-configuration gnus-uu-winconf-post-news)))
1799 ;; Asks for a file to encode, encodes it and inserts the result in
1800 ;; the current buffer. Returns the file name the user gave.
1801 (defun gnus-uu-post-insert-binary ()
1802 (let ((uuencode-buffer-name "*uuencode buffer*")
1803 file-path uubuf file-name)
1805 (setq file-path (read-file-name
1806 "What file do you want to encode? "))
1807 (if (not (file-exists-p file-path))
1808 (error "%s: No such file" file-path))
1810 (goto-char (point-max))
1811 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1813 (if (string-match "^~/" file-path)
1814 (setq file-path (concat "$HOME" (substring file-path 1))))
1815 (if (string-match "/[^/]*$" file-path)
1816 (setq file-name (substring file-path (1+ (match-beginning 0))))
1817 (setq file-name file-path))
1821 (set-buffer (setq uubuf
1822 (get-buffer-create uuencode-buffer-name)))
1824 (funcall gnus-uu-post-encode-method file-path file-name))
1825 (insert-buffer-substring uubuf)
1826 (error "Encoding unsuccessful"))
1827 (kill-buffer uubuf))
1830 ;; Posts the article and all of the encoded file.
1831 (defun gnus-uu-post-encoded (file-name &optional threaded)
1832 (let ((send-buffer-name "*uuencode send buffer*")
1833 (encoded-buffer-name "*encoded buffer*")
1834 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1835 (separator (concat mail-header-separator "\n\n"))
1836 uubuf length parts header i end beg
1837 beg-line minlen buf post-buf whole-len beg-binary end-binary)
1839 (setq post-buf (current-buffer))
1841 (goto-char (point-min))
1842 (if (not (re-search-forward
1843 (if gnus-uu-post-separate-description
1844 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
1846 (concat "^" (regexp-quote mail-header-separator) "$")) nil t))
1847 (error "Internal error: No binary/header separator"))
1850 (setq beg-binary (point))
1851 (setq end-binary (point-max))
1854 (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1856 (insert-buffer-substring post-buf beg-binary end-binary)
1857 (goto-char (point-min))
1858 (setq length (count-lines 1 (point-max)))
1859 (setq parts (/ length gnus-uu-post-length))
1860 (if (not (< (% length gnus-uu-post-length) 4))
1861 (setq parts (1+ parts))))
1863 (if gnus-uu-post-separate-description
1865 (kill-region (point) (point-max))
1867 (goto-char (point-min))
1869 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1871 (setq header (buffer-substring 1 (point)))
1873 (goto-char (point-min))
1874 (if (not gnus-uu-post-separate-description)
1876 (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1879 (insert (format " (0/%d)" parts))))
1885 (while (not (> i parts))
1886 (set-buffer (get-buffer-create send-buffer-name))
1889 (if (and threaded gnus-uu-post-message-id)
1890 (insert (format "References: %s\n" gnus-uu-post-message-id)))
1893 (- 62 (length (format top-string "" file-name i parts ""))))
1894 (if (> 1 (setq minlen (/ whole-len 2)))
1899 (make-string minlen ?-)
1902 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1904 (goto-char (point-min))
1905 (if (not (re-search-forward "^Subject: " nil t))
1910 (insert (format " (%d/%d)" i parts)))
1911 (if (or (and (= i 2) gnus-uu-post-separate-description)
1912 (and (= i 1) (not gnus-uu-post-separate-description)))
1913 (replace-match "Subject: Re: "))))
1915 (goto-char (point-max))
1920 (goto-char (point-max))
1921 (forward-line gnus-uu-post-length))
1922 (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1925 (insert-buffer-substring uubuf beg end)
1930 (goto-char (point-min))
1932 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
1935 (if (re-search-forward
1936 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
1943 (let (message-sent-message-via)
1946 (and (setq buf (get-buffer send-buffer-name))
1948 (and (setq buf (get-buffer encoded-buffer-name))
1951 (if (not gnus-uu-post-separate-description)
1953 (set-buffer-modified-p nil)
1954 (and (fboundp 'bury-buffer) (bury-buffer))))))
1958 ;; gnus-uu.el ends here