Added "/logout" command

This commit is contained in:
annelin 2019-04-06 10:46:29 +03:00
parent 08540206f0
commit 953b752eaf
2 changed files with 19 additions and 6 deletions

View file

@ -28,7 +28,7 @@ class TelegramClient
# we will check new messages in queue and auth data in forever loop # # we will check new messages in queue and auth data in forever loop #
begin begin
loop do while not @xmpp.online? === false do
self.process_outgoing_msg(@xmpp.message_queue.pop) unless @xmpp.message_queue.empty? # found something in message queue self.process_outgoing_msg(@xmpp.message_queue.pop) unless @xmpp.message_queue.empty? # found something in message queue
self.process_auth(:code, @xmpp.tg_auth_data[:code]) unless @xmpp.tg_auth_data[:code].nil? # found code in auth queue self.process_auth(:code, @xmpp.tg_auth_data[:code]) unless @xmpp.tg_auth_data[:code].nil? # found code in auth queue
self.process_auth(:password, @xmpp.tg_auth_data[:password]) unless @xmpp.tg_auth_data[:password].nil? # found 2fa password in auth queue self.process_auth(:password, @xmpp.tg_auth_data[:password]) unless @xmpp.tg_auth_data[:password].nil? # found 2fa password in auth queue
@ -65,6 +65,7 @@ class TelegramClient
when TD::Types::AuthorizationState::Ready when TD::Types::AuthorizationState::Ready
@logger.info 'Authorization successful!' @logger.info 'Authorization successful!'
@xmpp.send_message(nil, 'Authorization successful.') @xmpp.send_message(nil, 'Authorization successful.')
@xmpp.online!
end end
end end

View file

@ -33,7 +33,7 @@ class XMPPComponent
@logger.info 'New message from [%s] to [%s]' % [msg.from, msg.to] @logger.info 'New message from [%s] to [%s]' % [msg.from, msg.to]
return self.process_internal_command(msg.from.bare, msg.first_element_text('body') ) if msg.to == @@transport.jid # treat message as internal command if received as transport jid return self.process_internal_command(msg.from.bare, msg.first_element_text('body') ) if msg.to == @@transport.jid # treat message as internal command if received as transport jid
return @sessions[msg.from.bare].queue_message(msg.to.to_s, msg.first_element_text('body')) if @sessions.key? msg.from.bare # queue message for processing session is active for jid from return @sessions[msg.from.bare].queue_message(msg.to.to_s, msg.first_element_text('body')) if @sessions.key? msg.from.bare and @sessions[msg.from.bare].online? # queue message for processing session is active for jid from
end end
# process internal /command # # process internal /command #
@ -45,6 +45,9 @@ class XMPPComponent
when '/code', '/password' when '/code', '/password'
# we will pass auth data to user session if this session exists. # we will pass auth data to user session if this session exists.
@sessions[jfrom].enter_auth_data(body.split[0][1..8], body.split[1]) if @sessions.key? jfrom @sessions[jfrom].enter_auth_data(body.split[0][1..8], body.split[1]) if @sessions.key? jfrom
when '/logout'
# go offline
@sessions[jfrom].offline! if @sessions.key? jfrom
else # unknown command -- we will display sort of help message. else # unknown command -- we will display sort of help message.
reply = Jabber::Message.new; reply.from, reply.to, reply.body, reply.type = @@transport.jid, jfrom, ::HELP_MESSAGE, :chat reply = Jabber::Message.new; reply.from, reply.to, reply.body, reply.type = @@transport.jid, jfrom, ::HELP_MESSAGE, :chat
@@transport.send(reply) @@transport.send(reply)
@ -57,14 +60,14 @@ end
############################# #############################
class XMPPSession < XMPPComponent class XMPPSession < XMPPComponent
attr_accessor :user_jid, :tg_login, :tg_auth_data, :message_queue attr_accessor :user_jid, :tg_login, :tg_auth_data, :message_queue, :online
# start XMPP user session and Telegram client instance # # start XMPP user session and Telegram client instance #
def initialize(jid, tg_login) def initialize(jid, tg_login)
@logger = Logger.new(STDOUT); @logger.progname = '[XMPPSession: %s/%s]' % [jid, tg_login] @logger = Logger.new(STDOUT); @logger.progname = '[XMPPSession: %s/%s]' % [jid, tg_login]
@logger.info "Starting Telegram session" @logger.info "Starting Telegram session"
@user_jid, @tg_login, @tg_auth_data, @message_queue = jid, tg_login, {code: nil, password: nil}, Queue.new() @user_jid, @tg_login, @tg_auth_data, @message_queue = jid, tg_login, {code: nil, password: nil}, Queue.new()
@tg_client = Thread.new{ TelegramClient.new(self, tg_login) } @tg_client_thread = Thread.new{ TelegramClient.new(self, tg_login) }
end end
# send message to XMPP # # send message to XMPP #
@ -79,7 +82,6 @@ class XMPPSession < XMPPComponent
def queue_message(to, text = '') def queue_message(to, text = '')
@logger.info "Queuing message to be sent to Telegram network user -> " % to @logger.info "Queuing message to be sent to Telegram network user -> " % to
@message_queue << {to: to.split('@')[0], text: text} @message_queue << {to: to.split('@')[0], text: text}
puts @message_queue
end end
# enter auth data (we will share this data within :tg_auth_data to Telegram client thread ) # # enter auth data (we will share this data within :tg_auth_data to Telegram client thread ) #
@ -88,4 +90,14 @@ class XMPPSession < XMPPComponent
@tg_auth_data[typ.to_sym] = data @tg_auth_data[typ.to_sym] = data
end end
# session status #
def online?
@online
end
def online!
@online = true
end
def offline!
@online = false
end
end end