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