/
account.py
160 lines (101 loc) · 3.89 KB
/
account.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# coding=utf8
import encryption, contact, sqlite
class Account:
def __init__(self, fields):
self.id = fields['intAccountId']
self.type = fields['strType']
self.userAddress = fields['strUserAddress']
class ImapAccount(Account):
def __init__(self, fields, decryptionKey):
Account.__init__(self, fields)
self.server = fields['strServer']
self.port = fields['intPort']
self.username = fields['strUsername']
self.password = encryption.decrypt(decryptionKey, str(fields['strPassword']))
class SmsAccount(Account):
def __init__(self, fields):
Account.__init__(self, fields)
self.defaultCountry = fields['strDefaultCountry']
class IMAccount(Account):
def __init__(self, fields):
Account.__init__(self, fields)
self.service = fields['strService']
self.username = fields['strUsername']
def getImapAccounts(db, decryptionKey):
sql = """
SELECT
e.intAccountId,
a.strType, a.strUserAddress,
e.strServer, e.intPort,
e.strUsername, e.strPassword
FROM aAccount a
INNER JOIN aEmailAccount e ON e.intAccountId = a.intId
WHERE a.strType = 'imap'"""
accounts = db.executeMany(sql)
if not accounts:
return None
return [ImapAccount(account, decryptionKey) for account in accounts]
def getSmsAccounts(db):
sql = """
SELECT
s.intAccountId,
a.strType, a.strUserAddress,
s.strDefaultCountry
FROM aAccount a
INNER JOIN aSmsAccount s ON s.intAccountId = a.intId
WHERE strType = 'iPhone SMS'"""
accounts = db.executeMany(sql)
if not accounts:
return None
return [SmsAccount(account) for account in accounts]
def getIMAccounts(db):
sql = """
SELECT
s.intAccountId,
a.strType, a.strUserAddress,
s.strService, s.strUsername
FROM aAccount a
INNER JOIN aIMAccount i ON i.intAccountId = a.intId
WHERE a.strType = 'IM'"""
accounts = db.executeMany(sql)
if not accounts:
return None
return [IMAccount(account) for account in accounts]
def getAllAccounts(db):
return getImapAccounts(db) + getSmsAccounts(db)
def createAccount(db, type, userAddress):
sql = """
SELECT intId
FROM aAccount
WHERE strType = ?
AND strUserAddress = ?"""
result = db.executeOne(sql, (type, userAddress))
if result:
return result['intId']
else:
sql = """
INSERT INTO aAccount (strType, strUserAddress)
VALUES (?, ?)"""
return db.executeNoneReturnId(sql, (type, userAddress))
def createIMAccount(db, service, username):
if service == 'GTalk':
username = username.replace('@googlemail.com', '@gmail.com')
if username.find('@') == -1:
username = username + '@gmail.com'
ourContactId = contact.addAddressToMe(db, 'IM', username)
sql = """
SELECT intAccountId
FROM aIMAccount
WHERE strService = ?
AND strUsername = ?"""
result = db.executeOne(sql, (service, username))
if result:
return result['intAccountId'], ourContactId
else:
accountId = createAccount(db, 'IM', username)
# We need the OR IGNORE as createAccount() could return an existing account ID.
sql = """
INSERT OR IGNORE INTO aIMAccount (intAccountId, strService, strUsername)
VALUES (?, ?, ?)"""
db.executeNone(sql, (accountId, service, username))
return accountId, ourContactId