Hotmail updated. Add X-Gnus-Webmail.
[gnus] / lisp / webmail.el
1 ;;; webmail.el --- interfacing with web mail
2 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: hotmail netaddress my-deja netscape
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: Now mail.yahoo.com provides POP3 service, the webmail
27 ;; fetching is not going to be supported.
28
29 ;; Note: You need to have `url' and `w3' installed for this backend to
30 ;; work. `w3' must be 4.0pre46+one-line-cookie patch or standalone
31 ;; `url'.
32
33 ;; Todo: To support more web mail servers.
34
35 ;; Known bugs: 
36 ;; 1. Net@ddress may corrupt `X-Face'.
37
38 ;; Warning:
39 ;; Webmail is an experimental function, which means NO WARRANTY.
40
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
44
45 (require 'nnoo)
46 (require 'message)
47 (require 'gnus-util)
48 (require 'gnus)
49 (require 'nnmail)
50 (require 'mm-util)
51 (require 'mml)
52 (eval-when-compile
53   (ignore-errors
54     (require 'w3)
55     (require 'url)
56     (require 'url-cookie)
57     (require 'w3-forms)
58     (require 'nnweb)))
59 ;; Report failure to find w3 at load time if appropriate.
60 (eval '(progn
61          (require 'w3)
62          (require 'url)
63          (require 'url-cookie)
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      ;;(login-snarf . webmail-hotmail-login)
81      ;;(list-url "%s" webmail-aux)
82      (list-snarf . webmail-hotmail-list)
83      (article-snarf . webmail-hotmail-article)
84      (trash-url 
85       "%s&login=%s&f=33792&curmbox=ACTIVE&_lang=&foo=inbox&js=&page=&%s=on&_HMaction=MoveTo&tobox=trAsH&nullbox=" 
86       webmail-aux user id))
87     (yahoo
88      (paranoid agent cookie post)
89      (address . "mail.yahoo.com")
90      (open-url "http://mail.yahoo.com/")
91      (open-snarf . webmail-yahoo-open)
92      (login-url;; yahoo will not accept GET
93       content 
94       ("%s" webmail-aux)
95       ".tries=&.src=ym&.last=&promo=&.intl=&.bypass=&.partner=&.chkP=Y&.done=&login=%s&passwd=%s"
96       user password)
97      (login-snarf . webmail-yahoo-login)
98      (list-url "%s&rb=Inbox&YN=1" webmail-aux)
99      (list-snarf . webmail-yahoo-list)
100      (article-snarf . webmail-yahoo-article)
101      (trash-url 
102       "%s/ym/ShowFolder?YY=52107&inc=50&order=down&sort=date&pos=0&box=Inbox&DEL=Delete&destBox=&Mid=%s&destBox2="
103       webmail-aux id))
104     (netaddress
105      (paranoid cookie post)
106      (address . "www.netaddress.com")
107      (open-url "http://www.netaddress.com/")
108      (open-snarf . webmail-netaddress-open)
109      (login-url
110       content 
111       ("%s" webmail-aux)
112       "LoginState=2&SuccessfulLogin=%%2Ftpl&NewServerName=www.netaddress.com&JavaScript=JavaScript1.2&DomainID=4&NA31site=classic.netaddress.com&NA31port=80&UserID=%s&passwd=%s" 
113       user password)
114      (login-snarf . webmail-netaddress-login)
115      (list-url 
116       "http://www.netaddress.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
117       webmail-session)
118      (list-snarf . webmail-netaddress-list)
119      (article-url "http://www.netaddress.com/")
120      (article-snarf . webmail-netaddress-article)
121      (trash-url 
122       "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
123       webmail-session id))
124     (netscape
125      (paranoid cookie post agent)
126      (address . "webmail.netscape.com")
127      (open-url "http://ureg.netscape.com/iiop/UReg2/login/login?U2_LA=en&U2_BACK_FROM_CJ=true&U2_CS=iso-8859-1&U2_ENDURL=http://webmail.netscape.com/tpl/Subscribe/Step1&U2_NEW_ENDURL=http://webmail.netscape.com/tpl/Subscribe/Step1&U2_EXITURL=http://home.netscape.com/&U2_SOURCE=Webmail")
128      (open-snarf . webmail-netscape-open)
129      (login-url
130       content 
131       ("http://ureg.netscape.com/iiop/UReg2/login/loginform")
132       "%s&U2_USERNAME=%s&U2_PASSWORD=%s"
133       webmail-aux user password)
134      (login-snarf . webmail-netaddress-login)
135      (list-url 
136       "http://webmail.netscape.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
137       webmail-session)
138      (list-snarf . webmail-netaddress-list)
139      (article-url "http://webmail.netscape.com/")
140      (article-snarf . webmail-netscape-article)
141      (trash-url 
142       "http://webmail.netscape.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
143       webmail-session id))
144     (my-deja
145      (paranoid cookie post)
146      (address . "www.my-deja.com")
147      (open-url "http://www.my-deja.com/")
148      (open-snarf . webmail-my-deja-open)
149      (login-url
150       content 
151       ("%s" webmail-aux)
152       "member_name=%s&pw=%s&go=&priv_opt_MyDeja99="
153       user password)
154      (list-url "http://www.deja.com/rg_gotomail.xp")
155      (list-snarf . webmail-my-deja-list)
156      (article-snarf . webmail-my-deja-article)
157      (trash-url 
158       "%s/gmm_multiplex.femail?%%2Fgmm_domovemesg_top.femail=Move+to%%3A&folder_top=%s%%3Azzz%%3A%%7E6trash%%3AF%%3A0&docid=%s"
159       webmail-aux user id))))
160
161 (defvar webmail-variables
162   '(address article-snarf article-url list-snarf list-url 
163             login-url login-snarf open-url open-snarf site articles
164             post-process paranoid trash-url))
165
166 (defconst webmail-version "webmail 1.0")
167
168 (defvar webmail-newmail-only nil
169   "Only fetch new mails.")
170
171 (defvar webmail-move-to-trash-can t
172   "Move mail to trash can after fetch it.")
173
174 ;;; Internal variables
175
176 (defvar webmail-address nil)
177 (defvar webmail-paranoid nil)
178 (defvar webmail-aux nil)
179 (defvar webmail-session nil)
180 (defvar webmail-article-snarf nil)
181 (defvar webmail-article-url nil)
182 (defvar webmail-list-snarf nil)
183 (defvar webmail-list-url nil)
184 (defvar webmail-login-url nil)
185 (defvar webmail-login-snarf nil)
186 (defvar webmail-open-snarf nil)
187 (defvar webmail-open-url nil)
188 (defvar webmail-trash-url nil)
189 (defvar webmail-articles nil)
190 (defvar webmail-post-process nil)
191
192 (defvar webmail-buffer nil)
193 (defvar webmail-buffer-list nil)
194
195 (defvar webmail-type nil)
196
197 (defvar webmail-error-function nil)
198
199 (defvar webmail-debug-file "~/.emacs-webmail-debug")
200
201 ;;; Interface functions
202
203 (defun webmail-debug (str)
204   (with-temp-buffer
205     (insert "\n---------------- A bug at " str " ------------------\n")
206     (mapcar #'(lambda (sym) 
207                 (if (boundp sym)
208                     (pp `(setq ,sym ',(eval sym)) (current-buffer))))
209             '(webmail-type user))
210     (insert "---------------- webmail buffer ------------------\n\n")
211     (insert-buffer-substring webmail-buffer)
212     (insert "\n---------------- end of buffer ------------------\n\n")
213     (append-to-file (point-min) (point-max) webmail-debug-file)))
214
215 (defun webmail-error (str)
216   (if webmail-error-function
217       (funcall webmail-error-function str))
218   (message "%s HTML has changed; please get a new version of webmail (%s)"
219            webmail-type str)
220   (error "%s HTML has changed; please get a new version of webmail (%s)"
221          webmail-type str))
222
223 (defun webmail-setdefault (type)
224   (let ((type-def (cdr (assq type webmail-type-definition)))
225         (vars webmail-variables)
226         pair)
227     (setq webmail-type type)
228     (dolist (var vars)
229       (if (setq pair (assq var type-def))
230           (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
231         (set (intern (concat "webmail-" (symbol-name var))) nil)))))
232
233 (defun webmail-encode-www-form-urlencoded (pairs)
234   "Return PAIRS encoded for forms."
235   (mapconcat
236    (function
237     (lambda (data)
238       (concat (w3-form-encode-xwfu (car data)) "="
239               (w3-form-encode-xwfu (cdr data)))))
240    pairs "&"))
241
242 (defun webmail-fetch-simple (url content)
243   (let ((url-request-data content)
244         (url-request-method "POST")
245         (url-request-extra-headers
246          '(("Content-type" . "application/x-www-form-urlencoded"))))
247     (nnweb-insert url))
248   t)
249
250 (defun webmail-fetch-form (url pairs)
251   (let ((url-request-data (webmail-encode-www-form-urlencoded pairs))
252         (url-request-method "POST")
253         (url-request-extra-headers
254          '(("Content-type" . "application/x-www-form-urlencoded"))))
255     (nnweb-insert url))
256   t)
257
258 (defun webmail-eval (expr)
259   (cond
260    ((consp expr)
261     (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
262    ((symbolp expr)
263     (eval expr))
264    (t
265     expr)))
266
267 (defun webmail-url (xurl)
268   (mm-with-unibyte-current-buffer
269     (cond 
270      ((eq (car xurl) 'content)
271       (pop xurl)
272       (webmail-fetch-simple (if (stringp (car xurl))
273                                 (car xurl)
274                               (apply 'format (webmail-eval (car xurl))))
275                             (apply 'format (webmail-eval (cdr xurl)))))
276      ((eq (car xurl) 'post)
277       (pop xurl)
278       (webmail-fetch-form (car xurl) (webmail-eval (cdr xurl))))
279      (t
280       (nnweb-insert (apply 'format (webmail-eval xurl)))))))
281
282 (defun webmail-init ()
283   "Initialize buffers and such."
284   (if (gnus-buffer-live-p webmail-buffer)
285       (set-buffer webmail-buffer)
286     (setq webmail-buffer
287           (mm-with-unibyte
288             (nnheader-set-temp-buffer " *webmail*")))))
289
290 (defvar url-package-name)
291 (defvar url-package-version)
292 (defvar url-cookie-multiple-line)
293 (defvar url-confirmation-func)
294
295 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
296 ;;
297 ;; Netscape uses "GET" as redirect method when orignal method is POST
298 ;; and status is 302, .i.e no security risks by default without
299 ;; confirmation.
300 ;;
301 ;; Some web servers (at least Apache used by yahoo) return status 302
302 ;; instead of 303, though they mean 303.
303
304 (defun webmail-url-confirmation-func (prompt)
305   (cond 
306    ((equal prompt (concat "Honor redirection with non-GET method "
307                           "(possible security risks)? "))
308     nil)
309    ((equal prompt "Continue (with method of GET)? ")
310     t)
311    (t (error prompt))))
312
313 (defun webmail-refresh-redirect ()
314   "Redirect refresh url in META."
315   (goto-char (point-min))
316   (while (re-search-forward 
317           "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
318           nil t)
319     (let ((url (match-string 1)))
320       (erase-buffer)
321       (mm-with-unibyte-current-buffer
322         (nnweb-insert url)))
323     (goto-char (point-min))))
324
325 (defun webmail-fetch (file subtype user password)
326   (save-excursion
327     (webmail-setdefault subtype)
328     (let ((url-package-name (if (memq 'agent webmail-paranoid)
329                                 "Mozilla"
330                               url-package-name))
331           (url-package-version (if (memq 'agent webmail-paranoid)
332                                    "4.0"
333                                  url-package-version))
334           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
335                                         nil
336                                       url-cookie-multiple-line))
337           (url-confirmation-func (if (memq 'post webmail-paranoid)
338                                      'webmail-url-confirmation-func
339                                    url-confirmation-func))
340           (url-http-silence-on-insecure-redirection t)
341           url-cookie-storage url-cookie-secure-storage
342           url-cookie-confirmation
343           item id (n 0))
344       (webmail-init)
345       (setq webmail-articles nil)
346       (when webmail-open-url 
347         (erase-buffer)
348         (webmail-url webmail-open-url))
349       (if webmail-open-snarf (funcall webmail-open-snarf))
350       (when webmail-login-url 
351         (erase-buffer)
352         (webmail-url webmail-login-url))
353       (if webmail-login-snarf 
354           (funcall webmail-login-snarf))
355       (when webmail-list-url 
356         (erase-buffer)
357         (webmail-url webmail-list-url))
358       (if webmail-list-snarf 
359           (funcall webmail-list-snarf))
360       (while (setq item (pop webmail-articles))
361         (message "Fetching mail #%d..." (setq n (1+ n)))
362         (erase-buffer)
363         (mm-with-unibyte-current-buffer
364           (nnweb-insert (cdr item)))
365         (setq id (car item))
366         (if webmail-article-snarf 
367             (funcall webmail-article-snarf file id))
368         (when (and webmail-trash-url webmail-move-to-trash-can)
369           (message "Move mail #%d to trash can..." n)
370           (condition-case err
371               (progn
372                 (webmail-url webmail-trash-url)
373                 (let (buf)
374                   (while (setq buf (pop webmail-buffer-list))
375                     (kill-buffer buf))))
376             (error 
377              (let (buf)
378                (while (setq buf (pop webmail-buffer-list))
379                  (kill-buffer buf)))
380              (error err))))))
381     (if webmail-post-process
382         (funcall webmail-post-process))))
383
384 (defun webmail-encode-8bit ()
385   (goto-char (point-min))
386   (skip-chars-forward "^\200-\377")
387   (while (not (eobp))
388     (insert (format "&%d;" (mm-char-int (char-after))))
389     (delete-char 1)
390     (skip-chars-forward "^\200-\377")))
391
392 ;;; hotmail
393
394 (defun webmail-hotmail-open ()
395   (goto-char (point-min))
396   (if (re-search-forward 
397        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
398       (setq webmail-aux (match-string 1))
399     (webmail-error "open@1")))
400
401 (defun webmail-hotmail-login ()
402   (let (site)
403     (goto-char (point-min))
404     (if (re-search-forward 
405          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
406         (setq site (match-string 1))
407       (webmail-error "login@1"))
408     (goto-char (point-min))
409     (if (re-search-forward 
410          "\\(/cgi-bin/HoTMaiL\\?[^\"]*a=b[^\"]*\\)" nil t)
411         (setq webmail-aux (concat "http://" site (match-string 1)))
412       (webmail-error "login@2"))))
413
414 (defun webmail-hotmail-list ()
415   (let (site url newp)
416     (goto-char (point-min))
417     (if (re-search-forward "[0-9]+ new" nil t) 
418         (message "Found %s" (match-string 0))
419       (webmail-error "maybe your w3 version is too old"))
420     (goto-char (point-min))
421     (if (re-search-forward 
422          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
423         (setq site (match-string 1))
424       (webmail-error "list@1"))
425     (goto-char (point-min))
426     (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
427         (setq webmail-aux 
428               (concat "http://" site "/cgi-bin/HoTMaiL?disk=" 
429                       (match-string 1)))
430       (webmail-error "list@2"))
431     (goto-char (point-max))
432     (while (re-search-backward 
433             "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\"" 
434             nil t)
435       (if (setq url (match-string 1))
436           (progn
437             (if (or newp (not webmail-newmail-only))
438                 (let (id)
439                   (if (string-match "msg=\\([^&]+\\)" url)
440                       (setq id (match-string 1 url)))
441                   (push (cons id (concat "http://" site url "&raw=0")) 
442                         webmail-articles)))
443             (setq newp nil))
444         (setq newp t)))))
445
446 ;; Thank victor@idaccr.org (Victor S. Miller) for raw=0
447
448 (defun webmail-hotmail-article (file id)
449   (goto-char (point-min))
450   (if (not (search-forward "<pre>" nil t))
451       (webmail-error "article@3"))
452   (skip-chars-forward "\n\r\t ")
453   (delete-region (point-min) (point))
454   (if (not (search-forward "</pre>" nil t))
455       (webmail-error "article@3.1"))
456   (delete-region (match-beginning 0) (point-max))
457   (nnweb-remove-markup)
458   (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
459     (nnweb-decode-entities))
460   (goto-char (point-min))
461   (while (re-search-forward "\r\n?" nil t)
462     (replace-match "\n"))
463   (goto-char (point-min))
464   (insert "\n\n")
465   (if (not (looking-at "\n*From "))
466       (insert "From nobody " (current-time-string) "\n")
467     (forward-line))
468   (insert "X-Gnus-Webmail: " (symbol-value 'user)
469           "@" (symbol-name webmail-type) "\n")
470   (mm-append-to-file (point-min) (point-max) file))
471
472 (defun webmail-hotmail-article-old (file id)
473   (let (p attachment count mime hotmail-direct)
474     (save-restriction
475       (webmail-encode-8bit)
476       (goto-char (point-min))
477       (if (not (search-forward "<DIV>" nil t))
478           (if (not (search-forward "Reply&nbsp;All" nil t))
479               (webmail-error "article@1")
480             (setq hotmail-direct t))
481         (goto-char (match-beginning 0)))
482       (narrow-to-region (point-min) (point))
483       (if (not (search-backward "<table" nil t 2))
484           (webmail-error "article@1.1"))
485       (delete-region (point-min) (match-beginning 0)) 
486       (while (search-forward "<a href=" nil t)
487         (setq p (match-beginning 0))
488         (search-forward "</a>" nil t)
489         (delete-region p (match-end 0)))
490       (nnweb-remove-markup)
491       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
492         (nnweb-decode-entities))
493       (goto-char (point-min))
494       (delete-blank-lines)
495       (goto-char (point-min))
496       (when (search-forward "\n\n" nil t)
497         (backward-char)
498         (delete-region (point) (point-max)))
499       (goto-char (point-max))
500       (widen)
501       (insert "\n")
502       (setq p (point))
503       (while (re-search-forward 
504               "<tt>\\|<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" 
505               nil t)
506         (if (setq attachment (match-string 1))
507             (let ((filename (match-string 2))
508                   bufname);; Attachment
509               (delete-region p (match-end 0))
510               (save-excursion
511                 (set-buffer (generate-new-buffer " *webmail-att*"))
512                 (nnweb-insert attachment)
513                 (push (current-buffer) webmail-buffer-list)
514                 (setq bufname (buffer-name)))
515               (setq mime t)
516               (insert "<#part type=" 
517                       (or (and filename
518                                (string-match "\\.[^\\.]+$" filename)
519                                (mailcap-extension-to-mime
520                                 (match-string 0 filename)))
521                           "application/octet-stream"))
522               (insert " buffer=\"" bufname "\"")
523               (insert " filename=\"" filename "\"")
524               (insert " disposition=\"inline\"")
525               (insert "><#/part>\n")
526               (setq p (point)))
527           (delete-region p (match-end 0))
528           (if hotmail-direct
529               (if (not (search-forward "</tt>" nil t))
530                   (webmail-error "article@1.2")
531                 (delete-region (match-beginning 0) (match-end 0)))
532             (setq count 1)
533             (while (and (> count 0) 
534                         (re-search-forward "</div>\\|\\(<div>\\)" nil t))
535               (if (match-string 1)
536                   (setq count (1+ count))
537                 (if (= (setq count (1- count)) 0)
538                     (delete-region (match-beginning 0)
539                                    (match-end 0))))))
540           (narrow-to-region p (point))
541           (goto-char (point-min))
542           (cond 
543            ((looking-at "<pre>")
544             (goto-char (match-end 0))
545             (if (looking-at "$") (forward-char))
546             (delete-region (point-min) (point))
547             (nnweb-remove-markup)
548             (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
549               (nnweb-decode-entities))
550             nil)
551            (t
552             (setq mime t)
553             (insert "<#part type=\"text/html\" disposition=inline>")
554             (goto-char (point-max))
555             (insert "<#/part>")))
556           (goto-char (point-max))
557           (setq p (point))
558           (widen)))
559       (delete-region p (point-max))
560       (goto-char (point-min))
561       ;; Some blank line to seperate mails.
562       (insert "\n\nFrom nobody " (current-time-string) "\n")
563       (insert "X-Gnus-Webmail: " (symbol-value 'user)
564               "@" (symbol-name webmail-type) "\n")
565       (if id
566           (insert (format "X-Message-ID: <%s@hotmail.com>\n" id)))
567       (unless (looking-at "$") 
568         (if (search-forward "\n\n" nil t)
569             (forward-line -1)
570           (webmail-error "article@2")))
571       (narrow-to-region (point) (point-max))
572       (if mime
573           (insert "MIME-Version: 1.0\n"
574                   (prog1
575                       (mml-generate-mime)
576                     (delete-region (point-min) (point-max)))))
577       (goto-char (point-min))
578       (widen)
579       (let (case-fold-search)
580         (while (re-search-forward "^From " nil t)
581           (beginning-of-line)
582           (insert ">"))))
583     (mm-append-to-file (point-min) (point-max) file)))
584
585 ;;; yahoo
586
587 (defun webmail-yahoo-open ()
588   (goto-char (point-min))
589   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
590       (setq webmail-aux (match-string 1))
591     (webmail-error "open@1")))
592
593 (defun webmail-yahoo-login ()
594   (goto-char (point-min))
595   (if (re-search-forward "http://[^/]+[0-9]\\.mail\\.yahoo\\.com/" nil t)
596       (setq webmail-aux (match-string 0))
597     (webmail-error "login@1"))
598   (if (re-search-forward "YY=[0-9]+" nil t)
599       (setq webmail-aux (concat webmail-aux "ym/ShowFolder?"
600                                 (match-string 0)))
601     (webmail-error "login@2")))
602
603 (defun webmail-yahoo-list ()
604   (let (url (newp t) (tofetch 0))
605     (goto-char (point-min))
606     (when (re-search-forward 
607            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
608       ;;(setq listed (match-string 1))
609       (message "Found %s mail(s)" (match-string 2)))
610     (if (string-match "http://[^/]+" webmail-aux)
611         (setq webmail-aux (match-string 0 webmail-aux))
612       (webmail-error "list@1"))
613     (goto-char (point-min))
614     (while (re-search-forward 
615             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
616             nil t)
617       (if (setq url (match-string 1))
618           (progn
619             (when (or newp (not webmail-newmail-only))
620               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
621                     webmail-articles)
622               (setq tofetch (1+ tofetch)))
623             (setq newp t))
624         (setq newp nil)))
625     (setq webmail-articles (nreverse webmail-articles))
626     (message "Fetching %d mail(s)" tofetch)))
627
628 (defun webmail-yahoo-article (file id)
629   (let (p attachment)
630     (save-restriction
631       (goto-char (point-min))
632       (if (not (search-forward "value=\"Done\"" nil t))
633           (webmail-error "article@1"))
634       (if (not (search-forward "<table" nil t))
635           (webmail-error "article@2"))
636       (delete-region (point-min) (match-beginning 0)) 
637       (if (not (search-forward "</table>" nil t))
638           (webmail-error "article@3"))
639       (narrow-to-region (point-min) (match-end 0))
640       (while (search-forward "<a href=" nil t)
641         (setq p (match-beginning 0))
642         (search-forward "</a>" nil t)
643         (delete-region p (match-end 0)))
644       (nnweb-remove-markup)
645       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
646         (nnweb-decode-entities))
647       (goto-char (point-min))
648       (delete-blank-lines)
649       (goto-char (point-max))
650       (widen)
651       (insert "\n")
652       (setq p (point))
653       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
654         (setq attachment (match-string 0))
655         (let (bufname ct ctl cd description)
656           (if (not (search-forward "<table" nil t))
657               (webmail-error "article@4"))
658           (delete-region p (match-beginning 0))
659           (if (not (search-forward "</table>" nil t))
660               (webmail-error "article@5"))
661           (narrow-to-region p (match-end 0))
662           (nnweb-remove-markup)
663           (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
664             (nnweb-decode-entities))
665           (goto-char (point-min))
666           (delete-blank-lines)
667           (setq ct (mail-fetch-field "content-type")
668                 ctl (ignore-errors (mail-header-parse-content-type ct))
669                 ;;cte (mail-fetch-field "content-transfer-encoding")
670                 cd (mail-fetch-field "content-disposition")
671                 description (mail-fetch-field "content-description")
672                 id (mail-fetch-field "content-id"))
673           (delete-region (point-min) (point-max))
674           (widen)
675           (save-excursion
676             (set-buffer (generate-new-buffer " *webmail-att*"))
677             (nnweb-insert (concat webmail-aux attachment))
678             (push (current-buffer) webmail-buffer-list)
679             (setq bufname (buffer-name)))
680           (insert "<#part")
681           (if (and ctl (not (equal (car ctl) "text/")))
682               (insert " type=\"" (car ctl) "\""))
683           (insert " buffer=\"" bufname "\"")
684           (if cd
685               (insert " disposition=\"" cd "\""))
686           (if description
687               (insert " description=\"" description "\""))
688           (insert "><#/part>\n")
689           (setq p (point))))
690       (delete-region p (point-max))
691       (goto-char (point-min))
692       ;; Some blank line to seperate mails.
693       (insert "\n\nFrom nobody " (current-time-string) "\n")
694       (insert "X-Gnus-Webmail: " (symbol-value 'user)
695               "@" (symbol-name webmail-type) "\n")
696       (if id
697           (insert (format "X-Message-ID: <%s@yahoo.com>\n" id)))
698       (unless (looking-at "$") 
699         (if (search-forward "\n\n" nil t)
700             (forward-line -1)
701           (webmail-error "article@2")))
702       (narrow-to-region (point) (point-max))
703       (insert "MIME-Version: 1.0\n"
704               (prog1
705                   (mml-generate-mime)
706                 (delete-region (point-min) (point-max))))
707       (goto-char (point-min))
708       (widen)
709       (let (case-fold-search)
710         (while (re-search-forward "^From " nil t)
711           (beginning-of-line)
712           (insert ">"))))
713     (mm-append-to-file (point-min) (point-max) file)))
714
715 ;;; netaddress
716
717 (defun webmail-netscape-open ()
718   (goto-char (point-min))
719   (if (re-search-forward "login/hint\\?\\([^\"]+\\)\"" nil t)
720       (setq webmail-aux (match-string 1))
721     (webmail-error "open@1")))
722
723 (defun webmail-netaddress-open ()
724   (goto-char (point-min))
725   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
726       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
727     (webmail-error "open@1")))
728
729 (defun webmail-netaddress-login ()
730   (webmail-refresh-redirect)
731   (goto-char (point-min))
732   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
733       (setq webmail-session (match-string 1))
734     (webmail-error "login@1")))
735
736 (defun webmail-netaddress-list ()
737   (webmail-refresh-redirect)
738   (let (item id)
739     (goto-char (point-min))
740     (when (re-search-forward 
741            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
742       (message "Found %s mail(s), %s unread" 
743                (match-string 2) (match-string 1)))
744     (goto-char (point-min))
745     (while (re-search-forward 
746             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
747       (if (setq id (match-string 2))
748           (setq item 
749                 (cons id 
750                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
751                               (car webmail-article-url)
752                               webmail-session id)))
753         (if (or (not webmail-newmail-only)
754                 (equal (match-string 1) "True"))
755             (push item webmail-articles))))
756     (setq webmail-articles (nreverse webmail-articles))))
757
758 (defun webmail-netaddress-single-part ()
759   (goto-char (point-min))
760   (cond 
761    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
762     ;; text/plain
763     (replace-match "")
764     (while (re-search-forward "[\t\040\r\n]+" nil t)
765       (replace-match " "))
766     (goto-char (point-min))
767     (while (re-search-forward "<br>" nil t)
768       (replace-match "\n"))
769     (nnweb-remove-markup)
770     (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
771       (nnweb-decode-entities))
772     nil)
773    (t
774     (insert "<#part type=\"text/html\" disposition=inline>")
775     (goto-char (point-max))
776     (insert "<#/part>")
777     t)))
778
779 (defun webmail-netaddress-article (file id)
780   (webmail-refresh-redirect)
781   (let (p p1 attachment count mime type)
782     (save-restriction
783       (webmail-encode-8bit)
784       (goto-char (point-min))
785       (if (not (search-forward "Trash" nil t))
786           (webmail-error "article@1"))
787       (if (not (search-forward "<form>" nil t))
788           (webmail-error "article@2"))
789       (delete-region (point-min) (match-beginning 0)) 
790       (if (not (search-forward "</form>" nil t))
791           (webmail-error "article@3"))
792       (narrow-to-region (point-min) (match-end 0))
793       (goto-char (point-min))
794       (while (re-search-forward "[\040\t\r\n]+" nil t)
795         (replace-match " "))
796       (goto-char (point-min))
797       (while (search-forward "<b>" nil t)
798         (replace-match "\n"))
799       (nnweb-remove-markup)
800       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
801         (nnweb-decode-entities))
802       (goto-char (point-min))
803       (delete-blank-lines)
804       (goto-char (point-min))
805       (while (re-search-forward "^\040+\\|\040+$" nil t)
806         (replace-match ""))
807       (goto-char (point-min))
808       (while (re-search-forward "\040+" nil t)
809         (replace-match " "))
810       (goto-char (point-max))
811       (widen)
812       (insert "\n\n")
813       (setq p (point))
814       (unless (search-forward "<!-- Data -->" nil t)
815         (webmail-error "article@4"))
816       (forward-line 14)
817       (delete-region p (point))
818       (goto-char (point-max))
819       (unless (re-search-backward 
820                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
821         (webmail-error "article@5"))
822       (delete-region (point) (point-max))
823       (goto-char p)
824       (while (search-forward
825               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
826               nil t 2)
827         (setq mime t)
828         (unless (search-forward "</TABLE>" nil t)
829           (webmail-error "article@6"))
830         (setq p1 (point))
831         (if (search-backward "<IMG " p t)
832             (progn
833               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
834                 (webmail-error "article@7"))
835               (setq attachment (match-string 1))
836               (setq type (match-string 2))
837               (unless (search-forward "</TABLE>" nil t)
838                 (webmail-error "article@8"))
839               (delete-region p (point))
840               (let (bufname);; Attachment
841                 (save-excursion
842                   (set-buffer (generate-new-buffer " *webmail-att*"))
843                   (nnweb-insert (concat (car webmail-open-url) attachment))
844                   (push (current-buffer) webmail-buffer-list)
845                   (setq bufname (buffer-name)))
846                 (insert "<#part type=" type)
847                 (insert " buffer=\"" bufname "\"")
848                 (insert " disposition=\"inline\"")
849                 (insert "><#/part>\n")
850                 (setq p (point))))
851           (delete-region p p1)
852           (narrow-to-region 
853            p
854            (if (search-forward 
855                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
856                 nil t)
857                (match-beginning 0)
858              (point-max)))
859           (webmail-netaddress-single-part)
860           (goto-char (point-max))
861           (setq p (point))
862           (widen)))
863       (unless mime
864         (narrow-to-region p (point-max))
865         (setq mime (webmail-netaddress-single-part))
866         (widen))
867       (goto-char (point-min))
868       ;; Some blank line to seperate mails.
869       (insert "\n\nFrom nobody " (current-time-string) "\n")
870       (insert "X-Gnus-Webmail: " (symbol-value 'user)
871               "@" (symbol-name webmail-type) "\n")
872       (if id
873           (insert (format "X-Message-ID: <%s@%s>\n" id webmail-address)))
874       (unless (looking-at "$") 
875         (if (search-forward "\n\n" nil t)
876             (forward-line -1)
877           (webmail-error "article@2")))
878       (when mime
879         (narrow-to-region (point-min) (point))
880         (goto-char (point-min))
881         (while (not (eobp))
882           (if (looking-at "MIME-Version\\|Content-Type")
883               (delete-region (point) 
884                              (progn
885                                (forward-line 1)
886                                (if (re-search-forward "^[^ \t]" nil t)
887                                    (goto-char (match-beginning 0))
888                                  (point-max))))
889             (forward-line 1)))
890         (goto-char (point-max))
891         (widen)
892         (narrow-to-region (point) (point-max))
893         (insert "MIME-Version: 1.0\n"
894                 (prog1
895                     (mml-generate-mime)
896                   (delete-region (point-min) (point-max))))
897         (goto-char (point-min))
898         (widen))
899       (let (case-fold-search)
900         (while (re-search-forward "^From " nil t)
901           (beginning-of-line)
902           (insert ">"))))
903     (mm-append-to-file (point-min) (point-max) file)))
904
905 (defun webmail-netscape-article (file id)
906   (let (p p1 attachment count mime type)
907     (save-restriction
908       (webmail-encode-8bit)
909       (goto-char (point-min))
910       (if (not (search-forward "Trash" nil t))
911           (webmail-error "article@1"))
912       (if (not (search-forward "<form>" nil t))
913           (webmail-error "article@2"))
914       (delete-region (point-min) (match-beginning 0)) 
915       (if (not (search-forward "</form>" nil t))
916           (webmail-error "article@3"))
917       (narrow-to-region (point-min) (match-end 0))
918       (goto-char (point-min))
919       (while (re-search-forward "[\040\t\r\n]+" nil t)
920         (replace-match " "))
921       (goto-char (point-min))
922       (while (re-search-forward "<a href=[^>]*>[^<]*</a>" nil t)
923         (replace-match ""))
924       (goto-char (point-min))
925       (while (search-forward "<b>" nil t)
926         (replace-match "\n"))
927       (nnweb-remove-markup)
928       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
929         (nnweb-decode-entities))
930       (goto-char (point-min))
931       (delete-blank-lines)
932       (goto-char (point-min))
933       (while (re-search-forward "^\040+\\|\040+$" nil t)
934         (replace-match ""))
935       (goto-char (point-min))
936       (while (re-search-forward "\040+" nil t)
937         (replace-match " "))
938       (goto-char (point-max))
939       (widen)
940       (insert "\n\n")
941       (setq p (point))
942       (unless (search-forward "<!-- Data -->" nil t)
943         (webmail-error "article@4"))
944       (forward-line 14)
945       (delete-region p (point))
946       (goto-char (point-max))
947       (unless (re-search-backward 
948                "<form name=\"Transfer2\"" p t)
949         (webmail-error "article@5"))
950       (delete-region (point) (point-max))
951       (goto-char p)
952       (while (search-forward
953               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
954               nil t 2)
955         (setq mime t)
956         (unless (search-forward "</TABLE>" nil t)
957           (webmail-error "article@6"))
958         (setq p1 (point))
959         (if (search-backward "<IMG " p t)
960             (progn
961               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
962                 (webmail-error "article@7"))
963               (setq attachment (match-string 1))
964               (setq type (match-string 2))
965               (unless (search-forward "</TABLE>" nil t)
966                 (webmail-error "article@8"))
967               (delete-region p (point))
968               (let (bufname);; Attachment
969                 (save-excursion
970                   (set-buffer (generate-new-buffer " *webmail-att*"))
971                   (nnweb-insert (concat (car webmail-open-url) attachment))
972                   (push (current-buffer) webmail-buffer-list)
973                   (setq bufname (buffer-name)))
974                 (insert "<#part type=" type)
975                 (insert " buffer=\"" bufname "\"")
976                 (insert " disposition=\"inline\"")
977                 (insert "><#/part>\n")
978                 (setq p (point))))
979           (delete-region p p1)
980           (narrow-to-region 
981            p
982            (if (search-forward 
983                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
984                 nil t)
985                (match-beginning 0)
986              (point-max)))
987           (webmail-netaddress-single-part)
988           (goto-char (point-max))
989           (setq p (point))
990           (widen)))
991       (unless mime
992         (narrow-to-region p (point-max))
993         (setq mime (webmail-netaddress-single-part))
994         (widen))
995       (goto-char (point-min))
996       ;; Some blank line to seperate mails.
997       (insert "\n\nFrom nobody " (current-time-string) "\n")
998       (insert "X-Gnus-Webmail: " (symbol-value 'user)
999               "@" (symbol-name webmail-type) "\n")
1000       (if id
1001           (insert (format "X-Message-ID: <%s@%s>\n" id webmail-address)))
1002       (unless (looking-at "$") 
1003         (if (search-forward "\n\n" nil t)
1004             (forward-line -1)
1005           (webmail-error "article@2")))
1006       (when mime
1007         (narrow-to-region (point-min) (point))
1008         (goto-char (point-min))
1009         (while (not (eobp))
1010           (if (looking-at "MIME-Version\\|Content-Type")
1011               (delete-region (point) 
1012                              (progn
1013                                (forward-line 1)
1014                                (if (re-search-forward "^[^ \t]" nil t)
1015                                    (goto-char (match-beginning 0))
1016                                  (point-max))))
1017             (forward-line 1)))
1018         (goto-char (point-max))
1019         (widen)
1020         (narrow-to-region (point) (point-max))
1021         (insert "MIME-Version: 1.0\n"
1022                 (prog1
1023                     (mml-generate-mime)
1024                   (delete-region (point-min) (point-max))))
1025         (goto-char (point-min))
1026         (widen))
1027       (let (case-fold-search)
1028         (while (re-search-forward "^From " nil t)
1029           (beginning-of-line)
1030           (insert ">"))))
1031     (mm-append-to-file (point-min) (point-max) file)))
1032
1033 ;;; my-deja
1034
1035 (defun webmail-my-deja-open ()
1036   (webmail-refresh-redirect)
1037   (goto-char (point-min))
1038   (if (re-search-forward "action=\"\\([^\"]+login_confirm\\.xp[^\"]*\\)\"" 
1039                          nil t)
1040       (setq webmail-aux (match-string 1))
1041     (webmail-error "open@1")))
1042
1043 (defun webmail-my-deja-list ()
1044   (let (item id newp)
1045     (goto-char (point-min))
1046     (when (re-search-forward 
1047            "(\\([0-9]+\\) message(s), \\([0-9]+\\) new, \\([0-9]+\\)&nbsp;k )"
1048            nil t) 
1049       (message "Found %s mail(s), %s unread, total size %s K" 
1050                (match-string 1) (match-string 2) (match-string 3)))
1051     (goto-char (point-min))
1052     (while (re-search-forward 
1053             "&#149; &nbsp;&nbsp;\\|\\(http:[^\"]+\\)/display_seemesg\\.femail\\?docid=\\([^&\"]+\\)"
1054             nil t)
1055       (if (setq id (match-string 2))
1056           (when (or newp (not webmail-newmail-only))
1057             (push
1058              (cons id (format "%s/gmm_multiplex.femail?docid=%s&femail_page_name=display_page&bool_next_on_disp_pg=true&bool_prev_on_disp_pg=false&display_all_headers=false&%%2Fgmm_save.femail=Download&femail_page_name=display_page&bool_next_on_disp_pg=true&bool_prev_on_disp_pg=false&display_all_headers=false"
1059                               (match-string 1) id))
1060              webmail-articles)
1061             (setq webmail-aux (match-string 1))
1062             (setq newp nil))
1063         (setq newp t)))
1064     (setq webmail-articles (nreverse webmail-articles))))
1065
1066 (defun webmail-my-deja-article (file id)
1067   (let (url)
1068     (goto-char (point-min))
1069     (unless (re-search-forward "\\(http:[^\"]+/attachment/entire_message.txt[^\"]+\\)" nil t)
1070       (webmail-error "article@1"))
1071     (setq url (match-string 1))
1072     (erase-buffer)
1073     (mm-with-unibyte-current-buffer
1074       (nnweb-insert url))
1075     (goto-char (point-min))
1076     (while (search-forward "\r\n" nil t)
1077       (replace-match "\n"))
1078     (goto-char (point-min))
1079     (insert "\n\nFrom nobody " (current-time-string) "\n")
1080     (insert "X-Gnus-Webmail: " (symbol-value 'user)
1081             "@" (symbol-name webmail-type) "\n")
1082     (mm-append-to-file (point-min) (point-max) file)))
1083
1084 (provide 'webmail)
1085
1086 ;;; webmail.el ends here