Rewrite my-deja.
[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: Now mail.yahoo.com provides POP3 service, the webmail
27 ;; fetching is not going to be supported.
28
29 ;; Note: You need to have `url' and `w3' installed for this backend to
30 ;; work. `w3' must be 4.0pre46+one-line-cookie patch or standalone
31 ;; `url'.
32
33 ;; Todo: To support more web mail servers.
34
35 ;; Known bugs: 
36 ;; 1. Net@ddress may corrupt `X-Face'.
37
38 ;; Warning:
39 ;; Webmail is an experimental function, which means NO WARRANTY.
40
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
44
45 (require 'nnoo)
46 (require 'message)
47 (require 'gnus-util)
48 (require 'gnus)
49 (require 'nnmail)
50 (require 'mm-util)
51 (require 'mml)
52 (eval-when-compile
53   (ignore-errors
54     (require 'w3)
55     (require 'url)
56     (require 'url-cookie)
57     (require 'w3-forms)
58     (require 'nnweb)))
59 ;; Report failure to find w3 at load time if appropriate.
60 (eval '(progn
61          (require 'w3)
62          (require 'url)
63          (require 'url-cookie)
64          (require 'w3-forms)
65          (require 'nnweb)))
66
67 ;;;
68
69 (defvar webmail-type-definition
70   '((hotmail
71      ;; Hotmail hate other HTTP user agents and use one line cookie
72      (paranoid agent cookie post)
73      (address . "www.hotmail.com")
74      (open-url "http://www.hotmail.com/")
75      (open-snarf . webmail-hotmail-open)
76      ;; W3 hate redirect POST
77      (login-url
78       "http://%s/cgi-bin/dologin?login=%s&passwd=%s&enter=Sign+in&sec=no&curmbox=ACTIVE&_lang=&js=yes&id=2&tw=-10000&beta="
79       webmail-aux user password)
80      ;;(login-snarf . webmail-hotmail-login)
81      ;;(list-url "%s" webmail-aux)
82      (list-snarf . webmail-hotmail-list)
83      (article-snarf . webmail-hotmail-article)
84      (trash-url 
85       "%s&login=%s&f=33792&curmbox=ACTIVE&_lang=&foo=inbox&js=&page=&%s=on&_HMaction=MoveTo&tobox=trAsH&nullbox=" 
86       webmail-aux user id))
87     (yahoo
88      (paranoid agent cookie post)
89      (address . "mail.yahoo.com")
90      (open-url "http://mail.yahoo.com/")
91      (open-snarf . webmail-yahoo-open)
92      (login-url;; yahoo will not accept GET
93       content 
94       ("%s" webmail-aux)
95       ".tries=&.src=ym&.last=&promo=&.intl=&.bypass=&.partner=&.chkP=Y&.done=&login=%s&passwd=%s"
96       user password)
97      (login-snarf . webmail-yahoo-login)
98      (list-url "%s&rb=Inbox&YN=1" webmail-aux)
99      (list-snarf . webmail-yahoo-list)
100      (article-snarf . webmail-yahoo-article)
101      (trash-url 
102       "%s/ym/ShowFolder?YY=52107&inc=50&order=down&sort=date&pos=0&box=Inbox&DEL=Delete&destBox=&Mid=%s&destBox2="
103       webmail-aux id))
104     (netaddress
105      (paranoid cookie post)
106      (address . "www.netaddress.com")
107      (open-url "http://www.netaddress.com/")
108      (open-snarf . webmail-netaddress-open)
109      (login-url
110       content 
111       ("%s" webmail-aux)
112       "LoginState=2&SuccessfulLogin=%%2Ftpl&NewServerName=www.netaddress.com&JavaScript=JavaScript1.2&DomainID=4&NA31site=classic.netaddress.com&NA31port=80&UserID=%s&passwd=%s" 
113       user password)
114      (login-snarf . webmail-netaddress-login)
115      (list-url 
116       "http://www.netaddress.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
117       webmail-session)
118      (list-snarf . webmail-netaddress-list)
119      (article-url "http://www.netaddress.com/")
120      (article-snarf . webmail-netaddress-article)
121      (trash-url 
122       "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
123       webmail-session id))
124     (netscape
125      (paranoid cookie post agent)
126      (address . "webmail.netscape.com")
127      (open-url "http://ureg.netscape.com/iiop/UReg2/login/login?U2_LA=en&U2_BACK_FROM_CJ=true&U2_CS=iso-8859-1&U2_ENDURL=http://webmail.netscape.com/tpl/Subscribe/Step1&U2_NEW_ENDURL=http://webmail.netscape.com/tpl/Subscribe/Step1&U2_EXITURL=http://home.netscape.com/&U2_SOURCE=Webmail")
128      (open-snarf . webmail-netscape-open)
129      (login-url
130       content 
131       ("http://ureg.netscape.com/iiop/UReg2/login/loginform")
132       "%s&U2_USERNAME=%s&U2_PASSWORD=%s"
133       webmail-aux user password)
134      (login-snarf . webmail-netaddress-login)
135      (list-url 
136       "http://webmail.netscape.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
137       webmail-session)
138      (list-snarf . webmail-netaddress-list)
139      (article-url "http://webmail.netscape.com/")
140      (article-snarf . webmail-netscape-article)
141      (trash-url 
142       "http://webmail.netscape.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
143       webmail-session id))
144     (my-deja
145      (paranoid cookie post)
146      (address . "www.my-deja.com")
147      (open-url "http://www.my-deja.com/")
148      (open-snarf . webmail-my-deja-open)
149      (login-url
150       content 
151       ("%s" webmail-aux)
152       "member_name=%s&pw=%s&go=&priv_opt_MyDeja99="
153       user password)
154      (list-url "http://www.deja.com/rg_gotomail.xp")
155      (list-snarf . webmail-my-deja-list)
156      (article-snarf . webmail-my-deja-article)
157      (trash-url webmail-aux id))))
158
159 (defvar webmail-variables
160   '(address article-snarf article-url list-snarf list-url 
161             login-url login-snarf open-url open-snarf site articles
162             post-process paranoid trash-url))
163
164 (defconst webmail-version "webmail 1.0")
165
166 (defvar webmail-newmail-only nil
167   "Only fetch new mails.")
168
169 (defvar webmail-move-to-trash-can t
170   "Move mail to trash can after fetch it.")
171
172 ;;; Internal variables
173
174 (defvar webmail-address nil)
175 (defvar webmail-paranoid nil)
176 (defvar webmail-aux nil)
177 (defvar webmail-session nil)
178 (defvar webmail-article-snarf nil)
179 (defvar webmail-article-url nil)
180 (defvar webmail-list-snarf nil)
181 (defvar webmail-list-url nil)
182 (defvar webmail-login-url nil)
183 (defvar webmail-login-snarf nil)
184 (defvar webmail-open-snarf nil)
185 (defvar webmail-open-url nil)
186 (defvar webmail-trash-url nil)
187 (defvar webmail-articles nil)
188 (defvar webmail-post-process nil)
189
190 (defvar webmail-buffer nil)
191 (defvar webmail-buffer-list nil)
192
193 (defvar webmail-type nil)
194
195 (defvar webmail-error-function nil)
196
197 (defvar webmail-debug-file "~/.emacs-webmail-debug")
198
199 ;;; Interface functions
200
201 (defun webmail-debug (str)
202   (with-temp-buffer
203     (insert "\n---------------- A bug at " str " ------------------\n")
204     (mapcar #'(lambda (sym) 
205                 (if (boundp sym)
206                     (pp `(setq ,sym ',(eval sym)) (current-buffer))))
207             '(webmail-type user))
208     (insert "---------------- webmail buffer ------------------\n\n")
209     (insert-buffer-substring webmail-buffer)
210     (insert "\n---------------- end of buffer ------------------\n\n")
211     (append-to-file (point-min) (point-max) webmail-debug-file)))
212
213 (defun webmail-error (str)
214   (if webmail-error-function
215       (funcall webmail-error-function str))
216   (message "%s HTML has changed; please get a new version of webmail (%s)"
217            webmail-type str)
218   (error "%s HTML has changed; please get a new version of webmail (%s)"
219          webmail-type str))
220
221 (defun webmail-setdefault (type)
222   (let ((type-def (cdr (assq type webmail-type-definition)))
223         (vars webmail-variables)
224         pair)
225     (setq webmail-type type)
226     (dolist (var vars)
227       (if (setq pair (assq var type-def))
228           (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
229         (set (intern (concat "webmail-" (symbol-name var))) nil)))))
230
231 (defun webmail-encode-www-form-urlencoded (pairs)
232   "Return PAIRS encoded for forms."
233   (mapconcat
234    (function
235     (lambda (data)
236       (concat (w3-form-encode-xwfu (car data)) "="
237               (w3-form-encode-xwfu (cdr data)))))
238    pairs "&"))
239
240 (defun webmail-fetch-simple (url content)
241   (let ((url-request-data content)
242         (url-request-method "POST")
243         (url-request-extra-headers
244          '(("Content-type" . "application/x-www-form-urlencoded"))))
245     (nnweb-insert url))
246   t)
247
248 (defun webmail-fetch-form (url pairs)
249   (let ((url-request-data (webmail-encode-www-form-urlencoded pairs))
250         (url-request-method "POST")
251         (url-request-extra-headers
252          '(("Content-type" . "application/x-www-form-urlencoded"))))
253     (nnweb-insert url))
254   t)
255
256 (defun webmail-eval (expr)
257   (cond
258    ((consp expr)
259     (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
260    ((symbolp expr)
261     (eval expr))
262    (t
263     expr)))
264
265 (defun webmail-url (xurl)
266   (mm-with-unibyte-current-buffer
267     (cond 
268      ((eq (car xurl) 'content)
269       (pop xurl)
270       (webmail-fetch-simple (if (stringp (car xurl))
271                                 (car xurl)
272                               (apply 'format (webmail-eval (car xurl))))
273                             (apply 'format (webmail-eval (cdr xurl)))))
274      ((eq (car xurl) 'post)
275       (pop xurl)
276       (webmail-fetch-form (car xurl) (webmail-eval (cdr xurl))))
277      (t
278       (nnweb-insert (apply 'format (webmail-eval xurl)))))))
279
280 (defun webmail-init ()
281   "Initialize buffers and such."
282   (if (gnus-buffer-live-p webmail-buffer)
283       (set-buffer webmail-buffer)
284     (setq webmail-buffer
285           (mm-with-unibyte
286             (nnheader-set-temp-buffer " *webmail*")))))
287
288 (defvar url-package-name)
289 (defvar url-package-version)
290 (defvar url-cookie-multiple-line)
291 (defvar url-confirmation-func)
292
293 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
294 ;;
295 ;; Netscape uses "GET" as redirect method when orignal method is POST
296 ;; and status is 302, .i.e no security risks by default without
297 ;; confirmation.
298 ;;
299 ;; Some web servers (at least Apache used by yahoo) return status 302
300 ;; instead of 303, though they mean 303.
301
302 (defun webmail-url-confirmation-func (prompt)
303   (cond 
304    ((equal prompt (concat "Honor redirection with non-GET method "
305                           "(possible security risks)? "))
306     nil)
307    ((equal prompt "Continue (with method of GET)? ")
308     t)
309    (t (error prompt))))
310
311 (defun webmail-refresh-redirect ()
312   "Redirect refresh url in META."
313   (goto-char (point-min))
314   (while (re-search-forward 
315           "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
316           nil t)
317     (let ((url (match-string 1)))
318       (erase-buffer)
319       (mm-with-unibyte-current-buffer
320         (nnweb-insert url)))
321     (goto-char (point-min))))
322
323 (defun webmail-fetch (file subtype user password)
324   (save-excursion
325     (webmail-setdefault subtype)
326     (let ((url-package-name (if (memq 'agent webmail-paranoid)
327                                 "Mozilla"
328                               url-package-name))
329           (url-package-version (if (memq 'agent webmail-paranoid)
330                                    "4.0"
331                                  url-package-version))
332           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
333                                         nil
334                                       url-cookie-multiple-line))
335           (url-confirmation-func (if (memq 'post webmail-paranoid)
336                                      'webmail-url-confirmation-func
337                                    url-confirmation-func))
338           (url-http-silence-on-insecure-redirection t)
339           url-cookie-storage url-cookie-secure-storage
340           url-cookie-confirmation
341           item id (n 0))
342       (webmail-init)
343       (setq webmail-articles nil)
344       (when webmail-open-url 
345         (erase-buffer)
346         (webmail-url webmail-open-url))
347       (if webmail-open-snarf (funcall webmail-open-snarf))
348       (when webmail-login-url 
349         (erase-buffer)
350         (webmail-url webmail-login-url))
351       (if webmail-login-snarf 
352           (funcall webmail-login-snarf))
353       (when webmail-list-url 
354         (erase-buffer)
355         (webmail-url webmail-list-url))
356       (if webmail-list-snarf 
357           (funcall webmail-list-snarf))
358       (while (setq item (pop webmail-articles))
359         (message "Fetching mail #%d..." (setq n (1+ n)))
360         (erase-buffer)
361         (mm-with-unibyte-current-buffer
362           (nnweb-insert (cdr item)))
363         (setq id (car item))
364         (if webmail-article-snarf 
365             (funcall webmail-article-snarf file id))
366         (when (and webmail-trash-url webmail-move-to-trash-can)
367           (message "Move mail #%d to trash can..." n)
368           (condition-case err
369               (progn
370                 (webmail-url webmail-trash-url)
371                 (let (buf)
372                   (while (setq buf (pop webmail-buffer-list))
373                     (kill-buffer buf))))
374             (error 
375              (let (buf)
376                (while (setq buf (pop webmail-buffer-list))
377                  (kill-buffer buf)))
378              (error err))))))
379     (if webmail-post-process
380         (funcall webmail-post-process))))
381
382 (defun webmail-encode-8bit ()
383   (goto-char (point-min))
384   (skip-chars-forward "^\200-\377")
385   (while (not (eobp))
386     (insert (format "&%d;" (mm-char-int (char-after))))
387     (delete-char 1)
388     (skip-chars-forward "^\200-\377")))
389
390 ;;; hotmail
391
392 (defun webmail-hotmail-open ()
393   (goto-char (point-min))
394   (if (re-search-forward 
395        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
396       (setq webmail-aux (match-string 1))
397     (webmail-error "open@1")))
398
399 (defun webmail-hotmail-login ()
400   (let (site)
401     (goto-char (point-min))
402     (if (re-search-forward 
403          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
404         (setq site (match-string 1))
405       (webmail-error "login@1"))
406     (goto-char (point-min))
407     (if (re-search-forward 
408          "\\(/cgi-bin/HoTMaiL\\?[^\"]*a=b[^\"]*\\)" nil t)
409         (setq webmail-aux (concat "http://" site (match-string 1)))
410       (webmail-error "login@2"))))
411
412 (defun webmail-hotmail-list ()
413   (let (site url newp)
414     (goto-char (point-min))
415     (if (re-search-forward "[0-9]+ new" nil t) 
416         (message "Found %s" (match-string 0))
417       (webmail-error "maybe your w3 version is too old"))
418     (goto-char (point-min))
419     (if (re-search-forward 
420          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
421         (setq site (match-string 1))
422       (webmail-error "list@1"))
423     (goto-char (point-min))
424     (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
425         (setq webmail-aux 
426               (concat "http://" site "/cgi-bin/HoTMaiL?disk=" 
427                       (match-string 1)))
428       (webmail-error "list@2"))
429     (goto-char (point-max))
430     (while (re-search-backward 
431             "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\"" 
432             nil t)
433       (if (setq url (match-string 1))
434           (progn
435             (if (or newp (not webmail-newmail-only))
436                 (let (id)
437                   (if (string-match "msg=\\([^&]+\\)" url)
438                       (setq id (match-string 1 url)))
439                   (push (cons id (concat "http://" site url "&raw=0")) 
440                         webmail-articles)))
441             (setq newp nil))
442         (setq newp t)))))
443
444 ;; Thank victor@idaccr.org (Victor S. Miller) for raw=0
445
446 (defun webmail-hotmail-article (file id)
447   (goto-char (point-min))
448   (if (not (search-forward "<pre>" nil t))
449       (webmail-error "article@3"))
450   (skip-chars-forward "\n\r\t ")
451   (delete-region (point-min) (point))
452   (if (not (search-forward "</pre>" nil t))
453       (webmail-error "article@3.1"))
454   (delete-region (match-beginning 0) (point-max))
455   (nnweb-remove-markup)
456   (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
457     (nnweb-decode-entities))
458   (goto-char (point-min))
459   (while (re-search-forward "\r\n?" nil t)
460     (replace-match "\n"))
461   (goto-char (point-min))
462   (insert "\n\n")
463   (if (not (looking-at "\n*From "))
464       (insert "From nobody " (current-time-string) "\n")
465     (forward-line))
466   (insert "X-Gnus-Webmail: " (symbol-value 'user)
467           "@" (symbol-name webmail-type) "\n")
468   (mm-append-to-file (point-min) (point-max) file))
469
470 (defun webmail-hotmail-article-old (file id)
471   (let (p attachment count mime hotmail-direct)
472     (save-restriction
473       (webmail-encode-8bit)
474       (goto-char (point-min))
475       (if (not (search-forward "<DIV>" nil t))
476           (if (not (search-forward "Reply&nbsp;All" nil t))
477               (webmail-error "article@1")
478             (setq hotmail-direct t))
479         (goto-char (match-beginning 0)))
480       (narrow-to-region (point-min) (point))
481       (if (not (search-backward "<table" nil t 2))
482           (webmail-error "article@1.1"))
483       (delete-region (point-min) (match-beginning 0)) 
484       (while (search-forward "<a href=" nil t)
485         (setq p (match-beginning 0))
486         (search-forward "</a>" nil t)
487         (delete-region p (match-end 0)))
488       (nnweb-remove-markup)
489       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
490         (nnweb-decode-entities))
491       (goto-char (point-min))
492       (delete-blank-lines)
493       (goto-char (point-min))
494       (when (search-forward "\n\n" nil t)
495         (backward-char)
496         (delete-region (point) (point-max)))
497       (goto-char (point-max))
498       (widen)
499       (insert "\n")
500       (setq p (point))
501       (while (re-search-forward 
502               "<tt>\\|<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" 
503               nil t)
504         (if (setq attachment (match-string 1))
505             (let ((filename (match-string 2))
506                   bufname);; Attachment
507               (delete-region p (match-end 0))
508               (save-excursion
509                 (set-buffer (generate-new-buffer " *webmail-att*"))
510                 (nnweb-insert attachment)
511                 (push (current-buffer) webmail-buffer-list)
512                 (setq bufname (buffer-name)))
513               (setq mime t)
514               (insert "<#part type=" 
515                       (or (and filename
516                                (string-match "\\.[^\\.]+$" filename)
517                                (mailcap-extension-to-mime
518                                 (match-string 0 filename)))
519                           "application/octet-stream"))
520               (insert " buffer=\"" bufname "\"")
521               (insert " filename=\"" filename "\"")
522               (insert " disposition=\"inline\"")
523               (insert "><#/part>\n")
524               (setq p (point)))
525           (delete-region p (match-end 0))
526           (if hotmail-direct
527               (if (not (search-forward "</tt>" nil t))
528                   (webmail-error "article@1.2")
529                 (delete-region (match-beginning 0) (match-end 0)))
530             (setq count 1)
531             (while (and (> count 0) 
532                         (re-search-forward "</div>\\|\\(<div>\\)" nil t))
533               (if (match-string 1)
534                   (setq count (1+ count))
535                 (if (= (setq count (1- count)) 0)
536                     (delete-region (match-beginning 0)
537                                    (match-end 0))))))
538           (narrow-to-region p (point))
539           (goto-char (point-min))
540           (cond 
541            ((looking-at "<pre>")
542             (goto-char (match-end 0))
543             (if (looking-at "$") (forward-char))
544             (delete-region (point-min) (point))
545             (nnweb-remove-markup)
546             (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
547               (nnweb-decode-entities))
548             nil)
549            (t
550             (setq mime t)
551             (insert "<#part type=\"text/html\" disposition=inline>")
552             (goto-char (point-max))
553             (insert "<#/part>")))
554           (goto-char (point-max))
555           (setq p (point))
556           (widen)))
557       (delete-region p (point-max))
558       (goto-char (point-min))
559       ;; Some blank line to seperate mails.
560       (insert "\n\nFrom nobody " (current-time-string) "\n")
561       (insert "X-Gnus-Webmail: " (symbol-value 'user)
562               "@" (symbol-name webmail-type) "\n")
563       (if id
564           (insert (format "X-Message-ID: <%s@hotmail.com>\n" id)))
565       (unless (looking-at "$") 
566         (if (search-forward "\n\n" nil t)
567             (forward-line -1)
568           (webmail-error "article@2")))
569       (narrow-to-region (point) (point-max))
570       (if mime
571           (insert "MIME-Version: 1.0\n"
572                   (prog1
573                       (mml-generate-mime)
574                     (delete-region (point-min) (point-max)))))
575       (goto-char (point-min))
576       (widen)
577       (let (case-fold-search)
578         (while (re-search-forward "^From " nil t)
579           (beginning-of-line)
580           (insert ">"))))
581     (mm-append-to-file (point-min) (point-max) file)))
582
583 ;;; yahoo
584
585 (defun webmail-yahoo-open ()
586   (goto-char (point-min))
587   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
588       (setq webmail-aux (match-string 1))
589     (webmail-error "open@1")))
590
591 (defun webmail-yahoo-login ()
592   (goto-char (point-min))
593   (if (re-search-forward "http://[^/]+[0-9]\\.mail\\.yahoo\\.com/" nil t)
594       (setq webmail-aux (match-string 0))
595     (webmail-error "login@1"))
596   (if (re-search-forward "YY=[0-9]+" nil t)
597       (setq webmail-aux (concat webmail-aux "ym/ShowFolder?"
598                                 (match-string 0)))
599     (webmail-error "login@2")))
600
601 (defun webmail-yahoo-list ()
602   (let (url (newp t) (tofetch 0))
603     (goto-char (point-min))
604     (when (re-search-forward 
605            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
606       ;;(setq listed (match-string 1))
607       (message "Found %s mail(s)" (match-string 2)))
608     (if (string-match "http://[^/]+" webmail-aux)
609         (setq webmail-aux (match-string 0 webmail-aux))
610       (webmail-error "list@1"))
611     (goto-char (point-min))
612     (while (re-search-forward 
613             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
614             nil t)
615       (if (setq url (match-string 1))
616           (progn
617             (when (or newp (not webmail-newmail-only))
618               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
619                     webmail-articles)
620               (setq tofetch (1+ tofetch)))
621             (setq newp t))
622         (setq newp nil)))
623     (setq webmail-articles (nreverse webmail-articles))
624     (message "Fetching %d mail(s)" tofetch)))
625
626 (defun webmail-yahoo-article (file id)
627   (let (p attachment)
628     (save-restriction
629       (goto-char (point-min))
630       (if (not (search-forward "value=\"Done\"" nil t))
631           (webmail-error "article@1"))
632       (if (not (search-forward "<table" nil t))
633           (webmail-error "article@2"))
634       (delete-region (point-min) (match-beginning 0)) 
635       (if (not (search-forward "</table>" nil t))
636           (webmail-error "article@3"))
637       (narrow-to-region (point-min) (match-end 0))
638       (while (search-forward "<a href=" nil t)
639         (setq p (match-beginning 0))
640         (search-forward "</a>" nil t)
641         (delete-region p (match-end 0)))
642       (nnweb-remove-markup)
643       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
644         (nnweb-decode-entities))
645       (goto-char (point-min))
646       (delete-blank-lines)
647       (goto-char (point-max))
648       (widen)
649       (insert "\n")
650       (setq p (point))
651       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
652         (setq attachment (match-string 0))
653         (let (bufname ct ctl cd description)
654           (if (not (search-forward "<table" nil t))
655               (webmail-error "article@4"))
656           (delete-region p (match-beginning 0))
657           (if (not (search-forward "</table>" nil t))
658               (webmail-error "article@5"))
659           (narrow-to-region p (match-end 0))
660           (nnweb-remove-markup)
661           (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
662             (nnweb-decode-entities))
663           (goto-char (point-min))
664           (delete-blank-lines)
665           (setq ct (mail-fetch-field "content-type")
666                 ctl (ignore-errors (mail-header-parse-content-type ct))
667                 ;;cte (mail-fetch-field "content-transfer-encoding")
668                 cd (mail-fetch-field "content-disposition")
669                 description (mail-fetch-field "content-description")
670                 id (mail-fetch-field "content-id"))
671           (delete-region (point-min) (point-max))
672           (widen)
673           (save-excursion
674             (set-buffer (generate-new-buffer " *webmail-att*"))
675             (nnweb-insert (concat webmail-aux attachment))
676             (push (current-buffer) webmail-buffer-list)
677             (setq bufname (buffer-name)))
678           (insert "<#part")
679           (if (and ctl (not (equal (car ctl) "text/")))
680               (insert " type=\"" (car ctl) "\""))
681           (insert " buffer=\"" bufname "\"")
682           (if cd
683               (insert " disposition=\"" cd "\""))
684           (if description
685               (insert " description=\"" description "\""))
686           (insert "><#/part>\n")
687           (setq p (point))))
688       (delete-region p (point-max))
689       (goto-char (point-min))
690       ;; Some blank line to seperate mails.
691       (insert "\n\nFrom nobody " (current-time-string) "\n")
692       (insert "X-Gnus-Webmail: " (symbol-value 'user)
693               "@" (symbol-name webmail-type) "\n")
694       (if id
695           (insert (format "X-Message-ID: <%s@yahoo.com>\n" id)))
696       (unless (looking-at "$") 
697         (if (search-forward "\n\n" nil t)
698             (forward-line -1)
699           (webmail-error "article@2")))
700       (narrow-to-region (point) (point-max))
701       (insert "MIME-Version: 1.0\n"
702               (prog1
703                   (mml-generate-mime)
704                 (delete-region (point-min) (point-max))))
705       (goto-char (point-min))
706       (widen)
707       (let (case-fold-search)
708         (while (re-search-forward "^From " nil t)
709           (beginning-of-line)
710           (insert ">"))))
711     (mm-append-to-file (point-min) (point-max) file)))
712
713 ;;; netaddress
714
715 (defun webmail-netscape-open ()
716   (goto-char (point-min))
717   (if (re-search-forward "login/hint\\?\\([^\"]+\\)\"" nil t)
718       (setq webmail-aux (match-string 1))
719     (webmail-error "open@1")))
720
721 (defun webmail-netaddress-open ()
722   (goto-char (point-min))
723   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
724       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
725     (webmail-error "open@1")))
726
727 (defun webmail-netaddress-login ()
728   (webmail-refresh-redirect)
729   (goto-char (point-min))
730   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
731       (setq webmail-session (match-string 1))
732     (webmail-error "login@1")))
733
734 (defun webmail-netaddress-list ()
735   (webmail-refresh-redirect)
736   (let (item id)
737     (goto-char (point-min))
738     (when (re-search-forward 
739            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
740       (message "Found %s mail(s), %s unread" 
741                (match-string 2) (match-string 1)))
742     (goto-char (point-min))
743     (while (re-search-forward 
744             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
745       (if (setq id (match-string 2))
746           (setq item 
747                 (cons id 
748                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
749                               (car webmail-article-url)
750                               webmail-session id)))
751         (if (or (not webmail-newmail-only)
752                 (equal (match-string 1) "True"))
753             (push item webmail-articles))))
754     (setq webmail-articles (nreverse webmail-articles))))
755
756 (defun webmail-netaddress-single-part ()
757   (goto-char (point-min))
758   (cond 
759    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
760     ;; text/plain
761     (replace-match "")
762     (while (re-search-forward "[\t\040\r\n]+" nil t)
763       (replace-match " "))
764     (goto-char (point-min))
765     (while (re-search-forward "<br>" nil t)
766       (replace-match "\n"))
767     (nnweb-remove-markup)
768     (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
769       (nnweb-decode-entities))
770     nil)
771    (t
772     (insert "<#part type=\"text/html\" disposition=inline>")
773     (goto-char (point-max))
774     (insert "<#/part>")
775     t)))
776
777 (defun webmail-netaddress-article (file id)
778   (webmail-refresh-redirect)
779   (let (p p1 attachment count mime type)
780     (save-restriction
781       (webmail-encode-8bit)
782       (goto-char (point-min))
783       (if (not (search-forward "Trash" nil t))
784           (webmail-error "article@1"))
785       (if (not (search-forward "<form>" nil t))
786           (webmail-error "article@2"))
787       (delete-region (point-min) (match-beginning 0)) 
788       (if (not (search-forward "</form>" nil t))
789           (webmail-error "article@3"))
790       (narrow-to-region (point-min) (match-end 0))
791       (goto-char (point-min))
792       (while (re-search-forward "[\040\t\r\n]+" nil t)
793         (replace-match " "))
794       (goto-char (point-min))
795       (while (search-forward "<b>" nil t)
796         (replace-match "\n"))
797       (nnweb-remove-markup)
798       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
799         (nnweb-decode-entities))
800       (goto-char (point-min))
801       (delete-blank-lines)
802       (goto-char (point-min))
803       (while (re-search-forward "^\040+\\|\040+$" nil t)
804         (replace-match ""))
805       (goto-char (point-min))
806       (while (re-search-forward "\040+" nil t)
807         (replace-match " "))
808       (goto-char (point-max))
809       (widen)
810       (insert "\n\n")
811       (setq p (point))
812       (unless (search-forward "<!-- Data -->" nil t)
813         (webmail-error "article@4"))
814       (forward-line 14)
815       (delete-region p (point))
816       (goto-char (point-max))
817       (unless (re-search-backward 
818                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
819         (webmail-error "article@5"))
820       (delete-region (point) (point-max))
821       (goto-char p)
822       (while (search-forward
823               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
824               nil t 2)
825         (setq mime t)
826         (unless (search-forward "</TABLE>" nil t)
827           (webmail-error "article@6"))
828         (setq p1 (point))
829         (if (search-backward "<IMG " p t)
830             (progn
831               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
832                 (webmail-error "article@7"))
833               (setq attachment (match-string 1))
834               (setq type (match-string 2))
835               (unless (search-forward "</TABLE>" nil t)
836                 (webmail-error "article@8"))
837               (delete-region p (point))
838               (let (bufname);; Attachment
839                 (save-excursion
840                   (set-buffer (generate-new-buffer " *webmail-att*"))
841                   (nnweb-insert (concat (car webmail-open-url) attachment))
842                   (push (current-buffer) webmail-buffer-list)
843                   (setq bufname (buffer-name)))
844                 (insert "<#part type=" type)
845                 (insert " buffer=\"" bufname "\"")
846                 (insert " disposition=\"inline\"")
847                 (insert "><#/part>\n")
848                 (setq p (point))))
849           (delete-region p p1)
850           (narrow-to-region 
851            p
852            (if (search-forward 
853                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
854                 nil t)
855                (match-beginning 0)
856              (point-max)))
857           (webmail-netaddress-single-part)
858           (goto-char (point-max))
859           (setq p (point))
860           (widen)))
861       (unless mime
862         (narrow-to-region p (point-max))
863         (setq mime (webmail-netaddress-single-part))
864         (widen))
865       (goto-char (point-min))
866       ;; Some blank line to seperate mails.
867       (insert "\n\nFrom nobody " (current-time-string) "\n")
868       (insert "X-Gnus-Webmail: " (symbol-value 'user)
869               "@" (symbol-name webmail-type) "\n")
870       (if id
871           (insert (format "X-Message-ID: <%s@%s>\n" id webmail-address)))
872       (unless (looking-at "$") 
873         (if (search-forward "\n\n" nil t)
874             (forward-line -1)
875           (webmail-error "article@2")))
876       (when mime
877         (narrow-to-region (point-min) (point))
878         (goto-char (point-min))
879         (while (not (eobp))
880           (if (looking-at "MIME-Version\\|Content-Type")
881               (delete-region (point) 
882                              (progn
883                                (forward-line 1)
884                                (if (re-search-forward "^[^ \t]" nil t)
885                                    (goto-char (match-beginning 0))
886                                  (point-max))))
887             (forward-line 1)))
888         (goto-char (point-max))
889         (widen)
890         (narrow-to-region (point) (point-max))
891         (insert "MIME-Version: 1.0\n"
892                 (prog1
893                     (mml-generate-mime)
894                   (delete-region (point-min) (point-max))))
895         (goto-char (point-min))
896         (widen))
897       (let (case-fold-search)
898         (while (re-search-forward "^From " nil t)
899           (beginning-of-line)
900           (insert ">"))))
901     (mm-append-to-file (point-min) (point-max) file)))
902
903 (defun webmail-netscape-article (file id)
904   (let (p p1 attachment count mime type)
905     (save-restriction
906       (webmail-encode-8bit)
907       (goto-char (point-min))
908       (if (not (search-forward "Trash" nil t))
909           (webmail-error "article@1"))
910       (if (not (search-forward "<form>" nil t))
911           (webmail-error "article@2"))
912       (delete-region (point-min) (match-beginning 0)) 
913       (if (not (search-forward "</form>" nil t))
914           (webmail-error "article@3"))
915       (narrow-to-region (point-min) (match-end 0))
916       (goto-char (point-min))
917       (while (re-search-forward "[\040\t\r\n]+" nil t)
918         (replace-match " "))
919       (goto-char (point-min))
920       (while (re-search-forward "<a href=[^>]*>[^<]*</a>" nil t)
921         (replace-match ""))
922       (goto-char (point-min))
923       (while (search-forward "<b>" nil t)
924         (replace-match "\n"))
925       (nnweb-remove-markup)
926       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
927         (nnweb-decode-entities))
928       (goto-char (point-min))
929       (delete-blank-lines)
930       (goto-char (point-min))
931       (while (re-search-forward "^\040+\\|\040+$" nil t)
932         (replace-match ""))
933       (goto-char (point-min))
934       (while (re-search-forward "\040+" nil t)
935         (replace-match " "))
936       (goto-char (point-max))
937       (widen)
938       (insert "\n\n")
939       (setq p (point))
940       (unless (search-forward "<!-- Data -->" nil t)
941         (webmail-error "article@4"))
942       (forward-line 14)
943       (delete-region p (point))
944       (goto-char (point-max))
945       (unless (re-search-backward 
946                "<form name=\"Transfer2\"" p t)
947         (webmail-error "article@5"))
948       (delete-region (point) (point-max))
949       (goto-char p)
950       (while (search-forward
951               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
952               nil t 2)
953         (setq mime t)
954         (unless (search-forward "</TABLE>" nil t)
955           (webmail-error "article@6"))
956         (setq p1 (point))
957         (if (search-backward "<IMG " p t)
958             (progn
959               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
960                 (webmail-error "article@7"))
961               (setq attachment (match-string 1))
962               (setq type (match-string 2))
963               (unless (search-forward "</TABLE>" nil t)
964                 (webmail-error "article@8"))
965               (delete-region p (point))
966               (let (bufname);; Attachment
967                 (save-excursion
968                   (set-buffer (generate-new-buffer " *webmail-att*"))
969                   (nnweb-insert (concat (car webmail-open-url) attachment))
970                   (push (current-buffer) webmail-buffer-list)
971                   (setq bufname (buffer-name)))
972                 (insert "<#part type=" type)
973                 (insert " buffer=\"" bufname "\"")
974                 (insert " disposition=\"inline\"")
975                 (insert "><#/part>\n")
976                 (setq p (point))))
977           (delete-region p p1)
978           (narrow-to-region 
979            p
980            (if (search-forward 
981                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
982                 nil t)
983                (match-beginning 0)
984              (point-max)))
985           (webmail-netaddress-single-part)
986           (goto-char (point-max))
987           (setq p (point))
988           (widen)))
989       (unless mime
990         (narrow-to-region p (point-max))
991         (setq mime (webmail-netaddress-single-part))
992         (widen))
993       (goto-char (point-min))
994       ;; Some blank line to seperate mails.
995       (insert "\n\nFrom nobody " (current-time-string) "\n")
996       (insert "X-Gnus-Webmail: " (symbol-value 'user)
997               "@" (symbol-name webmail-type) "\n")
998       (if id
999           (insert (format "X-Message-ID: <%s@%s>\n" id webmail-address)))
1000       (unless (looking-at "$") 
1001         (if (search-forward "\n\n" nil t)
1002             (forward-line -1)
1003           (webmail-error "article@2")))
1004       (when mime
1005         (narrow-to-region (point-min) (point))
1006         (goto-char (point-min))
1007         (while (not (eobp))
1008           (if (looking-at "MIME-Version\\|Content-Type")
1009               (delete-region (point) 
1010                              (progn
1011                                (forward-line 1)
1012                                (if (re-search-forward "^[^ \t]" nil t)
1013                                    (goto-char (match-beginning 0))
1014                                  (point-max))))
1015             (forward-line 1)))
1016         (goto-char (point-max))
1017         (widen)
1018         (narrow-to-region (point) (point-max))
1019         (insert "MIME-Version: 1.0\n"
1020                 (prog1
1021                     (mml-generate-mime)
1022                   (delete-region (point-min) (point-max))))
1023         (goto-char (point-min))
1024         (widen))
1025       (let (case-fold-search)
1026         (while (re-search-forward "^From " nil t)
1027           (beginning-of-line)
1028           (insert ">"))))
1029     (mm-append-to-file (point-min) (point-max) file)))
1030
1031 ;;; my-deja
1032
1033 (defun webmail-my-deja-open ()
1034   (webmail-refresh-redirect)
1035   (goto-char (point-min))
1036   (if (re-search-forward "action=\"\\([^\"]+login_confirm\\.xp[^\"]*\\)\"" 
1037                          nil t)
1038       (setq webmail-aux (match-string 1))
1039     (webmail-error "open@1")))
1040
1041 (defun webmail-my-deja-list ()
1042   (let (item id newp base)
1043     (goto-char (point-min))
1044     (when (re-search-forward "href=\"\\(\\([^\"]*\\)/mailnf\\.[^\"]*\\)\"" 
1045                              nil t)
1046       (let ((url (match-string 1)))
1047         (setq base (match-string 2))
1048         (erase-buffer)
1049         (nnweb-insert url)))
1050     (goto-char (point-min))
1051     (when (re-search-forward 
1052            "(\\([0-9]+\\) Message.?-[^>]*\\([0-9]+\\) New"
1053            nil t) 
1054       (message "Found %s mail(s), %s unread" 
1055                (match-string 1) (match-string 2)))
1056     (goto-char (point-min))
1057     (while (re-search-forward 
1058             "newmail\\.gif\\|href=\"[^\"]*\\(mailnf\\.[^\"]+act=view[^\"]+mid=\\([^\"&]+\\)[^\"]+\\)\""
1059             nil t)
1060       (if (setq id (match-string 2))
1061           (when (and (or newp (not webmail-newmail-only))
1062                      (not (assoc id webmail-articles)))
1063             (push (cons id (setq webmail-aux 
1064                                  (concat base "/" (match-string 1))))
1065                   webmail-articles)
1066             (setq newp nil))
1067         (setq newp t)))
1068     (setq webmail-articles (nreverse webmail-articles))))
1069
1070 (defun webmail-my-deja-article-part (base)
1071   (let (p)
1072     (cond 
1073      ((looking-at "[\t\040\r\n]*<!--[^>]*>")
1074       (replace-match ""))
1075      ((looking-at "[\t\040\r\n]*</PRE>")
1076       (replace-match ""))
1077      ((looking-at "[\t\040\r\n]*<PRE>")
1078       ;; text/plain
1079       (replace-match "")
1080       (save-restriction
1081         (narrow-to-region (point)
1082                           (if (re-search-forward "</?PRE>" nil t)
1083                               (match-beginning 0)
1084                             (point-max)))
1085         (goto-char (point-min))
1086         (nnweb-remove-markup)
1087         (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
1088           (nnweb-decode-entities))
1089         (goto-char (point-max))))
1090      ((looking-at "[\t\040\r\n]*<TABLE")
1091       (save-restriction
1092         (narrow-to-region (point)
1093                           (if (search-forward "</TABLE>" nil t 2)
1094                               (point)
1095                             (point-max)))
1096         (goto-char (point-min))
1097         (let (name type url bufname)
1098           (if (and (search-forward "File Name:" nil t)
1099                    (re-search-forward "<FONT[^>]+>\\([^<]+\\)" nil t))
1100               (setq name (match-string 1)))
1101           (if (and (search-forward "File Type:" nil t)
1102                    (re-search-forward "<FONT[^>]+>\\([^<]+\\)" nil t))
1103               (setq type (match-string 1)))
1104           (unless (re-search-forward "action=\"getattach\\.cgi/\\([^\"]+\\)" 
1105                                      nil t)
1106             (webmail-error "article@5"))
1107           (setq url (concat base "/getattach.cgi/" (match-string 1)
1108                             "?sm=Download"))
1109           (while (re-search-forward 
1110                   "type=hidden name=\"\\([^\"]+\\)\" value=\"\\([^\"]+\\)" 
1111                   nil t)
1112             (setq url (concat url "&" (match-string 1) "="
1113                                   (match-string 2))))
1114           (delete-region (point-min) (point-max))
1115           (save-excursion
1116             (set-buffer (generate-new-buffer " *webmail-att*"))
1117             (nnweb-insert url)
1118             (push (current-buffer) webmail-buffer-list)
1119             (setq bufname (buffer-name)))
1120           (insert "<#part type=\"" type "\"")
1121           (if name (insert " filename=\"" name "\""))
1122           (insert " buffer=\"" bufname "\"")
1123           (insert " disposition=inline><#/part>"))))
1124      (t
1125       (insert "<#part type=\"text/html\" disposition=inline>")
1126       (goto-char (point-max))
1127       (insert "<#/part>")))))
1128
1129 (defun webmail-my-deja-article (file id)
1130   (let (base)
1131     (goto-char (point-min))
1132     (unless (string-match "\\([^\"]+\\)/mail" webmail-aux)
1133       (webmail-error "article@0"))
1134     (setq base (match-string 1 webmail-aux))
1135     (when (re-search-forward 
1136            "href=\"[^\"]*\\(mailnf\\.[^\"]+act=move[^\"]+mid=\\([^\"&]+\\)[^\"]+\\)\""
1137            nil t)
1138       (setq webmail-aux (concat base "/" (match-string 1)))
1139       (string-match "mid=[^\"&]+" webmail-aux)
1140       (setq webmail-aux (replace-match "mid=%s" nil nil webmail-aux)))
1141     (unless (search-forward "<HR noshade>" nil t)
1142       (webmail-error "article@1"))
1143     (delete-region (point-min) (point))
1144     (unless (search-forward "<HR noshade>" nil t)
1145       (webmail-error "article@2"))
1146     (save-restriction
1147       (narrow-to-region (point-min) (point))
1148       (while (search-forward "\r\n" nil t)
1149         (replace-match "\n"))
1150       (nnweb-remove-markup)
1151       (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities)))
1152         (nnweb-decode-entities))
1153       (goto-char (point-min))
1154       (while (re-search-forward "\n\n+" nil t)
1155         (replace-match "\n"))
1156       (goto-char (point-max)))
1157     (save-restriction
1158       (narrow-to-region (point) (point-max))
1159       (goto-char (point-max))
1160       (unless (search-backward "<HR noshade>" nil t)
1161         (webmail-error "article@3"))
1162       (unless (search-backward "</TT>" nil t)
1163         (webmail-error "article@4"))
1164       (delete-region (point) (point-max))
1165       (goto-char (point-min))
1166       (while (not (eobp)) 
1167         (webmail-my-deja-article-part base))
1168       (insert "MIME-Version: 1.0\n"
1169               (prog1
1170                   (mml-generate-mime)
1171                 (delete-region (point-min) (point-max)))))
1172     (goto-char (point-min))
1173     (insert "\n\nFrom nobody " (current-time-string) "\n")
1174     (insert "X-Gnus-Webmail: " (symbol-value 'user)
1175             "@" (symbol-name webmail-type) "\n")
1176     (if (eq (char-after) ?\n)
1177         (delete-char 1))
1178     (mm-append-to-file (point-min) (point-max) file)))
1179
1180 (provide 'webmail)
1181
1182 ;;; webmail.el ends here