2015-10-03 01:17:47 +02:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
|
|
|
import shared
|
|
|
|
import re
|
|
|
|
import sys
|
2015-10-03 12:12:18 +02:00
|
|
|
import inspect
|
|
|
|
from helper_sql import *
|
2015-10-03 17:24:21 +02:00
|
|
|
from addresses import decodeAddress
|
2015-10-31 10:12:12 +01:00
|
|
|
from foldertree import AccountMixin
|
2015-10-03 17:24:21 +02:00
|
|
|
from pyelliptic.openssl import OpenSSL
|
2015-10-13 23:32:36 +02:00
|
|
|
from utils import str_broadcast_subscribers
|
2015-10-03 17:24:21 +02:00
|
|
|
import time
|
2015-10-03 01:17:47 +02:00
|
|
|
|
2015-10-19 20:03:06 +02:00
|
|
|
def getSortedAccounts():
|
|
|
|
configSections = filter(lambda x: x != 'bitmessagesettings', shared.config.sections())
|
|
|
|
configSections.sort(cmp =
|
|
|
|
lambda x,y: cmp(unicode(shared.config.get(x, 'label'), 'utf-8').lower(), unicode(shared.config.get(y, 'label'), 'utf-8').lower())
|
|
|
|
)
|
|
|
|
return configSections
|
|
|
|
|
2015-11-19 04:05:58 +01:00
|
|
|
def getSortedSubscriptions(count = False):
|
|
|
|
queryreturn = sqlQuery('SELECT label, address, enabled FROM subscriptions ORDER BY label COLLATE NOCASE ASC')
|
|
|
|
ret = {}
|
|
|
|
for row in queryreturn:
|
|
|
|
label, address, enabled = row
|
|
|
|
ret[address] = {}
|
|
|
|
ret[address]["inbox"] = {}
|
|
|
|
ret[address]["inbox"]['label'] = label
|
|
|
|
ret[address]["inbox"]['enabled'] = enabled
|
|
|
|
ret[address]["inbox"]['count'] = 0
|
|
|
|
if count:
|
|
|
|
queryreturn = sqlQuery('''SELECT fromaddress, folder, count(msgid) as cnt
|
|
|
|
FROM inbox, subscriptions
|
|
|
|
WHERE read = 0 AND subscriptions.address = inbox.fromaddress
|
|
|
|
GROUP BY inbox.fromaddress, folder''')
|
|
|
|
for row in queryreturn:
|
|
|
|
address, folder, cnt = row
|
|
|
|
ret[address][folder]['count'] = cnt
|
|
|
|
return ret
|
|
|
|
|
2015-10-03 01:17:47 +02:00
|
|
|
def accountClass(address):
|
|
|
|
if not shared.config.has_section(address):
|
2015-10-13 23:32:36 +02:00
|
|
|
if address == str_broadcast_subscribers:
|
|
|
|
subscription = BroadcastAccount(address)
|
2015-11-27 02:10:27 +01:00
|
|
|
if subscription.type != AccountMixin.BROADCAST:
|
2015-10-13 23:32:36 +02:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
subscription = SubscriptionAccount(address)
|
2015-11-27 02:10:27 +01:00
|
|
|
if subscription.type != AccountMixin.SUBSCRIPTION:
|
2015-10-13 23:32:36 +02:00
|
|
|
return None
|
|
|
|
return subscription
|
2015-10-03 01:17:47 +02:00
|
|
|
try:
|
|
|
|
gateway = shared.config.get(address, "gateway")
|
2015-10-03 12:12:18 +02:00
|
|
|
for name, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass):
|
|
|
|
# obj = g(address)
|
|
|
|
if issubclass(cls, GatewayAccount) and cls.gatewayName == gateway:
|
|
|
|
return cls(address)
|
|
|
|
# general gateway
|
|
|
|
return GatewayAccount(address)
|
2015-10-03 01:17:47 +02:00
|
|
|
except:
|
2015-10-03 12:12:18 +02:00
|
|
|
pass
|
|
|
|
# no gateway
|
|
|
|
return BMAccount(address)
|
2015-10-10 19:58:01 +02:00
|
|
|
|
2015-10-31 10:12:12 +01:00
|
|
|
class AccountColor(AccountMixin):
|
|
|
|
def __init__(self, address, type = None):
|
|
|
|
self.isEnabled = True
|
|
|
|
self.address = address
|
|
|
|
if type is None:
|
2015-11-27 02:10:27 +01:00
|
|
|
if address is None:
|
|
|
|
self.type = AccountMixin.ALL
|
|
|
|
elif shared.safeConfigGetBoolean(self.address, 'mailinglist'):
|
|
|
|
self.type = AccountMixin.MAILINGLIST
|
2015-10-31 10:12:12 +01:00
|
|
|
elif shared.safeConfigGetBoolean(self.address, 'chan'):
|
2015-11-27 02:10:27 +01:00
|
|
|
self.type = AccountMixin.CHAN
|
2015-10-31 10:12:12 +01:00
|
|
|
elif sqlQuery(
|
|
|
|
'''select label from subscriptions where address=?''', self.address):
|
2015-11-27 02:10:27 +01:00
|
|
|
self.type = AccountMixin.SUBSCRIPTION
|
2015-10-31 10:12:12 +01:00
|
|
|
else:
|
2015-11-27 02:10:27 +01:00
|
|
|
self.type = AccountMixin.NORMAL
|
2015-10-31 10:12:12 +01:00
|
|
|
else:
|
|
|
|
self.type = type
|
|
|
|
|
|
|
|
|
2015-10-03 01:17:47 +02:00
|
|
|
class BMAccount(object):
|
2015-10-03 12:12:18 +02:00
|
|
|
def __init__(self, address = None):
|
2015-10-03 01:17:47 +02:00
|
|
|
self.address = address
|
2015-10-05 17:07:23 +02:00
|
|
|
self.type = 'normal'
|
|
|
|
if shared.config.has_section(address):
|
|
|
|
if shared.safeConfigGetBoolean(self.address, 'chan'):
|
2015-11-27 02:10:27 +01:00
|
|
|
self.type = AccountMixin.CHAN
|
2015-10-05 17:07:23 +02:00
|
|
|
elif shared.safeConfigGetBoolean(self.address, 'mailinglist'):
|
2015-11-27 02:10:27 +01:00
|
|
|
self.type = AccountMixin.MAILINGLIST
|
2015-10-13 23:32:36 +02:00
|
|
|
elif self.address == str_broadcast_subscribers:
|
2015-11-27 02:10:27 +01:00
|
|
|
self.type = AccountMixin.BROADCAST
|
2015-10-10 19:58:01 +02:00
|
|
|
else:
|
|
|
|
queryreturn = sqlQuery(
|
2015-10-13 23:32:36 +02:00
|
|
|
'''select label from subscriptions where address=?''', self.address)
|
2015-10-10 19:58:01 +02:00
|
|
|
if queryreturn:
|
2015-11-27 02:10:27 +01:00
|
|
|
self.type = AccountMixin.SUBSCRIPTION
|
2015-10-10 19:58:01 +02:00
|
|
|
|
2015-10-03 12:12:18 +02:00
|
|
|
def getLabel(self, address = None):
|
|
|
|
if address is None:
|
|
|
|
address = self.address
|
|
|
|
label = address
|
|
|
|
if shared.config.has_section(address):
|
|
|
|
label = shared.config.get(address, 'label')
|
|
|
|
queryreturn = sqlQuery(
|
|
|
|
'''select label from addressbook where address=?''', address)
|
|
|
|
if queryreturn != []:
|
|
|
|
for row in queryreturn:
|
|
|
|
label, = row
|
|
|
|
else:
|
|
|
|
queryreturn = sqlQuery(
|
|
|
|
'''select label from subscriptions where address=?''', address)
|
|
|
|
if queryreturn != []:
|
|
|
|
for row in queryreturn:
|
|
|
|
label, = row
|
|
|
|
return label
|
|
|
|
|
2015-10-03 01:17:47 +02:00
|
|
|
def parseMessage(self, toAddress, fromAddress, subject, message):
|
|
|
|
self.toAddress = toAddress
|
|
|
|
self.fromAddress = fromAddress
|
|
|
|
self.subject = subject
|
|
|
|
self.message = message
|
2015-10-03 12:12:18 +02:00
|
|
|
self.fromLabel = self.getLabel(fromAddress)
|
|
|
|
self.toLabel = self.getLabel(toAddress)
|
2015-10-03 01:17:47 +02:00
|
|
|
|
2015-10-10 19:58:01 +02:00
|
|
|
|
|
|
|
class SubscriptionAccount(BMAccount):
|
|
|
|
pass
|
2015-10-13 23:32:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BroadcastAccount(BMAccount):
|
|
|
|
pass
|
2015-10-10 19:58:01 +02:00
|
|
|
|
|
|
|
|
2015-10-03 01:17:47 +02:00
|
|
|
class GatewayAccount(BMAccount):
|
2015-10-03 12:12:18 +02:00
|
|
|
gatewayName = None
|
2015-10-03 01:17:47 +02:00
|
|
|
def __init__(self, address):
|
|
|
|
super(BMAccount, self).__init__(address)
|
2015-10-03 17:24:21 +02:00
|
|
|
|
|
|
|
def send(self):
|
|
|
|
status, addressVersionNumber, streamNumber, ripe = decodeAddress(self.toAddress)
|
|
|
|
ackdata = OpenSSL.rand(32)
|
|
|
|
t = ()
|
|
|
|
sqlExecute(
|
|
|
|
'''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
|
|
|
|
'',
|
|
|
|
self.toAddress,
|
|
|
|
ripe,
|
|
|
|
self.fromAddress,
|
|
|
|
self.subject,
|
|
|
|
self.message,
|
|
|
|
ackdata,
|
|
|
|
int(time.time()), # sentTime (this will never change)
|
|
|
|
int(time.time()), # lastActionTime
|
|
|
|
0, # sleepTill time. This will get set when the POW gets done.
|
|
|
|
'msgqueued',
|
|
|
|
0, # retryNumber
|
|
|
|
'sent', # folder
|
|
|
|
2, # encodingtype
|
|
|
|
shared.config.getint('bitmessagesettings', 'ttl')
|
|
|
|
)
|
|
|
|
|
|
|
|
shared.workerQueue.put(('sendmessage', self.toAddress))
|
2015-10-03 01:17:47 +02:00
|
|
|
|
|
|
|
def parseMessage(self, toAddress, fromAddress, subject, message):
|
|
|
|
super(BMAccount, self).parseMessage(toAddress, fromAddress, subject, message)
|
|
|
|
|
|
|
|
class MailchuckAccount(GatewayAccount):
|
2015-10-03 12:12:18 +02:00
|
|
|
# set "gateway" in keys.dat to this
|
|
|
|
gatewayName = "mailchuck"
|
2015-10-03 01:17:47 +02:00
|
|
|
registrationAddress = "BM-2cVYYrhaY5Gbi3KqrX9Eae2NRNrkfrhCSA"
|
|
|
|
unregistrationAddress = "BM-2cVMAHTRjZHCTPMue75XBK5Tco175DtJ9J"
|
|
|
|
relayAddress = "BM-2cWim8aZwUNqxzjMxstnUMtVEUQJeezstf"
|
|
|
|
regExpIncoming = re.compile("(.*)MAILCHUCK-FROM::(\S+) \| (.*)")
|
|
|
|
regExpOutgoing = re.compile("(\S+) (.*)")
|
|
|
|
def __init__(self, address):
|
|
|
|
super(GatewayAccount, self).__init__(address)
|
2015-10-03 17:24:21 +02:00
|
|
|
|
|
|
|
def createMessage(self, toAddress, fromAddress, subject, message):
|
|
|
|
self.subject = toAddress + " " + subject
|
|
|
|
self.toAddress = self.relayAddress
|
|
|
|
self.fromAddress = fromAddress
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def register(self, email):
|
|
|
|
self.toAddress = self.registrationAddress
|
|
|
|
self.subject = email
|
|
|
|
self.message = ""
|
|
|
|
self.fromAddress = self.address
|
|
|
|
self.send()
|
|
|
|
|
|
|
|
def unregister(self):
|
|
|
|
self.toAddress = self.unregistrationAddress
|
|
|
|
self.subject = ""
|
|
|
|
self.message = ""
|
|
|
|
self.fromAddress = self.address
|
|
|
|
self.send()
|
2015-10-03 01:17:47 +02:00
|
|
|
|
|
|
|
def parseMessage(self, toAddress, fromAddress, subject, message):
|
|
|
|
super(GatewayAccount, self).parseMessage(toAddress, fromAddress, subject, message)
|
|
|
|
if fromAddress == self.relayAddress:
|
|
|
|
matches = self.regExpIncoming.search(subject)
|
|
|
|
if not matches is None:
|
|
|
|
self.subject = ""
|
|
|
|
if not matches.group(1) is None:
|
|
|
|
self.subject += matches.group(1)
|
|
|
|
if not matches.group(3) is None:
|
|
|
|
self.subject += matches.group(3)
|
|
|
|
if not matches.group(2) is None:
|
2015-10-03 12:12:18 +02:00
|
|
|
self.fromLabel = matches.group(2)
|
2015-10-03 17:24:21 +02:00
|
|
|
self.fromAddress = matches.group(2)
|
2015-10-03 01:17:47 +02:00
|
|
|
if toAddress == self.relayAddress:
|
|
|
|
matches = self.regExpOutgoing.search(subject)
|
|
|
|
if not matches is None:
|
|
|
|
if not matches.group(2) is None:
|
|
|
|
self.subject = matches.group(2)
|
|
|
|
if not matches.group(1) is None:
|
2015-10-03 17:24:21 +02:00
|
|
|
self.toLabel = matches.group(1)
|
|
|
|
self.toAddress = matches.group(1)
|