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