1 ;;; webmail.el --- interfacing with web mail
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;; Note: You need to have `url' and `w3' installed for this backend to
27 ;; work. `w3' must be 4.0pre46+one-line-cookie patch or standalone
30 ;; Todo: To support more web mail servers.
33 ;; 1. Net@ddress may corrupt `X-Face'.
36 ;; Webmail is an experimental function, which means NO WARRANTY.
40 (eval-when-compile (require 'cl))
56 ;; Report failure to find w3 at load time if appropriate.
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 (list-snarf . webmail-hotmail-list)
78 (article-snarf . webmail-hotmail-article)
80 "%s&login=%s&f=33792&curmbox=ACTIVE&_lang=&js=&foo=inbox&page=&%s=on&Move+To.x=Move+To&tobox=trAsH"
83 (paranoid cookie post)
84 (address . "mail.yahoo.com")
85 (open-url "http://mail.yahoo.com/")
86 (open-snarf . webmail-yahoo-open)
87 (login-url;; yahoo will not accept GET
90 ".tries=1&.src=ym&.last=&promo=&lg=us&.intl=us&.bypass=&.chkP=Y&.done=http%%253a%%2F%%2Fedit.yahoo.com%%2Fconfig%%2Fmail%%253f.intl%%3D&login=%s&passwd=%s"
92 (login-snarf . webmail-yahoo-login)
93 (list-url "%s&rb=Inbox&YN=1" webmail-aux)
94 (list-snarf . webmail-yahoo-list)
95 (article-snarf . webmail-yahoo-article)
97 "%s/ym/us/ShowFolder?YY=52107&inc=50&order=down&sort=date&pos=0&box=Inbox&DEL=Delete&destBox=&Mid=%s&destBox2="
100 (paranoid cookie post)
101 (address . "www.netaddress.com")
102 (open-url "http://www.netaddress.com/")
103 (open-snarf . webmail-netaddress-open)
104 (login-url;; yahoo will not accept GET
107 "LoginState=2&SuccessfulLogin=%%2Ftpl&NewServerName=www.netaddress.com&JavaScript=JavaScript1.2&DomainID=4&NA31site=classic.netaddress.com&NA31port=80&UserID=%s&passwd=%s"
109 (login-snarf . webmail-netaddress-login)
111 "http://www.netaddress.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
113 (list-snarf . webmail-netaddress-list)
114 (article-snarf . webmail-netaddress-article)
116 "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
117 webmail-session id))))
119 (defvar webmail-variables
120 '(address article-snarf article-url list-snarf list-url
121 login-url login-snarf open-url open-snarf site articles
122 post-process paranoid trash-url))
124 (defconst webmail-version "webmail 1.0")
126 (defvar webmail-newmail-only nil
127 "Only fetch new mails.")
129 (defvar webmail-move-to-trash-can t
130 "Move mail to trash can after fetch it.")
132 ;;; Internal variables
134 (defvar webmail-address nil)
135 (defvar webmail-paranoid nil)
136 (defvar webmail-aux nil)
137 (defvar webmail-session nil)
138 (defvar webmail-article-snarf nil)
139 (defvar webmail-article-url nil)
140 (defvar webmail-list-snarf nil)
141 (defvar webmail-list-url nil)
142 (defvar webmail-login-url nil)
143 (defvar webmail-login-snarf nil)
144 (defvar webmail-open-snarf nil)
145 (defvar webmail-open-url nil)
146 (defvar webmail-trash-url nil)
147 (defvar webmail-articles nil)
148 (defvar webmail-post-process nil)
150 (defvar webmail-buffer nil)
151 (defvar webmail-buffer-list nil)
152 ;;; Interface functions
154 (defun webmail-setdefault (type)
155 (let ((type-def (cdr (assq type webmail-type-definition)))
156 (vars webmail-variables)
159 (if (setq pair (assq var type-def))
160 (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
161 (set (intern (concat "webmail-" (symbol-name var))) nil)))))
163 (defun webmail-encode-www-form-urlencoded (pairs)
164 "Return PAIRS encoded for forms."
168 (concat (w3-form-encode-xwfu (car data)) "="
169 (w3-form-encode-xwfu (cdr data)))))
172 (defun webmail-fetch-simple (url content)
173 (let ((url-request-data content)
174 (url-request-method "POST")
175 (url-request-extra-headers
176 '(("Content-type" . "application/x-www-form-urlencoded"))))
180 (defun webmail-fetch-form (url pairs)
181 (let ((url-request-data (webmail-encode-www-form-urlencoded pairs))
182 (url-request-method "POST")
183 (url-request-extra-headers
184 '(("Content-type" . "application/x-www-form-urlencoded"))))
188 (defun webmail-eval (expr)
191 (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
197 (defun webmail-url (xurl)
198 (mm-with-unibyte-current-buffer
200 ((eq (car xurl) 'content)
202 (webmail-fetch-simple (if (stringp (car xurl))
204 (apply 'format (webmail-eval (car xurl))))
205 (apply 'format (webmail-eval (cdr xurl)))))
206 ((eq (car xurl) 'post)
208 (webmail-fetch-form (car xurl) (webmail-eval (cdr xurl))))
210 (nnweb-insert (apply 'format (webmail-eval xurl)))))))
212 (defun webmail-init ()
213 "Initialize buffers and such."
214 (if (gnus-buffer-live-p webmail-buffer)
215 (set-buffer webmail-buffer)
217 (nnheader-set-temp-buffer " *webmail*"))))
219 (defvar url-package-name)
220 (defvar url-package-version)
221 (defvar url-cookie-multiple-line)
222 (defvar url-confirmation-func)
224 ;; Hack W3 POST redirect. See `url-parse-mime-headers'.
226 ;; Netscape uses "GET" as redirect method when orignal method is POST
227 ;; and status is 302, .i.e no security risks by default without
230 ;; Some web servers (at least Apache used by yahoo) return status 302
231 ;; instead of 303, though they mean 303.
233 (defun webmail-url-confirmation-func (prompt)
235 ((equal prompt (concat "Honor redirection with non-GET method "
236 "(possible security risks)? "))
238 ((equal prompt "Continue (with method of GET)? ")
242 (defun webmail-refresh-redirect ()
243 "Redirect refresh url in META."
244 (goto-char (point-min))
245 (while (re-search-forward "HTTP-EQUIV=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
247 (let ((url (match-string 1)))
250 (goto-char (point-min))))
252 (defun webmail-fetch (file subtype user password)
254 (webmail-setdefault subtype)
255 (let ((url-package-name (if (memq 'agent webmail-paranoid)
258 (url-package-version (if (memq 'agent webmail-paranoid)
260 url-package-version))
261 (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
263 url-cookie-multiple-line))
264 (url-confirmation-func (if (memq 'post webmail-paranoid)
265 'webmail-url-confirmation-func
266 url-confirmation-func))
267 url-cookie-storage url-cookie-secure-storage
268 url-cookie-confirmation
271 (setq webmail-articles nil)
272 (when webmail-open-url
274 (webmail-url webmail-open-url))
275 (if webmail-open-snarf (funcall webmail-open-snarf))
276 (when webmail-login-url
278 (webmail-url webmail-login-url))
279 (if webmail-login-snarf
280 (funcall webmail-login-snarf))
281 (when webmail-list-url
283 (webmail-url webmail-list-url))
284 (if webmail-list-snarf
285 (funcall webmail-list-snarf))
286 (while (setq item (pop webmail-articles))
287 (message "Fetching mail #%d..." (setq n (1+ n)))
289 (nnweb-insert (cdr item))
291 (if webmail-article-snarf
292 (funcall webmail-article-snarf file id))
293 (when (and webmail-trash-url webmail-move-to-trash-can)
294 (message "Move mail #%d to trash can..." n)
297 (webmail-url webmail-trash-url)
299 (while (setq buf (pop webmail-buffer-list))
303 (while (setq buf (pop webmail-buffer-list))
306 (if webmail-post-process
307 (funcall webmail-post-process))))
311 (defun webmail-hotmail-open ()
312 (goto-char (point-min))
313 (if (re-search-forward
314 "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
315 (setq webmail-aux (match-string 1))
316 (error "Can't find login url (open@1)")))
318 (defun webmail-hotmail-list ()
320 (goto-char (point-min))
321 (if (re-search-forward "[0-9]+ messages, [0-9]+ new")
322 (message "Found %s" (match-string 0)))
323 (goto-char (point-min))
324 (if (re-search-forward
325 "action=\"https?://\\([^/]+\\)/cgi-bin/HoTMaiL" nil t)
326 (setq site (match-string 1))
327 (error "Can't find server url (list@1)"))
328 (goto-char (point-min))
329 (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
331 (concat "http://" site "/cgi-bin/HoTMaiL?disk="
333 (error "Can't find disk (list@2)"))
334 (goto-char (point-max))
335 (while (re-search-backward
336 "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\""
338 (if (setq url (match-string 1))
340 (if (or newp (not webmail-newmail-only))
342 (if (string-match "msg=\\([^&]+\\)" url)
343 (setq id (match-string 1 url)))
344 (push (cons id (concat "http://" site url))
349 (defun webmail-hotmail-article (file id)
350 (let (p attachment count mime)
352 (goto-char (point-min))
353 (if (not (search-forward "<DIV>" nil t))
354 (error "Can't find start label (article@1)"))
355 (narrow-to-region (point-min) (match-beginning 0))
356 (if (not (search-backward "<table" nil t 2))
357 (error "Can't find start label (article@1.1)"))
358 (delete-region (point-min) (match-beginning 0))
359 (while (search-forward "<a href=" nil t)
360 (setq p (match-beginning 0))
361 (search-forward "</a>" nil t)
362 (delete-region p (match-end 0)))
363 (nnweb-remove-markup)
364 (nnweb-decode-entities)
365 (goto-char (point-min))
367 (goto-char (point-min))
368 (when (search-forward "\n\n" nil t)
370 (delete-region (point) (point-max)))
371 (goto-char (point-max))
375 (while (re-search-forward "<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" nil t)
376 (if (setq attachment (match-string 1))
377 (let ((filename (match-string 2))
378 bufname);; Attachment
379 (delete-region p (match-end 0))
381 (set-buffer (generate-new-buffer " *webmail-att*"))
382 (nnweb-insert attachment)
383 (push (current-buffer) webmail-buffer-list)
384 (setq bufname (buffer-name)))
386 (insert "<#part type="
388 (string-match "\\.[^\\.]+$" filename)
389 (mailcap-extension-to-mime
390 (match-string 0 filename)))
391 "application/octet-stream"))
392 (insert " buffer=\"" bufname "\"")
393 (insert " filename=\"" filename "\"")
394 (insert " disposition=\"inline\"")
395 (insert "><#/part>\n")
397 (delete-region p (match-end 0))
399 (while (and (> count 0)
400 (re-search-forward "</div>\\|\\(<div>\\)" nil t))
402 (setq count (1+ count))
403 (if (= (setq count (1- count)) 0)
404 (delete-region (match-beginning 0)
406 (narrow-to-region p (point))
407 (goto-char (point-min))
409 ((looking-at "<pre>")
410 (goto-char (match-end 0))
411 (if (looking-at "$") (forward-char))
412 (delete-region (point-min) (point))
413 (nnweb-remove-markup)
414 (nnweb-decode-entities)
418 (insert "<#part type=\"text/html\" disposition=inline>")
419 (goto-char (point-max))
420 (insert "<#/part>")))
421 (goto-char (point-max))
424 (delete-region p (point-max))
425 (goto-char (point-min))
426 ;; Some blank line to seperate mails.
427 (insert "\n\nFrom nobody " (current-time-string) "\n")
429 (insert "Message-ID: <" id "@hotmail.com>\n"))
430 (unless (looking-at "$")
431 (search-forward "\n\n" nil t)
433 (narrow-to-region (point) (point-max))
435 (insert "MIME-Version: 1.0\n"
438 (delete-region (point-min) (point-max)))))
439 (goto-char (point-min))
441 (let (case-fold-search)
442 (while (re-search-forward "^From " nil t)
445 (mm-append-to-file (point-min) (point-max) file)))
449 (defun webmail-yahoo-open ()
450 (goto-char (point-min))
451 (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
452 (setq webmail-aux (match-string 1))
453 (error "Can't find login url (open@1)")))
455 (defun webmail-yahoo-login ()
456 (goto-char (point-min))
457 (if (re-search-forward "http://[a-zA-Z][0-9]\\.mail\\.yahoo\\.com/" nil t)
458 (setq webmail-aux (match-string 0))
459 (error "Can't find login url (login@1)"))
460 (if (re-search-forward "YY=[0-9]+" nil t)
461 (setq webmail-aux (concat webmail-aux "ym/us/ShowFolder?"
463 (error "Can't find login url (login@2)")))
465 (defun webmail-yahoo-list ()
466 (let (url (newp t) (tofetch 0))
467 (goto-char (point-min))
468 (when (re-search-forward
469 "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t)
470 ;;(setq listed (match-string 1))
471 (message "Found %s mail(s)" (match-string 2)))
472 (if (string-match "http://[^/]+" webmail-aux)
473 (setq webmail-aux (match-string 0 webmail-aux))
474 (error "Can't find server url (list@1)"))
475 (goto-char (point-min))
476 (while (re-search-forward
477 "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/us/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
479 (if (setq url (match-string 1))
481 (when (or newp (not webmail-newmail-only))
482 (push (cons (match-string 2) (concat webmail-aux url "&toc=1"))
484 (setq tofetch (1+ tofetch)))
487 (message "Fetching %d mail(s)" tofetch)))
489 (defun webmail-yahoo-article (file id)
492 (goto-char (point-min))
493 (if (not (search-forward "value=\"Done\"" nil t))
494 (error "Can't find start label (article@1)"))
495 (if (not (search-forward "<table" nil t))
496 (error "Can't find start label (article@2)"))
497 (delete-region (point-min) (match-beginning 0))
498 (if (not (search-forward "</table>" nil t))
499 (error "Can't find start label (article@3)"))
500 (narrow-to-region (point-min) (match-end 0))
501 (while (search-forward "<a href=" nil t)
502 (setq p (match-beginning 0))
503 (search-forward "</a>" nil t)
504 (delete-region p (match-end 0)))
505 (nnweb-remove-markup)
506 (nnweb-decode-entities)
507 (goto-char (point-min))
509 (goto-char (point-max))
513 (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
514 (setq attachment (match-string 0))
515 (let (bufname ct ctl cd description)
516 (if (not (search-forward "<table" nil t))
517 (error "Can't find start label (article@4)"))
518 (delete-region p (match-beginning 0))
519 (if (not (search-forward "</table>" nil t))
520 (error "Can't find start label (article@5)"))
521 (narrow-to-region p (match-end 0))
522 (nnweb-remove-markup)
523 (nnweb-decode-entities)
524 (goto-char (point-min))
526 (setq ct (mail-fetch-field "content-type")
527 ctl (ignore-errors (mail-header-parse-content-type ct))
528 ;;cte (mail-fetch-field "content-transfer-encoding")
529 cd (mail-fetch-field "content-disposition")
530 description (mail-fetch-field "content-description")
531 id (mail-fetch-field "content-id"))
532 (delete-region (point-min) (point-max))
535 (set-buffer (generate-new-buffer " *webmail-att*"))
536 (nnweb-insert (concat webmail-aux attachment))
537 (push (current-buffer) webmail-buffer-list)
538 (setq bufname (buffer-name)))
540 (if (and ctl (not (equal (car ctl) "text/")))
541 (insert " type=\"" (car ctl) "\""))
542 (insert " buffer=\"" bufname "\"")
544 (insert " disposition=\"" cd "\""))
546 (insert " description=\"" description "\""))
547 (insert "><#/part>\n")
549 (delete-region p (point-max))
550 (goto-char (point-min))
551 ;; Some blank line to seperate mails.
552 (insert "\n\nFrom nobody " (current-time-string) "\n")
554 (insert "Message-ID: <" id "@yahoo.com>\n"))
555 (unless (looking-at "$")
556 (search-forward "\n\n" nil t)
558 (narrow-to-region (point) (point-max))
559 (insert "MIME-Version: 1.0\n"
562 (delete-region (point-min) (point-max))))
563 (goto-char (point-min))
565 (let (case-fold-search)
566 (while (re-search-forward "^From " nil t)
569 (mm-append-to-file (point-min) (point-max) file)))
573 (defun webmail-netaddress-open ()
574 (goto-char (point-min))
575 (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
576 (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
577 (error "Can't find login url (open@1)")))
579 (defun webmail-netaddress-login ()
580 (webmail-refresh-redirect)
581 (goto-char (point-min))
582 (if (re-search-forward "tpl/[^/]+/\\([^/]+\\)" nil t)
583 (setq webmail-session (match-string 1))
584 (error "Can't find login url (login@1)")))
586 (defun webmail-netaddress-list ()
588 (goto-char (point-min))
589 (when (re-search-forward
590 "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t)
591 (message "Found %s mail(s), %s unread"
592 (match-string 2) (match-string 1)))
593 (goto-char (point-min))
594 (while (re-search-forward
595 "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
596 (if (setq id (match-string 2))
599 (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
600 (car webmail-open-url)
601 webmail-session id)))
602 (if (or (not webmail-newmail-only)
603 (equal (match-string 1) "True"))
604 (push item webmail-articles))))))
606 (defun webmail-netaddress-single-part ()
607 (goto-char (point-min))
609 ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
612 (while (re-search-forward "[\t\040\r\n]+" nil t)
614 (goto-char (point-min))
615 (while (re-search-forward "<br>" nil t)
616 (replace-match "\n"))
617 (nnweb-remove-markup)
618 (nnweb-decode-entities)
621 (insert "<#part type=\"text/html\" disposition=inline>")
622 (goto-char (point-max))
626 (defun webmail-netaddress-article (file id)
627 (let (p p1 attachment count mime type)
629 (goto-char (point-min))
630 (if (not (search-forward "Trash" nil t))
631 (error "Can't find start label (article@1)"))
632 (if (not (search-forward "<form>" nil t))
633 (error "Can't find start label (article@2)"))
634 (delete-region (point-min) (match-beginning 0))
635 (if (not (search-forward "</form>" nil t))
636 (error "Can't find start label (article@3)"))
637 (narrow-to-region (point-min) (match-end 0))
638 (goto-char (point-min))
639 (while (re-search-forward "[\040\t\r\n]+" nil t)
641 (goto-char (point-min))
642 (while (search-forward "<b>" nil t)
643 (replace-match "\n"))
644 (nnweb-remove-markup)
645 (nnweb-decode-entities)
646 (goto-char (point-min))
648 (goto-char (point-min))
649 (while (re-search-forward "^\040+\\|\040+$" nil t)
651 (goto-char (point-min))
652 (while (re-search-forward "\040+" nil t)
654 (goto-char (point-max))
658 (unless (search-forward "<!-- Data -->" nil t)
659 (error "Can't find start label (article@4)"))
661 (delete-region p (point))
662 (goto-char (point-max))
663 (unless (re-search-backward
664 "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
665 (error "Can't find end label (article@5)"))
666 (delete-region (point) (point-max))
668 (while (search-forward
669 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
672 (unless (search-forward "</TABLE>" nil t)
673 (error "Can't find end label (article@6)"))
675 (if (search-backward "<IMG " p t)
677 (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
678 (error "Can't find tag (article@7)"))
679 (setq attachment (match-string 1))
680 (setq type (match-string 2))
681 (unless (search-forward "</TABLE>" nil t)
682 (error "Can't find end label (article@8)"))
683 (delete-region p (point))
684 (let (bufname);; Attachment
686 (set-buffer (generate-new-buffer " *webmail-att*"))
687 (nnweb-insert (concat (car webmail-open-url) attachment))
688 (push (current-buffer) webmail-buffer-list)
689 (setq bufname (buffer-name)))
690 (insert "<#part type=" type)
691 (insert " buffer=\"" bufname "\"")
692 (insert " disposition=\"inline\"")
693 (insert "><#/part>\n")
699 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
703 (webmail-netaddress-single-part)
704 (goto-char (point-max))
708 (narrow-to-region p (point-max))
709 (setq mime (webmail-netaddress-single-part))
711 (goto-char (point-min))
712 ;; Some blank line to seperate mails.
713 (insert "\n\nFrom nobody " (current-time-string) "\n")
715 (insert "Message-ID: <" id "@usa.net>\n"))
716 (unless (looking-at "$")
717 (search-forward "\n\n" nil t)
720 (narrow-to-region (point-min) (point))
721 (goto-char (point-min))
723 (if (looking-at "MIME-Version\\|Content-Type")
724 (delete-region (point)
727 (if (re-search-forward "^[^ \t]" nil t)
728 (goto-char (match-beginning 0))
731 (goto-char (point-max))
733 (narrow-to-region (point) (point-max))
734 (insert "MIME-Version: 1.0\n"
737 (delete-region (point-min) (point-max))))
738 (goto-char (point-min))
740 (let (case-fold-search)
741 (while (re-search-forward "^From " nil t)
744 (mm-append-to-file (point-min) (point-max) file)))
748 ;;; webmail.el ends here