Switch from sleekxmpp to slixmpp

This commit is contained in:
Alexei Sorokin 2017-05-31 15:15:21 +03:00
parent ee5fa7e9e2
commit bea576efb3
4 changed files with 67 additions and 56 deletions

View File

@ -1,3 +1,3 @@
# hptoad # hptoad
An MIT licensed XMPP bot written using Python and sleekxmpp. An MIT licensed XMPP bot written using Python 3 and slixmpp.

View File

@ -1,8 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys import sys
import sleekxmpp import slixmpp
opts = { opts = {
"jid": "botname@example.com", "jid": "botname@example.com",
@ -12,9 +11,14 @@ opts = {
} }
def on_failed_all_auth(event):
print("Auth: Could not connect to the server, or password mismatch!")
sys.exit(1)
def on_session_start(event): def on_session_start(event):
client.get_roster()
# client.send_presence() # client.send_presence()
client.get_roster()
body = "\n".join(sys.argv[1:]).strip() body = "\n".join(sys.argv[1:]).strip()
try: try:
@ -23,19 +27,19 @@ def on_session_start(event):
except Exception as e: except Exception as e:
print("%s: %s" % (type(e).__name__, str(e))) print("%s: %s" % (type(e).__name__, str(e)))
finally: finally:
client.disconnect(wait=True) client.disconnect()
def on_session_end(event):
sys.exit(0) sys.exit(0)
if __name__ == "__main__": if __name__ == "__main__":
if sys.version_info.major < 3:
sleekxmpp.util.misc_ops.setdefaultencoding("utf-8")
if len(sys.argv) <= 1: if len(sys.argv) <= 1:
print("At least one argument is required.") print("At least one argument is required.")
sys.exit(1) sys.exit(1)
client = sleekxmpp.ClientXMPP("%s/%s" % (opts["jid"], opts["resource"]), client = slixmpp.ClientXMPP("%s/%s" % (opts["jid"], opts["resource"]),
opts["password"]) opts["password"])
if opts["connect"]: if opts["connect"]:
@ -47,9 +51,8 @@ if __name__ == "__main__":
else: else:
connect = () connect = ()
if client.connect(connect): client.connect(connect)
client.add_event_handler("failed_all_auth", on_failed_all_auth)
client.add_event_handler("session_start", on_session_start) client.add_event_handler("session_start", on_session_start)
client.process(block=True) client.add_event_handler("session_end", on_session_end)
else: client.process(forever=True)
print("Could not connect to server, or password mismatch!")
sys.exit(1)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import logging import logging
import os import os
import re import re
@ -8,7 +8,7 @@ import signal
import subprocess import subprocess
import sys import sys
import time import time
import sleekxmpp import slixmpp
opts = { opts = {
"muc": "room@conference.example.com", "muc": "room@conference.example.com",
@ -22,10 +22,7 @@ opts = {
class Hptoad: class Hptoad:
def __init__(self, opts): def __init__(self, opts):
if sys.version_info.major < 3: self.client = slixmpp.ClientXMPP("%s/%s" % (opts["jid"],
sleekxmpp.util.misc_ops.setdefaultencoding("utf-8")
self.client = sleekxmpp.ClientXMPP("%s/%s" % (opts["jid"],
opts["resource"]), opts["resource"]),
opts["password"]) opts["password"])
self.client.register_plugin("xep_0199") # XMPP Ping. self.client.register_plugin("xep_0199") # XMPP Ping.
@ -43,10 +40,11 @@ class Hptoad:
self.bot_nick = self.pure_bot_nick self.bot_nick = self.pure_bot_nick
def register_handlers(self): def register_handlers(self):
self.client.add_event_handler("failed_all_auth",
self.on_failed_all_auth)
self.client.add_event_handler("session_start", self.on_session_start) self.client.add_event_handler("session_start", self.on_session_start)
self.client.add_event_handler("session_end", self.on_session_end) self.client.add_event_handler("session_end", self.on_session_end)
self.client.add_event_handler("message", self.on_message, self.client.add_event_handler("message", self.on_message)
threaded=True)
self.client.add_event_handler("muc::%s::presence" % self.muc, self.client.add_event_handler("muc::%s::presence" % self.muc,
self.on_muc_presence) self.on_muc_presence)
@ -63,13 +61,10 @@ class Hptoad:
else: else:
connect = () connect = ()
if not self.client.connect(connect): self.client.connect(connect)
self.logger.critical("Auth: Could not connect to server, or " +
"password mismatch!")
sys.exit(1)
def join_muc(self): def join_muc(self):
self.muc_obj.joinMUC(self.muc, self.bot_nick) self.muc_obj.join_muc(self.muc, self.bot_nick)
def log_exception(self, ex): def log_exception(self, ex):
self.logger.error("%s: %s" % (type(ex).__name__, str(ex))) self.logger.error("%s: %s" % (type(ex).__name__, str(ex)))
@ -83,7 +78,7 @@ class Hptoad:
if nick not in self.muc_obj.rooms[self.muc]: if nick not in self.muc_obj.rooms[self.muc]:
return False return False
affiliation = self.muc_obj.getJidProperty(muc, nick, "affiliation") affiliation = self.muc_obj.get_jid_property(muc, nick, "affiliation")
return True if affiliation in ("admin", "owner") else False return True if affiliation in ("admin", "owner") else False
_trim_regexp = re.compile("(`|\\$|\\.\\.)") _trim_regexp = re.compile("(`|\\$|\\.\\.)")
@ -98,6 +93,7 @@ class Hptoad:
# letter(ASCII or cyrillic), number, underscore only. # letter(ASCII or cyrillic), number, underscore only.
_cmd_validator_regexp = re.compile("^!(\\w|\\p{Cyrillic})*$") _cmd_validator_regexp = re.compile("^!(\\w|\\p{Cyrillic})*$")
@asyncio.coroutine
def prep_extern_cmd(self, body, nick, dir_path, is_admin=False): def prep_extern_cmd(self, body, nick, dir_path, is_admin=False):
cmd = body.split(" ", 1) cmd = body.split(" ", 1)
cmd[0] = cmd[0].strip() cmd[0] = cmd[0].strip()
@ -122,11 +118,12 @@ class Hptoad:
return proc_args, None return proc_args, None
@asyncio.coroutine
def extern_cmd(self, body, nick, from_id, dir_path, is_admin=False): def extern_cmd(self, body, nick, from_id, dir_path, is_admin=False):
reply = "" reply = ""
err = None err = None
cmd, prep_err = self.prep_extern_cmd(body, nick, dir_path, cmd, prep_err = yield from self.prep_extern_cmd(body, nick, dir_path,
is_admin=is_admin) is_admin=is_admin)
if prep_err: if prep_err:
@ -135,11 +132,11 @@ class Hptoad:
return reply, err return reply, err
try: try:
proc = subprocess.Popen(cmd, proc = yield from \
asyncio.create_subprocess_exec(*cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE)
universal_newlines=True) cmd_reply, cmd_err = yield from proc.communicate()
cmd_reply, cmd_err = proc.communicate()
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
reply = "%s: WAT" % nick reply = "%s: WAT" % nick
err = "Execute: %s" % str(e) err = "Execute: %s" % str(e)
@ -148,9 +145,10 @@ class Hptoad:
if cmd_err and len(cmd_err.strip()) > 0: if cmd_err and len(cmd_err.strip()) > 0:
err = "Process: %s" % cmd_err.strip() err = "Process: %s" % cmd_err.strip()
if cmd_reply and len(cmd_reply.strip()) > 0: if cmd_reply and len(cmd_reply.strip()) > 0:
reply = cmd_reply.strip() reply = cmd_reply.decode().strip()
return reply, err return reply, err
@asyncio.coroutine
def handle_cmd(self, body, nick, from_id, is_admin=False): def handle_cmd(self, body, nick, from_id, is_admin=False):
reply = "" reply = ""
err = None err = None
@ -174,14 +172,17 @@ class Hptoad:
reply = "%s: GTFO" % nick reply = "%s: GTFO" % nick
elif body.startswith("!"): # Any external command. elif body.startswith("!"): # Any external command.
reply, err = self.extern_cmd(body, nick, from_id, "plugins", reply, err = yield from self.extern_cmd(body, nick,
from_id, "plugins",
is_admin=is_admin) is_admin=is_admin)
return reply, err return reply, err
@asyncio.coroutine
def handle_self_message(self, body, nick, from_id): def handle_self_message(self, body, nick, from_id):
if body.startswith("!"): if body.startswith("!"):
msg, err = self.handle_cmd(body, nick, from_id, is_admin=True) msg, err = yield from self.handle_cmd(body, nick, from_id,
is_admin=True)
else: else:
msg = body.strip() msg = body.strip()
@ -189,6 +190,7 @@ class Hptoad:
self.client.send_message(mto=self.muc, mbody=msg, self.client.send_message(mto=self.muc, mbody=msg,
mtype="groupchat") mtype="groupchat")
@asyncio.coroutine
def handle_muc_message(self, body, nick, from_id): def handle_muc_message(self, body, nick, from_id):
is_admin = self.is_muc_admin(self.muc, nick) is_admin = self.is_muc_admin(self.muc, nick)
@ -199,13 +201,13 @@ class Hptoad:
call_regexp = re.compile("^%s[:,]" % self.bot_nick) call_regexp = re.compile("^%s[:,]" % self.bot_nick)
if body.startswith("!"): # Any external command. if body.startswith("!"): # Any external command.
reply, err = self.handle_cmd(body, nick, from_id, reply, err = yield from self.handle_cmd(body, nick, from_id,
is_admin=is_admin) is_admin=is_admin)
elif call_regexp.match(body): # Chat. elif call_regexp.match(body): # Chat.
cmd_body = call_regexp.sub("!answer", body) cmd_body = call_regexp.sub("!answer", body)
reply, err = self.extern_cmd(cmd_body, nick, from_id, "chat", reply, err = yield from self.extern_cmd(cmd_body, nick, from_id,
is_admin=is_admin) "chat", is_admin=is_admin)
if err: if err:
self.logger.error(err) self.logger.error(err)
@ -215,16 +217,22 @@ class Hptoad:
self.client.send_message(mto=self.muc, mbody=reply, self.client.send_message(mto=self.muc, mbody=reply,
mtype="groupchat") mtype="groupchat")
def on_failed_all_auth(self, event):
self.logger.critical("Auth: Could not connect to the server, or " +
"password mismatch!")
sys.exit(1)
def on_session_start(self, event): def on_session_start(self, event):
self.client.get_roster()
self.client.send_presence(pstatus="is there some food in this world?", self.client.send_presence(pstatus="is there some food in this world?",
ppriority=12) ppriority=12)
self.client.get_roster()
self.join_muc() self.join_muc()
def on_session_end(self, event): def on_session_end(self, event):
time.sleep(2.0) time.sleep(2.0)
self.connect() self.connect()
@asyncio.coroutine
def on_message(self, event): def on_message(self, event):
try: try:
if not event["type"] in ("chat", "normal", "groupchat"): if not event["type"] in ("chat", "normal", "groupchat"):
@ -238,12 +246,12 @@ class Hptoad:
if event["type"] == "groupchat": if event["type"] == "groupchat":
nick = event["mucnick"] nick = event["mucnick"]
if nick != self.bot_nick: if nick != self.bot_nick:
self.handle_muc_message(body, nick, from_id) yield from self.handle_muc_message(body, nick, from_id)
elif event["from"].bare == self.jid: elif event["from"].bare == self.jid:
# Use resource as a nickname with self messages. # Use resource as a nickname with self messages.
nick = from_id.resource nick = from_id.resource
self.handle_self_message(body, nick, from_id) yield from self.handle_self_message(body, nick, from_id)
except Exception as e: except Exception as e:
self.log_exception(e) self.log_exception(e)
@ -256,7 +264,7 @@ class Hptoad:
if not typ: if not typ:
typ = event["type"] typ = event["type"]
if not nick: if not nick:
nick = self.muc_obj.getNick(self.muc, from_id) nick = self.muc_obj.get_nick(self.muc, from_id)
if typ == "error": if typ == "error":
if event["error"]["code"] == "409": if event["error"]["code"] == "409":
@ -274,7 +282,7 @@ class Hptoad:
def run(self): def run(self):
self.register_handlers() self.register_handlers()
self.connect() self.connect()
self.client.process(block=True) self.client.process(forever=True)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1 +1 @@
sleekxmpp>=1.2.0 slixmpp