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