cleanup
This commit is contained in:
@@ -110,6 +110,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.process_command(iq)
|
self.process_command(iq)
|
||||||
else:
|
else:
|
||||||
self.gate_reply_message(iq, 'Only commands accepted. Try !help for more info.')
|
self.gate_reply_message(iq, 'Only commands accepted. Try !help for more info.')
|
||||||
|
|
||||||
elif iq['type'] == 'chat': # --- outgoing message ---
|
elif iq['type'] == 'chat': # --- outgoing message ---
|
||||||
if jid in self.tg_connections and self.tg_connections[jid].is_user_authorized():
|
if jid in self.tg_connections and self.tg_connections[jid].is_user_authorized():
|
||||||
if iq['body'].startswith('!'): # it is command!
|
if iq['body'].startswith('!'): # it is command!
|
||||||
@@ -119,15 +120,19 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.process_chat_group_command(iq)
|
self.process_chat_group_command(iq)
|
||||||
else:
|
else:
|
||||||
self.gate_reply_message(iq, 'Error.')
|
self.gate_reply_message(iq, 'Error.')
|
||||||
|
|
||||||
else: # -- normal message --
|
else: # -- normal message --
|
||||||
tg_id = int(iq['to'].node[1:])
|
try:
|
||||||
|
tg_id = int(iq['to'].node[1:])
|
||||||
|
except ValueError:
|
||||||
|
self.gate_reply_message(iq, 'Invalid JID')
|
||||||
tg_peer = None
|
tg_peer = None
|
||||||
msg = iq['body']
|
msg = iq['body']
|
||||||
reply_mid = None
|
reply_mid = None
|
||||||
|
|
||||||
if msg.startswith('>'): # quoting check
|
if msg.startswith('>'): # quoting check
|
||||||
msg_lines = msg.split('\n')
|
msg_lines = msg.split('\n')
|
||||||
matched = re.match(r'>[ ]*(?P<mid>[\d]+)[ ]*', msg_lines[0])
|
matched = re.match(r'>[ ]*(?P<mid>[\d]+)[ ]*', msg_lines[0]) #TODO: check regex
|
||||||
matched = matched.groupdict() if matched else {}
|
matched = matched.groupdict() if matched else {}
|
||||||
|
|
||||||
if 'mid' in matched: # citation
|
if 'mid' in matched: # citation
|
||||||
@@ -267,6 +272,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
|
|
||||||
if parsed[0] == '!help':
|
if parsed[0] == '!help':
|
||||||
self.gate_reply_message(iq, '=== Available gateway commands ===:\n\n'
|
self.gate_reply_message(iq, '=== Available gateway commands ===:\n\n'
|
||||||
|
|
||||||
'!help - Displays this text\n'
|
'!help - Displays this text\n'
|
||||||
'!login +123456789 - Initiates Telegram session\n'
|
'!login +123456789 - Initiates Telegram session\n'
|
||||||
'!code 12345 - Entering one-time code during auth\n'
|
'!code 12345 - Entering one-time code during auth\n'
|
||||||
@@ -276,17 +282,22 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
#'!delete_session 123 - Delete session\n'
|
#'!delete_session 123 - Delete session\n'
|
||||||
'!logout - Deletes current Telegram session at gate\n'
|
'!logout - Deletes current Telegram session at gate\n'
|
||||||
'!reload_dialogs - Reloads dialogs list from Telegram\n\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'
|
'!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'
|
'!join - Join Telegram conference via invite link \n\n'
|
||||||
|
|
||||||
'!import phone firstname lastname - Add Telegram contact with phone number \n\n'
|
'!import phone firstname lastname - Add Telegram contact with phone number \n\n'
|
||||||
|
|
||||||
'!group GroupName @InviteContact - Create a normal group\n'
|
'!group GroupName @InviteContact - Create a normal group\n'
|
||||||
'!supergroup SupergroupName - Create a supergroup\n'
|
'!supergroup SupergroupName - Create a supergroup\n'
|
||||||
'!channel ChannelName - Create a channel\n\n'
|
'!channel ChannelName - Create a channel\n\n'
|
||||||
|
|
||||||
'!name first last - Change your name in Telegram\n'
|
'!name first last - Change your name in Telegram\n'
|
||||||
'!about text - Change about text in Telegram\n'
|
'!about text - Change about text in Telegram\n'
|
||||||
'!username - Changes your @username in Telegram\n'
|
'!username - Changes your @username in Telegram\n\n'
|
||||||
|
|
||||||
'!roster - Lists yout TG roster\n\n'
|
'!roster - Lists yout TG roster\n'
|
||||||
)
|
)
|
||||||
elif parsed[0] == '!configure':
|
elif parsed[0] == '!configure':
|
||||||
config_exclude = ['jid', 'tg_phone']
|
config_exclude = ['jid', 'tg_phone']
|
||||||
@@ -314,6 +325,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.tg_connections[jid].send_code_request(parsed[1])
|
self.tg_connections[jid].send_code_request(parsed[1])
|
||||||
self.gate_reply_message(iq, 'Gate is connected. Telegram should send SMS message to you.')
|
self.gate_reply_message(iq, 'Gate is connected. Telegram should send SMS message to you.')
|
||||||
self.gate_reply_message(iq, 'Please enter one-time code via !code 12345.')
|
self.gate_reply_message(iq, 'Please enter one-time code via !code 12345.')
|
||||||
|
|
||||||
elif parsed[0] in ['!code', '!password']: # --------------------------------------------------
|
elif parsed[0] in ['!code', '!password']: # --------------------------------------------------
|
||||||
if not self.tg_connections[jid].is_user_authorized():
|
if not self.tg_connections[jid].is_user_authorized():
|
||||||
if parsed[0] == '!code':
|
if parsed[0] == '!code':
|
||||||
@@ -340,6 +352,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.gate_reply_message(iq, 'Authentication failed.')
|
self.gate_reply_message(iq, 'Authentication failed.')
|
||||||
else:
|
else:
|
||||||
self.gate_reply_message(iq, 'You are already authenticated. Please use !logout before new login.')
|
self.gate_reply_message(iq, 'You are already authenticated. Please use !logout before new login.')
|
||||||
|
|
||||||
elif parsed[0] == '!list_sessions': # --------------------------------------------------
|
elif parsed[0] == '!list_sessions': # --------------------------------------------------
|
||||||
if not self.tg_connections[jid].is_user_authorized():
|
if not self.tg_connections[jid].is_user_authorized():
|
||||||
self.gate_reply_message(iq, 'Error.')
|
self.gate_reply_message(iq, 'Error.')
|
||||||
@@ -352,10 +365,12 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
return
|
return
|
||||||
self.tg_process_dialogs(jid)
|
self.tg_process_dialogs(jid)
|
||||||
self.gate_reply_message(iq, 'Dialogs reloaded.')
|
self.gate_reply_message(iq, 'Dialogs reloaded.')
|
||||||
|
|
||||||
elif parsed[0] == '!logout': # --------------------------------------------------
|
elif parsed[0] == '!logout': # --------------------------------------------------
|
||||||
self.tg_connections[jid].log_out()
|
self.tg_connections[jid].log_out()
|
||||||
self.db_connection.execute("DELETE FROM accounts WHERE jid = ?", (jid,))
|
self.db_connection.execute("DELETE FROM accounts WHERE jid = ?", (jid,))
|
||||||
self.gate_reply_message(iq, 'Your Telegram session was deleted')
|
self.gate_reply_message(iq, 'Your Telegram session was deleted')
|
||||||
|
|
||||||
elif parsed[0] == '!add': # add user
|
elif parsed[0] == '!add': # add user
|
||||||
result = self.tg_connections[jid].get_entity(parsed[1])
|
result = self.tg_connections[jid].get_entity(parsed[1])
|
||||||
if type(result) == User:
|
if type(result) == User:
|
||||||
@@ -387,30 +402,30 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.tg_connections[jid].invoke(CreateChatRequest([groupuser], groupname))
|
self.tg_connections[jid].invoke(CreateChatRequest([groupuser], groupname))
|
||||||
self.tg_process_dialogs(jid)
|
self.tg_process_dialogs(jid)
|
||||||
|
|
||||||
elif parsed[0] == '!channel' and len(parsed) >= 2: # create new channel
|
elif parsed[0] == '!channel' and len(parsed) >= 2:
|
||||||
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)
|
||||||
|
|
||||||
elif parsed[0] == '!supergroup' and len(parsed) >= 2: # create new channel
|
elif parsed[0] == '!supergroup' and len(parsed) >= 2:
|
||||||
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)
|
||||||
|
|
||||||
elif parsed[0] == '!username' and len(parsed) >= 2: # create new channel
|
elif parsed[0] == '!username' and len(parsed) >= 2:
|
||||||
username = parsed[1]
|
username = parsed[1]
|
||||||
self.tg_connections[jid].invoke(UpdateUsernameRequest(username))
|
self.tg_connections[jid].invoke(UpdateUsernameRequest(username))
|
||||||
|
|
||||||
elif parsed[0] == '!name' and len(parsed) >= 2: # create new channel
|
elif parsed[0] == '!name' and len(parsed) >= 2:
|
||||||
firstname = parsed[1]
|
firstname = parsed[1]
|
||||||
lastname = parsed[2] if len(parsed) > 2 else None
|
lastname = parsed[2] if len(parsed) > 2 else None
|
||||||
self.tg_connections[jid].invoke(UpdateProfileRequest(first_name = firstname, last_name = lastname))
|
self.tg_connections[jid].invoke(UpdateProfileRequest(first_name = firstname, last_name = lastname))
|
||||||
|
|
||||||
elif parsed[0] == '!about' and len(parsed) >= 2: # create new channel
|
elif parsed[0] == '!about' and len(parsed) >= 2:
|
||||||
about = iq['body'][7:]
|
about = iq['body'][7:]
|
||||||
self.tg_connections[jid].invoke(UpdateProfileRequest(about = about))
|
self.tg_connections[jid].invoke(UpdateProfileRequest(about = about))
|
||||||
|
|
||||||
elif parsed[0] == '!import' and len(parsed) >= 3: # create new channel
|
elif parsed[0] == '!import' and len(parsed) >= 3:
|
||||||
phone = parsed[1]
|
phone = parsed[1]
|
||||||
firstname = parsed[2]
|
firstname = parsed[2]
|
||||||
lastname = parsed[3] if len(parsed) > 3 else None
|
lastname = parsed[3] if len(parsed) > 3 else None
|
||||||
@@ -420,7 +435,10 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
self.tg_process_dialogs(jid)
|
self.tg_process_dialogs(jid)
|
||||||
|
|
||||||
elif parsed[0] == '!roster': # create new channel
|
elif parsed[0] == '!roster': # create new channel
|
||||||
self.gate_reply_message(iq, str(self.contact_list[jid]))
|
response = "Telegram chats:\n"
|
||||||
|
for jid,tid in self.contact_list[jid].items():
|
||||||
|
response += "{}: {}\n".format(tid, jid)
|
||||||
|
self.gate_reply_message(iq, response))
|
||||||
|
|
||||||
else: # --------------------------------------------------
|
else: # --------------------------------------------------
|
||||||
self.gate_reply_message(iq, 'Unknown command. Try !help for list all commands.')
|
self.gate_reply_message(iq, 'Unknown command. Try !help for list all commands.')
|
||||||
@@ -431,6 +449,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
|
|
||||||
if parsed[0] == '!help':
|
if parsed[0] == '!help':
|
||||||
self.gate_reply_message(iq, '=== Available dialog commands ===:\n\n'
|
self.gate_reply_message(iq, '=== Available dialog commands ===:\n\n'
|
||||||
|
|
||||||
'!help - Displays this text\n'
|
'!help - Displays this text\n'
|
||||||
'!s/find/replace - Edit last message. Use empty `find` to edit whole message and empty `replace` to delete it.\n'
|
'!s/find/replace - Edit last message. Use empty `find` to edit whole message and empty `replace` to delete it.\n'
|
||||||
'!block - Blacklists current user\n'
|
'!block - Blacklists current user\n'
|
||||||
|
|||||||
Reference in New Issue
Block a user