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