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