better user info syncing and show names in mail

This commit is contained in:
Kevin Froman 2018-08-28 20:09:27 -05:00
parent c907558dd1
commit c0c0f838b6
6 changed files with 33 additions and 12 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "onionr/bitpeer"]
path = onionr/bitpeer
url = https://github.com/beardog108/bitpeer.py

View File

@ -265,17 +265,21 @@ class Onionr:
return self.cmds return self.cmds
def friendCmd(self): def friendCmd(self):
'''List, add, or remove friend(s)''' '''List, add, or remove friend(s)
Changes their peer DB entry.
'''
friend = '' friend = ''
try: try:
# Get the friend command
action = sys.argv[2] action = sys.argv[2]
except IndexError: except IndexError:
logger.info('Syntax: friend add/remove/list [address]') logger.info('Syntax: friend add/remove/list [address]')
else: else:
action = action.lower() action = action.lower()
if action == 'list': if action == 'list':
# List out peers marked as our friend
for friend in self.onionrCore.listPeers(randomOrder=False, trust=1): for friend in self.onionrCore.listPeers(randomOrder=False, trust=1):
if friend == self.onionrCore._crypto.pubKey: if friend == self.onionrCore._crypto.pubKey: # do not list our key
continue continue
friendProfile = onionrusers.OnionrUser(self.onionrCore, friend) friendProfile = onionrusers.OnionrUser(self.onionrCore, friend)
logger.info(friend + ' - ' + friendProfile.getName()) logger.info(friend + ' - ' + friendProfile.getName())

View File

@ -17,6 +17,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import onionrblockapi, logger
class OnionrUser: class OnionrUser:
def __init__(self, coreInst, publicKey): def __init__(self, coreInst, publicKey):
self.trust = 0 self.trust = 0
@ -59,3 +60,14 @@ class OnionrUser:
def forwardDecrypt(self, encrypted): def forwardDecrypt(self, encrypted):
return return
def findAndSetID(self):
'''Find any info about the user from existing blocks and cache it to their DB entry'''
infoBlocks = []
for bHash in self._core.getBlocksByType('userInfo'):
block = onionrblockapi.Block(bHash, core=self._core)
if block.signer == self.publicKey:
if block.verifySig():
newName = block.getMetadata('name')
if newName.isalnum():
logger.info('%s is now using the name %s.' % (self.publicKey, newName))
self._core.setPeerInfo(self.publicKey, 'name', newName)

View File

@ -23,7 +23,7 @@ import nacl.signing, nacl.encoding
from onionrblockapi import Block from onionrblockapi import Block
import onionrexceptions import onionrexceptions
from defusedxml import minidom from defusedxml import minidom
import pgpwords import pgpwords, onionrusers
if sys.version_info < (3, 6): if sys.version_info < (3, 6):
try: try:
import sha3 import sha3
@ -95,7 +95,6 @@ class OnionrUtils:
except IndexError: except IndexError:
logger.warn('No pow token') logger.warn('No pow token')
continue continue
#powHash = self._core._crypto.blake2bHash(base64.b64decode(key[1]) + self._core._crypto.blake2bHash(key[0].encode()))
value = base64.b64decode(key[1]) value = base64.b64decode(key[1])
hashedKey = self._core._crypto.blake2bHash(key[0]) hashedKey = self._core._crypto.blake2bHash(key[0])
powHash = self._core._crypto.blake2bHash(value + hashedKey) powHash = self._core._crypto.blake2bHash(value + hashedKey)
@ -106,6 +105,7 @@ class OnionrUtils:
if powHash.startswith(b'0000'): if powHash.startswith(b'0000'):
if not key[0] in self._core.listPeers(randomOrder=False) and type(key) != None and key[0] != self._core._crypto.pubKey: if not key[0] in self._core.listPeers(randomOrder=False) and type(key) != None and key[0] != self._core._crypto.pubKey:
if self._core.addPeer(key[0], key[1]): if self._core.addPeer(key[0], key[1]):
onionrusers.OnionrUser(self._core, key[0]).findAndSetID()
retVal = True retVal = True
else: else:
logger.warn("Failed to add key") logger.warn("Failed to add key")
@ -282,6 +282,7 @@ class OnionrUtils:
pass pass
else: else:
self._core.setPeerInfo(signer, 'name', peerName) self._core.setPeerInfo(signer, 'name', peerName)
logger.info('%s is now using the name %s.' % (signer, peerName))
except TypeError: except TypeError:
pass pass

View File

@ -19,7 +19,7 @@
''' '''
# Imports some useful libraries # Imports some useful libraries
import logger, config, threading, time import logger, config, threading, time, uuid
from onionrblockapi import Block from onionrblockapi import Block
plugin_name = 'cliui' plugin_name = 'cliui'
@ -32,7 +32,7 @@ class OnionrCLIUI:
return return
def start(self): def start(self):
name = input("Enter your name") name = input("Enter your name")
self.myCore.insertBlock("userInfo", sign=True, header='userInfo', meta={'name': name}) self.myCore.insertBlock("userInfo-" + str(uuid.uuid1()), sign=True, header='userInfo', meta={'name': name})
return return
def on_init(api, data = None): def on_init(api, data = None):

View File

@ -21,7 +21,7 @@
# Imports some useful libraries # Imports some useful libraries
import logger, config, threading, time, readline, datetime import logger, config, threading, time, readline, datetime
from onionrblockapi import Block from onionrblockapi import Block
import onionrexceptions import onionrexceptions, onionrusers
import locale import locale
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
@ -81,8 +81,15 @@ class OnionrMail:
continue continue
blockCount += 1 blockCount += 1
pmBlockMap[blockCount] = blockHash pmBlockMap[blockCount] = blockHash
block = Block(blockHash, core=self.myCore)
senderKey = block.getMetadata('signer')
senderDisplay = onionrusers.OnionrUser(self.myCore, senderKey)
if senderDisplay == 'anonymous':
senderDisplay = senderKey
blockDate = pmBlocks[blockHash].getDate().strftime("%m/%d %H:%M") blockDate = pmBlocks[blockHash].getDate().strftime("%m/%d %H:%M")
print('%s. %s: %s' % (blockCount, blockDate, blockHash)) print('%s. %s - %s: %s' % (blockCount, blockDate, senderDisplay, blockHash))
try: try:
choice = logger.readline('Enter a block number, -r to refresh, or -q to stop: ').strip().lower() choice = logger.readline('Enter a block number, -r to refresh, or -q to stop: ').strip().lower()