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