From 407774937dbb5be1a2f40a7e4949381fd9ae4c6a Mon Sep 17 00:00:00 2001 From: Vincent A Date: Wed, 1 Jan 2014 02:19:24 +0100 Subject: [PATCH] new messages module: guerrillamail --- modules/guerrillamail/__init__.py | 24 +++++++++ modules/guerrillamail/backend.py | 86 ++++++++++++++++++++++++++++++ modules/guerrillamail/browser.py | 70 ++++++++++++++++++++++++ modules/guerrillamail/favicon.png | Bin 0 -> 1081 bytes modules/guerrillamail/test.py | 37 +++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 modules/guerrillamail/__init__.py create mode 100644 modules/guerrillamail/backend.py create mode 100644 modules/guerrillamail/browser.py create mode 100644 modules/guerrillamail/favicon.png create mode 100644 modules/guerrillamail/test.py diff --git a/modules/guerrillamail/__init__.py b/modules/guerrillamail/__init__.py new file mode 100644 index 00000000..9b135b3a --- /dev/null +++ b/modules/guerrillamail/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Vincent A +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from .backend import GuerrillamailBackend + + +__all__ = ['GuerrillamailBackend'] diff --git a/modules/guerrillamail/backend.py b/modules/guerrillamail/backend.py new file mode 100644 index 00000000..fbcc6423 --- /dev/null +++ b/modules/guerrillamail/backend.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Vincent A +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.tools.backend import BaseBackend, BackendConfig +from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Thread, Message +from weboob.tools.value import Value + +from .browser import GuerrillamailBrowser + + +__all__ = ['GuerrillamailBackend'] + + +class GuerrillamailBackend(BaseBackend, ICapMessages, ICapMessagesPost): + NAME = 'guerrillamail' + DESCRIPTION = u'GuerrillaMail temp mailbox' + MAINTAINER = u'Vincent A' + EMAIL = 'dev@indigo.re' + LICENSE = 'AGPLv3+' + VERSION = '0.i' + + BROWSER = GuerrillamailBrowser + + CONFIG = BackendConfig(Value('inbox', label='Inbox', default='')) + + def iter_threads(self): + inbox = self.config['inbox'].get() + if not inbox: + raise NotImplementedError() + else: + return [self.get_thread(inbox)] + + def get_thread(self, _id): + t = Thread(_id) + t.title = 'Mail for %s' % _id + t.flags = t.IS_DISCUSSION + + first = True + for d in self.browser.get_mails(_id): + m = self.make_message(d, t) + + if not m.content: + m.content = self.browser.get_mail_content(m.id) + + if first: + first = False + t.root = m + else: + m.parent = t.root + m.parent.children.append(m) + + return t + + def post_message(self, m): + raise NotImplementedError() + for receiver in m.receivers: + self.browser.send_mail(m.sender, receiver, m.title, m.content) + + def make_message(self, d, thread): + m = Message(thread, d['id']) + m.children = [] + m.sender = d['from'] + m.flags = 0 + if not d.get('read', True): + m.flags = m.IS_UNREAD + m.title = d['subject'] + m.date = d['datetime'] + m.receivers = [d['to']] + return m diff --git a/modules/guerrillamail/browser.py b/modules/guerrillamail/browser.py new file mode 100644 index 00000000..260df5d4 --- /dev/null +++ b/modules/guerrillamail/browser.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Vincent A +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.tools.browser import BaseBrowser +from weboob.tools.date import datetime +from weboob.tools.parsers.jsonparser import json +from urllib import urlencode + +#from .pages import Page1, Page2 + + +__all__ = ['GuerrillamailBrowser'] + + +class GuerrillamailBrowser(BaseBrowser): + PROTOCOL = 'https' + DOMAIN = 'www.guerrillamail.com' + ENCODING = 'utf-8' + + def __init__(self, *args, **kw): + kw['parser'] = 'raw' + BaseBrowser.__init__(self, *args, **kw) + + def _get_unicode(self, url, *a): + return self.get_document(self.openurl(url, *a)).decode(self.ENCODING, 'replace') + + def _get_json(self, url, *a): + j = json.loads(self._get_unicode(url, *a)) + return j + + def get_mails(self, boxid): + params = {'email_user': boxid, 'lang': 'en', 'domain': 'guerrillamail.com'} + d = self._get_json('https://www.guerrillamail.com/ajax.php?f=set_email_user', urlencode(params)) + + d = self._get_json('https://www.guerrillamail.com/ajax.php?f=get_email_list&offset=0&domain=guerrillamail.com') + for m in d['list']: + info = {} + info['id'] = m['mail_id'] + info['from'] = m['mail_from'] +# info['to'] = m['mail_recipient'] + info['to'] = '%s@guerrillamail.com' % boxid + info['subject'] = m['mail_subject'] + info['datetime'] = datetime.fromtimestamp(int(m['mail_timestamp'])) + info['read'] = bool(int(m['mail_read'])) + yield info + + def get_mail_content(self, mailid): + d = self._get_json('https://www.guerrillamail.com/ajax.php?f=fetch_email&email_id=mr_%s&domain=guerrillamail.com' % mailid) + return d['mail_body'] + + def send_mail(self, from_, to, subject, body): + params = {'from': from_, 'to': to, 'subject': subject, 'body': body, 'attach': '', 'domain': 'guerrillamail.com'} + self._get_json('https://www.guerrillamail.com/ajax.php?f=send_email', urlencode(params)) diff --git a/modules/guerrillamail/favicon.png b/modules/guerrillamail/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..109169d73df21f67d299badd5a3a26d0c8339852 GIT binary patch literal 1081 zcmV-91jhS`P)e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00W^(L_t(|+U1(hYZE~f$6vxK zn1CX!#UBu>!9qc0@eq0lYU#mVQV?12;K7Th)~iVW01sYkLGWM?f|Upb3sMi#A`vA8 z5iJy2A`;M``16n`7KsO$nrvog-^|XNoyIQ&lHDcj_w&B*%}z+->dpQp7|{Ak18h|C ze>QIffVqx4yx)=l+<-{CAyN0bp#@w`{p{JE0N23avY{0m z)22m9D8>cAwPcy{VgQkJEnA~ary$v_<_jZ3P#77adk(Nc7=v3X0TkxLuCoe&M^t&y z^@-VuzNT%}EfPk4{_%4?0M6g|h`x4qqEGt!e`+v`0igsy#=Hs~YV)lSqyXqNF4D~0 zn6Y+412ARYGz{yX11_F@Knf!juK0$>oL48Jf4+0=H__viw{ z10nF&2TqLZE;$GdCvQ3-HV{t)QVx<&lPDKm?HW_{hvdKv5?(485$byg+OGa2a*gSAl;?IMkGX<>>WM+iQ zTyS(%9}8hPn~#(t-OHsDwfd?Mu`@t-cnZePHW49-J%(ei#VG#%w$o1NX zz;o#x+wA*ANatYO`~9mJ&W{ZkJW;R<0JUi?%b;IxKE3K%^XSdc!CgDM#(e7lM76-- z-s5C_Olcg`|4K7}H_0iWP(_|(2Zp^jtE?%p#GAPxvT^EzQj127E&10V_rWdJ5Y$Uu+< z5*YwB1Xc4`C|UqeLC|fU2SOWws1PX4iXi*}tf0321?vIm00000NkvXXu0mjf&Yahc literal 0 HcmV?d00001 diff --git a/modules/guerrillamail/test.py b/modules/guerrillamail/test.py new file mode 100644 index 00000000..7735d6be --- /dev/null +++ b/modules/guerrillamail/test.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Vincent A +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.tools.test import BackendTest +from uuid import uuid4 + + +class GuerrillamailTest(BackendTest): + BACKEND = 'guerrillamail' + + def test_guerrillamail(self): + box = uuid4() + thread = self.backend.get_thread(box) + self.assertTrue(thread) + message = thread.root + self.assertTrue(message) + self.assertTrue(message.sender) + self.assertTrue(message.title) + self.assertTrue(message.date) + self.assertTrue(message.receivers)