2019-02-16 06:01:26 +00:00
|
|
|
'''
|
2019-06-13 16:24:34 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-02-16 06:01:26 +00:00
|
|
|
|
|
|
|
Sets more abstract information related to a peer. Can be thought of as traditional 'contact' system
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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/>.
|
|
|
|
'''
|
2019-09-30 04:40:23 +00:00
|
|
|
import os, json
|
2019-06-19 06:57:13 +00:00
|
|
|
import unpaddedbase32
|
2019-09-30 04:40:23 +00:00
|
|
|
import niceware
|
|
|
|
|
|
|
|
import onionrexceptions
|
2019-02-17 06:21:29 +00:00
|
|
|
from onionrusers import onionrusers
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import bytesconverter, epoch
|
2019-07-19 19:49:56 +00:00
|
|
|
from utils import identifyhome
|
2019-09-29 20:39:03 +00:00
|
|
|
from onionrutils import mnemonickeys
|
2019-02-16 06:01:26 +00:00
|
|
|
class ContactManager(onionrusers.OnionrUser):
|
2019-07-19 19:49:56 +00:00
|
|
|
def __init__(self, publicKey, saveUser=False, recordExpireSeconds=5):
|
2019-09-09 22:29:29 +00:00
|
|
|
try:
|
2019-09-29 20:39:03 +00:00
|
|
|
if mnemonickeys.DELIMITER in publicKey:
|
2019-10-14 00:56:14 +00:00
|
|
|
publicKey = mnemonickeys.get_base32(publicKey)
|
2019-10-04 22:12:07 +00:00
|
|
|
#publicKey = unpaddedbase32.b32encode(bytesconverter.str_to_bytes(publicKey))
|
2019-09-09 22:29:29 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
publicKey = bytesconverter.bytes_to_str(unpaddedbase32.repad(bytesconverter.str_to_bytes(publicKey)))
|
2019-07-19 19:49:56 +00:00
|
|
|
super(ContactManager, self).__init__(publicKey, saveUser=saveUser)
|
|
|
|
home = identifyhome.identify_home()
|
|
|
|
self.dataDir = home + '/contacts/'
|
|
|
|
self.dataFile = '%s/contacts/%s.json' % (home, publicKey)
|
2019-02-17 18:20:10 +00:00
|
|
|
self.lastRead = 0
|
|
|
|
self.recordExpire = recordExpireSeconds
|
|
|
|
self.data = self._loadData()
|
|
|
|
self.deleted = False
|
|
|
|
|
|
|
|
if not os.path.exists(self.dataDir):
|
2019-02-17 06:21:29 +00:00
|
|
|
os.mkdir(self.dataDir)
|
|
|
|
|
|
|
|
def _writeData(self):
|
|
|
|
data = json.dumps(self.data)
|
|
|
|
with open(self.dataFile, 'w') as dataFile:
|
|
|
|
dataFile.write(data)
|
|
|
|
|
2019-02-17 18:20:10 +00:00
|
|
|
def _loadData(self):
|
2019-06-25 23:07:35 +00:00
|
|
|
self.lastRead = epoch.get_epoch()
|
2019-02-17 18:20:10 +00:00
|
|
|
retData = {}
|
|
|
|
if os.path.exists(self.dataFile):
|
|
|
|
with open(self.dataFile, 'r') as dataFile:
|
|
|
|
retData = json.loads(dataFile.read())
|
|
|
|
return retData
|
|
|
|
|
|
|
|
def set_info(self, key, value, autoWrite=True):
|
|
|
|
if self.deleted:
|
|
|
|
raise onionrexceptions.ContactDeleted
|
|
|
|
|
|
|
|
self.data[key] = value
|
|
|
|
if autoWrite:
|
|
|
|
self._writeData()
|
2019-02-16 06:01:26 +00:00
|
|
|
return
|
2019-02-17 18:20:10 +00:00
|
|
|
|
|
|
|
def get_info(self, key, forceReload=False):
|
|
|
|
if self.deleted:
|
|
|
|
raise onionrexceptions.ContactDeleted
|
|
|
|
|
2019-06-25 23:07:35 +00:00
|
|
|
if (epoch.get_epoch() - self.lastRead >= self.recordExpire) or forceReload:
|
2019-02-17 18:20:10 +00:00
|
|
|
self.data = self._loadData()
|
|
|
|
try:
|
|
|
|
return self.data[key]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2019-02-16 06:01:26 +00:00
|
|
|
def delete_contact(self):
|
2019-02-17 18:20:10 +00:00
|
|
|
self.deleted = True
|
|
|
|
if os.path.exists(self.dataFile):
|
|
|
|
os.remove(self.dataFile)
|