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