Stricter test.
[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-cookie-storage url-cookie-secure-storage
341           url-cookie-confirmation
342           item id (n 0))
343       (webmail-init)
344       (setq webmail-articles nil)
345       (when webmail-open-url 
346         (erase-buffer)
347         (webmail-url webmail-open-url))
348       (if webmail-open-snarf (funcall webmail-open-snarf))
349       (when webmail-login-url 
350         (erase-buffer)
351         (webmail-url webmail-login-url))
352       (if webmail-login-snarf 
353           (funcall webmail-login-snarf))
354       (when webmail-list-url 
355         (erase-buffer)
356         (webmail-url webmail-list-url))
357       (if webmail-list-snarf 
358           (funcall webmail-list-snarf))
359       (while (setq item (pop webmail-articles))
360         (message "Fetching mail #%d..." (setq n (1+ n)))
361         (erase-buffer)
362         (mm-with-unibyte-current-buffer
363           (nnweb-insert (cdr item)))
364         (setq id (car item))
365         (if webmail-article-snarf 
366             (funcall webmail-article-snarf file id))
367         (when (and webmail-trash-url webmail-move-to-trash-can)
368           (message "Move mail #%d to trash can..." n)
369           (condition-case err
370               (progn
371                 (webmail-url webmail-trash-url)
372                 (let (buf)
373                   (while (setq buf (pop webmail-buffer-list))
374                     (kill-buffer buf))))
375             (error 
376              (let (buf)
377                (while (setq buf (pop webmail-buffer-list))
378                  (kill-buffer buf)))
379              (error err))))))
380     (if webmail-post-process
381         (funcall webmail-post-process))))
382
383 (defun webmail-encode-8bit ()
384   (goto-char (point-min))
385   (skip-chars-forward "^\200-\377")
386   (while (not (eobp))
387     (insert (format "&%d;" (mm-char-int (char-after))))
388     (delete-char 1)
389     (skip-chars-forward "^\200-\377")))
390
391 ;;; hotmail
392
393 (defun webmail-hotmail-open ()
394   (goto-char (point-min))
395   (if (re-search-forward 
396        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
397       (setq webmail-aux (match-string 1))
398     (webmail-error "open@1")))
399
400 (defun webmail-hotmail-login ()
401   (let (site)
402     (goto-char (point-min))
403     (if (re-search-forward 
404          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
405         (setq site (match-string 1))
406       (webmail-error "login@1"))
407     (goto-char (point-min))
408     (if (re-search-forward 
409          "\\(/cgi-bin/HoTMaiL\\?[^\"]*curmbox=ACTIVE[^\"]*\\)" nil t)
410         (setq webmail-aux (concat "http://" site (match-string 1)))
411       (webmail-error "login@2"))))
412
413 (defun webmail-hotmail-list ()
414   (let (site url newp)
415     (goto-char (point-min))
416     (if (re-search-forward "[0-9]+ new" nil t) 
417         (message "Found %s" (match-string 0))
418       (webmail-error "maybe your w3 version is too old"))
419     (goto-char (point-min))
420     (if (re-search-forward 
421          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
422         (setq site (match-string 1))
423       (webmail-error "list@1"))
424     (goto-char (point-min))
425     (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
426         (setq webmail-aux 
427               (concat "http://" site "/cgi-bin/HoTMaiL?disk=" 
428                       (match-string 1)))
429       (webmail-error "list@2"))
430     (goto-char (point-max))
431     (while (re-search-backward 
432             "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\"" 
433             nil t)
434       (if (setq url (match-string 1))
435           (progn
436             (if (or newp (not webmail-newmail-only))
437                 (let (id)
438                   (if (string-match "msg=\\([^&]+\\)" url)
439                       (setq id (match-string 1 url)))
440                   (push (cons id (concat "http://" site url "&raw=0")) 
441                         webmail-articles)))
442             (setq newp nil))
443         (setq newp t)))))
444
445 ;; Thank victor@idaccr.org (Victor S. Miller) for raw=0
446
447 (defun webmail-hotmail-article (file id)
448   (goto-char (point-min))
449   (if (not (search-forward "<pre>" nil t))
450       (webmail-error "article@3"))
451   (skip-chars-forward "\n\r\t ")
452   (delete-region (point-min) (point))
453   (if (not (search-forward "</pre>" nil t))
454       (webmail-error "article@3.1"))
455   (delete-region (match-beginning 0) (point-max))
456   (nnweb-remove-markup)
457   (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
458     (nnweb-decode-entities))
459   (goto-char (point-min))
460   (while (re-search-forward "\r\n?" nil t)
461     (replace-match "\n"))
462   (goto-char (point-min))
463   (insert "\n\n")
464   (if (not (looking-at "\n*From "))
465       (insert "From nobody " (current-time-string) "\n"))
466   (mm-append-to-file (point-min) (point-max) file))
467
468 (defun webmail-hotmail-article-old (file id)
469   (let (p attachment count mime hotmail-direct)
470     (save-restriction
471       (webmail-encode-8bit)
472       (goto-char (point-min))
473       (if (not (search-forward "<DIV>" nil t))
474           (if (not (search-forward "Reply&nbsp;All" nil t))
475               (webmail-error "article@1")
476             (setq hotmail-direct t))
477         (goto-char (match-beginning 0)))
478       (narrow-to-region (point-min) (point))
479       (if (not (search-backward "<table" nil t 2))
480           (webmail-error "article@1.1"))
481       (delete-region (point-min) (match-beginning 0)) 
482       (while (search-forward "<a href=" nil t)
483         (setq p (match-beginning 0))
484         (search-forward "</a>" nil t)
485         (delete-region p (match-end 0)))
486       (nnweb-remove-markup)
487       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
488         (nnweb-decode-entities))
489       (goto-char (point-min))
490       (delete-blank-lines)
491       (goto-char (point-min))
492       (when (search-forward "\n\n" nil t)
493         (backward-char)
494         (delete-region (point) (point-max)))
495       (goto-char (point-max))
496       (widen)
497       (insert "\n")
498       (setq p (point))
499       (while (re-search-forward 
500               "<tt>\\|<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" 
501               nil t)
502         (if (setq attachment (match-string 1))
503             (let ((filename (match-string 2))
504                   bufname);; Attachment
505               (delete-region p (match-end 0))
506               (save-excursion
507                 (set-buffer (generate-new-buffer " *webmail-att*"))
508                 (nnweb-insert attachment)
509                 (push (current-buffer) webmail-buffer-list)
510                 (setq bufname (buffer-name)))
511               (setq mime t)
512               (insert "<#part type=" 
513                       (or (and filename
514                                (string-match "\\.[^\\.]+$" filename)
515                                (mailcap-extension-to-mime
516                                 (match-string 0 filename)))
517                           "application/octet-stream"))
518               (insert " buffer=\"" bufname "\"")
519               (insert " filename=\"" filename "\"")
520               (insert " disposition=\"inline\"")
521               (insert "><#/part>\n")
522               (setq p (point)))
523           (delete-region p (match-end 0))
524           (if hotmail-direct
525               (if (not (search-forward "</tt>" nil t))
526                   (webmail-error "article@1.2")
527                 (delete-region (match-beginning 0) (match-end 0)))
528             (setq count 1)
529             (while (and (> count 0) 
530                         (re-search-forward "</div>\\|\\(<div>\\)" nil t))
531               (if (match-string 1)
532                   (setq count (1+ count))
533                 (if (= (setq count (1- count)) 0)
534                     (delete-region (match-beginning 0)
535                                    (match-end 0))))))
536           (narrow-to-region p (point))
537           (goto-char (point-min))
538           (cond 
539            ((looking-at "<pre>")
540             (goto-char (match-end 0))
541             (if (looking-at "$") (forward-char))
542             (delete-region (point-min) (point))
543             (nnweb-remove-markup)
544             (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
545               (nnweb-decode-entities))
546             nil)
547            (t
548             (setq mime t)
549             (insert "<#part type=\"text/html\" disposition=inline>")
550             (goto-char (point-max))
551             (insert "<#/part>")))
552           (goto-char (point-max))
553           (setq p (point))
554           (widen)))
555       (delete-region p (point-max))
556       (goto-char (point-min))
557       ;; Some blank line to seperate mails.
558       (insert "\n\nFrom nobody " (current-time-string) "\n")
559       (if id
560           (insert (format "Message-ID: <%s@hotmail.com>\n" id)))
561       (unless (looking-at "$") 
562         (if (search-forward "\n\n" nil t)
563             (forward-line -1)
564           (webmail-error "article@2")))
565       (narrow-to-region (point) (point-max))
566       (if mime
567           (insert "MIME-Version: 1.0\n"
568                   (prog1
569                       (mml-generate-mime)
570                     (delete-region (point-min) (point-max)))))
571       (goto-char (point-min))
572       (widen)
573       (let (case-fold-search)
574         (while (re-search-forward "^From " nil t)
575           (beginning-of-line)
576           (insert ">"))))
577     (mm-append-to-file (point-min) (point-max) file)))
578
579 ;;; yahoo
580
581 (defun webmail-yahoo-open ()
582   (goto-char (point-min))
583   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
584       (setq webmail-aux (match-string 1))
585     (webmail-error "open@1")))
586
587 (defun webmail-yahoo-login ()
588   (goto-char (point-min))
589   (if (re-search-forward "http://[^/]+[0-9]\\.mail\\.yahoo\\.com/" nil t)
590       (setq webmail-aux (match-string 0))
591     (webmail-error "login@1"))
592   (if (re-search-forward "YY=[0-9]+" nil t)
593       (setq webmail-aux (concat webmail-aux "ym/ShowFolder?"
594                                 (match-string 0)))
595     (webmail-error "login@2")))
596
597 (defun webmail-yahoo-list ()
598   (let (url (newp t) (tofetch 0))
599     (goto-char (point-min))
600     (when (re-search-forward 
601            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
602       ;;(setq listed (match-string 1))
603       (message "Found %s mail(s)" (match-string 2)))
604     (if (string-match "http://[^/]+" webmail-aux)
605         (setq webmail-aux (match-string 0 webmail-aux))
606       (webmail-error "list@1"))
607     (goto-char (point-min))
608     (while (re-search-forward 
609             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
610             nil t)
611       (if (setq url (match-string 1))
612           (progn
613             (when (or newp (not webmail-newmail-only))
614               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
615                     webmail-articles)
616               (setq tofetch (1+ tofetch)))
617             (setq newp t))
618         (setq newp nil)))
619     (setq webmail-articles (nreverse webmail-articles))
620     (message "Fetching %d mail(s)" tofetch)))
621
622 (defun webmail-yahoo-article (file id)
623   (let (p attachment)
624     (save-restriction
625       (goto-char (point-min))
626       (if (not (search-forward "value=\"Done\"" nil t))
627           (webmail-error "article@1"))
628       (if (not (search-forward "<table" nil t))
629           (webmail-error "article@2"))
630       (delete-region (point-min) (match-beginning 0)) 
631       (if (not (search-forward "</table>" nil t))
632           (webmail-error "article@3"))
633       (narrow-to-region (point-min) (match-end 0))
634       (while (search-forward "<a href=" nil t)
635         (setq p (match-beginning 0))
636         (search-forward "</a>" nil t)
637         (delete-region p (match-end 0)))
638       (nnweb-remove-markup)
639       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
640         (nnweb-decode-entities))
641       (goto-char (point-min))
642       (delete-blank-lines)
643       (goto-char (point-max))
644       (widen)
645       (insert "\n")
646       (setq p (point))
647       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
648         (setq attachment (match-string 0))
649         (let (bufname ct ctl cd description)
650           (if (not (search-forward "<table" nil t))
651               (webmail-error "article@4"))
652           (delete-region p (match-beginning 0))
653           (if (not (search-forward "</table>" nil t))
654               (webmail-error "article@5"))
655           (narrow-to-region p (match-end 0))
656           (nnweb-remove-markup)
657           (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
658             (nnweb-decode-entities))
659           (goto-char (point-min))
660           (delete-blank-lines)
661           (setq ct (mail-fetch-field "content-type")
662                 ctl (ignore-errors (mail-header-parse-content-type ct))
663                 ;;cte (mail-fetch-field "content-transfer-encoding")
664                 cd (mail-fetch-field "content-disposition")
665                 description (mail-fetch-field "content-description")
666                 id (mail-fetch-field "content-id"))
667           (delete-region (point-min) (point-max))
668           (widen)
669           (save-excursion
670             (set-buffer (generate-new-buffer " *webmail-att*"))
671             (nnweb-insert (concat webmail-aux attachment))
672             (push (current-buffer) webmail-buffer-list)
673             (setq bufname (buffer-name)))
674           (insert "<#part")
675           (if (and ctl (not (equal (car ctl) "text/")))
676               (insert " type=\"" (car ctl) "\""))
677           (insert " buffer=\"" bufname "\"")
678           (if cd
679               (insert " disposition=\"" cd "\""))
680           (if description
681               (insert " description=\"" description "\""))
682           (insert "><#/part>\n")
683           (setq p (point))))
684       (delete-region p (point-max))
685       (goto-char (point-min))
686       ;; Some blank line to seperate mails.
687       (insert "\n\nFrom nobody " (current-time-string) "\n")
688       (if id
689           (insert (format "Message-ID: <%s@yahoo.com>\n" id)))
690       (unless (looking-at "$") 
691         (if (search-forward "\n\n" nil t)
692             (forward-line -1)
693           (webmail-error "article@2")))
694       (narrow-to-region (point) (point-max))
695       (insert "MIME-Version: 1.0\n"
696               (prog1
697                   (mml-generate-mime)
698                 (delete-region (point-min) (point-max))))
699       (goto-char (point-min))
700       (widen)
701       (let (case-fold-search)
702         (while (re-search-forward "^From " nil t)
703           (beginning-of-line)
704           (insert ">"))))
705     (mm-append-to-file (point-min) (point-max) file)))
706
707 ;;; netaddress
708
709 (defun webmail-netscape-open ()
710   (goto-char (point-min))
711   (if (re-search-forward "login/hint\\?\\([^\"]+\\)\"" nil t)
712       (setq webmail-aux (match-string 1))
713     (webmail-error "open@1")))
714
715 (defun webmail-netaddress-open ()
716   (goto-char (point-min))
717   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
718       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
719     (webmail-error "open@1")))
720
721 (defun webmail-netaddress-login ()
722   (webmail-refresh-redirect)
723   (goto-char (point-min))
724   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
725       (setq webmail-session (match-string 1))
726     (webmail-error "login@1")))
727
728 (defun webmail-netaddress-list ()
729   (webmail-refresh-redirect)
730   (let (item id)
731     (goto-char (point-min))
732     (when (re-search-forward 
733            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
734       (message "Found %s mail(s), %s unread" 
735                (match-string 2) (match-string 1)))
736     (goto-char (point-min))
737     (while (re-search-forward 
738             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
739       (if (setq id (match-string 2))
740           (setq item 
741                 (cons id 
742                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
743                               (car webmail-article-url)
744                               webmail-session id)))
745         (if (or (not webmail-newmail-only)
746                 (equal (match-string 1) "True"))
747             (push item webmail-articles))))
748     (setq webmail-articles (nreverse webmail-articles))))
749
750 (defun webmail-netaddress-single-part ()
751   (goto-char (point-min))
752   (cond 
753    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
754     ;; text/plain
755     (replace-match "")
756     (while (re-search-forward "[\t\040\r\n]+" nil t)
757       (replace-match " "))
758     (goto-char (point-min))
759     (while (re-search-forward "<br>" nil t)
760       (replace-match "\n"))
761     (nnweb-remove-markup)
762     (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
763       (nnweb-decode-entities))
764     nil)
765    (t
766     (insert "<#part type=\"text/html\" disposition=inline>")
767     (goto-char (point-max))
768     (insert "<#/part>")
769     t)))
770
771 (defun webmail-netaddress-article (file id)
772   (webmail-refresh-redirect)
773   (let (p p1 attachment count mime type)
774     (save-restriction
775       (webmail-encode-8bit)
776       (goto-char (point-min))
777       (if (not (search-forward "Trash" nil t))
778           (webmail-error "article@1"))
779       (if (not (search-forward "<form>" nil t))
780           (webmail-error "article@2"))
781       (delete-region (point-min) (match-beginning 0)) 
782       (if (not (search-forward "</form>" nil t))
783           (webmail-error "article@3"))
784       (narrow-to-region (point-min) (match-end 0))
785       (goto-char (point-min))
786       (while (re-search-forward "[\040\t\r\n]+" nil t)
787         (replace-match " "))
788       (goto-char (point-min))
789       (while (search-forward "<b>" nil t)
790         (replace-match "\n"))
791       (nnweb-remove-markup)
792       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
793         (nnweb-decode-entities))
794       (goto-char (point-min))
795       (delete-blank-lines)
796       (goto-char (point-min))
797       (while (re-search-forward "^\040+\\|\040+$" nil t)
798         (replace-match ""))
799       (goto-char (point-min))
800       (while (re-search-forward "\040+" nil t)
801         (replace-match " "))
802       (goto-char (point-max))
803       (widen)
804       (insert "\n\n")
805       (setq p (point))
806       (unless (search-forward "<!-- Data -->" nil t)
807         (webmail-error "article@4"))
808       (forward-line 14)
809       (delete-region p (point))
810       (goto-char (point-max))
811       (unless (re-search-backward 
812                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
813         (webmail-error "article@5"))
814       (delete-region (point) (point-max))
815       (goto-char p)
816       (while (search-forward
817               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
818               nil t 2)
819         (setq mime t)
820         (unless (search-forward "</TABLE>" nil t)
821           (webmail-error "article@6"))
822         (setq p1 (point))
823         (if (search-backward "<IMG " p t)
824             (progn
825               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
826                 (webmail-error "article@7"))
827               (setq attachment (match-string 1))
828               (setq type (match-string 2))
829               (unless (search-forward "</TABLE>" nil t)
830                 (webmail-error "article@8"))
831               (delete-region p (point))
832               (let (bufname);; Attachment
833                 (save-excursion
834                   (set-buffer (generate-new-buffer " *webmail-att*"))
835                   (nnweb-insert (concat (car webmail-open-url) attachment))
836                   (push (current-buffer) webmail-buffer-list)
837                   (setq bufname (buffer-name)))
838                 (insert "<#part type=" type)
839                 (insert " buffer=\"" bufname "\"")
840                 (insert " disposition=\"inline\"")
841                 (insert "><#/part>\n")
842                 (setq p (point))))
843           (delete-region p p1)
844           (narrow-to-region 
845            p
846            (if (search-forward 
847                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
848                 nil t)
849                (match-beginning 0)
850              (point-max)))
851           (webmail-netaddress-single-part)
852           (goto-char (point-max))
853           (setq p (point))
854           (widen)))
855       (unless mime
856         (narrow-to-region p (point-max))
857         (setq mime (webmail-netaddress-single-part))
858         (widen))
859       (goto-char (point-min))
860       ;; Some blank line to seperate mails.
861       (insert "\n\nFrom nobody " (current-time-string) "\n")
862       (if id
863           (insert (format "Message-ID: <%s@%s>\n" id webmail-address)))
864       (unless (looking-at "$") 
865         (if (search-forward "\n\n" nil t)
866             (forward-line -1)
867           (webmail-error "article@2")))
868       (when mime
869         (narrow-to-region (point-min) (point))
870         (goto-char (point-min))
871         (while (not (eobp))
872           (if (looking-at "MIME-Version\\|Content-Type")
873               (delete-region (point) 
874                              (progn
875                                (forward-line 1)
876                                (if (re-search-forward "^[^ \t]" nil t)
877                                    (goto-char (match-beginning 0))
878                                  (point-max))))
879             (forward-line 1)))
880         (goto-char (point-max))
881         (widen)
882         (narrow-to-region (point) (point-max))
883         (insert "MIME-Version: 1.0\n"
884                 (prog1
885                     (mml-generate-mime)
886                   (delete-region (point-min) (point-max))))
887         (goto-char (point-min))
888         (widen))
889       (let (case-fold-search)
890         (while (re-search-forward "^From " nil t)
891           (beginning-of-line)
892           (insert ">"))))
893     (mm-append-to-file (point-min) (point-max) file)))
894
895 (defun webmail-netscape-article (file id)
896   (let (p p1 attachment count mime type)
897     (save-restriction
898       (webmail-encode-8bit)
899       (goto-char (point-min))
900       (if (not (search-forward "Trash" nil t))
901           (webmail-error "article@1"))
902       (if (not (search-forward "<form>" nil t))
903           (webmail-error "article@2"))
904       (delete-region (point-min) (match-beginning 0)) 
905       (if (not (search-forward "</form>" nil t))
906           (webmail-error "article@3"))
907       (narrow-to-region (point-min) (match-end 0))
908       (goto-char (point-min))
909       (while (re-search-forward "[\040\t\r\n]+" nil t)
910         (replace-match " "))
911       (goto-char (point-min))
912       (while (re-search-forward "<a href=[^>]*>[^<]*</a>" nil t)
913         (replace-match ""))
914       (goto-char (point-min))
915       (while (search-forward "<b>" nil t)
916         (replace-match "\n"))
917       (nnweb-remove-markup)
918       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
919         (nnweb-decode-entities))
920       (goto-char (point-min))
921       (delete-blank-lines)
922       (goto-char (point-min))
923       (while (re-search-forward "^\040+\\|\040+$" nil t)
924         (replace-match ""))
925       (goto-char (point-min))
926       (while (re-search-forward "\040+" nil t)
927         (replace-match " "))
928       (goto-char (point-max))
929       (widen)
930       (insert "\n\n")
931       (setq p (point))
932       (unless (search-forward "<!-- Data -->" nil t)
933         (webmail-error "article@4"))
934       (forward-line 14)
935       (delete-region p (point))
936       (goto-char (point-max))
937       (unless (re-search-backward 
938                "<form name=\"Transfer2\"" p t)
939         (webmail-error "article@5"))
940       (delete-region (point) (point-max))
941       (goto-char p)
942       (while (search-forward
943               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
944               nil t 2)
945         (setq mime t)
946         (unless (search-forward "</TABLE>" nil t)
947           (webmail-error "article@6"))
948         (setq p1 (point))
949         (if (search-backward "<IMG " p t)
950             (progn
951               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
952                 (webmail-error "article@7"))
953               (setq attachment (match-string 1))
954               (setq type (match-string 2))
955               (unless (search-forward "</TABLE>" nil t)
956                 (webmail-error "article@8"))
957               (delete-region p (point))
958               (let (bufname);; Attachment
959                 (save-excursion
960                   (set-buffer (generate-new-buffer " *webmail-att*"))
961                   (nnweb-insert (concat (car webmail-open-url) attachment))
962                   (push (current-buffer) webmail-buffer-list)
963                   (setq bufname (buffer-name)))
964                 (insert "<#part type=" type)
965                 (insert " buffer=\"" bufname "\"")
966                 (insert " disposition=\"inline\"")
967                 (insert "><#/part>\n")
968                 (setq p (point))))
969           (delete-region p p1)
970           (narrow-to-region 
971            p
972            (if (search-forward 
973                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
974                 nil t)
975                (match-beginning 0)
976              (point-max)))
977           (webmail-netaddress-single-part)
978           (goto-char (point-max))
979           (setq p (point))
980           (widen)))
981       (unless mime
982         (narrow-to-region p (point-max))
983         (setq mime (webmail-netaddress-single-part))
984         (widen))
985       (goto-char (point-min))
986       ;; Some blank line to seperate mails.
987       (insert "\n\nFrom nobody " (current-time-string) "\n")
988       (if id
989           (insert (format "Message-ID: <%s@%s>\n" id webmail-address)))
990       (unless (looking-at "$") 
991         (if (search-forward "\n\n" nil t)
992             (forward-line -1)
993           (webmail-error "article@2")))
994       (when mime
995         (narrow-to-region (point-min) (point))
996         (goto-char (point-min))
997         (while (not (eobp))
998           (if (looking-at "MIME-Version\\|Content-Type")
999               (delete-region (point) 
1000                              (progn
1001                                (forward-line 1)
1002                                (if (re-search-forward "^[^ \t]" nil t)
1003                                    (goto-char (match-beginning 0))
1004                                  (point-max))))
1005             (forward-line 1)))
1006         (goto-char (point-max))
1007         (widen)
1008         (narrow-to-region (point) (point-max))
1009         (insert "MIME-Version: 1.0\n"
1010                 (prog1
1011                     (mml-generate-mime)
1012                   (delete-region (point-min) (point-max))))
1013         (goto-char (point-min))
1014         (widen))
1015       (let (case-fold-search)
1016         (while (re-search-forward "^From " nil t)
1017           (beginning-of-line)
1018           (insert ">"))))
1019     (mm-append-to-file (point-min) (point-max) file)))
1020
1021 ;;; my-deja
1022
1023 (defun webmail-my-deja-open ()
1024   (webmail-refresh-redirect)
1025   (goto-char (point-min))
1026   (if (re-search-forward "action=\"\\([^\"]+login_confirm\\.xp[^\"]*\\)\"" 
1027                          nil t)
1028       (setq webmail-aux (match-string 1))
1029     (webmail-error "open@1")))
1030
1031 (defun webmail-my-deja-list ()
1032   (let (item id newp)
1033     (goto-char (point-min))
1034     (when (re-search-forward 
1035            "(\\([0-9]+\\) message(s), \\([0-9]+\\) new, \\([0-9]+\\)&nbsp;k )"
1036            nil t) 
1037       (message "Found %s mail(s), %s unread, total size %s K" 
1038                (match-string 1) (match-string 2) (match-string 3)))
1039     (goto-char (point-min))
1040     (while (re-search-forward 
1041             "&#149; &nbsp;&nbsp;\\|\\(http:[^\"]+\\)/display_seemesg\\.femail\\?docid=\\([^&\"]+\\)"
1042             nil t)
1043       (if (setq id (match-string 2))
1044           (when (or newp (not webmail-newmail-only))
1045             (push
1046              (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"
1047                               (match-string 1) id))
1048              webmail-articles)
1049             (setq webmail-aux (match-string 1))
1050             (setq newp nil))
1051         (setq newp t)))
1052     (setq webmail-articles (nreverse webmail-articles))))
1053
1054 (defun webmail-my-deja-article (file id)
1055   (let (url)
1056     (goto-char (point-min))
1057     (unless (re-search-forward "\\(http:[^\"]+/attachment/entire_message.txt[^\"]+\\)" nil t)
1058       (webmail-error "article@1"))
1059     (setq url (match-string 1))
1060     (erase-buffer)
1061     (mm-with-unibyte-current-buffer
1062       (nnweb-insert url))
1063     (goto-char (point-min))
1064     (while (search-forward "\r\n" nil t)
1065       (replace-match "\n"))
1066     (goto-char (point-min))
1067     (insert "\n\nFrom nobody " (current-time-string) "\n")
1068     (mm-append-to-file (point-min) (point-max) file)))
1069
1070 (provide 'webmail)
1071
1072 ;;; webmail.el ends here