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