Fix yahoo mail.
[gnus] / lisp / webmail.el
1 ;;; webmail.el --- interfacing with web mail
2 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: hotmail netaddress my-deja netscape
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Note: 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
28 ;; `url'.
29
30 ;; Todo: To support more web mail servers.
31
32 ;; Known bugs: 
33 ;; 1. Net@ddress may corrupt `X-Face'.
34
35 ;; Warning:
36 ;; Webmail is an experimental function, which means NO WARRANTY.
37
38 ;;; Code:
39
40 (eval-when-compile (require 'cl))
41
42 (require 'nnoo)
43 (require 'message)
44 (require 'gnus-util)
45 (require 'gnus)
46 (require 'nnmail)
47 (require 'mm-util)
48 (require 'mml)
49 (eval-when-compile
50   (ignore-errors
51     (require 'w3)
52     (require 'url)
53     (require 'url-cookie)
54     (require 'w3-forms)
55     (require 'nnweb)))
56 ;; Report failure to find w3 at load time if appropriate.
57 (eval '(progn
58          (require 'w3)
59          (require 'url)
60          (require 'url-cookie)
61          (require 'w3-forms)
62          (require 'nnweb)))
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&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       "%s&U2_USERNAME=%s&U2_PASSWORD=%s"
130       webmail-aux user password)
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-url "http://www.my-deja.com/")
145      (open-snarf . webmail-my-deja-open)
146      (login-url
147       content 
148       ("%s" webmail-aux)
149       "member_name=%s&pw=%s&go=&priv_opt_MyDeja99="
150       user password)
151      (list-url "http://www.deja.com/rg_gotomail.xp")
152      (list-snarf . webmail-my-deja-list)
153      (article-snarf . webmail-my-deja-article)
154      (trash-url 
155       "%s/gmm_multiplex.femail?%%2Fgmm_domovemesg_top.femail=Move+to%%3A&folder_top=%s%%3Azzz%%3A%%7E6trash%%3AF%%3A0&docid=%s"
156       webmail-aux user id))))
157
158 (defvar webmail-variables
159   '(address article-snarf article-url list-snarf list-url 
160             login-url login-snarf open-url open-snarf site articles
161             post-process paranoid trash-url))
162
163 (defconst webmail-version "webmail 1.0")
164
165 (defvar webmail-newmail-only nil
166   "Only fetch new mails.")
167
168 (defvar webmail-move-to-trash-can t
169   "Move mail to trash can after fetch it.")
170
171 ;;; Internal variables
172
173 (defvar webmail-address nil)
174 (defvar webmail-paranoid nil)
175 (defvar webmail-aux nil)
176 (defvar webmail-session nil)
177 (defvar webmail-article-snarf nil)
178 (defvar webmail-article-url nil)
179 (defvar webmail-list-snarf nil)
180 (defvar webmail-list-url nil)
181 (defvar webmail-login-url nil)
182 (defvar webmail-login-snarf nil)
183 (defvar webmail-open-snarf nil)
184 (defvar webmail-open-url nil)
185 (defvar webmail-trash-url nil)
186 (defvar webmail-articles nil)
187 (defvar webmail-post-process nil)
188
189 (defvar webmail-buffer nil)
190 (defvar webmail-buffer-list nil)
191
192 (defvar webmail-type nil)
193
194 (defvar webmail-error-function nil)
195
196 (defvar webmail-debug-file "~/.emacs-webmail-debug")
197
198 ;;; Interface functions
199
200 (defun webmail-debug (str)
201   (with-temp-buffer
202     (insert "\n---------------- A bug at " str " ------------------\n")
203     (mapcar #'(lambda (sym) 
204                 (if (boundp sym)
205                     (pp `(setq ,sym ',(eval sym)) (current-buffer))))
206             '(webmail-type user))
207     (insert "---------------- webmail buffer ------------------\n\n")
208     (insert-buffer-substring webmail-buffer)
209     (insert "\n---------------- end of buffer ------------------\n\n")
210     (append-to-file (point-min) (point-max) webmail-debug-file)))
211
212 (defun webmail-error (str)
213   (if webmail-error-function
214       (funcall webmail-error-function str))
215   (message "%s HTML has changed; please get a new version of webmail (%s)"
216            webmail-type str)
217   (error "%s HTML has changed; please get a new version of webmail (%s)"
218          webmail-type str))
219
220 (defun webmail-setdefault (type)
221   (let ((type-def (cdr (assq type webmail-type-definition)))
222         (vars webmail-variables)
223         pair)
224     (setq webmail-type type)
225     (dolist (var vars)
226       (if (setq pair (assq var type-def))
227           (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
228         (set (intern (concat "webmail-" (symbol-name var))) nil)))))
229
230 (defun webmail-encode-www-form-urlencoded (pairs)
231   "Return PAIRS encoded for forms."
232   (mapconcat
233    (function
234     (lambda (data)
235       (concat (w3-form-encode-xwfu (car data)) "="
236               (w3-form-encode-xwfu (cdr data)))))
237    pairs "&"))
238
239 (defun webmail-fetch-simple (url content)
240   (let ((url-request-data content)
241         (url-request-method "POST")
242         (url-request-extra-headers
243          '(("Content-type" . "application/x-www-form-urlencoded"))))
244     (nnweb-insert url))
245   t)
246
247 (defun webmail-fetch-form (url pairs)
248   (let ((url-request-data (webmail-encode-www-form-urlencoded pairs))
249         (url-request-method "POST")
250         (url-request-extra-headers
251          '(("Content-type" . "application/x-www-form-urlencoded"))))
252     (nnweb-insert url))
253   t)
254
255 (defun webmail-eval (expr)
256   (cond
257    ((consp expr)
258     (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
259    ((symbolp expr)
260     (eval expr))
261    (t
262     expr)))
263
264 (defun webmail-url (xurl)
265   (mm-with-unibyte-current-buffer
266     (cond 
267      ((eq (car xurl) 'content)
268       (pop xurl)
269       (webmail-fetch-simple (if (stringp (car xurl))
270                                 (car xurl)
271                               (apply 'format (webmail-eval (car xurl))))
272                             (apply 'format (webmail-eval (cdr xurl)))))
273      ((eq (car xurl) 'post)
274       (pop xurl)
275       (webmail-fetch-form (car xurl) (webmail-eval (cdr xurl))))
276      (t
277       (nnweb-insert (apply 'format (webmail-eval xurl)))))))
278   
279 (defun webmail-init ()
280   "Initialize buffers and such."
281   (if (gnus-buffer-live-p webmail-buffer)
282       (set-buffer webmail-buffer)
283     (setq webmail-buffer
284           (mm-with-unibyte
285             (nnheader-set-temp-buffer " *webmail*")))))
286
287 (defvar url-package-name)
288 (defvar url-package-version)
289 (defvar url-cookie-multiple-line)
290 (defvar url-confirmation-func)
291
292 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
293 ;;
294 ;; Netscape uses "GET" as redirect method when orignal method is POST
295 ;; and status is 302, .i.e no security risks by default without
296 ;; confirmation.
297 ;;
298 ;; Some web servers (at least Apache used by yahoo) return status 302
299 ;; instead of 303, though they mean 303.
300
301 (defun webmail-url-confirmation-func (prompt)
302   (cond 
303    ((equal prompt (concat "Honor redirection with non-GET method "
304                           "(possible security risks)? "))
305     nil)
306    ((equal prompt "Continue (with method of GET)? ")
307     t)
308    (t (error prompt))))
309
310 (defun webmail-refresh-redirect ()
311   "Redirect refresh url in META."
312   (goto-char (point-min))
313   (while (re-search-forward 
314           "HTTP-EQUIV=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
315           nil t)
316     (let ((url (match-string 1)))
317       (erase-buffer)
318       (mm-with-unibyte-current-buffer
319         (nnweb-insert url)))
320     (goto-char (point-min))))
321
322 (defun webmail-fetch (file subtype user password)
323   (save-excursion
324     (webmail-setdefault subtype)
325     (let ((url-package-name (if (memq 'agent webmail-paranoid)
326                                 "Mozilla"
327                               url-package-name))
328           (url-package-version (if (memq 'agent webmail-paranoid)
329                                    "4.0"
330                                  url-package-version))
331           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
332                                         nil
333                                       url-cookie-multiple-line))
334           (url-confirmation-func (if (memq 'post webmail-paranoid)
335                                      'webmail-url-confirmation-func
336                                    url-confirmation-func))
337           url-cookie-storage url-cookie-secure-storage
338           url-cookie-confirmation
339           item id (n 0))
340       (webmail-init)
341       (setq webmail-articles nil)
342       (when webmail-open-url 
343         (erase-buffer)
344         (webmail-url webmail-open-url))
345       (if webmail-open-snarf (funcall webmail-open-snarf))
346       (when webmail-login-url 
347         (erase-buffer)
348         (webmail-url webmail-login-url))
349       (if webmail-login-snarf 
350           (funcall webmail-login-snarf))
351       (when webmail-list-url 
352         (erase-buffer)
353         (webmail-url webmail-list-url))
354       (if webmail-list-snarf 
355           (funcall webmail-list-snarf))
356       (while (setq item (pop webmail-articles))
357         (message "Fetching mail #%d..." (setq n (1+ n)))
358         (erase-buffer)
359         (mm-with-unibyte-current-buffer
360           (nnweb-insert (cdr item)))
361         (setq id (car item))
362         (if webmail-article-snarf 
363             (funcall webmail-article-snarf file id))
364         (when (and webmail-trash-url webmail-move-to-trash-can)
365           (message "Move mail #%d to trash can..." n)
366           (condition-case err
367               (progn
368                 (webmail-url webmail-trash-url)
369                 (let (buf)
370                   (while (setq buf (pop webmail-buffer-list))
371                     (kill-buffer buf))))
372             (error 
373              (let (buf)
374                (while (setq buf (pop webmail-buffer-list))
375                  (kill-buffer buf)))
376              (error err))))))
377     (if webmail-post-process
378         (funcall webmail-post-process))))
379
380 (defun webmail-encode-8bit ()
381   (goto-char (point-min))
382   (skip-chars-forward "^\200-\377")
383   (while (not (eobp))
384     (insert (format "&%d;" (mm-char-int (char-after))))
385     (delete-char 1)
386     (skip-chars-forward "^\200-\377")))
387
388 ;;; hotmail
389
390 (defun webmail-hotmail-open ()
391   (goto-char (point-min))
392   (if (re-search-forward 
393        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
394       (setq webmail-aux (match-string 1))
395     (webmail-error "open@1")))
396
397 (defun webmail-hotmail-login ()
398   (let (site)
399     (goto-char (point-min))
400     (if (re-search-forward 
401          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
402         (setq site (match-string 1))
403       (webmail-error "login@1"))
404     (goto-char (point-min))
405     (if (re-search-forward 
406          "\\(/cgi-bin/HoTMaiL\\?[^\"]*curmbox=ACTIVE[^\"]*\\)" nil t)
407         (setq webmail-aux (concat "http://" site (match-string 1)))
408       (webmail-error "login@2"))))
409
410 (defun webmail-hotmail-list ()
411   (let (site url newp)
412     (goto-char (point-min))
413     (if (re-search-forward "[0-9]+ new" nil t) 
414         (message "Found %s" (match-string 0))
415       (webmail-error "maybe your w3 version is too old"))
416     (goto-char (point-min))
417     (if (re-search-forward 
418          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
419         (setq site (match-string 1))
420       (webmail-error "list@1"))
421     (goto-char (point-min))
422     (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
423         (setq webmail-aux 
424               (concat "http://" site "/cgi-bin/HoTMaiL?disk=" 
425                       (match-string 1)))
426       (webmail-error "list@2"))
427     (goto-char (point-max))
428     (while (re-search-backward 
429             "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\"" 
430             nil t)
431       (if (setq url (match-string 1))
432           (progn
433             (if (or newp (not webmail-newmail-only))
434                 (let (id)
435                   (if (string-match "msg=\\([^&]+\\)" url)
436                       (setq id (match-string 1 url)))
437                   (push (cons id (concat "http://" site url "&raw=0")) 
438                         webmail-articles)))
439             (setq newp nil))
440         (setq newp t)))))
441
442 ;; Thank victor@idaccr.org (Victor S. Miller) for raw=0
443
444 (defun webmail-hotmail-article (file id)
445   (goto-char (point-min))
446   (if (not (search-forward "<pre>" nil t))
447       (webmail-error "article@3"))
448   (skip-chars-forward "\n\r\t ")
449   (delete-region (point-min) (point))
450   (if (not (search-forward "</pre>" nil t))
451       (webmail-error "article@3.1"))
452   (delete-region (match-beginning 0) (point-max))
453   (nnweb-remove-markup)
454   (nnweb-decode-entities)
455   (goto-char (point-min))
456   (while (re-search-forward "\r\n?" nil t)
457     (replace-match "\n"))
458   (goto-char (point-min))
459   (insert "\n\n")
460   (if (not (looking-at "\n*From "))
461       (insert "From nobody " (current-time-string) "\n"))
462   (mm-append-to-file (point-min) (point-max) file))
463
464 (defun webmail-hotmail-article-old (file id)
465   (let (p attachment count mime hotmail-direct)
466     (save-restriction
467       (webmail-encode-8bit)
468       (goto-char (point-min))
469       (if (not (search-forward "<DIV>" nil t))
470           (if (not (search-forward "Reply&nbsp;All" nil t))
471               (webmail-error "article@1")
472             (setq hotmail-direct t))
473         (goto-char (match-beginning 0)))
474       (narrow-to-region (point-min) (point))
475       (if (not (search-backward "<table" nil t 2))
476           (webmail-error "article@1.1"))
477       (delete-region (point-min) (match-beginning 0)) 
478       (while (search-forward "<a href=" nil t)
479         (setq p (match-beginning 0))
480         (search-forward "</a>" nil t)
481         (delete-region p (match-end 0)))
482       (nnweb-remove-markup)
483       (nnweb-decode-entities)
484       (goto-char (point-min))
485       (delete-blank-lines)
486       (goto-char (point-min))
487       (when (search-forward "\n\n" nil t)
488         (backward-char)
489         (delete-region (point) (point-max)))
490       (goto-char (point-max))
491       (widen)
492       (insert "\n")
493       (setq p (point))
494       (while (re-search-forward 
495               "<tt>\\|<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" 
496               nil t)
497         (if (setq attachment (match-string 1))
498             (let ((filename (match-string 2))
499                   bufname);; Attachment
500               (delete-region p (match-end 0))
501               (save-excursion
502                 (set-buffer (generate-new-buffer " *webmail-att*"))
503                 (nnweb-insert attachment)
504                 (push (current-buffer) webmail-buffer-list)
505                 (setq bufname (buffer-name)))
506               (setq mime t)
507               (insert "<#part type=" 
508                       (or (and filename
509                                (string-match "\\.[^\\.]+$" filename)
510                                (mailcap-extension-to-mime
511                                 (match-string 0 filename)))
512                           "application/octet-stream"))
513               (insert " buffer=\"" bufname "\"")
514               (insert " filename=\"" filename "\"")
515               (insert " disposition=\"inline\"")
516               (insert "><#/part>\n")
517               (setq p (point)))
518           (delete-region p (match-end 0))
519           (if hotmail-direct
520               (if (not (search-forward "</tt>" nil t))
521                   (webmail-error "article@1.2")
522                 (delete-region (match-beginning 0) (match-end 0)))
523             (setq count 1)
524             (while (and (> count 0) 
525                         (re-search-forward "</div>\\|\\(<div>\\)" nil t))
526               (if (match-string 1)
527                   (setq count (1+ count))
528                 (if (= (setq count (1- count)) 0)
529                     (delete-region (match-beginning 0)
530                                    (match-end 0))))))
531           (narrow-to-region p (point))
532           (goto-char (point-min))
533           (cond 
534            ((looking-at "<pre>")
535             (goto-char (match-end 0))
536             (if (looking-at "$") (forward-char))
537             (delete-region (point-min) (point))
538             (nnweb-remove-markup)
539             (nnweb-decode-entities)
540             nil)
541            (t
542             (setq mime t)
543             (insert "<#part type=\"text/html\" disposition=inline>")
544             (goto-char (point-max))
545             (insert "<#/part>")))
546           (goto-char (point-max))
547           (setq p (point))
548           (widen)))
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")
553       (if id
554           (insert (format "Message-ID: <%s@hotmail.com>\n" id)))
555       (unless (looking-at "$") 
556         (if (search-forward "\n\n" nil t)
557             (forward-line -1)
558           (webmail-error "article@2")))
559       (narrow-to-region (point) (point-max))
560       (if mime
561           (insert "MIME-Version: 1.0\n"
562                   (prog1
563                       (mml-generate-mime)
564                     (delete-region (point-min) (point-max)))))
565       (goto-char (point-min))
566       (widen)
567       (let (case-fold-search)
568         (while (re-search-forward "^From " nil t)
569           (beginning-of-line)
570           (insert ">"))))
571     (mm-append-to-file (point-min) (point-max) file)))
572
573 ;;; yahoo
574
575 (defun webmail-yahoo-open ()
576   (goto-char (point-min))
577   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
578       (setq webmail-aux (match-string 1))
579     (webmail-error "open@1")))
580
581 (defun webmail-yahoo-login ()
582   (goto-char (point-min))
583   (if (re-search-forward "http://[^/]+[0-9]\\.mail\\.yahoo\\.com/" nil t)
584       (setq webmail-aux (match-string 0))
585     (webmail-error "login@1"))
586   (if (re-search-forward "YY=[0-9]+" nil t)
587       (setq webmail-aux (concat webmail-aux "ym/ShowFolder?"
588                                 (match-string 0)))
589     (webmail-error "login@2")))
590
591 (defun webmail-yahoo-list ()
592   (let (url (newp t) (tofetch 0))
593     (goto-char (point-min))
594     (when (re-search-forward 
595            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
596       ;;(setq listed (match-string 1))
597       (message "Found %s mail(s)" (match-string 2)))
598     (if (string-match "http://[^/]+" webmail-aux)
599         (setq webmail-aux (match-string 0 webmail-aux))
600       (webmail-error "list@1"))
601     (goto-char (point-min))
602     (while (re-search-forward 
603             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
604             nil t)
605       (if (setq url (match-string 1))
606           (progn
607             (when (or newp (not webmail-newmail-only))
608               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
609                     webmail-articles)
610               (setq tofetch (1+ tofetch)))
611             (setq newp t))
612         (setq newp nil)))
613     (setq webmail-articles (nreverse webmail-articles))
614     (message "Fetching %d mail(s)" tofetch)))
615
616 (defun webmail-yahoo-article (file id)
617   (let (p attachment)
618     (save-restriction
619       (goto-char (point-min))
620       (if (not (search-forward "value=\"Done\"" nil t))
621           (webmail-error "article@1"))
622       (if (not (search-forward "<table" nil t))
623           (webmail-error "article@2"))
624       (delete-region (point-min) (match-beginning 0)) 
625       (if (not (search-forward "</table>" nil t))
626           (webmail-error "article@3"))
627       (narrow-to-region (point-min) (match-end 0))
628       (while (search-forward "<a href=" nil t)
629         (setq p (match-beginning 0))
630         (search-forward "</a>" nil t)
631         (delete-region p (match-end 0)))
632       (nnweb-remove-markup)
633       (nnweb-decode-entities)
634       (goto-char (point-min))
635       (delete-blank-lines)
636       (goto-char (point-max))
637       (widen)
638       (insert "\n")
639       (setq p (point))
640       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
641         (setq attachment (match-string 0))
642         (let (bufname ct ctl cd description)
643           (if (not (search-forward "<table" nil t))
644               (webmail-error "article@4"))
645           (delete-region p (match-beginning 0))
646           (if (not (search-forward "</table>" nil t))
647               (webmail-error "article@5"))
648           (narrow-to-region p (match-end 0))
649           (nnweb-remove-markup)
650           (nnweb-decode-entities)
651           (goto-char (point-min))
652           (delete-blank-lines)
653           (setq ct (mail-fetch-field "content-type")
654                 ctl (ignore-errors (mail-header-parse-content-type ct))
655                 ;;cte (mail-fetch-field "content-transfer-encoding")
656                 cd (mail-fetch-field "content-disposition")
657                 description (mail-fetch-field "content-description")
658                 id (mail-fetch-field "content-id"))
659           (delete-region (point-min) (point-max))
660           (widen)
661           (save-excursion
662             (set-buffer (generate-new-buffer " *webmail-att*"))
663             (nnweb-insert (concat webmail-aux attachment))
664             (push (current-buffer) webmail-buffer-list)
665             (setq bufname (buffer-name)))
666           (insert "<#part")
667           (if (and ctl (not (equal (car ctl) "text/")))
668               (insert " type=\"" (car ctl) "\""))
669           (insert " buffer=\"" bufname "\"")
670           (if cd
671               (insert " disposition=\"" cd "\""))
672           (if description
673               (insert " description=\"" description "\""))
674           (insert "><#/part>\n")
675           (setq p (point))))
676       (delete-region p (point-max))
677       (goto-char (point-min))
678       ;; Some blank line to seperate mails.
679       (insert "\n\nFrom nobody " (current-time-string) "\n")
680       (if id
681           (insert (format "Message-ID: <%s@yahoo.com>\n" id)))
682       (unless (looking-at "$") 
683         (if (search-forward "\n\n" nil t)
684             (forward-line -1)
685           (webmail-error "article@2")))
686       (narrow-to-region (point) (point-max))
687       (insert "MIME-Version: 1.0\n"
688               (prog1
689                   (mml-generate-mime)
690                 (delete-region (point-min) (point-max))))
691       (goto-char (point-min))
692       (widen)
693       (let (case-fold-search)
694         (while (re-search-forward "^From " nil t)
695           (beginning-of-line)
696           (insert ">"))))
697     (mm-append-to-file (point-min) (point-max) file)))
698
699 ;;; netaddress
700
701 (defun webmail-netscape-open ()
702   (goto-char (point-min))
703   (if (re-search-forward "login/hint\\?\\([^\"]+\\)\"" nil t)
704       (setq webmail-aux (match-string 1))
705     (webmail-error "open@1")))
706
707 (defun webmail-netaddress-open ()
708   (goto-char (point-min))
709   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
710       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
711     (webmail-error "open@1")))
712
713 (defun webmail-netaddress-login ()
714   (webmail-refresh-redirect)
715   (goto-char (point-min))
716   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
717       (setq webmail-session (match-string 1))
718     (webmail-error "login@1")))
719
720 (defun webmail-netaddress-list ()
721   (let (item id)
722     (goto-char (point-min))
723     (when (re-search-forward 
724            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
725       (message "Found %s mail(s), %s unread" 
726                (match-string 2) (match-string 1)))
727     (goto-char (point-min))
728     (while (re-search-forward 
729             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
730       (if (setq id (match-string 2))
731           (setq item 
732                 (cons id 
733                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
734                               (car webmail-article-url)
735                               webmail-session id)))
736         (if (or (not webmail-newmail-only)
737                 (equal (match-string 1) "True"))
738             (push item webmail-articles))))
739     (setq webmail-articles (nreverse webmail-articles))))
740
741 (defun webmail-netaddress-single-part ()
742   (goto-char (point-min))
743   (cond 
744    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
745     ;; text/plain
746     (replace-match "")
747     (while (re-search-forward "[\t\040\r\n]+" nil t)
748       (replace-match " "))
749     (goto-char (point-min))
750     (while (re-search-forward "<br>" nil t)
751       (replace-match "\n"))
752     (nnweb-remove-markup)
753     (nnweb-decode-entities) 
754     nil)
755    (t
756     (insert "<#part type=\"text/html\" disposition=inline>")
757     (goto-char (point-max))
758     (insert "<#/part>")
759     t)))
760
761 (defun webmail-netaddress-article (file id)
762   (let (p p1 attachment count mime type)
763     (save-restriction
764       (webmail-encode-8bit)
765       (goto-char (point-min))
766       (if (not (search-forward "Trash" nil t))
767           (webmail-error "article@1"))
768       (if (not (search-forward "<form>" nil t))
769           (webmail-error "article@2"))
770       (delete-region (point-min) (match-beginning 0)) 
771       (if (not (search-forward "</form>" nil t))
772           (webmail-error "article@3"))
773       (narrow-to-region (point-min) (match-end 0))
774       (goto-char (point-min))
775       (while (re-search-forward "[\040\t\r\n]+" nil t)
776         (replace-match " "))
777       (goto-char (point-min))
778       (while (search-forward "<b>" nil t)
779         (replace-match "\n"))
780       (nnweb-remove-markup)
781       (nnweb-decode-entities)
782       (goto-char (point-min))
783       (delete-blank-lines)
784       (goto-char (point-min))
785       (while (re-search-forward "^\040+\\|\040+$" nil t)
786         (replace-match ""))
787       (goto-char (point-min))
788       (while (re-search-forward "\040+" nil t)
789         (replace-match " "))
790       (goto-char (point-max))
791       (widen)
792       (insert "\n\n")
793       (setq p (point))
794       (unless (search-forward "<!-- Data -->" nil t)
795         (webmail-error "article@4"))
796       (forward-line 14)
797       (delete-region p (point))
798       (goto-char (point-max))
799       (unless (re-search-backward 
800                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
801         (webmail-error "article@5"))
802       (delete-region (point) (point-max))
803       (goto-char p)
804       (while (search-forward
805               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
806               nil t 2)
807         (setq mime t)
808         (unless (search-forward "</TABLE>" nil t)
809           (webmail-error "article@6"))
810         (setq p1 (point))
811         (if (search-backward "<IMG " p t)
812             (progn
813               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
814                 (webmail-error "article@7"))
815               (setq attachment (match-string 1))
816               (setq type (match-string 2))
817               (unless (search-forward "</TABLE>" nil t)
818                 (webmail-error "article@8"))
819               (delete-region p (point))
820               (let (bufname);; Attachment
821                 (save-excursion
822                   (set-buffer (generate-new-buffer " *webmail-att*"))
823                   (nnweb-insert (concat (car webmail-open-url) attachment))
824                   (push (current-buffer) webmail-buffer-list)
825                   (setq bufname (buffer-name)))
826                 (insert "<#part type=" type)
827                 (insert " buffer=\"" bufname "\"")
828                 (insert " disposition=\"inline\"")
829                 (insert "><#/part>\n")
830                 (setq p (point))))
831           (delete-region p p1)
832           (narrow-to-region 
833            p
834            (if (search-forward 
835                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
836                 nil t)
837                (match-beginning 0)
838              (point-max)))
839           (webmail-netaddress-single-part)
840           (goto-char (point-max))
841           (setq p (point))
842           (widen)))
843       (unless mime
844         (narrow-to-region p (point-max))
845         (setq mime (webmail-netaddress-single-part))
846         (widen))
847       (goto-char (point-min))
848       ;; Some blank line to seperate mails.
849       (insert "\n\nFrom nobody " (current-time-string) "\n")
850       (if id
851           (insert (format "Message-ID: <%s@%s>\n" id webmail-address)))
852       (unless (looking-at "$") 
853         (if (search-forward "\n\n" nil t)
854             (forward-line -1)
855           (webmail-error "article@2")))
856       (when mime
857         (narrow-to-region (point-min) (point))
858         (goto-char (point-min))
859         (while (not (eobp))
860           (if (looking-at "MIME-Version\\|Content-Type")
861               (delete-region (point) 
862                              (progn
863                                (forward-line 1)
864                                (if (re-search-forward "^[^ \t]" nil t)
865                                    (goto-char (match-beginning 0))
866                                  (point-max))))
867             (forward-line 1)))
868         (goto-char (point-max))
869         (widen)
870         (narrow-to-region (point) (point-max))
871         (insert "MIME-Version: 1.0\n"
872                 (prog1
873                     (mml-generate-mime)
874                   (delete-region (point-min) (point-max))))
875         (goto-char (point-min))
876         (widen))
877       (let (case-fold-search)
878         (while (re-search-forward "^From " nil t)
879           (beginning-of-line)
880           (insert ">"))))
881     (mm-append-to-file (point-min) (point-max) file)))
882
883 (defun webmail-netscape-article (file id)
884   (let (p p1 attachment count mime type)
885     (save-restriction
886       (webmail-encode-8bit)
887       (goto-char (point-min))
888       (if (not (search-forward "Trash" nil t))
889           (webmail-error "article@1"))
890       (if (not (search-forward "<form>" nil t))
891           (webmail-error "article@2"))
892       (delete-region (point-min) (match-beginning 0)) 
893       (if (not (search-forward "</form>" nil t))
894           (webmail-error "article@3"))
895       (narrow-to-region (point-min) (match-end 0))
896       (goto-char (point-min))
897       (while (re-search-forward "[\040\t\r\n]+" nil t)
898         (replace-match " "))
899       (goto-char (point-min))
900       (while (re-search-forward "<a href=[^>]*>[^<]*</a>" nil t)
901         (replace-match ""))
902       (goto-char (point-min))
903       (while (search-forward "<b>" nil t)
904         (replace-match "\n"))
905       (nnweb-remove-markup)
906       (nnweb-decode-entities)
907       (goto-char (point-min))
908       (delete-blank-lines)
909       (goto-char (point-min))
910       (while (re-search-forward "^\040+\\|\040+$" nil t)
911         (replace-match ""))
912       (goto-char (point-min))
913       (while (re-search-forward "\040+" nil t)
914         (replace-match " "))
915       (goto-char (point-max))
916       (widen)
917       (insert "\n\n")
918       (setq p (point))
919       (unless (search-forward "<!-- Data -->" nil t)
920         (webmail-error "article@4"))
921       (forward-line 14)
922       (delete-region p (point))
923       (goto-char (point-max))
924       (unless (re-search-backward 
925                "<form name=\"Transfer2\"" p t)
926         (webmail-error "article@5"))
927       (delete-region (point) (point-max))
928       (goto-char p)
929       (while (search-forward
930               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
931               nil t 2)
932         (setq mime t)
933         (unless (search-forward "</TABLE>" nil t)
934           (webmail-error "article@6"))
935         (setq p1 (point))
936         (if (search-backward "<IMG " p t)
937             (progn
938               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
939                 (webmail-error "article@7"))
940               (setq attachment (match-string 1))
941               (setq type (match-string 2))
942               (unless (search-forward "</TABLE>" nil t)
943                 (webmail-error "article@8"))
944               (delete-region p (point))
945               (let (bufname);; Attachment
946                 (save-excursion
947                   (set-buffer (generate-new-buffer " *webmail-att*"))
948                   (nnweb-insert (concat (car webmail-open-url) attachment))
949                   (push (current-buffer) webmail-buffer-list)
950                   (setq bufname (buffer-name)))
951                 (insert "<#part type=" type)
952                 (insert " buffer=\"" bufname "\"")
953                 (insert " disposition=\"inline\"")
954                 (insert "><#/part>\n")
955                 (setq p (point))))
956           (delete-region p p1)
957           (narrow-to-region 
958            p
959            (if (search-forward 
960                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
961                 nil t)
962                (match-beginning 0)
963              (point-max)))
964           (webmail-netaddress-single-part)
965           (goto-char (point-max))
966           (setq p (point))
967           (widen)))
968       (unless mime
969         (narrow-to-region p (point-max))
970         (setq mime (webmail-netaddress-single-part))
971         (widen))
972       (goto-char (point-min))
973       ;; Some blank line to seperate mails.
974       (insert "\n\nFrom nobody " (current-time-string) "\n")
975       (if id
976           (insert (format "Message-ID: <%s@%s>\n" id webmail-address)))
977       (unless (looking-at "$") 
978         (if (search-forward "\n\n" nil t)
979             (forward-line -1)
980           (webmail-error "article@2")))
981       (when mime
982         (narrow-to-region (point-min) (point))
983         (goto-char (point-min))
984         (while (not (eobp))
985           (if (looking-at "MIME-Version\\|Content-Type")
986               (delete-region (point) 
987                              (progn
988                                (forward-line 1)
989                                (if (re-search-forward "^[^ \t]" nil t)
990                                    (goto-char (match-beginning 0))
991                                  (point-max))))
992             (forward-line 1)))
993         (goto-char (point-max))
994         (widen)
995         (narrow-to-region (point) (point-max))
996         (insert "MIME-Version: 1.0\n"
997                 (prog1
998                     (mml-generate-mime)
999                   (delete-region (point-min) (point-max))))
1000         (goto-char (point-min))
1001         (widen))
1002       (let (case-fold-search)
1003         (while (re-search-forward "^From " nil t)
1004           (beginning-of-line)
1005           (insert ">"))))
1006     (mm-append-to-file (point-min) (point-max) file)))
1007
1008 ;;; my-deja
1009
1010 (defun webmail-my-deja-open ()
1011   (webmail-refresh-redirect)
1012   (goto-char (point-min))
1013   (if (re-search-forward "action=\"\\([^\"]+login_confirm\\.xp[^\"]*\\)\"" 
1014                          nil t)
1015       (setq webmail-aux (match-string 1))
1016     (webmail-error "open@1")))
1017
1018 (defun webmail-my-deja-list ()
1019   (let (item id newp)
1020     (goto-char (point-min))
1021     (when (re-search-forward 
1022            "(\\([0-9]+\\) message(s), \\([0-9]+\\) new, \\([0-9]+\\)&nbsp;k )"
1023            nil t) 
1024       (message "Found %s mail(s), %s unread, total size %s K" 
1025                (match-string 1) (match-string 2) (match-string 3)))
1026     (goto-char (point-min))
1027     (while (re-search-forward 
1028             "&#149; &nbsp;&nbsp;\\|\\(http:[^\"]+\\)/display_seemesg\\.femail\\?docid=\\([^&\"]+\\)"
1029             nil t)
1030       (if (setq id (match-string 2))
1031           (when (or newp (not webmail-newmail-only))
1032             (push
1033              (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"
1034                               (match-string 1) id))
1035              webmail-articles)
1036             (setq webmail-aux (match-string 1))
1037             (setq newp nil))
1038         (setq newp t)))
1039     (setq webmail-articles (nreverse webmail-articles))))
1040
1041 (defun webmail-my-deja-article (file id)
1042   (let (url)
1043     (goto-char (point-min))
1044     (unless (re-search-forward "\\(http:[^\"]+/attachment/entire_message.txt[^\"]+\\)" nil t)
1045       (webmail-error "article@1"))
1046     (setq url (match-string 1))
1047     (erase-buffer)
1048     (mm-with-unibyte-current-buffer
1049       (nnweb-insert url))
1050     (goto-char (point-min))
1051     (while (search-forward "\r\n" nil t)
1052       (replace-match "\n"))
1053     (goto-char (point-min))
1054     (insert "\n\nFrom nobody " (current-time-string) "\n")
1055     (mm-append-to-file (point-min) (point-max) file)))
1056
1057 (provide 'webmail)
1058
1059 ;;; webmail.el ends here