continue rewrite
This commit is contained in:
136
xmpp_tg/xmpp.py
136
xmpp_tg/xmpp.py
@@ -266,6 +266,13 @@ class XMPPTelegram(ComponentXMPP):
|
||||
_unknown_command_handler = lambda self, self2: "Unknown command, for a list send '!help'"
|
||||
_on_connect = lambda: None
|
||||
|
||||
class WrongNumberOfArgsError(Exception):
|
||||
pass
|
||||
|
||||
def _min_args(self, num_args):
|
||||
if len(self.arguments) < num_args:
|
||||
raise WrongNumberOfArgumentsError("!{} needs at least {} arguments".format(self._command, num_args))
|
||||
|
||||
def __init__(self, msg):
|
||||
self._command = msg["body"].split(" ")[0][1:]
|
||||
self._handler = getattr(self, self._command, self._unknown_command_handler)
|
||||
@@ -347,8 +354,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
"""Initiates Telegram session
|
||||
|
||||
Usage: !login <phone number in international format>"""
|
||||
if len(hndl.arguments) != 1:
|
||||
return "Wrong number of arguments"
|
||||
hndl._min_args(1)
|
||||
|
||||
phone_no = hndl.arguments[0]
|
||||
|
||||
@@ -366,6 +372,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
return 'Please enter one-time code via !code 12345.'
|
||||
|
||||
def code(hndl, self):
|
||||
hndl._min_args(1)
|
||||
|
||||
code = hndl.arguments[0]
|
||||
jid = hndl.jid
|
||||
@@ -392,6 +399,8 @@ class XMPPTelegram(ComponentXMPP):
|
||||
return 'You are already authenticated. Please use !logout before new login.'
|
||||
|
||||
def password(hndl, self):
|
||||
hndl._min_args(1)
|
||||
|
||||
password = hndl.arguments[1]
|
||||
jid = self.jid
|
||||
|
||||
@@ -433,8 +442,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
|
||||
def add(hndl, self): #1 arg
|
||||
"""Add contact by nickname or t.me link"""
|
||||
if len(hndl.arguments) != 1:
|
||||
return "Wrong number of arguments"
|
||||
hndl._min_args(1)
|
||||
|
||||
argument = hndl.arguments[0]
|
||||
jid = hndl.jid
|
||||
@@ -454,8 +462,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
|
||||
def join(hndl, self): #1 arg
|
||||
"""Join conference via invite link"""
|
||||
if len(hndl.arguments) != 1:
|
||||
return "Wrong number of arguments"
|
||||
hndl._min_args(1)
|
||||
|
||||
argument = hndl.arguments[0]
|
||||
jid = hndl.jid
|
||||
@@ -466,6 +473,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def group(self): #2 args
|
||||
hndl._min_args(2)
|
||||
# group name? #
|
||||
groupname = parsed[1]
|
||||
|
||||
@@ -477,39 +485,51 @@ class XMPPTelegram(ComponentXMPP):
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def channel(self): #1 arg
|
||||
hndl._min_args(1)
|
||||
groupname = parsed[1]
|
||||
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, broadcast = True))
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def supergroup(self): #1 arg
|
||||
hndl._min_args(1)
|
||||
groupname = parsed[1]
|
||||
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, megagroup = True))
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def username(self): #1 arg
|
||||
"""Change your Telegram display name"""
|
||||
username = parsed[1]
|
||||
self.tg_connections[jid].invoke(UpdateUsernameRequest(username))
|
||||
"""Change your Telegram nickname"""
|
||||
hndl._min_args(1)
|
||||
|
||||
def name(self): #1 or 2 args
|
||||
"""Change your Telegram display name"""
|
||||
firstname = parsed[1]
|
||||
lastname = parsed[2] if len(parsed) > 2 else None
|
||||
self.tg_connections[jid].invoke(UpdateProfileRequest(first_name = firstname, last_name = lastname))
|
||||
username = hndl.arguments[0]
|
||||
|
||||
def about(self): #>0 args
|
||||
self.tg_connections[hndl.jid].invoke(UpdateUsernameRequest(username))
|
||||
|
||||
def name(hndl, self): #1 or 2 args
|
||||
"""Change your Telegram display name"""
|
||||
hndl._min_args(1)
|
||||
|
||||
firstname = hndl.arguments[0]
|
||||
lastname = hndl.arguments[1] if len(hndl.arguments) > 1 else None
|
||||
|
||||
self.tg_connections[hndl.jid].invoke(UpdateProfileRequest(first_name = firstname, last_name = lastname))
|
||||
|
||||
def about(hndl, self): #>0 args
|
||||
"""Change your Telegram 'about' text"""
|
||||
about = iq['body'][7:]
|
||||
self.tg_connections[jid].invoke(UpdateProfileRequest(about = about))
|
||||
hndl._min_args(1)
|
||||
|
||||
def import_contact(self): #2 args
|
||||
about = hndl.msg['body'][7:]
|
||||
self.tg_connections[hndl.jid].invoke(UpdateProfileRequest(about = about))
|
||||
|
||||
def import_contact(hndl, self): #2 args
|
||||
"""Add contact by phone number"""
|
||||
phone = parsed[1]
|
||||
firstname = parsed[2]
|
||||
lastname = parsed[3] if len(parsed) > 3 else None
|
||||
hndl._min_args(2)
|
||||
|
||||
phone = hndl.arguments[0]
|
||||
firstname = hndl.arguments[1]
|
||||
lastname = hndl.arguments[2] if len(hndl.arguments) > 2 else None
|
||||
|
||||
contact = InputPhoneContact(client_id=generate_random_long(), phone=phone, first_name=firstname, last_name=lastname)
|
||||
self.tg_connections[jid].invoke(ImportContactsRequest([contact]))
|
||||
self.tg_connections[hndl.jid].invoke(ImportContactsRequest([contact]))
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def roster(hndl, self):
|
||||
@@ -519,41 +539,9 @@ class XMPPTelegram(ComponentXMPP):
|
||||
response += "{}: {}\n".format(tid, jid)
|
||||
return response
|
||||
|
||||
|
||||
def process_command(self, msg):
|
||||
"""
|
||||
Commands to gateway, users or chats (starts with !)
|
||||
:param iq:
|
||||
:return:
|
||||
"""
|
||||
logging.info("received message "+str(msg["body"])+" from "+str(msg["from"]))
|
||||
is_command = msg["body"].startswith("!") and msg["body"][1] != "_"
|
||||
|
||||
if is_command:
|
||||
command = msg["body"].split(" ")[0][1:]
|
||||
handler = self.GateMessageHandler(msg)._handler
|
||||
try:
|
||||
reply = str(handler(self))
|
||||
except Exception as e:
|
||||
if self.config["debug"]:
|
||||
reply = traceback.format_exc()
|
||||
else:
|
||||
if isinstance(e, NotAuthorizedError):
|
||||
reply = str(e)
|
||||
else:
|
||||
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
||||
traceback.print_exc()
|
||||
reply = "Internal error, please contact Sysadmin"
|
||||
if reply is not None:
|
||||
self.gate_reply_message(msg, reply)
|
||||
#msg.reply(reply).send()
|
||||
|
||||
parsed = msg['body'].split(' ')
|
||||
jid = msg['from'].bare
|
||||
|
||||
if parsed[0] == '!help':
|
||||
self.gate_reply_message(msg, '=== Available gateway commands ===:\n\n'
|
||||
|
||||
def oldhelp(hndl, self):
|
||||
"""Old !help message"""
|
||||
return ('=== Available gateway commands ===:\n\n'
|
||||
'!help - Displays this text\n'
|
||||
'!login +123456789 - Initiates Telegram session\n'
|
||||
'!code 12345 - Entering one-time code during auth\n'
|
||||
@@ -578,8 +566,38 @@ class XMPPTelegram(ComponentXMPP):
|
||||
'!about text - Change about text in Telegram\n'
|
||||
'!username - Changes your @username in Telegram\n\n'
|
||||
|
||||
'!roster - Lists yout TG roster\n'
|
||||
)
|
||||
'!roster - Lists yout TG roster\n')
|
||||
|
||||
|
||||
def process_command(self, msg):
|
||||
"""
|
||||
Commands to gateway, users or chats (starts with !)
|
||||
:param iq:
|
||||
:return:
|
||||
"""
|
||||
logging.info("received message "+str(msg["body"])+" from "+str(msg["from"]))
|
||||
is_command = msg["body"].startswith("!") and msg["body"][1] != "_"
|
||||
|
||||
if is_command:
|
||||
command = msg["body"].split(" ")[0][1:]
|
||||
handler = self.GateMessageHandler(msg)._handler
|
||||
try:
|
||||
reply = str(handler(self))
|
||||
except Exception as e:
|
||||
if self.config["debug"]:
|
||||
reply = traceback.format_exc()
|
||||
else:
|
||||
if isinstance(e, NotAuthorizedError):
|
||||
reply = str(e)
|
||||
elif isinstance(e, self.MessageHandler.WrongNumberOfArgsError):
|
||||
reply = str(e)
|
||||
else:
|
||||
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
||||
traceback.print_exc()
|
||||
reply = "Internal error, please contact Sysadmin"
|
||||
if reply is not None:
|
||||
self.gate_reply_message(msg, reply)
|
||||
#msg.reply(reply).send()
|
||||
|
||||
def process_chat_user_command(self, iq):
|
||||
parsed = iq['body'].split(' ')
|
||||
|
||||
Reference in New Issue
Block a user