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