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