Version 2.2.8

[FIX] Better support on multiple Jabber resources
[FIX] Fixed unavailable presences from your contacts while shutdown process
[NEW] Config option `keeponline` to always keep Telegram session online (..and rely on XMPP offline messages)
This commit is contained in:
annelin 2020-03-09 09:04:24 +02:00
parent 5014133cc0
commit cb62c63771

View file

@ -8,8 +8,10 @@ HELP_GATE_CMD = %q{Available commands:
/setbio — update about /setbio — update about
/setpassword [old] [new] — set or remove password /setpassword [old] [new] — set or remove password
/config [param] [value] — view or update configuration options /config [param] [value] — view or update configuration options
Configuration options Configuration options
timezone 00:00 adjust timezone for Telegram user statuses timezone <timezone> adjust timezone for Telegram user statuses (example: +02:00)
keeponline <bool> always keep telegram session online and rely on jabber offline messages (example: true)
} }
HELP_CHAT_CMD= %q{Available commands: HELP_CHAT_CMD= %q{Available commands:
@ -52,13 +54,14 @@ class TelegramClient
@xmpp = xmpp @xmpp = xmpp
@jid = jid @jid = jid
@session = session @session = session
@resources = Set.new
@cache = {chats: {nil => []}, users: {}} @cache = {chats: {nil => []}, users: {}}
@resources = [] self.connect() if @session[:keeponline] == 'true'
end end
## connect telegram client ## connect telegram client
def connect(resource=nil) def connect(resource=nil)
return self.refresh(resource) if self.online? # already connected return self.roster(resource) if self.online? # already connected
@logger.warn 'Connecting to Telegram network..' @logger.warn 'Connecting to Telegram network..'
@telegram = TD::Client.new(database_directory: 'sessions/' + @jid, files_directory: 'sessions/' + @jid + '/files/') @telegram = TD::Client.new(database_directory: 'sessions/' + @jid, files_directory: 'sessions/' + @jid + '/files/')
@telegram.on(TD::Types::Update::AuthorizationState) do |u| @logger.debug(u); self.update_authorizationstate(u) end @telegram.on(TD::Types::Update::AuthorizationState) do |u| @logger.debug(u); self.update_authorizationstate(u) end
@ -69,25 +72,26 @@ class TelegramClient
@telegram.on(TD::Types::Update::MessageContent) do |u| @logger.debug(u); self.update_messagecontent(u) end @telegram.on(TD::Types::Update::MessageContent) do |u| @logger.debug(u); self.update_messagecontent(u) end
@telegram.on(TD::Types::Update::DeleteMessages) do |u| @logger.debug(u); self.update_deletemessages(u) end @telegram.on(TD::Types::Update::DeleteMessages) do |u| @logger.debug(u); self.update_deletemessages(u) end
@telegram.on(TD::Types::Update::File) do |u| @logger.debug(u); self.update_file(u) end @telegram.on(TD::Types::Update::File) do |u| @logger.debug(u); self.update_file(u) end
@telegram.connect() @telegram.connect().wait()
@resources << resource @resources << resource
end end
## disconnect telegram client ## disconnect telegram client
def disconnect(resource=nil) def disconnect(resource=nil, quit=false)
@resources.delete resource @resources.delete(resource)
return if @resources.count > 0 or not self.online? return unless (@resources.empty? && @session[:keeponline] != 'true') || quit
@logger.warn 'Disconnecting from Telegram network..' @logger.warn 'Disconnecting from Telegram network..'
@cache[:chats].each_key do |chat| @xmpp.send_presence(@jid, chat, :unavailable) end # we're offline (unsubscribe if logout) @telegram.dispose() if self.online?
@telegram.dispose()
@telegram = nil @telegram = nil
@cache[:chats].each_key do |chat| @xmpp.send_presence(@jid, chat, :unavailable) end
end end
## refresh roster ## resend statuses to (to another resource for example)
def refresh(resource=nil) def roster(resource=nil)
return if @resources.include? resource return if @resources.include? resource # we know it
@logger.warn 'Refreshing roster for resource %s' % resource @logger.warn 'Sending roster for %s' % resource
@cache[:chats].each_key do |chat| self.process_status_update(chat) if chat; end @cache[:chats].each_key do |chat| self.process_status_update(chat) end
@xmpp.send_presence(@jid, nil, nil, nil, "Logged in as: %s" % @session[:login])
@resources << resource @resources << resource
end end
@ -96,10 +100,6 @@ class TelegramClient
@telegram and @telegram.alive? @telegram and @telegram.alive?
end end
def authorized?
@telegram and @telegram.alive and @state == TD::Types::AuthorizationState::Ready
end
######################################################################### #########################################################################
# telegram updates handlers ############################################# # telegram updates handlers #############################################
@ -124,9 +124,9 @@ class TelegramClient
@logger.warn 'Authorization successful!' @logger.warn 'Authorization successful!'
@telegram.get_me.then{|me| @me = me}.wait @telegram.get_me.then{|me| @me = me}.wait
@telegram.get_chats(limit=999).wait @telegram.get_chats(limit=999).wait
@xmpp.send_presence(@jid, nil, nil, nil, "Logged in %s" % @session[:login])
@xmpp.send_presence(@jid, nil, :subscribe, nil, nil) @xmpp.send_presence(@jid, nil, :subscribe, nil, nil)
@xmpp.send_presence(@jid, nil, :subscribed, nil, nil) @xmpp.send_presence(@jid, nil, :subscribed, nil, nil)
@xmpp.send_presence(@jid, nil, nil, nil, "Logged in as: %s" % @session[:login])
end end
end end
@ -222,7 +222,7 @@ class TelegramClient
## get user and chat information from cache (or try to retrieve it, if missing) ## get user and chat information from cache (or try to retrieve it, if missing)
def get_contact(id) def get_contact(id)
return unless self.online? # we're offline. return unless self.online? && id # we're offline.
@telegram.search_public_chat(id).then{|chat| id = chat.id }.wait if id[0] == '@' @telegram.search_public_chat(id).then{|chat| id = chat.id }.wait if id[0] == '@'
@telegram.get_user(id).wait if not @cache[:users][id] and (id>0) @telegram.get_user(id).wait if not @cache[:users][id] and (id>0)
@telegram.get_chat(id).wait if not @cache[:chats][id] @telegram.get_chat(id).wait if not @cache[:chats][id]