Re-org and start of API #47
3 changed files with 108 additions and 139 deletions
23
bumper/db.py
23
bumper/db.py
|
|
@ -24,23 +24,16 @@ def os_db_path(): # createdir=True):
|
|||
|
||||
|
||||
def db_get():
|
||||
try:
|
||||
# Will create the database if it doesn't exist
|
||||
db = TinyDB(db_file())
|
||||
# Will create the database if it doesn't exist
|
||||
db = TinyDB(db_file())
|
||||
|
||||
# Will create the tables if they don't exist
|
||||
db.table("users", cache_size=0)
|
||||
db.table("clients", cache_size=0)
|
||||
db.table("bots", cache_size=0)
|
||||
db.table("tokens", cache_size=0)
|
||||
# Will create the tables if they don't exist
|
||||
db.table("users", cache_size=0)
|
||||
db.table("clients", cache_size=0)
|
||||
db.table("bots", cache_size=0)
|
||||
db.table("tokens", cache_size=0)
|
||||
|
||||
return db
|
||||
|
||||
except json.decoder.JSONDecodeError as jerr:
|
||||
bumperlog.error("JsonErr: {} - Doc: {}".format(jerr.msg, jerr.doc))
|
||||
|
||||
except Exception as ex:
|
||||
bumperlog.error(ex)
|
||||
return db
|
||||
|
||||
|
||||
def user_add(userid):
|
||||
|
|
|
|||
|
|
@ -354,41 +354,33 @@ class BumperMQTTServer_Plugin:
|
|||
return authenticated
|
||||
|
||||
async def on_broker_client_connected(self, client_id):
|
||||
try:
|
||||
|
||||
didsplit = str(client_id).split("@")
|
||||
didsplit = str(client_id).split("@")
|
||||
|
||||
bot = bumper.bot_get(didsplit[0])
|
||||
if bot:
|
||||
bumper.bot_set_mqtt(bot["did"], True)
|
||||
return
|
||||
bot = bumper.bot_get(didsplit[0])
|
||||
if bot:
|
||||
bumper.bot_set_mqtt(bot["did"], True)
|
||||
return
|
||||
|
||||
# clientuserid = didsplit[0]
|
||||
clientresource = didsplit[1].split("/")[1]
|
||||
client = bumper.client_get(clientresource)
|
||||
if client:
|
||||
bumper.client_set_mqtt(client["resource"], True)
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
mqttserverlog.exception("{}".format(e))
|
||||
# clientuserid = didsplit[0]
|
||||
clientresource = didsplit[1].split("/")[1]
|
||||
client = bumper.client_get(clientresource)
|
||||
if client:
|
||||
bumper.client_set_mqtt(client["resource"], True)
|
||||
return
|
||||
|
||||
async def on_broker_client_disconnected(self, client_id):
|
||||
try:
|
||||
didsplit = str(client_id).split("@")
|
||||
|
||||
bot = bumper.bot_get(didsplit[0])
|
||||
if bot:
|
||||
bumper.bot_set_mqtt(bot["did"], False)
|
||||
return
|
||||
didsplit = str(client_id).split("@")
|
||||
|
||||
# clientuserid = didsplit[0]
|
||||
clientresource = didsplit[1].split("/")[1]
|
||||
client = bumper.client_get(clientresource)
|
||||
if client:
|
||||
bumper.client_set_mqtt(client["resource"], False)
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
mqttserverlog.exception("{}".format(e))
|
||||
bot = bumper.bot_get(didsplit[0])
|
||||
if bot:
|
||||
bumper.bot_set_mqtt(bot["did"], False)
|
||||
return
|
||||
|
||||
# clientuserid = didsplit[0]
|
||||
clientresource = didsplit[1].split("/")[1]
|
||||
client = bumper.client_get(clientresource)
|
||||
if client:
|
||||
bumper.client_set_mqtt(client["resource"], False)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -51,17 +51,14 @@ class XMPPServer:
|
|||
asyncio.create_task(bumper.shutdown())
|
||||
|
||||
def disconnect(self):
|
||||
try:
|
||||
xmppserverlog.debug("waiting for all clients to disconnect")
|
||||
for client in self.clients:
|
||||
client._disconnect()
|
||||
|
||||
self.exit_flag = True
|
||||
xmppserverlog.debug("shutting down")
|
||||
self.server_coro.cancel()
|
||||
xmppserverlog.debug("waiting for all clients to disconnect")
|
||||
for client in self.clients:
|
||||
client._disconnect()
|
||||
|
||||
except Exception as e:
|
||||
xmppserverlog.error("{}".format(e))
|
||||
self.exit_flag = True
|
||||
xmppserverlog.debug("shutting down")
|
||||
self.server_coro.cancel()
|
||||
|
||||
|
||||
class XMPPServer_Protocol(asyncio.Protocol):
|
||||
|
|
@ -615,80 +612,70 @@ class XMPPAsyncClient:
|
|||
xmppserverlog.exception("{}".format(e))
|
||||
|
||||
def _handle_session(self, xml):
|
||||
try:
|
||||
res = '<iq type="result" id="{}" />'.format(xml.get("id"))
|
||||
self._set_state("READY")
|
||||
self.send(res)
|
||||
asyncio.Task(self.schedule_ping(30))
|
||||
|
||||
except Exception as e:
|
||||
xmppserverlog.exception("{}".format(e))
|
||||
res = '<iq type="result" id="{}" />'.format(xml.get("id"))
|
||||
self._set_state("READY")
|
||||
self.send(res)
|
||||
asyncio.Task(self.schedule_ping(30))
|
||||
|
||||
def _handle_presence(self, xml):
|
||||
try:
|
||||
|
||||
if len(xml) and xml[0].tag == "status":
|
||||
if len(xml) and xml[0].tag == "status":
|
||||
xmppserverlog.debug(
|
||||
"bot presence {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8").decode("utf-8")
|
||||
)
|
||||
)
|
||||
# Most likely a bot, possibly hello world in text
|
||||
|
||||
# Send dummy return
|
||||
self.send('<presence to="{}"> dummy </presence>'.format(self.bumper_jid))
|
||||
|
||||
# If it is a BOT, send extras
|
||||
if self.type == self.BOT:
|
||||
# get device info
|
||||
self.send(
|
||||
'<iq type="set" id="14" to="{}" from="{}"><query xmlns="com:ctl"><ctl td="GetDeviceInfo"/></query></iq>'.format(
|
||||
self.bumper_jid, XMPPServer.server_id
|
||||
)
|
||||
)
|
||||
|
||||
else:
|
||||
xmppserverlog.debug(
|
||||
"client presence - {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8").decode("utf-8")
|
||||
)
|
||||
)
|
||||
|
||||
if xml.get("type") == "available":
|
||||
xmppserverlog.debug(
|
||||
"bot presence {} ".format(
|
||||
"client presence available - {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8").decode("utf-8")
|
||||
)
|
||||
)
|
||||
# Most likely a bot, possibly hello world in text
|
||||
|
||||
# Send dummy return
|
||||
self.send(
|
||||
'<presence to="{}"> dummy </presence>'.format(self.bumper_jid)
|
||||
)
|
||||
|
||||
# If it is a BOT, send extras
|
||||
if self.type == self.BOT:
|
||||
# get device info
|
||||
self.send(
|
||||
'<iq type="set" id="14" to="{}" from="{}"><query xmlns="com:ctl"><ctl td="GetDeviceInfo"/></query></iq>'.format(
|
||||
self.bumper_jid, XMPPServer.server_id
|
||||
)
|
||||
)
|
||||
|
||||
else:
|
||||
elif xml.get("type") == "unavailable":
|
||||
xmppserverlog.debug(
|
||||
"client presence - {} ".format(
|
||||
"client presence unavailable (DISCONNECT) - {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8").decode("utf-8")
|
||||
)
|
||||
)
|
||||
|
||||
if xml.get("type") == "available":
|
||||
xmppserverlog.debug(
|
||||
"client presence available - {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8").decode("utf-8")
|
||||
)
|
||||
self._set_state("DISCONNECT")
|
||||
else:
|
||||
# Sometimes the android app sends these
|
||||
xmppserverlog.debug(
|
||||
"client presence (UNKNOWN) - {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8")
|
||||
)
|
||||
|
||||
# Send dummy return
|
||||
self.send(
|
||||
'<presence to="{}"> dummy </presence>'.format(self.bumper_jid)
|
||||
)
|
||||
elif xml.get("type") == "unavailable":
|
||||
xmppserverlog.debug(
|
||||
"client presence unavailable (DISCONNECT) - {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8").decode("utf-8")
|
||||
)
|
||||
)
|
||||
|
||||
self._set_state("DISCONNECT")
|
||||
else:
|
||||
# Sometimes the android app sends these
|
||||
xmppserverlog.debug(
|
||||
"client presence (UNKNOWN) - {} ".format(
|
||||
ET.tostring(xml, encoding="utf-8")
|
||||
)
|
||||
)
|
||||
# Send dummy return
|
||||
self.send(
|
||||
'<presence to="{}"> dummy </presence>'.format(self.bumper_jid)
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
xmppserverlog.exception("{}".format(e))
|
||||
)
|
||||
# Send dummy return
|
||||
self.send(
|
||||
'<presence to="{}"> dummy </presence>'.format(self.bumper_jid)
|
||||
)
|
||||
|
||||
def _parse_data(self, data):
|
||||
|
||||
|
|
@ -804,34 +791,31 @@ class XMPPAsyncClient:
|
|||
xmppserverlog.exception("{}".format(e))
|
||||
|
||||
def _handle_iq(self, xml, data):
|
||||
try:
|
||||
if len(xml):
|
||||
child = self._tag_strip_uri(xml[0].tag)
|
||||
else:
|
||||
child = None
|
||||
|
||||
if xml.tag == "iq":
|
||||
if child == "bind":
|
||||
self._handle_bind(xml)
|
||||
elif child == "session":
|
||||
self._handle_session(xml)
|
||||
elif child == "ping":
|
||||
self._handle_ping(xml, data)
|
||||
elif child == "query":
|
||||
if self.type == self.BOT:
|
||||
self._handle_result(xml, data)
|
||||
else:
|
||||
self._handle_ctl(xml, data)
|
||||
elif xml.get("type") == "result":
|
||||
if self.type == self.BOT:
|
||||
self._handle_result(xml, data)
|
||||
else:
|
||||
self._handle_result(xml, data)
|
||||
elif xml.get("type") == "set":
|
||||
if self.type == self.BOT:
|
||||
self._handle_result(xml, data)
|
||||
else:
|
||||
self._handle_result(xml, data)
|
||||
if len(xml):
|
||||
child = self._tag_strip_uri(xml[0].tag)
|
||||
else:
|
||||
child = None
|
||||
|
||||
except Exception as e:
|
||||
xmppserverlog.exception("{}".format(e))
|
||||
if xml.tag == "iq":
|
||||
if child == "bind":
|
||||
self._handle_bind(xml)
|
||||
elif child == "session":
|
||||
self._handle_session(xml)
|
||||
elif child == "ping":
|
||||
self._handle_ping(xml, data)
|
||||
elif child == "query":
|
||||
if self.type == self.BOT:
|
||||
self._handle_result(xml, data)
|
||||
else:
|
||||
self._handle_ctl(xml, data)
|
||||
elif xml.get("type") == "result":
|
||||
if self.type == self.BOT:
|
||||
self._handle_result(xml, data)
|
||||
else:
|
||||
self._handle_result(xml, data)
|
||||
elif xml.get("type") == "set":
|
||||
if self.type == self.BOT:
|
||||
self._handle_result(xml, data)
|
||||
else:
|
||||
self._handle_result(xml, data)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue