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