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