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