(spam-report-gmane-spam): Remove redundant message.
[gnus] / lisp / webmail.el
1 ;;; webmail.el --- interface of web mail
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: hotmail netaddress my-deja netscape
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Note: Now mail.yahoo.com provides POP3 service, the webmail
29 ;; fetching is not going to be supported.
30
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
33 ;; `url'.
34
35 ;; Todo: To support more web mail servers.
36
37 ;; Known bugs:
38 ;; 1. Net@ddress may corrupt `X-Face'.
39
40 ;; Warning:
41 ;; Webmail is an experimental function, which means NO WARRANTY.
42
43 ;;; Code:
44
45 (eval-when-compile (require 'cl))
46
47 (require 'nnoo)
48 (require 'message)
49 (require 'gnus-util)
50 (require 'gnus)
51 (require 'nnmail)
52 (require 'mm-util)
53 (require 'mm-url)
54 (require 'mml)
55 (eval-when-compile
56   (ignore-errors
57     (require 'url)
58     (require 'url-cookie)))
59 ;; Report failure to find w3 at load time if appropriate.
60 (eval '(progn
61          (require 'url)
62          (require 'url-cookie)))
63
64 ;;;
65
66 (defvar webmail-type-definition
67   '((hotmail
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
74      (login-url
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)
81      (trash-url
82       "%s&login=%s&f=33792&curmbox=ACTIVE&_lang=&foo=inbox&js=&page=&%s=on&_HMaction=MoveTo&tobox=trAsH&nullbox="
83       webmail-aux user id))
84     (yahoo
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
90       content
91       ("%s" webmail-aux)
92       ".tries=&.src=ym&.last=&promo=&.intl=&.bypass=&.partner=&.chkP=Y&.done=&login=%s&passwd=%s"
93       user password)
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)
98      (trash-url
99       "%s/ym/ShowFolder?YY=52107&inc=50&order=down&sort=date&pos=0&box=Inbox&DEL=Delete&destBox=&Mid=%s&destBox2="
100       webmail-aux id))
101     (netaddress
102      (paranoid cookie post)
103      (address . "www.netaddress.com")
104      (open-url "http://www.netaddress.com/")
105      (open-snarf . webmail-netaddress-open)
106      (login-url
107       content
108       ("%s" webmail-aux)
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"
110       user password)
111      (login-snarf . webmail-netaddress-login)
112      (list-url
113       "http://www.netaddress.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
114       webmail-session)
115      (list-snarf . webmail-netaddress-list)
116      (article-url "http://www.netaddress.com/")
117      (article-snarf . webmail-netaddress-article)
118      (trash-url
119       "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
120       webmail-session id))
121     (netscape
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)
126      (login-url
127       content
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)
132      (list-url
133       "http://webmail.netscape.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
134       webmail-session)
135      (list-snarf . webmail-netaddress-list)
136      (article-url "http://webmail.netscape.com/")
137      (article-snarf . webmail-netscape-article)
138      (trash-url
139       "http://webmail.netscape.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
140       webmail-session id))
141     (my-deja
142      (paranoid cookie post)
143      (address . "www.my-deja.com")
144      ;;(open-snarf . webmail-my-deja-open)
145      (login-url
146       content
147       ("http://mydeja.google.com/cgi-bin/deja/maillogin.py")
148       "userid=%s&password=%s"
149       user password)
150      (list-snarf . webmail-my-deja-list)
151      (article-snarf . webmail-my-deja-article)
152      (trash-url webmail-aux id))))
153
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))
158
159 (defconst webmail-version "webmail 1.0")
160
161 (defvar webmail-newmail-only nil
162   "Only fetch new mails.")
163
164 (defvar webmail-move-to-trash-can t
165   "Move mail to trash can after fetch it.")
166
167 ;;; Internal variables
168
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)
184
185 (defvar webmail-buffer nil)
186 (defvar webmail-buffer-list nil)
187
188 (defvar webmail-type nil)
189
190 (defvar webmail-error-function nil)
191
192 (defvar webmail-debug-file "~/.emacs-webmail-debug")
193
194 ;;; Interface functions
195
196 (defun webmail-debug (str)
197   (with-temp-buffer
198     (insert "\n---------------- A bug at " str " ------------------\n")
199     (mapcar #'(lambda (sym)
200                 (if (boundp 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)))
207
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)"
212            webmail-type str)
213   (error "%s HTML has changed or your w3 package is too old.(%s)"
214          webmail-type str))
215
216 (defun webmail-setdefault (type)
217   (let ((type-def (cdr (assq type webmail-type-definition)))
218         (vars webmail-variables)
219         pair)
220     (setq webmail-type type)
221     (dolist (var vars)
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)))))
225
226 (defun webmail-eval (expr)
227   (cond
228    ((consp expr)
229     (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
230    ((symbolp expr)
231     (eval expr))
232    (t
233     expr)))
234
235 (defun webmail-url (xurl)
236   (mm-with-unibyte-current-buffer
237     (cond
238      ((eq (car xurl) 'content)
239       (pop xurl)
240       (mm-url-fetch-simple (if (stringp (car xurl))
241                                 (car xurl)
242                               (apply 'format (webmail-eval (car xurl))))
243                             (apply 'format (webmail-eval (cdr xurl)))))
244      ((eq (car xurl) 'post)
245       (pop xurl)
246       (mm-url-fetch-form (car xurl) (webmail-eval (cdr xurl))))
247      (t
248       (mm-url-insert (apply 'format (webmail-eval xurl)))))))
249
250 (defun webmail-init ()
251   "Initialize buffers and such."
252   (if (gnus-buffer-live-p webmail-buffer)
253       (set-buffer webmail-buffer)
254     (setq webmail-buffer
255           (nnheader-set-temp-buffer " *webmail*"))
256     (mm-disable-multibyte)))
257
258 (defvar url-package-name)
259 (defvar url-package-version)
260 (defvar url-cookie-multiple-line)
261 (defvar url-confirmation-func)
262
263 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
264 ;;
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
267 ;; confirmation.
268 ;;
269 ;; Some web servers (at least Apache used by yahoo) return status 302
270 ;; instead of 303, though they mean 303.
271
272 (defun webmail-url-confirmation-func (prompt)
273   (cond
274    ((equal prompt (concat "Honor redirection with non-GET method "
275                           "(possible security risks)? "))
276     nil)
277    ((equal prompt "Continue (with method of GET)? ")
278     t)
279    (t (error prompt))))
280
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=\\([^\"]+\\)\""
286           nil t)
287     (let ((url (match-string 1)))
288       (erase-buffer)
289       (mm-with-unibyte-current-buffer
290         (mm-url-insert url)))
291     (goto-char (point-min))))
292
293 (defun webmail-fetch (file subtype user password)
294   (save-excursion
295     (webmail-setdefault subtype)
296     (let ((url-package-name (if (memq 'agent webmail-paranoid)
297                                 "Mozilla"
298                               url-package-name))
299           (url-package-version (if (memq 'agent webmail-paranoid)
300                                    "4.0"
301                                  url-package-version))
302           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
303                                         nil
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
311           item id (n 0))
312       (webmail-init)
313       (setq webmail-articles nil)
314       (when webmail-open-url
315         (erase-buffer)
316         (webmail-url webmail-open-url))
317       (if webmail-open-snarf (funcall webmail-open-snarf))
318       (when webmail-login-url
319         (erase-buffer)
320         (webmail-url webmail-login-url))
321       (if webmail-login-snarf
322           (funcall webmail-login-snarf))
323       (when webmail-list-url
324         (erase-buffer)
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)))
330         (erase-buffer)
331         (mm-with-unibyte-current-buffer
332           (mm-url-insert (cdr item)))
333         (setq id (car 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)
338           (condition-case err
339               (progn
340                 (webmail-url webmail-trash-url)
341                 (let (buf)
342                   (while (setq buf (pop webmail-buffer-list))
343                     (kill-buffer buf))))
344             (error
345              (let (buf)
346                (while (setq buf (pop webmail-buffer-list))
347                  (kill-buffer buf)))
348              (error err))))))
349     (if webmail-post-process
350         (funcall webmail-post-process))))
351
352 (defun webmail-encode-8bit ()
353   (goto-char (point-min))
354   (skip-chars-forward "^\200-\377")
355   (while (not (eobp))
356     (insert (format "&%d;" (mm-char-int (char-after))))
357     (delete-char 1)
358     (skip-chars-forward "^\200-\377")))
359
360 ;;; hotmail
361
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")))
368
369 (defun webmail-hotmail-login ()
370   (let (site)
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"))))
381
382 (defun webmail-hotmail-list ()
383   (goto-char (point-min))
384   (skip-chars-forward " \t\n\r")
385   (let (site url newp (total "0"))
386     (if (eobp)
387         (setq 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))
390                    (match-string 2))
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)
402           (setq webmail-aux
403                 (concat "http://" site "/cgi-bin/HoTMaiL?disk="
404                         (match-string 1)))
405         (webmail-error "list@2"))
406       (goto-char (point-max))
407       (while (re-search-backward
408               "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\""
409               nil t)
410         (if (setq url (match-string 1))
411             (progn
412               (if (or newp (not webmail-newmail-only))
413                   (let (id)
414                     (if (string-match "msg=\\([^&]+\\)" url)
415                         (setq id (match-string 1 url)))
416                     (push (cons id (concat "http://" site url "&raw=0"))
417                           webmail-articles)))
418               (setq newp nil))
419           (setq newp t))))))
420
421 ;; Thank victor@idaccr.org (Victor S. Miller) for raw=0
422
423 (defun webmail-hotmail-article (file id)