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