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