summaryrefslogtreecommitdiff
path: root/service/pixelated/adapter/mail_service.py
blob: 5a60d1bb23eca93463232952f6fab39f59c3f6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated 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.
#
# Pixelated 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 Pixelated. If not, see <http://www.gnu.org/licenses/>.
import traceback
import sys
import os
from twisted.internet import defer
from pixelated.bitmask_libraries.config import LeapConfig
from pixelated.bitmask_libraries.provider import LeapProvider
from pixelated.bitmask_libraries.session import LeapSessionFactory
from pixelated.bitmask_libraries.auth import LeapCredentials
from pixelated.adapter.pixelated_mailbox import PixelatedMailbox
from pixelated.adapter.tag import Tag


def open_leap_session(username, password, server_name):
    try:
        certs_home = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "certificates"))

        config = LeapConfig(certs_home=certs_home)
        provider = LeapProvider(server_name, config)
        session = LeapSessionFactory(provider).create(LeapCredentials(username, password))
        return session
    except:
        traceback.print_exc(file=sys.stdout)
        raise


class MailService:
    __slots__ = ['leap_session', 'account', 'mailbox_name']

    def __init__(self, leap_session):
        self.leap_session = leap_session
        self.account = leap_session.account
        self.mailbox_name = 'INBOX'

    @property
    def mailbox(self):
        return PixelatedMailbox(self.account.getMailbox(self.mailbox_name))

    def mails(self, query):
        if not query:
            return self.mailbox.mails()

        mails = []
        if query['tags']:
            tags = [Tag(tag) for tag in query['tags']]
            for leap_mailbox_name in self.account.mailboxes:
                mailbox = PixelatedMailbox(self.account.getMailbox(leap_mailbox_name))
                if len(mailbox.all_tags().intersection(tags)):
                    # mailbox has at least one mail with tag
                    for mail in mailbox.mails():
                        if len(mail.tags.intersection(tags)) > 0:
                            mails.append(mail)
        return mails

    def update_tags(self, mail_id, new_tags):
        mail = self.mail(mail_id)
        tags = set(Tag(str_tag) for str_tag in new_tags)
        current_tags, removed_tags = mail.update_tags(tags)
        self._update_mail_flags(current_tags, removed_tags, mail_id)
        self._update_mailbox_tags(tags)
        return current_tags

    def _update_mailbox_tags(self, tags):
        self.mailbox.update_tags(tags)

    def _update_mail_flags(self, current_tags, removed_tags, mail_id):
        new_flags = ['tag_' + tag.name for tag in current_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS]
        self._append_mail_flags(mail_id, new_flags)

        removed_flags = ['tag_' + tag.name for tag in removed_tags if tag.name not in PixelatedMailbox.SPECIAL_TAGS]
        self._remove_mail_flags(mail_id, removed_flags)

    def _append_mail_flags(self, mail_id, flags):
        self._set_mail_flags(mail_id, flags, 1)

    def _remove_mail_flags(self, mail_id, flags):
        self._set_mail_flags(mail_id, flags, -1)

    def _set_mail_flags(self, mail_id, flags, operation):
        observer = defer.Deferred()
        leap_mailbox = self.account.getMailbox(self.mailbox_name)
        self.mailbox.messages.set_flags(leap_mailbox, [mail_id], tuple(flags), operation, observer)

    def mail(self, mail_id):
        return self.mailbox.mail(mail_id)

    def all_tags(self):
        return self.mailbox.all_tags()

    def thread(self, thread_id):
        raise NotImplementedError()

    def mark_as_read(self, mail_id):
        raise NotImplementedError()

    def tags_for_thread(self, thread):
        raise NotImplementedError()

    def add_tag_to_thread(self, thread_id, tag):
        raise NotImplementedError()

    def remove_tag_from_thread(self, thread_id, tag):
        raise NotImplementedError()

    def delete_mail(self, mail_id):
        raise NotImplementedError()

    def save_draft(self, draft):
        raise NotImplementedError()

    def send_draft(self, draft):
        raise NotImplementedError()

    def draft_reply_for(self, mail_id):
        raise NotImplementedError()

    def all_contacts(self, query):
        raise NotImplementedError()

    def drafts(self):
        raise NotImplementedError()