*** 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 Free Software Foundation, Inc.
3
4 ;; Author: Lars Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Created: 2 Oct 1993
6 ;; Version: v3.0
7 ;; Keyword: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;;; Code: 
28
29 (require 'gnus)
30
31 ;; Default viewing action rules
32
33 (defvar gnus-uu-default-view-rules 
34   '(("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed s/\r//g")
35     ("\\.pas$" "cat %s | sed s/\r//g")
36     ("\\.[1-9]$" "groff -mandoc -Tascii %s | sed s/\b.//g")
37     ("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "xv")
38     ("\\.tga$" "tgatoppm %s | xv -")
39     ("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$" 
40      "sox -v .5 %s -t .au -u - > /dev/audio")
41     ("\\.au$" "cat %s > /dev/audio")
42     ("\\.mod$" "str32")
43     ("\\.ps$" "ghostview")
44     ("\\.dvi$" "xdvi")
45     ("\\.html$" "xmosaic")
46     ("\\.mpe?g$" "mpeg_play")
47     ("\\.\\(flc\\|fli\\|rle\\|iff\\|pfx\\|avi\\|sme\\|rpza\\|dl\\|qt\\|rsrc\\|mov\\)$" "xanim")
48     ("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$" 
49      "gnus-uu-archive"))
50
51   "Default actions to be taken when the user asks to view a file.  
52 To change the behaviour, you can either edit this variable or set
53 `gnus-uu-user-view-rules' to something useful.
54
55 For example:
56
57 To make gnus-uu use 'xli' to display JPEG and GIF files, put the
58 following in your .emacs file
59
60   (setq gnus-uu-user-view-rules '((\"jpg$\\\\|gif$\" \"xli\")))
61
62 Both these variables are lists of lists with two string elements. The
63 first string is a regular expression. If the file name matches this
64 regular expression, the command in the second string is executed with
65 the file as an argument.
66
67 If the command string contains \"%s\", the file name will be inserted
68 at that point in the command string. If there's no \"%s\" in the
69 command string, the file name will be appended to the command string
70 before executing.
71
72 There are several user variables to tailor the behaviour of gnus-uu to
73 your needs. First we have `gnus-uu-user-view-rules', which is the
74 variable gnus-uu first consults when trying to decide how to view a
75 file. If this variable contains no matches, gnus-uu examines the
76 default rule vaiable provided in this package. If gnus-uu finds no
77 match here, it uses `gnus-uu-user-view-rules-end' to try to make a
78 match.")
79
80 (defvar gnus-uu-user-view-rules nil 
81   "Variable detailing what actions are to be taken to view a file.
82 See the documentation on the `gnus-uu-default-view-rules' variable for 
83 details.")
84
85 (defvar gnus-uu-user-view-rules-end 
86   '(("" "file"))
87   "Variable saying what actions are to be taken if no rule matched the file name.
88 See the documentation on the `gnus-uu-default-view-rules' variable for 
89 details.")
90
91 ;; Default unpacking commands
92
93 (defvar gnus-uu-default-archive-rules 
94   '(("\\.tar$" "tar xf")
95     ("\\.zip$" "unzip -o")
96     ("\\.ar$" "ar x")
97     ("\\.arj$" "unarj x")
98     ("\\.zoo$" "zoo -e")
99     ("\\.\\(lzh\\|lha\\)$" "lha x")
100     ("\\.Z$" "uncompress")
101     ("\\.gz$" "gunzip")
102     ("\\.arc$" "arc -x")))
103
104 (defvar gnus-uu-destructive-archivers 
105   (list "uncompress" "gunzip"))
106
107 (defvar gnus-uu-user-archive-rules nil
108   "A list that can be set to override the default archive unpacking commands.
109 To use, for instance, 'untar' to unpack tar files and 'zip -x' to
110 unpack zip files, say the following:
111   (setq gnus-uu-user-archive-rules 
112     '((\"\\\\.tar$\" \"untar\")
113       (\"\\\\.zip$\" \"zip -x\")))")
114
115 (defvar gnus-uu-ignore-files-by-name nil
116   "A regular expression saying what files should not be viewed based on name.
117 If, for instance, you want gnus-uu to ignore all .au and .wav files, 
118 you could say something like
119
120   (setq gnus-uu-ignore-files-by-name \"\\\\.au$\\\\|\\\\.wav$\")
121
122 Note that this variable can be used in conjunction with the
123 `gnus-uu-ignore-files-by-type' variable.")
124
125 (defvar gnus-uu-ignore-files-by-type nil
126   "A regular expression saying what files that shouldn't be viewed, based on MIME file type.
127 If, for instance, you want gnus-uu to ignore all audio files and all mpegs, 
128 you could say something like
129
130   (setq gnus-uu-ignore-files-by-type \"audio/\\\\|video/mpeg\")
131
132 Note that this variable can be used in conjunction with the
133 `gnus-uu-ignore-files-by-name' variable.")
134
135 ;; Pseudo-MIME support
136
137 (defconst gnus-uu-ext-to-mime-list
138   '(("\\.gif$" "image/gif")
139     ("\\.jpe?g$" "image/jpeg")
140     ("\\.tiff?$" "image/tiff")
141     ("\\.xwd$" "image/xwd")
142     ("\\.pbm$" "image/pbm")
143     ("\\.pgm$" "image/pgm")
144     ("\\.ppm$" "image/ppm")
145     ("\\.xbm$" "image/xbm")
146     ("\\.pcx$" "image/pcx")
147     ("\\.tga$" "image/tga")
148     ("\\.ps$" "image/postscript")
149     ("\\.fli$" "video/fli")
150     ("\\.wav$" "audio/wav")
151     ("\\.aiff$" "audio/aiff")
152     ("\\.hcom$" "audio/hcom")
153     ("\\.voc$" "audio/voc")
154     ("\\.smp$" "audio/smp")
155     ("\\.mod$" "audio/mod")
156     ("\\.dvi$" "image/dvi")
157     ("\\.mpe?g$" "video/mpeg")
158     ("\\.au$" "audio/basic")
159     ("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
160     ("\\.\\(c\\|h\\)$" "text/source")
161     ("read.*me" "text/plain")
162     ("\\.html$" "text/html")
163     ("\\.bat$" "text/bat")
164     ("\\.[1-6]$" "text/man")
165     ("\\.flc$" "video/flc")
166     ("\\.rle$" "video/rle")
167     ("\\.pfx$" "video/pfx")
168     ("\\.avi$" "video/avi")
169     ("\\.sme$" "video/sme")
170     ("\\.rpza$" "video/prza")
171     ("\\.dl$" "video/dl")
172     ("\\.qt$" "video/qt")
173     ("\\.rsrc$" "video/rsrc")
174     ("\\..*$" "unknown/unknown")))
175
176 ;; Various variables users may set 
177
178 (defvar gnus-uu-tmp-dir "/tmp/" 
179   "Variable saying where gnus-uu is to do its work.
180 Default is \"/tmp/\".")
181
182 (defvar gnus-uu-do-not-unpack-archives nil 
183   "Non-nil means that gnus-uu won't peek inside archives looking for files to dispay. 
184 Default is nil.")
185
186 (defvar gnus-uu-view-and-save nil 
187   "Non-nil means that the user will always be asked to save a file after viewing it.
188 If the variable is nil, the suer will only be asked to save if the
189 viewing is unsuccessful. Default is nil.")
190
191 (defvar gnus-uu-ignore-default-view-rules nil
192   "Non-nil means that gnus-uu will ignore the default viewing rules.
193 Only the user viewing rules will be consulted. Default is nil.")
194
195 (defvar gnus-uu-ignore-default-archive-rules nil 
196   "Non-nil means that gnus-uu will ignore the default archive unpacking commands.  
197 Only the user unpacking commands will be consulted. Default is nil.")
198
199 (defvar gnus-uu-kill-carriage-return t
200   "Non-nil means that gnus-uu will strip all carriage returns from articles.
201 Default is t.")
202
203 (defvar gnus-uu-view-with-metamail nil
204   "Non-nil means that files will be viewed with metamail.
205 The gnus-uu viewing functions will be ignored and gnus-uu will try
206 to guess at a content-type based on file name suffixes. Default
207 it nil.")
208
209 (defvar gnus-uu-unmark-articles-not-decoded nil
210   "Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread. 
211 Default is nil.")
212
213 (defvar gnus-uu-correct-stripped-uucode nil
214   "Non-nil means that gnus-uu will *try* to fix uuencoded files that have had traling spaces deleted. 
215 Default is nil.")
216
217 (defvar gnus-uu-save-in-digest nil
218   "Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
219 If this variable is nil, gnus-uu will just save everything in a 
220 file without any embellishments. The digesting almost conforms to RFC1153 -
221 no easy way to specify any meaningful volume and issue numbers were found, 
222 so I simply dropped them.")
223
224 (defvar gnus-uu-save-separate-articles nil
225   "Non-nil means that gnus-uu will save articles in separate files.")
226
227 ;; Internal variables
228
229 (defvar gnus-uu-saved-article-name nil)
230
231 (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
232 (defconst gnus-uu-end-string "^end[ \t]*$")
233
234 (defconst gnus-uu-body-line "^M")
235 (let ((i 61))
236   (while (> (setq i (1- i)) 0)
237     (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
238   (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
239
240 ;"^M.............................................................?$"
241
242 (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
243
244 (defvar gnus-uu-shar-file-name nil)
245 (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
246
247 (defvar gnus-uu-file-name nil)
248 (defvar gnus-uu-list-of-files-decoded nil)
249 (defconst gnus-uu-uudecode-process nil)
250
251 (defvar gnus-uu-generated-file-list nil)
252 (defvar gnus-uu-work-dir nil)
253
254 (defconst gnus-uu-output-buffer-name "*Gnus UU Output*")
255
256 (defconst gnus-uu-highest-article-number 1)
257
258 ;; Commands.
259
260 (defun gnus-uu-decode-uu (n)
261   "Uudecodes the current article."
262   (interactive "P") 
263   (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n))
264
265 (defun gnus-uu-decode-uu-and-save (n dir)
266   "Decodes and saves the resulting file."
267   (interactive "P\nDWhere do you want to save the file(s)? ")
268   (gnus-uu-decode-with-method 'gnus-uu-uustrip-article n dir))
269
270 (defun gnus-uu-decode-unshar (n)
271   "Unshars the current article."
272   (interactive "P")
273   (gnus-uu-decode-with-method 'gnus-uu-unshar-article n))
274
275 (defun gnus-uu-decode-unshar-and-save (n dir)
276   "Unshars and saves the current article."
277   (interactive "P\nDWhere do you want to save the file(s)? ")
278   (gnus-uu-decode-with-method 'gnus-uu-unshar-article n dir))
279
280 (defun gnus-uu-decode-save (n file)
281   "Saves the current article."
282   (interactive "P\nDWhere do you want to save the file(s)? ")
283   (setq gnus-uu-saved-article-name file)
284   (gnus-uu-decode-with-method 'gnus-uu-save-article n nil)
285   (setq gnus-uu-generated-file-list 
286         (delete file gnus-uu-generated-file-list)))
287
288 (defun gnus-uu-decode-binhex (n dir)
289   "Unbinhexes the current article."
290   (interactive "P\nDWhere do you want to save the file(s)? ")
291   (gnus-uu-decode-with-method 'gnus-uu-binhex-article n dir))
292
293 ;; Digest and forward articles
294
295 (defun gnus-uu-digest-and-forward (n)
296   "Digests and forwards all articles in this series."
297   (interactive "P")
298   (gnus-uu-initialize)
299   (let ((gnus-uu-save-in-digest t)
300         (file (concat gnus-uu-work-dir (make-temp-name "forward"))))
301     (gnus-uu-decode-save n file)
302     (switch-to-buffer (get-buffer-create "*gnus-uu-forward*"))
303     (erase-buffer)
304     (delete-other-windows)
305     (insert-file file)
306     (goto-char (point-min))
307     (funcall gnus-mail-forward-method)))
308
309 ;; Process marking.
310
311 (defun gnus-uu-mark-by-regexp (regexp)
312   "Ask for a regular expression and set the process mark on all articles that match."
313   (interactive (list (read-from-minibuffer "Mark (regexp): ")))
314   (let ((articles (gnus-uu-find-articles-matching regexp)))
315     (while articles
316       (gnus-summary-set-process-mark (car articles))
317       (setq articles (cdr articles)))
318     (message ""))
319   (gnus-summary-position-cursor))
320
321 (defun gnus-uu-mark-region (beg end)
322   "Marks all articles between point and mark."
323   (interactive "r")
324   (save-excursion
325     (goto-char beg)
326     (while (< (point) end)
327       (gnus-summary-set-process-mark (gnus-summary-article-number))
328       (forward-line 1)))
329   (gnus-summary-position-cursor))
330       
331 (defun gnus-uu-mark-thread ()
332   "Marks all articles downwards in this thread."
333   (interactive)
334   (save-excursion
335     (let ((level (gnus-summary-thread-level)))
336     (while (and (gnus-summary-set-process-mark (gnus-summary-article-number))
337                 (zerop (gnus-summary-next-subject 1))
338                 (> (gnus-summary-thread-level) level)))))
339   (gnus-summary-position-cursor))
340
341 (defun gnus-uu-mark-sparse ()
342   "Mark all series that have some articles marked."
343   (interactive)
344   (let ((marked (nreverse gnus-newsgroup-processable))
345         subject articles total)
346     (or marked (error "No articles marked with the process mark"))
347     (setq gnus-newsgroup-processable nil)
348     (save-excursion
349       (while marked
350         (setq subject (header-subject 
351                        (gnus-gethash 
352                         (int-to-string (car marked))
353                         gnus-newsgroup-headers-hashtb-by-number)))
354         (setq articles (gnus-uu-find-articles-matching 
355                         (gnus-uu-reginize-string subject)))
356         (setq total (nconc total articles))
357         (while articles
358           (gnus-summary-set-process-mark (car articles))
359           (setcdr marked (delq (car articles) (cdr marked)))
360           (setq articles (cdr articles)))
361         (setq marked (cdr marked)))
362       (setq gnus-newsgroup-processable (nreverse total)))
363     (gnus-summary-position-cursor)))
364
365 ;; Work functions
366
367 (defun gnus-uu-decode-with-method (method n &optional save)
368   (gnus-uu-initialize)
369   (let ((articles (gnus-uu-get-list-of-articles n))
370         files)
371     (setq files (gnus-uu-grab-articles articles method t))
372     (and save (gnus-uu-save-files files save))
373     (setq files (gnus-uu-unpack-files files))
374     (gnus-uu-add-file (mapcar (lambda (file) (cdr (assq 'name file))) files))
375     (setq files (gnus-uu-get-actions files))
376     (gnus-summary-insert-pseudos files)))
377
378 (defun gnus-uu-save-files (files dir)
379   (let ((len (length files))
380         file)
381     (while files
382       (setq file (cdr (assq 'name (car files))))
383       (copy-file file (if (file-directory-p dir)
384                           (concat dir (file-name-nondirectory file))
385                         dir))
386       (setq files (cdr files)))
387     (message "Saved %d file%s" len (if (> len 1) "s" ""))))
388
389 ;; Functions for saving and possibly digesting articles without
390 ;; any decoding.
391
392 ;; Function called by gnus-uu-grab-articles to treat each article.
393 (defun gnus-uu-save-article (buffer in-state)
394   (cond 
395    (gnus-uu-save-separate-articles
396     (save-excursion
397       (set-buffer buffer)
398       (write-region 1 (point-max) (concat gnus-uu-saved-article-name 
399                                           gnus-current-article))
400       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
401             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name 
402                                                  'begin 'end))
403             ((eq in-state 'last) (list 'end))
404             (t (list 'middle)))))
405    ((not gnus-uu-save-in-digest)
406     (save-excursion
407       (set-buffer buffer)
408       (write-region 1 (point-max) gnus-uu-saved-article-name t)
409       (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
410             ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name 
411                                                  'begin 'end))
412             ((eq in-state 'last) (list 'end))
413             (t (list 'middle)))))
414     (t
415      (let (beg subj name headers headline sorthead body end-string state)
416        (string-match "/\\([^/]*\\)$" gnus-uu-saved-article-name)
417        (setq name (substring gnus-uu-saved-article-name (match-beginning 1)
418                              (match-end 1)))
419        (if (or (eq in-state 'first) 
420                (eq in-state 'first-and-last))
421            (progn 
422              (setq state (list 'begin))
423              (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
424                              (erase-buffer))
425              (save-excursion 
426                (set-buffer (get-buffer-create "*gnus-uu-pre*"))
427                (erase-buffer)
428                (insert (format 
429                         "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
430                         (current-time-string) name name))))
431          (if (not (eq in-state 'end))
432              (setq state (list 'middle))))
433        (save-excursion
434          (set-buffer (get-buffer "*gnus-uu-body*"))
435          (goto-char (setq beg (point-max)))
436          (save-excursion
437            (save-restriction
438              (set-buffer buffer)
439              (goto-char 1)
440              (re-search-forward "\n\n")
441              (setq body (buffer-substring (1- (point)) (point-max)))
442              (narrow-to-region 1 (point))
443              (setq headers (list "Date:" "From:" "To:" "Cc:" "Subject:"
444                                  "Message-ID:" "Keywords:" "Summary:"))
445              (while headers
446                (setq headline (car headers))
447                (setq headers (cdr headers))
448                (goto-char 1)
449                (if (re-search-forward (concat "^" headline ".*$") nil t)
450                    (setq sorthead 
451                          (concat sorthead (buffer-substring 
452                                            (match-beginning 0)
453                                            (match-end 0)) "\n"))))
454              (widen)))
455          (insert sorthead)(goto-char (point-max))
456          (insert body)(goto-char (point-max))
457          (insert (concat "\n" (make-string 30 ?-) "\n\n"))
458          (goto-char beg)
459          (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
460              (progn
461                (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
462                (save-excursion 
463                 (set-buffer (get-buffer "*gnus-uu-pre*"))
464                 (insert (format "   %s\n" subj))))))
465       (if (or (eq in-state 'last)
466               (eq in-state 'first-and-last))
467           (progn
468             (save-excursion
469               (set-buffer (get-buffer "*gnus-uu-pre*"))
470               (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
471               (write-region 1 (point-max) gnus-uu-saved-article-name))
472             (save-excursion
473               (set-buffer (get-buffer "*gnus-uu-body*"))
474               (goto-char (point-max))
475               (insert 
476                (concat (setq end-string (format "End of %s Digest" name)) 
477                        "\n"))
478               (insert (concat (make-string (length end-string) ?*) "\n"))
479               (write-region 1 (point-max) gnus-uu-saved-article-name t))
480             (kill-buffer (get-buffer "*gnus-uu-pre*"))
481             (kill-buffer (get-buffer "*gnus-uu-body*"))
482             (setq state (cons 'end state))))
483       (if (memq 'begin state)
484           (cons gnus-uu-saved-article-name state)
485         state)))))
486
487 ;; Binhex treatment - not very advanced. 
488
489 (defconst gnus-uu-binhex-body-line 
490   "^[^:]...............................................................$")
491 (defconst gnus-uu-binhex-begin-line 
492   "^:...............................................................$")
493 (defconst gnus-uu-binhex-end-line
494   ":$")
495 (defvar gnus-uu-binhex-article-name nil)
496
497 (defun gnus-uu-binhex-article (buffer in-state)
498   (let (state start-char)
499     (save-excursion
500       (set-buffer buffer)
501       (widen)
502       (goto-char 1)
503       (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
504           (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
505               (setq state (list 'wrong-type))))
506
507       (if (memq 'wrong-type state)
508           ()
509         (beginning-of-line)
510         (setq start-char (point))
511         (if (looking-at gnus-uu-binhex-begin-line)
512             (progn
513               (setq state (list 'begin))
514               (write-region 1 1 gnus-uu-binhex-article-name))
515           (setq state (list 'middle)))
516         (goto-char (point-max))
517         (re-search-backward (concat gnus-uu-binhex-body-line "\\|" 
518                                     gnus-uu-binhex-end-line) nil t)
519         (if (looking-at gnus-uu-binhex-end-line)
520             (setq state (if (memq 'begin state)
521                             (cons 'end state)
522                           (list 'end))))
523         (beginning-of-line)
524         (forward-line 1)
525         (if (file-exists-p gnus-uu-binhex-article-name)
526             (append-to-file start-char (point) gnus-uu-binhex-article-name))))
527     (if (memq 'begin state)
528         (cons gnus-uu-binhex-article-name state)
529       state)))
530       
531
532 ;; Find actions.
533
534 (defun gnus-uu-get-actions (files)
535   (let ((ofiles files)
536         action name)
537     (while files
538       (setq name (cdr (assq 'name (car files))))
539       (and 
540        (setq action (gnus-uu-get-action name))
541        (setcar files (cons (cons 'execute (if (string-match "%" action)
542                                               (format action name)
543                                             (concat action " " name)))
544                            (car files))))
545       (setq files (cdr files)))
546     ofiles))
547
548 (defun gnus-uu-get-action (file-name)
549   (let (action)
550     (setq action 
551           (gnus-uu-choose-action 
552            file-name
553            (append 
554             gnus-uu-user-view-rules
555             (if gnus-uu-ignore-default-view-rules 
556                 nil 
557               gnus-uu-default-view-rules)
558             gnus-uu-user-view-rules-end)))
559     (if (and (not (string= (or action "") "gnus-uu-archive")) 
560              gnus-uu-view-with-metamail)
561         (if (setq action 
562                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
563             (setq action (format "metamail -d -b -c \"%s\"" action))))
564     action))
565
566
567 ;; Functions for treating subjects and collecting series.
568
569 (defun gnus-uu-reginize-string (string)
570   ;; Takes a string and puts a \ in front of every special character;
571   ;; ignores any leading "version numbers" thingies that they use in
572   ;; the comp.binaries groups, and either replaces anything that looks
573   ;; like "2/3" with "[0-9]+/[0-9]+" or, if it can't find something
574   ;; like that, replaces the last two numbers with "[0-9]+". This, in
575   ;; my experience, should get most postings of a series.
576   (let ((count 2)
577         (vernum "v[0-9]+[a-z][0-9]+:")
578         reg beg)
579     (save-excursion
580       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
581       (buffer-disable-undo (current-buffer))
582       (erase-buffer)
583       (insert (regexp-quote string))
584       (setq beg 1)
585
586       (setq case-fold-search nil)
587       (goto-char 1)
588       (if (looking-at vernum)
589           (progn
590             (replace-match vernum t t)
591             (setq beg (length vernum))))
592
593       (goto-char beg)
594       (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
595           (replace-match " [0-9]+/[0-9]+")
596
597         (goto-char beg)
598         (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
599             (replace-match "[0-9]+ of [0-9]+")
600
601           (end-of-line)
602           (while (and (re-search-backward "[0-9]" nil t) (> count 0))
603             (while (and 
604                     (looking-at "[0-9]") 
605                     (< 1 (goto-char (1- (point))))))
606             (re-search-forward "[0-9]+" nil t)
607             (replace-match "[0-9]+")
608             (backward-char 5)
609             (setq count (1- count)))))
610
611       (goto-char beg)
612       (while (re-search-forward "[ \t]+" nil t)
613         (replace-match "[ \t]*" t t))
614
615       (buffer-substring 1 (point-max)))))
616
617 (defun gnus-uu-get-list-of-articles (n)
618   ;; If N is non-nil, the article numbers of the N next articles
619   ;; will be returned.
620   ;; If any articles have been marked as processable, they will be
621   ;; returned. 
622   ;; Failing that, articles that have subjects that are part of the
623   ;; same "series" as the current will be returned.
624   (let (articles process)
625   (cond 
626    (n
627     (let ((backward (< n 0))
628           (n (abs n)))
629       (save-excursion
630         (while (and (> n 0)
631                     (setq articles (cons (gnus-summary-article-number) 
632                                          articles))
633                     (gnus-summary-search-forward nil nil backward))
634           (setq n (1- n))))
635       (nreverse articles)))
636    (gnus-newsgroup-processable
637     (reverse gnus-newsgroup-processable))
638    (t
639     (gnus-uu-find-articles-matching)))))
640
641 (defun gnus-uu-string< (l1 l2)
642   (string< (car l1) (car l2)))
643
644 (defun gnus-uu-find-articles-matching 
645   (&optional subject only-unread do-not-translate)
646   ;; Finds all articles that matches the regexp SUBJECT.  If it is
647   ;; nil, the current article name will be used. If ONLY-UNREAD is
648   ;; non-nil, only unread articles are chosen. If DO-NOT-TRANSLATE is
649   ;; non-nil, article names are not equalized before sorting.
650   (let ((subject (or subject 
651                      (gnus-uu-reginize-string (gnus-summary-subject-string))))
652         beg end list-of-subjects)
653     (save-excursion
654       (if (not subject)
655           ()
656         ;; Collect all subjects matching subject.
657         (let ((case-fold-search t)
658               subj mark)
659           (goto-char (point-min))
660           (while (not (eobp))
661             (and (setq subj (gnus-summary-subject-string))
662                  (string-match subject subj)
663                  (or (not only-unread)
664                      (= (setq mark (gnus-summary-article-mark)) 
665                         gnus-unread-mark)
666                      (= mark gnus-ticked-mark)
667                      (= mark gnus-dormant-mark))
668                  (setq list-of-subjects 
669                        (cons (cons subj (gnus-summary-article-number))
670                              list-of-subjects)))
671             (forward-line 1)))
672
673         ;; Expand numbers, sort, and return the list of article
674         ;; numbers.
675         (mapcar (lambda (sub) (cdr sub)) 
676                 (sort (gnus-uu-expand-numbers 
677                        list-of-subjects
678                        (not do-not-translate)) 
679                       'gnus-uu-string<))))))
680
681 (defun gnus-uu-expand-numbers (string-list &optional translate)
682   ;; Takes a list of strings and "expands" all numbers in all the
683   ;; strings.  That is, this function makes all numbers equal length by
684   ;; prepending lots of zeroes before each number. This is to ease later
685   ;; sorting to find out what sequence the articles are supposed to be
686   ;; decoded in. Returns the list of expanded strings.
687   (let ((out-list string-list)
688         string pos num)
689     (save-excursion
690       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
691       (buffer-disable-undo (current-buffer))
692       (while string-list
693         (erase-buffer)
694         (insert (car (car string-list)))
695         ;; Translate multiple spaces to one space.
696         (goto-char 1)
697         (while (re-search-forward "[ \t]+" nil t)
698           (replace-match " "))
699         ;; Translate all characters to "a".
700         (goto-char 1)
701         (if translate 
702             (while (re-search-forward "[A-Za-z]" nil t)
703               (replace-match "a" t t)))
704         ;; Expand numbers.
705         (goto-char 1)
706         (while (re-search-forward "[0-9]+" nil t)
707           (replace-match  
708            (format "%06d" 
709                    (string-to-int (buffer-substring 
710                                    (match-beginning 0) (match-end 0))))))
711         (setq string (buffer-substring 1 (point-max)))
712         (setcar (car string-list) string)
713         (setq string-list (cdr string-list))))
714     out-list))
715
716
717 ;; `gnus-uu-grab-articles' is the general multi-article treatment
718 ;; function.  It takes a list of articles to be grabbed and a function
719 ;; to apply to each article.
720 ;;
721 ;; The function to be called should take two parameters.  The first
722 ;; parameter is the article buffer. The function should leave the
723 ;; result, if any, in this buffer. Most treatment functions will just
724 ;; generate files...
725 ;;
726 ;; The second parameter is the state of the list of articles, and can
727 ;; have four values: `first', `middle', `last' and `first-and-last'.
728 ;;
729 ;; The function should return a list. The list may contain the
730 ;; following symbols:
731 ;; `error' if an error occurred
732 ;; `begin' if the beginning of an encoded file has been received
733 ;;   If the list returned contains a `begin', the first element of
734 ;;   the list *must* be a string with the file name of the decoded
735 ;;   file.
736 ;; `end' if the the end of an encoded file has been received
737 ;; `middle' if the article was a body part of an encoded file
738 ;; `wrong-type' if the article was not a part of an encoded file
739 ;; `ok', which can be used everything is ok
740
741 (defvar gnus-uu-has-been-grabbed nil)
742
743 (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
744   (let (art)
745     (if (not (and gnus-uu-has-been-grabbed
746                   gnus-uu-unmark-articles-not-decoded))
747         ()
748       (if dont-unmark-last-article
749           (progn
750             (setq art (car gnus-uu-has-been-grabbed))
751             (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
752       (while gnus-uu-has-been-grabbed
753         (gnus-summary-tick-article (car gnus-uu-has-been-grabbed) t)
754         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
755       (if dont-unmark-last-article
756           (setq gnus-uu-has-been-grabbed (list art))))))
757
758 ;; This function takes a list of articles and a function to apply to
759 ;; each article grabbed. 
760 ;; 
761 ;; This function returns a list of files decoded if the grabbing and
762 ;; the process-function has been successful and nil otherwise.
763 (defun gnus-uu-grab-articles 
764   (articles process-function &optional sloppy limit no-errors)
765   (let ((state 'first) 
766         (wrong-type t)
767         has-been-begin has-been-end 
768         article result-file result-files process-state article-buffer)
769  
770     (if (not (gnus-server-opened gnus-current-select-method))
771         (progn
772           (gnus-start-news-server)
773           (gnus-request-group gnus-newsgroup-name)))
774
775     (setq gnus-uu-has-been-grabbed nil)
776
777     (while (and articles 
778                 (not (memq 'error process-state))
779                 (or sloppy
780                     (not (memq 'end process-state))))
781
782       (setq article (car articles))
783       (setq articles (cdr articles))
784       (setq gnus-uu-has-been-grabbed (cons article gnus-uu-has-been-grabbed))
785
786       (if (> article gnus-uu-highest-article-number) 
787           (setq gnus-uu-highest-article-number article))
788
789       (if (eq articles ()) 
790           (if (eq state 'first)
791               (setq state 'first-and-last)
792             (setq state 'last)))
793
794       (message "Getting article %d" article)
795
796       (if (not (= (or gnus-current-article 0) article))
797           (progn
798             (gnus-request-article article gnus-newsgroup-name
799                                   nntp-server-buffer)
800             (setq gnus-last-article gnus-current-article)
801             (setq gnus-current-article article)
802             (if (stringp nntp-server-buffer)
803                 (setq article-buffer nntp-server-buffer)
804               (setq article-buffer (buffer-name nntp-server-buffer))))
805         (setq article-buffer gnus-article-buffer))
806
807       (buffer-disable-undo article-buffer)
808       (gnus-summary-mark-as-read article)
809       (and (memq article gnus-newsgroup-processable)
810            (gnus-summary-remove-process-mark article))
811
812       (setq process-state (funcall process-function article-buffer state))
813
814       (if (or (memq 'begin process-state)
815               (and (or (eq state 'first) (eq state 'first-and-last))
816                    (memq 'ok process-state)))
817           (progn
818             (if has-been-begin
819                 (if (and result-file (file-exists-p result-file)) 
820                     (delete-file result-file)))
821             (if (memq 'begin process-state)
822                 (setq result-file (car process-state)))
823             (setq has-been-begin t)
824             (setq has-been-end nil)))
825
826       (if (memq 'end process-state)
827           (progn
828             (setq gnus-uu-has-been-grabbed nil)
829             (setq result-files (cons result-file result-files))
830             (setq has-been-end t)
831             (setq has-been-begin nil)
832             (and limit (= (length result-files) limit)
833                  (setq articles nil))))
834
835       (if (and (or (eq state 'last) (eq state 'first-and-last))
836                (not (memq 'end process-state)))
837                 (if (and result-file (file-exists-p result-file))
838                     (delete-file result-file)))
839
840 ;      (setq result-file nil)
841
842       (if (not (memq 'wrong-type process-state))
843           (setq wrong-type nil)
844         (if gnus-uu-unmark-articles-not-decoded
845             (gnus-summary-tick-article article t)))
846
847       (if sloppy (setq wrong-type nil))
848
849       (if (and (not has-been-begin)
850                (not sloppy)
851                (or (memq 'end process-state)
852                    (memq 'middle process-state)))
853           (progn
854             (setq process-state (list 'error))
855             (message "No begin part at the beginning")
856             (sleep-for 2))
857         (setq state 'middle)))
858
859     ;; Make sure the last article is put in the article buffer & fix
860     ;; windows etc.
861
862     (if (not (string= article-buffer gnus-article-buffer))
863         (save-excursion
864           (set-buffer (get-buffer-create gnus-article-buffer))
865           (let ((buffer-read-only nil))
866             (widen)
867             (erase-buffer)
868             (insert-buffer-substring article-buffer)
869             (goto-char 1))))
870     (run-hooks 'gnus-mark-article-hook)
871
872     (if result-files
873         ()
874       (if (not has-been-begin)
875           (if (not no-errors) (message "Wrong type file"))
876         (if (memq 'error process-state)
877             (setq result-files nil)
878           (if (not (or (memq 'ok process-state) 
879                        (memq 'end process-state)))
880               (progn
881                 (if (not no-errors)
882                     (message "End of articles reached before end of file"))
883                 (setq result-files nil))
884             (gnus-uu-unmark-list-of-grabbed)))))
885     (setq gnus-uu-list-of-files-decoded result-files)
886     (and result-files
887          (mapcar (lambda (file) (list (cons 'name file))) result-files))))
888
889 (defun gnus-uu-uudecode-sentinel (process event)
890   (delete-process (get-process process)))
891
892 (defun gnus-uu-uustrip-article (process-buffer in-state)
893   ;; Uudecodes a file asynchronously.
894   (let ((state (list 'ok))
895         (process-connection-type nil)
896         start-char pst name-beg name-end)
897     (save-excursion
898       (set-buffer process-buffer)
899       (let ((case-fold-search nil)
900             (buffer-read-only nil))
901
902         (goto-char 1)
903
904         (if gnus-uu-kill-carriage-return
905             (progn
906               (while (search-forward "\r" nil t)
907                 (delete-backward-char 1))
908               (goto-char 1)))
909
910         (if (not (re-search-forward gnus-uu-begin-string nil t))
911             (if (not (re-search-forward gnus-uu-body-line nil t))
912                 (setq state (list 'wrong-type))))
913      
914         (if (memq 'wrong-type state)
915             ()
916           (beginning-of-line)
917           (setq start-char (point))
918
919           (if (looking-at gnus-uu-begin-string)
920               (progn 
921                 (setq name-end (match-end 1))
922
923                 ; Replace any slashes and spaces in file names before decoding
924                 (goto-char (setq name-beg (match-beginning 1)))
925                 (while (re-search-forward "/" name-end t)
926                   (replace-match ","))
927                 (goto-char name-beg)
928                 (while (re-search-forward " " name-end t)
929                   (replace-match "_"))
930                 (goto-char name-beg)
931                 (if (re-search-forward "_*$" name-end t)
932                   (replace-match ""))
933
934                 (setq gnus-uu-file-name (buffer-substring name-beg name-end))
935                 (and gnus-uu-uudecode-process
936                      (setq pst (process-status 
937                                 (or gnus-uu-uudecode-process "nevair")))
938                      (if (or (eq pst 'stop) (eq pst 'run))
939                          (progn
940                            (delete-process gnus-uu-uudecode-process)
941                            (gnus-uu-unmark-list-of-grabbed t))))
942                 (if (get-process "*uudecode*")
943                     (delete-process "*uudecode*"))
944                 (setq gnus-uu-uudecode-process
945                       (start-process 
946                        "*uudecode*" 
947                        (get-buffer-create gnus-uu-output-buffer-name)
948                        "sh" "-c" 
949                        (format "cd %s ; uudecode" gnus-uu-work-dir)))
950                 (set-process-sentinel 
951                  gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
952                 (setq state (list 'begin))
953                 (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
954             (setq state (list 'middle)))
955         
956           (goto-char (point-max))
957
958           (re-search-backward 
959            (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
960           (beginning-of-line)
961
962           (if (looking-at gnus-uu-end-string)
963               (setq state (cons 'end state)))
964           (forward-line 1)
965
966           (and gnus-uu-uudecode-process
967                (setq pst (process-status 
968                           (or gnus-uu-uudecode-process "nevair")))
969                (if (or (eq pst 'run) (eq pst 'stop))
970                    (progn
971                      (if gnus-uu-correct-stripped-uucode
972                          (progn
973                            (gnus-uu-check-correct-stripped-uucode 
974                             start-char (point))
975                            (goto-char (point-max))
976                            (re-search-backward 
977                             (concat gnus-uu-body-line "\\|" 
978                                     gnus-uu-end-string) 
979                             nil t)
980                            (forward-line 1)))
981
982                      (condition-case err
983                          (process-send-region gnus-uu-uudecode-process 
984                                               start-char (point))
985                        (error 
986                         (progn 
987                           (delete-process gnus-uu-uudecode-process)
988                           (message "gnus-uu: Couldn't uudecode")
989 ;                         (sleep-for 2)
990                           (setq state (list 'wrong-type)))))
991
992                      (if (memq 'end state)
993                          (accept-process-output gnus-uu-uudecode-process)))
994                  (setq state (list 'wrong-type))))
995           (if (not gnus-uu-uudecode-process)
996               (setq state (list 'wrong-type)))))
997
998       (if (memq 'begin state)
999           (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
1000         state))))
1001
1002 ;; This function is used by `gnus-uu-grab-articles' to treat
1003 ;; a shared article.
1004 (defun gnus-uu-unshar-article (process-buffer in-state)
1005   (let ((state (list 'ok))
1006         start-char)
1007     (save-excursion
1008      (set-buffer process-buffer)
1009      (goto-char 1)
1010      (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
1011          (setq state (list 'wrong-type))
1012        (beginning-of-line)
1013        (setq start-char (point))
1014        (call-process-region 
1015         start-char (point-max) "sh" nil 
1016         (get-buffer-create gnus-uu-output-buffer-name) nil 
1017         "-c" (concat "cd " gnus-uu-work-dir " ; sh"))))
1018     state))
1019
1020 ;; Returns the name of what the shar file is going to unpack.
1021 (defun gnus-uu-find-name-in-shar ()
1022   (let ((oldpoint (point))
1023         res)
1024     (goto-char 1)
1025     (if (re-search-forward gnus-uu-shar-name-marker nil t)
1026         (setq res (buffer-substring (match-beginning 1) (match-end 1))))
1027     (goto-char oldpoint)
1028     res))
1029
1030 ;; `gnus-uu-choose-action' chooses what action to perform given the name
1031 ;; and `gnus-uu-file-action-list'.  Returns either nil if no action is
1032 ;; found, or the name of the command to run if such a rule is found.
1033 (defun gnus-uu-choose-action (file-name file-action-list &optional no-ignore)
1034   (let ((action-list (copy-sequence file-action-list))
1035         rule action)
1036     (and 
1037      (or no-ignore 
1038          (and (not 
1039                (and gnus-uu-ignore-files-by-name
1040                     (string-match gnus-uu-ignore-files-by-name file-name)))
1041               (not 
1042                (and gnus-uu-ignore-files-by-type
1043                     (string-match gnus-uu-ignore-files-by-type 
1044                                   (or (gnus-uu-choose-action 
1045                                        file-name gnus-uu-ext-to-mime-list t) 
1046                                       ""))))))
1047      (while (not (or (eq action-list ()) action))
1048        (setq rule (car action-list))
1049        (setq action-list (cdr action-list))
1050        (if (string-match (car rule) file-name)
1051            (setq action (car (cdr rule))))))
1052     action))
1053
1054 (defun gnus-uu-treat-archive (file-path)
1055   ;; Unpacks an archive. Returns t if unpacking is successful.
1056   (let ((did-unpack t)
1057         action command files file file-name dir)
1058     (setq action (gnus-uu-choose-action 
1059                   file-path (append gnus-uu-user-archive-rules
1060                                     (if gnus-uu-ignore-default-archive-rules
1061                                         nil
1062                                       gnus-uu-default-archive-rules))))
1063
1064     (if (not action) (error "No unpackers for the file %s" file-path))
1065
1066     (string-match "/[^/]*$" file-path)
1067     (setq file-name (substring file-path (1+ (match-beginning 0))))
1068     (setq dir (substring file-path 0 (match-beginning 0)))
1069
1070     (if (member action gnus-uu-destructive-archivers)
1071         (copy-file file-path (concat file-path "~") t))
1072
1073     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
1074
1075     (save-excursion
1076       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
1077       (erase-buffer))
1078
1079     (message "Unpacking: %s..." (gnus-uu-command action file-path))
1080
1081     (if (= 0 (call-process "sh" nil 
1082                            (get-buffer-create gnus-uu-output-buffer-name)
1083                            nil "-c" command))
1084         (message "")
1085       (message "Error during unpacking of archive")
1086       (setq did-unpack nil))
1087
1088     (if (member action gnus-uu-destructive-archivers)
1089         (rename-file (concat file-path "~") file-path t))
1090
1091     did-unpack))
1092
1093 (defun gnus-uu-dir-files (dir)
1094   (let ((dirs (directory-files dir t "[^/][^\\.][^\\.]?$"))
1095         files file)
1096     (while dirs
1097       (if (file-directory-p (setq file (car dirs)))
1098           (setq files (append files (gnus-uu-dir-files file)))
1099         (setq files (cons file files)))
1100       (setq dirs (cdr dirs)))
1101     files))
1102
1103 (defun gnus-uu-unpack-files (files &optional ignore)
1104   ;; Go through FILES and look for files to unpack. 
1105   (let* ((totfiles (gnus-uu-ls-r gnus-uu-work-dir))
1106          (ofiles files)
1107          file did-unpack)
1108     (while files
1109       (setq file (cdr (assq 'name (car files))))
1110       (if (and (not (member file ignore))
1111                (equal (gnus-uu-get-action (file-name-nondirectory file))
1112                       "gnus-uu-archive"))
1113           (progn
1114             (setq did-unpack (cons file did-unpack))
1115             (or (gnus-uu-treat-archive file)
1116                 (message "Error during unpacking of %s" file))
1117             (let* ((newfiles (gnus-uu-ls-r gnus-uu-work-dir))
1118                    (nfiles newfiles))
1119               (while nfiles
1120                 (or (member (car nfiles) totfiles)
1121                     (setq ofiles (cons (list (cons 'name (car nfiles))
1122                                              (cons 'original file))
1123                                        ofiles)))
1124                 (setq nfiles (cdr nfiles)))
1125               (setq totfiles newfiles))))
1126       (setq files (cdr files)))
1127     (if did-unpack 
1128         (gnus-uu-unpack-files ofiles (append did-unpack ignore))
1129       ofiles)))
1130
1131 (defun gnus-uu-ls-r (dir)
1132   (let* ((files (gnus-uu-directory-files dir t))
1133          (ofiles files))
1134     (while files
1135       (if (file-directory-p (car files))
1136           (progn
1137             (setq ofiles (delete (car files) ofiles))
1138             (setq ofiles (append ofiles (gnus-uu-ls-r (car files))))))
1139       (setq files (cdr files)))
1140     ofiles))
1141
1142 ;; Various stuff
1143
1144 (defun gnus-uu-directory-files (dir &optional full)
1145   (let (files out file)
1146     (setq files (directory-files dir full))
1147     (while files
1148       (setq file (car files))
1149       (setq files (cdr files))
1150       (or (string-match "/\\.\\.?$" file)
1151           (setq out (cons file out))))
1152     (setq out (nreverse out))
1153     out))
1154
1155 (defun gnus-uu-check-correct-stripped-uucode (start end)
1156   (let (found beg length short)
1157     (if (not gnus-uu-correct-stripped-uucode)
1158         ()
1159       (goto-char start)
1160
1161       (if (re-search-forward " \\|`" end t)
1162           (progn
1163             (goto-char start)
1164             (while (not (eobp))
1165               (progn
1166                 (if (looking-at "\n") (replace-match ""))
1167                 (forward-line 1))))
1168             
1169         (while (not (eobp))
1170           (if (looking-at (concat gnus-uu-begin-string "\\|" 
1171                                   gnus-uu-end-string))
1172               ()
1173             (if (not found)
1174                 (progn
1175                   (beginning-of-line)
1176                   (setq beg (point))
1177                   (end-of-line)
1178                   (setq length (- (point) beg))))
1179             (setq found t)
1180             (beginning-of-line)
1181             (setq beg (point))
1182             (end-of-line)
1183             (if (not (= length (- (point) beg)))
1184                 (insert (make-string (- length (- (point) beg)) ? ))))
1185           (forward-line 1))))))
1186
1187 (defun gnus-uu-initialize ()
1188   (setq gnus-uu-highest-article-number 1)
1189   (gnus-uu-check-for-generated-files)
1190   (setq gnus-uu-tmp-dir (expand-file-name gnus-uu-tmp-dir))
1191   (if (string-match "[^/]$" gnus-uu-tmp-dir) 
1192       (setq gnus-uu-tmp-dir (concat gnus-uu-tmp-dir "/")))
1193   (if (not (file-directory-p gnus-uu-tmp-dir))
1194       (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
1195     (if (not (file-writable-p gnus-uu-tmp-dir))
1196         (error "Temp directory %s can't be written to" gnus-uu-tmp-dir)))
1197   (setq gnus-uu-work-dir 
1198         (concat gnus-uu-tmp-dir (make-temp-name "gnus")))
1199   (gnus-uu-add-file gnus-uu-work-dir)
1200   (if (not (file-directory-p gnus-uu-work-dir)) 
1201       (make-directory gnus-uu-work-dir))
1202   (setq gnus-uu-work-dir (concat gnus-uu-work-dir "/")))
1203
1204 ;; Kills the temporary uu buffers, kills any processes, etc.
1205 (defun gnus-uu-clean-up ()
1206   (let (buf pst)
1207     (and gnus-uu-uudecode-process
1208          (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
1209          (if (or (eq pst 'stop) (eq pst 'run))
1210              (delete-process gnus-uu-uudecode-process)))
1211     (and (setq buf (get-buffer gnus-uu-output-buffer-name))
1212          (kill-buffer buf))))
1213
1214 ;; `gnus-uu-check-for-generated-files' deletes any generated files that
1215 ;; hasn't been deleted, if, for instance, the user terminated decoding
1216 ;; with `C-g'.
1217 (defun gnus-uu-check-for-generated-files ()
1218   (let (file dirs)
1219     (while gnus-uu-generated-file-list
1220       (setq file (car gnus-uu-generated-file-list))
1221       (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
1222       (if (not (string-match "/\\.[\\.]?$" file))
1223           (progn
1224             (if (file-directory-p file)
1225                 (setq dirs (cons file dirs))
1226               (if (file-exists-p file)
1227                   (delete-file file))))))
1228     (setq dirs (nreverse dirs))
1229     (while dirs
1230       (setq file (car dirs))
1231       (setq dirs (cdr dirs))
1232       (if (file-directory-p file)
1233           (if (string-match "/$" file)
1234               (delete-directory (substring file 0 (match-beginning 0)))
1235             (delete-directory file))))))
1236
1237 ;; Add a file (or a list of files) to be checked (and deleted if it/they
1238 ;; still exists upon exiting the newsgroup).
1239 (defun gnus-uu-add-file (file)
1240   (if (stringp file)
1241       (setq gnus-uu-generated-file-list 
1242             (cons file gnus-uu-generated-file-list))
1243     (setq gnus-uu-generated-file-list 
1244           (append file gnus-uu-generated-file-list))))
1245
1246 ;; Inputs an action and a file and returns a full command, putting
1247 ;; ticks round the file name and escaping any ticks in the file name.
1248 (defun gnus-uu-command (action file)
1249   (let ((ofile ""))
1250     (while (string-match "`\\|\"\\|\\$\\|\\\\" file)
1251       (progn
1252         (setq ofile
1253               (concat ofile (substring file 0 (match-beginning 0)) "\\"
1254                       (substring file (match-beginning 0) (match-end 0))))
1255         (setq file (substring file (1+ (match-beginning 0))))))
1256     (setq ofile (concat "\"" ofile file "\""))
1257     (if (string-match "%s" action)
1258         (format action ofile)
1259       (concat action " " ofile))))
1260
1261
1262 ;; Initializing
1263
1264 (add-hook 'gnus-exit-group-hook 'gnus-uu-clean-up)
1265 (add-hook 'gnus-exit-group-hook 'gnus-uu-check-for-generated-files)
1266
1267
1268 ;; Major mode for posting encoded articles.
1269
1270 (require 'sendmail)
1271 (require 'rnews)
1272
1273 ;; Any function that is to be used as and encoding method will take two
1274 ;; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
1275 ;; and "spiral.jpg", respectively.) The function should return nil if
1276 ;; the encoding wasn't successful.
1277 (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
1278   "Function used for encoding binary files.
1279 There are three functions supplied with gnus-uu for encoding files:
1280 `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1281 `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
1282 headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
1283 uuencode and adds MIME headers.")
1284
1285 (defvar gnus-uu-post-include-before-composing nil
1286   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
1287 If this variable is t, you can either include an encoded file with
1288 \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
1289
1290 (defvar gnus-uu-post-length 990
1291   "Maximum length of an article.
1292 The encoded file will be split into how many articles it takes to
1293 post the entire file.")
1294
1295 (defvar gnus-uu-post-threaded nil
1296   "Non-nil means that gnus-uu will post the encoded file in a thread.
1297 This may not be smart, as no other decoder I have seen are able to
1298 follow threads when collecting uuencoded articles. (Well, I have seen
1299 one package that does that - gnus-uu, but somehow, I don't think that 
1300 counts...) Default is nil.")
1301
1302 (defvar gnus-uu-post-separate-description t
1303   "Non-nil means that the description will be posted in a separate article.
1304 The first article will typically be numbered (0/x). If this variable
1305 is nil, the description the user enters will be included at the 
1306 beginning of the first article, which will be numbered (1/x). Default 
1307 is t.")
1308
1309 (defconst gnus-uu-post-binary-separator "--binary follows this line--")
1310 (defvar gnus-uu-post-message-id nil)
1311 (defvar gnus-uu-post-inserted-file-name nil)
1312 (defvar gnus-uu-winconf-post-news nil)
1313
1314 ;; The following map and mode was taken from rnewspost.el and edited
1315 ;; somewhat.
1316 (defvar gnus-uu-post-reply-mode-map nil)
1317 (if gnus-uu-post-reply-mode-map
1318     ()
1319   (setq gnus-uu-post-reply-mode-map (make-keymap))
1320   (define-key gnus-uu-post-reply-mode-map "\C-c?" 'describe-mode)
1321   (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-d" 
1322     'news-reply-distribution)
1323   (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-k" 
1324     'news-reply-keywords)
1325   (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-n" 
1326     'news-reply-newsgroups)
1327       
1328   (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-f" 
1329     'news-reply-followup-to)
1330   (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-s" 'mail-subject)
1331   (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-a" 
1332     'gnus-uu-post-reply-summary)
1333   (define-key gnus-uu-post-reply-mode-map "\C-c\C-r" 
1334     'news-caesar-buffer-body)
1335   (define-key gnus-uu-post-reply-mode-map "\C-c\C-w" 'news-reply-signature)
1336   (define-key gnus-uu-post-reply-mode-map "\C-c\C-y" 
1337     'news-reply-yank-original)
1338   (define-key gnus-uu-post-reply-mode-map "\C-c\C-q" 
1339     'mail-fill-yanked-message)
1340   (define-key gnus-uu-post-reply-mode-map "\C-c\C-c" 
1341     'gnus-uu-post-news-inews)
1342   (define-key gnus-uu-post-reply-mode-map "\C-c\C-s" 
1343     'gnus-uu-post-news-inews)
1344   (define-key gnus-uu-post-reply-mode-map "\C-c\C-i" 
1345     'gnus-uu-post-insert-binary-in-article)
1346   )
1347
1348 ;; This mode was taken from rnewspost.el and modified slightly.
1349 (defun gnus-uu-post-reply-mode ()
1350   "Major mode for editing binary news to be posted on USENET.
1351 First-time posters are asked to please read the articles in newsgroup:
1352                                                      news.announce.newusers .
1353
1354 Like news-reply-mode, which is like Text Mode, but with these
1355 additional commands:
1356
1357 \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-news-inews]  post the message.
1358 C-c C-f  move to a header field (and create it if there isn't):
1359          C-c C-f C-n  move to Newsgroups:       C-c C-f C-s  move to Subj:
1360          C-c C-f C-f  move to Followup-To:      C-c C-f C-k  move to Keywords:
1361          C-c C-f C-d  move to Distribution:     C-c C-f C-a  move to Summary:
1362 C-c C-y  news-reply-yank-original (insert current message, in NEWS).
1363 C-c C-q  mail-fill-yanked-message (fill what was yanked).
1364 C-c C-r  caesar rotate all letters by 13 places in the article's body (rot13).
1365 \\[gnus-uu-post-insert-binary-in-article]  encode and include a file in this article.
1366
1367 This mode is almost identical to news-reply-mode, but has some
1368 additional commands for treating encoded binary articles. In
1369 particular, \\[gnus-uu-post-news-inews] will ask for a file to include, if
1370 one hasn't been included already. It will post, first, the message
1371 composed, and then it will post as many additional articles it takes
1372 to post the entire encoded files.
1373
1374    Relevant Variables
1375
1376    `gnus-uu-post-encode-method' 
1377    There are three functions supplied with gnus-uu for encoding files:
1378    `gnus-uu-post-encode-uuencode', which does straight uuencoding;
1379    `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
1380    headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
1381    uuencode and adds MIME headers.
1382  
1383    `gnus-uu-post-include-before-composing'
1384    Non-nil means that gnus-uu will ask for a file to encode before you
1385    compose the article.  If this variable is t, you can either include
1386    an encoded file with `C-c C-i' or have one included for you when you 
1387    post the article.
1388
1389    `gnus-uu-post-length'
1390    Maximum length of an article. The encoded file will be split into how 
1391    many articles it takes to post the entire file.
1392
1393    `gnus-uu-post-separate-description'
1394    Non-nil means that the description will be posted in a separate 
1395    article. The first article will typically be numbered (0/x). If 
1396    this variable is nil, the description the user enters will be 
1397    included at the beginning of the first article, which will be 
1398    numbered (1/x). Default is t.
1399
1400    `gnus-uu-post-threaded'
1401    Non-nil means that gnus-uu will post the encoded file in a thread.
1402    This may not be smart, as no other decoder I have seen are able to
1403    follow threads when collecting uuencoded articles. (Well, I have seen
1404    one package that does that - gnus-uu, but somehow, I don't think that 
1405    counts...) Default is nil.
1406 "
1407   (interactive)
1408   ;; require...
1409   (or (fboundp 'mail-setup) (load "sendmail"))
1410   (kill-all-local-variables)
1411   (make-local-variable 'mail-reply-buffer)
1412   (setq mail-reply-buffer nil)
1413   (set-syntax-table text-mode-syntax-table)
1414   (use-local-map gnus-uu-post-reply-mode-map)
1415   (setq local-abbrev-table text-mode-abbrev-table)
1416   (setq major-mode 'gnus-uu-post-reply-mode)
1417   (setq mode-name "Gnus UU News")
1418   (make-local-variable 'paragraph-separate)
1419   (make-local-variable 'paragraph-start)
1420   (setq paragraph-start (concat "^" mail-header-separator "$\\|"
1421                                 paragraph-start))
1422   (setq paragraph-separate (concat "^" mail-header-separator "$\\|"
1423                                    paragraph-separate))
1424   (run-hooks 'text-mode-hook 'gnus-uu-post-reply-mode-hook))
1425
1426 (defun gnus-uu-post-news ()
1427   "Compose an article and post an encoded file."
1428   (interactive)
1429   (setq gnus-uu-post-inserted-file-name nil)
1430   (setq gnus-uu-winconf-post-news (current-window-configuration))
1431   (let (news-reply-mode)
1432     (fset 'news-reply-mode 'gnus-uu-post-reply-mode)
1433     (gnus-summary-post-news)
1434     (if gnus-uu-post-include-before-composing
1435         (save-excursion (setq gnus-uu-post-inserted-file-name 
1436                               (gnus-uu-post-insert-binary))))))
1437
1438 (defun gnus-uu-post-insert-binary-in-article ()
1439   "Inserts an encoded file in the buffer.
1440 The user will be asked for a file name."
1441   (interactive)
1442   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1443       (error "Not in post-news buffer"))
1444   (save-excursion 
1445     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
1446
1447 ;; Encodes with uuencode and substitutes all spaces with backticks.
1448 (defun gnus-uu-post-encode-uuencode (path file-name)
1449   (if (gnus-uu-post-encode-file "uuencode" path file-name)
1450       (progn
1451         (goto-char 1)
1452         (forward-line 1)
1453         (while (re-search-forward " " nil t)
1454           (replace-match "`"))
1455         t)))
1456
1457 ;; Encodes with uuencode and adds MIME headers.
1458 (defun gnus-uu-post-encode-mime-uuencode (path file-name)
1459   (if (gnus-uu-post-encode-uuencode path file-name)
1460       (progn
1461         (gnus-uu-post-make-mime file-name "x-uue")
1462         t)))
1463
1464 ;; Encodes with base64 and adds MIME headers
1465 (defun gnus-uu-post-encode-mime (path file-name)
1466   (if (gnus-uu-post-encode-file "mmencode" path file-name)
1467       (progn
1468         (gnus-uu-post-make-mime file-name "base64")
1469         t)))
1470
1471 ;; Adds MIME headers.
1472 (defun gnus-uu-post-make-mime (file-name encoding)
1473   (goto-char 1)
1474   (insert (format "Content-Type: %s; name=\"%s\"\n" 
1475                   (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list) 
1476                   file-name))
1477   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
1478   (save-restriction
1479     (set-buffer gnus-post-news-buffer)
1480     (goto-char 1)
1481     (re-search-forward mail-header-separator)
1482     (beginning-of-line)
1483     (forward-line -1)
1484     (narrow-to-region 1 (point))
1485     (or (mail-fetch-field "mime-version")
1486         (progn
1487           (widen)
1488           (insert "MIME-Version: 1.0\n")))
1489     (widen)))
1490
1491 ;; Encodes a file PATH with COMMAND, leaving the result in the
1492 ;; current buffer.
1493 (defun gnus-uu-post-encode-file (command path file-name)
1494   (= 0 (call-process "sh" nil t nil "-c" 
1495                      (format "%s %s %s" command path file-name))))
1496
1497 (defun gnus-uu-post-news-inews ()
1498   "Posts the composed news article and encoded file.
1499 If no file has been included, the user will be asked for a file."
1500   (interactive)
1501   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
1502       (error "Not in post news buffer"))
1503
1504   (let (file-name)
1505
1506     (if gnus-uu-post-inserted-file-name
1507         (setq file-name gnus-uu-post-inserted-file-name)
1508       (setq file-name (gnus-uu-post-insert-binary)))
1509   
1510     (if gnus-uu-post-threaded
1511         (let ((gnus-required-headers 
1512                (if (memq 'Message-ID gnus-required-headers)
1513                    gnus-required-headers
1514                  (cons 'Message-ID gnus-required-headers)))
1515               gnus-inews-article-hook elem)
1516
1517           (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
1518                                             gnus-inews-article-hook
1519                                           (list gnus-inews-article-hook)))
1520           (setq gnus-inews-article-hook 
1521                 (cons
1522                  '(lambda ()
1523                     (save-excursion
1524                       (goto-char 1)
1525                       (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
1526                           (setq gnus-uu-post-message-id 
1527                                 (buffer-substring 
1528                                  (match-beginning 1) (match-end 1)))
1529                         (setq gnus-uu-post-message-id nil))))
1530                  gnus-inews-article-hook))
1531           (gnus-uu-post-encoded file-name t))
1532       (gnus-uu-post-encoded file-name nil)))
1533   (setq gnus-uu-post-inserted-file-name nil)
1534   (and gnus-uu-winconf-post-news
1535        (set-window-configuration gnus-uu-winconf-post-news)))
1536       
1537 ;; Asks for a file to encode, encodes it and inserts the result in
1538 ;; the current buffer. Returns the file name the user gave.
1539 (defun gnus-uu-post-insert-binary ()
1540   (let ((uuencode-buffer-name "*uuencode buffer*")
1541         file-path post-buf uubuf file-name)
1542
1543     (setq file-path (read-file-name 
1544                      "What file do you want to encode? "))
1545     (if (not (file-exists-p file-path))
1546         (error "%s: No such file" file-path))
1547
1548     (goto-char (point-max))
1549     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
1550     
1551     (if (string-match "^~/" file-path)
1552         (setq file-path (concat "$HOME" (substring file-path 1))))
1553     (if (string-match "/[^/]*$" file-path)
1554         (setq file-name (substring file-path (1+ (match-beginning 0))))
1555       (setq file-name file-path))
1556
1557     (unwind-protect
1558         (if (save-excursion
1559               (set-buffer (setq uubuf 
1560                                 (get-buffer-create uuencode-buffer-name)))
1561               (erase-buffer)
1562               (funcall gnus-uu-post-encode-method file-path file-name))
1563             (insert-buffer uubuf)
1564           (error "Encoding unsuccessful"))
1565       (kill-buffer uubuf))
1566     file-name))
1567
1568 ;; Posts the article and all of the encoded file.
1569 (defun gnus-uu-post-encoded (file-name &optional threaded)
1570   (let ((send-buffer-name "*uuencode send buffer*")
1571         (encoded-buffer-name "*encoded buffer*")
1572         (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
1573         (separator (concat mail-header-separator "\n\n"))
1574         file uubuf length parts header i end beg
1575         beg-line minlen buf post-buf whole-len beg-binary end-binary)
1576
1577     (setq post-buf (current-buffer))
1578
1579     (goto-char 1)
1580     (if (not (re-search-forward 
1581               (if gnus-uu-post-separate-description 
1582                   gnus-uu-post-binary-separator 
1583                 mail-header-separator) nil t))
1584         (error "Internal error: No binary/header separator"))
1585     (beginning-of-line)
1586     (forward-line 1)
1587     (setq beg-binary (point))
1588     (setq end-binary (point-max))
1589
1590     (save-excursion 
1591       (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
1592       (erase-buffer)
1593       (insert-buffer-substring post-buf beg-binary end-binary)
1594       (goto-char 1)
1595       (setq length (count-lines 1 (point-max)))
1596       (setq parts (/ length gnus-uu-post-length))
1597       (if (not (< (% length gnus-uu-post-length) 4))
1598           (setq parts (1+ parts))))
1599
1600     (if gnus-uu-post-separate-description
1601         (forward-line -1))
1602     (kill-region (point) (point-max))
1603
1604     (goto-char 1)
1605     (search-forward mail-header-separator nil t)
1606     (beginning-of-line)
1607     (setq header (buffer-substring 1 (point)))
1608
1609     (goto-char 1)
1610     (if (not gnus-uu-post-separate-description)
1611         ()
1612       (if (and (not threaded) (re-search-forward "^Subject: " nil t))
1613           (progn
1614             (end-of-line)
1615             (insert (format " (0/%d)" parts))))
1616       (gnus-inews-news))
1617
1618     (save-excursion
1619       (setq i 1)
1620       (setq beg 1)
1621       (while (not (> i parts))
1622         (set-buffer (get-buffer-create send-buffer-name))
1623         (erase-buffer)
1624         (insert header)
1625         (if (and threaded gnus-uu-post-message-id)
1626             (insert (format "References: %s\n" gnus-uu-post-message-id)))
1627         (insert separator)
1628         (setq whole-len
1629               (- 62 (length (format top-string "" file-name i parts ""))))
1630         (if (> 1 (setq minlen (/ whole-len 2)))
1631             (setq minlen 1))
1632         (setq 
1633          beg-line 
1634          (format top-string
1635                  (make-string minlen ?-) 
1636                  file-name i parts
1637                  (make-string 
1638                   (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
1639
1640         (goto-char 1)
1641         (if (not (re-search-forward "^Subject: " nil t))
1642             ()
1643           (if (not threaded)
1644               (progn
1645                 (end-of-line)
1646                 (insert (format " (%d/%d)" i parts)))
1647             (if (or (and (= i 2) gnus-uu-post-separate-description)
1648                     (and (= i 1) (not gnus-uu-post-separate-description)))
1649                 (replace-match "Subject: Re: "))))
1650                   
1651         (goto-char (point-max))
1652         (save-excursion
1653           (set-buffer uubuf)
1654           (goto-char beg)
1655           (if (= i parts)
1656               (goto-char (point-max))
1657             (forward-line gnus-uu-post-length))
1658           (if (and (= (1+ i) parts) (< (count-lines (point) (point-max)) 4))
1659               (forward-line -4))
1660           (setq end (point)))
1661         (insert-buffer-substring uubuf beg end)
1662         (insert beg-line)
1663         (insert "\n")
1664         (setq beg end)
1665         (setq i (1+ i))
1666         (goto-char 1)
1667         (re-search-forward mail-header-separator nil t)
1668         (beginning-of-line)
1669         (forward-line 2)
1670         (if (re-search-forward gnus-uu-post-binary-separator nil t)
1671             (progn 
1672               (replace-match "")
1673               (forward-line 1)))
1674         (insert beg-line)
1675         (insert "\n")
1676         (gnus-inews-news)))
1677
1678     (and (setq buf (get-buffer send-buffer-name))
1679          (kill-buffer buf))
1680     (and (setq buf (get-buffer encoded-buffer-name))
1681          (kill-buffer buf))
1682
1683     (if (not gnus-uu-post-separate-description)
1684         (progn
1685           (set-buffer-modified-p nil)
1686           (and (fboundp 'bury-buffer) (bury-buffer))))))
1687
1688 (provide 'gnus-uu)
1689
1690 ;; gnus-uu.el ends here