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