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