Require url-cookie.
[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-decode-entities ()
213   (goto-char (point-min))
214   (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
215     (replace-match (char-to-string 
216                     (if (eq (aref (match-string 1) 0) ?\#)
217                         (string-to-number (substring (match-string 1) 1))
218                       (or (cdr (assq (intern (match-string 1))
219                                      w3-html-entities))
220                           ?#)))
221                    t t)))
222
223 (defun webmail-decode-entities-string (str)
224   (with-temp-buffer
225     (insert str)
226     (webmail-decode-entities)
227     (buffer-substring (point-min) (point-max))))
228
229 (defun webmail-remove-markup ()
230   (goto-char (point-min))
231   (while (search-forward "<!--" nil t)
232     (delete-region (match-beginning 0)
233                    (or (search-forward "-->" nil t)
234                        (point-max))))
235   (goto-char (point-min))
236   (while (re-search-forward "<[^>]+>" nil t)
237     (replace-match "" t t)))
238
239 (defun webmail-init ()
240   "Initialize buffers and such."
241   (if (gnus-buffer-live-p webmail-buffer)
242       (set-buffer webmail-buffer)
243     (setq webmail-buffer
244           (nnheader-set-temp-buffer " *webmail*"))))
245
246 (defvar url-package-name)
247 (defvar url-package-version)
248 (defvar url-cookie-multiple-line)
249 (defvar url-confirmation-func)
250
251 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
252 ;;
253 ;; Netscape uses "GET" as redirect method when orignal method is POST
254 ;; and status is 302, .i.e no security risks by default without
255 ;; confirmation.
256 ;;
257 ;; Some web servers (at least Apache used by yahoo) return status 302
258 ;; instead of 303, though they mean 303.
259
260 (defun webmail-url-confirmation-func (prompt)
261   (cond 
262    ((equal prompt (concat "Honor redirection with non-GET method "
263                           "(possible security risks)? "))
264     nil)
265    ((equal prompt "Continue (with method of GET)? ")
266     t)
267    (t (error prompt))))
268
269 (defun webmail-refresh-redirect ()
270   "Redirect refresh url in META."
271   (goto-char (point-min))
272   (while (re-search-forward "HTTP-EQUIV=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
273                             nil t)
274     (let ((url (match-string 1)))
275       (erase-buffer)
276       (nnweb-insert url))
277     (goto-char (point-min))))
278
279 (defun webmail-fetch (file subtype user password)
280   (save-excursion
281     (webmail-setdefault subtype)
282     (let ((url-package-name (if (memq 'agent webmail-paranoid)
283                                 "Mozilla"
284                               url-package-name))
285           (url-package-version (if (memq 'agent webmail-paranoid)
286                                    "4.0"
287                                  url-package-version))
288           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
289                                         nil
290                                       url-cookie-multiple-line))
291           (url-confirmation-func (if (memq 'post webmail-paranoid)
292                                      'webmail-url-confirmation-func
293                                    url-confirmation-func))
294           url-cookie-storage url-cookie-secure-storage
295           url-cookie-confirmation
296           item id (n 0))
297       (webmail-init)
298       (setq webmail-articles nil)
299       (when webmail-open-url 
300         (erase-buffer)
301         (webmail-url webmail-open-url))
302       (if webmail-open-snarf (funcall webmail-open-snarf))
303       (when webmail-login-url 
304         (erase-buffer)
305         (webmail-url webmail-login-url))
306       (if webmail-login-snarf 
307           (funcall webmail-login-snarf))
308       (when webmail-list-url 
309         (erase-buffer)
310         (webmail-url webmail-list-url))
311       (if webmail-list-snarf 
312           (funcall webmail-list-snarf))
313       (while (setq item (pop webmail-articles))
314         (message "Fetching mail #%d..." (setq n (1+ n)))
315         (erase-buffer)
316         (nnweb-insert (cdr item))
317         (setq id (car item))
318         (if webmail-article-snarf 
319             (funcall webmail-article-snarf file id))
320         (when (and webmail-trash-url webmail-move-to-trash-can)
321           (message "Move mail #%d to trash can..." n)
322           (condition-case err
323               (progn
324                 (webmail-url webmail-trash-url)
325                 (let (buf)
326                   (while (setq buf (pop webmail-buffer-list))
327                     (kill-buffer buf))))
328             (error 
329              (let (buf)
330                (while (setq buf (pop webmail-buffer-list))
331                  (kill-buffer buf)))
332              (error err))))))
333     (if webmail-post-process
334         (funcall webmail-post-process))))
335
336 ;;; hotmail
337
338 (defun webmail-hotmail-open ()
339   (goto-char (point-min))
340   (if (re-search-forward 
341        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
342       (setq webmail-aux (match-string 1))
343     (error "Can't find login url (open@1)")))
344
345 (defun webmail-hotmail-list ()
346   (let (site url newp)
347     (goto-char (point-min))
348     (if (re-search-forward "[0-9]+ messages, [0-9]+ new") 
349         (message "Found %s" (match-string 0)))
350     (goto-char (point-min))
351     (if (re-search-forward 
352          "action=\"https?://\\([^/]+\\)/cgi-bin/HoTMaiL" nil t)
353         (setq site (match-string 1))
354       (error "Can't find server url (list@1)"))
355     (goto-char (point-min))
356     (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
357         (setq webmail-aux 
358               (concat "http://" site "/cgi-bin/HoTMaiL?disk=" 
359                       (match-string 1)))
360       (error "Can't find disk (list@2)"))
361     (goto-char (point-max))
362     (while (re-search-backward 
363             "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\"" 
364             nil t)
365       (if (setq url (match-string 1))
366           (progn
367             (if (or newp (not webmail-newmail-only))
368                 (let (id)
369                   (if (string-match "msg=\\([^&]+\\)" url)
370                       (setq id (match-string 1 url)))
371                   (push (cons id (concat "http://" site url)) 
372                         webmail-articles)))
373             (setq newp nil))
374         (setq newp t)))))
375
376 (defun webmail-hotmail-article (file id)
377   (let (p attachment count mime)
378     (save-restriction
379       (goto-char (point-min))
380       (if (not (search-forward "<DIV>" nil t))
381           (error "Can't find start label (article@1)"))
382       (narrow-to-region (point-min) (match-beginning 0))
383       (if (not (search-backward "<table" nil t 2))
384           (error "Can't find start label (article@1.1)"))
385       (delete-region (point-min) (match-beginning 0)) 
386       (while (search-forward "<a href=" nil t)
387         (setq p (match-beginning 0))
388         (search-forward "</a>" nil t)
389         (delete-region p (match-end 0)))
390       (webmail-remove-markup)
391       (webmail-decode-entities)
392       (goto-char (point-min))
393       (delete-blank-lines)
394       (goto-char (point-min))
395       (when (search-forward "\n\n" nil t)
396         (backward-char)
397         (delete-region (point) (point-max)))
398       (goto-char (point-max))
399       (widen)
400       (insert "\n")
401       (setq p (point))
402       (while (re-search-forward "<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" nil t)
403         (if (setq attachment (match-string 1))
404             (let ((filename (match-string 2))
405                   bufname);; Attachment
406               (delete-region p (match-end 0))
407               (save-excursion
408                 (set-buffer (generate-new-buffer " *webmail-att*"))
409                 (nnweb-insert attachment)
410                 (push (current-buffer) webmail-buffer-list)
411                 (setq bufname (buffer-name)))
412               (setq mime t)
413               (insert "<#part type=" 
414                       (or (and filename
415                                (string-match "\\.[^\\.]+$" filename)
416                                (mailcap-extension-to-mime
417                                 (match-string 0 filename)))
418                           "application/octet-stream"))
419               (insert " buffer=\"" bufname "\"")
420               (insert " filename=\"" filename "\"")
421               (insert " disposition=\"inline\"")
422               (insert "><#/part>\n")
423               (setq p (point)))
424           (delete-region p (match-end 0))
425           (setq count 1)
426           (while (and (> count 0) 
427                       (re-search-forward "</div>\\|\\(<div>\\)" nil t))
428             (if (match-string 1)
429                 (setq count (1+ count))
430               (if (= (setq count (1- count)) 0)
431                   (delete-region (match-beginning 0)
432                                  (match-end 0)))))
433           (narrow-to-region p (point))
434           (goto-char (point-min))
435           (cond 
436            ((looking-at "<pre>")
437             (goto-char (match-end 0))
438             (if (looking-at "$") (forward-char))
439             (delete-region (point-min) (point))
440             (webmail-remove-markup)
441             (webmail-decode-entities)
442             nil)
443            (t
444             (setq mime t)
445             (insert "<#part type=\"text/html\" disposition=inline>")
446             (goto-char (point-max))
447             (insert "<#/part>")))
448           (goto-char (point-max))
449           (setq p (point))
450           (widen)))
451       (delete-region p (point-max))
452       (goto-char (point-min))
453       ;; Some blank line to seperate mails.
454       (insert "\n\nFrom nobody " (current-time-string) "\n")
455       (if id
456           (insert "Message-ID: <" id "@hotmail.com>\n"))
457       (unless (looking-at "$") 
458         (search-forward "\n\n" nil t)
459         (forward-line -1))
460       (narrow-to-region (point) (point-max))
461       (if mime
462           (insert "MIME-Version: 1.0\n"
463                   (prog1
464                       (mml-generate-mime)
465                     (delete-region (point-min) (point-max)))))
466       (goto-char (point-min))
467       (widen)
468       (let (case-fold-search)
469         (while (re-search-forward "^From " nil t)
470           (beginning-of-line)
471           (insert ">"))))
472     (mm-append-to-file (point-min) (point-max) file)))
473
474 ;;; yahoo
475
476 (defun webmail-yahoo-open ()
477   (goto-char (point-min))
478   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
479       (setq webmail-aux (match-string 1))
480     (error "Can't find login url (open@1)")))
481
482 (defun webmail-yahoo-login ()
483   (goto-char (point-min))
484   (if (re-search-forward "http://[a-zA-Z][0-9]\\.mail\\.yahoo\\.com/" nil t)
485       (setq webmail-aux (match-string 0))
486     (error "Can't find login url (login@1)"))
487   (if (re-search-forward "YY=[0-9]+" nil t)
488       (setq webmail-aux (concat webmail-aux "ym/us/ShowFolder?"
489                                 (match-string 0)))
490     (error "Can't find login url (login@2)")))
491
492 (defun webmail-yahoo-list ()
493   (let (url (newp t) (tofetch 0))
494     (goto-char (point-min))
495     (when (re-search-forward 
496            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
497       ;;(setq listed (match-string 1))
498       (message "Found %s mail(s)" (match-string 2)))
499     (if (string-match "http://[^/]+" webmail-aux)
500         (setq webmail-aux (match-string 0 webmail-aux))
501       (error "Can't find server url (list@1)"))
502     (goto-char (point-min))
503     (while (re-search-forward 
504             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/us/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
505             nil t)
506       (if (setq url (match-string 1))
507           (progn
508             (when (or newp (not webmail-newmail-only))
509               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
510                     webmail-articles)
511               (setq tofetch (1+ tofetch)))
512             (setq newp t))
513         (setq newp nil)))
514     (message "Fetching %d mail(s)" tofetch)))
515
516 (defun webmail-yahoo-article (file id)
517   (let (p attachment)
518     (save-restriction
519       (goto-char (point-min))
520       (if (not (search-forward "value=\"Done\"" nil t))
521           (error "Can't find start label (article@1)"))
522       (if (not (search-forward "<table" nil t))
523           (error "Can't find start label (article@2)"))
524       (delete-region (point-min) (match-beginning 0)) 
525       (if (not (search-forward "</table>" nil t))
526           (error "Can't find start label (article@3)"))
527       (narrow-to-region (point-min) (match-end 0))
528       (while (search-forward "<a href=" nil t)
529         (setq p (match-beginning 0))
530         (search-forward "</a>" nil t)
531         (delete-region p (match-end 0)))
532       (webmail-remove-markup)
533       (webmail-decode-entities)
534       (goto-char (point-min))
535       (delete-blank-lines)
536       (goto-char (point-max))
537       (widen)
538       (insert "\n")
539       (setq p (point))
540       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
541         (setq attachment (match-string 0))
542         (let (bufname ct ctl cd description)
543           (if (not (search-forward "<table" nil t))
544               (error "Can't find start label (article@4)"))
545           (delete-region p (match-beginning 0))
546           (if (not (search-forward "</table>" nil t))
547               (error "Can't find start label (article@5)"))
548           (narrow-to-region p (match-end 0))
549           (webmail-remove-markup)
550           (webmail-decode-entities)
551           (goto-char (point-min))
552           (delete-blank-lines)
553           (setq ct (mail-fetch-field "content-type")
554                 ctl (ignore-errors (mail-header-parse-content-type ct))
555                 ;;cte (mail-fetch-field "content-transfer-encoding")
556                 cd (mail-fetch-field "content-disposition")
557                 description (mail-fetch-field "content-description")
558                 id (mail-fetch-field "content-id"))
559           (delete-region (point-min) (point-max))
560           (widen)
561           (save-excursion
562             (set-buffer (generate-new-buffer " *webmail-att*"))
563             (nnweb-insert (concat webmail-aux attachment))
564             (push (current-buffer) webmail-buffer-list)
565             (setq bufname (buffer-name)))
566           (insert "<#part")
567           (if (and ctl (not (equal (car ctl) "text/")))
568               (insert " type=\"" (car ctl) "\""))
569           (insert " buffer=\"" bufname "\"")
570           (if cd
571               (insert " disposition=\"" cd "\""))
572           (if description
573               (insert " description=\"" description "\""))
574           (insert "><#/part>\n")
575           (setq p (point))))
576       (delete-region p (point-max))
577       (goto-char (point-min))
578       ;; Some blank line to seperate mails.
579       (insert "\n\nFrom nobody " (current-time-string) "\n")
580       (if id
581           (insert "Message-ID: <" id "@yahoo.com>\n"))
582       (unless (looking-at "$") 
583         (search-forward "\n\n" nil t)
584         (forward-line -1))
585       (narrow-to-region (point) (point-max))
586       (insert "MIME-Version: 1.0\n"
587               (prog1
588                   (mml-generate-mime)
589                 (delete-region (point-min) (point-max))))
590       (goto-char (point-min))
591       (widen)
592       (let (case-fold-search)
593         (while (re-search-forward "^From " nil t)
594           (beginning-of-line)
595           (insert ">"))))
596     (mm-append-to-file (point-min) (point-max) file)))
597
598 ;;; netaddress
599
600 (defun webmail-netaddress-open ()
601   (goto-char (point-min))
602   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
603       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
604     (error "Can't find login url (open@1)")))
605
606 (defun webmail-netaddress-login ()
607   (webmail-refresh-redirect)
608   (goto-char (point-min))
609   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
610       (setq webmail-session (match-string 1))
611     (error "Can't find login url (login@1)")))
612
613 (defun webmail-netaddress-list ()
614   (let (item id)
615     (goto-char (point-min))
616     (when (re-search-forward 
617            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
618       (message "Found %s mail(s), %s unread" 
619                (match-string 2) (match-string 1)))
620     (goto-char (point-min))
621     (while (re-search-forward 
622             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
623       (if (setq id (match-string 2))
624           (setq item 
625                 (cons id 
626                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
627                               (car webmail-open-url)
628                               webmail-session id)))
629         (if (or (not webmail-newmail-only)
630                 (equal (match-string 1) "True"))
631             (push item webmail-articles))))))
632
633 (defun webmail-netaddress-single-part ()
634   (goto-char (point-min))
635   (cond 
636    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
637     ;; text/plain
638     (replace-match "")
639     (while (re-search-forward "[\t\040\r\n]+" nil t)
640       (replace-match " "))
641     (goto-char (point-min))
642     (while (re-search-forward "<br>" nil t)
643       (replace-match "\n"))
644     (webmail-remove-markup)
645     (webmail-decode-entities) 
646     nil)
647    (t
648     (insert "<#part type=\"text/html\" disposition=inline>")
649     (goto-char (point-max))
650     (insert "<#/part>")
651     t)))
652
653 (defun webmail-netaddress-article (file id)
654   (let (p p1 attachment count mime type)
655     (save-restriction
656       (goto-char (point-min))
657       (if (not (search-forward "Trash" nil t))
658           (error "Can't find start label (article@1)"))
659       (if (not (search-forward "<form>" nil t))
660           (error "Can't find start label (article@2)"))
661       (delete-region (point-min) (match-beginning 0)) 
662       (if (not (search-forward "</form>" nil t))
663           (error "Can't find start label (article@3)"))
664       (narrow-to-region (point-min) (match-end 0))
665       (goto-char (point-min))
666       (while (re-search-forward "[\040\t\r\n]+" nil t)
667         (replace-match " "))
668       (goto-char (point-min))
669       (while (search-forward "<b>" nil t)
670         (replace-match "\n"))
671       (webmail-remove-markup)
672       (webmail-decode-entities)
673       (goto-char (point-min))
674       (delete-blank-lines)
675       (goto-char (point-min))
676       (while (re-search-forward "^\040+\\|\040+$" nil t)
677         (replace-match ""))
678       (goto-char (point-min))
679       (while (re-search-forward "\040+" nil t)
680         (replace-match " "))
681       (goto-char (point-max))
682       (widen)
683       (insert "\n\n")
684       (setq p (point))
685       (unless (search-forward "<!-- Data -->" nil t)
686         (error "Can't find start label (article@4)"))
687       (forward-line 14)
688       (delete-region p (point))
689       (goto-char (point-max))
690       (unless (re-search-backward 
691                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
692         (error "Can't find end label (article@5)"))
693       (delete-region (point) (point-max))
694       (goto-char p)
695       (while (search-forward
696               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
697               nil t 2)
698         (setq mime t)
699         (unless (search-forward "</TABLE>" nil t)
700           (error "Can't find end label (article@6)"))
701         (setq p1 (point))
702         (if (search-backward "<IMG " p t)
703             (progn
704               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
705                 (error "Can't find tag (article@7)"))
706               (setq attachment (match-string 1))
707               (setq type (match-string 2))
708               (unless (search-forward "</TABLE>" nil t)
709                 (error "Can't find end label (article@8)"))
710               (delete-region p (point))
711               (let (bufname);; Attachment
712                 (save-excursion
713                   (set-buffer (generate-new-buffer " *webmail-att*"))
714                   (nnweb-insert (concat (car webmail-open-url) attachment))
715                   (push (current-buffer) webmail-buffer-list)
716                   (setq bufname (buffer-name)))
717                 (insert "<#part type=" type)
718                 (insert " buffer=\"" bufname "\"")
719                 (insert " disposition=\"inline\"")
720                 (insert "><#/part>\n")
721                 (setq p (point))))
722           (delete-region p p1)
723           (narrow-to-region 
724            p
725            (if (search-forward 
726                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
727                 nil t)
728                (match-beginning 0)
729              (point-max)))
730           (webmail-netaddress-single-part)
731           (goto-char (point-max))
732           (setq p (point))
733           (widen)))
734       (unless mime
735         (narrow-to-region p (point-max))
736         (setq mime (webmail-netaddress-single-part))
737         (widen))
738       (goto-char (point-min))
739       ;; Some blank line to seperate mails.
740       (insert "\n\nFrom nobody " (current-time-string) "\n")
741       (if id
742           (insert "Message-ID: <" id "@usa.net>\n"))
743       (unless (looking-at "$") 
744         (search-forward "\n\n" nil t)
745         (forward-line -1))
746       (when mime
747         (narrow-to-region (point-min) (point))
748         (goto-char (point-min))
749         (while (not (eobp))
750           (if (looking-at "MIME-Version\\|Content-Type")
751               (delete-region (point) 
752                              (progn
753                                (forward-line 1)
754                                (if (re-search-forward "^[^ \t]" nil t)
755                                    (goto-char (match-beginning 0))
756                                  (point-max))))
757             (forward-line 1)))
758         (goto-char (point-max))
759         (widen)
760         (narrow-to-region (point) (point-max))
761         (insert "MIME-Version: 1.0\n"
762                 (prog1
763                     (mml-generate-mime)
764                   (delete-region (point-min) (point-max))))
765         (goto-char (point-min))
766         (widen))
767       (let (case-fold-search)
768         (while (re-search-forward "^From " nil t)
769           (beginning-of-line)
770           (insert ">"))))
771     (mm-append-to-file (point-min) (point-max) file)))
772
773 (provide 'webmail)
774
775 ;;; webmail.el ends here