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