whitespace cleanup

This commit is contained in:
2019-03-10 17:05:32 +01:00
parent cea496c808
commit 5f2e2e517d

View File

@@ -1,18 +1,18 @@
from telethon import TelegramClient from telethon import TelegramClient
from telethon.utils import get_extension from telethon.utils import get_extension
from telethon.tl.types import UpdateShortMessage, UpdateShortChatMessage, UpdateEditMessage, UpdateDeleteMessages, \ from telethon.tl.types import UpdateShortMessage, UpdateShortChatMessage, UpdateEditMessage, UpdateDeleteMessages, \
UpdateNewMessage, UpdateUserStatus, UpdateShort, Updates, UpdateNewChannelMessage,\ UpdateNewMessage, UpdateUserStatus, UpdateShort, Updates, UpdateNewChannelMessage, \
UpdateChannelTooLong, UpdateDeleteChannelMessages, UpdateEditChannelMessage,\ UpdateChannelTooLong, UpdateDeleteChannelMessages, UpdateEditChannelMessage, \
UpdateUserName UpdateUserName
from telethon.tl.types import InputPeerChat, InputPeerUser, InputPeerChannel, InputUser from telethon.tl.types import InputPeerChat, InputPeerUser, InputPeerChannel, InputUser
from telethon.tl.types import MessageMediaDocument, MessageMediaPhoto, MessageMediaUnsupported, MessageMediaContact,\ from telethon.tl.types import MessageMediaDocument, MessageMediaPhoto, MessageMediaUnsupported, MessageMediaContact, \
MessageMediaGeo, MessageMediaEmpty, MessageMediaVenue MessageMediaGeo, MessageMediaEmpty, MessageMediaVenue
from telethon.tl.types import DocumentAttributeAnimated, DocumentAttributeAudio, DocumentAttributeFilename,\ from telethon.tl.types import DocumentAttributeAnimated, DocumentAttributeAudio, DocumentAttributeFilename, \
DocumentAttributeSticker, DocumentAttributeVideo, DocumentAttributeHasStickers DocumentAttributeSticker, DocumentAttributeVideo, DocumentAttributeHasStickers
from telethon.tl.types import Message, MessageService, MessageActionChannelCreate, MessageActionChannelMigrateFrom,\ from telethon.tl.types import Message, MessageService, MessageActionChannelCreate, MessageActionChannelMigrateFrom, \
MessageActionChatCreate, MessageActionChatAddUser, MessageActionChatDeleteUser,\ MessageActionChatCreate, MessageActionChatAddUser, MessageActionChatDeleteUser, \
MessageActionChatEditTitle, MessageActionChatJoinedByLink, MessageActionChatMigrateTo,\ MessageActionChatEditTitle, MessageActionChatJoinedByLink, MessageActionChatMigrateTo, \
MessageActionPinMessage MessageActionPinMessage
from telethon.tl.types import UserStatusOnline, UserStatusOffline, UserStatusRecently from telethon.tl.types import UserStatusOnline, UserStatusOffline, UserStatusRecently
from telethon.tl.types import User, Chat, Channel from telethon.tl.types import User, Chat, Channel
from telethon.tl.types import PeerUser, PeerChat, PeerChannel from telethon.tl.types import PeerUser, PeerChat, PeerChannel
@@ -25,14 +25,16 @@ from telethon.tl.functions.contacts import ResolveUsernameRequest
import os, threading, queue, hashlib, time, datetime import os, threading, queue, hashlib, time, datetime
from xmpp_tg.utils import localtime, display_tg_name 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):
super().__init__(session, api_id, api_hash, proxy=proxy, update_workers = 4) super().__init__(session, api_id, api_hash, proxy=proxy, update_workers = 4)
self.me = None self.me = None
self.xmpp_gate = xmpp_gate self.xmpp_gate = xmpp_gate
@@ -41,7 +43,7 @@ class TelegramGateClient(TelegramClient):
self._media_queue = queue.Queue() self._media_queue = queue.Queue()
self._media_thread = threading.Thread(name='MediaDownloaderThread', target=self.media_thread_downloader) self._media_thread = threading.Thread(name='MediaDownloaderThread', target=self.media_thread_downloader)
self._status_updates = dict() self._status_updates = dict()
self._status_update_thread = threading.Thread(name = 'StatusUpdateThread', target = self.status_updater_thread) self._status_update_thread = threading.Thread(name = 'StatusUpdateThread', target = self.status_updater_thread)
@@ -49,12 +51,12 @@ class TelegramGateClient(TelegramClient):
self._message_cache_users = dict() self._message_cache_users = dict()
self._message_cache_groups = dict() self._message_cache_groups = dict()
self._message_cache_supergroups = dict() self._message_cache_supergroups = dict()
self._del_pts = 0
def xmpp_update_handler(self, obj): self._del_pts = 0
def xmpp_update_handler(self, update_obj):
""" """
Main function: Telegram update handler. Main function: Telegram update handler.
:param media: :param media:
@@ -65,127 +67,127 @@ class TelegramGateClient(TelegramClient):
# we have received some updates, so we're logined and can get <me> object and start mtd / upd threads # # we have received some updates, so we're logined and can get <me> object and start mtd / upd threads #
if not self.me: if not self.me:
me = self.get_me() me = self.get_me()
self.me = InputPeerUser(me.id, me.access_hash) self.me = InputPeerUser(me.id, me.access_hash)
self._media_thread.start() self._media_thread.start()
self._status_update_thread.start() self._status_update_thread.start()
nl = '\n' nl = '\n'
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(update_obj) if update_obj.fwd_from else '' # process forward messages
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(update_obj.user_id) # get peer information
self.invoke(ReadHistoryRequest(InputPeerUser(usr.id, usr.access_hash), update_obj.id)) # delivery report
fwd_from = self._process_forward_msg(obj) if 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) )
usr = self._get_user_information(obj.user_id) # get peer information
self.invoke(ReadHistoryRequest( InputPeerUser(usr.id, usr.access_hash), 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
msg = ''
fwd_from = ''
mid = obj.message.id
cid = None
# detect message type msg = ''
is_user = type(obj.message.to_id) is PeerUser fwd_from = ''
is_group = type(obj.message.to_id) is PeerChat mid = update_obj.message.id
is_supergroup = type(obj.message.to_id) is PeerChannel
# detect from id
if is_user:
cid = obj.message.from_id
user = self._get_user_information(cid)
peer = InputPeerUser(user.id, user.access_hash)
prefix = 'u'
prefix = 'b' if user.bot else prefix
elif is_group:
cid = obj.message.to_id.chat_id
peer = InputPeerChat(cid)
prefix = 'g'
elif is_supergroup:
cid = 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
prefix = 's'
# our message #
if type(obj.message) == MessageService:
obj.message.fwd_from, obj.message.post, obj.message.edit_date, obj.message.media = None, None, None, None
msg = self._process_info_msg(obj.message, peer)
elif type(obj.message) == Message:
msg = obj.message.message
# is forwarded?
if obj.message.fwd_from:
fwd_from = self._process_forward_msg(obj.message)
# maybe its channel? # # detect message type
if obj.message.post: is_user = type(update_obj.message.to_id) is PeerUser
prefix = 'c' is_group = type(update_obj.message.to_id) is PeerChat
is_supergroup = type(update_obj.message.to_id) is PeerChannel
# get sender information from chat info #
if not is_user and not obj.message.post:
usr = self._get_user_information(obj.message.from_id)
nickname = display_tg_name(usr)
msg = '[User: {}] {}'.format(nickname, msg)
# detect from id
# message media # if is_user:
if obj.message.media: cid = update_obj.message.from_id
msg = '{} {}'.format( msg, self._process_media_msg(obj.message.media) ) user = self._get_user_information(cid)
peer = InputPeerUser(user.id, user.access_hash)
# edited # prefix = 'u'
if obj.message.edit_date: prefix = 'b' if user.bot else prefix
msg = '[Edited] {}'.format(msg) elif is_group:
cid = update_obj.message.to_id.chat_id
# send message # peer = InputPeerChat(cid)
self.gate_send_message(prefix + str(cid), mbody = '[MSG {}] {}{}'.format(mid, fwd_from, msg) ) prefix = 'g'
elif is_supergroup:
# delivery report cid = update_obj.message.to_id.channel_id
if is_supergroup: 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
self.invoke(ReadHistoryChannel(peer, mid)) prefix = 's'
else:
self.invoke(ReadHistoryRequest(peer, mid)) # our message #
if type(update_obj.message) == MessageService:
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(update_obj.message, peer)
elif type(update_obj.message) == Message:
msg = update_obj.message.message
# is forwarded?
if update_obj.message.fwd_from:
fwd_from = self._process_forward_msg(update_obj.message)
# maybe its channel? #
if update_obj.message.post:
prefix = 'c'
# get sender information from chat info #
if not is_user and not update_obj.message.post:
usr = self._get_user_information(update_obj.message.from_id)
nickname = display_tg_name(usr)
msg = '[User: {}] {}'.format(nickname, msg)
# message media #
if update_obj.message.media:
msg = '{} {}'.format(msg, self._process_media_msg(update_obj.message.media))
# edited #
if update_obj.message.edit_date:
msg = '[Edited] {}'.format(msg)
# send message #
self.xmpp_send_message(prefix + str(cid), mbody ='[MSG {}] {}{}'.format(mid, fwd_from, msg))
# delivery report
if is_supergroup:
self.invoke(ReadHistoryChannel(peer, mid))
else:
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
except Exception: except Exception:
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 )
self.xmpp_gate.send_message( mto=self.jid, mfrom=mfrom + '@' + self.xmpp_gate.config['jid'], mtype='chat', mbody=mbody) self.xmpp_gate.send_message( mto=self.jid, mfrom=mfrom + '@' + self.xmpp_gate.config['jid'], mtype='chat', mbody=mbody)
@@ -227,19 +229,19 @@ class TelegramGateClient(TelegramClient):
if type(attrib) == match: if type(attrib) == match:
return attrib return attrib
return None return None
def _get_user_information(self, uid): def _get_user_information(self, uid):
if uid in self.xmpp_gate.tg_dialogs[self.jid]['users']: if uid in self.xmpp_gate.tg_dialogs[self.jid]['users']:
return self.xmpp_gate.tg_dialogs[self.jid]['users'][uid] return self.xmpp_gate.tg_dialogs[self.jid]['users'][uid]
entity = self.get_entity(uid) entity = self.get_entity(uid)
if entity.access_hash: if entity.access_hash:
self.xmpp_gate.tg_dialogs[self.jid]['users'][uid] = entity self.xmpp_gate.tg_dialogs[self.jid]['users'][uid] = entity
return entity return entity
else: else:
return {'first_name': 'Unknown', 'last_name': 'user', 'access_hash': -1, 'id': 0, 'bot': False} return {'first_name': 'Unknown', 'last_name': 'user', 'access_hash': -1, 'id': 0, 'bot': False}
def _process_forward_msg(self, message): def _process_forward_msg(self, message):
""" """
@@ -250,7 +252,7 @@ class TelegramGateClient(TelegramClient):
:return: :return:
""" """
if message.fwd_from.from_id: # from user if message.fwd_from.from_id: # from user
usr = self._get_user_information(message.fwd_from.from_id) usr = self._get_user_information(message.fwd_from.from_id)
fwd_from = display_tg_name(usr) fwd_from = display_tg_name(usr)
@@ -271,14 +273,14 @@ class TelegramGateClient(TelegramClient):
if type(media) is MessageMediaDocument: # document if type(media) is MessageMediaDocument: # document
attributes = media.document.attributes attributes = media.document.attributes
attributes_types = [type(a) for a in attributes] attributes_types = [type(a) for a in attributes]
size_text = '|Size: {:.2f} Mb'.format(media.document.size / 1024 / 1024) size_text = '|Size: {:.2f} Mb'.format(media.document.size / 1024 / 1024)
if media.document.size > self.xmpp_gate.config['media_max_download_size']: # too big file if media.document.size > self.xmpp_gate.config['media_max_download_size']: # too big file
g_link = {'link': 'File is too big to be downloaded via this gateway. Sorry.'} g_link = {'link': 'File is too big to be downloaded via this gateway. Sorry.'}
else: # add it to download queue if everything is ok else: # add it to download queue if everything is ok
g_link = self.generate_media_link(media) g_link = self.generate_media_link(media)
self._media_queue.put({'media': media, 'file': g_link['name']}) self._media_queue.put({'media': media, 'file': g_link['name']})
attr_fn = self.get_document_attribute(attributes, DocumentAttributeFilename) attr_fn = self.get_document_attribute(attributes, DocumentAttributeFilename)
@@ -289,12 +291,12 @@ class TelegramGateClient(TelegramClient):
if DocumentAttributeSticker in attributes_types: # sticker if DocumentAttributeSticker in attributes_types: # sticker
smile = self.get_document_attribute(attributes, DocumentAttributeSticker).alt smile = self.get_document_attribute(attributes, DocumentAttributeSticker).alt
msg = '[Sticker {}] {}'.format(smile, g_link['link']) msg = '[Sticker {}] {}'.format(smile, g_link['link'])
elif DocumentAttributeAudio in attributes_types: # audio file elif DocumentAttributeAudio in attributes_types: # audio file
attr_a = self.get_document_attribute(attributes, DocumentAttributeAudio) attr_a = self.get_document_attribute(attributes, DocumentAttributeAudio)
if attr_a.voice: # voicemessage if attr_a.voice: # voicemessage
msg = '[VoiceMessage|{} sec] {}'.format(attr_a.duration, g_link['link']) msg = '[VoiceMessage|{} sec] {}'.format(attr_a.duration, g_link['link'])
else: # other audio else: # other audio
attr_f = self.get_document_attribute(attributes, DocumentAttributeFilename) attr_f = self.get_document_attribute(attributes, DocumentAttributeFilename)
msg = '[Audio|File:{}{}|Performer:{}|Title:{}|Duration:{} sec] {}' \ msg = '[Audio|File:{}{}|Performer:{}|Title:{}|Duration:{} sec] {}' \
@@ -326,7 +328,7 @@ class TelegramGateClient(TelegramClient):
msg = '{} {}'.format(media.caption, msg) msg = '{} {}'.format(media.caption, msg)
elif type(media) is MessageMediaContact: # contact elif type(media) is MessageMediaContact: # contact
msg = 'First name: {} / Last name: {} / Phone: {}'\ msg = 'First name: {} / Last name: {} / Phone: {}' \
.format(media.first_name, media.last_name, media.phone_number) .format(media.first_name, media.last_name, media.phone_number)
elif type(media) in [MessageMediaGeo, MessageMediaVenue]: # address elif type(media) in [MessageMediaGeo, MessageMediaVenue]: # address
map_link_template = 'https://maps.google.com/maps?q={0:.4f},{1:.4f}&ll={0:.4f},{1:.4f}&z=16' map_link_template = 'https://maps.google.com/maps?q={0:.4f},{1:.4f}&ll={0:.4f},{1:.4f}&z=16'
@@ -345,7 +347,7 @@ class TelegramGateClient(TelegramClient):
:param users: :param users:
:return: :return:
""" """
msg = '' msg = ''
usr = self._get_user_information(message.from_id) usr = self._get_user_information(message.from_id)
nickname = display_tg_name(usr) nickname = display_tg_name(usr)
@@ -362,13 +364,13 @@ class TelegramGateClient(TelegramClient):
elif type(message.action) is MessageActionChatAddUser: elif type(message.action) is MessageActionChatAddUser:
added_users = [] added_users = []
for user_id in message.action.users: for user_id in message.action.users:
usr = self._get_user_information(user_id) usr = self._get_user_information(user_id)
added_users.append(display_tg_name(usr)) added_users.append(display_tg_name(usr))
msg = 'User [{}] has just invited [{}]'.format(nickname, ','.join(added_users))
# user exit # msg = 'User [{}] has just invited [{}]'.format(nickname, ','.join(added_users))
elif type(message.action) is MessageActionChatDeleteUser:
# user exit #
elif type(message.action) is MessageActionChatDeleteUser:
usr = self._get_user_information(message.action.user_id) usr = self._get_user_information(message.action.user_id)
msg = 'User [{}] has just left the room'.format(display_tg_name(usr)) msg = 'User [{}] has just left the room'.format(display_tg_name(usr))
@@ -416,9 +418,9 @@ class TelegramGateClient(TelegramClient):
print('MTD ::: Media downloaded') print('MTD ::: Media downloaded')
except Exception: except Exception:
print(traceback.format_exc()) print(traceback.format_exc())
def status_updater_thread(self): def status_updater_thread(self):
while True: while True:
try: try:
if len(self._status_updates) > 0: if len(self._status_updates) > 0: