c9dfaef2ca2173a57772f29b0b3ddcf6d72e3473
[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 "FILE: wc_pnames.asp -->" nil t))
383           (error "Can't find start label (article@1)"))
384       (setq p (match-end 0))
385       (search-backward "<table" nil t)
386       (narrow-to-region (point-min) p)
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-max))
397       (widen)
398       (insert "\n")
399       (setq p (point))
400       (while (re-search-forward "<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" nil t)
401         (if (setq attachment (match-string 1))
402             (let ((filename (match-string 2))
403                   bufname) ;; Attachment
404               (delete-region p (match-end 0))
405               (save-excursion
406                 (set-buffer (generate-new-buffer " *webmail-att*"))
407                 (nnweb-insert attachment)
408                 (push (current-buffer) webmail-buffer-list)
409                 (setq bufname (buffer-name)))
410               (setq mime t)
411               (insert "<#part type=" 
412                       (or (and filename
413                                (string-match "\\.[^\\.]+$" filename)
414                                (mailcap-extension-to-mime
415                                 (match-string 0 filename)))
416                           "application/octet-stream"))
417               (insert " buffer=\"" bufname "\"")
418               (insert " filename=\"" filename "\"")
419               (insert " disposition=\"inline\"")
420               (insert "><#/part>\n")
421               (setq p (point)))
422           (delete-region p (match-end 0))
423           (setq count 1)
424           (while (and (> count 0) 
425                       (re-search-forward "</div>\\|\\(<div>\\)" nil t))
426             (if (match-string 1)
427                 (setq count (1+ count))
428               (if (= (setq count (1- count)) 0)
429                   (delete-region (match-beginning 0)
430                                  (match-end 0)))))
431           (narrow-to-region p (point))
432           (goto-char (point-min))
433           (cond 
434            ((looking-at "<pre>")
435             (goto-char (match-end 0))
436             (if (looking-at "$") (forward-char))
437             (delete-region (point-min) (point))
438             (webmail-remove-markup)
439             (webmail-decode-entities)
440             nil)
441            (t
442             (setq mime t)
443             (insert "<#part type=\"text/html\" disposition=inline>")
444             (goto-char (point-max))
445             (insert "<#/part>")))
446           (goto-char (point-max))
447           (setq p (point))
448           (widen)))
449       (delete-region p (point-max))
450       (goto-char (point-min))
451       ;; Some blank line to seperate mails.
452       (insert "\n\nFrom nobody " (current-time-string) "\n")
453       (if id
454           (insert "Message-ID: <" id "@hotmail.com>\n"))
455       (unless (looking-at "$") 
456         (search-forward "\n\n" nil t)
457         (forward-line -1))
458       (narrow-to-region (point) (point-max))
459       (if mime
460           (insert "MIME-Version: 1.0\n"
461                   (prog1
462                       (mml-generate-mime)
463                     (delete-region (point-min) (point-max)))))
464       (goto-char (point-min))
465       (widen)
466       (let (case-fold-search)
467         (while (re-search-forward "^From " nil t)
468           (beginning-of-line)
469           (insert ">"))))
470     (mm-append-to-file (point-min) (point-max) file)))
471
472 ;;; yahoo
473
474 (defun webmail-yahoo-open ()
475   (goto-char (point-min))
476   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
477       (setq webmail-aux (match-string 1))
478     (error "Can't find login url (open@1)")))
479
480 (defun webmail-yahoo-login ()
481   (goto-char (point-min))
482   (if (re-search-forward "http://[a-zA-Z][0-9]\\.mail\\.yahoo\\.com/" nil t)
483       (setq webmail-aux (match-string 0))
484     (error "Can't find login url (login@1)"))
485   (if (re-search-forward "YY=[0-9]+" nil t)
486       (setq webmail-aux (concat webmail-aux "ym/us/ShowFolder?"
487                                 (match-string 0)))
488     (error "Can't find login url (login@2)")))
489
490 (defun webmail-yahoo-list ()
491   (let (url (newp t) (tofetch 0))
492     (goto-char (point-min))
493     (when (re-search-forward 
494            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
495       ;(setq listed (match-string 1))
496       (message "Found %s mail(s)" (match-string 2)))
497     (if (string-match "http://[^/]+" webmail-aux)
498         (setq webmail-aux (match-string 0 webmail-aux))
499       (error "Can't find server url (list@1)"))
500     (goto-char (point-min))
501     (while (re-search-forward 
502             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/us/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
503             nil t)
504       (if (setq url (match-string 1))
505           (progn
506             (when (or newp (not webmail-newmail-only))
507               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
508                     webmail-articles)
509               (setq tofetch (1+ tofetch)))
510             (setq newp t))
511         (setq newp nil)))
512     (message "Fetching %d mail(s)" tofetch)))
513
514 (defun webmail-yahoo-article (file id)
515   (let (p attachment)
516     (save-restriction
517       (goto-char (point-min))
518       (if (not (search-forward "value=\"Done\"" nil t))
519           (error "Can't find start label (article@1)"))
520       (if (not (search-forward "<table" nil t))
521           (error "Can't find start label (article@2)"))
522       (delete-region (point-min) (match-beginning 0)) 
523       (if (not (search-forward "</table>" nil t))
524           (error "Can't find start label (article@3)"))
525       (narrow-to-region (point-min) (match-end 0))
526       (while (search-forward "<a href=" nil t)
527         (setq p (match-beginning 0))
528         (search-forward "</a>" nil t)
529         (delete-region p (match-end 0)))
530       (webmail-remove-markup)
531       (webmail-decode-entities)
532       (goto-char (point-min))
533       (delete-blank-lines)
534       (goto-char (point-max))
535       (widen)
536       (insert "\n")
537       (setq p (point))
538       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
539         (setq attachment (match-string 0))
540         (let (bufname ct ctl cd description)
541           (if (not (search-forward "<table" nil t))
542               (error "Can't find start label (article@4)"))
543           (delete-region p (match-beginning 0))
544           (if (not (search-forward "</table>" nil t))
545               (error "Can't find start label (article@5)"))
546           (narrow-to-region p (match-end 0))
547           (webmail-remove-markup)
548           (webmail-decode-entities)
549           (goto-char (point-min))
550           (delete-blank-lines)
551           (setq ct (mail-fetch-field "content-type")
552                 ctl (ignore-errors (mail-header-parse-content-type ct))
553                 ;;cte (mail-fetch-field "content-transfer-encoding")
554                 cd (mail-fetch-field "content-disposition")
555                 description (mail-fetch-field "content-description")
556                 id (mail-fetch-field "content-id"))
557           (delete-region (point-min) (point-max))
558           (widen)
559           (save-excursion
560             (set-buffer (generate-new-buffer " *webmail-att*"))
561             (nnweb-insert (concat webmail-aux attachment))
562             (push (current-buffer) webmail-buffer-list)
563             (setq bufname (buffer-name)))
564           (insert "<#part")
565           (if (and ctl (not (equal (car ctl) "text/")))
566               (insert " type=\"" (car ctl) "\""))
567           (insert " buffer=\"" bufname "\"")
568           (if cd
569               (insert " disposition=\"" cd "\""))
570           (if description
571               (insert " description=\"" description "\""))
572           (insert "><#/part>\n")
573           (setq p (point))))
574       (delete-region p (point-max))
575       (goto-char (point-min))
576       ;; Some blank line to seperate mails.
577       (insert "\n\nFrom nobody " (current-time-string) "\n")
578       (if id
579           (insert "Message-ID: <" id "@yahoo.com>\n"))
580       (unless (looking-at "$") 
581         (search-forward "\n\n" nil t)
582         (forward-line -1))
583       (narrow-to-region (point) (point-max))
584       (insert "MIME-Version: 1.0\n"
585               (prog1
586                   (mml-generate-mime)
587                 (delete-region (point-min) (point-max))))
588       (goto-char (point-min))
589       (widen)
590       (let (case-fold-search)
591         (while (re-search-forward "^From " nil t)
592           (beginning-of-line)
593           (insert ">"))))
594     (mm-append-to-file (point-min) (point-max) file)))
595
596 ;;; netaddress
597
598 (defun webmail-netaddress-open ()
599   (goto-char (point-min))
600   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
601       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
602     (error "Can't find login url (open@1)")))
603
604 (defun webmail-netaddress-login ()
605   (webmail-refresh-redirect)
606   (goto-char (point-min))
607   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
608       (setq webmail-session (match-string 1))
609     (error "Can't find login url (login@1)")))
610
611 (defun webmail-netaddress-list ()
612   (let (item id)
613     (goto-char (point-min))
614     (when (re-search-forward 
615            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
616       (message "Found %s mail(s), %s unread" 
617                (match-string 2) (match-string 1)))
618     (goto-char (point-min))
619     (while (re-search-forward 
620             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
621       (if (setq id (match-string 2))
622           (setq item 
623                 (cons id 
624                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
625                               (car webmail-open-url)
626                                webmail-session id)))
627         (if (or (not webmail-newmail-only)
628                 (equal (match-string 1) "True"))
629             (push item webmail-articles))))))
630
631 (defun webmail-netaddress-single-part ()
632   (goto-char (point-min))
633   (cond 
634    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
635     ;; text/plain
636     (replace-match "")
637     (while (re-search-forward "[\t\040\r\n]+" nil t)
638       (replace-match " "))
639     (goto-char (point-min))
640     (while (re-search-forward "<br>" nil t)
641         (replace-match "\n"))
642     (webmail-remove-markup)
643     (webmail-decode-entities) 
644     nil)
645    (t
646     (insert "<#part type=\"text/html\" disposition=inline>")
647     (goto-char (point-max))
648     (insert "<#/part>")
649     t)))
650
651 (defun webmail-netaddress-article (file id)
652   (let (p p1 attachment count mime type)
653     (save-restriction
654       (goto-char (point-min))
655       (if (not (search-forward "Trash" nil t))
656           (error "Can't find start label (article@1)"))
657       (if (not (search-forward "<form>" nil t))
658           (error "Can't find start label (article@2)"))
659       (delete-region (point-min) (match-beginning 0)) 
660       (if (not (search-forward "</form>" nil t))
661           (error "Can't find start label (article@3)"))
662       (narrow-to-region (point-min) (match-end 0))
663       (goto-char (point-min))
664       (while (re-search-forward "[\040\t\r\n]+" nil t)
665         (replace-match " "))
666       (goto-char (point-min))
667       (while (search-forward "<b>" nil t)
668         (replace-match "\n"))
669       (webmail-remove-markup)
670       (webmail-decode-entities)
671       (goto-char (point-min))
672       (delete-blank-lines)
673       (goto-char (point-min))
674       (while (re-search-forward "^\040+\\|\040+$" nil t)
675         (replace-match ""))
676       (goto-char (point-min))
677       (while (re-search-forward "\040+" nil t)
678         (replace-match " "))
679       (goto-char (point-max))
680       (widen)
681       (insert "\n\n")
682       (setq p (point))
683       (unless (search-forward "<!-- Data -->" nil t)
684         (error "Can't find start label (article@4)"))
685       (forward-line 14)
686       (delete-region p (point))
687       (goto-char (point-max))
688       (unless (re-search-backward 
689                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
690         (error "Can't find end label (article@5)"))
691       (delete-region (point) (point-max))
692       (goto-char p)
693       (while (search-forward
694               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
695               nil t 2)
696         (setq mime t)
697         (unless (search-forward "</TABLE>" nil t)
698           (error "Can't find end label (article@6)"))
699         (setq p1 (point))
700         (if (search-backward "<IMG " p t)
701             (progn
702               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
703                 (error "Can't find tag (article@7)"))
704               (setq attachment (match-string 1))
705               (setq type (match-string 2))
706               (unless (search-forward "</TABLE>" nil t)
707                 (error "Can't find end label (article@8)"))
708               (delete-region p (point))
709               (let (bufname) ;; Attachment
710                 (save-excursion
711                   (set-buffer (generate-new-buffer " *webmail-att*"))
712                   (nnweb-insert (concat (car webmail-open-url) attachment))
713                   (push (current-buffer) webmail-buffer-list)
714                   (setq bufname (buffer-name)))
715                 (insert "<#part type=" type)
716                 (insert " buffer=\"" bufname "\"")
717                 (insert " disposition=\"inline\"")
718                 (insert "><#/part>\n")
719                 (setq p (point))))
720           (delete-region p p1)
721           (narrow-to-region 
722            p
723            (if (search-forward 
724                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
725                 nil t)
726                (match-beginning 0)
727              (point-max)))
728           (webmail-netaddress-single-part)
729           (goto-char (point-max))
730           (setq p (point))
731           (widen)))
732       (unless mime
733           (narrow-to-region p (point-max))
734           (setq mime (webmail-netaddress-single-part))
735           (widen))
736       (goto-char (point-min))
737       ;; Some blank line to seperate mails.
738       (insert "\n\nFrom nobody " (current-time-string) "\n")
739       (if id
740           (insert "Message-ID: <" id "@usa.net>\n"))
741       (unless (looking-at "$") 
742         (search-forward "\n\n" nil t)
743         (forward-line -1))
744       (when mime
745         (narrow-to-region (point-min) (point))
746         (goto-char (point-min))
747         (while (not (eobp))
748           (if (looking-at "MIME-Version\\|Content-Type")
749               (delete-region (point) 
750                              (progn
751                                (forward-line 1)
752                                (if (re-search-forward "^[^ \t]" nil t)
753                                    (goto-char (match-beginning 0))
754                                  (point-max))))
755             (forward-line 1)))
756         (goto-char (point-max))
757         (widen)
758         (narrow-to-region (point) (point-max))
759         (insert "MIME-Version: 1.0\n"
760                 (prog1
761                     (mml-generate-mime)
762                   (delete-region (point-min) (point-max))))
763         (goto-char (point-min))
764         (widen))
765       (let (case-fold-search)
766         (while (re-search-forward "^From " nil t)
767           (beginning-of-line)
768           (insert ">"))))
769     (mm-append-to-file (point-min) (point-max) file)))
770
771 (provide 'webmail)
772
773 ;;; webmail.el ends here