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