1c2cc680e83bb581a779ce59bc2720810a6136f4
[bpt/emacs.git] / lisp / gnus / gnus-uu.el
1 ;;; gnus-uu.el --- extract (uu)encoded files in Gnus
2
3 ;; Copyright (C) 1985-1987, 1993-1998, 2000-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Created: 2 Oct 1993
7 ;; Keyword: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-art)
32 (require 'message)
33 (require 'gnus-msg)
34 (require 'mm-decode)
35 (require 'yenc)
36
37 (defgroup gnus-extract nil
38 "Extracting encoded files."
39 :prefix "gnus-uu-"
40 :group 'gnus)
41
42 (defgroup gnus-extract-view nil
43 "Viewwing extracted files."
44 :group 'gnus-extract)
45
46 (defgroup gnus-extract-archive nil
47 "Extracting encoded archives."
48 :group 'gnus-extract)
49
50 (defgroup gnus-extract-post nil
51 "Extracting encoded archives."
52 :prefix "gnus-uu-post"
53 :group 'gnus-extract)
54
55 ;; Default viewing action rules
56
57 (defcustom gnus-uu-default-view-rules
58 '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed 's/\r$//'")
59 ("\\.pas$" "cat %s | sed 's/\r$//'")
60 ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
61 ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "display")
62 ("\\.tga$" "tgatoppm %s | ee -")
63 ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$"
64 "sox -v .5 %s -t .au -u - > /dev/audio")
65 ("\\.au$" "cat %s > /dev/audio")
66 ("\\.midi?$" "playmidi -f")
67 ("\\.mod$" "str32")
68 ("\\.ps$" "ghostview")
69 ("\\.dvi$" "xdvi")
70 ("\\.html$" "xmosaic")
71 ("\\.mpe?g$" "mpeg_play")
72 ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
73 ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$"
74 "gnus-uu-archive"))
75 "*Default actions to be taken when the user asks to view a file.
76 To change the behavior, you can either edit this variable or set
77 `gnus-uu-user-view-rules' to something useful.
78
79 For example:
80
81 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
82 following in your .emacs file:
83
84 (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
85
86 Both these variables are lists of lists with two string elements. The
87 first string is a regular expression. If the file name matches this
88 regular expression, the command in the second string is executed with
89 the file as an argument.
90
91 If the command string contains \"%s\", the file name will be inserted
92 at that point in the command string. If there's no \"%s\" in the
93 command string, the file name will be appended to the command string
94 before executing.
95
96 There are several user variables to tailor the behavior of gnus-uu to
97 your needs. First we have `gnus-uu-user-view-rules', which is the
98 variable gnus-uu first consults when trying to decide how to view a
99 file. If this variable contains no matches, gnus-uu examines the
100 default rule variable provided in this package. If gnus-uu finds no
101 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
102 match."
103 :group 'gnus-extract-view
104 :type '(repeat (group regexp (string :tag "Command"))))
105
106 (defcustom gnus-uu-user-view-rules nil
107 "What actions are to be taken to view a file.
108 See the documentation on the `gnus-uu-default-view-rules' variable for
109 details."
110 :group 'gnus-extract-view
111 :type '(repeat (group regexp (string :tag "Command"))))
112
113 (defcustom gnus-uu-user-view-rules-end
114 '(("" "file"))
115 "*What actions are to be taken if no rule matched the file name.
116 See the documentation on the `gnus-uu-default-view-rules' variable for
117 details."
118 :group 'gnus-extract-view
119 :type '(repeat (group regexp (string :tag "Command"))))
120
121 ;; Default unpacking commands
122
123 (defcustom gnus-uu-default-archive-rules
124 '(("\\.tar$" "tar xf")
125 ("\\.zip$" "unzip -o")
126 ("\\.ar$" "ar x")
127 ("\\.arj$" "unarj x")
128 ("\\.zoo$" "zoo -e")
129 ("\\.\\(lzh\\|lha\\)$" "lha x")
130 ("\\.Z$" "uncompress")
131 ("\\.gz$" "gunzip")
132 ("\\.arc$" "arc -x"))
133 "*See `gnus-uu-user-archive-rules'."
134 :group 'gnus-extract-archive
135 :type '(repeat (group regexp (string :tag "Command"))))
136
137 (defvar gnus-uu-destructive-archivers
138 (list "uncompress" "gunzip"))
139
140 (defcustom gnus-uu-user-archive-rules nil
141 "A list that can be set to override the default archive unpacking commands.
142 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
143 unpack zip files, say the following:
144 (setq gnus-uu-user-archive-rules
145 '((\"\\\\.tar$\" \"untar\")
146 (\"\\\\.zip$\" \"zip -x\")))"
147 :group 'gnus-extract-archive
148 :type '(repeat (group regexp (string :tag "Command"))))
149
150 (defcustom gnus-uu-ignore-files-by-name nil
151 "*A regular expression saying what files should not be viewed based on name.
152 If, for instance, you want gnus-uu to ignore all .au and .wav files,
153 you could say something like
154
155 (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
156
157 Note that this variable can be used in conjunction with the
158 `gnus-uu-ignore-files-by-type' variable."
159 :group 'gnus-extract
160 :type '(choice (const :tag "off" nil)
161 (regexp :format "%v")))
162
163 (defcustom gnus-uu-ignore-files-by-type nil
164 "*A regular expression saying what files that shouldn't be viewed, based on MIME file type.
165 If, for instance, you want gnus-uu to ignore all audio files and all mpegs,
166 you could say something like
167
168 (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
169
170 Note that this variable can be used in conjunction with the
171 `gnus-uu-ignore-files-by-name' variable."
172 :group 'gnus-extract
173 :type '(choice (const :tag "off" nil)
174 (regexp :format "%v")))
175
176 ;; Pseudo-MIME support
177
178 (defconst gnus-uu-ext-to-mime-list
179 '(("\\.gif$" "image/gif")
180 ("\\.jpe?g$" "image/jpeg")
181 ("\\.tiff?$" "image/tiff")
182 ("\\.xwd$" "image/xwd")
183 ("\\.pbm$" "image/pbm")
184 ("\\.pgm$" "image/pgm")
185 ("\\.ppm$" "image/ppm")
186 ("\\.xbm$" "image/xbm")
187 ("\\.pcx$" "image/pcx")
188 ("\\.tga$" "image/tga")
189 ("\\.ps$" "image/postscript")
190 ("\\.fli$" "video/fli")
191 ("\\.wav$" "audio/wav")
192 ("\\.aiff$" "audio/aiff")
193 ("\\.hcom$" "audio/hcom")
194 ("\\.voc$" "audio/voc")
195 ("\\.smp$" "audio/smp")
196 ("\\.mod$" "audio/mod")
197 ("\\.dvi$" "image/dvi")
198 ("\\.mpe?g$" "video/mpeg")
199 ("\\.au$" "audio/basic")
200 ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
201 ("\\.\\(c\\|h\\)$" "text/source")
202 ("read.*me" "text/plain")
203 ("\\.html$" "text/html")
204 ("\\.bat$" "text/bat")
205 ("\\.[1-6]$" "text/man")
206 ("\\.flc$" "video/flc")
207 ("\\.rle$" "video/rle")
208 ("\\.pfx$" "video/pfx")
209 ("\\.avi$" "video/avi")
210 ("\\.sme$" "video/sme")
211 ("\\.rpza$" "video/prza")
212 ("\\.dl$" "video/dl")
213 ("\\.qt$" "video/qt")
214 ("\\.rsrc$" "video/rsrc")
215 ("\\..*$" "unknown/unknown")))
216
217 ;; Various variables users may set
218
219 (defcustom gnus-uu-tmp-dir
220 (cond ((fboundp 'temp-directory) (temp-directory))
221 ((boundp 'temporary-file-directory) temporary-file-directory)
222 ("/tmp/"))
223 "*Variable saying where gnus-uu is to do its work.
224 Default is \"/tmp/\"."
225 :group 'gnus-extract
226 :type 'directory)
227
228 (defcustom gnus-uu-do-not-unpack-archives nil
229 "*Non-nil means that gnus-uu won't peek inside archives looking for files to display.
230 Default is nil."
231 :group 'gnus-extract-archive
232 :type 'boolean)
233
234 (defcustom gnus-uu-ignore-default-view-rules nil
235 "*Non-nil means that gnus-uu will ignore the default viewing rules.
236 Only the user viewing rules will be consulted. Default is nil."
237 :group 'gnus-extract-view
238 :type 'boolean)
239
240 (defcustom gnus-uu-grabbed-file-functions nil
241 "Functions run on each file after successful decoding.
242 They will be called with the name of the file as the argument.
243 Likely functions you can use in this list are `gnus-uu-grab-view'
244 and `gnus-uu-grab-move'."
245 :group 'gnus-extract
246 :options '(gnus-uu-grab-view gnus-uu-grab-move)
247 :type 'hook)
248
249 (defcustom gnus-uu-ignore-default-archive-rules nil
250 "*Non-nil means that gnus-uu will ignore the default archive unpacking commands.
251 Only the user unpacking commands will be consulted. Default is nil."
252 :group 'gnus-extract-archive
253 :type 'boolean)
254
255 (defcustom gnus-uu-kill-carriage-return t
256 "*Non-nil means that gnus-uu will strip all carriage returns from articles.
257 Default is t."
258 :group 'gnus-extract
259 :type 'boolean)
260
261 (defcustom gnus-uu-view-with-metamail nil
262 "*Non-nil means that files will be viewed with metamail.
263 The gnus-uu viewing functions will be ignored and gnus-uu will try
264 to guess at a content-type based on file name suffixes. Default
265 it nil."
266 :group 'gnus-extract
267 :type 'boolean)
268
269 (defcustom gnus-uu-unmark-articles-not-decoded nil
270 "*Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread.
271 Default is nil."
272 :group 'gnus-extract
273 :type 'boolean)
274
275 (defcustom gnus-uu-correct-stripped-uucode nil
276 "*Non-nil means that gnus-uu will *try* to fix uuencoded files that have had trailing spaces deleted.
277 Default is nil."
278 :group 'gnus-extract
279 :type 'boolean)
280
281 (defcustom gnus-uu-save-in-digest nil
282 "*Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
283 If this variable is nil, gnus-uu will just save everything in a
284 file without any embellishments. The digesting almost conforms to RFC1153 -
285 no easy way to specify any meaningful volume and issue numbers were found,
286 so I simply dropped them."
287 :group 'gnus-extract
288 :type 'boolean)
289
290 (defcustom gnus-uu-pre-uudecode-hook nil
291 "Hook run before sending a message to uudecode."
292 :group 'gnus-extract
293 :type 'hook)
294
295 (defcustom gnus-uu-digest-headers
296 '("^Date:" "^From:" "^To:" "^Cc:" "^Subject:" "^Message-ID:" "^Keywords:"
297 "^Summary:" "^References:" "^Content-Type:" "^Content-Transfer-Encoding:"
298 "^MIME-Version:" "^Content-Disposition:" "^Content-Description:"
299 "^Content-ID:")
300 "*List of regexps to match headers included in digested messages.
301 The headers will be included in the sequence they are matched. If nil
302 include all headers."
303 :group 'gnus-extract
304 :type '(repeat regexp))
305
306 (defcustom gnus-uu-save-separate-articles nil
307 "*Non-nil means that gnus-uu will save articles in separate files."
308 :group 'gnus-extract
309 :type 'boolean)
310
311 (defcustom gnus-uu-be-dangerous 'ask
312 "*Specifies what to do if unusual situations arise during decoding.
313 If nil, be as conservative as possible. If t, ignore things that
314 didn't work, and overwrite existing files. Otherwise, ask each time."
315 :group 'gnus-extract
316 :type '(choice (const :tag "conservative" nil)
317 (const :tag "ask" ask)
318 (const :tag "liberal" t)))
319
320 ;; Internal variables
321
322 (defvar gnus-uu-saved-article-name nil)
323
324 (defvar gnus-uu-begin-string "^begin[ \t]+0?[0-7][0-7][0-7][ \t]+\\(.*\\)$")
325 (defvar gnus-uu-end-string "^end[ \t]*$")
326
327 (defvar gnus-uu-body-line "^M")
328 (let ((i 61))
329 (while (> (setq i (1- i)) 0)
330 (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
331 (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
332
333 ;"^M.............................................................?$"
334
335 (defvar gnus-uu-shar-begin-string "^#! */bin/sh")
336
337 (defvar gnus-uu-shar-name-marker
338 "begin 0?[0-7][0-7][0-7][ \t]+\\(\\(\\w\\|[.\\:]\\)*\\b\\)")
339
340 (defvar gnus-uu-postscript-begin-string "^%!PS-")
341 (defvar gnus-uu-postscript-end-string "^%%EOF$")
342
343 (defvar gnus-uu-file-name nil)
344 (defvar gnus-uu-uudecode-process nil)
345 (defvar gnus-uu-binhex-article-name nil)
346 (defvar gnus-uu-yenc-article-name nil)
347
348 (defvar gnus-uu-work-dir nil)
349
350 (defvar gnus-uu-output-buffer-name " *Gnus UU Output*")
351
352 (defvar gnus-uu-default-dir gnus-article-save-directory)
353 (defvar gnus-uu-digest-from-subject nil)
354 (defvar gnus-uu-digest-buffer nil)
355
356 ;; Commands.
357
358 (defun gnus-uu-decode-uu (&optional n)
359 "Uudecodes the current article."
360 (interactive "P")
361 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
362
363 (defun gnus-uu-decode-uu-and-save (n dir)
364 "Decodes and saves the resulting file."
365 (interactive
366 (list current-prefix-arg
367 (file-name-as-directory
368 (read-file-name "Uudecode and save in dir: "
369 gnus-uu-default-dir
370 gnus-uu-default-dir t))))
371 (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir nil nil t))
372
373 (defun gnus-uu-decode-unshar (&optional n)
374 "Unshars the current article."
375 (interactive "P")
376 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n nil nil 'scan t))
377
378 (defun gnus-uu-decode-unshar-and-save (n dir)
379 "Unshars and saves the current article."
380 (interactive
381 (list current-prefix-arg
382 (file-name-as-directory
383 (read-file-name "Unshar and save in dir: "
384 gnus-uu-default-dir
385 gnus-uu-default-dir t))))
386 (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir nil 'scan t))
387
388 (defun gnus-uu-decode-save (n file)
389 "Saves the current article."
390 (interactive
391 (list current-prefix-arg
392 (read-file-name
393 (if gnus-uu-save-separate-articles
394 "Save articles in dir: "
395 "Save articles in file: ")
396 gnus-uu-default-dir
397 gnus-uu-default-dir)))
398 (setq gnus-uu-saved-article-name file)
399 (gnus-uu-decode-with-method 'gnus-uu-save-article n nil t))
400
401 (defun gnus-uu-decode-binhex (n dir)
402 "Unbinhexes the current article."
403 (interactive
404 (list current-prefix-arg
405 (file-name-as-directory
406 (read-file-name "Unbinhex and save in dir: "
407 gnus-uu-default-dir
408 gnus-uu-default-dir))))
409 (setq gnus-uu-binhex-article-name
410 (mm-make-temp-file (expand-file-name "binhex" gnus-uu-work-dir)))
411 (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
412
413 (defun gnus-uu-decode-yenc (n dir)
414 "Decode the yEnc-encoded current article."
415 (interactive
416 (list current-prefix-arg
417 (file-name-as-directory
418 (read-file-name "yEnc decode and save in dir: "
419 gnus-uu-default-dir
420 gnus-uu-default-dir))))
421 (setq gnus-uu-yenc-article-name nil)
422 (gnus-uu-decode-with-method 'gnus-uu-yenc-article n dir nil t))
423
424 (defun gnus-uu-decode-uu-view (&optional n)
425 "Uudecodes and views the current article."
426 (interactive "P")
427 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
428 (gnus-uu-decode-uu n)))
429
430 (defun gnus-uu-decode-uu-and-save-view (n dir)
431 "Decodes, views and saves the resulting file."
432 (interactive
433 (list current-prefix-arg
434 (read-file-name "Uudecode, view and save in dir: "
435 gnus-uu-default-dir
436 gnus-uu-default-dir t)))
437 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
438 (gnus-uu-decode-uu-and-save n dir)))
439
440 (defun gnus-uu-decode-unshar-view (&optional n)
441 "Unshars and views the current article."
442 (interactive "P")
443 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
444 (gnus-uu-decode-unshar n)))
445
446 (defun gnus-uu-decode-unshar-and-save-view (n dir)
447 "Unshars and saves the current article."
448 (interactive
449 (list current-prefix-arg
450 (read-file-name "Unshar, view and save in dir: "
451 gnus-uu-default-dir
452 gnus-uu-default-dir t)))
453 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
454 (gnus-uu-decode-unshar-and-save n dir)))
455
456 (defun gnus-uu-decode-save-view (n file)
457 "Saves and views the current article."
458 (interactive
459 (list current-prefix-arg
460 (read-file-name (if gnus-uu-save-separate-articles
461 "Save articles is dir: "
462 "Save articles in file: ")
463 gnus-uu-default-dir gnus-uu-default-dir)))
464 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
465 (gnus-uu-decode-save n file)))
466
467 (defun gnus-uu-decode-binhex-view (n file)
468 "Unbinhexes and views the current article."
469 (interactive
470 (list current-prefix-arg
471 (read-file-name "Unbinhex, view and save in dir: "
472 gnus-uu-default-dir gnus-uu-default-dir)))
473 (setq gnus-uu-binhex-article-name
474 (mm-make-temp-file (expand-file-name "binhex" gnus-uu-work-dir)))
475 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
476 (gnus-uu-decode-binhex n file)))
477
478
479 ;; Digest and forward articles
480
481 (defun gnus-uu-digest-mail-forward (&optional n post)
482 "Digests and forwards all articles in this series."
483 (interactive "P")
484 (let ((gnus-uu-save-in-digest t)
485 (file (mm-make-temp-file (nnheader-concat gnus-uu-tmp-dir "forward")))
486 (message-forward-as-mime message-forward-as-mime)
487 (mail-parse-charset gnus-newsgroup-charset)
488 (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
489 gnus-uu-digest-buffer subject from)
490 (if (and n (not (numberp n)))
491 (setq message-forward-as-mime (not message-forward-as-mime)
492 n nil))
493 (let ((gnus-article-reply (gnus-summary-work-articles n)))
494 (when (and (not n)
495 (= (length gnus-article-reply) 1))
496 ;; The case where neither a number of articles nor a region is
497 ;; specified.
498 (gnus-summary-top-thread)
499 (setq gnus-article-reply (nreverse (gnus-uu-find-articles-matching))))
500 (gnus-setup-message 'forward
501 (setq gnus-uu-digest-from-subject nil)
502 (setq gnus-uu-digest-buffer
503 (gnus-get-buffer-create " *gnus-uu-forward*"))
504 ;; Specify articles to be forwarded. Note that they should be
505 ;; reversed; see `gnus-uu-get-list-of-articles'.
506 (let ((gnus-newsgroup-processable (reverse gnus-article-reply)))
507 (gnus-uu-decode-save n file)
508 (setq gnus-article-reply gnus-newsgroup-processable))
509 ;; Restore the value of `gnus-newsgroup-processable' to which
510 ;; it should be set when it is not `let'-bound.
511 (setq gnus-newsgroup-processable (reverse gnus-article-reply))
512 (switch-to-buffer gnus-uu-digest-buffer)
513 (let ((fs gnus-uu-digest-from-subject))
514 (when fs
515 (setq from (caar fs)
516 subject (gnus-simplify-subject-fuzzy (cdar fs))
517 fs (cdr fs))
518 (while (and fs (or from subject))
519 (when from
520 (unless (string= from (caar fs))
521 (setq from nil)))
522 (when subject
523 (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
524 subject)
525 (setq subject nil)))
526 (setq fs (cdr fs))))
527 (unless subject
528 (setq subject "Digested Articles"))
529 (unless from
530 (setq from
531 (if (gnus-news-group-p gnus-newsgroup-name)
532 gnus-newsgroup-name
533 "Various"))))
534 (goto-char (point-min))
535 (when (re-search-forward "^Subject: ")
536 (delete-region (point) (point-at-eol))
537 (insert subject))
538 (goto-char (point-min))
539 (when (re-search-forward "^From:")
540 (delete-region (point) (point-at-eol))
541 (insert " " from))
542 (let ((message-forward-decoded-p t))
543 (message-forward post t))))
544 (setq gnus-uu-digest-from-subject nil)))
545
546 (defun gnus-uu-digest-post-forward (&optional n)
547 "Digest and forward to a newsgroup."
548 (interactive "P")
549 (gnus-uu-digest-mail-forward n t))
550
551 ;; Process marking.
552
553 (defun gnus-message-process-mark (unmarkp new-marked)
554 (let ((old (- (length gnus-newsgroup-processable) (length new-marked))))
555 (gnus-message 6 "%d mark%s %s%s"
556 (length new-marked)
557 (if (= (length new-marked) 1) "" "s")
558 (if unmarkp "removed" "added")
559 (cond
560 ((and (zerop old)
561 (not unmarkp))
562 "")
563 (unmarkp
564 (format ", %d remain marked"
565 (length gnus-newsgroup-processable)))
566 (t
567 (format ", %d already marked" old))))))
568
569 (defun gnus-new-processable (unmarkp articles)
570 (if unmarkp
571 (gnus-intersection gnus-newsgroup-processable articles)
572 (gnus-set-difference articles gnus-newsgroup-processable)))
573
574 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
575 "Set the process mark on articles whose subjects match REGEXP.
576 When called interactively, prompt for REGEXP.
577 Optional UNMARK non-nil means unmark instead of mark."
578 (interactive "sMark (regexp): \nP")
579 (save-excursion
580 (let* ((articles (gnus-uu-find-articles-matching regexp))
581 (new-marked (gnus-new-processable unmark articles)))
582 (while articles
583 (if unmark
584 (gnus-summary-remove-process-mark (pop articles))
585 (gnus-summary-set-process-mark (pop articles))))
586 (gnus-message-process-mark unmark new-marked)))
587 (gnus-summary-position-point))
588
589 (defun gnus-uu-unmark-by-regexp (regexp)
590 "Remove the process mark from articles whose subjects match REGEXP.
591 When called interactively, prompt for REGEXP."
592 (interactive "sUnmark (regexp): ")
593 (gnus-uu-mark-by-regexp regexp t))
594
595 (defun gnus-uu-mark-series (&optional silent)
596 "Mark the current series with the process mark."
597 (interactive)
598 (let* ((articles (gnus-uu-find-articles-matching))
599 (l (length articles)))
600 (while articles
601 (gnus-summary-set-process-mark (car articles))
602 (setq articles (cdr articles)))
603 (unless silent
604 (gnus-message 6 "Marked %d articles" l))
605 (gnus-summary-position-point)
606 l))
607
608 (defun gnus-uu-mark-region (beg end &optional unmark)
609 "Set the process mark on all articles between point and mark."
610 (interactive "r")
611 (save-excursion
612 (goto-char beg)
613 (while (< (point) end)
614 (if unmark
615 (gnus-summary-remove-process-mark (gnus-summary-article-number))
616 (gnus-summary-set-process-mark (gnus-summary-article-number)))
617 (forward-line 1)))
618 (gnus-summary-position-point))
619
620 (defun gnus-uu-unmark-region (beg end)
621 "Remove the process mark from all articles between point and mark."
622 (interactive "r")
623 (gnus-uu-mark-region beg end t))
624
625 (defun gnus-uu-mark-buffer ()
626 "Set the process mark on all articles in the buffer."
627 (interactive)
628 (gnus-uu-mark-region (point-min) (point-max)))
629
630 (defun gnus-uu-unmark-buffer ()
631 "Remove the process mark on all articles in the buffer."
632 (interactive)
633 (gnus-uu-mark-region (point-min) (point-max) t))
634
635 (defun gnus-uu-mark-thread ()
636 "Marks all articles downwards in this thread."
637 (interactive)
638 (gnus-save-hidden-threads
639 (let ((level (gnus-summary-thread-level)))
640 (while (and (gnus-summary-set-process-mark
641 (gnus-summary-article-number))
642 (zerop (gnus-summary-next-subject 1 nil t))
643 (> (gnus-summary-thread-level) level)))))
644 (gnus-summary-position-point))
645
646 (defun gnus-uu-unmark-thread ()
647 "Unmarks all articles downwards in this thread."
648 (interactive)
649 (let ((level (gnus-summary-thread-level)))
650 (while (and (gnus-summary-remove-process-mark
651 (gnus-summary-article-number))
652 (zerop (gnus-summary-next-subject 1))
653 (> (gnus-summary-thread-level) level))))
654 (gnus-summary-position-point))
655
656 (defun gnus-uu-invert-processable ()
657 "Invert the list of process-marked articles."
658 (interactive)
659 (let ((data gnus-newsgroup-data)
660 number)
661 (save-excursion
662 (while data
663 (if (memq (setq number (gnus-data-number (pop data)))
664 gnus-newsgroup-processable)
665 (gnus-summary-remove-process-mark number)
666 (gnus-summary-set-process-mark number)))))
667 (gnus-summary-position-point))
668
669 (defun gnus-uu-mark-over (&optional score)
670 "Mark all articles with a score over SCORE (the prefix)."
671 (interactive "P")
672 (let ((score (or score gnus-summary-default-score 0))
673 (data gnus-newsgroup-data))
674 (save-excursion
675 (while data
676 (when (> (or (cdr (assq (gnus-data-number (car data))
677 gnus-newsgroup-scored))
678 gnus-summary-default-score 0)
679 score)
680 (gnus-summary-set-process-mark (caar data)))
681 (setq data (cdr data))))
682 (gnus-summary-position-point)))
683
684 (defun gnus-uu-mark-sparse ()
685 "Mark all series that have some articles marked."
686 (interactive)
687 (let ((marked (nreverse gnus-newsgroup-processable))
688 subject articles total headers)
689 (unless marked
690 (error "No articles marked with the process mark"))
691 (setq gnus-newsgroup-processable nil)
692 (save-excursion
693 (while marked
694 (and (vectorp (setq headers
695 (gnus-summary-article-header (car marked))))
696 (setq subject (mail-header-subject headers)
697 articles (gnus-uu-find-articles-matching
698 (gnus-uu-reginize-string subject))
699 total (nconc total articles)))
700 (while articles
701 (gnus-summary-set-process-mark (car articles))
702 (setcdr marked (delq (car articles) (cdr marked)))
703 (setq articles (cdr articles)))
704 (setq marked (cdr marked)))
705 (setq gnus-newsgroup-processable (nreverse total)))
706 (gnus-summary-position-point)))
707
708 (defun gnus-uu-mark-all ()
709 "Mark all articles in \"series\" order."
710 (interactive)
711 (setq gnus-newsgroup-processable nil)
712 (save-excursion
713 (let ((data gnus-newsgroup-data)
714 (count 0)
715 number)
716 (while data
717 (when (and (not (memq (setq number (gnus-data-number (car data)))
718 gnus-newsgroup-processable))
719 (vectorp (gnus-data-header (car data))))
720 (gnus-summary-goto-subject number)
721 (setq count (+ count (gnus-uu-mark-series t))))
722 (setq data (cdr data)))
723 (gnus-message 6 "Marked %d articles" count)))
724 (gnus-summary-position-point))
725
726 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
727
728 (defun gnus-uu-decode-postscript (&optional n)
729 "Gets postscript of the current article."
730 (interactive "P")
731 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
732
733 (defun gnus-uu-decode-postscript-view (&optional n)
734 "Gets and views the current article."
735 (interactive "P")
736 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
737 (gnus-uu-decode-postscript n)))
738
739 (defun gnus-uu-decode-postscript-and-save (n dir)
740 "Extracts postscript and saves the current article."
741 (interactive
742 (list current-prefix-arg
743 (file-name-as-directory
744 (read-file-name "Save in dir: "
745 gnus-uu-default-dir
746 gnus-uu-default-dir t))))
747 (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
748 n dir nil nil t))
749
750 (defun gnus-uu-decode-postscript-and-save-view (n dir)
751 "Decodes, views and saves the resulting file."
752 (interactive
753 (list current-prefix-arg
754 (read-file-name "Where do you want to save the file(s)? "
755 gnus-uu-default-dir
756 gnus-uu-default-dir t)))
757 (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
758 (gnus-uu-decode-postscript-and-save n dir)))
759
760
761 ;; Internal functions.
762
763 (defun gnus-uu-decode-with-method (method n &optional save not-insert
764 scan cdir)
765 (gnus-uu-initialize scan)
766 (when save
767 (setq gnus-uu-default-dir save))
768 ;; Create the directory we save to.
769 (when (and scan cdir save
770 (not (file-exists-p save)))
771 (make-directory save t))
772 (let ((articles (gnus-uu-get-list-of-articles n))
773 files)
774 (setq files (gnus-uu-grab-articles articles method t))
775 (let ((gnus-current-article (car articles)))
776 (when scan
777 (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
778 (when save
779 (gnus-uu-save-files files save))
780 (when (eq gnus-uu-do-not-unpack-archives nil)
781 (setq files (gnus-uu-unpack-files files)))
782 (setq files (nreverse (gnus-uu-get-actions files)))
783 (or not-insert (not gnus-insert-pseudo-articles)
784 (gnus-summary-insert-pseudos files save))))
785
786 (defun gnus-uu-scan-directory (dir &optional rec)
787 "Return a list of all files under DIR."
788 (let ((files (directory-files dir t))
789 out file)
790 (while (setq file (pop files))
791 (unless (member (file-name-nondirectory file) '("." ".."))
792 (push (list (cons 'name file)
793 (cons 'article gnus-current-article))
794 out)
795 (when (file-directory-p file)
796 (setq out (nconc (gnus-uu-scan-directory file t) out)))))
797 (if rec
798 out
799 (nreverse out))))
800
801 (defun gnus-uu-save-files (files dir)
802 "Save FILES in DIR."
803 (let ((len (length files))
804 (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
805 to-file file fromdir)
806 (while (setq file (cdr (assq 'name (pop files))))
807 (when (file-exists-p file)
808 (string-match reg file)
809 (setq fromdir (substring file (match-end 0)))
810 (if (file-directory-p file)
811 (gnus-make-directory (concat dir fromdir))
812 (setq to-file (concat dir fromdir))
813 (when (or (not (file-exists-p to-file))
814 (eq gnus-uu-be-dangerous t)
815 (and gnus-uu-be-dangerous
816 (gnus-y-or-n-p (format "%s exists; overwrite? "
817 to-file))))
818 (copy-file file to-file t t)))))
819 (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
820
821 ;; Functions for saving and possibly digesting articles without
822 ;; any decoding.
823
824 ;; Function called by gnus-uu-grab-articles to treat each article.
825 (defun gnus-uu-save-article (buffer in-state)
826 (cond
827 (gnus-uu-save-separate-articles
828 (with-current-buffer buffer
829 (let ((coding-system-for-write mm-text-coding-system))
830 (gnus-write-buffer
831 (concat gnus-uu-saved-article-name gnus-current-article)))
832 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
833 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
834 'begin 'end))
835 ((eq in-state 'last) (list 'end))
836 (t (list 'middle)))))
837 ((not gnus-uu-save-in-digest)
838 (with-current-buffer buffer
839 (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
840 (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
841 ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
842 'begin 'end))
843 ((eq in-state 'last) (list 'end))
844 (t (list 'middle)))))
845 (t
846 (let ((header (gnus-summary-article-header)))
847 (push (cons (mail-header-from header)
848 (mail-header-subject header))
849 gnus-uu-digest-from-subject))
850 (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
851 beg subj headers headline sorthead body end-string state)
852 (if (or (eq in-state 'first)
853 (eq in-state 'first-and-last))
854 (progn
855 (setq state (list 'begin))
856 (with-current-buffer (gnus-get-buffer-create "*gnus-uu-body*")
857 (erase-buffer))
858 (with-current-buffer (gnus-get-buffer-create "*gnus-uu-pre*")
859 (erase-buffer)
860 (insert (format
861 "Date: %s\nFrom: %s\nSubject: %s Digest\n\n"
862 (message-make-date) name name))
863 (when (and message-forward-as-mime gnus-uu-digest-buffer)
864 (insert
865 "<#mml type=message/rfc822>\nSubject: Topics\n\n<#/mml>\n")
866 (forward-line -1))
867 (insert "Topics:\n")))
868 (when (not (eq in-state 'end))
869 (setq state (list 'middle))))
870 (with-current-buffer "*gnus-uu-body*"
871 (goto-char (setq beg (point-max)))
872 (save-excursion
873 (save-restriction
874 (set-buffer buffer)
875 (let (buffer-read-only)
876 (set-text-properties (point-min) (point-max) nil)
877 ;; These two are necessary for XEmacs 19.12 fascism.
878 (put-text-property (point-min) (point-max) 'invisible nil)
879 (put-text-property (point-min) (point-max) 'intangible nil))
880 (when (and message-forward-as-mime
881 message-forward-show-mml
882 gnus-uu-digest-buffer)
883 (mm-enable-multibyte)
884 (mime-to-mml))
885 (goto-char (point-min))
886 (search-forward "\n\n")
887 (unless (and message-forward-as-mime gnus-uu-digest-buffer)
888 ;; Quote all 30-dash lines.
889 (save-excursion
890 (while (re-search-forward "^-" nil t)
891 (beginning-of-line)
892 (delete-char 1)
893 (insert "- "))))
894 (setq body (buffer-substring (1- (point)) (point-max)))
895 (narrow-to-region (point-min) (point))
896 (if (not (setq headers gnus-uu-digest-headers))
897 (setq sorthead (buffer-string))
898 (while headers
899 (setq headline (car headers))
900 (setq headers (cdr headers))
901 (goto-char (point-min))
902 (while (re-search-forward headline nil t)
903 (setq sorthead
904 (concat sorthead
905 (buffer-substring
906 (match-beginning 0)
907 (or (and (re-search-forward "^[^ \t]" nil t)
908 (1- (point)))
909 (progn (forward-line 1) (point)))))))))
910 (widen)))
911 (if (and message-forward-as-mime gnus-uu-digest-buffer)
912 (if message-forward-show-mml
913 (progn
914 (insert "\n<#mml type=message/rfc822>\n")
915 (insert sorthead) (goto-char (point-max))
916 (insert body) (goto-char (point-max))
917 (insert "\n<#/mml>\n"))
918 (let ((buf (mml-generate-new-buffer " *mml*")))
919 (with-current-buffer buf
920 (insert sorthead)
921 (goto-char (point-min))
922 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
923 (setq subj (buffer-substring (match-beginning 1)
924 (match-end 1))))
925 (goto-char (point-max))
926 (insert body))
927 (insert "\n<#part type=message/rfc822"
928 " buffer=\"" (buffer-name buf) "\">\n")))
929 (insert sorthead) (goto-char (point-max))
930 (insert body) (goto-char (point-max))
931 (insert (concat "\n" (make-string 30 ?-) "\n\n")))
932 (goto-char beg)
933 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
934 (setq subj (buffer-substring (match-beginning 1) (match-end 1))))
935 (when subj
936 (with-current-buffer "*gnus-uu-pre*"
937 (insert (format " %s\n" subj)))))
938 (when (or (eq in-state 'last)
939 (eq in-state 'first-and-last))
940 (if (and message-forward-as-mime gnus-uu-digest-buffer)
941 (with-current-buffer gnus-uu-digest-buffer
942 (erase-buffer)
943 (insert-buffer-substring "*gnus-uu-pre*")
944 (goto-char (point-max))
945 (insert-buffer-substring "*gnus-uu-body*"))
946 (with-current-buffer "*gnus-uu-pre*"
947 (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
948 (if gnus-uu-digest-buffer
949 (with-current-buffer gnus-uu-digest-buffer
950 (erase-buffer)
951 (insert-buffer-substring "*gnus-uu-pre*"))
952 (let ((coding-system-for-write mm-text-coding-system))
953 (gnus-write-buffer gnus-uu-saved-article-name))))
954 (with-current-buffer "*gnus-uu-body*"
955 (goto-char (point-max))
956 (insert
957 (concat (setq end-string (format "End of %s Digest" name))
958 "\n"))
959 (insert (concat (make-string (length end-string) ?*) "\n"))
960 (if gnus-uu-digest-buffer
961 (with-current-buffer gnus-uu-digest-buffer
962 (goto-char (point-max))
963 (insert-buffer-substring "*gnus-uu-body*"))
964 (let ((coding-system-for-write mm-text-coding-system)
965 (file-name-coding-system nnmail-pathname-coding-system))
966 (write-region
967 (point-min) (point-max) gnus-uu-saved-article-name t)))))
968 (gnus-kill-buffer "*gnus-uu-pre*")
969 (gnus-kill-buffer "*gnus-uu-body*")
970 (push 'end state))
971 (if (memq 'begin state)
972 (cons gnus-uu-saved-article-name state)
973 state)))))
974
975 ;; Binhex treatment - not very advanced.
976
977 (defvar gnus-uu-binhex-body-line
978 "^[^:]...............................................................$")
979 (defvar gnus-uu-binhex-begin-line
980 "^:...............................................................$")
981 (defvar gnus-uu-binhex-end-line
982 ":$")
983
984 (defun gnus-uu-binhex-article (buffer in-state)
985 (let (state start-char)
986 (with-current-buffer buffer
987 (widen)
988 (goto-char (point-min))
989 (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
990 (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
991 (setq state (list 'wrong-type))))
992
993 (if (memq 'wrong-type state)
994 ()
995 (beginning-of-line)
996 (setq start-char (point))
997 (if (looking-at gnus-uu-binhex-begin-line)
998 (progn
999 (setq state (list 'begin))
1000 (write-region (point-min) (point-min)
1001 gnus-uu-binhex-article-name))
1002 (setq state (list 'middle)))
1003 (goto-char (point-max))
1004 (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
1005 gnus-uu-binhex-end-line)
1006 nil t)
1007 (when (looking-at gnus-uu-binhex-end-line)
1008 (setq state (if (memq 'begin state)
1009 (cons 'end state)
1010 (list 'end))))
1011 (beginning-of-line)
1012 (forward-line 1)
1013 (when (file-exists-p gnus-uu-binhex-article-name)
1014 (mm-append-to-file start-char (point) gnus-uu-binhex-article-name))))
1015 (if (memq 'begin state)
1016 (cons gnus-uu-binhex-article-name state)
1017 state)))
1018
1019 ;; yEnc
1020
1021 (defun gnus-uu-yenc-article (buffer in-state)
1022 (with-current-buffer gnus-original-article-buffer
1023 (widen)
1024 (let ((file-name (yenc-extract-filename))
1025 state start-char)
1026 (when (not file-name)
1027 (setq state (list 'wrong-type)))
1028
1029 (if (memq 'wrong-type state)
1030 ()
1031 (when (yenc-first-part-p)
1032 (setq gnus-uu-yenc-article-name
1033 (expand-file-name file-name gnus-uu-work-dir))
1034 (push 'begin state))
1035 (when (yenc-last-part-p)
1036 (push 'end state))
1037 (unless state
1038 (push 'middle state))
1039 (mm-with-unibyte-buffer
1040 (insert-buffer-substring gnus-original-article-buffer)
1041 (yenc-decode-region (point-min) (point-max))
1042 (when (and (member 'begin state)
1043 (file-exists-p gnus-uu-yenc-article-name))
1044 (delete-file gnus-uu-yenc-article-name))
1045 (mm-append-to-file (point-min) (point-max)
1046 gnus-uu-yenc-article-name)))
1047 (if (memq 'begin state)
1048 (cons file-name state)
1049 state))))
1050
1051 ;; PostScript
1052
1053 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
1054 (let ((state (list 'ok))
1055 start-char end-char file-name)
1056 (with-current-buffer process-buffer
1057 (goto-char (point-min))
1058 (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
1059 (setq state (list 'wrong-type))
1060 (beginning-of-line)
1061 (setq start-char (point))
1062 (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
1063 (setq state (list 'wrong-type))
1064 (setq end-char (point))
1065 (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1066 (insert-buffer-substring process-buffer start-char end-char)
1067 (setq file-name (concat gnus-uu-work-dir
1068 (cdr gnus-article-current) ".ps"))
1069 (write-region (point-min) (point-max) file-name)
1070 (setq state (list file-name 'begin 'end)))))
1071 state))
1072
1073
1074 ;; Find actions.
1075
1076 (defun gnus-uu-get-actions (files)
1077 (let ((ofiles files)
1078 action name)
1079 (while files
1080 (setq name (cdr (assq 'name (car files))))
1081 (and
1082 (setq action (gnus-uu-get-action name))
1083 (setcar files (nconc (list (if (string= action "gnus-uu-archive")
1084 (cons 'action "file")
1085 (cons 'action action))
1086 (cons 'execute (gnus-uu-command
1087 action name)))
1088 (car files))))
1089 (setq files (cdr files)))
1090 ofiles))
1091
1092 (defun gnus-uu-get-action (file-name)
1093 (let (action)
1094 (setq action
1095 (gnus-uu-choose-action
1096 file-name
1097 (append
1098 gnus-uu-user-view-rules
1099 (if gnus-uu-ignore-default-view-rules
1100 nil
1101 gnus-uu-default-view-rules)
1102 gnus-uu-user-view-rules-end)))
1103 (when (and (not (string= (or action "") "gnus-uu-archive"))
1104 gnus-uu-view-with-metamail)
1105 (when (setq action
1106 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
1107 (setq action (format "metamail -d -b -c \"%s\"" action))))
1108 action))
1109
1110
1111 ;; Functions for treating subjects and collecting series.
1112
1113 (defun gnus-uu-reginize-string (string)
1114 ;; Takes a string and puts a \ in front of every special character;
1115 ;; replaces the last thing that looks like "2/3" with "[0-9]+/3"
1116 ;; or, if it can't find something like that, tries "2 of 3", then
1117 ;; finally just replaces the next to last number with "[0-9]+".
1118 (with-current-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name)
1119 (buffer-disable-undo)
1120 (erase-buffer)
1121 (insert (regexp-quote string))
1122
1123 (setq case-fold-search nil)
1124
1125 (end-of-line)
1126 (if (re-search-backward "\\([^0-9]\\)[0-9]+/\\([0-9]+\\)" nil t)
1127 (replace-match "\\1[0-9]+/\\2")
1128
1129 (end-of-line)
1130 (if (re-search-backward "\\([^0-9]\\)[0-9]+[ \t]*of[ \t]*\\([0-9]+\\)"
1131 nil t)
1132 (replace-match "\\1[0-9]+ of \\2")
1133
1134 (end-of-line)
1135 (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1136 nil t)
1137 (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1138
1139 (goto-char (point-min))
1140 (while (re-search-forward "[ \t]+" nil t)
1141 (replace-match "[ \t]+" t t))
1142
1143 (buffer-string)))
1144
1145 (defun gnus-uu-get-list-of-articles (n)
1146 ;; If N is non-nil, the article numbers of the N next articles
1147 ;; will be returned.
1148 ;; If any articles have been marked as processable, they will be
1149 ;; returned.
1150 ;; Failing that, articles that have subjects that are part of the
1151 ;; same "series" as the current will be returned.
1152 (let (articles)
1153 (cond
1154 (n
1155 (setq n (prefix-numeric-value n))
1156 (let ((backward (< n 0))
1157 (n (abs n)))
1158 (save-excursion
1159 (while (and (> n 0)
1160 (push (gnus-summary-article-number)
1161 articles)
1162 (gnus-summary-search-forward nil nil backward))
1163 (setq n (1- n))))
1164 (nreverse articles)))
1165 (gnus-newsgroup-processable
1166 (reverse gnus-newsgroup-processable))
1167 (t
1168 (gnus-uu-find-articles-matching)))))
1169
1170 (defun gnus-uu-string< (l1 l2)
1171 (string< (car l1) (car l2)))
1172
1173 (defun gnus-uu-find-articles-matching
1174 (&optional subject only-unread do-not-translate)
1175 ;; Finds all articles that matches the regexp SUBJECT. If it is
1176 ;; nil, the current article name will be used. If ONLY-UNREAD is
1177 ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
1178 ;; non-nil, article names are not equalized before sorting.
1179 (let ((subject (or subject
1180 (gnus-uu-reginize-string (gnus-summary-article-subject))))
1181 list-of-subjects)
1182 (save-excursion
1183 (when subject
1184 ;; Collect all subjects matching subject.
1185 (let ((case-fold-search t)
1186 (data gnus-newsgroup-data)
1187 subj mark d)
1188 (while data
1189 (setq d (pop data))
1190 (and (not (gnus-data-pseudo-p d))
1191 (or (not only-unread)
1192 (= (setq mark (gnus-data-mark d))
1193 gnus-unread-mark)
1194 (= mark gnus-ticked-mark)
1195 (= mark gnus-dormant-mark))
1196 (setq subj (mail-header-subject (gnus-data-header d)))
1197 (string-match subject subj)
1198 (push (cons subj (gnus-data-number d))
1199 list-of-subjects))))
1200
1201 ;; Expand numbers, sort, and return the list of article
1202 ;; numbers.
1203 (mapcar 'cdr
1204 (sort (gnus-uu-expand-numbers
1205 list-of-subjects
1206 (not do-not-translate))
1207 'gnus-uu-string<))))))
1208
1209 (defun gnus-uu-expand-numbers (string-list &optional translate)
1210 ;; Takes a list of strings and "expands" all numbers in all the
1211 ;; strings. That is, this function makes all numbers equal length by
1212 ;; prepending lots of zeroes before each number. This is to ease later
1213 ;; sorting to find out what sequence the articles are supposed to be
1214 ;; decoded in. Returns the list of expanded strings.
1215 (let ((out-list string-list)
1216 string)
1217 (with-current-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name)
1218 (buffer-disable-undo)
1219 (while string-list
1220 (erase-buffer)
1221 (insert (caar string-list))
1222 ;; Translate multiple spaces to one space.
1223 (goto-char (point-min))
1224 (while (re-search-forward "[ \t]+" nil t)
1225 (replace-match " "))
1226 ;; Translate all characters to "a".
1227 (goto-char (point-min))
1228 (when translate
1229 (while (re-search-forward "[A-Za-z]" nil t)
1230 (replace-match "a" t t)))
1231 ;; Expand numbers.
1232 (goto-char (point-min))
1233 (while (re-search-forward "[0-9]+" nil t)
1234 (ignore-errors
1235 (replace-match
1236 (format "%06d"
1237 (string-to-number (buffer-substring
1238 (match-beginning 0) (match-end 0)))))))
1239 (setq string (buffer-substring (point-min) (point-max)))
1240 (setcar (car string-list) string)
1241 (setq string-list (cdr string-list))))
1242 out-list))
1243
1244
1245 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1246 ;; function. It takes a list of articles to be grabbed and a function
1247 ;; to apply to each article.
1248 ;;
1249 ;; The function to be called should take two parameters. The first
1250 ;; parameter is the article buffer. The function should leave the
1251 ;; result, if any, in this buffer. Most treatment functions will just
1252 ;; generate files...
1253 ;;
1254 ;; The second parameter is the state of the list of articles, and can
1255 ;; have four values: `first', `middle', `last' and `first-and-last'.
1256 ;;
1257 ;; The function should return a list. The list may contain the
1258 ;; following symbols:
1259 ;; `error' if an error occurred
1260 ;; `begin' if the beginning of an encoded file has been received
1261 ;; If the list returned contains a `begin', the first element of
1262 ;; the list *must* be a string with the file name of the decoded
1263 ;; file.
1264 ;; `end' if the end of an encoded file has been received
1265 ;; `middle' if the article was a body part of an encoded file
1266 ;; `wrong-type' if the article was not a part of an encoded file
1267 ;; `ok', which can be used everything is ok
1268
1269 (defvar gnus-uu-has-been-grabbed nil)
1270
1271 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1272 (let (art)
1273 (if (not (and gnus-uu-has-been-grabbed
1274 gnus-uu-unmark-articles-not-decoded))
1275 ()
1276 (when dont-unmark-last-article
1277 (setq art (car gnus-uu-has-been-grabbed))
1278 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1279 (while gnus-uu-has-been-grabbed
1280 (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1281 (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1282 (when dont-unmark-last-article
1283 (setq gnus-uu-has-been-grabbed (list art))))))
1284
1285 ;; This function takes a list of articles and a function to apply to
1286 ;; each article grabbed.
1287 ;;
1288 ;; This function returns a list of files decoded if the grabbing and
1289 ;; the process-function has been successful and nil otherwise.
1290 (defun gnus-uu-grab-articles (articles process-function
1291 &optional sloppy limit no-errors)
1292 (let ((state 'first)
1293 (gnus-asynchronous nil)
1294 (gnus-inhibit-treatment t)
1295 has-been-begin article result-file result-files process-state
1296 gnus-summary-display-article-function
1297 gnus-article-prepare-hook gnus-display-mime-function
1298 article-series files)
1299
1300 (while (and articles
1301 (not (memq 'error process-state))
1302 (or sloppy
1303 (not (memq 'end process-state))))
1304
1305 (setq article (pop articles))
1306 (when (vectorp (gnus-summary-article-header article))
1307 (push article article-series)
1308
1309 (unless articles
1310 (if (eq state 'first)
1311 (setq state 'first-and-last)
1312 (setq state 'last)))
1313
1314 (let ((part (gnus-uu-part-number article)))
1315 (gnus-message 6 "Getting article %d%s..."
1316 article (if (string= part "") "" (concat ", " part))))
1317 (gnus-summary-display-article article)
1318
1319 ;; Push the article to the processing function.
1320 (with-current-buffer gnus-original-article-buffer
1321 (let ((buffer-read-only nil))
1322 (with-current-buffer gnus-summary-buffer
1323 (setq process-state
1324 (funcall process-function
1325 gnus-original-article-buffer state)))))
1326
1327 (gnus-summary-remove-process-mark article)
1328
1329 ;; If this is the beginning of a decoded file, we push it
1330 ;; on to a list.
1331 (when (or (memq 'begin process-state)
1332 (and (or (eq state 'first)
1333 (eq state 'first-and-last))
1334 (memq 'ok process-state)))
1335 (when has-been-begin
1336 ;; If there is a `result-file' here, that means that the
1337 ;; file was unsuccessfully decoded, so we delete it.
1338 (when (and result-file
1339 (file-exists-p result-file)
1340 (not gnus-uu-be-dangerous)
1341 (or (eq gnus-uu-be-dangerous t)
1342 (gnus-y-or-n-p
1343 (format "Delete unsuccessfully decoded file %s? "
1344 result-file))))
1345 (delete-file result-file)))
1346 (when (memq 'begin process-state)
1347 (setq result-file (car process-state)))
1348 (setq has-been-begin t))
1349
1350 ;; Check whether we have decoded one complete file.
1351 (when (memq 'end process-state)
1352 (setq article-series nil)
1353 (setq has-been-begin nil)
1354 (if (stringp result-file)
1355 (setq files (list result-file))
1356 (setq files result-file))
1357 (setq result-file (car files))
1358 (while files
1359 (push (list (cons 'name (pop files))
1360 (cons 'article article))
1361 result-files))
1362 ;; Allow user-defined functions to be run on this file.
1363 (when gnus-uu-grabbed-file-functions
1364 (let ((funcs gnus-uu-grabbed-file-functions))
1365 (unless (listp funcs)
1366 (setq funcs (list funcs)))
1367 (while funcs
1368 (funcall (pop funcs) result-file))))
1369 (setq result-file nil)
1370 ;; Check whether we have decoded enough articles.
1371 (and limit (= (length result-files) limit)
1372 (setq articles nil)))
1373
1374 ;; If this is the last article to be decoded, and
1375 ;; we still haven't reached the end, then we delete
1376 ;; the partially decoded file.
1377 (and (or (eq state 'last) (eq state 'first-and-last))
1378 (not (memq 'end process-state))
1379 result-file
1380 (file-exists-p result-file)
1381 (not gnus-uu-be-dangerous)
1382 (or (eq gnus-uu-be-dangerous t)
1383 (gnus-y-or-n-p
1384 (format "Delete incomplete file %s? " result-file)))
1385 (delete-file result-file))
1386
1387 ;; If this was a file of the wrong sort, then
1388 (when (and (or (memq 'wrong-type process-state)
1389 (memq 'error process-state))
1390 gnus-uu-unmark-articles-not-decoded)
1391 (gnus-summary-tick-article article t))
1392
1393 ;; Set the new series state.
1394 (if (and (not has-been-begin)
1395 (not sloppy)
1396 (or (memq 'end process-state)
1397 (memq 'middle process-state)))
1398 (progn
1399 (setq process-state (list 'error))
1400 (gnus-message 2 "No begin part at the beginning")
1401 (sleep-for 2))
1402 (setq state 'middle))))
1403
1404 ;; When there are no result-files, then something must be wrong.
1405 (if result-files
1406 (message "")
1407 (cond
1408 ((not has-been-begin)
1409 (gnus-message 2 "Wrong type file"))
1410 ((memq 'error process-state)
1411 (gnus-message 2 "An error occurred during decoding"))
1412 ((not (or (memq 'ok process-state)
1413 (memq 'end process-state)))
1414 (gnus-message 2 "End of articles reached before end of file")))
1415 ;; Make unsuccessfully decoded articles unread.
1416 (when gnus-uu-unmark-articles-not-decoded
1417 (while article-series
1418 (gnus-summary-tick-article (pop article-series) t))))
1419
1420 ;; The original article buffer is hosed, shoot it down.
1421 (gnus-kill-buffer gnus-original-article-buffer)
1422 (setq gnus-current-article nil)
1423 result-files))
1424
1425 (defun gnus-uu-grab-view (file)
1426 "View FILE using the gnus-uu methods."
1427 (let ((action (gnus-uu-get-action file)))
1428 (gnus-execute-command
1429 (if (string-match "%" action)
1430 (format action file)
1431 (concat action " " file))
1432 (eq gnus-view-pseudos 'not-confirm))))
1433
1434 (defun gnus-uu-grab-move (file)
1435 "Move FILE to somewhere."
1436 (when gnus-uu-default-dir
1437 (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1438 (file-name-nondirectory file))))
1439 (rename-file file to-file)
1440 (unless (file-exists-p file)
1441 (make-symbolic-link to-file file)))))
1442
1443 (defun gnus-uu-part-number (article)
1444 (let* ((header (gnus-summary-article-header article))
1445 (subject (and header (mail-header-subject header)))
1446 (part nil))
1447 (if subject
1448 (while (string-match "[0-9]+/[0-9]+\\|[0-9]+[ \t]+of[ \t]+[0-9]+"
1449 subject)
1450 (setq part (match-string 0 subject))
1451 (setq subject (substring subject (match-end 0)))))
1452 (or part
1453 (while (string-match "[0-9]+[^0-9]+[0-9]+" subject)
1454 (setq part (match-string 0 subject))
1455 (setq subject (substring subject (match-end 0)))))
1456 (or part "")))
1457
1458 (defun gnus-uu-uudecode-sentinel (process event)
1459 (delete-process (get-process process)))
1460
1461 (defun gnus-uu-uustrip-article (process-buffer in-state)
1462 ;; Uudecodes a file asynchronously.
1463 (with-current-buffer process-buffer
1464 (let ((state (list 'wrong-type))
1465 process-connection-type case-fold-search buffer-read-only
1466 files start-char)
1467 (goto-char (point-min))
1468
1469 ;; Deal with ^M at the end of the lines.
1470 (when gnus-uu-kill-carriage-return
1471 (save-excursion
1472 (while (search-forward "\r" nil t)
1473 (delete-char -1))))
1474
1475 (while (or (re-search-forward gnus-uu-begin-string nil t)
1476 (re-search-forward gnus-uu-body-line nil t))
1477 (setq state (list 'ok))
1478 ;; Ok, we are at the first uucoded line.
1479 (beginning-of-line)
1480 (setq start-char (point))
1481
1482 (if (not (looking-at gnus-uu-begin-string))
1483 (setq state (list 'middle))
1484 ;; This is the beginning of a uuencoded article.
1485 ;; We replace certain characters that could make things messy.
1486 (setq gnus-uu-file-name
1487 (gnus-map-function
1488 mm-file-name-rewrite-functions
1489 (file-name-nondirectory (match-string 1))))
1490 (replace-match (concat "begin 644 " gnus-uu-file-name) t t)
1491
1492 ;; Remove any non gnus-uu-body-line right after start.
1493 (forward-line 1)
1494 (while (and (not (eobp))
1495 (not (looking-at gnus-uu-body-line)))
1496 (gnus-delete-line))
1497
1498 ;; If a process is running, we kill it.
1499 (when (and gnus-uu-uudecode-process
1500 (memq (process-status gnus-uu-uudecode-process)
1501 '(run stop)))
1502 (delete-process gnus-uu-uudecode-process)
1503 (gnus-uu-unmark-list-of-grabbed t))
1504
1505 ;; Start a new uudecoding process.
1506 (let ((cdir default-directory))
1507 (unwind-protect
1508 (progn
1509 (cd gnus-uu-work-dir)
1510 (setq gnus-uu-uudecode-process
1511 (start-process
1512 "*uudecode*"
1513 (gnus-get-buffer-create gnus-uu-output-buffer-name)
1514 shell-file-name shell-command-switch
1515 (format "cd %s %s uudecode" gnus-uu-work-dir
1516 gnus-shell-command-separator))))
1517 (cd cdir)))
1518 (set-process-sentinel
1519 gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1520 (setq state (list 'begin))
1521 (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1522
1523 ;; We look for the end of the thing to be decoded.
1524 (if (re-search-forward gnus-uu-end-string nil t)
1525 (push 'end state)
1526 (goto-char (point-max))
1527 (re-search-backward gnus-uu-body-line nil t))
1528
1529 (forward-line 1)
1530
1531 (when gnus-uu-uudecode-process
1532 (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1533 ;; Try to correct mishandled uucode.
1534 (when gnus-uu-correct-stripped-uucode
1535 (gnus-uu-check-correct-stripped-uucode start-char (point)))
1536 (gnus-run-hooks 'gnus-uu-pre-uudecode-hook)
1537
1538 ;; Send the text to the process.
1539 (condition-case nil
1540 (process-send-region
1541 gnus-uu-uudecode-process start-char (point))
1542 (error
1543 (progn
1544 (delete-process gnus-uu-uudecode-process)
1545 (gnus-message 2 "gnus-uu: Couldn't uudecode")
1546 (setq state (list 'wrong-type)))))
1547
1548 (if (memq 'end state)
1549 (progn
1550 ;; Send an EOF, just in case.
1551 (ignore-errors
1552 (process-send-eof gnus-uu-uudecode-process))
1553 (while (memq (process-status gnus-uu-uudecode-process)
1554 '(open run))
1555 (accept-process-output gnus-uu-uudecode-process 1)))
1556 (when (or (not gnus-uu-uudecode-process)
1557 (not (memq (process-status gnus-uu-uudecode-process)
1558 '(run stop))))
1559 (setq state (list 'wrong-type)))))))
1560
1561 (if (memq 'begin state)
1562 (cons (if (= (length files) 1) (car files) files) state)
1563 state))))
1564
1565 (defvar gnus-uu-unshar-warning
1566 "*** WARNING ***
1567
1568 Shell archives are an archaic method of bundling files for distribution
1569 across computer networks. During the unpacking process, arbitrary commands
1570 are executed on your system, and all kinds of nasty things can happen.
1571 Please examine the archive very carefully before you instruct Emacs to
1572 unpack it. You can browse the archive buffer using \\[scroll-other-window].
1573
1574 If you are unsure what to do, please answer \"no\"."
1575 "Text of warning message displayed by `gnus-uu-unshar-article'.
1576 Make sure that this text consists only of few text lines. Otherwise,
1577 Gnus might fail to display all of it.")
1578
1579
1580 ;; This function is used by `gnus-uu-grab-articles' to treat
1581 ;; a shared article.
1582 (defun gnus-uu-unshar-article (process-buffer in-state)
1583 (let ((state (list 'ok))
1584 start-char)
1585 (with-current-buffer process-buffer
1586 (goto-char (point-min))
1587 (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1588 (setq state (list 'wrong-type))
1589 (save-window-excursion
1590 (save-excursion
1591 (switch-to-buffer (current-buffer))
1592 (delete-other-windows)
1593 (let ((buffer (get-buffer-create (generate-new-buffer-name
1594 "*Warning*"))))
1595 (unless
1596 (unwind-protect
1597 (with-current-buffer buffer
1598 (insert (substitute-command-keys
1599 gnus-uu-unshar-warning))
1600 (goto-char (point-min))
1601 (display-buffer buffer)
1602 (yes-or-no-p "This is a shell archive, unshar it? "))
1603 (kill-buffer buffer))
1604 (setq state (list 'error))))))
1605 (unless (memq 'error state)
1606 (beginning-of-line)
1607 (setq start-char (point))
1608 (call-process-region
1609 start-char (point-max) shell-file-name nil
1610 (gnus-get-buffer-create gnus-uu-output-buffer-name) nil
1611 shell-command-switch
1612 (concat "cd " gnus-uu-work-dir " "
1613 gnus-shell-command-separator " sh")))))
1614 state))
1615
1616 ;; Returns the name of what the shar file is going to unpack.
1617 (defun gnus-uu-find-name-in-shar ()
1618 (let ((oldpoint (point))
1619 res)
1620 (goto-char (point-min))
1621 (when (re-search-forward gnus-uu-shar-name-marker nil t)
1622 (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1623 (goto-char oldpoint)
1624 res))
1625
1626 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1627 ;; and `gnus-uu-file-action-list'. Returns either nil if no action is
1628 ;; found, or the name of the command to run if such a rule is found.
1629 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1630 (let ((action-list (copy-sequence file-action-list))
1631 (case-fold-search t)
1632 rule action)
1633 (and
1634 (unless no-ignore
1635 (and (not
1636 (and gnus-uu-ignore-files-by-name
1637 (string-match gnus-uu-ignore-files-by-name file-name)))
1638 (not
1639 (and gnus-uu-ignore-files-by-type
1640 (string-match gnus-uu-ignore-files-by-type
1641 (or (gnus-uu-choose-action
1642 file-name gnus-uu-ext-to-mime-list t)
1643 ""))))))
1644 (while (not (or (eq action-list ()) action))
1645 (setq rule (car action-list))
1646 (setq action-list (cdr action-list))
1647 (when (string-match (car rule) file-name)
1648 (setq action (cadr rule)))))
1649 action))
1650
1651 (defun gnus-uu-treat-archive (file-path)
1652 ;; Unpacks an archive. Returns t if unpacking is successful.
1653 (let ((did-unpack t)
1654 action command dir)
1655 (setq action (gnus-uu-choose-action
1656 file-path (append gnus-uu-user-archive-rules
1657 (if gnus-uu-ignore-default-archive-rules
1658 nil
1659 gnus-uu-default-archive-rules))))
1660
1661 (when (not action)
1662 (error "No unpackers for the file %s" file-path))
1663
1664 (string-match "/[^/]*$" file-path)
1665 (setq dir (substring file-path 0 (match-beginning 0)))
1666
1667 (when (member action gnus-uu-destructive-archivers)
1668 (copy-file file-path (concat file-path "~") t))
1669
1670 (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1671
1672 (with-current-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name)
1673 (erase-buffer))
1674
1675 (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1676
1677 (if (eq 0 (call-process shell-file-name nil
1678 (gnus-get-buffer-create gnus-uu-output-buffer-name)
1679 nil shell-command-switch command))
1680 (message "")
1681 (gnus-message 2 "Error during unpacking of archive")
1682 (setq did-unpack nil))
1683
1684 (when (member action gnus-uu-destructive-archivers)
1685 (rename-file (concat file-path "~") file-path t))
1686
1687 did-unpack))
1688
1689 (defun gnus-uu-dir-files (dir)
1690 (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1691 files file)
1692 (while dirs
1693 (if (file-directory-p (setq file (car dirs)))
1694 (setq files (append files (gnus-uu-dir-files file)))
1695 (push file files))
1696 (setq dirs (cdr dirs)))
1697 files))
1698
1699 (defun gnus-uu-unpack-files (files &optional ignore)
1700 ;; Go through FILES and look for files to unpack.
1701 (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1702 (ofiles files)
1703 file did-unpack)
1704 (while files
1705 (setq file (cdr (assq 'name (car files))))
1706 (when (and (not (member file ignore))
1707 (equal (gnus-uu-get-action (file-name-nondirectory file))
1708 "gnus-uu-archive"))
1709 (push file did-unpack)
1710 (unless (gnus-uu-treat-archive file)
1711 (gnus-message 2 "Error during unpacking of %s" file))
1712 (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1713 (nfiles newfiles))
1714 (while nfiles
1715 (unless (member (car nfiles) totfiles)
1716 (push (list (cons 'name (car nfiles))
1717 (cons 'original file))
1718 ofiles))
1719 (setq nfiles (cdr nfiles)))
1720 (setq totfiles newfiles)))
1721 (setq files (cdr files)))
1722 (if did-unpack
1723 (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1724 ofiles)))
1725
1726 (defun gnus-uu-ls-r (dir)
1727 (let* ((files (gnus-uu-directory-files dir t))
1728 (ofiles files))
1729 (while files
1730 (when (file-directory-p (car files))
1731 (setq ofiles (delete (car files) ofiles))
1732 (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1733 (setq files (cdr files)))
1734 ofiles))
1735
1736 ;; Various stuff
1737
1738 (defun gnus-uu-directory-files (dir &optional full)
1739 (let (files out file)
1740 (setq files (directory-files dir full))
1741 (while files
1742 (setq file (car files))
1743 (setq files (cdr files))
1744 (unless (member (file-name-nondirectory file) '("." ".."))
1745 (push file out)))
1746 (setq out (nreverse out))
1747 out))
1748
1749 (defun gnus-uu-check-correct-stripped-uucode (start end)
1750 (save-excursion
1751 (let (found beg length)
1752 (unless gnus-uu-correct-stripped-uucode
1753 (goto-char start)
1754
1755 (if (re-search-forward " \\|`" end t)
1756 (progn
1757 (goto-char start)
1758 (while (not (eobp))
1759 (progn
1760 (when (looking-at "\n")
1761 (replace-match ""))
1762 (forward-line 1))))
1763
1764 (while (not (eobp))
1765 (unless (looking-at (concat gnus-uu-begin-string "\\|"
1766 gnus-uu-end-string))
1767 (when (not found)
1768 (setq length (- (point-at-eol) (point-at-bol))))
1769 (setq found t)
1770 (beginning-of-line)
1771 (setq beg (point))
1772 (end-of-line)
1773 (unless (= length (- (point) beg))
1774 (insert (make-string (- length (- (point) beg)) ? ))))
1775 (forward-line 1)))))))
1776
1777 (defvar gnus-uu-tmp-alist nil)
1778
1779 (defun gnus-uu-initialize (&optional scan)
1780 (let (entry)
1781 (if (and (not scan)
1782 (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1783 (if (file-exists-p (cdr entry))
1784 (setq gnus-uu-work-dir (cdr entry))
1785 (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1786 nil)))
1787 t
1788 (setq gnus-uu-tmp-dir (file-name-as-directory
1789 (expand-file-name gnus-uu-tmp-dir)))
1790 (if (not (file-directory-p gnus-uu-tmp-dir))
1791 (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1792 (when (not (file-writable-p gnus-uu-tmp-dir))
1793 (error "Temp directory %s can't be written to"
1794 gnus-uu-tmp-dir)))
1795
1796 (setq gnus-uu-work-dir
1797 (mm-make-temp-file (concat gnus-uu-tmp-dir "gnus") 'dir))
1798 (gnus-set-file-modes gnus-uu-work-dir 448)
1799 (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1800 (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1801 gnus-uu-tmp-alist))))
1802
1803
1804 ;; Kills the temporary uu buffers, kills any processes, etc.
1805 (defun gnus-uu-clean-up ()
1806 (let (buf)
1807 (and gnus-uu-uudecode-process
1808 (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1809 '(stop run))
1810 (delete-process gnus-uu-uudecode-process))
1811 (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1812 (kill-buffer buf))))
1813
1814 ;; Inputs an action and a filename and returns a full command, making sure
1815 ;; that the filename will be treated as a single argument when the shell
1816 ;; executes the command.
1817 (defun gnus-uu-command (action file)
1818 (let ((quoted-file (shell-quote-argument file)))
1819 (if (string-match "%s" action)
1820 (format action quoted-file)
1821 (concat action " " quoted-file))))
1822
1823 (defun gnus-uu-delete-work-dir (&optional dir)
1824 "Delete recursively all files and directories under `gnus-uu-work-dir'."
1825 (if dir
1826 (gnus-message 7 "Deleting directory %s..." dir)
1827 (setq dir gnus-uu-work-dir))
1828 (when (and dir
1829 (file-exists-p dir))
1830 (let ((files (directory-files dir t nil t))
1831 file)
1832 (while (setq file (pop files))
1833 (unless (member (file-name-nondirectory file) '("." ".."))
1834 (if (file-directory-p file)
1835 (gnus-uu-delete-work-dir file)
1836 (gnus-message 9 "Deleting file %s..." file)
1837 (condition-case err
1838 (delete-file file)
1839 (error (gnus-message 3 "Deleting file %s failed... %s" file err))))))
1840 (condition-case err
1841 (delete-directory dir)
1842 (error (gnus-message 3 "Deleting directory %s failed... %s" file err))))
1843 (gnus-message 7 "")))
1844
1845 ;; Initializing
1846
1847 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1848 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1849
1850 \f
1851
1852 ;;;
1853 ;;; uuencoded posting
1854 ;;;
1855
1856 ;; Any function that is to be used as and encoding method will take two
1857 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1858 ;; and "spiral.jpg", respectively.) The function should return nil if
1859 ;; the encoding wasn't successful.
1860 (defcustom gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1861 "Function used for encoding binary files.
1862 There are three functions supplied with gnus-uu for encoding files:
1863 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1864 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1865 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1866 uuencode and adds MIME headers."
1867 :group 'gnus-extract-post
1868 :type '(radio (function-item gnus-uu-post-encode-uuencode)
1869 (function-item gnus-uu-post-encode-mime)
1870 (function-item gnus-uu-post-encode-mime-uuencode)
1871 (function :tag "Other")))
1872
1873 (defcustom gnus-uu-post-include-before-composing nil
1874 "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1875 If this variable is t, you can either include an encoded file with
1876 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article."
1877 :group 'gnus-extract-post
1878 :type 'boolean)
1879
1880 (defcustom gnus-uu-post-length 990
1881 "Maximum length of an article.
1882 The encoded file will be split into how many articles it takes to
1883 post the entire file."
1884 :group 'gnus-extract-post
1885 :type 'integer)
1886
1887 (defcustom gnus-uu-post-threaded nil
1888 "Non-nil means that gnus-uu will post the encoded file in a thread.
1889 This may not be smart, as no other decoder I have seen are able to
1890 follow threads when collecting uuencoded articles. (Well, I have seen
1891 one package that does that - gnus-uu, but somehow, I don't think that
1892 counts...) The default is nil."
1893 :group 'gnus-extract-post
1894 :type 'boolean)
1895
1896 (defcustom gnus-uu-post-separate-description t
1897 "Non-nil means that the description will be posted in a separate article.
1898 The first article will typically be numbered (0/x). If this variable
1899 is nil, the description the user enters will be included at the
1900 beginning of the first article, which will be numbered (1/x). Default
1901 is t."
1902 :group 'gnus-extract-post
1903 :type 'boolean)
1904
1905 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1906 (defvar gnus-uu-post-message-id nil)
1907 (defvar gnus-uu-post-inserted-file-name nil)
1908 (defvar gnus-uu-winconf-post-news nil)
1909
1910 (defun gnus-uu-post-news ()
1911 "Compose an article and post an encoded file."
1912 (interactive)
1913 (setq gnus-uu-post-inserted-file-name nil)
1914 (setq gnus-uu-winconf-post-news (current-window-configuration))
1915
1916 (gnus-summary-post-news)
1917
1918 (let ((map (make-sparse-keymap)))
1919 (set-keymap-parent map (current-local-map))
1920 (use-local-map map))
1921 ;;(local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1922 (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1923 (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1924 (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1925
1926 (when gnus-uu-post-include-before-composing
1927 (save-excursion (setq gnus-uu-post-inserted-file-name
1928 (gnus-uu-post-insert-binary)))))
1929
1930 (defun gnus-uu-post-insert-binary-in-article ()
1931 "Inserts an encoded file in the buffer.
1932 The user will be asked for a file name."
1933 (interactive)
1934 (save-excursion
1935 (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1936
1937 ;; Encodes with uuencode and substitutes all spaces with backticks.
1938 (defun gnus-uu-post-encode-uuencode (path file-name)
1939 (when (gnus-uu-post-encode-file "uuencode" path file-name)
1940 (goto-char (point-min))
1941 (forward-line 1)
1942 (while (search-forward " " nil t)
1943 (replace-match "`"))
1944 t))
1945
1946 ;; Encodes with uuencode and adds MIME headers.
1947 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1948 (when (gnus-uu-post-encode-uuencode path file-name)
1949 (gnus-uu-post-make-mime file-name "x-uue")
1950 t))
1951
1952 ;; Encodes with base64 and adds MIME headers
1953 (defun gnus-uu-post-encode-mime (path file-name)
1954 (when (eq 0 (call-process shell-file-name nil t nil shell-command-switch
1955 (format "%s %s -o %s" "mmencode" path file-name)))
1956 (gnus-uu-post-make-mime file-name "base64")
1957 t))
1958
1959 ;; Adds MIME headers.
1960 (defun gnus-uu-post-make-mime (file-name encoding)
1961 (goto-char (point-min))
1962 (insert (format "Content-Type: %s; name=\"%s\"\n"
1963 (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1964 file-name))
1965 (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1966 (save-restriction
1967 (set-buffer gnus-message-buffer)
1968 (goto-char (point-min))
1969 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1970 (forward-line -1)
1971 (narrow-to-region (point-min) (point))
1972 (unless (mail-fetch-field "mime-version")
1973 (widen)
1974 (insert "MIME-Version: 1.0\n"))
1975 (widen)))
1976
1977 ;; Encodes a file PATH with COMMAND, leaving the result in the
1978 ;; current buffer.
1979 (defun gnus-uu-post-encode-file (command path file-name)
1980 (eq 0 (call-process shell-file-name nil t nil shell-command-switch
1981 (format "%s %s %s" command path file-name))))
1982
1983 (defun gnus-uu-post-news-inews ()
1984 "Posts the composed news article and encoded file.
1985 If no file has been included, the user will be asked for a file."
1986 (interactive)
1987
1988 (let (file-name)
1989
1990 (if gnus-uu-post-inserted-file-name
1991 (setq file-name gnus-uu-post-inserted-file-name)
1992 (setq file-name (gnus-uu-post-insert-binary)))
1993
1994 (gnus-uu-post-encoded file-name gnus-uu-post-threaded))
1995 (setq gnus-uu-post-inserted-file-name nil)
1996 (when gnus-uu-winconf-post-news
1997 (set-window-configuration gnus-uu-winconf-post-news)))
1998
1999 ;; Asks for a file to encode, encodes it and inserts the result in
2000 ;; the current buffer. Returns the file name the user gave.
2001 (defun gnus-uu-post-insert-binary ()
2002 (let ((uuencode-buffer-name "*uuencode buffer*")
2003 file-path uubuf file-name)
2004
2005 (setq file-path (read-file-name
2006 "What file do you want to encode? "))
2007 (when (not (file-exists-p file-path))
2008 (error "%s: No such file" file-path))
2009
2010 (goto-char (point-max))
2011 (insert (format "\n%s\n" gnus-uu-post-binary-separator))
2012
2013 ;; #### Unix-specific?
2014 (when (string-match "^~/" file-path)
2015 (setq file-path (concat "$HOME" (substring file-path 1))))
2016 ;; #### Unix-specific?
2017 (if (string-match "/[^/]*$" file-path)
2018 (setq file-name (substring file-path (1+ (match-beginning 0))))
2019 (setq file-name file-path))
2020
2021 (unwind-protect
2022 (if (with-current-buffer
2023 (setq uubuf (gnus-get-buffer-create uuencode-buffer-name))
2024 (erase-buffer)
2025 (funcall gnus-uu-post-encode-method file-path file-name))
2026 (insert-buffer-substring uubuf)
2027 (error "Encoding unsuccessful"))
2028 (kill-buffer uubuf))
2029 file-name))
2030
2031 ;; Posts the article and all of the encoded file.
2032 (defun gnus-uu-post-encoded (file-name &optional threaded)
2033 (let ((send-buffer-name "*uuencode send buffer*")
2034 (encoded-buffer-name "*encoded buffer*")
2035 (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
2036 (separator (concat mail-header-separator "\n\n"))
2037 uubuf length parts header i end beg
2038 beg-line minlen post-buf whole-len beg-binary end-binary)
2039
2040 (setq post-buf (current-buffer))
2041
2042 (goto-char (point-min))
2043 (when (not (re-search-forward
2044 (if gnus-uu-post-separate-description
2045 (concat "^" (regexp-quote gnus-uu-post-binary-separator)
2046 "$")
2047 (concat "^" (regexp-quote mail-header-separator) "$"))
2048 nil t))
2049 (error "Internal error: No binary/header separator"))
2050 (beginning-of-line)
2051 (forward-line 1)
2052 (setq beg-binary (point))
2053 (setq end-binary (point-max))
2054
2055 (with-current-buffer
2056 (setq uubuf (gnus-get-buffer-create encoded-buffer-name))
2057 (erase-buffer)
2058 (insert-buffer-substring post-buf beg-binary end-binary)
2059 (goto-char (point-min))
2060 (setq length (count-lines (point-min) (point-max)))
2061 (setq parts (/ length gnus-uu-post-length))
2062 (unless (< (% length gnus-uu-post-length) 4)
2063 (incf parts)))
2064
2065 (when gnus-uu-post-separate-description
2066 (forward-line -1))
2067 (delete-region (point) (point-max))
2068
2069 (goto-char (point-min))
2070 (re-search-forward
2071 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2072 (setq header (buffer-substring (point-min) (point-at-bol)))
2073
2074 (goto-char (point-min))
2075 (when gnus-uu-post-separate-description
2076 (when (re-search-forward "^Subject: " nil t)
2077 (end-of-line)
2078 (insert (format " (0/%d)" parts)))
2079 (save-excursion
2080 (message-send))
2081 (setq gnus-uu-post-message-id (message-fetch-field "message-id")))
2082
2083 (save-excursion
2084 (setq i 1)
2085 (setq beg 1)
2086 (while (not (> i parts))
2087 (set-buffer (gnus-get-buffer-create send-buffer-name))
2088 (erase-buffer)
2089 (insert header)
2090 (when (and threaded gnus-uu-post-message-id)
2091 (insert "References: " gnus-uu-post-message-id "\n"))
2092 (insert separator)
2093 (setq whole-len
2094 (- 62 (length (format top-string "" file-name i parts ""))))
2095 (when (> 1 (setq minlen (/ whole-len 2)))
2096 (setq minlen 1))
2097 (setq
2098 beg-line
2099 (format top-string
2100 (make-string minlen ?-)
2101 file-name i parts
2102 (make-string
2103 (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
2104
2105 (goto-char (point-min))
2106 (when (re-search-forward "^Subject: " nil t)
2107 (end-of-line)
2108 (insert (format " (%d/%d)" i parts)))
2109
2110 (goto-char (point-max))
2111 (with-current-buffer uubuf
2112 (goto-char beg)
2113 (if (= i parts)
2114 (goto-char (point-max))
2115 (forward-line gnus-uu-post-length))
2116 (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
2117 (forward-line -4))
2118 (setq end (point)))
2119 (insert-buffer-substring uubuf beg end)
2120 (insert beg-line "\n")
2121 (setq beg end)
2122 (incf i)
2123 (goto-char (point-min))
2124 (re-search-forward
2125 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2126 (beginning-of-line)
2127 (forward-line 2)
2128 (when (re-search-forward
2129 (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
2130 nil t)
2131 (replace-match "")
2132 (forward-line 1))
2133 (insert beg-line)
2134 (insert "\n")
2135 (let (message-sent-message-via)
2136 (save-excursion
2137 (message-send))
2138 (setq gnus-uu-post-message-id
2139 (concat (message-fetch-field "references") " "
2140 (message-fetch-field "message-id"))))))
2141
2142 (gnus-kill-buffer send-buffer-name)
2143 (gnus-kill-buffer encoded-buffer-name)
2144
2145 (when (not gnus-uu-post-separate-description)
2146 (set-buffer-modified-p nil)
2147 (bury-buffer))))
2148
2149 (provide 'gnus-uu)
2150
2151 ;;; gnus-uu.el ends here