805b26cf877934fad612678b4f504e63a0604645
[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
8 ;; original file is 
9 ;;  gif.el written by Art Mellor @ Cayman Systems, Inc. 1991
10
11 (require 'tm-view)
12
13 ;; This regular expression controls what types of subject lines can be
14 ;; parsed. Currently handles lines like:
15 ;;      foo [1/3]
16 ;;      foo (1/3)
17 ;;      foo 1/3
18 ;;      foo [1 of 3]
19 ;;      foo (1 of 3)
20 ;;      foo 1 of 3
21 ;;      foo1 of 3
22
23 (defvar mime/gp:subject-start-regexp "[ \t]*\\(v[0-9]+i[0-9]+:[ \t]+\\)?")
24
25 (defvar mime/gp:subject-end-regexp "\\([[(]?\\)\\([0-9]+\\)\\(/\\| [oO][fF] \\)\\([0-9]+\\)\\([])]?\\)[ \t]*$")
26
27 ;; display Article at the cursor in Subject buffer.
28 (defun mime/gp:display-article ()
29   (save-excursion   
30     (cond
31      ((eq target 'gnus4)
32       (gnus-summary-display-article (gnus-summary-article-number)))
33      ((eq target 'mh-e)
34       (mh-show))
35      (t
36         (error "Fatal. Unsupported mode")))))
37    
38 (defun mime/decode-message/grab-partials (beg end cal)
39   (interactive)
40   (let* ((id (cdr (assoc "id" cal)))
41          (number (cdr (assoc "number" cal)))
42          (total (cdr (assoc "total" cal)))
43          (buffer (generate-new-buffer id))
44          (mother mime/preview-buffer)
45          target
46          subject-buf
47          (article-buf (buffer-name (current-buffer)))
48          (subject-id nil)
49          (part-num 1)
50          (part-missing nil))
51     (cond ((eq major-mode 'gnus-article-mode)
52            (progn
53              (setq subject-buf gnus-summary-buffer)
54              (setq target 'gnus4)))
55           ((eq major-mode 'mh-show-mode)
56            (progn
57              (string-match "^show-\\(.+\\)$" article-buf)
58              (setq subject-buf (substring article-buf (match-beginning 1) (match-end 1)))
59              (setq target 'mh-e)))
60           (t (error "%s is not supported. Sorry." major-mode)))
61     
62     (if (and (eq beg (point-min)) (eq end (point-max)))
63         (save-excursion
64           (goto-char (point-min))
65           (re-search-forward "^$")
66           (let ((delim (match-beginning 0)))
67             (goto-char (point-min))
68             (if (re-search-forward "^[Ss]ubject:.*$" delim t)
69                 (let ((tail (match-end 0)))
70                   (beginning-of-line)
71                   (re-search-forward (concat "^[Ss]ubject:" mime/gp:subject-start-regexp) tail t)
72                   (let ((start (point)))
73                     (if (and (re-search-forward mime/gp:subject-end-regexp tail t)
74                              (eq (string-to-int number)
75                                  (string-to-int (buffer-substring (match-beginning 2) (match-end 2))))
76                              (eq (string-to-int total)
77                                  (string-to-int (buffer-substring (match-beginning 4) (match-end 4)))))
78                         (setq subject-id (buffer-substring start (match-end 1)))
79                       (setq part-missing (string-to-int number)))))
80               (setq part-missing t))))
81       (setq part-missing t))
82
83     ;; if you can't parse the subject line, try simple decoding method
84     (if (or part-missing
85             (not (y-or-n-p "Merge partials?")))
86         (progn
87           (kill-buffer buffer)
88           (mime/decode-message/partial-region beg end cal))
89       (progn 
90         (set-buffer subject-buf)
91         (setq part-missing (mime/gp:part-missing-p subject-id (string-to-int total)))
92         (if part-missing
93             (progn
94               (kill-buffer buffer)
95               (error "Couldn't find part %d" part-missing)))
96         (save-excursion
97           (while (<= part-num (string-to-int total))
98             (goto-char (point-min))
99             (message "Grabbing part %d of %d" part-num (string-to-int total))
100             (re-search-forward
101                       (concat (regexp-quote subject-id) "0*"
102                               (int-to-string part-num)) nil t)
103             (mime/gp:display-article)
104             (save-excursion
105               (set-buffer article-buf)
106               (goto-char (point-min))
107               (re-search-forward "^$")
108               (let ((delimit (point)))
109                 (goto-char (point-min))
110                 (if (not
111                      (and
112                       (re-search-forward
113                        "^[Cc]ontent-[Tt]ype:[ \t]*message/partial;" delimit t)
114                       (re-search-forward
115                        (concat "[ \t]+id=[ \t]*\""
116                                (regexp-quote id) "\";") delimit)
117                       (re-search-forward
118                        (concat "[ \t]+number=[ \t]*"
119                                (int-to-string part-num) ";") delimit)))
120                     (progn
121                       (kill-buffer buffer)
122                       (error "Couldn't find part %d" part-num)))
123                 (append-to-buffer buffer (+ delimit 1) (point-max))))
124             (setq part-num (+ part-num 1))))
125         (mime/gp:display-article)
126         (save-excursion
127           (set-buffer article-buf)
128           (make-variable-buffer-local 'mime/content-list)
129           (setq mime/content-list (mime/parse-contents)))
130         (delete-other-windows)
131         (switch-to-buffer buffer)
132         (goto-char (point-min))
133         (setq major-mode 'mime/show-message-mode)
134         (mime/viewer-mode mother)
135         (pop-to-buffer (current-buffer))
136         ))))
137
138 ;; Check if all the parts are there
139 (defun mime/gp:part-missing-p (subject-string num-parts)
140   (save-excursion
141     (let ((part-num 1)
142           (cant-find nil))
143
144       (while (and (<= part-num num-parts) (not cant-find))
145         (goto-char (point-min))
146         ;; If the parts are numbered 01/10, then chop off the leading 0
147         (if (not (re-search-forward
148                   (concat (regexp-quote subject-id) "0*" 
149                           (int-to-string part-num))
150                   nil t))
151             (setq cant-find part-num)
152           (progn
153             (message "Found part %d of %d." part-num num-parts)
154             (setq part-num (+ part-num 1)))))
155       cant-find)))
156
157
158 (set-atype 'mime/content-decoding-condition
159            '((type . "message/partial")
160              (method . mime/decode-message/grab-partials)
161              ))
162
163 (provide 'tm-partial)