continue rewrite
This commit is contained in:
128
xmpp_tg/xmpp.py
128
xmpp_tg/xmpp.py
@@ -266,6 +266,13 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
_unknown_command_handler = lambda self, self2: "Unknown command, for a list send '!help'"
|
_unknown_command_handler = lambda self, self2: "Unknown command, for a list send '!help'"
|
||||||
_on_connect = lambda: None
|
_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):
|
def __init__(self, msg):
|
||||||
self._command = msg["body"].split(" ")[0][1:]
|
self._command = msg["body"].split(" ")[0][1:]
|
||||||
self._handler = getattr(self, self._command, self._unknown_command_handler)
|
self._handler = getattr(self, self._command, self._unknown_command_handler)
|
||||||
@@ -347,8 +354,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
"""Initiates Telegram session
|
"""Initiates Telegram session
|
||||||
|
|
||||||
Usage: !login <phone number in international format>"""
|
Usage: !login <phone number in international format>"""
|
||||||
if len(hndl.arguments) != 1:
|
hndl._min_args(1)
|
||||||
return "Wrong number of arguments"
|
|
||||||
|
|
||||||
phone_no = hndl.arguments[0]
|
phone_no = hndl.arguments[0]
|
||||||
|
|
||||||
@@ -366,6 +372,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
return 'Please enter one-time code via !code 12345.'
|
return 'Please enter one-time code via !code 12345.'
|
||||||
|
|
||||||
def code(hndl, self):
|
def code(hndl, self):
|
||||||
|
hndl._min_args(1)
|
||||||
|
|
||||||
code = hndl.arguments[0]
|
code = hndl.arguments[0]
|
||||||
jid = hndl.jid
|
jid = hndl.jid
|
||||||
@@ -392,6 +399,8 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
return 'You are already authenticated. Please use !logout before new login.'
|
return 'You are already authenticated. Please use !logout before new login.'
|
||||||
|
|
||||||
def password(hndl, self):
|
def password(hndl, self):
|
||||||
|
hndl._min_args(1)
|
||||||
|
|
||||||
password = hndl.arguments[1]
|
password = hndl.arguments[1]
|
||||||
jid = self.jid
|
jid = self.jid
|
||||||
|
|
||||||
@@ -433,8 +442,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
|
|
||||||
def add(hndl, self): #1 arg
|
def add(hndl, self): #1 arg
|
||||||
"""Add contact by nickname or t.me link"""
|
"""Add contact by nickname or t.me link"""
|
||||||
if len(hndl.arguments) != 1:
|
hndl._min_args(1)
|
||||||
return "Wrong number of arguments"
|
|
||||||
|
|
||||||
argument = hndl.arguments[0]
|
argument = hndl.arguments[0]
|
||||||
jid = hndl.jid
|
jid = hndl.jid
|
||||||
@@ -454,8 +462,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
|
|
||||||
def join(hndl, self): #1 arg
|
def join(hndl, self): #1 arg
|
||||||
"""Join conference via invite link"""
|
"""Join conference via invite link"""
|
||||||
if len(hndl.arguments) != 1:
|
hndl._min_args(1)
|
||||||
return "Wrong number of arguments"
|
|
||||||
|
|
||||||
argument = hndl.arguments[0]
|
argument = hndl.arguments[0]
|
||||||
jid = hndl.jid
|
jid = hndl.jid
|
||||||
@@ -466,6 +473,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.tg_process_dialogs(jid)
|
self.tg_process_dialogs(jid)
|
||||||
|
|
||||||
def group(self): #2 args
|
def group(self): #2 args
|
||||||
|
hndl._min_args(2)
|
||||||
# group name? #
|
# group name? #
|
||||||
groupname = parsed[1]
|
groupname = parsed[1]
|
||||||
|
|
||||||
@@ -477,39 +485,51 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.tg_process_dialogs(jid)
|
self.tg_process_dialogs(jid)
|
||||||
|
|
||||||
def channel(self): #1 arg
|
def channel(self): #1 arg
|
||||||
|
hndl._min_args(1)
|
||||||
groupname = parsed[1]
|
groupname = parsed[1]
|
||||||
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, broadcast = True))
|
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, broadcast = True))
|
||||||
self.tg_process_dialogs(jid)
|
self.tg_process_dialogs(jid)
|
||||||
|
|
||||||
def supergroup(self): #1 arg
|
def supergroup(self): #1 arg
|
||||||
|
hndl._min_args(1)
|
||||||
groupname = parsed[1]
|
groupname = parsed[1]
|
||||||
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, megagroup = True))
|
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, megagroup = True))
|
||||||
self.tg_process_dialogs(jid)
|
self.tg_process_dialogs(jid)
|
||||||
|
|
||||||
def username(self): #1 arg
|
def username(self): #1 arg
|
||||||
"""Change your Telegram display name"""
|
"""Change your Telegram nickname"""
|
||||||
username = parsed[1]
|
hndl._min_args(1)
|
||||||
self.tg_connections[jid].invoke(UpdateUsernameRequest(username))
|
|
||||||
|
|
||||||
def name(self): #1 or 2 args
|
username = hndl.arguments[0]
|
||||||
"""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))
|
|
||||||
|
|
||||||
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"""
|
"""Change your Telegram 'about' text"""
|
||||||
about = iq['body'][7:]
|
hndl._min_args(1)
|
||||||
self.tg_connections[jid].invoke(UpdateProfileRequest(about = about))
|
|
||||||
|
|
||||||
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"""
|
"""Add contact by phone number"""
|
||||||
phone = parsed[1]
|
hndl._min_args(2)
|
||||||
firstname = parsed[2]
|
|
||||||
lastname = parsed[3] if len(parsed) > 3 else None
|
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)
|
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)
|
self.tg_process_dialogs(jid)
|
||||||
|
|
||||||
def roster(hndl, self):
|
def roster(hndl, self):
|
||||||
@@ -519,6 +539,35 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
response += "{}: {}\n".format(tid, jid)
|
response += "{}: {}\n".format(tid, jid)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
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'
|
||||||
|
'!password abc123 - Entering password during two-factor auth\n'
|
||||||
|
'!configure - Configure transport settings\n'
|
||||||
|
#'!list_sessions - List all created sessions at Telegram servers\n'
|
||||||
|
#'!delete_session 123 - Delete session\n'
|
||||||
|
'!logout - Deletes current Telegram session at gate\n'
|
||||||
|
'!reload_dialogs - Reloads dialogs list from Telegram\n\n'
|
||||||
|
|
||||||
|
'!add - Find and add Telegram contact. Any formats accepted (nickname or t.me link)\n\n'
|
||||||
|
|
||||||
|
'!join - Join Telegram conference via invite link \n\n'
|
||||||
|
|
||||||
|
'!import phone firstname lastname - Add Telegram contact with phone number \n\n'
|
||||||
|
|
||||||
|
'!group GroupName @InviteContact - Create a normal group\n'
|
||||||
|
'!supergroup SupergroupName - Create a supergroup\n'
|
||||||
|
'!channel ChannelName - Create a channel\n\n'
|
||||||
|
|
||||||
|
'!name first last - Change your name in Telegram\n'
|
||||||
|
'!about text - Change about text in Telegram\n'
|
||||||
|
'!username - Changes your @username in Telegram\n\n'
|
||||||
|
|
||||||
|
'!roster - Lists yout TG roster\n')
|
||||||
|
|
||||||
|
|
||||||
def process_command(self, msg):
|
def process_command(self, msg):
|
||||||
"""
|
"""
|
||||||
@@ -540,6 +589,8 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
else:
|
else:
|
||||||
if isinstance(e, NotAuthorizedError):
|
if isinstance(e, NotAuthorizedError):
|
||||||
reply = str(e)
|
reply = str(e)
|
||||||
|
elif isinstance(e, self.MessageHandler.WrongNumberOfArgsError):
|
||||||
|
reply = str(e)
|
||||||
else:
|
else:
|
||||||
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
@@ -548,39 +599,6 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.gate_reply_message(msg, reply)
|
self.gate_reply_message(msg, reply)
|
||||||
#msg.reply(reply).send()
|
#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'
|
|
||||||
|
|
||||||
'!help - Displays this text\n'
|
|
||||||
'!login +123456789 - Initiates Telegram session\n'
|
|
||||||
'!code 12345 - Entering one-time code during auth\n'
|
|
||||||
'!password abc123 - Entering password during two-factor auth\n'
|
|
||||||
'!configure - Configure transport settings\n'
|
|
||||||
#'!list_sessions - List all created sessions at Telegram servers\n'
|
|
||||||
#'!delete_session 123 - Delete session\n'
|
|
||||||
'!logout - Deletes current Telegram session at gate\n'
|
|
||||||
'!reload_dialogs - Reloads dialogs list from Telegram\n\n'
|
|
||||||
|
|
||||||
'!add - Find and add Telegram contact. Any formats accepted (nickname or t.me link)\n\n'
|
|
||||||
|
|
||||||
'!join - Join Telegram conference via invite link \n\n'
|
|
||||||
|
|
||||||
'!import phone firstname lastname - Add Telegram contact with phone number \n\n'
|
|
||||||
|
|
||||||
'!group GroupName @InviteContact - Create a normal group\n'
|
|
||||||
'!supergroup SupergroupName - Create a supergroup\n'
|
|
||||||
'!channel ChannelName - Create a channel\n\n'
|
|
||||||
|
|
||||||
'!name first last - Change your name in Telegram\n'
|
|
||||||
'!about text - Change about text in Telegram\n'
|
|
||||||
'!username - Changes your @username in Telegram\n\n'
|
|
||||||
|
|
||||||
'!roster - Lists yout TG roster\n'
|
|
||||||
)
|
|
||||||
|
|
||||||
def process_chat_user_command(self, iq):
|
def process_chat_user_command(self, iq):
|
||||||
parsed = iq['body'].split(' ')
|
parsed = iq['body'].split(' ')
|
||||||
jid = iq['from'].bare
|
jid = iq['from'].bare
|
||||||
|
|||||||
Reference in New Issue
Block a user