From e7610ef310e1f48081ed79eeb8c8f49cd71046fe Mon Sep 17 00:00:00 2001 From: annelin Date: Sat, 6 Apr 2019 11:23:14 +0300 Subject: [PATCH] Comments, comments.. --- inc/telegramclient.rb | 13 ++++++----- inc/xmppcomponent.rb | 53 ++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/inc/telegramclient.rb b/inc/telegramclient.rb index 4be6e29..9fb3378 100644 --- a/inc/telegramclient.rb +++ b/inc/telegramclient.rb @@ -16,17 +16,18 @@ class TelegramClient # instance initialization # def initialize(xmpp, login) - @xmpp = xmpp - @login = login - @logger = Logger.new(STDOUT); @logger.progname = '[TelegramClient: %s/%s]' % [@xmpp.user_jid, @login] + @logger = Logger.new(STDOUT); @logger.progname = '[TelegramClient: %s/%s]' % [xmpp.user_jid, login] # create logger + @xmpp = xmpp # our XMPP user session. we will send messages back to Jabber through this instance. + @login = login # store tg login + # spawn telegram client and specify callback handlers @logger.info 'Spawning Telegram client instance..' @client = TD::Client.new(database_directory: 'sessions/' + @login, files_directory: 'sessions/' + @login + '/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::NewMessage) do |update| self.message_handler(update) end # register new message update handler @client.connect # - # we will check new messages in queue and auth data in forever loop # + # we will check for outgoing messages in a queue and/or auth data from XMPP thread while XMPP indicates that service is online # begin while not @xmpp.online? === false do self.process_outgoing_msg(@xmpp.message_queue.pop) unless @xmpp.message_queue.empty? # found something in message queue @@ -72,8 +73,8 @@ class TelegramClient # message from telegram network handler # def message_handler(update) @logger.info 'Got NewMessage update' - from = update.message.chat_id - text = update.message.content.text.text + from = update.message.chat_id.to_s + text = update.message.content.text.text.to_s @xmpp.send_message(from, text) if not update.message.is_outgoing end diff --git a/inc/xmppcomponent.rb b/inc/xmppcomponent.rb index 2ecd862..b6d056b 100644 --- a/inc/xmppcomponent.rb +++ b/inc/xmppcomponent.rb @@ -1,12 +1,15 @@ require 'xmpp4r' +############################# ### Some constants ######### ::HELP_MESSAGE = "Unknown command. \n\n Please, use /login to try log in. ☺" +############################# ############################# ## XMPP Transport Class ##### ############################# class XMPPComponent + # init class and set logger # def initialize() @logger = Logger.new(STDOUT); @logger.progname = '[XMPPComponent]' end @@ -27,40 +30,40 @@ class XMPPComponent exit 1 end end - + + ############################# + #### Callback handlers ##### + ############################# + # new message to XMPP component # def message_handler(msg) @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 @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 + ############################# + #### Command handlers ##### + ############################# + # process internal /command # def process_internal_command(jfrom, body) case body.split[0] # /command argument = [command, argument] - when '/login' - # we will try to create new user session for JID and try to start telegram client for login - @sessions[jfrom] = XMPPSession.new(jfrom, body.split[1]) - when '/code', '/password' - # 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 - when '/logout' - # go offline - @sessions[jfrom].offline! if @sessions.key? jfrom - 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 - @@transport.send(reply) + when '/login' then @sessions[jfrom] = XMPPSession.new(jfrom, body.split[1]) # create new session for jid and spawn tg instance + when '/code', '/password' then @sessions[jfrom].enter_auth_data(body.split[0][1..8], body.split[1]) if @sessions.key? jfrom # pass auth data to telegram instance + when '/logout' then @sessions[jfrom].offline! if @sessions.key? jfrom # go offline + else reply = Jabber::Message.new; reply.from, reply.to, reply.body, reply.type = @@transport.jid, jfrom, ::HELP_MESSAGE, :chat; @@transport.send(reply) # unknown command -- we will display sort of help message. end end + end ############################# ## XMPP Session Class ####### ############################# - class XMPPSession < XMPPComponent - attr_accessor :user_jid, :tg_login, :tg_auth_data, :message_queue, :online + attr_reader :user_jid, :tg_login, :tg_auth_data, :message_queue + attr_accessor :online # start XMPP user session and Telegram client instance # def initialize(jid, tg_login) @@ -70,7 +73,9 @@ class XMPPSession < XMPPComponent @tg_client_thread = Thread.new{ TelegramClient.new(self, tg_login) } end - # send message to XMPP # + ########################################### + + # send message to current user via XMPP # def send_message(from = nil, body = '') @logger.info "Incoming message from Telegram network <- %s" % from.to_s from = from.nil? ? @@transport.jid : from.to_s+'@'+@@transport.jid.to_s @@ -90,14 +95,10 @@ class XMPPSession < XMPPComponent @tg_auth_data[typ.to_sym] = data end + ########################################### + # session status # - def online? - @online - end - def online! - @online = true - end - def offline! - @online = false - end + def online?() @online end + def online!() @online = true end + def offline!() @online = false end end