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