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