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