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