Onionr/onionr/static-data/default-plugins/pms/sentboxdb.py

77 lines
2.5 KiB
Python
Raw Normal View History

2018-11-11 02:10:58 +00:00
'''
2019-06-20 07:59:32 +00:00
Onionr - Private P2P Communication
2018-11-11 02:10:58 +00:00
This file handles the sentbox for the mail plugin
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import sqlite3, os
from onionrutils import epoch
2019-07-29 18:05:27 +00:00
from utils import identifyhome, reconstructhash
2018-11-11 02:10:58 +00:00
class SentBox:
2019-07-21 00:29:08 +00:00
def __init__(self):
self.dbLocation = identifyhome.identify_home() + '/sentbox.db'
2018-11-11 02:10:58 +00:00
if not os.path.exists(self.dbLocation):
self.createDB()
return
2019-03-03 06:26:55 +00:00
def connect(self):
self.conn = sqlite3.connect(self.dbLocation)
self.cursor = self.conn.cursor()
def close(self):
self.conn.close()
2018-12-09 17:29:39 +00:00
2018-11-11 02:10:58 +00:00
def createDB(self):
conn = sqlite3.connect(self.dbLocation)
cursor = conn.cursor()
cursor.execute('''CREATE TABLE sent(
hash id not null,
peer text not null,
message text not null,
2019-02-11 22:36:43 +00:00
subject text not null,
2018-11-11 02:10:58 +00:00
date int not null
);
''')
conn.commit()
2019-03-03 06:26:55 +00:00
conn.close()
2018-11-11 02:10:58 +00:00
return
def listSent(self):
2019-03-03 06:26:55 +00:00
self.connect()
2018-11-11 02:10:58 +00:00
retData = []
for entry in self.cursor.execute('SELECT * FROM sent;'):
2019-02-11 22:36:43 +00:00
retData.append({'hash': entry[0], 'peer': entry[1], 'message': entry[2], 'subject': entry[3], 'date': entry[4]})
2019-03-03 06:26:55 +00:00
self.close()
2018-11-11 02:10:58 +00:00
return retData
2018-12-09 17:29:39 +00:00
2019-02-11 22:36:43 +00:00
def addToSent(self, blockID, peer, message, subject=''):
2019-07-29 18:05:27 +00:00
blockID = reconstructhash.deconstruct_hash(blockID)
2019-03-03 06:26:55 +00:00
self.connect()
args = (blockID, peer, message, subject, epoch.get_epoch())
2019-02-11 22:36:43 +00:00
self.cursor.execute('INSERT INTO sent VALUES(?, ?, ?, ?, ?)', args)
2018-11-11 02:10:58 +00:00
self.conn.commit()
2019-03-03 06:26:55 +00:00
self.close()
2018-11-11 02:10:58 +00:00
return
2018-12-09 17:29:39 +00:00
2018-11-11 02:10:58 +00:00
def removeSent(self, blockID):
2019-07-29 18:05:27 +00:00
blockID = reconstructhash.deconstruct_hash(blockID)
2019-03-03 06:26:55 +00:00
self.connect()
2018-11-11 02:10:58 +00:00
args = (blockID,)
self.cursor.execute('DELETE FROM sent where hash=?', args)
self.conn.commit()
2019-03-03 06:26:55 +00:00
self.close()
2018-12-09 17:29:39 +00:00
return