From a958b99fefc499d5ad20fb684fda7d292f7b981e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 28 Jan 2020 21:39:01 -0600 Subject: [PATCH] remove sent mail from sentbox when block is blacklisted --- src/onionrblocks/onionrblacklist.py | 5 +++- static-data/default-plugins/pms/main.py | 2 ++ .../default-plugins/pms/onblacklist.py | 12 ++++++++++ static-data/default-plugins/pms/sentboxdb.py | 23 +++++++++++-------- tests/test_onionrvalues.py | 19 +++++++++++++++ tests/test_template.py | 3 --- 6 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 static-data/default-plugins/pms/onblacklist.py create mode 100644 tests/test_onionrvalues.py diff --git a/src/onionrblocks/onionrblacklist.py b/src/onionrblocks/onionrblacklist.py index 544e7d2d..a0824edf 100755 --- a/src/onionrblocks/onionrblacklist.py +++ b/src/onionrblocks/onionrblacklist.py @@ -1,6 +1,6 @@ """Onionr - Private P2P Communication. -Handle maintenence of a blacklist database, for blocks and peers +Handle maintenance of a blacklist database, for blocks and peers """ import sqlite3 import os @@ -99,6 +99,9 @@ class OnionrBlackList: # we hash the data so we can remove data entirely from our node's disk hashed = bytesconverter.bytes_to_str(onionrcrypto.hashers.sha3_hash(data)) + + event('blacklist_add', data={'data': data, 'hash': hashed}) + if len(hashed) > 64: raise Exception("Hashed data is too large") diff --git a/static-data/default-plugins/pms/main.py b/static-data/default-plugins/pms/main.py index 180b6dd8..ca1ae569 100755 --- a/static-data/default-plugins/pms/main.py +++ b/static-data/default-plugins/pms/main.py @@ -34,6 +34,8 @@ PLUGIN_VERSION = '0.0.1' sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) import sentboxdb, mailapi, loadinbox # import after path insert +from onblacklist import on_blacklist_add + flask_blueprint = mailapi.flask_blueprint security_whitelist = ['staticfiles.mail', 'staticfiles.mailindex'] diff --git a/static-data/default-plugins/pms/onblacklist.py b/static-data/default-plugins/pms/onblacklist.py new file mode 100644 index 00000000..0e521967 --- /dev/null +++ b/static-data/default-plugins/pms/onblacklist.py @@ -0,0 +1,12 @@ +from threading import Thread + +from onionrutils import localcommand + + +def on_blacklist_add(api, data=None): + blacklisted_data = data['data'] + + def remove(): + localcommand.local_command(f'/mail/deletemsg/{blacklisted_data}', post=True) + + Thread(target=remove).start() diff --git a/static-data/default-plugins/pms/sentboxdb.py b/static-data/default-plugins/pms/sentboxdb.py index fee62bce..b07445a8 100755 --- a/static-data/default-plugins/pms/sentboxdb.py +++ b/static-data/default-plugins/pms/sentboxdb.py @@ -1,9 +1,13 @@ -''' +""" Onionr - Private P2P Communication This file handles the sentbox for the mail plugin -''' -''' +""" +import sqlite3 +import os +from onionrutils import epoch +from utils import identifyhome, reconstructhash +""" 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 @@ -16,17 +20,16 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . -''' -import sqlite3, os -from onionrutils import epoch -from utils import identifyhome, reconstructhash +""" + + class SentBox: def __init__(self): self.dbLocation = identifyhome.identify_home() + '/sentbox.db' if not os.path.exists(self.dbLocation): self.createDB() return - + def connect(self): self.conn = sqlite3.connect(self.dbLocation) self.cursor = self.conn.cursor() @@ -37,14 +40,14 @@ class SentBox: def createDB(self): conn = sqlite3.connect(self.dbLocation) cursor = conn.cursor() - cursor.execute('''CREATE TABLE sent( + cursor.execute("""CREATE TABLE sent( hash id not null, peer text not null, message text not null, subject text not null, date int not null ); - ''') + """) conn.commit() conn.close() return diff --git a/tests/test_onionrvalues.py b/tests/test_onionrvalues.py new file mode 100644 index 00000000..0bce9fe5 --- /dev/null +++ b/tests/test_onionrvalues.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +import sys, os +sys.path.append(".") +sys.path.append("src/") +import unittest, uuid +import json +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR + +from utils import identifyhome, createdirs +from etc import onionrvalues + +class TestOnionrValues(unittest.TestCase): + def test_default_expire(self): + self.assertEqual(onionrvalues.DEFAULT_EXPIRE, 2592000) + + +unittest.main() diff --git a/tests/test_template.py b/tests/test_template.py index 3e8cca75..7ab6cf6b 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -14,9 +14,6 @@ createdirs.create_dirs() setup_config() class TestTemplate(unittest.TestCase): - ''' - Tests both the onionrusers class and the contactmanager (which inherits it) - ''' def test_true(self): self.assertTrue(True)