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