8e037a9152c6fb2a30accba4a24eef630098ac99
[elisp/tm.git] / tm-partial.el
1 ;;; 
2 ;;; tm-partial.el
3 ;;; 
4 ;;; Grabbing all MIME "message/partial"s.
5 ;;; by Yasuo OKABE @ Kyoto University 1994
6 ;;; modified by MORIOKA Tomohiko
7 ;;;         and Shuhei KOBAYASHI <shuhei@cmpt01.phys.tohoku.ac.jp>
8 ;;;
9 ;;; original file is 
10 ;;;  gif.el written by Art Mellor @ Cayman Systems, Inc. 1991
11 ;;;
12 ;;; This file is a part of tm (Tools for MIME).
13 ;;;
14 ;;; $Id: tm-partial.el,v 6.0 1995/09/20 14:39:23 morioka Exp $
15 ;;;
16
17 (require 'tm-view)
18
19 ;; This regular expression controls what types of subject lines can be
20 ;; parsed. Currently handles lines like:
21 ;;      foo [1/3]
22 ;;      foo (1/3)
23 ;;      foo 1/3
24 ;;      foo [1 of 3]
25 ;;      foo (1 of 3)
26 ;;      foo 1 of 3
27 ;;      foo1 of 3
28
29 (defvar mime/gp:subject-start-regexp "[ \t]*\\(v[0-9]+i[0-9]+:[ \t]+\\)?")
30
31 (defvar mime/gp:subject-end-regexp
32   "\\([[(]?\\)\\([0-9]+\\)\\(/\\| [oO][fF] \\)\\([0-9]+\\)\\([])]?\\)[ \t]*$")
33
34 ;; display Article at the cursor in Subject buffer.
35 (defun mime/gp:display-article ()
36   (save-excursion   
37     (cond
38      ((eq target 'gnus4)
39       (let ((gnus-show-all-headers t))
40         (gnus-summary-display-article (gnus-summary-article-number))
41         ))
42      ((eq target 'mh-e)
43       (mh-show)
44       )
45      ((eq target 'vm)
46       (let ((vm-follow-summary-cursor t))
47         (vm-follow-summary-cursor)
48         (vm-select-folder-buffer)
49         (vm-check-for-killed-summary)
50         (vm-error-if-folder-empty)
51         (vm-display (current-buffer) t '(tm-vm/view-message)
52                     '(tm-vm/view-mesage reading-message))
53         (vm-widen-page)
54         (goto-char (point-max))
55         (widen)
56         (narrow-to-region (point) (vm-start-of (car vm-message-pointer)))
57         (goto-char (point-min))
58         ))
59      (t (error "Fatal. Unsupported mode")))))
60
61 (defun mime/decode-message/grab-partials (beg end cal)
62   (interactive)
63   (let* ((id (cdr (assoc "id" cal)))
64          (number (cdr (assoc "number" cal)))
65          (total (cdr (assoc "total" cal)))
66          (buffer (generate-new-buffer id))
67          (mother mime::article/preview-buffer)
68          target
69          subject-buf
70          (article-buf (buffer-name (current-buffer)))
71          (subject-id nil)
72          (part-num 1)
73          (part-missing nil))
74     (cond ((eq major-mode 'gnus-article-mode)
75            (setq subject-buf gnus-summary-buffer)
76            (setq target 'gnus4)
77            )
78           ((eq major-mode 'mh-show-mode)
79            (string-match "^show-\\(.+\\)$" article-buf)
80            (setq subject-buf
81                  (substring article-buf (match-beginning 1) (match-end 1)))
82            (setq target 'mh-e)
83            )
84           ((eq major-mode 'vm-mode)
85            (setq subject-buf vm-summary-buffer)
86            (setq target 'vm)
87            )
88           (t (error "%s is not supported. Sorry." major-mode)))
89     
90     (if (and (eq beg (point-min)) (eq end (point-max)))
91         (save-excursion
92           (goto-char (point-min))
93           (re-search-forward "^$")
94           (let ((delim (match-beginning 0)))
95             (goto-char (point-min))
96             (if (re-search-forward "^[Ss]ubject:.*$" delim t)
97                 (let ((tail (match-end 0)))
98                   (beginning-of-line)
99                   (re-search-forward (concat "^[Ss]ubject:" mime/gp:subject-start-regexp) tail t)
100                   (let ((start (point)))
101                     (if (and (re-search-forward mime/gp:subject-end-regexp tail t)
102                              (eq (string-to-int number)
103                                  (string-to-int (buffer-substring (match-beginning 2) (match-end 2))))
104                              (eq (string-to-int total)
105                                  (string-to-int (buffer-substring (match-beginning 4) (match-end 4)))))
106                         (setq subject-id (buffer-substring start (match-end 1)))
107                       (setq part-missing (string-to-int number)))))
108               (setq part-missing t))))
109       (setq part-missing t))
110
111     ;; if you can't parse the subject line, try simple decoding method
112     (if (or part-missing
113             (not (y-or-n-p "Merge partials?")))
114         (progn
115           (kill-buffer buffer)
116           (mime/decode-message/partial-region beg end cal))
117       (progn 
118         (set-buffer subject-buf)
119         (setq part-missing (mime/gp:part-missing-p subject-id (string-to-int total)))
120         (if part-missing
121             (progn
122               (kill-buffer buffer)
123               (error "Couldn't find part %d" part-missing)))
124         (save-excursion
125           (while (<= part-num (string-to-int total))
126             (goto-char (point-min))
127             (message "Grabbing part %d of %d" part-num (string-to-int total))
128             (re-search-forward
129                       (concat (regexp-quote subject-id) "0*"
130                               (int-to-string part-num)) nil t)
131             (mime/gp:display-article)
132             (save-excursion
133               (set-buffer article-buf)
134               (goto-char (point-min))
135               (re-search-forward "^$")
136               (let ((delimit (1+ (point))))
137                 (goto-char (point-min))
138                 (if (not
139                      (let ((params (cdr (mime/Content-Type))))
140                        (and (equal (assoc-value "id" params) id)
141                             (= (string-to-int (assoc-value "number" params))
142                                part-num)
143                             )))
144                     (progn
145                       (kill-buffer buffer)
146                       (error "Couldn't find part %d" part-num)))
147                 (if (< delimit (point-max))
148                     (append-to-buffer buffer delimit (point-max))
149                   )))
150             (setq part-num (+ part-num 1))))
151         (mime/gp:display-article)
152         (save-excursion
153           (set-buffer article-buf)
154           ;; (make-variable-buffer-local 'mime/content-list)
155           ;; (setq mime/content-list (mime/parse-contents))
156           (make-variable-buffer-local 'mime::article/content-info)
157           (setq mime::article/content-info (mime-viewer/parse))
158           )
159         (delete-other-windows)
160         (switch-to-buffer buffer)
161         (goto-char (point-min))
162         (setq major-mode 'mime/show-message-mode)
163         (mime/viewer-mode mother)
164         (pop-to-buffer (current-buffer))
165         ))))
166
167 ;; Check if all the parts are there
168 (defun mime/gp:part-missing-p (subject-string num-parts)
169   (save-excursion
170     (let ((part-num 1)
171           (cant-find nil))
172
173       (while (and (<= part-num num-parts) (not cant-find))
174         (goto-char (point-min))
175         ;; If the parts are numbered 01/10, then chop off the leading 0
176         (if (not (re-search-forward
177                   (concat (regexp-quote subject-id) "0*" 
178                           (int-to-string part-num))
179                   nil t))
180             (setq cant-find part-num)
181           (progn
182             (message "Found part %d of %d." part-num num-parts)
183             (setq part-num (+ part-num 1)))))
184       cant-find)))
185
186
187 ;;; @ set up
188 ;;;
189
190 (set-atype 'mime/content-decoding-condition
191            '((type . "message/partial")
192              (method . mime/decode-message/grab-partials)
193              (major-mode . gnus-article-mode)
194              ))
195
196 (set-atype 'mime/content-decoding-condition
197            '((type . "message/partial")
198              (method . mime/decode-message/grab-partials)
199              (major-mode . mh-show-mode)
200              ))
201
202 (set-atype 'mime/content-decoding-condition
203            '((type . "message/partial")
204              (method . mime/decode-message/grab-partials)
205              (major-mode . vm-mode)
206              ))
207
208 (provide 'tm-partial)