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