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