Comments, comments..
This commit is contained in:
parent
953b752eaf
commit
e7610ef310
|
@ -16,17 +16,18 @@ class TelegramClient
|
||||||
# instance initialization #
|
# instance initialization #
|
||||||
def initialize(xmpp, login)
|
def initialize(xmpp, login)
|
||||||
|
|
||||||
@xmpp = xmpp
|
@logger = Logger.new(STDOUT); @logger.progname = '[TelegramClient: %s/%s]' % [xmpp.user_jid, login] # create logger
|
||||||
@login = login
|
@xmpp = xmpp # our XMPP user session. we will send messages back to Jabber through this instance.
|
||||||
@logger = Logger.new(STDOUT); @logger.progname = '[TelegramClient: %s/%s]' % [@xmpp.user_jid, @login]
|
@login = login # store tg login
|
||||||
|
|
||||||
|
# spawn telegram client and specify callback handlers
|
||||||
@logger.info 'Spawning Telegram client instance..'
|
@logger.info 'Spawning Telegram client instance..'
|
||||||
@client = TD::Client.new(database_directory: 'sessions/' + @login, files_directory: 'sessions/' + @login + '/files/') # create 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::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
|
||||||
@client.connect #
|
@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
|
begin
|
||||||
while not @xmpp.online? === false 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
|
||||||
|
@ -72,8 +73,8 @@ class TelegramClient
|
||||||
# message from telegram network handler #
|
# message from telegram network handler #
|
||||||
def message_handler(update)
|
def message_handler(update)
|
||||||
@logger.info 'Got NewMessage update'
|
@logger.info 'Got NewMessage update'
|
||||||
from = update.message.chat_id
|
from = update.message.chat_id.to_s
|
||||||
text = update.message.content.text.text
|
text = update.message.content.text.text.to_s
|
||||||
@xmpp.send_message(from, text) if not update.message.is_outgoing
|
@xmpp.send_message(from, text) if not update.message.is_outgoing
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
require 'xmpp4r'
|
require 'xmpp4r'
|
||||||
|
|
||||||
|
#############################
|
||||||
### Some constants #########
|
### Some constants #########
|
||||||
::HELP_MESSAGE = "Unknown command. \n\n Please, use /login <phonenumber> to try log in. ☺"
|
::HELP_MESSAGE = "Unknown command. \n\n Please, use /login <phonenumber> to try log in. ☺"
|
||||||
|
#############################
|
||||||
|
|
||||||
#############################
|
#############################
|
||||||
## XMPP Transport Class #####
|
## XMPP Transport Class #####
|
||||||
#############################
|
#############################
|
||||||
class XMPPComponent
|
class XMPPComponent
|
||||||
|
# init class and set logger #
|
||||||
def initialize()
|
def initialize()
|
||||||
@logger = Logger.new(STDOUT); @logger.progname = '[XMPPComponent]'
|
@logger = Logger.new(STDOUT); @logger.progname = '[XMPPComponent]'
|
||||||
end
|
end
|
||||||
|
@ -28,39 +31,39 @@ class XMPPComponent
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#############################
|
||||||
|
#### Callback handlers #####
|
||||||
|
#############################
|
||||||
|
|
||||||
# new message to XMPP component #
|
# new message to XMPP component #
|
||||||
def message_handler(msg)
|
def message_handler(msg)
|
||||||
@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 and @sessions[msg.from.bare].online? # 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
|
||||||
|
|
||||||
|
#############################
|
||||||
|
#### Command handlers #####
|
||||||
|
#############################
|
||||||
|
|
||||||
# process internal /command #
|
# process internal /command #
|
||||||
def process_internal_command(jfrom, body)
|
def process_internal_command(jfrom, body)
|
||||||
case body.split[0] # /command argument = [command, argument]
|
case body.split[0] # /command argument = [command, argument]
|
||||||
when '/login'
|
when '/login' then @sessions[jfrom] = XMPPSession.new(jfrom, body.split[1]) # create new session for jid <jfrom> and spawn tg instance
|
||||||
# we will try to create new user session for JID <jfrom> and try to start telegram client for login <body.split[1]>
|
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
|
||||||
@sessions[jfrom] = XMPPSession.new(jfrom, body.split[1])
|
when '/logout' then @sessions[jfrom].offline! if @sessions.key? jfrom # go offline
|
||||||
when '/code', '/password'
|
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.
|
||||||
# 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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#############################
|
#############################
|
||||||
## XMPP Session Class #######
|
## XMPP Session Class #######
|
||||||
#############################
|
#############################
|
||||||
|
|
||||||
class XMPPSession < XMPPComponent
|
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 #
|
# start XMPP user session and Telegram client instance #
|
||||||
def initialize(jid, tg_login)
|
def initialize(jid, tg_login)
|
||||||
|
@ -70,7 +73,9 @@ class XMPPSession < XMPPComponent
|
||||||
@tg_client_thread = 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 current user via XMPP #
|
||||||
def send_message(from = nil, body = '')
|
def send_message(from = nil, body = '')
|
||||||
@logger.info "Incoming message from Telegram network <- %s" % from.to_s
|
@logger.info "Incoming message from Telegram network <- %s" % from.to_s
|
||||||
from = from.nil? ? @@transport.jid : from.to_s+'@'+@@transport.jid.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
|
@tg_auth_data[typ.to_sym] = data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
###########################################
|
||||||
|
|
||||||
# session status #
|
# session status #
|
||||||
def online?
|
def online?() @online end
|
||||||
@online
|
def online!() @online = true end
|
||||||
end
|
def offline!() @online = false end
|
||||||
def online!
|
|
||||||
@online = true
|
|
||||||
end
|
|
||||||
def offline!
|
|
||||||
@online = false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Reference in a new issue