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