Release v1.0-beta3
[FIX] Correct behaviour of /connect and /disconnect commands that also affects Jabber presences
This commit is contained in:
parent
993b62c1ce
commit
3545c692e1
|
@ -28,8 +28,8 @@ class TelegramClient
|
||||||
# instance initialization #
|
# instance initialization #
|
||||||
def initialize(xmpp, jid, login)
|
def initialize(xmpp, jid, login)
|
||||||
return if not @@loglevel # call .configure() first
|
return if not @@loglevel # call .configure() first
|
||||||
|
|
||||||
@logger = Logger.new(STDOUT); @logger.level = @@loglevel; @logger.progname = '[TelegramClient: %s/%s]' % [jid, login] # create logger
|
@logger = Logger.new(STDOUT); @logger.level = @@loglevel; @logger.progname = '[TelegramClient: %s/%s]' % [jid, login] # create logger
|
||||||
|
@logger.info 'Starting Telegram client..'
|
||||||
@xmpp = xmpp # XMPP stream
|
@xmpp = xmpp # XMPP stream
|
||||||
@jid = jid # user JID
|
@jid = jid # user JID
|
||||||
@timezone = '-00:00' # default timezone is UTC
|
@timezone = '-00:00' # default timezone is UTC
|
||||||
|
@ -39,9 +39,12 @@ class TelegramClient
|
||||||
@auth_state = 'nil' # too.
|
@auth_state = 'nil' # too.
|
||||||
@cache = {chats: {}, users: {}, users_fullinfo: {}, userpics: {}, unread_msg: {} } # cache storage
|
@cache = {chats: {}, users: {}, users_fullinfo: {}, userpics: {}, unread_msg: {} } # cache storage
|
||||||
@files_dir = File.dirname(__FILE__) + '/../sessions/' + @jid + '/files/'
|
@files_dir = File.dirname(__FILE__) + '/../sessions/' + @jid + '/files/'
|
||||||
|
end
|
||||||
|
|
||||||
# spawn telegram client and specify callback handlers
|
# initialize and connect telegram client #
|
||||||
@logger.info 'Starting Telegram client..'
|
def connect()
|
||||||
|
return if @client and @client.ready?
|
||||||
|
@logger.info 'Connecting to Telegram network..'
|
||||||
@client = TD::Client.new(database_directory: 'sessions/' + @jid, files_directory: 'sessions/' + @jid + '/files/') # create telegram client instance
|
@client = TD::Client.new(database_directory: 'sessions/' + @jid, files_directory: 'sessions/' + @jid + '/files/') # create telegram client instance
|
||||||
@client.on(TD::Types::Update::AuthorizationState) do |update| self.auth_handler(update) end # register auth update handler
|
@client.on(TD::Types::Update::AuthorizationState) do |update| self.auth_handler(update) end # register auth update handler
|
||||||
@client.on(TD::Types::Update::NewMessage) do |update| self.message_handler(update) end # register new message update handler
|
@client.on(TD::Types::Update::NewMessage) do |update| self.message_handler(update) end # register new message update handler
|
||||||
|
@ -51,15 +54,12 @@ class TelegramClient
|
||||||
@client.on(TD::Types::Update::NewChat) do |update| self.new_chat_handler(update) end # register new chat handler
|
@client.on(TD::Types::Update::NewChat) do |update| self.new_chat_handler(update) end # register new chat handler
|
||||||
@client.on(TD::Types::Update::User) do |update| self.user_handler(update) end # new user update?
|
@client.on(TD::Types::Update::User) do |update| self.user_handler(update) end # new user update?
|
||||||
@client.on(TD::Types::Update::UserStatus) do |update| self.status_update_handler(update) end # register status handler
|
@client.on(TD::Types::Update::UserStatus) do |update| self.status_update_handler(update) end # register status handler
|
||||||
|
@client.connect()
|
||||||
end
|
end
|
||||||
|
|
||||||
# connect/disconnect #
|
# disconnect and destroy telegram client #
|
||||||
def connect()
|
|
||||||
@logger.info 'Connecting to Telegram network..' if not @client.ready?
|
|
||||||
@client.connect() if not @client.ready?
|
|
||||||
end
|
|
||||||
|
|
||||||
def disconnect(logout = false)
|
def disconnect(logout = false)
|
||||||
|
return if not @client
|
||||||
@logger.info 'Disconnecting..'
|
@logger.info 'Disconnecting..'
|
||||||
@cache[:chats].each_key do |chat_id| @xmpp.presence(@jid, chat_id.to_s, :unavailable) end # send offline presences
|
@cache[:chats].each_key do |chat_id| @xmpp.presence(@jid, chat_id.to_s, :unavailable) end # send offline presences
|
||||||
(logout) ? @client.log_out : @client.dispose # logout if needed
|
(logout) ? @client.log_out : @client.dispose # logout if needed
|
||||||
|
|
|
@ -197,7 +197,7 @@ class XMPPComponent
|
||||||
def process_command(from, body)
|
def process_command(from, body)
|
||||||
case body.split[0] # /command argument = [command, argument]
|
case body.split[0] # /command argument = [command, argument]
|
||||||
when '/login' # create new session
|
when '/login' # create new session
|
||||||
@sessions[from.bare.to_s] = TelegramClient.new(self, from.bare.to_s, body.split[1]) if not @sessions.key? from.bare.to_s
|
@sessions[from.bare.to_s] = TelegramClient.new(self, from.bare.to_s, body.split[1]) if not (@sessions.key? from.bare.to_s and @sessions[from.bare.to_s].online?)
|
||||||
@sessions[from.bare.to_s].connect()
|
@sessions[from.bare.to_s].connect()
|
||||||
self.request_tz(from)
|
self.request_tz(from)
|
||||||
self.update_db(from.bare.to_s)
|
self.update_db(from.bare.to_s)
|
||||||
|
|
Reference in a new issue