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