2016-09-30 20:53:12 +00:00
|
|
|
from sqlite3 import connect
|
2016-04-03 20:51:46 +00:00
|
|
|
import settings
|
2016-03-12 17:37:54 +00:00
|
|
|
from os import chdir
|
2016-05-15 20:02:05 +00:00
|
|
|
import os.path
|
2017-02-11 17:07:28 +00:00
|
|
|
from toxes import ToxES
|
2016-03-12 11:18:00 +00:00
|
|
|
|
|
|
|
|
2016-03-29 14:11:30 +00:00
|
|
|
PAGE_SIZE = 42
|
|
|
|
|
2016-09-07 21:10:32 +00:00
|
|
|
TIMEOUT = 11
|
|
|
|
|
2017-03-04 19:15:42 +00:00
|
|
|
SAVE_MESSAGES = 250
|
2016-10-22 18:23:03 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
MESSAGE_OWNER = {
|
|
|
|
'ME': 0,
|
2016-06-04 19:17:32 +00:00
|
|
|
'FRIEND': 1,
|
|
|
|
'NOT_SENT': 2
|
2016-03-12 15:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-22 11:35:22 +00:00
|
|
|
class History:
|
2016-04-03 20:51:46 +00:00
|
|
|
|
2016-03-12 11:18:00 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self._name = name
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-05-15 20:02:05 +00:00
|
|
|
path = settings.ProfileHelper.get_path() + self._name + '.hstr'
|
|
|
|
if os.path.exists(path):
|
2017-02-11 17:07:28 +00:00
|
|
|
decr = ToxES.get_instance()
|
2016-06-05 11:59:36 +00:00
|
|
|
try:
|
|
|
|
with open(path, 'rb') as fin:
|
|
|
|
data = fin.read()
|
|
|
|
if decr.is_data_encrypted(data):
|
|
|
|
data = decr.pass_decrypt(data)
|
|
|
|
with open(path, 'wb') as fout:
|
|
|
|
fout.write(data)
|
|
|
|
except:
|
|
|
|
os.remove(path)
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(name + '.hstr', timeout=TIMEOUT)
|
2016-03-12 11:18:00 +00:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('CREATE TABLE IF NOT EXISTS friends('
|
|
|
|
' tox_id TEXT PRIMARY KEY'
|
|
|
|
')')
|
|
|
|
db.close()
|
|
|
|
|
2016-05-15 20:02:05 +00:00
|
|
|
def save(self):
|
2017-02-11 17:07:28 +00:00
|
|
|
encr = ToxES.get_instance()
|
2016-05-15 20:02:05 +00:00
|
|
|
if encr.has_password():
|
|
|
|
path = settings.ProfileHelper.get_path() + self._name + '.hstr'
|
|
|
|
with open(path, 'rb') as fin:
|
|
|
|
data = fin.read()
|
2016-06-21 20:43:43 +00:00
|
|
|
data = encr.pass_encrypt(bytes(data))
|
2016-05-15 20:02:05 +00:00
|
|
|
with open(path, 'wb') as fout:
|
|
|
|
fout.write(data)
|
|
|
|
|
2016-03-15 20:35:15 +00:00
|
|
|
def export(self, directory):
|
2016-04-03 20:51:46 +00:00
|
|
|
path = settings.ProfileHelper.get_path() + self._name + '.hstr'
|
2016-03-15 20:35:15 +00:00
|
|
|
new_path = directory + self._name + '.hstr'
|
|
|
|
with open(path, 'rb') as fin:
|
|
|
|
data = fin.read()
|
2017-02-11 17:07:28 +00:00
|
|
|
encr = ToxES.get_instance()
|
2016-06-14 18:47:03 +00:00
|
|
|
if encr.has_password():
|
|
|
|
data = encr.pass_encrypt(data)
|
2016-03-15 20:35:15 +00:00
|
|
|
with open(new_path, 'wb') as fout:
|
|
|
|
fout.write(data)
|
|
|
|
|
2016-03-12 11:18:00 +00:00
|
|
|
def add_friend_to_db(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-03-12 11:18:00 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, ))
|
|
|
|
cursor.execute('CREATE TABLE id' + tox_id + '('
|
|
|
|
' id INTEGER PRIMARY KEY,'
|
|
|
|
' message TEXT,'
|
|
|
|
' owner INTEGER,'
|
2016-03-25 13:24:38 +00:00
|
|
|
' unix_time REAL,'
|
2016-03-12 11:18:00 +00:00
|
|
|
' message_type INTEGER'
|
|
|
|
')')
|
|
|
|
db.commit()
|
2016-03-12 16:22:33 +00:00
|
|
|
except:
|
2016-10-02 17:20:49 +00:00
|
|
|
print('Database is locked!')
|
2016-03-12 11:18:00 +00:00
|
|
|
db.rollback()
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
def delete_friend_from_db(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-03-12 11:18:00 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, ))
|
|
|
|
cursor.execute('DROP TABLE id' + tox_id + ';')
|
|
|
|
db.commit()
|
2016-03-12 16:22:33 +00:00
|
|
|
except:
|
2016-10-02 17:20:49 +00:00
|
|
|
print('Database is locked!')
|
2016-03-12 11:18:00 +00:00
|
|
|
db.rollback()
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
2016-03-12 16:22:33 +00:00
|
|
|
def friend_exists_in_db(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-03-12 16:22:33 +00:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
db.close()
|
|
|
|
return result is not None
|
|
|
|
|
2016-03-12 11:18:00 +00:00
|
|
|
def save_messages_to_db(self, tox_id, messages_iter):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-03-12 11:18:00 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
2016-03-12 17:37:54 +00:00
|
|
|
cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time, message_type) '
|
|
|
|
'VALUES (?, ?, ?, ?);', messages_iter)
|
|
|
|
db.commit()
|
|
|
|
except:
|
2016-10-02 17:20:49 +00:00
|
|
|
print('Database is locked!')
|
2016-03-12 17:37:54 +00:00
|
|
|
db.rollback()
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
2016-06-13 21:32:45 +00:00
|
|
|
def update_messages(self, tox_id, unsent_time):
|
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-06-13 21:32:45 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('UPDATE id' + tox_id + ' SET owner = 0 '
|
|
|
|
'WHERE unix_time < ' + str(unsent_time) + ' AND owner = 2;')
|
|
|
|
db.commit()
|
|
|
|
except:
|
2016-10-02 17:20:49 +00:00
|
|
|
print('Database is locked!')
|
2016-06-13 21:32:45 +00:00
|
|
|
db.rollback()
|
|
|
|
finally:
|
|
|
|
db.close()
|
2016-06-07 09:27:58 +00:00
|
|
|
|
2016-06-28 09:44:27 +00:00
|
|
|
def delete_message(self, tox_id, time):
|
2017-03-04 20:18:26 +00:00
|
|
|
start, end = str(time - 0.01), str(time + 0.01)
|
2016-06-28 09:44:27 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-06-28 09:44:27 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
2017-03-04 20:18:26 +00:00
|
|
|
cursor.execute('DELETE FROM id' + tox_id + ' WHERE unix_time < ' + end + ' AND unix_time > ' +
|
|
|
|
start + ';')
|
2016-06-28 09:44:27 +00:00
|
|
|
db.commit()
|
|
|
|
except:
|
2016-10-02 17:20:49 +00:00
|
|
|
print('Database is locked!')
|
2016-06-28 09:44:27 +00:00
|
|
|
db.rollback()
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
2016-03-12 17:37:54 +00:00
|
|
|
def delete_messages(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-09-07 21:10:32 +00:00
|
|
|
db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-03-12 17:37:54 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('DELETE FROM id' + tox_id + ';')
|
2016-03-12 11:18:00 +00:00
|
|
|
db.commit()
|
2016-03-12 16:22:33 +00:00
|
|
|
except:
|
2016-10-02 17:20:49 +00:00
|
|
|
print('Database is locked!')
|
2016-03-12 11:18:00 +00:00
|
|
|
db.rollback()
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
def messages_getter(self, tox_id):
|
2016-03-12 15:25:16 +00:00
|
|
|
return History.MessageGetter(self._name, tox_id)
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-06-22 11:35:22 +00:00
|
|
|
class MessageGetter:
|
2016-09-07 21:10:32 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def __init__(self, name, tox_id):
|
2016-10-01 20:06:15 +00:00
|
|
|
self._count = 0
|
|
|
|
self._name = name
|
|
|
|
self._tox_id = tox_id
|
|
|
|
self._db = self._cursor = None
|
|
|
|
|
|
|
|
def connect(self):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-10-01 20:06:15 +00:00
|
|
|
self._db = connect(self._name + '.hstr', timeout=TIMEOUT)
|
2016-03-12 15:25:16 +00:00
|
|
|
self._cursor = self._db.cursor()
|
2016-10-01 20:06:15 +00:00
|
|
|
self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + self._tox_id +
|
2016-03-12 15:25:16 +00:00
|
|
|
' ORDER BY unix_time DESC;')
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-10-01 20:06:15 +00:00
|
|
|
def disconnect(self):
|
|
|
|
self._db.close()
|
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def get_one(self):
|
2016-10-01 20:06:15 +00:00
|
|
|
self.connect()
|
|
|
|
self.skip()
|
|
|
|
data = self._cursor.fetchone()
|
|
|
|
self._count += 1
|
|
|
|
self.disconnect()
|
|
|
|
return data
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def get_all(self):
|
2016-10-01 20:06:15 +00:00
|
|
|
self.connect()
|
|
|
|
data = self._cursor.fetchall()
|
|
|
|
self.disconnect()
|
|
|
|
self._count = len(data)
|
|
|
|
return data
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def get(self, count):
|
2016-10-01 20:06:15 +00:00
|
|
|
self.connect()
|
|
|
|
self.skip()
|
|
|
|
data = self._cursor.fetchmany(count)
|
|
|
|
self.disconnect()
|
|
|
|
self._count += len(data)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def skip(self):
|
|
|
|
if self._count:
|
|
|
|
self._cursor.fetchmany(self._count)
|
|
|
|
|
|
|
|
def delete_one(self):
|
2016-10-02 17:20:49 +00:00
|
|
|
if self._count:
|
|
|
|
self._count -= 1
|