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