553f0d946d0efe0035cb89523c57e1b093d45b2b
[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 (nreverse (gnus-uu-find-articles-matching))))
492       ;; Specify articles to be forwarded.  Note that they should be
493       ;; reversed; see `gnus-uu-get-list-of-articles'.
494       (setq gnus-newsgroup-processable (reverse gnus-article-reply))
495       (gnus-setup-message 'forward
496         (setq gnus-uu-digest-from-subject nil)
497         (setq gnus-uu-digest-buffer
498               (gnus-get-buffer-create " *gnus-uu-forward*"))
499         (gnus-uu-decode-save n file)
500         (switch-to-buffer gnus-uu-digest-buffer)
501         (let ((fs gnus-uu-digest-from-subject))
502           (when fs
503             (setq from (caar fs)
504                   subject (gnus-simplify-subject-fuzzy (cdar fs))
505                   fs (cdr fs))
506             (while (and fs (or from subject))
507               (when from
508                 (unless (string= from (caar fs))
509                   (setq from nil)))
510               (when subject
511                 (unless (string= (gnus-simplify-subject-fuzzy (cdar fs))
512                                  subject)
513                   (setq subject nil)))
514               (setq fs (cdr fs))))
515           (unless subject
516             (setq subject "Digested Articles"))
517           (unless from
518             (setq from
519                   (if (gnus-news-group-p gnus-newsgroup-name)
520                       gnus-newsgroup-name
521                     "Various"))))
522         (goto-char (point-min))
523         (when (re-search-forward "^Subject: ")
524           (delete-region (point) (point-at-eol))
525           (insert subject))
526         (goto-char (point-min))
527         (when (re-search-forward "^From:")
528           (delete-region (point) (point-at-eol))
529           (insert " " from))
530         (let ((message-forward-decoded-p t))
531           (message-forward post t))))
532     (setq gnus-uu-digest-from-subject nil)))
533
534 (defun gnus-uu-digest-post-forward (&optional n)
535   "Digest and forward to a newsgroup."
536   (interactive "P")
537   (gnus-uu-digest-mail-forward n t))
538
539 ;; Process marking.
540
541 (defun gnus-message-process-mark (unmarkp new-marked)
542   (let ((old (- (length gnus-newsgroup-processable) (length new-marked))))
543     (gnus-message 6 "%d mark%s %s%s"
544                   (length new-marked)
545                   (if (= (length new-marked) 1) "" "s")
546                   (if unmarkp "removed" "added")
547                   (cond
548                    ((and (zerop old)
549                          (not unmarkp))
550                     "")
551                    (unmarkp
552                     (format ", %d remain marked"
553                             (length gnus-newsgroup-processable)))
554                    (t
555                     (format ", %d already marked" old))))))
556
557 (defun gnus-new-processable (unmarkp articles)
558   (if unmarkp
559       (gnus-intersection gnus-newsgroup-processable articles)
560     (gnus-set-difference articles gnus-newsgroup-processable)))
561
562 (defun gnus-uu-mark-by-regexp (regexp &optional unmark)
563   "Set the process mark on articles whose subjects match REGEXP.
564 When called interactively, prompt for REGEXP.
565 Optional UNMARK non-nil means unmark instead of mark."
566   (interactive "sMark (regexp): \nP")
567   (save-excursion
568     (let* ((articles (gnus-uu-find-articles-matching regexp))
569            (new-marked (gnus-new-processable unmark articles)))
570       (while articles
571         (if unmark
572             (gnus-summary-remove-process-mark (pop articles))
573           (gnus-summary-set-process-mark (pop articles))))
574       (gnus-message-process-mark unmark new-marked)))
575   (gnus-summary-position-point))
576
577 (defun gnus-uu-unmark-by-regexp (regexp)
578   "Remove the process mark from articles whose subjects match REGEXP.
579 When called interactively, prompt for REGEXP."
580   (interactive "sUnmark (regexp): ")
581   (gnus-uu-mark-by-regexp regexp t))
582
583 (defun gnus-uu-mark-series (&optional silent)
584   "Mark the current series with the process mark."
585   (interactive)
586   (let* ((articles (gnus-uu-find-articles-matching))
587          (l (length articles)))
588     (while articles
589       (gnus-summary-set-process-mark (car articles))
590       (setq articles (cdr articles)))
591     (unless silent
592       (gnus-message 6 "Marked %d articles" l))
593     (gnus-summary-position-point)
594     l))
595
596 (defun gnus-uu-mark-region (beg end &optional unmark)
597   "Set the process mark on all articles between point and mark."
598   (interactive "r")
599   (save-excursion
600     (goto-char beg)
601     (while (< (point) end)
602       (if unmark
603           (gnus-summary-remove-process-mark (gnus-summary-article-number))
604         (gnus-summary-set-process-mark (gnus-summary-article-number)))
605       (forward-line 1)))
606   (gnus-summary-position-point))
607
608 (defun gnus-uu-unmark-region (beg end)
609   "Remove the process mark from all articles between point and mark."
610   (interactive "r")
611   (gnus-uu-mark-region beg end t))
612
613 (defun gnus-uu-mark-buffer ()
614   "Set the process mark on all articles in the buffer."
615   (interactive)
616   (gnus-uu-mark-region (point-min) (point-max)))
617
618 (defun gnus-uu-unmark-buffer ()
619   "Remove the process mark on all articles in the buffer."
620   (interactive)
621   (gnus-uu-mark-region (point-min) (point-max) t))
622
623 (defun gnus-uu-mark-thread ()
624   "Marks all articles downwards in this thread."
625   (interactive)
626   (gnus-save-hidden-threads
627     (let ((level (gnus-summary-thread-level)))
628       (while (and (gnus-summary-set-process-mark
629                    (gnus-summary-article-number))
630                   (zerop (gnus-summary-next-subject 1 nil t))
631                   (> (gnus-summary-thread-level) level)))))
632   (gnus-summary-position-point))
633
634 (defun gnus-uu-unmark-thread ()
635   "Unmarks all articles downwards in this thread."
636   (interactive)
637   (let ((level (gnus-summary-thread-level)))
638     (while (and (gnus-summary-remove-process-mark
639                  (gnus-summary-article-number))
640                 (zerop (gnus-summary-next-subject 1))
641                 (> (gnus-summary-thread-level) level))))
642   (gnus-summary-position-point))
643
644 (defun gnus-uu-invert-processable ()
645   "Invert the list of process-marked articles."
646   (interactive)
647   (let ((data gnus-newsgroup-data)
648         number)
649     (save-excursion
650       (while data
651         (if (memq (setq number (gnus-data-number (pop data)))
652                   gnus-newsgroup-processable)
653             (gnus-summary-remove-process-mark number)
654           (gnus-summary-set-process-mark number)))))
655   (gnus-summary-position-point))
656
657 (defun gnus-uu-mark-over (&optional score)
658   "Mark all articles with a score over SCORE (the prefix)."
659   (interactive "P")
660   (let ((score (or score gnus-summary-default-score 0))
661         (data gnus-newsgroup-data))
662     (save-excursion
663       (while data
664         (when (> (or (cdr (assq (gnus-data-number (car data))
665                                 gnus-newsgroup-scored))
666                      gnus-summary-default-score 0)
667                  score)
668           (gnus-summary-set-process-mark (caar data)))
669         (setq data (cdr data))))
670     (gnus-summary-position-point)))
671
672 (defun gnus-uu-mark-sparse ()
673   "Mark all series that have some articles marked."
674   (interactive)
675   (let ((marked (nreverse gnus-newsgroup-processable))
676         subject articles total headers)
677     (unless marked
678       (error "No articles marked with the process mark"))
679     (setq gnus-newsgroup-processable nil)
680     (save-excursion
681       (while marked
682         (and (vectorp (setq headers
683                             (gnus-summary-article-header (car marked))))
684              (setq subject (mail-header-subject headers)
685                    articles (gnus-uu-find-articles-matching
686                              (gnus-uu-reginize-string subject))
687                    total (nconc total articles)))
688         (while articles
689           (gnus-summary-set-process-mark (car articles))
690           (setcdr marked (delq (car articles) (cdr marked)))
691           (setq articles (cdr articles)))
692         (setq marked (cdr marked)))
693       (setq gnus-newsgroup-processable (nreverse total)))
694     (gnus-summary-position-point)))
695
696 (defun gnus-uu-mark-all ()
697   "Mark all articles in \"series\" order."
698   (interactive)
699   (setq gnus-newsgroup-processable nil)
700   (save-excursion
701     (let ((data gnus-newsgroup-data)
702           (count 0)
703           number)
704       (while data
705         (when (and (not (memq (setq number (gnus-data-number (car data)))
706                               gnus-newsgroup-processable))
707                    (vectorp (gnus-data-header (car data))))
708           (gnus-summary-goto-subject number)
709           (setq count (+ count (gnus-uu-mark-series t))))
710         (setq data (cdr data)))
711       (gnus-message 6 "Marked %d articles" count)))
712   (gnus-summary-position-point))
713
714 ;; All PostScript functions written by Erik Selberg <speed@cs.washington.edu>.
715
716 (defun gnus-uu-decode-postscript (&optional n)
717   "Gets postscript of the current article."
718   (interactive "P")
719   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article n))
720
721 (defun gnus-uu-decode-postscript-view (&optional n)
722   "Gets and views the current article."
723   (interactive "P")
724   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
725     (gnus-uu-decode-postscript n)))
726
727 (defun gnus-uu-decode-postscript-and-save (n dir)
728   "Extracts postscript and saves the current article."
729   (interactive
730    (list current-prefix-arg
731          (file-name-as-directory
732           (read-file-name "Save in dir: "
733                           gnus-uu-default-dir
734                           gnus-uu-default-dir t))))
735   (gnus-uu-decode-with-method 'gnus-uu-decode-postscript-article
736                               n dir nil nil t))
737
738 (defun gnus-uu-decode-postscript-and-save-view (n dir)
739   "Decodes, views and saves the resulting file."
740   (interactive
741    (list current-prefix-arg
742          (read-file-name "Where do you want to save the file(s)? "
743                          gnus-uu-default-dir
744                          gnus-uu-default-dir t)))
745   (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
746     (gnus-uu-decode-postscript-and-save n dir)))
747
748
749 ;; Internal functions.
750
751 (defun gnus-uu-decode-with-method (method n &optional save not-insert
752                                           scan cdir)
753   (gnus-uu-initialize scan)
754   (when save
755     (setq gnus-uu-default-dir save))
756   ;; Create the directory we save to.
757   (when (and scan cdir save
758              (not (file-exists-p save)))
759     (make-directory save t))
760   (let ((articles (gnus-uu-get-list-of-articles n))
761         files)
762     (setq files (gnus-uu-grab-articles articles method t))
763     (let ((gnus-current-article (car articles)))
764       (when scan
765         (setq files (gnus-uu-scan-directory gnus-uu-work-dir))))
766     (when save
767       (gnus-uu-save-files files save))
768     (when (eq gnus-uu-do-not-unpack-archives nil)
769       (setq files (gnus-uu-unpack-files files)))
770     (setq files (nreverse (gnus-uu-get-actions files)))
771     (or not-insert (not gnus-insert-pseudo-articles)
772         (gnus-summary-insert-pseudos files save))))
773
774 (defun gnus-uu-scan-directory (dir &optional rec)
775   "Return a list of all files under DIR."
776   (let ((files (directory-files dir t))
777         out file)
778     (while (setq file (pop files))
779       (unless (member (file-name-nondirectory file) '("." ".."))
780         (push (list (cons 'name file)
781                     (cons 'article gnus-current-article))
782               out)
783         (when (file-directory-p file)
784           (setq out (nconc (gnus-uu-scan-directory file t) out)))))
785     (if rec
786         out
787       (nreverse out))))
788
789 (defun gnus-uu-save-files (files dir)
790   "Save FILES in DIR."
791   (let ((len (length files))
792         (reg (concat "^" (regexp-quote gnus-uu-work-dir)))
793         to-file file fromdir)
794     (while (setq file (cdr (assq 'name (pop files))))
795       (when (file-exists-p file)
796         (string-match reg file)
797         (setq fromdir (substring file (match-end 0)))
798         (if (file-directory-p file)
799             (gnus-make-directory (concat dir fromdir))
800           (setq to-file (concat dir fromdir))
801           (when (or (not (file-exists-p to-file))
802                     (eq gnus-uu-be-dangerous t)
803                     (and gnus-uu-be-dangerous
804                          (gnus-y-or-n-p (format "%s exists; overwrite? "
805                                                 to-file))))
806             (copy-file file to-file t t)))))
807     (gnus-message 5 "Saved %d file%s" len (if (= len 1) "" "s"))))
808
809 ;; Functions for saving and possibly digesting articles without
810 ;; any decoding.
811
812 ;; Function called by gnus-uu-grab-articles to treat each article.
813 (defun gnus-uu-save-article (buffer in-state)
814   (cond
815    (gnus-uu-save-separate-articles
816     (save-excursion
817       (set-buffer buffer)
818       (let ((coding-system-for-write mm-text-coding-system))
819         (gnus-write-buffer
820          (concat gnus-uu-saved-article-name gnus-current-article)))
821       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
822             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
823                                                  'begin 'end))
824             ((eq in-state 'last) (list 'end))
825             (t (list 'middle)))))
826    ((not gnus-uu-save-in-digest)
827     (save-excursion
828       (set-buffer buffer)
829       (write-region (point-min) (point-max) gnus-uu-saved-article-name t)
830       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
831             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name
832                                                  'begin 'end))
833             ((eq in-state 'last) (list 'end))
834             (t (list 'middle)))))
835    (t
836     (let ((header (gnus-summary-article-header)))
837       (push (cons (mail-header-from header)
838                   (mail-header-subject header))
839             gnus-uu-digest-from-subject))
840     (let ((name (file-name-nondirectory gnus-uu-saved-article-name))
841           beg subj headers headline sorthead body end-string state)
842       (if (or (eq in-state 'first)
843               (eq in-state 'first-and-last))
844           (progn
845             (setq state (list 'begin))
846             (save-excursion
847               (set-buffer (gnus-get-buffer-create "*gnus-uu-body*"))
848               (erase-buffer))
849             (save-excursion
850               (set-buffer (gnus-get-buffer-create "*gnus-uu-pre*"))
851               (erase-buffer)
852               (insert (format
853                        "Date: %s\nFrom: %s\nSubject: %s Digest\n\n"
854                        (message-make-date) name name))
855               (when (and message-forward-as-mime gnus-uu-digest-buffer)
856                 (insert "<#part type=message/rfc822>\nSubject: Topics\n\n"))
857               (insert "Topics:\n")))
858         (when (not (eq in-state 'end))
859           (setq state (list 'middle))))
860       (save-excursion
861         (set-buffer "*gnus-uu-body*")
862         (goto-char (setq beg (point-max)))
863         (save-excursion
864           (save-restriction
865             (set-buffer buffer)
866             (let (buffer-read-only)
867               (set-text-properties (point-min) (point-max) nil)
868               ;; These two are necessary for XEmacs 19.12 fascism.
869               (put-text-property (point-min) (point-max) 'invisible nil)
870               (put-text-property (point-min) (point-max) 'intangible nil))
871             (when (and message-forward-as-mime
872                        message-forward-show-mml
873                        gnus-uu-digest-buffer)
874               (mm-enable-multibyte)
875               (mime-to-mml))
876             (goto-char (point-min))
877             (search-forward "\n\n")
878             (unless (and message-forward-as-mime gnus-uu-digest-buffer)
879               ;; Quote all 30-dash lines.
880               (save-excursion
881                 (while (re-search-forward "^-" nil t)
882                   (beginning-of-line)
883                   (delete-char 1)
884                   (insert "- "))))
885             (setq body (buffer-substring (1- (point)) (point-max)))
886             (narrow-to-region (point-min) (point))
887             (if (not (setq headers gnus-uu-digest-headers))
888                 (setq sorthead (buffer-string))
889               (while headers
890                 (setq headline (car headers))
891                 (setq headers (cdr headers))
892                 (goto-char (point-min))
893                 (while (re-search-forward headline nil t)
894                   (setq sorthead
895                         (concat sorthead
896                                 (buffer-substring
897                                  (match-beginning 0)
898                                  (or (and (re-search-forward "^[^ \t]" nil t)
899                                           (1- (point)))
900                                      (progn (forward-line 1) (point)))))))))
901             (widen)))
902         (if (and message-forward-as-mime gnus-uu-digest-buffer)
903           (if message-forward-show-mml
904               (progn
905                 (insert "\n<#mml type=message/rfc822>\n")
906                 (insert sorthead) (goto-char (point-max))
907                 (insert body) (goto-char (point-max))
908                 (insert "\n<#/mml>\n"))
909             (let ((buf (mml-generate-new-buffer " *mml*")))
910               (with-current-buffer buf
911                 (insert sorthead)
912                 (goto-char (point-min))
913                 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
914                   (setq subj (buffer-substring (match-beginning 1)
915                                                (match-end 1))))
916                 (goto-char (point-max))
917                 (insert body))
918               (insert "\n<#part type=message/rfc822"
919                       " buffer=\"" (buffer-name buf) "\">\n")))
920           (insert sorthead) (goto-char (point-max))
921           (insert body) (goto-char (point-max))
922           (insert (concat "\n" (make-string 30 ?-) "\n\n")))
923         (goto-char beg)
924         (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
925           (setq subj (buffer-substring (match-beginning 1) (match-end 1))))
926         (when subj
927           (save-excursion
928             (set-buffer "*gnus-uu-pre*")
929             (insert (format "   %s\n" subj)))))
930       (when (or (eq in-state 'last)
931                 (eq in-state 'first-and-last))
932         (if (and message-forward-as-mime gnus-uu-digest-buffer)
933             (with-current-buffer gnus-uu-digest-buffer
934               (erase-buffer)
935               (insert-buffer-substring "*gnus-uu-pre*")
936               (goto-char (point-max))
937               (insert-buffer-substring "*gnus-uu-body*"))
938           (save-excursion
939             (set-buffer "*gnus-uu-pre*")
940             (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
941             (if gnus-uu-digest-buffer
942                 (with-current-buffer gnus-uu-digest-buffer
943                   (erase-buffer)
944                   (insert-buffer-substring "*gnus-uu-pre*"))
945               (let ((coding-system-for-write mm-text-coding-system))
946                 (gnus-write-buffer gnus-uu-saved-article-name))))
947           (save-excursion
948             (set-buffer "*gnus-uu-body*")
949             (goto-char (point-max))
950             (insert
951              (concat (setq end-string (format "End of %s Digest" name))
952                      "\n"))
953             (insert (concat (make-string (length end-string) ?*) "\n"))
954             (if gnus-uu-digest-buffer
955                 (with-current-buffer gnus-uu-digest-buffer
956                   (goto-char (point-max))
957                   (insert-buffer-substring "*gnus-uu-body*"))
958               (let ((coding-system-for-write mm-text-coding-system)
959                     (file-name-coding-system nnmail-pathname-coding-system))
960                 (write-region
961                  (point-min) (point-max) gnus-uu-saved-article-name t)))))
962         (gnus-kill-buffer "*gnus-uu-pre*")
963         (gnus-kill-buffer "*gnus-uu-body*")
964         (push 'end state))
965       (if (memq 'begin state)
966           (cons gnus-uu-saved-article-name state)
967         state)))))
968
969 ;; Binhex treatment - not very advanced.
970
971 (defvar gnus-uu-binhex-body-line
972   "^[^:]...............................................................$")
973 (defvar gnus-uu-binhex-begin-line
974   "^:...............................................................$")
975 (defvar gnus-uu-binhex-end-line
976   ":$")
977
978 (defun gnus-uu-binhex-article (buffer in-state)
979   (let (state start-char)
980     (save-excursion
981       (set-buffer buffer)
982       (widen)
983       (goto-char (point-min))
984       (when (not (re-search-forward gnus-uu-binhex-begin-line nil t))
985         (when (not (re-search-forward gnus-uu-binhex-body-line nil t))
986           (setq state (list 'wrong-type))))
987
988       (if (memq 'wrong-type state)
989           ()
990         (beginning-of-line)
991         (setq start-char (point))
992         (if (looking-at gnus-uu-binhex-begin-line)
993             (progn
994               (setq state (list 'begin))
995               (write-region (point-min) (point-min)
996                             gnus-uu-binhex-article-name))
997           (setq state (list 'middle)))
998         (goto-char (point-max))
999         (re-search-backward (concat gnus-uu-binhex-body-line "\\|"
1000                                     gnus-uu-binhex-end-line)
1001                             nil t)
1002         (when (looking-at gnus-uu-binhex-end-line)
1003           (setq state (if (memq 'begin state)
1004                           (cons 'end state)
1005                         (list 'end))))
1006         (beginning-of-line)
1007         (forward-line 1)
1008         (when (file-exists-p gnus-uu-binhex-article-name)
1009           (mm-append-to-file start-char (point) gnus-uu-binhex-article-name))))
1010     (if (memq 'begin state)
1011         (cons gnus-uu-binhex-article-name state)
1012       state)))
1013
1014 ;; PostScript
1015
1016 (defun gnus-uu-decode-postscript-article (process-buffer in-state)
1017   (let ((state (list 'ok))
1018         start-char end-char file-name)
1019     (save-excursion
1020       (set-buffer process-buffer)
1021       (goto-char (point-min))
1022       (if (not (re-search-forward gnus-uu-postscript-begin-string nil t))
1023           (setq state (list 'wrong-type))
1024         (beginning-of-line)
1025         (setq start-char (point))
1026         (if (not (re-search-forward gnus-uu-postscript-end-string nil t))
1027             (setq state (list 'wrong-type))
1028           (setq end-char (point))
1029           (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1030           (insert-buffer-substring process-buffer start-char end-char)
1031           (setq file-name (concat gnus-uu-work-dir
1032                                   (cdr gnus-article-current) ".ps"))
1033           (write-region (point-min) (point-max) file-name)
1034           (setq state (list file-name 'begin 'end)))))
1035     state))
1036
1037
1038 ;; Find actions.
1039
1040 (defun gnus-uu-get-actions (files)
1041   (let ((ofiles files)
1042         action name)
1043     (while files
1044       (setq name (cdr (assq 'name (car files))))
1045       (and
1046        (setq action (gnus-uu-get-action name))
1047        (setcar files (nconc (list (if (string= action "gnus-uu-archive")
1048                                       (cons 'action "file")
1049                                     (cons 'action action))
1050                                   (cons 'execute (gnus-uu-command
1051                                                   action name)))
1052                             (car files))))
1053       (setq files (cdr files)))
1054     ofiles))
1055
1056 (defun gnus-uu-get-action (file-name)
1057   (let (action)
1058     (setq action
1059           (gnus-uu-choose-action
1060            file-name
1061            (append
1062             gnus-uu-user-view-rules
1063             (if gnus-uu-ignore-default-view-rules
1064                 nil
1065               gnus-uu-default-view-rules)
1066             gnus-uu-user-view-rules-end)))
1067     (when (and (not (string= (or action "") "gnus-uu-archive"))
1068                gnus-uu-view-with-metamail)
1069       (when (setq action
1070                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
1071         (setq action (format "metamail -d -b -c \"%s\"" action))))
1072     action))
1073
1074
1075 ;; Functions for treating subjects and collecting series.
1076
1077 (defun gnus-uu-reginize-string (string)
1078   ;; Takes a string and puts a \ in front of every special character;
1079   ;; replaces the last thing that looks like "2/3" with "[0-9]+/3"
1080   ;; or, if it can't find something like that, tries "2 of 3", then
1081   ;; finally just replaces the next to last number with "[0-9]+".
1082   (save-excursion
1083     (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1084     (buffer-disable-undo)
1085     (erase-buffer)
1086     (insert (regexp-quote string))
1087
1088     (setq case-fold-search nil)
1089
1090     (end-of-line)
1091     (if (re-search-backward "\\([^0-9]\\)[0-9]+/\\([0-9]+\\)" nil t)
1092         (replace-match "\\1[0-9]+/\\2")
1093
1094       (end-of-line)
1095       (if (re-search-backward "\\([^0-9]\\)[0-9]+[ \t]*of[ \t]*\\([0-9]+\\)"
1096                               nil t)
1097           (replace-match "\\1[0-9]+ of \\2")
1098
1099         (end-of-line)
1100         (if (re-search-backward "\\([^0-9]\\)[0-9]+\\([^0-9]+\\)[0-9]+"
1101                                 nil t)
1102             (replace-match "\\1[0-9]+\\2[0-9]+" t nil nil nil))))
1103
1104     (goto-char 1)
1105     (while (re-search-forward "[ \t]+" nil t)
1106       (replace-match "[ \t]+" t t))
1107
1108     (buffer-string)))
1109
1110 (defun gnus-uu-get-list-of-articles (n)
1111   ;; If N is non-nil, the article numbers of the N next articles
1112   ;; will be returned.
1113   ;; If any articles have been marked as processable, they will be
1114   ;; returned.
1115   ;; Failing that, articles that have subjects that are part of the
1116   ;; same "series" as the current will be returned.
1117   (let (articles)
1118     (cond
1119      (n
1120       (setq n (prefix-numeric-value n))
1121       (let ((backward (< n 0))
1122             (n (abs n)))
1123         (save-excursion
1124           (while (and (> n 0)
1125                       (push (gnus-summary-article-number)
1126                             articles)
1127                       (gnus-summary-search-forward nil nil backward))
1128             (setq n (1- n))))
1129         (nreverse articles)))
1130      (gnus-newsgroup-processable
1131       (reverse gnus-newsgroup-processable))
1132      (t
1133       (gnus-uu-find-articles-matching)))))
1134
1135 (defun gnus-uu-string< (l1 l2)
1136   (string< (car l1) (car l2)))
1137
1138 (defun gnus-uu-find-articles-matching
1139   (&optional subject only-unread do-not-translate)
1140   ;; Finds all articles that matches the regexp SUBJECT.  If it is
1141   ;; nil, the current article name will be used.  If ONLY-UNREAD is
1142   ;; non-nil, only unread articles are chosen.  If DO-NOT-TRANSLATE is
1143   ;; non-nil, article names are not equalized before sorting.
1144   (let ((subject (or subject
1145                      (gnus-uu-reginize-string (gnus-summary-article-subject))))
1146         list-of-subjects)
1147     (save-excursion
1148       (when subject
1149         ;; Collect all subjects matching subject.
1150         (let ((case-fold-search t)
1151               (data gnus-newsgroup-data)
1152               subj mark d)
1153           (while data
1154             (setq d (pop data))
1155             (and (not (gnus-data-pseudo-p d))
1156                  (or (not only-unread)
1157                      (= (setq mark (gnus-data-mark d))
1158                         gnus-unread-mark)
1159                      (= mark gnus-ticked-mark)
1160                      (= mark gnus-dormant-mark))
1161                  (setq subj (mail-header-subject (gnus-data-header d)))
1162                  (string-match subject subj)
1163                  (push (cons subj (gnus-data-number d))
1164                        list-of-subjects))))
1165
1166         ;; Expand numbers, sort, and return the list of article
1167         ;; numbers.
1168         (mapcar 'cdr
1169                 (sort (gnus-uu-expand-numbers
1170                        list-of-subjects
1171                        (not do-not-translate))
1172                       'gnus-uu-string<))))))
1173
1174 (defun gnus-uu-expand-numbers (string-list &optional translate)
1175   ;; Takes a list of strings and "expands" all numbers in all the
1176   ;; strings.  That is, this function makes all numbers equal length by
1177   ;; prepending lots of zeroes before each number.  This is to ease later
1178   ;; sorting to find out what sequence the articles are supposed to be
1179   ;; decoded in.  Returns the list of expanded strings.
1180   (let ((out-list string-list)
1181         string)
1182     (save-excursion
1183       (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1184       (buffer-disable-undo)
1185       (while string-list
1186         (erase-buffer)
1187         (insert (caar string-list))
1188         ;; Translate multiple spaces to one space.
1189         (goto-char (point-min))
1190         (while (re-search-forward "[ \t]+" nil t)
1191           (replace-match " "))
1192         ;; Translate all characters to "a".
1193         (goto-char (point-min))
1194         (when translate
1195           (while (re-search-forward "[A-Za-z]" nil t)
1196             (replace-match "a" t t)))
1197         ;; Expand numbers.
1198         (goto-char (point-min))
1199         (while (re-search-forward "[0-9]+" nil t)
1200           (ignore-errors
1201             (replace-match
1202              (format "%06d"
1203                      (string-to-number (buffer-substring
1204                                      (match-beginning 0) (match-end 0)))))))
1205         (setq string (buffer-substring 1 (point-max)))
1206         (setcar (car string-list) string)
1207         (setq string-list (cdr string-list))))
1208     out-list))
1209
1210
1211 ;; `gnus-uu-grab-articles' is the general multi-article treatment
1212 ;; function.  It takes a list of articles to be grabbed and a function
1213 ;; to apply to each article.
1214 ;;
1215 ;; The function to be called should take two parameters.  The first
1216 ;; parameter is the article buffer.  The function should leave the
1217 ;; result, if any, in this buffer.  Most treatment functions will just
1218 ;; generate files...
1219 ;;
1220 ;; The second parameter is the state of the list of articles, and can
1221 ;; have four values: `first', `middle', `last' and `first-and-last'.
1222 ;;
1223 ;; The function should return a list.  The list may contain the
1224 ;; following symbols:
1225 ;; `error' if an error occurred
1226 ;; `begin' if the beginning of an encoded file has been received
1227 ;;   If the list returned contains a `begin', the first element of
1228 ;;   the list *must* be a string with the file name of the decoded
1229 ;;   file.
1230 ;; `end' if the end of an encoded file has been received
1231 ;; `middle' if the article was a body part of an encoded file
1232 ;; `wrong-type' if the article was not a part of an encoded file
1233 ;; `ok', which can be used everything is ok
1234
1235 (defvar gnus-uu-has-been-grabbed nil)
1236
1237 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
1238   (let (art)
1239     (if (not (and gnus-uu-has-been-grabbed
1240                   gnus-uu-unmark-articles-not-decoded))
1241         ()
1242       (when dont-unmark-last-article
1243         (setq art (car gnus-uu-has-been-grabbed))
1244         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1245       (while gnus-uu-has-been-grabbed
1246         (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
1247         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
1248       (when dont-unmark-last-article
1249         (setq gnus-uu-has-been-grabbed (list art))))))
1250
1251 ;; This function takes a list of articles and a function to apply to
1252 ;; each article grabbed.
1253 ;;
1254 ;; This function returns a list of files decoded if the grabbing and
1255 ;; the process-function has been successful and nil otherwise.
1256 (defun gnus-uu-grab-articles (articles process-function
1257                                        &optional sloppy limit no-errors)
1258   (let ((state 'first)
1259         (gnus-asynchronous nil)
1260         (gnus-inhibit-treatment t)
1261         has-been-begin article result-file result-files process-state
1262         gnus-summary-display-article-function
1263         gnus-article-prepare-hook gnus-display-mime-function
1264         article-series files)
1265
1266     (while (and articles
1267                 (not (memq 'error process-state))
1268                 (or sloppy
1269                     (not (memq 'end process-state))))
1270
1271       (setq article (pop articles))
1272       (when (vectorp (gnus-summary-article-header article))
1273         (push article article-series)
1274
1275         (unless articles
1276           (if (eq state 'first)
1277               (setq state 'first-and-last)
1278             (setq state 'last)))
1279
1280         (let ((part (gnus-uu-part-number article)))
1281           (gnus-message 6 "Getting article %d%s..."
1282                         article (if (string= part "") "" (concat ", " part))))
1283         (gnus-summary-display-article article)
1284
1285         ;; Push the article to the processing function.
1286         (save-excursion
1287           (set-buffer gnus-original-article-buffer)
1288           (let ((buffer-read-only nil))
1289             (save-excursion
1290               (set-buffer gnus-summary-buffer)
1291               (setq process-state
1292                     (funcall process-function
1293                              gnus-original-article-buffer state)))))
1294
1295         (gnus-summary-remove-process-mark article)
1296
1297         ;; If this is the beginning of a decoded file, we push it
1298         ;; on to a list.
1299         (when (or (memq 'begin process-state)
1300                   (and (or (eq state 'first)
1301                            (eq state 'first-and-last))
1302                        (memq 'ok process-state)))
1303           (when has-been-begin
1304             ;; If there is a `result-file' here, that means that the
1305             ;; file was unsuccessfully decoded, so we delete it.
1306             (when (and result-file
1307                        (file-exists-p result-file)
1308                        (not gnus-uu-be-dangerous)
1309                        (or (eq gnus-uu-be-dangerous t)
1310                            (gnus-y-or-n-p
1311                             (format "Delete unsuccessfully decoded file %s? "
1312                                     result-file))))
1313               (delete-file result-file)))
1314           (when (memq 'begin process-state)
1315             (setq result-file (car process-state)))
1316           (setq has-been-begin t))
1317
1318         ;; Check whether we have decoded one complete file.
1319         (when (memq 'end process-state)
1320           (setq article-series nil)
1321           (setq has-been-begin nil)
1322           (if (stringp result-file)
1323               (setq files (list result-file))
1324             (setq files result-file))
1325           (setq result-file (car files))
1326           (while files
1327             (push (list (cons 'name (pop files))
1328                         (cons 'article article))
1329                   result-files))
1330           ;; Allow user-defined functions to be run on this file.
1331           (when gnus-uu-grabbed-file-functions
1332             (let ((funcs gnus-uu-grabbed-file-functions))
1333               (unless (listp funcs)
1334                 (setq funcs (list funcs)))
1335               (while funcs
1336                 (funcall (pop funcs) result-file))))
1337           (setq result-file nil)
1338           ;; Check whether we have decoded enough articles.
1339           (and limit (= (length result-files) limit)
1340                (setq articles nil)))
1341
1342         ;; If this is the last article to be decoded, and
1343         ;; we still haven't reached the end, then we delete
1344         ;; the partially decoded file.
1345         (and (or (eq state 'last) (eq state 'first-and-last))
1346              (not (memq 'end process-state))
1347              result-file
1348              (file-exists-p result-file)
1349              (not gnus-uu-be-dangerous)
1350              (or (eq gnus-uu-be-dangerous t)
1351                  (gnus-y-or-n-p
1352                   (format "Delete incomplete file %s? " result-file)))
1353              (delete-file result-file))
1354
1355         ;; If this was a file of the wrong sort, then
1356         (when (and (or (memq 'wrong-type process-state)
1357                        (memq 'error process-state))
1358                    gnus-uu-unmark-articles-not-decoded)
1359           (gnus-summary-tick-article article t))
1360
1361         ;; Set the new series state.
1362         (if (and (not has-been-begin)
1363                  (not sloppy)
1364                  (or (memq 'end process-state)
1365                      (memq 'middle process-state)))
1366             (progn
1367               (setq process-state (list 'error))
1368               (gnus-message 2 "No begin part at the beginning")
1369               (sleep-for 2))
1370           (setq state 'middle))))
1371
1372       ;; When there are no result-files, then something must be wrong.
1373     (if result-files
1374         (message "")
1375       (cond
1376        ((not has-been-begin)
1377         (gnus-message 2 "Wrong type file"))
1378        ((memq 'error process-state)
1379         (gnus-message 2 "An error occurred during decoding"))
1380        ((not (or (memq 'ok process-state)
1381                  (memq 'end process-state)))
1382         (gnus-message 2 "End of articles reached before end of file")))
1383       ;; Make unsuccessfully decoded articles unread.
1384       (when gnus-uu-unmark-articles-not-decoded
1385         (while article-series
1386           (gnus-summary-tick-article (pop article-series) t))))
1387
1388     ;; The original article buffer is hosed, shoot it down.
1389     (gnus-kill-buffer gnus-original-article-buffer)
1390     (setq gnus-current-article nil)
1391     result-files))
1392
1393 (defun gnus-uu-grab-view (file)
1394   "View FILE using the gnus-uu methods."
1395   (let ((action (gnus-uu-get-action file)))
1396     (gnus-execute-command
1397      (if (string-match "%" action)
1398          (format action file)
1399        (concat action " " file))
1400      (eq gnus-view-pseudos 'not-confirm))))
1401
1402 (defun gnus-uu-grab-move (file)
1403   "Move FILE to somewhere."
1404   (when gnus-uu-default-dir
1405     (let ((to-file (concat (file-name-as-directory gnus-uu-default-dir)
1406                            (file-name-nondirectory file))))
1407       (rename-file file to-file)
1408       (unless (file-exists-p file)
1409         (make-symbolic-link to-file file)))))
1410
1411 (defun gnus-uu-part-number (article)
1412   (let* ((header (gnus-summary-article-header article))
1413          (subject (and header (mail-header-subject header)))
1414          (part nil))
1415     (if subject
1416         (while (string-match "[0-9]+/[0-9]+\\|[0-9]+[ \t]+of[ \t]+[0-9]+"
1417                              subject)
1418           (setq part (match-string 0 subject))
1419           (setq subject (substring subject (match-end 0)))))
1420     (or part
1421         (while (string-match "[0-9]+[^0-9]+[0-9]+" subject)
1422           (setq part (match-string 0 subject))
1423           (setq subject (substring subject (match-end 0)))))
1424     (or part "")))
1425
1426 (defun gnus-uu-uudecode-sentinel (process event)
1427   (delete-process (get-process process)))
1428
1429 (defun gnus-uu-uustrip-article (process-buffer in-state)
1430   ;; Uudecodes a file asynchronously.
1431   (save-excursion
1432     (set-buffer process-buffer)
1433     (let ((state (list 'wrong-type))
1434           process-connection-type case-fold-search buffer-read-only
1435           files start-char)
1436       (goto-char (point-min))
1437
1438       ;; Deal with ^M at the end of the lines.
1439       (when gnus-uu-kill-carriage-return
1440         (save-excursion
1441           (while (search-forward "\r" nil t)
1442             (delete-backward-char 1))))
1443
1444       (while (or (re-search-forward gnus-uu-begin-string nil t)
1445                  (re-search-forward gnus-uu-body-line nil t))
1446         (setq state (list 'ok))
1447         ;; Ok, we are at the first uucoded line.
1448         (beginning-of-line)
1449         (setq start-char (point))
1450
1451         (if (not (looking-at gnus-uu-begin-string))
1452             (setq state (list 'middle))
1453           ;; This is the beginning of a uuencoded article.
1454           ;; We replace certain characters that could make things messy.
1455           (setq gnus-uu-file-name
1456                 (gnus-map-function
1457                  mm-file-name-rewrite-functions
1458                  (file-name-nondirectory (match-string 1))))
1459           (replace-match (concat "begin 644 " gnus-uu-file-name) t t)
1460
1461           ;; Remove any non gnus-uu-body-line right after start.
1462           (forward-line 1)
1463           (while (and (not (eobp))
1464                       (not (looking-at gnus-uu-body-line)))
1465             (gnus-delete-line))
1466
1467           ;; If a process is running, we kill it.
1468           (when (and gnus-uu-uudecode-process
1469                      (memq (process-status gnus-uu-uudecode-process)
1470                            '(run stop)))
1471             (delete-process gnus-uu-uudecode-process)
1472             (gnus-uu-unmark-list-of-grabbed t))
1473
1474           ;; Start a new uudecoding process.
1475           (let ((cdir default-directory))
1476             (unwind-protect
1477                 (progn
1478                   (cd gnus-uu-work-dir)
1479                   (setq gnus-uu-uudecode-process
1480                         (start-process
1481                          "*uudecode*"
1482                          (gnus-get-buffer-create gnus-uu-output-buffer-name)
1483                          shell-file-name shell-command-switch
1484                          (format "cd %s %s uudecode" gnus-uu-work-dir
1485                                  gnus-shell-command-separator))))
1486               (cd cdir)))
1487           (set-process-sentinel
1488            gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
1489           (setq state (list 'begin))
1490           (push (concat gnus-uu-work-dir gnus-uu-file-name) files))
1491
1492         ;; We look for the end of the thing to be decoded.
1493         (if (re-search-forward gnus-uu-end-string nil t)
1494             (push 'end state)
1495           (goto-char (point-max))
1496           (re-search-backward gnus-uu-body-line nil t))
1497
1498         (forward-line 1)
1499
1500         (when gnus-uu-uudecode-process
1501           (when (memq (process-status gnus-uu-uudecode-process) '(run stop))
1502             ;; Try to correct mishandled uucode.
1503             (when gnus-uu-correct-stripped-uucode
1504               (gnus-uu-check-correct-stripped-uucode start-char (point)))
1505             (gnus-run-hooks 'gnus-uu-pre-uudecode-hook)
1506
1507             ;; Send the text to the process.
1508             (condition-case nil
1509                 (process-send-region
1510                  gnus-uu-uudecode-process start-char (point))
1511               (error
1512                (progn
1513                  (delete-process gnus-uu-uudecode-process)
1514                  (gnus-message 2 "gnus-uu: Couldn't uudecode")
1515                  (setq state (list 'wrong-type)))))
1516
1517             (if (memq 'end state)
1518                 (progn
1519                   ;; Send an EOF, just in case.
1520                   (ignore-errors
1521                     (process-send-eof gnus-uu-uudecode-process))
1522                   (while (memq (process-status gnus-uu-uudecode-process)
1523                                '(open run))
1524                     (accept-process-output gnus-uu-uudecode-process 1)))
1525               (when (or (not gnus-uu-uudecode-process)
1526                         (not (memq (process-status gnus-uu-uudecode-process)
1527                                    '(run stop))))
1528                 (setq state (list 'wrong-type)))))))
1529
1530       (if (memq 'begin state)
1531           (cons (if (= (length files) 1) (car files) files) state)
1532         state))))
1533
1534 (defvar gnus-uu-unshar-warning
1535   "*** WARNING ***
1536
1537 Shell archives are an archaic method of bundling files for distribution
1538 across computer networks.  During the unpacking process, arbitrary commands
1539 are executed on your system, and all kinds of nasty things can happen.
1540 Please examine the archive very carefully before you instruct Emacs to
1541 unpack it.  You can browse the archive buffer using \\[scroll-other-window].
1542
1543 If you are unsure what to do, please answer \"no\"."
1544   "Text of warning message displayed by `gnus-uu-unshar-article'.
1545 Make sure that this text consists only of few text lines.  Otherwise,
1546 Gnus might fail to display all of it.")
1547
1548
1549 ;; This function is used by `gnus-uu-grab-articles' to treat
1550 ;; a shared article.
1551 (defun gnus-uu-unshar-article (process-buffer in-state)
1552   (let ((state (list 'ok))
1553         start-char)
1554     (save-excursion
1555       (set-buffer process-buffer)
1556       (goto-char (point-min))
1557       (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1558           (setq state (list 'wrong-type))
1559         (save-window-excursion
1560           (save-excursion
1561             (switch-to-buffer (current-buffer))
1562             (delete-other-windows)
1563             (let ((buffer (get-buffer-create (generate-new-buffer-name
1564                                               "*Warning*"))))
1565               (unless
1566                   (unwind-protect
1567                       (with-current-buffer buffer
1568                         (insert (substitute-command-keys
1569                                  gnus-uu-unshar-warning))
1570                         (goto-char (point-min))
1571                         (display-buffer buffer)
1572                         (yes-or-no-p "This is a shell archive, unshar it? "))
1573                     (kill-buffer buffer))
1574                 (setq state (list 'error))))))
1575         (unless (memq 'error state)
1576           (beginning-of-line)
1577           (setq start-char (point))
1578           (call-process-region
1579            start-char (point-max) shell-file-name nil
1580            (gnus-get-buffer-create gnus-uu-output-buffer-name) nil
1581            shell-command-switch
1582            (concat "cd " gnus-uu-work-dir " "
1583                    gnus-shell-command-separator  " sh")))))
1584     state))
1585
1586 ;; Returns the name of what the shar file is going to unpack.
1587 (defun gnus-uu-find-name-in-shar ()
1588   (let ((oldpoint (point))
1589         res)
1590     (goto-char (point-min))
1591     (when (re-search-forward gnus-uu-shar-name-marker nil t)
1592       (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1593     (goto-char oldpoint)
1594     res))
1595
1596 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1597 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1598 ;; found, or the name of the command to run if such a rule is found.
1599 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1600   (let ((action-list (copy-sequence file-action-list))
1601         (case-fold-search t)
1602         rule action)
1603     (and
1604      (unless no-ignore
1605        (and (not
1606              (and gnus-uu-ignore-files-by-name
1607                   (string-match gnus-uu-ignore-files-by-name file-name)))
1608             (not
1609              (and gnus-uu-ignore-files-by-type
1610                   (string-match gnus-uu-ignore-files-by-type
1611                                 (or (gnus-uu-choose-action
1612                                      file-name gnus-uu-ext-to-mime-list t)
1613                                     ""))))))
1614      (while (not (or (eq action-list ()) action))
1615        (setq rule (car action-list))
1616        (setq action-list (cdr action-list))
1617        (when (string-match (car rule) file-name)
1618          (setq action (cadr rule)))))
1619     action))
1620
1621 (defun gnus-uu-treat-archive (file-path)
1622   ;; Unpacks an archive.  Returns t if unpacking is successful.
1623   (let ((did-unpack t)
1624         action command dir)
1625     (setq action (gnus-uu-choose-action
1626                   file-path (append gnus-uu-user-archive-rules
1627                                     (if gnus-uu-ignore-default-archive-rules
1628                                         nil
1629                                       gnus-uu-default-archive-rules))))
1630
1631     (when (not action)
1632       (error "No unpackers for the file %s" file-path))
1633
1634     (string-match "/[^/]*$" file-path)
1635     (setq dir (substring file-path 0 (match-beginning 0)))
1636
1637     (when (member action gnus-uu-destructive-archivers)
1638       (copy-file file-path (concat file-path "~") t))
1639
1640     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1641
1642     (save-excursion
1643       (set-buffer (gnus-get-buffer-create gnus-uu-output-buffer-name))
1644       (erase-buffer))
1645
1646     (gnus-message 5 "Unpacking: %s..." (gnus-uu-command action file-path))
1647
1648     (if (eq 0 (call-process shell-file-name nil
1649                            (gnus-get-buffer-create gnus-uu-output-buffer-name)
1650                            nil shell-command-switch command))
1651         (message "")
1652       (gnus-message 2 "Error during unpacking of archive")
1653       (setq did-unpack nil))
1654
1655     (when (member action gnus-uu-destructive-archivers)
1656       (rename-file (concat file-path "~") file-path t))
1657
1658     did-unpack))
1659
1660 (defun gnus-uu-dir-files (dir)
1661   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1662         files file)
1663     (while dirs
1664       (if (file-directory-p (setq file (car dirs)))
1665           (setq files (append files (gnus-uu-dir-files file)))
1666         (push file files))
1667       (setq dirs (cdr dirs)))
1668     files))
1669
1670 (defun gnus-uu-unpack-files (files &optional ignore)
1671   ;; Go through FILES and look for files to unpack.
1672   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1673          (ofiles files)
1674          file did-unpack)
1675     (while files
1676       (setq file (cdr (assq 'name (car files))))
1677       (when (and (not (member file ignore))
1678                  (equal (gnus-uu-get-action (file-name-nondirectory file))
1679                         "gnus-uu-archive"))
1680         (push file did-unpack)
1681         (unless (gnus-uu-treat-archive file)
1682           (gnus-message 2 "Error during unpacking of %s" file))
1683         (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1684                (nfiles newfiles))
1685           (while nfiles
1686             (unless (member (car nfiles) totfiles)
1687               (push (list (cons 'name (car nfiles))
1688                           (cons 'original file))
1689                     ofiles))
1690             (setq nfiles (cdr nfiles)))
1691           (setq totfiles newfiles)))
1692       (setq files (cdr files)))
1693     (if did-unpack
1694         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1695       ofiles)))
1696
1697 (defun gnus-uu-ls-r (dir)
1698   (let* ((files (gnus-uu-directory-files dir t))
1699          (ofiles files))
1700     (while files
1701       (when (file-directory-p (car files))
1702         (setq ofiles (delete (car files) ofiles))
1703         (setq ofiles (append ofiles (gnus-uu-ls-r (car files)))))
1704       (setq files (cdr files)))
1705     ofiles))
1706
1707 ;; Various stuff
1708
1709 (defun gnus-uu-directory-files (dir &optional full)
1710   (let (files out file)
1711     (setq files (directory-files dir full))
1712     (while files
1713       (setq file (car files))
1714       (setq files (cdr files))
1715       (unless (member (file-name-nondirectory file) '("." ".."))
1716         (push file out)))
1717     (setq out (nreverse out))
1718     out))
1719
1720 (defun gnus-uu-check-correct-stripped-uucode (start end)
1721   (save-excursion
1722     (let (found beg length)
1723       (unless gnus-uu-correct-stripped-uucode
1724         (goto-char start)
1725
1726         (if (re-search-forward " \\|`" end t)
1727             (progn
1728               (goto-char start)
1729               (while (not (eobp))
1730                 (progn
1731                   (when (looking-at "\n")
1732                     (replace-match ""))
1733                   (forward-line 1))))
1734
1735           (while (not (eobp))
1736             (unless (looking-at (concat gnus-uu-begin-string "\\|"
1737                                         gnus-uu-end-string))
1738               (when (not found)
1739                 (setq length (- (point-at-eol) (point-at-bol))))
1740               (setq found t)
1741               (beginning-of-line)
1742               (setq beg (point))
1743               (end-of-line)
1744               (unless (= length (- (point) beg))
1745                 (insert (make-string (- length (- (point) beg)) ? ))))
1746             (forward-line 1)))))))
1747
1748 (defvar gnus-uu-tmp-alist nil)
1749
1750 (defun gnus-uu-initialize (&optional scan)
1751   (let (entry)
1752     (if (and (not scan)
1753              (when (setq entry (assoc gnus-newsgroup-name gnus-uu-tmp-alist))
1754                (if (file-exists-p (cdr entry))
1755                    (setq gnus-uu-work-dir (cdr entry))
1756                  (setq gnus-uu-tmp-alist (delq entry gnus-uu-tmp-alist))
1757                  nil)))
1758         t
1759       (setq gnus-uu-tmp-dir (file-name-as-directory
1760                              (expand-file-name gnus-uu-tmp-dir)))
1761       (if (not (file-directory-p gnus-uu-tmp-dir))
1762           (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1763         (when (not (file-writable-p gnus-uu-tmp-dir))
1764           (error "Temp directory %s can't be written to"
1765                  gnus-uu-tmp-dir)))
1766
1767       (setq gnus-uu-work-dir
1768             (mm-make-temp-file (concat gnus-uu-tmp-dir "gnus") 'dir))
1769       (gnus-set-file-modes gnus-uu-work-dir 448)
1770       (setq gnus-uu-work-dir (file-name-as-directory gnus-uu-work-dir))
1771       (push (cons gnus-newsgroup-name gnus-uu-work-dir)
1772             gnus-uu-tmp-alist))))
1773
1774
1775 ;; Kills the temporary uu buffers, kills any processes, etc.
1776 (defun gnus-uu-clean-up ()
1777   (let (buf)
1778     (and gnus-uu-uudecode-process
1779          (memq (process-status (or gnus-uu-uudecode-process "nevair"))
1780                '(stop run))
1781          (delete-process gnus-uu-uudecode-process))
1782     (when (setq buf (get-buffer gnus-uu-output-buffer-name))
1783       (kill-buffer buf))))
1784
1785 ;; Inputs an action and a filename and returns a full command, making sure
1786 ;; that the filename will be treated as a single argument when the shell
1787 ;; executes the command.
1788 (defun gnus-uu-command (action file)
1789   (let ((quoted-file (shell-quote-argument file)))
1790     (if (string-match "%s" action)
1791         (format action quoted-file)
1792       (concat action " " quoted-file))))
1793
1794 (defun gnus-uu-delete-work-dir (&optional dir)
1795   "Delete recursively all files and directories under `gnus-uu-work-dir'."
1796   (if dir
1797       (gnus-message 7 "Deleting directory %s..." dir)
1798     (setq dir gnus-uu-work-dir))
1799   (when (and dir
1800              (file-exists-p dir))
1801     (let ((files (directory-files dir t nil t))
1802           file)
1803       (while (setq file (pop files))
1804         (unless (member (file-name-nondirectory file) '("." ".."))
1805           (if (file-directory-p file)
1806               (gnus-uu-delete-work-dir file)
1807             (gnus-message 9 "Deleting file %s..." file)
1808             (condition-case err
1809                 (delete-file file)
1810               (error (gnus-message 3 "Deleting file %s failed... %s" file err))))))
1811       (condition-case err
1812           (delete-directory dir)
1813         (error (gnus-message 3 "Deleting directory %s failed... %s" file err))))
1814     (gnus-message 7 "")))
1815
1816 ;; Initializing
1817
1818 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1819 (add-hook 'gnus-exit-group-hook 'gnus-uu-delete-work-dir)
1820
1821 \f
1822
1823 ;;;
1824 ;;; uuencoded posting
1825 ;;;
1826
1827 ;; Any function that is to be used as and encoding method will take two
1828 ;; parameters: PATH-NAME and FILE-NAME.  (E.g. "/home/gaga/spiral.jpg"
1829 ;; and "spiral.jpg", respectively.) The function should return nil if
1830 ;; the encoding wasn't successful.
1831 (defcustom gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1832   "Function used for encoding binary files.
1833 There are three functions supplied with gnus-uu for encoding files:
1834 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1835 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME
1836 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with
1837 uuencode and adds MIME headers."
1838   :group 'gnus-extract-post
1839   :type '(radio (function-item gnus-uu-post-encode-uuencode)
1840                 (function-item gnus-uu-post-encode-mime)
1841                 (function-item gnus-uu-post-encode-mime-uuencode)
1842                 (function :tag "Other")))
1843
1844 (defcustom gnus-uu-post-include-before-composing nil
1845   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1846 If this variable is t, you can either include an encoded file with
1847 \\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article."
1848   :group 'gnus-extract-post
1849   :type 'boolean)
1850
1851 (defcustom gnus-uu-post-length 990
1852   "Maximum length of an article.
1853 The encoded file will be split into how many articles it takes to
1854 post the entire file."
1855   :group 'gnus-extract-post
1856   :type 'integer)
1857
1858 (defcustom gnus-uu-post-threaded nil
1859   "Non-nil means that gnus-uu will post the encoded file in a thread.
1860 This may not be smart, as no other decoder I have seen are able to
1861 follow threads when collecting uuencoded articles.  (Well, I have seen
1862 one package that does that - gnus-uu, but somehow, I don't think that
1863 counts...)  The default is nil."
1864   :group 'gnus-extract-post
1865   :type 'boolean)
1866
1867 (defcustom gnus-uu-post-separate-description t
1868   "Non-nil means that the description will be posted in a separate article.
1869 The first article will typically be numbered (0/x).  If this variable
1870 is nil, the description the user enters will be included at the
1871 beginning of the first article, which will be numbered (1/x).  Default
1872 is t."
1873   :group 'gnus-extract-post
1874   :type 'boolean)
1875
1876 (defvar gnus-uu-post-binary-separator "--binary follows this line--")
1877 (defvar gnus-uu-post-message-id nil)
1878 (defvar gnus-uu-post-inserted-file-name nil)
1879 (defvar gnus-uu-winconf-post-news nil)
1880
1881 (defun gnus-uu-post-news ()
1882   "Compose an article and post an encoded file."
1883   (interactive)
1884   (setq gnus-uu-post-inserted-file-name nil)
1885   (setq gnus-uu-winconf-post-news (current-window-configuration))
1886
1887   (gnus-summary-post-news)
1888
1889   (let ((map (make-sparse-keymap)))
1890     (set-keymap-parent map (current-local-map))
1891     (use-local-map map))
1892   ;;(local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
1893   (local-set-key "\C-c\C-c" 'gnus-uu-post-news-inews)
1894   (local-set-key "\C-c\C-s" 'gnus-uu-post-news-inews)
1895   (local-set-key "\C-c\C-i" 'gnus-uu-post-insert-binary-in-article)
1896
1897   (when gnus-uu-post-include-before-composing
1898     (save-excursion (setq gnus-uu-post-inserted-file-name
1899                           (gnus-uu-post-insert-binary)))))
1900
1901 (defun gnus-uu-post-insert-binary-in-article ()
1902   "Inserts an encoded file in the buffer.
1903 The user will be asked for a file name."
1904   (interactive)
1905   (save-excursion
1906     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1907
1908 ;; Encodes with uuencode and substitutes all spaces with backticks.
1909 (defun gnus-uu-post-encode-uuencode (path file-name)
1910   (when (gnus-uu-post-encode-file "uuencode" path file-name)
1911     (goto-char (point-min))
1912     (forward-line 1)
1913     (while (search-forward " " nil t)
1914       (replace-match "`"))
1915     t))
1916
1917 ;; Encodes with uuencode and adds MIME headers.
1918 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1919   (when (gnus-uu-post-encode-uuencode path file-name)
1920     (gnus-uu-post-make-mime file-name "x-uue")
1921     t))
1922
1923 ;; Encodes with base64 and adds MIME headers
1924 (defun gnus-uu-post-encode-mime (path file-name)
1925   (when (eq 0 (call-process shell-file-name nil t nil shell-command-switch
1926                             (format "%s %s -o %s" "mmencode" path file-name)))
1927     (gnus-uu-post-make-mime file-name "base64")
1928     t))
1929
1930 ;; Adds MIME headers.
1931 (defun gnus-uu-post-make-mime (file-name encoding)
1932   (goto-char (point-min))
1933   (insert (format "Content-Type: %s; name=\"%s\"\n"
1934                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list)
1935                   file-name))
1936   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1937   (save-restriction
1938     (set-buffer gnus-message-buffer)
1939     (goto-char (point-min))
1940     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1941     (forward-line -1)
1942     (narrow-to-region (point-min) (point))
1943     (unless (mail-fetch-field "mime-version")
1944       (widen)
1945       (insert "MIME-Version: 1.0\n"))
1946     (widen)))
1947
1948 ;; Encodes a file PATH with COMMAND, leaving the result in the
1949 ;; current buffer.
1950 (defun gnus-uu-post-encode-file (command path file-name)
1951   (eq 0 (call-process shell-file-name nil t nil shell-command-switch
1952                       (format "%s %s %s" command path file-name))))
1953
1954 (defun gnus-uu-post-news-inews ()
1955   "Posts the composed news article and encoded file.
1956 If no file has been included, the user will be asked for a file."
1957   (interactive)
1958
1959   (let (file-name)
1960
1961     (if gnus-uu-post-inserted-file-name
1962         (setq file-name gnus-uu-post-inserted-file-name)
1963       (setq file-name (gnus-uu-post-insert-binary)))
1964
1965     (gnus-uu-post-encoded file-name gnus-uu-post-threaded))
1966   (setq gnus-uu-post-inserted-file-name nil)
1967   (when gnus-uu-winconf-post-news
1968     (set-window-configuration gnus-uu-winconf-post-news)))
1969
1970 ;; Asks for a file to encode, encodes it and inserts the result in
1971 ;; the current buffer.  Returns the file name the user gave.
1972 (defun gnus-uu-post-insert-binary ()
1973   (let ((uuencode-buffer-name "*uuencode buffer*")
1974         file-path uubuf file-name)
1975
1976     (setq file-path (read-file-name
1977                      "What file do you want to encode? "))
1978     (when (not (file-exists-p file-path))
1979       (error "%s: No such file" file-path))
1980
1981     (goto-char (point-max))
1982     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1983
1984     ;; #### Unix-specific?
1985     (when (string-match "^~/" file-path)
1986       (setq file-path (concat "$HOME" (substring file-path 1))))
1987     ;; #### Unix-specific?
1988     (if (string-match "/[^/]*$" file-path)
1989         (setq file-name (substring file-path (1+ (match-beginning 0))))
1990       (setq file-name file-path))
1991
1992     (unwind-protect
1993         (if (save-excursion
1994               (set-buffer (setq uubuf
1995                                 (gnus-get-buffer-create uuencode-buffer-name)))
1996               (erase-buffer)
1997               (funcall gnus-uu-post-encode-method file-path file-name))
1998             (insert-buffer-substring uubuf)
1999           (error "Encoding unsuccessful"))
2000       (kill-buffer uubuf))
2001     file-name))
2002
2003 ;; Posts the article and all of the encoded file.
2004 (defun gnus-uu-post-encoded (file-name &optional threaded)
2005   (let ((send-buffer-name "*uuencode send buffer*")
2006         (encoded-buffer-name "*encoded buffer*")
2007         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
2008         (separator (concat mail-header-separator "\n\n"))
2009         uubuf length parts header i end beg
2010         beg-line minlen post-buf whole-len beg-binary end-binary)
2011
2012     (setq post-buf (current-buffer))
2013
2014     (goto-char (point-min))
2015     (when (not (re-search-forward
2016                 (if gnus-uu-post-separate-description
2017                     (concat "^" (regexp-quote gnus-uu-post-binary-separator)
2018                             "$")
2019                   (concat "^" (regexp-quote mail-header-separator) "$"))
2020                 nil t))
2021       (error "Internal error: No binary/header separator"))
2022     (beginning-of-line)
2023     (forward-line 1)
2024     (setq beg-binary (point))
2025     (setq end-binary (point-max))
2026
2027     (save-excursion
2028       (set-buffer (setq uubuf (gnus-get-buffer-create encoded-buffer-name)))
2029       (erase-buffer)
2030       (insert-buffer-substring post-buf beg-binary end-binary)
2031       (goto-char (point-min))
2032       (setq length (count-lines (point-min) (point-max)))
2033       (setq parts (/ length gnus-uu-post-length))
2034       (unless (< (% length gnus-uu-post-length) 4)
2035         (incf parts)))
2036
2037     (when gnus-uu-post-separate-description
2038       (forward-line -1))
2039     (delete-region (point) (point-max))
2040
2041     (goto-char (point-min))
2042     (re-search-forward
2043      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2044     (setq header (buffer-substring (point-min) (point-at-bol)))
2045
2046     (goto-char (point-min))
2047     (when gnus-uu-post-separate-description
2048       (when (re-search-forward "^Subject: " nil t)
2049         (end-of-line)
2050         (insert (format " (0/%d)" parts)))
2051       (save-excursion
2052         (message-send))
2053       (setq gnus-uu-post-message-id (message-fetch-field "message-id")))
2054
2055     (save-excursion
2056       (setq i 1)
2057       (setq beg 1)
2058       (while (not (> i parts))
2059         (set-buffer (gnus-get-buffer-create send-buffer-name))
2060         (erase-buffer)
2061         (insert header)
2062         (when (and threaded gnus-uu-post-message-id)
2063           (insert "References: " gnus-uu-post-message-id "\n"))
2064         (insert separator)
2065         (setq whole-len
2066               (- 62 (length (format top-string "" file-name i parts ""))))
2067         (when (> 1 (setq minlen (/ whole-len 2)))
2068           (setq minlen 1))
2069         (setq
2070          beg-line
2071          (format top-string
2072                  (make-string minlen ?-)
2073                  file-name i parts
2074                  (make-string
2075                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
2076
2077         (goto-char (point-min))
2078         (when (re-search-forward "^Subject: " nil t)
2079           (end-of-line)
2080           (insert (format " (%d/%d)" i parts)))
2081
2082         (goto-char (point-max))
2083         (save-excursion
2084           (set-buffer uubuf)
2085           (goto-char beg)
2086           (if (= i parts)
2087               (goto-char (point-max))
2088             (forward-line gnus-uu-post-length))
2089           (when (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
2090             (forward-line -4))
2091           (setq end (point)))
2092         (insert-buffer-substring uubuf beg end)
2093         (insert beg-line "\n")
2094         (setq beg end)
2095         (incf i)
2096         (goto-char (point-min))
2097         (re-search-forward
2098          (concat "^" (regexp-quote mail-header-separator) "$") nil t)
2099         (beginning-of-line)
2100         (forward-line 2)
2101         (when (re-search-forward
2102                (concat "^" (regexp-quote gnus-uu-post-binary-separator) "$")
2103                nil t)
2104           (replace-match "")
2105           (forward-line 1))
2106         (insert beg-line)
2107         (insert "\n")
2108         (let (message-sent-message-via)
2109           (save-excursion
2110             (message-send))
2111           (setq gnus-uu-post-message-id
2112                 (concat (message-fetch-field "references") " "
2113                         (message-fetch-field "message-id"))))))
2114
2115     (gnus-kill-buffer send-buffer-name)
2116     (gnus-kill-buffer encoded-buffer-name)
2117
2118     (when (not gnus-uu-post-separate-description)
2119       (set-buffer-modified-p nil)
2120       (bury-buffer))))
2121
2122 (provide 'gnus-uu)
2123
2124 ;;; arch-tag: 05312384-0a83-4720-9a58-b3160b888853
2125 ;;; gnus-uu.el ends here