Fix up the nnregistry entry.
[gnus] / lisp / webmail.el
1 ;;; webmail.el --- interface of web mail
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: hotmail netaddress
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Note: Now mail.yahoo.com provides POP3 service, the webmail
27 ;; fetching is not going to be supported.
28
29 ;; Note: You need to have `url' and `w3' installed for this backend to
30 ;; work. `w3' must be 4.0pre46+one-line-cookie patch or standalone
31 ;; `url'.
32
33 ;; Todo: To support more web mail servers.
34
35 ;; Known bugs:
36 ;; 1. Net@ddress may corrupt `X-Face'.
37
38 ;; Warning:
39 ;; Webmail is an experimental function, which means NO WARRANTY.
40
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
44
45 (require 'nnoo)
46 (require 'message)
47 (require 'gnus-util)
48 (require 'gnus)
49 (require 'nnmail)
50 (require 'mm-util)
51 (require 'mm-url)
52 (require 'mml)
53 (eval-when-compile
54   (ignore-errors
55     (require 'url)
56     (require 'url-cookie)))
57 ;; Report failure to find w3 at load time if appropriate.
58 (eval '(progn
59          (require 'url)
60          (require 'url-cookie)))
61
62 ;;;
63
64 (defvar webmail-type-definition
65   '((hotmail
66      ;; Hotmail hate other HTTP user agents and use one line cookie
67      (paranoid agent cookie post)
68      (address . "www.hotmail.com")
69      (open-url "http://www.hotmail.com/")
70      (open-snarf . webmail-hotmail-open)
71      ;; W3 hate redirect POST
72      (login-url
73       "http://%s/cgi-bin/dologin?login=%s&passwd=%s&enter=Sign+in&sec=no&curmbox=ACTIVE&_lang=&js=yes&id=2&tw=-10000&beta="
74       webmail-aux user password)
75      ;;(login-snarf . webmail-hotmail-login)
76      ;;(list-url "%s" webmail-aux)
77      (list-snarf . webmail-hotmail-list)
78      (article-snarf . webmail-hotmail-article)
79      (trash-url
80       "%s&login=%s&f=33792&curmbox=ACTIVE&_lang=&foo=inbox&js=&page=&%s=on&_HMaction=MoveTo&tobox=trAsH&nullbox="
81       webmail-aux user id))
82     (yahoo
83      (paranoid agent 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=&.src=ym&.last=&promo=&.intl=&.bypass=&.partner=&.chkP=Y&.done=&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/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&Domain=usa.net&NA31site=classic.netaddress.com&NA31port=80&UserID=%s&passwd=%s"
108       user password)
109      (login-snarf . webmail-netaddress-login)
110      (list-url
111       "http://www.netaddress.com/tpl/Mail/%s/List?FolderID=-4&SortUseCase=True"
112       webmail-session)
113      (list-snarf . webmail-netaddress-list)
114      (article-url "http://www.netaddress.com/")
115      (article-snarf . webmail-netaddress-article)
116      (trash-url
117       "http://www.netaddress.com/tpl/Message/%s/Move?FolderID=-4&Q=%s&N=&Sort=Date&F=-1"
118       webmail-session id))))
119
120 (defvar webmail-variables
121   '(address article-snarf article-url list-snarf list-url
122             login-url login-snarf open-url open-snarf site articles
123             post-process paranoid trash-url))
124
125 (defconst webmail-version "webmail 1.0")
126
127 (defvar webmail-newmail-only nil
128   "Only fetch new mails.")
129
130 (defvar webmail-move-to-trash-can t
131   "Move mail to trash can after fetch it.")
132
133 ;;; Internal variables
134
135 (defvar webmail-address nil)
136 (defvar webmail-paranoid nil)
137 (defvar webmail-aux nil)
138 (defvar webmail-session nil)
139 (defvar webmail-article-snarf nil)
140 (defvar webmail-article-url nil)
141 (defvar webmail-list-snarf nil)
142 (defvar webmail-list-url nil)
143 (defvar webmail-login-url nil)
144 (defvar webmail-login-snarf nil)
145 (defvar webmail-open-snarf nil)
146 (defvar webmail-open-url nil)
147 (defvar webmail-trash-url nil)
148 (defvar webmail-articles nil)
149 (defvar webmail-post-process nil)
150
151 (defvar webmail-buffer nil)
152 (defvar webmail-buffer-list nil)
153
154 (defvar webmail-type nil)
155
156 (defvar webmail-error-function nil)
157
158 (defvar webmail-debug-file "~/.emacs-webmail-debug")
159
160 ;;; Interface functions
161
162 (defun webmail-debug (str)
163   (with-temp-buffer
164     (insert "\n---------------- A bug at " str " ------------------\n")
165     (dolist (sym '(webmail-type user))
166       (if (boundp sym)
167           (gnus-pp `(setq ,sym ',(eval sym)))))
168     (insert "---------------- webmail buffer ------------------\n\n")
169     (insert-buffer-substring webmail-buffer)
170     (insert "\n---------------- end of buffer ------------------\n\n")
171     (append-to-file (point-min) (point-max) webmail-debug-file)))
172
173 (defun webmail-error (str)
174   (if webmail-error-function
175       (funcall webmail-error-function str))
176   (message "%s HTML has changed or your w3 package is too old.(%s)"
177            webmail-type str)
178   (error "%s HTML has changed or your w3 package is too old.(%s)"
179          webmail-type str))
180
181 (defun webmail-setdefault (type)
182   (let ((type-def (cdr (assq type webmail-type-definition)))
183         (vars webmail-variables)
184         pair)
185     (setq webmail-type type)
186     (dolist (var vars)
187       (if (setq pair (assq var type-def))
188           (set (intern (concat "webmail-" (symbol-name var))) (cdr pair))
189         (set (intern (concat "webmail-" (symbol-name var))) nil)))))
190
191 (defun webmail-eval (expr)
192   (cond
193    ((consp expr)
194     (cons (webmail-eval (car expr)) (webmail-eval (cdr expr))))
195    ((symbolp expr)
196     (eval expr))
197    (t
198     expr)))
199
200 (defun webmail-url (xurl)
201   (mm-with-unibyte-current-buffer
202     (cond
203      ((eq (car xurl) 'content)
204       (pop xurl)
205       (mm-url-fetch-simple (if (stringp (car xurl))
206                                 (car xurl)
207                               (apply 'format (webmail-eval (car xurl))))
208                             (apply 'format (webmail-eval (cdr xurl)))))
209      ((eq (car xurl) 'post)
210       (pop xurl)
211       (mm-url-fetch-form (car xurl) (webmail-eval (cdr xurl))))
212      (t
213       (mm-url-insert (apply 'format (webmail-eval xurl)))))))
214
215 (defun webmail-init ()
216   "Initialize buffers and such."
217   (if (gnus-buffer-live-p webmail-buffer)
218       (set-buffer webmail-buffer)
219     (setq webmail-buffer
220           (nnheader-set-temp-buffer " *webmail*"))
221     (mm-disable-multibyte)))
222
223 (defvar url-package-name)
224 (defvar url-package-version)
225 (defvar url-cookie-multiple-line)
226 (defvar url-confirmation-func)
227
228 ;; Hack W3 POST redirect.  See `url-parse-mime-headers'.
229 ;;
230 ;; Netscape uses "GET" as redirect method when orignal method is POST
231 ;; and status is 302, .i.e no security risks by default without
232 ;; confirmation.
233 ;;
234 ;; Some web servers (at least Apache used by yahoo) return status 302
235 ;; instead of 303, though they mean 303.
236
237 (defun webmail-url-confirmation-func (prompt)
238   (cond
239    ((equal prompt (concat "Honor redirection with non-GET method "
240                           "(possible security risks)? "))
241     nil)
242    ((equal prompt "Continue (with method of GET)? ")
243     t)
244    (t (error prompt))))
245
246 (defun webmail-refresh-redirect ()
247   "Redirect refresh url in META."
248   (goto-char (point-min))
249   (while (re-search-forward
250           "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\""
251           nil t)
252     (let ((url (match-string 1)))
253       (erase-buffer)
254       (mm-with-unibyte-current-buffer
255         (mm-url-insert url)))
256     (goto-char (point-min))))
257
258 (defun webmail-fetch (file subtype user password)
259   (save-excursion
260     (webmail-setdefault subtype)
261     (let ((url-package-name (if (memq 'agent webmail-paranoid)
262                                 "Mozilla"
263                               url-package-name))
264           (url-package-version (if (memq 'agent webmail-paranoid)
265                                    "4.0"
266                                  url-package-version))
267           (url-cookie-multiple-line (if (memq 'cookie webmail-paranoid)
268                                         nil
269                                       url-cookie-multiple-line))
270           (url-confirmation-func (if (memq 'post webmail-paranoid)
271                                      'webmail-url-confirmation-func
272                                    url-confirmation-func))
273           (url-http-silence-on-insecure-redirection t)
274           url-cookie-storage url-cookie-secure-storage
275           url-cookie-confirmation
276           item id (n 0))
277       (webmail-init)
278       (setq webmail-articles nil)
279       (when webmail-open-url
280         (erase-buffer)
281         (webmail-url webmail-open-url))
282       (if webmail-open-snarf (funcall webmail-open-snarf))
283       (when webmail-login-url
284         (erase-buffer)
285         (webmail-url webmail-login-url))
286       (if webmail-login-snarf
287           (funcall webmail-login-snarf))
288       (when webmail-list-url
289         (erase-buffer)
290         (webmail-url webmail-list-url))
291       (if webmail-list-snarf
292           (funcall webmail-list-snarf))
293       (while (setq item (pop webmail-articles))
294         (message "Fetching mail #%d..." (setq n (1+ n)))
295         (erase-buffer)
296         (mm-with-unibyte-current-buffer
297           (mm-url-insert (cdr item)))
298         (setq id (car item))
299         (if webmail-article-snarf
300             (funcall webmail-article-snarf file id))
301         (when (and webmail-trash-url webmail-move-to-trash-can)
302           (message "Move mail #%d to trash can..." n)
303           (condition-case err
304               (progn
305                 (webmail-url webmail-trash-url)
306                 (let (buf)
307                   (while (setq buf (pop webmail-buffer-list))
308                     (kill-buffer buf))))
309             (error
310              (let (buf)
311                (while (setq buf (pop webmail-buffer-list))
312                  (kill-buffer buf)))
313              (error err))))))
314     (if webmail-post-process
315         (funcall webmail-post-process))))
316
317 (defun webmail-encode-8bit ()
318   (goto-char (point-min))
319   (skip-chars-forward "^\200-\377")
320   (while (not (eobp))
321     (insert (format "&%d;" (mm-char-int (char-after))))
322     (delete-char 1)
323     (skip-chars-forward "^\200-\377")))
324
325 ;;; hotmail
326
327 (defun webmail-hotmail-open ()
328   (goto-char (point-min))
329   (if (re-search-forward
330        "action=\"https?://\\([^/]+\\)/cgi-bin/dologin" nil t)
331       (setq webmail-aux (match-string 1))
332     (webmail-error "open@1")))
333
334 (defun webmail-hotmail-login ()
335   (let (site)
336     (goto-char (point-min))
337     (if (re-search-forward
338          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
339         (setq site (match-string 1))
340       (webmail-error "login@1"))
341     (goto-char (point-min))
342     (if (re-search-forward
343          "\\(/cgi-bin/HoTMaiL\\?[^\"]*a=b[^\"]*\\)" nil t)
344         (setq webmail-aux (concat "http://" site (match-string 1)))
345       (webmail-error "login@2"))))
346
347 (defun webmail-hotmail-list ()
348   (goto-char (point-min))
349   (skip-chars-forward " \t\n\r")
350   (let (site url newp (total "0"))
351     (if (eobp)
352         (setq total "0")
353       (if (re-search-forward "\\([0-9]+\\) *<b>(\\([0-9]+\\) new)" nil t)
354           (message "Found %s (%s new)" (setq total (match-string 1))
355                    (match-string 2))
356         (if (re-search-forward "\\([0-9]+\\) new" nil t)
357             (message "Found %s new" (setq total (match-string 1)))
358           (webmail-error "list@0"))))
359     (unless (equal total "0")
360       (goto-char (point-min))
361       (if (re-search-forward
362          "https?://\\([^/]+hotmail\\.msn\\.com\\)/cgi-bin/" nil t)
363           (setq site (match-string 1))
364         (webmail-error "list@1"))
365       (goto-char (point-min))
366       (if (re-search-forward "disk=\\([^&]*\\)&" nil t)
367           (setq webmail-aux
368                 (concat "http://" site "/cgi-bin/HoTMaiL?disk="
369                         (match-string 1)))
370         (webmail-error "list@2"))
371       (goto-char (point-max))
372       (while (re-search-backward
373               "newmail\\.gif\\|href=\"\\(/cgi-bin/getmsg\\?[^\"]+\\)\""
374               nil t)
375         (if (setq url (match-string 1))
376             (progn
377               (if (or newp (not webmail-newmail-only))
378                   (let (id)
379                     (if (string-match "msg=\\([^&]+\\)" url)
380                         (setq id (match-string 1 url)))
381                     (push (cons id (concat "http://" site url "&raw=0"))
382                           webmail-articles)))
383               (setq newp nil))
384           (setq newp t))))))
385
386 ;; Thank victor@idaccr.org (Victor S. Miller) for raw=0
387
388 (defun webmail-hotmail-article (file id)
389   (goto-char (point-min))
390   (skip-chars-forward " \t\n\r")
391   (unless (eobp)
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     (mm-url-remove-markup)
400     (mm-url-decode-entities-nbsp)
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       (forward-line))
409     (insert "X-Gnus-Webmail: " (symbol-value 'user)
410             "@" (symbol-name webmail-type) "\n")
411     (mm-append-to-file (point-min) (point-max) file)))
412
413 (defun webmail-hotmail-article-old (file id)
414   (let (p attachment count mime hotmail-direct)
415     (save-restriction
416       (webmail-encode-8bit)
417       (goto-char (point-min))
418       (if (not (search-forward "<DIV>" nil t))
419           (if (not (search-forward "Reply&nbsp;All" nil t))
420               (webmail-error "article@1")
421             (setq hotmail-direct t))
422         (goto-char (match-beginning 0)))
423       (narrow-to-region (point-min) (point))
424       (if (not (search-backward "<table" nil t 2))
425           (webmail-error "article@1.1"))
426       (delete-region (point-min) (match-beginning 0))
427       (while (search-forward "<a href=" nil t)
428         (setq p (match-beginning 0))
429         (search-forward "</a>" nil t)
430         (delete-region p (match-end 0)))
431       (mm-url-remove-markup)
432       (mm-url-decode-entities-nbsp)
433       (goto-char (point-min))
434       (delete-blank-lines)
435       (goto-char (point-min))
436       (when (search-forward "\n\n" nil t)
437         (backward-char)
438         (delete-region (point) (point-max)))
439       (goto-char (point-max))
440       (widen)
441       (insert "\n")
442       (setq p (point))
443       (while (re-search-forward
444               "<tt>\\|<div>\\|\\(http://[^/]+/cgi-bin/getmsg/\\([^\?]+\\)\?[^\"]*\\)\""
445               nil t)
446         (if (setq attachment (match-string 1))
447             (let ((filename (match-string 2))
448                   bufname);; Attachment
449               (delete-region p (match-end 0))
450               (save-excursion
451                 (set-buffer (generate-new-buffer " *webmail-att*"))
452                 (mm-url-insert attachment)
453                 (push (current-buffer) webmail-buffer-list)
454                 (setq bufname (buffer-name)))
455               (setq mime t)
456               (insert "<#part type="
457                       (or (and filename
458                                (string-match "\\.[^\\.]+$" filename)
459                                (mailcap-extension-to-mime
460                                 (match-string 0 filename)))
461                           "application/octet-stream"))
462               (insert " buffer=\"" bufname "\"")
463               (insert " filename=\"" filename "\"")
464               (insert " disposition=\"inline\"")
465               (insert "><#/part>\n")
466               (setq p (point)))
467           (delete-region p (match-end 0))
468           (if hotmail-direct
469               (if (not (search-forward "</tt>" nil t))
470                   (webmail-error "article@1.2")
471                 (delete-region (match-beginning 0) (match-end 0)))
472             (setq count 1)
473             (while (and (> count 0)
474                         (re-search-forward "</div>\\|\\(<div>\\)" nil t))
475               (if (match-string 1)
476                   (setq count (1+ count))
477                 (if (= (setq count (1- count)) 0)
478                     (delete-region (match-beginning 0)
479                                    (match-end 0))))))
480           (narrow-to-region p (point))
481           (goto-char (point-min))
482           (cond
483            ((looking-at "<pre>")
484             (goto-char (match-end 0))
485             (if (looking-at "$") (forward-char))
486             (delete-region (point-min) (point))
487             (mm-url-remove-markup)
488             (mm-url-decode-entities-nbsp)
489             nil)
490            (t
491             (setq mime t)
492             (insert "<#part type=\"text/html\" disposition=inline>")
493             (goto-char (point-max))
494             (insert "<#/part>")))
495           (goto-char (point-max))
496           (setq p (point))
497           (widen)))
498       (delete-region p (point-max))
499       (goto-char (point-min))
500       ;; Some blank line to separate mails.
501       (insert "\n\nFrom nobody " (current-time-string) "\n")
502       (insert "X-Gnus-Webmail: " (symbol-value 'user)
503               "@" (symbol-name webmail-type) "\n")
504       (if id
505           (insert (format "X-Message-ID: <%s@hotmail.com>\n" id)))
506       (unless (looking-at "$")
507         (if (search-forward "\n\n" nil t)
508             (forward-line -1)
509           (webmail-error "article@2")))
510       (narrow-to-region (point) (point-max))
511       (if mime
512           (insert "MIME-Version: 1.0\n"
513                   (prog1
514                       (mml-generate-mime)
515                     (delete-region (point-min) (point-max)))))
516       (goto-char (point-min))
517       (widen)
518       (let (case-fold-search)
519         (while (re-search-forward "^From " nil t)
520           (beginning-of-line)
521           (insert ">"))))
522     (mm-append-to-file (point-min) (point-max) file)))
523
524 ;;; yahoo
525
526 (defun webmail-yahoo-open ()
527   (goto-char (point-min))
528   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
529       (setq webmail-aux (match-string 1))
530     (webmail-error "open@1")))
531
532 (defun webmail-yahoo-login ()
533   (goto-char (point-min))
534   (if (re-search-forward "http://[^/]+[0-9]\\.mail\\.yahoo\\.com/" nil t)
535       (setq webmail-aux (match-string 0))
536     (webmail-error "login@1"))
537   (if (re-search-forward "YY=[0-9]+" nil t)
538       (setq webmail-aux (concat webmail-aux "ym/ShowFolder?"
539                                 (match-string 0)))
540     (webmail-error "login@2")))
541
542 (defun webmail-yahoo-list ()
543   (let (url (newp t) (tofetch 0))
544     (goto-char (point-min))
545     (when (re-search-forward
546            "showing [0-9]+-\\([0-9]+\\) of \\([0-9]+\\)" nil t)
547       ;;(setq listed (match-string 1))
548       (message "Found %s mail(s)" (match-string 2)))
549     (if (string-match "http://[^/]+" webmail-aux)
550         (setq webmail-aux (match-string 0 webmail-aux))
551       (webmail-error "list@1"))
552     (goto-char (point-min))
553     (while (re-search-forward
554             "bgcolor=\"#eeeeee\"\\|href=\"\\(/ym/ShowLetter\\?MsgId=\\([^&]+\\)&[^\"]*\\)\""
555             nil t)
556       (if (setq url (match-string 1))
557           (progn
558             (when (or newp (not webmail-newmail-only))
559               (push (cons (match-string 2) (concat webmail-aux url "&toc=1"))
560                     webmail-articles)
561               (setq tofetch (1+ tofetch)))
562             (setq newp t))
563         (setq newp nil)))
564     (setq webmail-articles (nreverse webmail-articles))
565     (message "Fetching %d mail(s)" tofetch)))
566
567 (defun webmail-yahoo-article (file id)
568   (let (p attachment)
569     (save-restriction
570       (goto-char (point-min))
571       (if (not (search-forward "value=\"Done\"" nil t))
572           (webmail-error "article@1"))
573       (if (not (search-forward "<table" nil t))
574           (webmail-error "article@2"))
575       (delete-region (point-min) (match-beginning 0))
576       (if (not (search-forward "</table>" nil t))
577           (webmail-error "article@3"))
578       (narrow-to-region (point-min) (match-end 0))
579       (while (search-forward "<a href=" nil t)
580         (setq p (match-beginning 0))
581         (search-forward "</a>" nil t)
582         (delete-region p (match-end 0)))
583       (mm-url-remove-markup)
584       (mm-url-decode-entities-nbsp)
585       (goto-char (point-min))
586       (delete-blank-lines)
587       (goto-char (point-max))
588       (widen)
589       (insert "\n")
590       (setq p (point))
591       (while (re-search-forward "[^\"]*/ShowLetter/[^\?]+\?[^\"]*" nil t)
592         (setq attachment (match-string 0))
593         (let (bufname ct ctl cd description)
594           (if (not (search-forward "<table" nil t))
595               (webmail-error "article@4"))
596           (delete-region p (match-beginning 0))
597           (if (not (search-forward "</table>" nil t))
598               (webmail-error "article@5"))
599           (narrow-to-region p (match-end 0))
600           (mm-url-remove-markup)
601           (mm-url-decode-entities-nbsp)
602           (goto-char (point-min))
603           (delete-blank-lines)
604           (setq ct (mail-fetch-field "content-type")
605                 ctl (and ct (mail-header-parse-content-type ct))
606                 ;;cte (mail-fetch-field "content-transfer-encoding")
607                 cd (mail-fetch-field "content-disposition")
608                 description (mail-fetch-field "content-description")
609                 id (mail-fetch-field "content-id"))
610           (delete-region (point-min) (point-max))
611           (widen)
612           (save-excursion
613             (set-buffer (generate-new-buffer " *webmail-att*"))
614             (mm-url-insert (concat webmail-aux attachment))
615             (push (current-buffer) webmail-buffer-list)
616             (setq bufname (buffer-name)))
617           (insert "<#part")
618           (if (and ctl (not (equal (car ctl) "text/")))
619               (insert " type=\"" (car ctl) "\""))
620           (insert " buffer=\"" bufname "\"")
621           (if cd
622               (insert " disposition=\"" cd "\""))
623           (if description
624               (insert " description=\"" description "\""))
625           (insert "><#/part>\n")
626           (setq p (point))))
627       (delete-region p (point-max))
628       (goto-char (point-min))
629       ;; Some blank line to separate mails.
630       (insert "\n\nFrom nobody " (current-time-string) "\n")
631       (insert "X-Gnus-Webmail: " (symbol-value 'user)
632               "@" (symbol-name webmail-type) "\n")
633       (if id
634           (insert (format "X-Message-ID: <%s@yahoo.com>\n" id)))
635       (unless (looking-at "$")
636         (if (search-forward "\n\n" nil t)
637             (forward-line -1)
638           (webmail-error "article@2")))
639       (narrow-to-region (point) (point-max))
640       (insert "MIME-Version: 1.0\n"
641               (prog1
642                   (mml-generate-mime)
643                 (delete-region (point-min) (point-max))))
644       (goto-char (point-min))
645       (widen)
646       (let (case-fold-search)
647         (while (re-search-forward "^From " nil t)
648           (beginning-of-line)
649           (insert ">"))))
650     (mm-append-to-file (point-min) (point-max) file)))
651
652 ;;; netaddress
653
654 (defun webmail-netaddress-open ()
655   (goto-char (point-min))
656   (if (re-search-forward "action=\"\\([^\"]+\\)\"" nil t)
657       (setq webmail-aux (concat (car webmail-open-url) (match-string 1)))
658     (webmail-error "open@1")))
659
660 (defun webmail-netaddress-login ()
661   (webmail-refresh-redirect)
662   (goto-char (point-min))
663   (if (re-search-forward  "tpl/[^/]+/\\([^/]+\\)" nil t)
664       (setq webmail-session (match-string 1))
665     (webmail-error "login@1")))
666
667 (defun webmail-netaddress-list ()
668   (webmail-refresh-redirect)
669   (let (item id)
670     (goto-char (point-min))
671     (when (re-search-forward
672            "(\\([0-9]+\\) unread, \\([0-9]+\\) total)" nil t)
673       (message "Found %s mail(s), %s unread"
674                (match-string 2) (match-string 1)))
675     (goto-char (point-min))
676     (while (re-search-forward
677             "MR\\[i\\]\\.R='\\([^']*\\)'\\|MR\\[i\\]\\.Q='\\([^']+\\)'" nil t)
678       (if (setq id (match-string 2))
679           (setq item
680                 (cons id
681                       (format "%s/tpl/Message/%s/Read?Q=%s&FolderID=-4&SortUseCase=True&Sort=Date&Headers=True"
682                               (car webmail-article-url)
683                               webmail-session id)))
684         (if (or (not webmail-newmail-only)
685                 (equal (match-string 1) "True"))
686             (push item webmail-articles))))
687     (setq webmail-articles (nreverse webmail-articles))))
688
689 (defun webmail-netaddress-single-part ()
690   (goto-char (point-min))
691   (cond
692    ((looking-at "[\t\040\r\n]*<font face=[^>]+>[\t\040\r\n]*")
693     ;; text/plain
694     (replace-match "")
695     (while (re-search-forward "[\t\040\r\n]+" nil t)
696       (replace-match " "))
697     (goto-char (point-min))
698     (while (re-search-forward "<br>" nil t)
699       (replace-match "\n"))
700     (mm-url-remove-markup)
701     (mm-url-decode-entities-nbsp)
702     nil)
703    (t
704     (insert "<#part type=\"text/html\" disposition=inline>")
705     (goto-char (point-max))
706     (insert "<#/part>")
707     t)))
708
709 (defun webmail-netaddress-article (file id)
710   (webmail-refresh-redirect)
711   (let (p p1 attachment count mime type)
712     (save-restriction
713       (webmail-encode-8bit)
714       (goto-char (point-min))
715       (if (not (search-forward "Trash" nil t))
716           (webmail-error "article@1"))
717       (if (not (search-forward "<form>" nil t))
718           (webmail-error "article@2"))
719       (delete-region (point-min) (match-beginning 0))
720       (if (not (search-forward "</form>" nil t))
721           (webmail-error "article@3"))
722       (narrow-to-region (point-min) (match-end 0))
723       (goto-char (point-min))
724       (while (re-search-forward "[\040\t\r\n]+" nil t)
725         (replace-match " "))
726       (goto-char (point-min))
727       (while (search-forward "<b>" nil t)
728         (replace-match "\n"))
729       (mm-url-remove-markup)
730       (mm-url-decode-entities-nbsp)
731       (goto-char (point-min))
732       (delete-blank-lines)
733       (goto-char (point-min))
734       (while (re-search-forward "^\040+\\|\040+$" nil t)
735         (replace-match ""))
736       (goto-char (point-min))
737       (while (re-search-forward "\040+" nil t)
738         (replace-match " "))
739       (goto-char (point-max))
740       (widen)
741       (insert "\n\n")
742       (setq p (point))
743       (unless (search-forward "<!-- Data -->" nil t)
744         (webmail-error "article@4"))
745       (forward-line 14)
746       (delete-region p (point))
747       (goto-char (point-max))
748       (unless (re-search-backward
749                "[\040\t]*<br>[\040\t\r\n]*<br>[\040\t\r\n]*<form" p t)
750         (webmail-error "article@5"))
751       (delete-region (point) (point-max))
752       (goto-char p)
753       (while (search-forward
754               "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
755               nil t 2)
756         (setq mime t)
757         (unless (search-forward "</TABLE>" nil t)
758           (webmail-error "article@6"))
759         (setq p1 (point))
760         (if (search-backward "<IMG " p t)
761             (progn
762               (unless (re-search-forward "HREF=\"\\(/tpl/Attachment/[^/]+/\\([^/]+/[^\?]+\\)[^\"]+\\)\"" p1 t)
763                 (webmail-error "article@7"))
764               (setq attachment (match-string 1))
765               (setq type (match-string 2))
766               (unless (search-forward "</TABLE>" nil t)
767                 (webmail-error "article@8"))
768               (delete-region p (point))
769               (let (bufname);; Attachment
770                 (save-excursion
771                   (set-buffer (generate-new-buffer " *webmail-att*"))
772                   (mm-url-insert (concat (car webmail-open-url) attachment))
773                   (push (current-buffer) webmail-buffer-list)
774                   (setq bufname (buffer-name)))
775                 (insert "<#part type=" type)
776                 (insert " buffer=\"" bufname "\"")
777                 (insert " disposition=\"inline\"")
778                 (insert "><#/part>\n")
779                 (setq p (point))))
780           (delete-region p p1)
781           (narrow-to-region
782            p
783            (if (search-forward
784                 "<TABLE border=\"0\" WIDTH=\"98%\" cellpadding=0 cellspacing=0>"
785                 nil t)
786                (match-beginning 0)
787              (point-max)))
788           (webmail-netaddress-single-part)
789           (goto-char (point-max))
790           (setq p (point))
791           (widen)))
792       (unless mime
793         (narrow-to-region p (point-max))
794         (setq mime (webmail-netaddress-single-part))
795         (widen))
796       (goto-char (point-min))
797       ;; Some blank line to separate mails.
798       (insert "\n\nFrom nobody " (current-time-string) "\n")
799       (insert "X-Gnus-Webmail: " (symbol-value 'user)
800               "@" (symbol-name webmail-type) "\n")
801       (if id
802           (insert (format "X-Message-ID: <%s@%s>\n" id webmail-address)))
803       (unless (looking-at "$")
804         (if (search-forward "\n\n" nil t)
805             (forward-line -1)
806           (webmail-error "article@2")))
807       (when mime
808         (narrow-to-region (point-min) (point))
809         (goto-char (point-min))
810         (while (not (eobp))
811           (if (looking-at "MIME-Version\\|Content-Type")
812               (delete-region (point)
813                              (progn
814                                (forward-line 1)
815                                (if (re-search-forward "^[^ \t]" nil t)
816                                    (goto-char (match-beginning 0))
817                                  (point-max))))
818             (forward-line 1)))
819         (goto-char (point-max))
820         (widen)
821         (narrow-to-region (point) (point-max))
822         (insert "MIME-Version: 1.0\n"
823                 (prog1
824                     (mml-generate-mime)
825                   (delete-region (point-min) (point-max))))
826         (goto-char (point-min))
827         (widen))
828       (let (case-fold-search)
829         (while (re-search-forward "^From " nil t)
830           (beginning-of-line)
831           (insert ">"))))
832     (mm-append-to-file (point-min) (point-max) file)))
833
834 (provide 'webmail)
835
836 ;;; webmail.el ends here