whitespace cleanup
This commit is contained in:
@@ -28,6 +28,8 @@ from xmpp_tg.utils import localtime, display_tg_name
|
|||||||
import xmpp_tg.monkey
|
import xmpp_tg.monkey
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
# modified by adnidor
|
||||||
|
|
||||||
|
|
||||||
class TelegramGateClient(TelegramClient):
|
class TelegramGateClient(TelegramClient):
|
||||||
def __init__(self, session, api_id, api_hash, xmpp_gate, jid, phone, proxy=None):
|
def __init__(self, session, api_id, api_hash, xmpp_gate, jid, phone, proxy=None):
|
||||||
@@ -53,7 +55,7 @@ class TelegramGateClient(TelegramClient):
|
|||||||
self._del_pts = 0
|
self._del_pts = 0
|
||||||
|
|
||||||
|
|
||||||
def xmpp_update_handler(self, obj):
|
def xmpp_update_handler(self, update_obj):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Main function: Telegram update handler.
|
Main function: Telegram update handler.
|
||||||
@@ -75,88 +77,88 @@ class TelegramGateClient(TelegramClient):
|
|||||||
try:
|
try:
|
||||||
|
|
||||||
# message from normal chat #
|
# message from normal chat #
|
||||||
if type(obj) in [UpdateShortMessage] and not obj.out:
|
if type(update_obj) in [UpdateShortMessage] and not update_obj.out:
|
||||||
|
|
||||||
fwd_from = self._process_forward_msg(obj) if obj.fwd_from else '' # process forward messages
|
fwd_from = self._process_forward_msg(update_obj) if update_obj.fwd_from else '' # process forward messages
|
||||||
self.gate_send_message( mfrom='u' + str(obj.user_id), mbody = '[MSG {}] {}{}'.format(obj.id, fwd_from, obj.message) )
|
self.xmpp_send_message(mfrom='u' + str(update_obj.user_id), mbody ='[MSG {}] {}{}'.format(update_obj.id, fwd_from, update_obj.message))
|
||||||
usr = self._get_user_information(obj.user_id) # get peer information
|
usr = self._get_user_information(update_obj.user_id) # get peer information
|
||||||
self.invoke(ReadHistoryRequest( InputPeerUser(usr.id, usr.access_hash), obj.id )) # delivery report
|
self.invoke(ReadHistoryRequest(InputPeerUser(usr.id, usr.access_hash), update_obj.id)) # delivery report
|
||||||
|
|
||||||
# message from normal group #
|
# message from normal group #
|
||||||
if type(obj) in [UpdateShortChatMessage] and not obj.out:
|
if type(update_obj) in [UpdateShortChatMessage] and not update_obj.out:
|
||||||
fwd_from = self._process_forward_msg(obj) if obj.fwd_from else '' # process forward messages
|
fwd_from = self._process_forward_msg(update_obj) if update_obj.fwd_from else '' # process forward messages
|
||||||
usr = self._get_user_information(obj.from_id)
|
usr = self._get_user_information(update_obj.from_id)
|
||||||
nickname = display_tg_name(usr)
|
nickname = display_tg_name(usr)
|
||||||
|
|
||||||
# send message
|
# send message
|
||||||
self.gate_send_message(mfrom='g' + str(obj.chat_id), mbody ='[MSG {}] [User: {}] {}{}'.format(obj.id, nickname, fwd_from, obj.message) )
|
self.xmpp_send_message(mfrom='g' + str(update_obj.chat_id), mbody ='[MSG {}] [User: {}] {}{}'.format(update_obj.id, nickname, fwd_from, update_obj.message))
|
||||||
self.invoke(ReadHistoryRequest(InputPeerChat(obj.chat_id), obj.id))
|
self.invoke(ReadHistoryRequest(InputPeerChat(update_obj.chat_id), update_obj.id))
|
||||||
|
|
||||||
|
|
||||||
# message from supergroup or media message #
|
# message from supergroup or media message #
|
||||||
if type(obj) in [UpdateNewMessage, UpdateNewChannelMessage, UpdateEditMessage, UpdateEditChannelMessage] and not obj.message.out:
|
if type(update_obj) in [UpdateNewMessage, UpdateNewChannelMessage, UpdateEditMessage, UpdateEditChannelMessage] and not update_obj.message.out:
|
||||||
|
|
||||||
cid = None
|
cid = None
|
||||||
msg = ''
|
msg = ''
|
||||||
fwd_from = ''
|
fwd_from = ''
|
||||||
mid = obj.message.id
|
mid = update_obj.message.id
|
||||||
|
|
||||||
|
|
||||||
# detect message type
|
# detect message type
|
||||||
is_user = type(obj.message.to_id) is PeerUser
|
is_user = type(update_obj.message.to_id) is PeerUser
|
||||||
is_group = type(obj.message.to_id) is PeerChat
|
is_group = type(update_obj.message.to_id) is PeerChat
|
||||||
is_supergroup = type(obj.message.to_id) is PeerChannel
|
is_supergroup = type(update_obj.message.to_id) is PeerChannel
|
||||||
|
|
||||||
|
|
||||||
# detect from id
|
# detect from id
|
||||||
if is_user:
|
if is_user:
|
||||||
cid = obj.message.from_id
|
cid = update_obj.message.from_id
|
||||||
user = self._get_user_information(cid)
|
user = self._get_user_information(cid)
|
||||||
peer = InputPeerUser(user.id, user.access_hash)
|
peer = InputPeerUser(user.id, user.access_hash)
|
||||||
prefix = 'u'
|
prefix = 'u'
|
||||||
prefix = 'b' if user.bot else prefix
|
prefix = 'b' if user.bot else prefix
|
||||||
elif is_group:
|
elif is_group:
|
||||||
cid = obj.message.to_id.chat_id
|
cid = update_obj.message.to_id.chat_id
|
||||||
peer = InputPeerChat(cid)
|
peer = InputPeerChat(cid)
|
||||||
prefix = 'g'
|
prefix = 'g'
|
||||||
elif is_supergroup:
|
elif is_supergroup:
|
||||||
cid = obj.message.to_id.channel_id
|
cid = update_obj.message.to_id.channel_id
|
||||||
peer = InputPeerChannel(cid, self.xmpp_gate.tg_dialogs[self.jid]['supergroups'][cid].access_hash) if cid in self.xmpp_gate.tg_dialogs[self.jid]['supergroups'] else None
|
peer = InputPeerChannel(cid, self.xmpp_gate.tg_dialogs[self.jid]['supergroups'][cid].access_hash) if cid in self.xmpp_gate.tg_dialogs[self.jid]['supergroups'] else None
|
||||||
prefix = 's'
|
prefix = 's'
|
||||||
|
|
||||||
# our message #
|
# our message #
|
||||||
if type(obj.message) == MessageService:
|
if type(update_obj.message) == MessageService:
|
||||||
obj.message.fwd_from, obj.message.post, obj.message.edit_date, obj.message.media = None, None, None, None
|
update_obj.message.fwd_from, update_obj.message.post, update_obj.message.edit_date, update_obj.message.media = None, None, None, None
|
||||||
msg = self._process_info_msg(obj.message, peer)
|
msg = self._process_info_msg(update_obj.message, peer)
|
||||||
elif type(obj.message) == Message:
|
elif type(update_obj.message) == Message:
|
||||||
msg = obj.message.message
|
msg = update_obj.message.message
|
||||||
|
|
||||||
|
|
||||||
# is forwarded?
|
# is forwarded?
|
||||||
if obj.message.fwd_from:
|
if update_obj.message.fwd_from:
|
||||||
fwd_from = self._process_forward_msg(obj.message)
|
fwd_from = self._process_forward_msg(update_obj.message)
|
||||||
|
|
||||||
# maybe its channel? #
|
# maybe its channel? #
|
||||||
if obj.message.post:
|
if update_obj.message.post:
|
||||||
prefix = 'c'
|
prefix = 'c'
|
||||||
|
|
||||||
# get sender information from chat info #
|
# get sender information from chat info #
|
||||||
if not is_user and not obj.message.post:
|
if not is_user and not update_obj.message.post:
|
||||||
usr = self._get_user_information(obj.message.from_id)
|
usr = self._get_user_information(update_obj.message.from_id)
|
||||||
nickname = display_tg_name(usr)
|
nickname = display_tg_name(usr)
|
||||||
msg = '[User: {}] {}'.format(nickname, msg)
|
msg = '[User: {}] {}'.format(nickname, msg)
|
||||||
|
|
||||||
|
|
||||||
# message media #
|
# message media #
|
||||||
if obj.message.media:
|
if update_obj.message.media:
|
||||||
msg = '{} {}'.format( msg, self._process_media_msg(obj.message.media) )
|
msg = '{} {}'.format(msg, self._process_media_msg(update_obj.message.media))
|
||||||
|
|
||||||
# edited #
|
# edited #
|
||||||
if obj.message.edit_date:
|
if update_obj.message.edit_date:
|
||||||
msg = '[Edited] {}'.format(msg)
|
msg = '[Edited] {}'.format(msg)
|
||||||
|
|
||||||
# send message #
|
# send message #
|
||||||
self.gate_send_message(prefix + str(cid), mbody = '[MSG {}] {}{}'.format(mid, fwd_from, msg) )
|
self.xmpp_send_message(prefix + str(cid), mbody ='[MSG {}] {}{}'.format(mid, fwd_from, msg))
|
||||||
|
|
||||||
# delivery report
|
# delivery report
|
||||||
if is_supergroup:
|
if is_supergroup:
|
||||||
@@ -165,15 +167,15 @@ class TelegramGateClient(TelegramClient):
|
|||||||
self.invoke(ReadHistoryRequest(peer, mid))
|
self.invoke(ReadHistoryRequest(peer, mid))
|
||||||
|
|
||||||
# Status Updates #
|
# Status Updates #
|
||||||
if type(obj) is UpdateUserStatus:
|
if type(update_obj) is UpdateUserStatus:
|
||||||
# process status update #
|
# process status update #
|
||||||
if type(obj.status) is UserStatusOnline:
|
if type(update_obj.status) is UserStatusOnline:
|
||||||
self._status_updates[str(obj.user_id)] = { 'status': None, 'message': 'Online' }
|
self._status_updates[str(update_obj.user_id)] = {'status': None, 'message': 'Online'}
|
||||||
elif type(obj.status) is UserStatusOffline:
|
elif type(update_obj.status) is UserStatusOffline:
|
||||||
status = 'away' if datetime.datetime.utcnow() - obj.status.was_online < datetime.timedelta(hours = self.xmpp_gate.accounts[self.jid]['status_xa_interval'] ) else 'xa'
|
status = 'away' if datetime.datetime.utcnow() - update_obj.status.was_online < datetime.timedelta(hours = self.xmpp_gate.accounts[self.jid]['status_xa_interval']) else 'xa'
|
||||||
self._status_updates[str(obj.user_id)] = { 'status': status, 'message': localtime(obj.status.was_online).strftime('Last seen at %H:%M %d/%m/%Y') }
|
self._status_updates[str(update_obj.user_id)] = {'status': status, 'message': localtime(update_obj.status.was_online).strftime('Last seen at %H:%M %d/%m/%Y')}
|
||||||
elif type(obj.status) is UserStatusRecently:
|
elif type(update_obj.status) is UserStatusRecently:
|
||||||
self._status_updates[str(obj.user_id)] = { 'status': 'dnd', 'message': 'Last seen recently' }
|
self._status_updates[str(update_obj.user_id)] = {'status': 'dnd', 'message': 'Last seen recently'}
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -182,7 +184,7 @@ class TelegramGateClient(TelegramClient):
|
|||||||
print('Exception occurs!')
|
print('Exception occurs!')
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
||||||
def gate_send_message(self, mfrom, mbody):
|
def xmpp_send_message(self, mfrom, mbody):
|
||||||
tg_from = int(mfrom[1:])
|
tg_from = int(mfrom[1:])
|
||||||
if not tg_from in self.xmpp_gate.tg_dialogs[self.jid]['users'] and not tg_from in self.xmpp_gate.tg_dialogs[self.jid]['groups'] and not tg_from in self.xmpp_gate.tg_dialogs[self.jid]['supergroups']: # new contact appeared
|
if not tg_from in self.xmpp_gate.tg_dialogs[self.jid]['users'] and not tg_from in self.xmpp_gate.tg_dialogs[self.jid]['groups'] and not tg_from in self.xmpp_gate.tg_dialogs[self.jid]['supergroups']: # new contact appeared
|
||||||
self.xmpp_gate.tg_process_dialogs( self.jid )
|
self.xmpp_gate.tg_process_dialogs( self.jid )
|
||||||
|
|||||||
Reference in New Issue
Block a user