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