bi-directional messages work properly now; important TODO: handling short/long Tox IDs properly

master
Michał 'rysiek' Woźniak 2015-02-08 19:17:58 +01:00
parent e2dbd34a1f
commit 649be47e0b
4 changed files with 98 additions and 16 deletions

View File

@ -151,6 +151,9 @@ class ToxManager(mp.Process):
def handle_tox_message(self, toxid, toxmsg):
# TODO: stubity-stub, build the message stanza
# sfrom is just a "short" Tox id, the public key *without* nospam and checksum
# https://github.com/irungentoo/toxcore/issues/854
# this needs to get handled on the XMPP side, matched against the user's roster
msg = Message(sto='rysiek@tox-prosody', sfrom='%s@tox.tox-prosody' % toxid)
msg['body'] = 'Got message from %s:\n%s' % (toxid, toxmsg)
msg['type'] = 'chat'

View File

@ -33,12 +33,12 @@ from time import sleep
from os.path import exists
SERVER = [
# "172.17.42.1",
# 33445,
# "3FA2E5273F0C368576FE120B374664E3B41E2CDF21639AFED3DC301490FFB01FAAA47B78D5F4"
"80.232.246.79",
"172.17.42.1",
33445,
"0B8DCEAA7BDDC44BB11173F987CAE3566A2D7057D8DD3CC642BD472B9391002A"
"3FA2E5273F0C368576FE120B374664E3B41E2CDF21639AFED3DC301490FFB01FAAA47B78D5F4"
# "80.232.246.79",
# 33445,
# "0B8DCEAA7BDDC44BB11173F987CAE3566A2D7057D8DD3CC642BD472B9391002A"
]
DATA = 'echo.data'
@ -58,6 +58,7 @@ class EchoBot(Tox):
def connect(self):
print('connecting...')
self.bootstrap_from_address(SERVER[0], SERVER[1], SERVER[2])
print('+-- server: %s' % SERVER)
def loop(self):
checked = False

View File

@ -110,30 +110,103 @@ class ToXMPPComponent(ComponentXMPP):
while True:
# handle the Tox to XMPP queue items
while not self.t2x_queue.empty():
# yay
tdbg('toxmpp.run() : we got message on the queue!')
# get the msg
msg = self.t2x_queue.get()
tdbg(' +-- from : %(from)s\nTOXMPP :: +-- to : %(to)s\nTOXMPP :: +-- msg : %(body)s\nTOXMPP :: +-- msg : %(type)s' % msg)
# could it be so simple?
# we have to handle the "from" part -- it contains only the "short" Tox ID:
# https://github.com/irungentoo/toxcore/issues/854
# ...insufficient to build the correct internal JID (these use the "long" version)
# so we have to match it against rosters containing the 'to' external JID
toxjid = self.get_jid_from_toxid(msg['from'].user, msg['to'])
# debug info
tdbg(' +-- from : %s' % toxjid)
tdbg(' +-- to : %(to)s\nTOXMPP :: +-- msg : %(body)s\nTOXMPP :: +-- msg : %(type)s' % msg )
msg['from'] = toxjid
# just send it already
if isinstance(msg, sleekxmpp.Message):
tdbg('toxmpp.run() : sending the message out');
self.send_message(mto=msg['to'],
mfrom=msg['from'],
mbody=msg['body'],
mtype=msg['type'])
"""
# set the stream
msg.stream = self
# send the message
msg.send()
"""
# wat
else:
raise TypeError('sleekxmpp.Message() expected')
#self.handle_t2x_queue_item(self.t2x_queue.get())
sleep(0.5)
# getting an internal JID from a ToxID (either "short" or a "full" one)
# for each "short" Tox IDs we might have several different "long" versions
# so we need to find the one that has a given friend_jid in its roster
# TODO: this has to get fixed on some other level!
def get_jid_from_toxid(self, toxid, friend_jid):
# well, if it's a valid "long" Tox id, just use this
if len(toxid) == 76:
# if not valid, complain
if not self._verify_toxid(toxid):
raise ValueError("Valid (long) Tox ID expected, '%s' received" % toxid)
# assemble the internal jid
return '%s@%s' % (toxid, self.boundjid.bare)
# ah, so is it a short tox id?
if len(toxid) == 64:
# while we're at it... let's check if it's a valid short tox id
try:
int(toxid, 16)
except ValueError:
raise ValueError("Valid (short) Tox ID expected, '%s' received" % toxid)
# doesn't contain 0x in front?
if toxid.lower()[0:2] == '0x':
raise ValueError("Valid (short) Tox ID expected, '%s' received" % toxid)
# okay, we have a *valid* short Tox ID on our hands
# let's loop through internal JIDs and see if any fit
ijids = []
for ijid in self.roster:
# found a matching one?
# is the external JID in its roster?
if ijid.startswith(toxid) and friend_jid in self.roster[ijid]:
ijids.append(ijid)
# this is where it gets tricky
# 1. a given external JID can be in several internal JIDs rosters
# even in several that match a given short Tox ID
# this could happen if an external JID user added a given Tox user
# to their roster several times with the Tox user changing their
# nospam in the meantime
if ijids:
# well, at least we've found *something*; is there more than one matching jid?
if len(ijids) == 1:
# nope, just return this
return ijids[0]
# more than one? uh-huh. find the first one that has friend_jid in roster
# TODO: this is going away as soon as the whole short/long ToxID thing gets fixed somewhere else
for ijid in ijids:
if friend_jid in self.roster[ijid]:
return ijid
# well, just return the first one
else:
return ijids[0]
# 2. the external JID can even be unavailable in any internal JID's
# rosters
else:
return '%s@%s' % (toxid, self.boundjid.bare)
# dafuq?
else:
raise ValueError("Valid (long or short) Tox ID expected, '%s' received" % toxid)
def handle_message(self, qitem):
"""
handling the message stanza
@ -150,6 +223,8 @@ class ToXMPPComponent(ComponentXMPP):
Checking if a given string is a valid ToxID, i.e. is 76 chars
long hex (i.e. A-Fa-f0-9) string
https://libtoxcore.so/core_concepts.html
checking *only* for the long, nospam-containing version!
cf. https://github.com/irungentoo/toxcore/issues/854
TODO: verifying the checksum?
"""
try:

View File

@ -199,7 +199,10 @@ class ToxNode(Tox):
# fire the callback
if callable(self.message_received_callback):
print('+-- calling message_received_callback()')
self.message_received_callback(self.get_client_id(friend_id), message)
# get_client_id() will only give us the public key,
# *not* the full ToxID a given user has been "friended" with use of
# https://github.com/irungentoo/toxcore/issues/854
self.message_received_callback(self.get_client_id(friend_id).lower(), message)