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 00000000..109169d7 Binary files /dev/null and b/modules/guerrillamail/favicon.png differ 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)