5770ea7dd7ef44e6cb4cedf9af2b7178aac80ce7
[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 yahoo netaddress my-deja
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=&js=&foo=inbox&page=&%s=on&Move+To.x=Move+To&tobox=trAsH" 
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-snarf . webmail-netaddress-article)
115      (trash-url 
116       "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
117       webmail-session id))
118     (my-deja
119      (paranoid cookie post)
120      (address . "www.my-deja.com")
121      (open-url "http://www.my-deja.com/")
122      (open-snarf . webmail-my-deja-open)
123      (login-url
124       content 
125       ("%s" webmail-aux)
126       "user=%s&pw=%s&autologout=60&go="
127       user password)
128      (list-url "http://www.deja.com/rg_gotomail.xp")
129      (list-snarf . webmail-my-deja-list)
130      (article-snarf . webmail-my-deja-article)
131      (trash-url 
132       "%s/gmm_multiplex.femail?%%2Fgmm_domovemesg_top.femail=Move+to%%3A&folder_top=%s%%3Azzz%%3A%%7E6trash%%3AF%%3A0&docid=%s"
133       webmail-aux user id))))
134
135 (defvar webmail-variables
136   '(address article-snarf article-url list-snarf list-url 
137             login-url login-snarf open-url open-snarf site articles
138             post-process paranoid trash-url))
139
140 (defconst webmail-version "webmail 1.0")
141
142 (defvar webmail-newmail-only nil
143   "Only fetch new mails.")
144
145 (defvar webmail-move-to-trash-can t
146   "Move mail to trash can after fetch it.")
147
148 ;;; Internal variables
149
150 (defvar webmail-address nil)
151 (defvar webmail-paranoid nil)
152 (defvar webmail-aux nil)
153 (defvar webmail-session nil)
154 (defvar webmail-article-snarf nil)
155 (defvar webmail-article-url nil)
156 (defvar webmail-list-snarf nil)
157 (defvar webmail-list-url nil)
158 (defvar webmail-login-url nil)
159 (defvar webmail-login-snarf nil)
160 (defvar webmail-open-snarf nil)
161 (defvar webmail-open-url nil)
162 (defvar webmail-trash-url nil)
163 (defvar webmail-articles nil)
164 (defvar webmail-post-process nil)
165
166 (defvar webmail-buffer nil)
167 (defvar webmail-buffer-list nil)
168
169 (defvar webmail-type nil)
170
171 ;;; Interface functions
172
173 (defun webmail-error (str)
174   (error "%s HTML has changed; please get a new version of webmail (%s)"
175          webmail-type str))
176
177 (defun webmail-setdefault (type)
178   (let ((type-def (cdr (assq type webmail-type-definition)))
179         (vars webmail-variables)
180         pair)
181     (setq webmail-type type)
182     (dolist (var vars)
183       (if (setq pair (assq var type-def))
184           (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
185         (set (intern (concat "webmail-" (symbol-name var))) nil)))))
186
187 (defun webmail-encode-www-form-urlencoded (pairs)
188   "Return PAIRS encoded for forms."
189   (mapconcat
190    (function
191     (lambda (data)
192       (concat (w3-form-encode-xwfu (car data)) "="
193               (w3-form-encode-xwfu (cdr data)))))
194    pairs "&"))
195
196 (defun webmail-fetch-simple (url content)
197   (let ((url-request-data content)
198         (url-request-method "POST")
199         (url-request-extra-headers
200          '(("Content-type" . "application/x-www-form-urlencoded"))))
201     (nnweb-insert url))
202   t)
203
204 (defun webmail-fetch-form (url pairs)
205   (let ((url-request-data (webmail-encode-www-form-urlencoded pairs))
206         (url-request-method "POST")
207         (url-request-extra-headers
208          '(("Content-type" . "application/x-www-form-urlencoded"))))
209     (nnweb-insert url))
210   t)
211
212 (defun webmail-eval (expr)
213   (cond
214    ((consp expr)
215     (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
216    ((symbolp expr)
217     (eval expr))
218    (t
219     expr)))
220
221 (defun webmail-url (xurl)
222   (mm-with-unibyte-current-buffer
223     (cond 
224      ((eq (car xurl) 'content)
225       (pop xurl)
226       (webmail-fetch-simple (if (stringp (car xurl))
227                                 (car xurl)
228                               (apply 'format (webmail-eval (car xurl))))
229                             (apply 'format (webmail-eval (cdr xurl)))))
230      ((eq (car xurl) 'post)
231       (pop xurl)
232       (webmail-fetch-form (car xurl) (webmail-eval (cdr xurl))))
233      (t
234       (nnweb-insert (apply 'format (webmail-eval xurl)))))))
235   
236 (defun webmail-init ()
237   "Initialize buffers and such."
238   (if (gnus-buffer-live-p webmail-buffer)
239       (set-buffer webmail-buffer)
240     (setq webmail-buffer
241           (nnheader-set-temp-buffer " *webmail*"))))
242
243 (defvar url-package-name)
244 (defvar url-package-version)
245 (defvar url-cookie-multiple-line)
246 (defvar url-confirmation-func)
247
248 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
249 ;;
250 ;; Netscape uses "GET" as redirect method when orignal method is POST
251 ;; and status is 302, .i.e no security risks by default without
252 ;; confirmation.
253 ;;
254 ;; Some web servers (at least Apache used by yahoo) return status 302
255 ;; instead of 303, though they mean 303.
256
257 (defun webmail-url-confirmation-func (prompt)
258   (cond 
259    ((equal prompt (concat "Honor redirection with non-GET method "
260                           "(possible security risks)? "))
261     nil)
262    ((equal prompt "Continue (with method of GET)? ")
263     t)
264    (t (error prompt))))
265
266 (defun webmail-refresh-redirect ()
267   "Redirect refresh url in META."
268   (goto-char (point-min))
269   (while (re-search-forward 
270           "HTTP-EQUIV=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
271           nil t)
272     (let ((url (match-string 1)))
273       (erase-buffer)
274       (mm-with-unibyte-current-buffer
275         (nnweb-insert url)))
276     (goto-char (point-min))))
277
278 (defun webmail-fetch (file subtype user password)
279   (save-excursion
280     (webmail-setdefault subtype)
281     (let ((url-package-name (if (memq 'agent webmail-paranoid)
282                                 "Mozilla"
283                               url-package-name))
284           (url-package-version (if (memq 'agent webmail-paranoid)
285                                    "4.0"
286                                  url-package-version))
287           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
288                                         nil
289                                       url-cookie-multiple-line))
290           (url-confirmation-func (if (memq 'post webmail-paranoid)
291                                      'webmail-url-confirmation-func
292                                    url-confirmation-func))
293           url-cookie-storage url-cookie-secure-storage
294           url-cookie-confirmation
295           item id (n 0))
296       (webmail-init)
297       (setq webmail-articles nil)
298       (when webmail-open-url 
299         (erase-buffer)
300         (webmail-url webmail-open-url))
301       (if webmail-open-snarf (funcall webmail-open-snarf))
302       (when webmail-login-url 
303         (erase-buffer)
304         (webmail-url webmail-login-url))
305       (if webmail-login-snarf 
306           (funcall webmail-login-snarf))
307       (when webmail-list-url 
308         (erase-buffer)
309         (webmail-url webmail-list-url))
310       (if webmail-list-snarf 
311           (funcall webmail-list-snarf))
312       (while (setq item (pop webmail-articles))
313         (message "Fetching mail #%d..." (setq n (1+ n)))
314         (erase-buffer)
315         (mm-with-unibyte-current-buffer
316           (nnweb-insert (cdr item)))
317         (setq id (car item))
318         (if webmail-article-snarf 
319             (funcall webmail-article-snarf file id))
320         (when (and webmail-trash-url webmail-move-to-trash-can)
321           (message "Move mail #%d to trash can..." n)
322           (condition-case err
323               (progn
324                 (webmail-url webmail-trash-url)
325                 (let (buf)
326                   (while (setq buf (pop webmail-buffer-list))
327                     (kill-buffer buf))))
328             (error 
329              (let (buf)
330                (while (setq buf (pop webmail-buffer-list))
331                  (kill-buffer buf)))
332              (error err))))))
333     (if webmail-post-process
334         (funcall webmail-post-process))))
335
336 ;;; hotmail
337
338 (defun webmail-hotmail-open ()
339   (goto-char (point-min))
340   (if (re-search-forward 
341        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
342       (setq webmail-aux (match-string 1))
343     (webmail-error "open@1")))
344
345 (defun webmail-hotmail-list ()
346   (let (site url newp)
347     (goto-char (point-min))
348     (if (re-search-forward "[0-9]+ messages, [0-9]+ new" nil t) 
349         (message "Found %s" (match-string 0))
350       (webmail-error "maybe your w3 version is too old"))
351     (goto-char (point-min))
352     (if (re-search-forward 
353          "action=\"https?://\\([^/]+\\)/cgi-bin/HoTMaiL" nil t)
354         (setq site (match-string 1))
355       (webmail-error "list@1"))
356     (goto-char (point-min))
357     (if (re-search-forward "disk=\\([^&]+\\)&" nil t)
358         (setq webmail-aux 
359               (concat "http://" site "/cgi-bin/HoTMaiL?disk=" 
360                       (match-string 1)))
361       (webmail-error "list@2"))
362     (goto-char (point-max))
363     (while (re-search-backward 
364             "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\"" 
365             nil t)
366       (if (setq url (match-string 1))
367           (progn
368             (if (or newp (not webmail-newmail-only))
369                 (let (id)
370                   (if (string-match "msg=\\([^&]+\\)" url)
371                       (setq id (match-string 1 url)))
372                   (push (cons id (concat "http://" site url)) 
373                         webmail-articles)))
374             (setq newp nil))
375         (setq newp t)))))
376
377 (defun webmail-hotmail-article (file id)
378   (let (p attachment count mime)
379     (save-restriction
380       (goto-char (point-min))
381       (if (not (search-forward "<DIV>" nil t))
382           (webmail-error "article@1"))
383       (narrow-to-region (point-min) (match-beginning 0))
384       (if (not (search-backward "<table" nil t 2))
385           (webmail-error "article@1.1"))
386       (delete-region (point-min) (match-beginning 0)) 
387       (while (search-forward "<a href=" nil t)
388         (setq p (match-beginning 0))
389         (search-forward "</a>" nil t)
390         (delete-region p (match-end 0)))
391       (nnweb-remove-markup)
392       (nnweb-decode-entities)
393       (goto-char (point-min))
394       (delete-blank-lines)
395       (goto-char (point-min))
396       (when (search-forward "\n\n" nil t)
397         (backward-char)
398         (delete-region (point) (point-max)))
399       (goto-char (point-max))
400       (widen)
401       (insert "\n")
402       (setq p (point))
403       (while (re-search-forward 
404               "<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" 
405               nil t)
406         (if (setq attachment (match-string 1))
407             (let ((filename (match-string 2))
408                   bufname);; Attachment
409               (delete-region p (match-end 0))
410               (save-excursion
411                 (set-buffer (generate-new-buffer " *webmail-att*"))
412                 (nnweb-insert attachment)
413                 (push (current-buffer) webmail-buffer-list)
414                 (setq bufname (buffer-name)))
415               (setq mime t)
416               (insert "<#part type=" 
417                       (or (and filename
418                                (string-match "\\.[^\\.]+$" filename)
419                                (mailcap-extension-to-mime
420                                 (match-string 0 filename)))
421                           "application/octet-stream"))
422               (insert " buffer=\"" bufname "\"")
423               (insert " filename=\"" filename "\"")
424               (insert " disposition=\"inline\"")
425               (insert "><#/part>\n")
426               (setq p (point)))
427           (delete-region p (match-end 0))
428           (setq count 1)
429           (while (and (> count 0) 
430                       (re-search-forward "</div>\\|\\(<div>\\)" nil t))
431             (if (match-string 1)
432                 (setq count (1+ count))
433               (if (= (setq count (1- count)) 0)
434                   (delete-region (match-beginning 0)
435                                  (match-end 0)))))
436           (narrow-to-region p (point))
437           (goto-char (point-min))
438           (cond 
439            ((looking-at "<pre>")
440             (goto-char (match-end 0))
441             (if (looking-at "$") (forward-char))
442             (delete-region (point-min) (point))
443             (nnweb-remove-markup)
444             (nnweb-decode-entities)
445             nil)
446            (t
447             (setq mime t)
448             (insert "<#part type=\"text/html\" disposition=inline>")
449             (goto-char (point-max))
450             (insert "<#/part>")))
451           (goto-char (point-max))
452           (setq p (point))
453           (widen)))
454       (delete-region p (point-max))
455       (goto-char (point-min))
456       ;; Some blank line to seperate mails.
457       (insert "\n\nFrom nobody " (current-time-string) "\n")
458       (if id
459           (insert "Message-ID: <" id "@hotmail.com>\n"))
460       (unless (looking-at "$") 
461         (if (search-forward "\n\n" nil t)
462             (forward-line -1)
463           (webmail-error "article@2")))
464       (narrow-to-region (point) (point-max))
465       (if mime
466           (insert "MIME-Version: 1.0\n"
467                   (prog1
468                       (mml-generate-mime)
469                     (delete-region (point-min) (point-max)))))
470       (goto-char (point-min))
471       (widen)
472       (let (case-fold-search)
473         (while (re-search-forward "^From " nil t)
474           (beginning-of-line)
475           (insert ">"))))
476     (mm-append-to-file (point-min) (point-max) file)))
477
478 ;;; yahoo
479
480 (defun webmail-yahoo-open ()
481   (goto-char (point-min))
482   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
483       (setq webmail-aux (match-string 1))
484     (webmail-error "open@1")))
485
486 (defun webmail-yahoo-login ()
487   (goto-char (point-min))
488   (if (re-search-forward "http://[a-zA-Z][0-9]\\.mail\\.yahoo\\.com/" nil t)
489       (setq webmail-aux (match-string 0))
490     (webmail-error "login@1"))
491   (if (re-search-forward "YY=[0-9]+" nil t)
492       (setq webmail-aux (concat webmail-aux "ym/us/ShowFolder?"
493                                 (match-string 0)))
494     (webmail-error "login@2")))
495
496 (defun webmail-yahoo-list ()
497   (let (url (newp t) (tofetch 0))
498     (goto-char (point-min))
499     (when (re-search-forward 
500            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
501       ;;(setq listed (match-string 1))
502       (message "Found %s mail(s)" (match-string 2)))
503     (if (string-match "http://[^/]+" webmail-aux)
504         (setq webmail-aux (match-string 0 webmail-aux))
505       (webmail-error "list@1"))
506     (goto-char (point-min))
507     (while (re-search-forward 
508             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/us/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
509             nil t)
510       (if (setq url (match-string 1))
511           (progn
512             (when (or newp (not webmail-newmail-only))
513               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
514                     webmail-articles)
515               (setq tofetch (1+ tofetch)))
516             (setq newp t))
517         (setq newp nil)))
518     (message "Fetching %d mail(s)" tofetch)))
519
520 (defun webmail-yahoo-article (file id)
521   (let (p attachment)
522     (save-restriction
523       (goto-char (point-min))
524       (if (not (search-forward "value=\"Done\"" nil t))
525           (webmail-error "article@1"))
526       (if (not (search-forward "<table" nil t))
527           (webmail-error "article@2"))
528       (delete-region (point-min) (match-beginning 0)) 
529       (if (not (search-forward "</table>" nil t))
530           (webmail-error "article@3"))
531       (narrow-to-region (point-min) (match-end 0))
532       (while (search-forward "<a href=" nil t)
533         (setq p (match-beginning 0))
534         (search-forward "</a>" nil t)
535         (delete-region p (match-end 0)))
536       (nnweb-remove-markup)
537       (nnweb-decode-entities)
538       (goto-char (point-min))
539       (delete-blank-lines)
540       (goto-char (point-max))
541       (widen)
542       (insert "\n")
543       (setq p (point))
544       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
545         (setq attachment (match-string 0))
546         (let (bufname ct ctl cd description)
547           (if (not (search-forward "<table" nil t))
548               (webmail-error "article@4"))
549           (delete-region p (match-beginning 0))
550           (if (not (search-forward "</table>" nil t))
551               (webmail-error "article@5"))
552           (narrow-to-region p (match-end 0))
553           (nnweb-remove-markup)
554           (nnweb-decode-entities)
555           (goto-char (point-min))
556           (delete-blank-lines)
557           (setq ct (mail-fetch-field "content-type")
558                 ctl (ignore-errors (mail-header-parse-content-type ct))
559                 ;;cte (mail-fetch-field "content-transfer-encoding")
560                 cd (mail-fetch-field "content-disposition")
561                 description (mail-fetch-field "content-description")
562                 id (mail-fetch-field "content-id"))
563           (delete-region (point-min) (point-max))
564           (widen)
565           (save-excursion
566             (set-buffer (generate-new-buffer " *webmail-att*"))
567             (nnweb-insert (concat webmail-aux attachment))
568             (push (current-buffer) webmail-buffer-list)
569             (setq bufname (buffer-name)))
570           (insert "<#part")
571           (if (and ctl (not (equal (car ctl) "text/")))
572               (insert " type=\"" (car ctl) "\""))
573           (insert " buffer=\"" bufname "\"")
574           (if cd
575               (insert " disposition=\"" cd "\""))
576           (if description
577               (insert " description=\"" description "\""))
578           (insert "><#/part>\n")
579           (setq p (point))))
580       (delete-region p (point-max))
581       (goto-char (point-min))
582       ;; Some blank line to seperate mails.
583       (insert "\n\nFrom nobody " (current-time-string) "\n")
584       (if id
585           (insert "Message-ID: <" id "@yahoo.com>\n"))
586       (unless (looking-at "$") 
587         (if (search-forward "\n\n" nil t)
588             (forward-line -1)
589           (webmail-error "article@2")))
590       (narrow-to-region (point) (point-max))
591       (insert "MIME-Version: 1.0\n"
592               (prog1
593                   (mml-generate-mime)
594                 (delete-region (point-min) (point-max))))
595       (goto-char (point-min))
596       (widen)
597       (let (case-fold-search)
598         (while (re-search-forward "^From " nil t)
599           (beginning-of-line)
600           (insert ">"))))
601     (mm-append-to-file (point-min) (point-max) file)))
602
603 ;;; netaddress
604
605 (defun webmail-netaddress-open ()
606   (goto-char (point-min))
607   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
608       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
609     (webmail-error "open@1")))
610
611 (defun webmail-netaddress-login ()
612   (webmail-refresh-redirect)
613   (goto-char (point-min))
614   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
615       (setq webmail-session (match-string 1))
616     (webmail-error "login@1")))
617
618 (defun webmail-netaddress-list ()
619   (let (item id)
620     (goto-char (point-min))
621     (when (re-search-forward 
622            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
623       (message "Found %s mail(s), %s unread" 
624                (match-string 2) (match-string 1)))
625     (goto-char (point-min))
626     (while (re-search-forward 
627             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
628       (if (setq id (match-string 2))
629           (setq item 
630                 (cons id 
631                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
632                               (car webmail-open-url)
633                               webmail-session id)))
634         (if (or (not webmail-newmail-only)
635                 (equal (match-string 1) "True"))
636             (push item webmail-articles))))))
637
638 (defun webmail-netaddress-single-part ()
639   (goto-char (point-min))
640   (cond 
641    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
642     ;; text/plain
643     (replace-match "")
644     (while (re-search-forward "[\t\040\r\n]+" nil t)
645       (replace-match " "))
646     (goto-char (point-min))
647     (while (re-search-forward "<br>" nil t)
648       (replace-match "\n"))
649     (nnweb-remove-markup)
650     (nnweb-decode-entities) 
651     nil)
652    (t
653     (insert "<#part type=\"text/html\" disposition=inline>")
654     (goto-char (point-max))
655     (insert "<#/part>")
656     t)))
657
658 (defun webmail-netaddress-article (file id)
659   (let (p p1 attachment count mime type)
660     (save-restriction
661       (goto-char (point-min))
662       (if (not (search-forward "Trash" nil t))
663           (webmail-error "article@1"))
664       (if (not (search-forward "<form>" nil t))
665           (webmail-error "article@2"))
666       (delete-region (point-min) (match-beginning 0)) 
667       (if (not (search-forward "</form>" nil t))
668           (webmail-error "article@3"))
669       (narrow-to-region (point-min) (match-end 0))
670       (goto-char (point-min))
671       (while (re-search-forward "[\040\t\r\n]+" nil t)
672         (replace-match " "))
673       (goto-char (point-min))
674       (while (search-forward "<b>" nil t)
675         (replace-match "\n"))
676       (nnweb-remove-markup)
677       (nnweb-decode-entities)
678       (goto-char (point-min))
679       (delete-blank-lines)
680       (goto-char (point-min))
681       (while (re-search-forward "^\040+\\|\040+$" nil t)
682         (replace-match ""))
683       (goto-char (point-min))
684       (while (re-search-forward "\040+" nil t)
685         (replace-match " "))
686       (goto-char (point-max))
687       (widen)
688       (insert "\n\n")
689       (setq p (point))
690       (unless (search-forward "<!-- Data -->" nil t)
691         (webmail-error "article@4"))
692       (forward-line 14)
693       (delete-region p (point))
694       (goto-char (point-max))
695       (unless (re-search-backward 
696                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
697         (webmail-error "article@5"))
698       (delete-region (point) (point-max))
699       (goto-char p)
700       (while (search-forward
701               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
702               nil t 2)
703         (setq mime t)
704         (unless (search-forward "</TABLE>" nil t)
705           (webmail-error "article@6"))
706         (setq p1 (point))
707         (if (search-backward "<IMG " p t)
708             (progn
709               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
710                 (webmail-error "article@7"))
711               (setq attachment (match-string 1))
712               (setq type (match-string 2))
713               (unless (search-forward "</TABLE>" nil t)
714                 (webmail-error "article@8"))
715               (delete-region p (point))
716               (let (bufname);; Attachment
717                 (save-excursion
718                   (set-buffer (generate-new-buffer " *webmail-att*"))
719                   (nnweb-insert (concat (car webmail-open-url) attachment))
720                   (push (current-buffer) webmail-buffer-list)
721                   (setq bufname (buffer-name)))
722                 (insert "<#part type=" type)
723                 (insert " buffer=\"" bufname "\"")
724                 (insert " disposition=\"inline\"")
725                 (insert "><#/part>\n")
726                 (setq p (point))))
727           (delete-region p p1)
728           (narrow-to-region 
729            p
730            (if (search-forward 
731                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
732                 nil t)
733                (match-beginning 0)
734              (point-max)))
735           (webmail-netaddress-single-part)
736           (goto-char (point-max))
737           (setq p (point))
738           (widen)))
739       (unless mime
740         (narrow-to-region p (point-max))
741         (setq mime (webmail-netaddress-single-part))
742         (widen))
743       (goto-char (point-min))
744       ;; Some blank line to seperate mails.
745       (insert "\n\nFrom nobody " (current-time-string) "\n")
746       (if id
747           (insert "Message-ID: <" id "@usa.net>\n"))
748       (unless (looking-at "$") 
749         (if (search-forward "\n\n" nil t)
750             (forward-line -1)
751           (webmail-error "article@2")))
752       (when mime
753         (narrow-to-region (point-min) (point))
754         (goto-char (point-min))
755         (while (not (eobp))
756           (if (looking-at "MIME-Version\\|Content-Type")
757               (delete-region (point) 
758                              (progn
759                                (forward-line 1)
760                                (if (re-search-forward "^[^ \t]" nil t)
761                                    (goto-char (match-beginning 0))
762                                  (point-max))))
763             (forward-line 1)))
764         (goto-char (point-max))
765         (widen)
766         (narrow-to-region (point) (point-max))
767         (insert "MIME-Version: 1.0\n"
768                 (prog1
769                     (mml-generate-mime)
770                   (delete-region (point-min) (point-max))))
771         (goto-char (point-min))
772         (widen))
773       (let (case-fold-search)
774         (while (re-search-forward "^From " nil t)
775           (beginning-of-line)
776           (insert ">"))))
777     (mm-append-to-file (point-min) (point-max) file)))
778
779 ;;; my-deja
780
781 (defun webmail-my-deja-open ()
782   (webmail-refresh-redirect)
783   (goto-char (point-min))
784   (if (re-search-forward "action=\"\\([^\"]+login_confirm\\.xp[^\"]+\\)\"" 
785                          nil t)
786       (setq webmail-aux (match-string 1))
787     (webmail-error "open@1")))
788
789 (defun webmail-my-deja-list ()
790   (let (item id newp)
791     (goto-char (point-min))
792     (when (re-search-forward 
793            "(\\([0-9]+\\) message(s), \\([0-9]+\\) new, \\([0-9]+\\)&nbsp;k )"
794            nil t) 
795       (message "Found %s mail(s), %s unread, total size %s K" 
796                (match-string 1) (match-string 2) (match-string 3)))
797     (goto-char (point-min))
798     (while (re-search-forward 
799             "&#149; &nbsp;&nbsp;\\|\\(http:[^\"]+\\)/display_seemesg\\.femail\\?docid=\\([^&\"]+\\)"
800             nil t)
801       (if (setq id (match-string 2))
802           (when (or newp (not webmail-newmail-only))
803             (push
804              (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"
805                               (match-string 1) id))
806              webmail-articles)
807             (setq webmail-aux (match-string 1))
808             (setq newp nil))
809         (setq newp t)))
810     (setq webmail-articles (nreverse webmail-articles))))
811
812 (defun webmail-my-deja-article (file id)
813   (let (url)
814     (goto-char (point-min))
815     (unless (re-search-forward "\\(http:[^\"]+/attachment/entire_message.txt[^\"]+\\)" nil t)
816       (webmail-error "article@1"))
817     (setq url (match-string 1))
818     (erase-buffer)
819     (mm-with-unibyte-current-buffer
820       (nnweb-insert url))
821     (goto-char (point-min))
822     (while (search-forward "\r\n" nil t)
823       (replace-match "\n"))
824     (goto-char (point-min))
825     (insert "\n\nFrom nobody " (current-time-string) "\n")
826     (mm-append-to-file (point-min) (point-max) file)))
827
828 (provide 'webmail)
829
830 ;;; webmail.el ends here