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