1 ;;; webmail.el --- interface of web mail
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: hotmail netaddress my-deja netscape
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Note: Now mail.yahoo.com provides POP3 service, the webmail
29 ;; fetching is not going to be supported.
31 ;; Note: You need to have `url' and `w3' installed for this backend to
32 ;; work. `w3' must be 4.0pre46+one-line-cookie patch or standalone
35 ;; Todo: To support more web mail servers.
38 ;; 1. Net@ddress may corrupt `X-Face'.
41 ;; Webmail is an experimental function, which means NO WARRANTY.
45 (eval-when-compile (require 'cl))
58 (require 'url-cookie)))
59 ;; Report failure to find w3 at load time if appropriate.
62 (require 'url-cookie)))
66 (defvar webmail-type-definition
68 ;; Hotmail hate other HTTP user agents and use one line cookie
69 (paranoid agent cookie post)
70 (address . "www.hotmail.com")
71 (open-url "http://www.hotmail.com/")
72 (open-snarf . webmail-hotmail-open)
73 ;; W3 hate redirect POST
75 "http://%s/cgi-bin/dologin?login=%s&passwd=%s&enter=Sign+in&sec=no&curmbox=ACTIVE&_lang=&js=yes&id=2&tw=-10000&beta="
76 webmail-aux user password)
77 ;;(login-snarf . webmail-hotmail-login)
78 ;;(list-url "%s" webmail-aux)
79 (list-snarf . webmail-hotmail-list)
80 (article-snarf . webmail-hotmail-article)
82 "%s&login=%s&f=33792&curmbox=ACTIVE&_lang=&foo=inbox&js=&page=&%s=on&_HMaction=MoveTo&tobox=trAsH&nullbox="
85 (paranoid agent cookie post)
86 (address . "mail.yahoo.com")
87 (open-url "http://mail.yahoo.com/")
88 (open-snarf . webmail-yahoo-open)
89 (login-url;; yahoo will not accept GET
92 ".tries=&.src=ym&.last=&promo=&.intl=&.bypass=&.partner=&.chkP=Y&.done=&login=%s&passwd=%s"
94 (login-snarf . webmail-yahoo-login)
95 (list-url "%s&rb=Inbox&YN=1" webmail-aux)
96 (list-snarf . webmail-yahoo-list)
97 (article-snarf . webmail-yahoo-article)
99 "%s/ym/ShowFolder?YY=52107&inc=50&order=down&sort=date&pos=0&box=Inbox&DEL=Delete&destBox=&Mid=%s&destBox2="
102 (paranoid cookie post)
103 (address . "www.netaddress.com")
104 (open-url "http://www.netaddress.com/")
105 (open-snarf . webmail-netaddress-open)
109 "LoginState=2&SuccessfulLogin=%%2Ftpl&NewServerName=www.netaddress.com&JavaScript=JavaScript1.2&DomainID=4&Domain=usa.net&NA31site=classic.netaddress.com&NA31port=80&UserID=%s&passwd=%s"
111 (login-snarf . webmail-netaddress-login)
113 "http://www.netaddress.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
115 (list-snarf . webmail-netaddress-list)
116 (article-url "http://www.netaddress.com/")
117 (article-snarf . webmail-netaddress-article)
119 "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
122 (paranoid cookie post agent)
123 (address . "webmail.netscape.com")
124 (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")
125 (open-snarf . webmail-netscape-open)
128 ("http://ureg.netscape.com/iiop/UReg2/login/loginform")
129 "U2_USERNAME=%s&U2_PASSWORD=%s%s"
130 user password webmail-aux)
131 (login-snarf . webmail-netaddress-login)
133 "http://webmail.netscape.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
135 (list-snarf . webmail-netaddress-list)
136 (article-url "http://webmail.netscape.com/")
137 (article-snarf . webmail-netscape-article)
139 "http://webmail.netscape.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
142 (paranoid cookie post)
143 (address . "www.my-deja.com")
144 ;;(open-snarf . webmail-my-deja-open)
147 ("http://mydeja.google.com/cgi-bin/deja/maillogin.py")
148 "userid=%s&password=%s"
150 (list-snarf . webmail-my-deja-list)
151 (article-snarf . webmail-my-deja-article)
152 (trash-url webmail-aux id))))
154 (defvar webmail-variables
155 '(address article-snarf article-url list-snarf list-url
156 login-url login-snarf open-url open-snarf site articles
157 post-process paranoid trash-url))
159 (defconst webmail-version "webmail 1.0")
161 (defvar webmail-newmail-only nil
162 "Only fetch new mails.")
164 (defvar webmail-move-to-trash-can t
165 "Move mail to trash can after fetch it.")
167 ;;; Internal variables
169 (defvar webmail-address nil)
170 (defvar webmail-paranoid nil)
171 (defvar webmail-aux nil)
172 (defvar webmail-session nil)
173 (defvar webmail-article-snarf nil)
174 (defvar webmail-article-url nil)
175 (defvar webmail-list-snarf nil)
176 (defvar webmail-list-url nil)
177 (defvar webmail-login-url nil)
178 (defvar webmail-login-snarf nil)
179 (defvar webmail-open-snarf nil)
180 (defvar webmail-open-url nil)
181 (defvar webmail-trash-url nil)
182 (defvar webmail-articles nil)
183 (defvar webmail-post-process nil)
185 (defvar webmail-buffer nil)
186 (defvar webmail-buffer-list nil)
188 (defvar webmail-type nil)
190 (defvar webmail-error-function nil)
192 (defvar webmail-debug-file "~/.emacs-webmail-debug")
194 ;;; Interface functions
196 (defun webmail-debug (str)
198 (insert "\n---------------- A bug at " str " ------------------\n")
199 (mapcar #'(lambda (sym)
201 (gnus-pp `(setq ,sym ',(eval sym)))))
202 '(webmail-type user))
203 (insert "---------------- webmail buffer ------------------\n\n")
204 (insert-buffer-substring webmail-buffer)
205 (insert "\n---------------- end of buffer ------------------\n\n")
206 (append-to-file (point-min) (point-max) webmail-debug-file)))
208 (defun webmail-error (str)
209 (if webmail-error-function
210 (funcall webmail-error-function str))
211 (message "%s HTML has changed or your w3 package is too old.(%s)"
213 (error "%s HTML has changed or your w3 package is too old.(%s)"
216 (defun webmail-setdefault (type)
217 (let ((type-def (cdr (assq type webmail-type-definition)))
218 (vars webmail-variables)
220 (setq webmail-type type)
222 (if (setq pair (assq var type-def))
223 (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
224 (set (intern (concat "webmail-" (symbol-name var))) nil)))))
226 (defun webmail-eval (expr)
229 (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
235 (defun webmail-url (xurl)
236 (mm-with-unibyte-current-buffer
238 ((eq (car xurl) 'content)
240 (mm-url-fetch-simple (if (stringp (car xurl))
242 (apply 'format (webmail-eval (car xurl))))
243 (apply 'format (webmail-eval (cdr xurl)))))
244 ((eq (car xurl) 'post)
246 (mm-url-fetch-form (car xurl) (webmail-eval (cdr xurl))))
248 (mm-url-insert (apply 'format (webmail-eval xurl)))))))
250 (defun webmail-init ()
251 "Initialize buffers and such."
252 (if (gnus-buffer-live-p webmail-buffer)
253 (set-buffer webmail-buffer)
255 (nnheader-set-temp-buffer " *webmail*"))
256 (mm-disable-multibyte)))
258 (defvar url-package-name)
259 (defvar url-package-version)
260 (defvar url-cookie-multiple-line)
261 (defvar url-confirmation-func)
263 ;; Hack W3 POST redirect. See `url-parse-mime-headers'.
265 ;; Netscape uses "GET" as redirect method when orignal method is POST
266 ;; and status is 302, .i.e no security risks by default without
269 ;; Some web servers (at least Apache used by yahoo) return status 302
270 ;; instead of 303, though they mean 303.
272 (defun webmail-url-confirmation-func (prompt)
274 ((equal prompt (concat "Honor redirection with non-GET method "
275 "(possible security risks)? "))
277 ((equal prompt "Continue (with method of GET)? ")
281 (defun webmail-refresh-redirect ()
282 "Redirect refresh url in META."
283 (goto-char (point-min))
284 (while (re-search-forward
285 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
287 (let ((url (match-string 1)))
289 (mm-with-unibyte-current-buffer
290 (mm-url-insert url)))
291 (goto-char (point-min))))
293 (defun webmail-fetch (file subtype user password)
295 (webmail-setdefault subtype)
296 (let ((url-package-name (if (memq 'agent webmail-paranoid)
299 (url-package-version (if (memq 'agent webmail-paranoid)
301 url-package-version))
302 (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
304 url-cookie-multiple-line))
305 (url-confirmation-func (if (memq 'post webmail-paranoid)
306 'webmail-url-confirmation-func
307 url-confirmation-func))
308 (url-http-silence-on-insecure-redirection t)
309 url-cookie-storage url-cookie-secure-storage
310 url-cookie-confirmation
313 (setq webmail-articles nil)
314 (when webmail-open-url
316 (webmail-url webmail-open-url))
317 (if webmail-open-snarf (funcall webmail-open-snarf))
318 (when webmail-login-url
320 (webmail-url webmail-login-url))
321 (if webmail-login-snarf
322 (funcall webmail-login-snarf))
323 (when webmail-list-url
325 (webmail-url webmail-list-url))
326 (if webmail-list-snarf
327 (funcall webmail-list-snarf))
328 (while (setq item (pop webmail-articles))
329 (message "Fetching mail #%d..." (setq n (1+ n)))
331 (mm-with-unibyte-current-buffer
332 (mm-url-insert (cdr item)))
334 (if webmail-article-snarf
335 (funcall webmail-article-snarf file id))
336 (when (and webmail-trash-url webmail-move-to-trash-can)
337 (message "Move mail #%d to trash can..." n)
340 (webmail-url webmail-trash-url)
342 (while (setq buf (pop webmail-buffer-list))
346 (while (setq buf (pop webmail-buffer-list))
349 (if webmail-post-process
350 (funcall webmail-post-process))))
352 (defun webmail-encode-8bit ()
353 (goto-char (point-min))
354 (skip-chars-forward "^\200-\377")
356 (insert (format "&%d;" (mm-char-int (char-after))))
358 (skip-chars-forward "^\200-\377")))
362 (defun webmail-hotmail-open ()
363 (goto-char (point-min))
364 (if (re-search-forward
365 "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
366 (setq webmail-aux (match-string 1))
367 (webmail-error "open@1")))
369 (defun webmail-hotmail-login ()
371 (goto-char (point-min))
372 (if (re-search-forward
373 "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
374 (setq site (match-string 1))
375 (webmail-error "login@1"))
376 (goto-char (point-min))
377 (if (re-search-forward
378 "\\(/cgi-bin/HoTMaiL\\?[^\"]*a=b[^\"]*\\)" nil t)
379 (setq webmail-aux (concat "http://" site (match-string 1)))
380 (webmail-error "login@2"))))
382 (defun webmail-hotmail-list ()
383 (goto-char (point-min))
384 (skip-chars-forward " \t\n\r")
385 (let (site url newp (total "0"))
388 (if (re-search-forward "\\([0-9]+\\) *<b>(\\([0-9]+\\) new)" nil t)
389 (message "Found %s (%s new)" (setq total (match-string 1))
391 (if (re-search-forward "\\([0-9]+\\) new" nil t)
392 (message "Found %s new" (setq total (match-string 1)))
393 (webmail-error "list@0"))))
394 (unless (equal total "0")
395 (goto-char (point-min))
396 (if (re-search-forward
397 "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
398 (setq site (match-string 1))
399 (webmail-error "list@1"))
400 (goto-char (point-min))
401 (if (re-search-forward "disk=\\([^&]*\\)&" nil t)
403 (concat "http://" site "/cgi-bin/HoTMaiL?disk="
405 (webmail-error "list@2"))
406 (goto-char (point-max))
407 (while (re-search-backward
408 "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\""
410 (if (setq url (match-string 1))
412 (if (or newp (not webmail-newmail-only))
414 (if (string-match "msg=\\([^&]+\\)" url)
415 (setq id (match-string 1 url)))
416 (push (cons id (concat "http://" site url "&raw=0"))
421 ;; Thank victor@idaccr.org (Victor S. Miller) for raw=0
423 (defun webmail-hotmail-article (file id)