Add Eric Marsden's patch.
[gnus] / lisp / webmail.el
1 ;;; webmail.el --- interfacing with web mail
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: hotmail
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Note: You need to have `url' and `w3' installed for this backend to
27 ;; work. `w3' must be 4.0pre46+one-line-cookie patch or standalone
28 ;; `url'.
29
30 ;; Todo: To support more web mail servers.
31
32 ;; Known bugs: 
33 ;; 1. Net@ddress may corrupt `X-Face'.
34
35 ;; Warning:
36 ;; Webmail is an experimental function, which means NO WARRANTY.
37
38 ;;; Code:
39
40 (eval-when-compile (require 'cl))
41
42 (require 'nnoo)
43 (require 'message)
44 (require 'gnus-util)
45 (require 'gnus)
46 (require 'nnmail)
47 (require 'mm-util)
48 (require 'mml)
49 (eval-when-compile
50   (ignore-errors
51     (require 'w3)
52     (require 'url)
53     (require 'url-cookie)
54     (require 'w3-forms)
55     (require 'nnweb)))
56 ;; Report failure to find w3 at load time if appropriate.
57 (eval '(progn
58          (require 'w3)
59          (require 'url)
60          (require 'url-cookie)
61          (require 'w3-forms)
62          (require 'nnweb)))
63
64 ;;;
65
66 (defvar webmail-type-definition
67   '((hotmail
68      ;; Hotmail hate other HTTP user agents and use one line cookie
69      (paranoid agent cookie post)
70      (address . "www.hotmail.com")
71      (open-url "http://www.hotmail.com/")
72      (open-snarf . webmail-hotmail-open)
73      ;; W3 hate redirect POST
74      (login-url
75       "http://%s/cgi-bin/dologin?login=%s&passwd=%s&enter=Sign+in&sec=no&curmbox=ACTIVE&_lang=&js=yes&id=2&tw=-10000&beta="
76       webmail-aux user password)
77      (list-snarf . webmail-hotmail-list)
78      (article-snarf . webmail-hotmail-article)
79      (trash-url 
80       "%s&login=%s&f=33792&curmbox=ACTIVE&_lang=&js=&foo=inbox&page=&%s=on&Move+To.x=Move+To&tobox=trAsH" 
81       webmail-aux user id))
82     (yahoo
83      (paranoid cookie post)
84      (address . "mail.yahoo.com")
85      (open-url "http://mail.yahoo.com/")
86      (open-snarf . webmail-yahoo-open)
87      (login-url;; yahoo will not accept GET
88       content 
89       ("%s" webmail-aux)
90       ".tries=1&.src=ym&.last=&promo=&lg=us&.intl=us&.bypass=&.chkP=Y&.done=http%%253a%%2F%%2Fedit.yahoo.com%%2Fconfig%%2Fmail%%253f.intl%%3D&login=%s&passwd=%s" 
91       user password)
92      (login-snarf . webmail-yahoo-login)
93      (list-url "%s&rb=Inbox&YN=1" webmail-aux)
94      (list-snarf . webmail-yahoo-list)
95      (article-snarf . webmail-yahoo-article)
96      (trash-url 
97       "%s/ym/us/ShowFolder?YY=52107&inc=50&order=down&sort=date&pos=0&box=Inbox&DEL=Delete&destBox=&Mid=%s&destBox2="
98       webmail-aux id))
99     (netaddress
100      (paranoid cookie post)
101      (address . "www.netaddress.com")
102      (open-url "http://www.netaddress.com/")
103      (open-snarf . webmail-netaddress-open)
104      (login-url;; yahoo will not accept GET
105       content 
106       ("%s" webmail-aux)
107       "LoginState=2&SuccessfulLogin=%%2Ftpl&NewServerName=www.netaddress.com&JavaScript=JavaScript1.2&DomainID=4&NA31site=classic.netaddress.com&NA31port=80&UserID=%s&passwd=%s" 
108       user password)
109      (login-snarf . webmail-netaddress-login)
110      (list-url 
111       "http://www.netaddress.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
112       webmail-session)
113      (list-snarf . webmail-netaddress-list)
114      (article-snarf . webmail-netaddress-article)
115      (trash-url 
116       "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
117       webmail-session id))))
118
119 (defvar webmail-variables
120   '(address article-snarf article-url list-snarf list-url 
121             login-url login-snarf open-url open-snarf site articles
122             post-process paranoid trash-url))
123
124 (defconst webmail-version "webmail 1.0")
125
126 (defvar webmail-newmail-only nil
127   "Only fetch new mails.")
128
129 (defvar webmail-move-to-trash-can t
130   "Move mail to trash can after fetch it.")
131
132 ;;; Internal variables
133
134 (defvar webmail-address nil)
135 (defvar webmail-paranoid nil)
136 (defvar webmail-aux nil)
137 (defvar webmail-session nil)
138 (defvar webmail-article-snarf nil)
139 (defvar webmail-article-url nil)
140 (defvar webmail-list-snarf nil)
141 (defvar webmail-list-url nil)
142 (defvar webmail-login-url nil)
143 (defvar webmail-login-snarf nil)
144 (defvar webmail-open-snarf nil)
145 (defvar webmail-open-url nil)
146 (defvar webmail-trash-url nil)
147 (defvar webmail-articles nil)
148 (defvar webmail-post-process nil)
149
150 (defvar webmail-buffer nil)
151 (defvar webmail-buffer-list nil)
152 ;;; Interface functions
153
154 (defun webmail-setdefault (type)
155   (let ((type-def (cdr (assq type webmail-type-definition)))
156         (vars webmail-variables)
157         pair)
158     (dolist (var vars)
159       (if (setq pair (assq var type-def))
160           (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
161         (set (intern (concat "webmail-" (symbol-name var))) nil)))))
162
163 (defun webmail-encode-www-form-urlencoded (pairs)
164   "Return PAIRS encoded for forms."
165   (mapconcat
166    (function
167     (lambda (data)
168       (concat (w3-form-encode-xwfu (car data)) "="
169               (w3-form-encode-xwfu (cdr data)))))
170    pairs "&"))
171
172 (defun webmail-fetch-simple (url content)
173   (let ((url-request-data content)
174         (url-request-method "POST")
175         (url-request-extra-headers
176          '(("Content-type" . "application/x-www-form-urlencoded"))))
177     (nnweb-insert url))
178   t)
179
180 (defun webmail-fetch-form (url pairs)
181   (let ((url-request-data (webmail-encode-www-form-urlencoded pairs))
182         (url-request-method "POST")
183         (url-request-extra-headers
184          '(("Content-type" . "application/x-www-form-urlencoded"))))
185     (nnweb-insert url))
186   t)
187
188 (defun webmail-eval (expr)
189   (cond
190    ((consp expr)
191     (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
192    ((symbolp expr)
193     (eval expr))
194    (t
195     expr)))
196
197 (defun webmail-url (xurl)
198   (mm-with-unibyte-current-buffer
199     (cond 
200      ((eq (car xurl) 'content)
201       (pop xurl)
202       (webmail-fetch-simple (if (stringp (car xurl))
203                                 (car xurl)
204                               (apply 'format (webmail-eval (car xurl))))
205                             (apply 'format (webmail-eval (cdr xurl)))))
206      ((eq (car xurl) 'post)
207       (pop xurl)
208       (webmail-fetch-form (car xurl) (webmail-eval (cdr xurl))))
209      (t
210       (nnweb-insert (apply 'format (webmail-eval xurl)))))))
211   
212 (defun webmail-init ()
213   "Initialize buffers and such."
214   (if (gnus-buffer-live-p webmail-buffer)
215       (set-buffer webmail-buffer)
216     (setq webmail-buffer
217           (nnheader-set-temp-buffer " *webmail*"))))
218
219 (defvar url-package-name)
220 (defvar url-package-version)
221 (defvar url-cookie-multiple-line)
222 (defvar url-confirmation-func)
223
224 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
225 ;;
226 ;; Netscape uses "GET" as redirect method when orignal method is POST
227 ;; and status is 302, .i.e no security risks by default without
228 ;; confirmation.
229 ;;
230 ;; Some web servers (at least Apache used by yahoo) return status 302
231 ;; instead of 303, though they mean 303.
232
233 (defun webmail-url-confirmation-func (prompt)
234   (cond 
235    ((equal prompt (concat "Honor redirection with non-GET method "
236                           "(possible security risks)? "))
237     nil)
238    ((equal prompt "Continue (with method of GET)? ")
239     t)
240    (t (error prompt))))
241
242 (defun webmail-refresh-redirect ()
243   "Redirect refresh url in META."
244   (goto-char (point-min))
245   (while (re-search-forward "HTTP-EQUIV=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
246                             nil t)
247     (let ((url (match-string 1)))
248       (erase-buffer)
249       (nnweb-insert url))
250     (goto-char (point-min))))
251
252 (defun webmail-fetch (file subtype user password)
253   (save-excursion
254     (webmail-setdefault subtype)
255     (let ((url-package-name (if (memq 'agent webmail-paranoid)
256                                 "Mozilla"
257                               url-package-name))
258           (url-package-version (if (memq 'agent webmail-paranoid)
259                                    "4.0"
260                                  url-package-version))
261           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
262                                         nil
263                                       url-cookie-multiple-line))
264           (url-confirmation-func (if (memq 'post webmail-paranoid)
265                                      'webmail-url-confirmation-func
266                                    url-confirmation-func))
267           url-cookie-storage url-cookie-secure-storage
268           url-cookie-confirmation
269           item id (n 0))
270       (webmail-init)
271       (setq webmail-articles nil)
272       (when webmail-open-url 
273         (erase-buffer)
274         (webmail-url webmail-open-url))
275       (if webmail-open-snarf (funcall webmail-open-snarf))
276       (when webmail-login-url 
277         (erase-buffer)
278         (webmail-url webmail-login-url))
279       (if webmail-login-snarf 
280           (funcall webmail-login-snarf))
281       (when webmail-list-url 
282         (erase-buffer)
283         (webmail-url webmail-list-url))
284       (if webmail-list-snarf 
285           (funcall webmail-list-snarf))
286       (while (setq item (pop webmail-articles))
287         (message "Fetching mail #%d..." (setq n (1+ n)))
288         (erase-buffer)
289         (nnweb-insert (cdr item))
290         (setq id (car item))
291         (if webmail-article-snarf 
292             (funcall webmail-article-snarf file id))
293         (when (and webmail-trash-url webmail-move-to-trash-can)
294           (message "Move mail #%d to trash can..." n)
295           (condition-case err
296               (progn
297                 (webmail-url webmail-trash-url)
298                 (let (buf)
299                   (while (setq buf (pop webmail-buffer-list))
300                     (kill-buffer buf))))
301             (error 
302              (let (buf)
303                (while (setq buf (pop webmail-buffer-list))
304                  (kill-buffer buf)))
305              (error err))))))
306     (if webmail-post-process
307         (funcall webmail-post-process))))
308
309 ;;; hotmail
310
311 (defun webmail-hotmail-open ()
312   (goto-char (point-min))
313   (if (re-search-forward 
314        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
315       (setq webmail-aux (match-string 1))
316     (error "Can't find login url (open@1)")))
317
318 (defun webmail-hotmail-list ()
319   (let (site url newp)
320     (goto-char (point-min))
321     (if (re-search-forward "[0-9]+ messages, [0-9]+ new") 
322         (message "Found %s" (match-string 0)))
323     (goto-char (point-min))
324     (if (re-search-forward 
325          "action=\"https?://\\([^/]+\\)/cgi-bin/HoTMaiL" nil t)
326         (setq site (match-string 1))
327       (error "Can't find server url (list@1)"))
328     (goto-char (point-min))
329     (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
330         (setq webmail-aux 
331               (concat "http://" site "/cgi-bin/HoTMaiL?disk=" 
332                       (match-string 1)))
333       (error "Can't find disk (list@2)"))
334     (goto-char (point-max))
335     (while (re-search-backward 
336             "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\"" 
337             nil t)
338       (if (setq url (match-string 1))
339           (progn
340             (if (or newp (not webmail-newmail-only))
341                 (let (id)
342                   (if (string-match "msg=\\([^&]+\\)" url)
343                       (setq id (match-string 1 url)))
344                   (push (cons id (concat "http://" site url)) 
345                         webmail-articles)))
346             (setq newp nil))
347         (setq newp t)))))
348
349 (defun webmail-hotmail-article (file id)
350   (let (p attachment count mime)
351     (save-restriction
352       (goto-char (point-min))
353       (if (not (search-forward "<DIV>" nil t))
354           (error "Can't find start label (article@1)"))
355       (narrow-to-region (point-min) (match-beginning 0))
356       (if (not (search-backward "<table" nil t 2))
357           (error "Can't find start label (article@1.1)"))
358       (delete-region (point-min) (match-beginning 0)) 
359       (while (search-forward "<a href=" nil t)
360         (setq p (match-beginning 0))
361         (search-forward "</a>" nil t)
362         (delete-region p (match-end 0)))
363       (nnweb-remove-markup)
364       (nnweb-decode-entities)
365       (goto-char (point-min))
366       (delete-blank-lines)
367       (goto-char (point-min))
368       (when (search-forward "\n\n" nil t)
369         (backward-char)
370         (delete-region (point) (point-max)))
371       (goto-char (point-max))
372       (widen)
373       (insert "\n")
374       (setq p (point))
375       (while (re-search-forward "<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" nil t)
376         (if (setq attachment (match-string 1))
377             (let ((filename (match-string 2))
378                   bufname);; Attachment
379               (delete-region p (match-end 0))
380               (save-excursion
381                 (set-buffer (generate-new-buffer " *webmail-att*"))
382                 (nnweb-insert attachment)
383                 (push (current-buffer) webmail-buffer-list)
384                 (setq bufname (buffer-name)))
385               (setq mime t)
386               (insert "<#part type=" 
387                       (or (and filename
388                                (string-match "\\.[^\\.]+$" filename)
389                                (mailcap-extension-to-mime
390                                 (match-string 0 filename)))
391                           "application/octet-stream"))
392               (insert " buffer=\"" bufname "\"")
393               (insert " filename=\"" filename "\"")
394               (insert " disposition=\"inline\"")
395               (insert "><#/part>\n")
396               (setq p (point)))
397           (delete-region p (match-end 0))
398           (setq count 1)
399           (while (and (> count 0) 
400                       (re-search-forward "</div>\\|\\(<div>\\)" nil t))
401             (if (match-string 1)
402                 (setq count (1+ count))
403               (if (= (setq count (1- count)) 0)
404                   (delete-region (match-beginning 0)
405                                  (match-end 0)))))
406           (narrow-to-region p (point))
407           (goto-char (point-min))
408           (cond 
409            ((looking-at "<pre>")
410             (goto-char (match-end 0))
411             (if (looking-at "$") (forward-char))
412             (delete-region (point-min) (point))
413             (nnweb-remove-markup)
414             (nnweb-decode-entities)
415             nil)
416            (t
417             (setq mime t)
418             (insert "<#part type=\"text/html\" disposition=inline>")
419             (goto-char (point-max))
420             (insert "<#/part>")))
421           (goto-char (point-max))
422           (setq p (point))
423           (widen)))
424       (delete-region p (point-max))
425       (goto-char (point-min))
426       ;; Some blank line to seperate mails.
427       (insert "\n\nFrom nobody " (current-time-string) "\n")
428       (if id
429           (insert "Message-ID: <" id "@hotmail.com>\n"))
430       (unless (looking-at "$") 
431         (search-forward "\n\n" nil t)
432         (forward-line -1))
433       (narrow-to-region (point) (point-max))
434       (if mime
435           (insert "MIME-Version: 1.0\n"
436                   (prog1
437                       (mml-generate-mime)
438                     (delete-region (point-min) (point-max)))))
439       (goto-char (point-min))
440       (widen)
441       (let (case-fold-search)
442         (while (re-search-forward "^From " nil t)
443           (beginning-of-line)
444           (insert ">"))))
445     (mm-append-to-file (point-min) (point-max) file)))
446
447 ;;; yahoo
448
449 (defun webmail-yahoo-open ()
450   (goto-char (point-min))
451   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
452       (setq webmail-aux (match-string 1))
453     (error "Can't find login url (open@1)")))
454
455 (defun webmail-yahoo-login ()
456   (goto-char (point-min))
457   (if (re-search-forward "http://[a-zA-Z][0-9]\\.mail\\.yahoo\\.com/" nil t)
458       (setq webmail-aux (match-string 0))
459     (error "Can't find login url (login@1)"))
460   (if (re-search-forward "YY=[0-9]+" nil t)
461       (setq webmail-aux (concat webmail-aux "ym/us/ShowFolder?"
462                                 (match-string 0)))
463     (error "Can't find login url (login@2)")))
464
465 (defun webmail-yahoo-list ()
466   (let (url (newp t) (tofetch 0))
467     (goto-char (point-min))
468     (when (re-search-forward 
469            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
470       ;;(setq listed (match-string 1))
471       (message "Found %s mail(s)" (match-string 2)))
472     (if (string-match "http://[^/]+" webmail-aux)
473         (setq webmail-aux (match-string 0 webmail-aux))
474       (error "Can't find server url (list@1)"))
475     (goto-char (point-min))
476     (while (re-search-forward 
477             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/us/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
478             nil t)
479       (if (setq url (match-string 1))
480           (progn
481             (when (or newp (not webmail-newmail-only))
482               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
483                     webmail-articles)
484               (setq tofetch (1+ tofetch)))
485             (setq newp t))
486         (setq newp nil)))
487     (message "Fetching %d mail(s)" tofetch)))
488
489 (defun webmail-yahoo-article (file id)
490   (let (p attachment)
491     (save-restriction
492       (goto-char (point-min))
493       (if (not (search-forward "value=\"Done\"" nil t))
494           (error "Can't find start label (article@1)"))
495       (if (not (search-forward "<table" nil t))
496           (error "Can't find start label (article@2)"))
497       (delete-region (point-min) (match-beginning 0)) 
498       (if (not (search-forward "</table>" nil t))
499           (error "Can't find start label (article@3)"))
500       (narrow-to-region (point-min) (match-end 0))
501       (while (search-forward "<a href=" nil t)
502         (setq p (match-beginning 0))
503         (search-forward "</a>" nil t)
504         (delete-region p (match-end 0)))
505       (nnweb-remove-markup)
506       (nnweb-decode-entities)
507       (goto-char (point-min))
508       (delete-blank-lines)
509       (goto-char (point-max))
510       (widen)
511       (insert "\n")
512       (setq p (point))
513       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
514         (setq attachment (match-string 0))
515         (let (bufname ct ctl cd description)
516           (if (not (search-forward "<table" nil t))
517               (error "Can't find start label (article@4)"))
518           (delete-region p (match-beginning 0))
519           (if (not (search-forward "</table>" nil t))
520               (error "Can't find start label (article@5)"))
521           (narrow-to-region p (match-end 0))
522           (nnweb-remove-markup)
523           (nnweb-decode-entities)
524           (goto-char (point-min))
525           (delete-blank-lines)
526           (setq ct (mail-fetch-field "content-type")
527                 ctl (ignore-errors (mail-header-parse-content-type ct))
528                 ;;cte (mail-fetch-field "content-transfer-encoding")
529                 cd (mail-fetch-field "content-disposition")
530                 description (mail-fetch-field "content-description")
531                 id (mail-fetch-field "content-id"))
532           (delete-region (point-min) (point-max))
533           (widen)
534           (save-excursion
535             (set-buffer (generate-new-buffer " *webmail-att*"))
536             (nnweb-insert (concat webmail-aux attachment))
537             (push (current-buffer) webmail-buffer-list)
538             (setq bufname (buffer-name)))
539           (insert "<#part")
540           (if (and ctl (not (equal (car ctl) "text/")))
541               (insert " type=\"" (car ctl) "\""))
542           (insert " buffer=\"" bufname "\"")
543           (if cd
544               (insert " disposition=\"" cd "\""))
545           (if description
546               (insert " description=\"" description "\""))
547           (insert "><#/part>\n")
548           (setq p (point))))
549       (delete-region p (point-max))
550       (goto-char (point-min))
551       ;; Some blank line to seperate mails.
552       (insert "\n\nFrom nobody " (current-time-string) "\n")
553       (if id
554           (insert "Message-ID: <" id "@yahoo.com>\n"))
555       (unless (looking-at "$") 
556         (search-forward "\n\n" nil t)
557         (forward-line -1))
558       (narrow-to-region (point) (point-max))
559       (insert "MIME-Version: 1.0\n"
560               (prog1
561                   (mml-generate-mime)
562                 (delete-region (point-min) (point-max))))
563       (goto-char (point-min))
564       (widen)
565       (let (case-fold-search)
566         (while (re-search-forward "^From " nil t)
567           (beginning-of-line)
568           (insert ">"))))
569     (mm-append-to-file (point-min) (point-max) file)))
570
571 ;;; netaddress
572
573 (defun webmail-netaddress-open ()
574   (goto-char (point-min))
575   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
576       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
577     (error "Can't find login url (open@1)")))
578
579 (defun webmail-netaddress-login ()
580   (webmail-refresh-redirect)
581   (goto-char (point-min))
582   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
583       (setq webmail-session (match-string 1))
584     (error "Can't find login url (login@1)")))
585
586 (defun webmail-netaddress-list ()
587   (let (item id)
588     (goto-char (point-min))
589     (when (re-search-forward 
590            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
591       (message "Found %s mail(s), %s unread" 
592                (match-string 2) (match-string 1)))
593     (goto-char (point-min))
594     (while (re-search-forward 
595             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
596       (if (setq id (match-string 2))
597           (setq item 
598                 (cons id 
599                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
600                               (car webmail-open-url)
601                               webmail-session id)))
602         (if (or (not webmail-newmail-only)
603                 (equal (match-string 1) "True"))
604             (push item webmail-articles))))))
605
606 (defun webmail-netaddress-single-part ()
607   (goto-char (point-min))
608   (cond 
609    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
610     ;; text/plain
611     (replace-match "")
612     (while (re-search-forward "[\t\040\r\n]+" nil t)
613       (replace-match " "))
614     (goto-char (point-min))
615     (while (re-search-forward "<br>" nil t)
616       (replace-match "\n"))
617     (nnweb-remove-markup)
618     (nnweb-decode-entities) 
619     nil)
620    (t
621     (insert "<#part type=\"text/html\" disposition=inline>")
622     (goto-char (point-max))
623     (insert "<#/part>")
624     t)))
625
626 (defun webmail-netaddress-article (file id)
627   (let (p p1 attachment count mime type)
628     (save-restriction
629       (goto-char (point-min))
630       (if (not (search-forward "Trash" nil t))
631           (error "Can't find start label (article@1)"))
632       (if (not (search-forward "<form>" nil t))
633           (error "Can't find start label (article@2)"))
634       (delete-region (point-min) (match-beginning 0)) 
635       (if (not (search-forward "</form>" nil t))
636           (error "Can't find start label (article@3)"))
637       (narrow-to-region (point-min) (match-end 0))
638       (goto-char (point-min))
639       (while (re-search-forward "[\040\t\r\n]+" nil t)
640         (replace-match " "))
641       (goto-char (point-min))
642       (while (search-forward "<b>" nil t)
643         (replace-match "\n"))
644       (nnweb-remove-markup)
645       (nnweb-decode-entities)
646       (goto-char (point-min))
647       (delete-blank-lines)
648       (goto-char (point-min))
649       (while (re-search-forward "^\040+\\|\040+$" nil t)
650         (replace-match ""))
651       (goto-char (point-min))
652       (while (re-search-forward "\040+" nil t)
653         (replace-match " "))
654       (goto-char (point-max))
655       (widen)
656       (insert "\n\n")
657       (setq p (point))
658       (unless (search-forward "<!-- Data -->" nil t)
659         (error "Can't find start label (article@4)"))
660       (forward-line 14)
661       (delete-region p (point))
662       (goto-char (point-max))
663       (unless (re-search-backward 
664                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
665         (error "Can't find end label (article@5)"))
666       (delete-region (point) (point-max))
667       (goto-char p)
668       (while (search-forward
669               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
670               nil t 2)
671         (setq mime t)
672         (unless (search-forward "</TABLE>" nil t)
673           (error "Can't find end label (article@6)"))
674         (setq p1 (point))
675         (if (search-backward "<IMG " p t)
676             (progn
677               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
678                 (error "Can't find tag (article@7)"))
679               (setq attachment (match-string 1))
680               (setq type (match-string 2))
681               (unless (search-forward "</TABLE>" nil t)
682                 (error "Can't find end label (article@8)"))
683               (delete-region p (point))
684               (let (bufname);; Attachment
685                 (save-excursion
686                   (set-buffer (generate-new-buffer " *webmail-att*"))
687                   (nnweb-insert (concat (car webmail-open-url) attachment))
688                   (push (current-buffer) webmail-buffer-list)
689                   (setq bufname (buffer-name)))
690                 (insert "<#part type=" type)
691                 (insert " buffer=\"" bufname "\"")
692                 (insert " disposition=\"inline\"")
693                 (insert "><#/part>\n")
694                 (setq p (point))))
695           (delete-region p p1)
696           (narrow-to-region 
697            p
698            (if (search-forward 
699                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
700                 nil t)
701                (match-beginning 0)
702              (point-max)))
703           (webmail-netaddress-single-part)
704           (goto-char (point-max))
705           (setq p (point))
706           (widen)))
707       (unless mime
708         (narrow-to-region p (point-max))
709         (setq mime (webmail-netaddress-single-part))
710         (widen))
711       (goto-char (point-min))
712       ;; Some blank line to seperate mails.
713       (insert "\n\nFrom nobody " (current-time-string) "\n")
714       (if id
715           (insert "Message-ID: <" id "@usa.net>\n"))
716       (unless (looking-at "$") 
717         (search-forward "\n\n" nil t)
718         (forward-line -1))
719       (when mime
720         (narrow-to-region (point-min) (point))
721         (goto-char (point-min))
722         (while (not (eobp))
723           (if (looking-at "MIME-Version\\|Content-Type")
724               (delete-region (point) 
725                              (progn
726                                (forward-line 1)
727                                (if (re-search-forward "^[^ \t]" nil t)
728                                    (goto-char (match-beginning 0))
729                                  (point-max))))
730             (forward-line 1)))
731         (goto-char (point-max))
732         (widen)
733         (narrow-to-region (point) (point-max))
734         (insert "MIME-Version: 1.0\n"
735                 (prog1
736                     (mml-generate-mime)
737                   (delete-region (point-min) (point-max))))
738         (goto-char (point-min))
739         (widen))
740       (let (case-fold-search)
741         (while (re-search-forward "^From " nil t)
742           (beginning-of-line)
743           (insert ">"))))
744     (mm-append-to-file (point-min) (point-max) file)))
745
746 (provide 'webmail)
747
748 ;;; webmail.el ends here