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