restructure

This commit is contained in:
2019-02-27 03:12:55 +01:00
parent 282ced3fe6
commit 7d2ba56450

View File

@@ -4,6 +4,7 @@ import logging, traceback, pprint
from sleekxmpp.componentxmpp import ComponentXMPP from sleekxmpp.componentxmpp import ComponentXMPP
from sleekxmpp import Presence, Message from sleekxmpp import Presence, Message
from sleekxmpp.exceptions import XMPPError
from telethon.tl.functions.messages import GetDialogsRequest, SendMessageRequest, SendMediaRequest, EditMessageRequest, DeleteMessagesRequest, ImportChatInviteRequest, GetFullChatRequest, AddChatUserRequest, DeleteChatUserRequest, CreateChatRequest, DeleteHistoryRequest from telethon.tl.functions.messages import GetDialogsRequest, SendMessageRequest, SendMediaRequest, EditMessageRequest, DeleteMessagesRequest, ImportChatInviteRequest, GetFullChatRequest, AddChatUserRequest, DeleteChatUserRequest, CreateChatRequest, DeleteHistoryRequest
from telethon.tl.functions.account import UpdateStatusRequest, GetAuthorizationsRequest, UpdateProfileRequest, UpdateUsernameRequest from telethon.tl.functions.account import UpdateStatusRequest, GetAuthorizationsRequest, UpdateProfileRequest, UpdateUsernameRequest
@@ -125,54 +126,57 @@ class XMPPTelegram(ComponentXMPP):
self.gate_reply_message(iq, 'Error.') self.gate_reply_message(iq, 'Error.')
else: # -- normal message -- else: # -- normal message --
try: self.send_tg_message(iq)
tg_id = int(iq['to'].node[1:])
except ValueError:
self.gate_reply_message(iq, 'Invalid JID')
tg_peer = None
msg = iq['body']
reply_mid = None
if msg.startswith('>'): # quoting check def send_tg_message(self, iq):
msg_lines = msg.split('\n') jid = iq['from'].bare
matched = re.match(r'>[ ]*(?P<mid>[\d]+)[ ]*', msg_lines[0]) #TODO: check regex try:
matched = matched.groupdict() if matched else {} tg_id = int(iq['to'].node[1:])
except ValueError:
raise XMPPError(text="Invalid Telegram-ID")
tg_peer = None
msg = iq['body']
reply_mid = None
if 'mid' in matched: # citation if msg.startswith('>'): # quoting check
reply_mid = int(matched['mid']) msg_lines = msg.split('\n')
msg = '\n'.join(msg_lines[1:]) matched = re.match(r'>[ ]*(?P<mid>[\d]+)[ ]*', msg_lines[0]) #TODO: check regex
matched = matched.groupdict() if matched else {}
if iq['to'].bare.startswith( ('u', 'b') ): # normal user if 'mid' in matched: # citation
tg_peer = InputPeerUser(tg_id, self.tg_dialogs[jid]['users'][tg_id].access_hash) reply_mid = int(matched['mid'])
elif iq['to'].bare.startswith('g'): # generic group msg = '\n'.join(msg_lines[1:])
tg_peer = InputPeerChat(tg_id)
elif iq['to'].bare.startswith( ('s', 'c') ): # supergroup
tg_peer = InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash)
# peer OK. if iq['to'].bare.startswith( ('u', 'b') ): # normal user
if tg_peer: tg_peer = InputPeerUser(tg_id, self.tg_dialogs[jid]['users'][tg_id].access_hash)
result = None elif iq['to'].bare.startswith('g'): # generic group
tg_peer = InputPeerChat(tg_id)
elif iq['to'].bare.startswith( ('s', 'c') ): # supergroup
tg_peer = InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash)
# detect media # peer OK.
if msg.startswith('http') and re.match(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg): if tg_peer:
urls = re.findall(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg) result = None
message = msg.replace(urls[0], '')
media = InputMediaPhotoExternal(urls[0], "Image")
try:
result = self.tg_connections[jid].invoke(SendMediaRequest(tg_peer, media, message, random_id = generate_random_long(), reply_to_msg_id = reply_mid))
except Exception:
print('Media upload failed.')
# media send failed. # # detect media
if not result: if msg.startswith('http') and re.match(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg):
result = self.tg_connections[jid].invoke(SendMessageRequest(tg_peer, msg, generate_random_long(), reply_to_msg_id=reply_mid)) urls = re.findall(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg)
message = msg.replace(urls[0], '')
media = InputMediaPhotoExternal(urls[0], "Image")
try:
result = self.tg_connections[jid].invoke(SendMediaRequest(tg_peer, media, message, random_id = generate_random_long(), reply_to_msg_id = reply_mid))
except Exception:
print('Media upload failed.')
# find sent message id and save it # media send failed. #
if result and hasattr(result, 'id'): # update id if not result:
msg_id = result.id result = self.tg_connections[jid].invoke(SendMessageRequest(tg_peer, msg, generate_random_long(), reply_to_msg_id=reply_mid))
self.tg_dialogs[jid]['messages'][tg_id] = {'id': msg_id, 'body': msg}
#self.send_message(mto=iq['from'], mfrom=iq['to'], mtype='chat', mbody='[Your MID:{}]'.format(msg_id))
# find sent message id and save it
if result and hasattr(result, 'id'): # update id
msg_id = result.id
self.tg_dialogs[jid]['messages'][tg_id] = {'id': msg_id, 'body': msg}
#self.send_message(mto=iq['from'], mfrom=iq['to'], mtype='chat', mbody='[Your MID:{}]'.format(msg_id))
def event_presence_unsub(self, presence): def event_presence_unsub(self, presence):
return return