Enhance hotmail-snarf.
[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)) 
384                         webmail-articles)))
385             (setq newp nil))
386         (setq newp t)))))
387
388 (defun webmail-hotmail-article (file id)
389   (let (p attachment count mime hotmail-direct)
390     (save-restriction
391       (webmail-encode-8bit)
392       (goto-char (point-min))
393       (if (not (search-forward "<DIV>" nil t))
394           (if (not (search-forward "Reply&nbsp;All" nil t))
395               (webmail-error "article@1")
396             (setq hotmail-direct t))
397         (goto-char (match-beginning 0)))
398       (narrow-to-region (point-min) (point))
399       (if (not (search-backward "<table" nil t 2))
400           (webmail-error "article@1.1"))
401       (delete-region (point-min) (match-beginning 0)) 
402       (while (search-forward "<a href=" nil t)
403         (setq p (match-beginning 0))
404         (search-forward "</a>" nil t)
405         (delete-region p (match-end 0)))
406       (nnweb-remove-markup)
407       (nnweb-decode-entities)
408       (goto-char (point-min))
409       (delete-blank-lines)
410       (goto-char (point-min))
411       (when (search-forward "\n\n" nil t)
412         (backward-char)
413         (delete-region (point) (point-max)))
414       (goto-char (point-max))
415       (widen)
416       (insert "\n")
417       (setq p (point))
418       (while (re-search-forward 
419               "<tt>\\|<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\"" 
420               nil t)
421         (if (setq attachment (match-string 1))
422             (let ((filename (match-string 2))
423                   bufname);; Attachment
424               (delete-region p (match-end 0))
425               (save-excursion
426                 (set-buffer (generate-new-buffer " *webmail-att*"))
427                 (nnweb-insert attachment)
428                 (push (current-buffer) webmail-buffer-list)
429                 (setq bufname (buffer-name)))
430               (setq mime t)
431               (insert "<#part type=" 
432                       (or (and filename
433                                (string-match "\\.[^\\.]+$" filename)
434                                (mailcap-extension-to-mime
435                                 (match-string 0 filename)))
436                           "application/octet-stream"))
437               (insert " buffer=\"" bufname "\"")
438               (insert " filename=\"" filename "\"")
439               (insert " disposition=\"inline\"")
440               (insert "><#/part>\n")
441               (setq p (point)))
442           (delete-region p (match-end 0))
443           (if hotmail-direct
444               (if (not (search-forward "</tt>" nil t))
445                   (webmail-error "article@1.2")
446                 (delete-region (match-beginning 0) (match-end 0)))
447             (setq count 1)
448             (while (and (> count 0) 
449                         (re-search-forward "</div>\\|\\(<div>\\)" nil t))
450               (if (match-string 1)
451                   (setq count (1+ count))
452                 (if (= (setq count (1- count)) 0)
453                     (delete-region (match-beginning 0)
454                                    (match-end 0))))))
455           (narrow-to-region p (point))
456           (goto-char (point-min))
457           (cond 
458            ((looking-at "<pre>")
459             (goto-char (match-end 0))
460             (if (looking-at "$") (forward-char))
461             (delete-region (point-min) (point))
462             (nnweb-remove-markup)
463             (nnweb-decode-entities)
464             nil)
465            (t
466             (setq mime t)
467             (insert "<#part type=\"text/html\" disposition=inline>")
468             (goto-char (point-max))
469             (insert "<#/part>")))
470           (goto-char (point-max))
471           (setq p (point))
472           (widen)))
473       (delete-region p (point-max))
474       (goto-char (point-min))
475       ;; Some blank line to seperate mails.
476       (insert "\n\nFrom nobody " (current-time-string) "\n")
477       (if id
478           (insert (format "Message-ID: <%s@hotmail.com>\n" id)))
479       (unless (looking-at "$") 
480         (if (search-forward "\n\n" nil t)
481             (forward-line -1)
482           (webmail-error "article@2")))
483       (narrow-to-region (point) (point-max))
484       (if mime
485           (insert "MIME-Version: 1.0\n"
486                   (prog1
487                       (mml-generate-mime)
488                     (delete-region (point-min) (point-max)))))
489       (goto-char (point-min))
490       (widen)
491       (let (case-fold-search)
492         (while (re-search-forward "^From " nil t)
493           (beginning-of-line)
494           (insert ">"))))
495     (mm-append-to-file (point-min) (point-max) file)))
496
497 ;;; yahoo
498
499 (defun webmail-yahoo-open ()
500   (goto-char (point-min))
501   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
502       (setq webmail-aux (match-string 1))
503     (webmail-error "open@1")))
504
505 (defun webmail-yahoo-login ()
506   (goto-char (point-min))
507   (if (re-search-forward "http://[a-zA-Z][0-9]\\.mail\\.yahoo\\.com/" nil t)
508       (setq webmail-aux (match-string 0))
509     (webmail-error "login@1"))
510   (if (re-search-forward "YY=[0-9]+" nil t)
511       (setq webmail-aux (concat webmail-aux "ym/us/ShowFolder?"
512                                 (match-string 0)))
513     (webmail-error "login@2")))
514
515 (defun webmail-yahoo-list ()
516   (let (url (newp t) (tofetch 0))
517     (goto-char (point-min))
518     (when (re-search-forward 
519            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t) 
520       ;;(setq listed (match-string 1))
521       (message "Found %s mail(s)" (match-string 2)))
522     (if (string-match "http://[^/]+" webmail-aux)
523         (setq webmail-aux (match-string 0 webmail-aux))
524       (webmail-error "list@1"))
525     (goto-char (point-min))
526     (while (re-search-forward 
527             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/us/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
528             nil t)
529       (if (setq url (match-string 1))
530           (progn
531             (when (or newp (not webmail-newmail-only))
532               (push (cons (match-string 2) (concat webmail-aux url "&toc=1")) 
533                     webmail-articles)
534               (setq tofetch (1+ tofetch)))
535             (setq newp t))
536         (setq newp nil)))
537     (setq webmail-articles (nreverse webmail-articles))
538     (message "Fetching %d mail(s)" tofetch)))
539
540 (defun webmail-yahoo-article (file id)
541   (let (p attachment)
542     (save-restriction
543       (goto-char (point-min))
544       (if (not (search-forward "value=\"Done\"" nil t))
545           (webmail-error "article@1"))
546       (if (not (search-forward "<table" nil t))
547           (webmail-error "article@2"))
548       (delete-region (point-min) (match-beginning 0)) 
549       (if (not (search-forward "</table>" nil t))
550           (webmail-error "article@3"))
551       (narrow-to-region (point-min) (match-end 0))
552       (while (search-forward "<a href=" nil t)
553         (setq p (match-beginning 0))
554         (search-forward "</a>" nil t)
555         (delete-region p (match-end 0)))
556       (nnweb-remove-markup)
557       (nnweb-decode-entities)
558       (goto-char (point-min))
559       (delete-blank-lines)
560       (goto-char (point-max))
561       (widen)
562       (insert "\n")
563       (setq p (point))
564       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
565         (setq attachment (match-string 0))
566         (let (bufname ct ctl cd description)
567           (if (not (search-forward "<table" nil t))
568               (webmail-error "article@4"))
569           (delete-region p (match-beginning 0))
570           (if (not (search-forward "</table>" nil t))
571               (webmail-error "article@5"))
572           (narrow-to-region p (match-end 0))
573           (nnweb-remove-markup)
574           (nnweb-decode-entities)
575           (goto-char (point-min))
576           (delete-blank-lines)
577           (setq ct (mail-fetch-field "content-type")
578                 ctl (ignore-errors (mail-header-parse-content-type ct))
579                 ;;cte (mail-fetch-field "content-transfer-encoding")
580                 cd (mail-fetch-field "content-disposition")
581                 description (mail-fetch-field "content-description")
582                 id (mail-fetch-field "content-id"))
583           (delete-region (point-min) (point-max))
584           (widen)
585           (save-excursion
586             (set-buffer (generate-new-buffer " *webmail-att*"))
587             (nnweb-insert (concat webmail-aux attachment))
588             (push (current-buffer) webmail-buffer-list)
589             (setq bufname (buffer-name)))
590           (insert "<#part")
591           (if (and ctl (not (equal (car ctl) "text/")))
592               (insert " type=\"" (car ctl) "\""))
593           (insert " buffer=\"" bufname "\"")
594           (if cd
595               (insert " disposition=\"" cd "\""))
596           (if description
597               (insert " description=\"" description "\""))
598           (insert "><#/part>\n")
599           (setq p (point))))
600       (delete-region p (point-max))
601       (goto-char (point-min))
602       ;; Some blank line to seperate mails.
603       (insert "\n\nFrom nobody " (current-time-string) "\n")
604       (if id
605           (insert (format "Message-ID: <%s@yahoo.com>\n" id)))
606       (unless (looking-at "$") 
607         (if (search-forward "\n\n" nil t)
608             (forward-line -1)
609           (webmail-error "article@2")))
610       (narrow-to-region (point) (point-max))
611       (insert "MIME-Version: 1.0\n"
612               (prog1
613                   (mml-generate-mime)
614                 (delete-region (point-min) (point-max))))
615       (goto-char (point-min))
616       (widen)
617       (let (case-fold-search)
618         (while (re-search-forward "^From " nil t)
619           (beginning-of-line)
620           (insert ">"))))
621     (mm-append-to-file (point-min) (point-max) file)))
622
623 ;;; netaddress
624
625 (defun webmail-netaddress-open ()
626   (goto-char (point-min))
627   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
628       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
629     (webmail-error "open@1")))
630
631 (defun webmail-netaddress-login ()
632   (webmail-refresh-redirect)
633   (goto-char (point-min))
634   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
635       (setq webmail-session (match-string 1))
636     (webmail-error "login@1")))
637
638 (defun webmail-netaddress-list ()
639   (let (item id)
640     (goto-char (point-min))
641     (when (re-search-forward 
642            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t) 
643       (message "Found %s mail(s), %s unread" 
644                (match-string 2) (match-string 1)))
645     (goto-char (point-min))
646     (while (re-search-forward 
647             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
648       (if (setq id (match-string 2))
649           (setq item 
650                 (cons id 
651                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
652                               (car webmail-open-url)
653                               webmail-session id)))
654         (if (or (not webmail-newmail-only)
655                 (equal (match-string 1) "True"))
656             (push item webmail-articles))))
657     (setq webmail-articles (nreverse webmail-articles))))
658
659 (defun webmail-netaddress-single-part ()
660   (goto-char (point-min))
661   (cond 
662    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
663     ;; text/plain
664     (replace-match "")
665     (while (re-search-forward "[\t\040\r\n]+" nil t)
666       (replace-match " "))
667     (goto-char (point-min))
668     (while (re-search-forward "<br>" nil t)
669       (replace-match "\n"))
670     (nnweb-remove-markup)
671     (nnweb-decode-entities) 
672     nil)
673    (t
674     (insert "<#part type=\"text/html\" disposition=inline>")
675     (goto-char (point-max))
676     (insert "<#/part>")
677     t)))
678
679 (defun webmail-netaddress-article (file id)
680   (let (p p1 attachment count mime type)
681     (save-restriction
682       (webmail-encode-8bit)
683       (goto-char (point-min))
684       (if (not (search-forward "Trash" nil t))
685           (webmail-error "article@1"))
686       (if (not (search-forward "<form>" nil t))
687           (webmail-error "article@2"))
688       (delete-region (point-min) (match-beginning 0)) 
689       (if (not (search-forward "</form>" nil t))
690           (webmail-error "article@3"))
691       (narrow-to-region (point-min) (match-end 0))
692       (goto-char (point-min))
693       (while (re-search-forward "[\040\t\r\n]+" nil t)
694         (replace-match " "))
695       (goto-char (point-min))
696       (while (search-forward "<b>" nil t)
697         (replace-match "\n"))
698       (nnweb-remove-markup)
699       (nnweb-decode-entities)
700       (goto-char (point-min))
701       (delete-blank-lines)
702       (goto-char (point-min))
703       (while (re-search-forward "^\040+\\|\040+$" nil t)
704         (replace-match ""))
705       (goto-char (point-min))
706       (while (re-search-forward "\040+" nil t)
707         (replace-match " "))
708       (goto-char (point-max))
709       (widen)
710       (insert "\n\n")
711       (setq p (point))
712       (unless (search-forward "<!-- Data -->" nil t)
713         (webmail-error "article@4"))
714       (forward-line 14)
715       (delete-region p (point))
716       (goto-char (point-max))
717       (unless (re-search-backward 
718                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
719         (webmail-error "article@5"))
720       (delete-region (point) (point-max))
721       (goto-char p)
722       (while (search-forward
723               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
724               nil t 2)
725         (setq mime t)
726         (unless (search-forward "</TABLE>" nil t)
727           (webmail-error "article@6"))
728         (setq p1 (point))
729         (if (search-backward "<IMG " p t)
730             (progn
731               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
732                 (webmail-error "article@7"))
733               (setq attachment (match-string 1))
734               (setq type (match-string 2))
735               (unless (search-forward "</TABLE>" nil t)
736                 (webmail-error "article@8"))
737               (delete-region p (point))
738               (let (bufname);; Attachment
739                 (save-excursion
740                   (set-buffer (generate-new-buffer " *webmail-att*"))
741                   (nnweb-insert (concat (car webmail-open-url) attachment))
742                   (push (current-buffer) webmail-buffer-list)
743                   (setq bufname (buffer-name)))
744                 (insert "<#part type=" type)
745                 (insert " buffer=\"" bufname "\"")
746                 (insert " disposition=\"inline\"")
747                 (insert "><#/part>\n")
748                 (setq p (point))))
749           (delete-region p p1)
750           (narrow-to-region 
751            p
752            (if (search-forward 
753                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
754                 nil t)
755                (match-beginning 0)
756              (point-max)))
757           (webmail-netaddress-single-part)
758           (goto-char (point-max))
759           (setq p (point))
760           (widen)))
761       (unless mime
762         (narrow-to-region p (point-max))
763         (setq mime (webmail-netaddress-single-part))
764         (widen))
765       (goto-char (point-min))
766       ;; Some blank line to seperate mails.
767       (insert "\n\nFrom nobody " (current-time-string) "\n")
768       (if id
769           (insert (format "Message-ID: <%s@usa.net>\n" id)))
770       (unless (looking-at "$") 
771         (if (search-forward "\n\n" nil t)
772             (forward-line -1)
773           (webmail-error "article@2")))
774       (when mime
775         (narrow-to-region (point-min) (point))
776         (goto-char (point-min))
777         (while (not (eobp))
778           (if (looking-at "MIME-Version\\|Content-Type")
779               (delete-region (point) 
780                              (progn
781                                (forward-line 1)
782                                (if (re-search-forward "^[^ \t]" nil t)
783                                    (goto-char (match-beginning 0))
784                                  (point-max))))
785             (forward-line 1)))
786         (goto-char (point-max))
787         (widen)
788         (narrow-to-region (point) (point-max))
789         (insert "MIME-Version: 1.0\n"
790                 (prog1
791                     (mml-generate-mime)
792                   (delete-region (point-min) (point-max))))
793         (goto-char (point-min))
794         (widen))
795       (let (case-fold-search)
796         (while (re-search-forward "^From " nil t)
797           (beginning-of-line)
798           (insert ">"))))
799     (mm-append-to-file (point-min) (point-max) file)))
800
801 ;;; my-deja
802
803 (defun webmail-my-deja-open ()
804   (webmail-refresh-redirect)
805   (goto-char (point-min))
806   (if (re-search-forward "action=\"\\([^\"]+login_confirm\\.xp[^\"]+\\)\"" 
807                          nil t)
808       (setq webmail-aux (match-string 1))
809     (webmail-error "open@1")))
810
811 (defun webmail-my-deja-list ()
812   (let (item id newp)
813     (goto-char (point-min))
814     (when (re-search-forward 
815            "(\\([0-9]+\\) message(s), \\([0-9]+\\) new, \\([0-9]+\\)&nbsp;k )"
816            nil t) 
817       (message "Found %s mail(s), %s unread, total size %s K" 
818                (match-string 1) (match-string 2) (match-string 3)))
819     (goto-char (point-min))
820     (while (re-search-forward 
821             "&#149; &nbsp;&nbsp;\\|\\(http:[^\"]+\\)/display_seemesg\\.femail\\?docid=\\([^&\"]+\\)"
822             nil t)
823       (if (setq id (match-string 2))
824           (when (or newp (not webmail-newmail-only))
825             (push
826              (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"
827                               (match-string 1) id))
828              webmail-articles)
829             (setq webmail-aux (match-string 1))
830             (setq newp nil))
831         (setq newp t)))
832     (setq webmail-articles (nreverse webmail-articles))))
833
834 (defun webmail-my-deja-article (file id)
835   (let (url)
836     (goto-char (point-min))
837     (unless (re-search-forward "\\(http:[^\"]+/attachment/entire_message.txt[^\"]+\\)" nil t)
838       (webmail-error "article@1"))
839     (setq url (match-string 1))
840     (erase-buffer)
841     (mm-with-unibyte-current-buffer
842       (nnweb-insert url))
843     (goto-char (point-min))
844     (while (search-forward "\r\n" nil t)
845       (replace-match "\n"))
846     (goto-char (point-min))
847     (insert "\n\nFrom nobody " (current-time-string) "\n")
848     (mm-append-to-file (point-min) (point-max) file)))
849
850 (provide 'webmail)
851
852 ;;; webmail.el ends here