2019-04-06 06:12:54 +00:00
require 'xmpp4r'
2019-04-07 11:58:07 +00:00
require 'sqlite3'
2019-04-06 06:12:54 +00:00
2019-04-06 08:23:14 +00:00
#############################
2019-04-06 07:29:48 +00:00
### Some constants #########
:: HELP_MESSAGE = " Unknown command. \n \n Please, use /login <phonenumber> to try log in. ☺ "
2019-04-06 08:23:14 +00:00
#############################
2019-04-06 07:29:48 +00:00
2019-04-06 06:12:54 +00:00
#############################
## XMPP Transport Class #####
#############################
class XMPPComponent
2019-04-07 11:58:07 +00:00
2019-04-06 08:23:14 +00:00
# init class and set logger #
2019-04-07 11:58:07 +00:00
def initialize ( params )
@logger = Logger . new ( STDOUT ) ; @logger . level = params [ 'loglevel' ] || Logger :: DEBUG ; @logger . progname = '[XMPPComponent]'
@config = { host : params [ " host " ] || 'localhost' , port : params [ " port " ] || 8899 , jid : params [ " jid " ] || 'tlgrm.rxtx.us' , secret : params [ 'secret' ] || '' } # default config
@sessions = { }
@db = SQLite3 :: Database . new ( params [ 'db_path' ] || 'users.db' )
@db . execute ( " CREATE TABLE IF NOT EXISTS users(jid varchar(256), tg_login varchar(256), PRIMARY KEY(jid) ); " )
@db . results_as_hash = true
end
# database #
def load_db ( jid = nil ) # load
@logger . info " Initializing database... "
query = ( jid . nil? ) ? " SELECT * FROM users " : " SELECT * FROM users where jid = '%s'; " % jid
@logger . debug ( query )
@db . execute ( query ) do | user |
@logger . info " Found session for JID %s and TG login %s " % [ user [ " jid " ] . to_s , user [ " tg_login " ] ]
@sessions [ user [ " jid " ] ] = XMPPSession . new ( user [ " jid " ] , user [ " tg_login " ] )
end
2019-04-06 07:29:48 +00:00
end
2019-04-07 11:58:07 +00:00
def update_db ( jid , delete = false ) # write
return if not @sessions . key? jid
@logger . info " Writing database [add %s].. " % jid . to_s
query = ( delete ) ? " DELETE FROM users where jid = '%s'; " % jid . to_s : " INSERT OR REPLACE INTO users(jid, tg_login) VALUES('%s', '%s'); " % [ jid . to_s , @sessions [ jid ] . tg_login . to_s ]
@logger . debug query
@db . execute ( query )
end
2019-04-06 06:12:54 +00:00
# transport initialization & connecting to XMPP server #
2019-04-07 11:58:07 +00:00
def connect ( ) # :jid => transport_jid, :host => xmpp_server, :port => xmpp_component_port, :secret => xmpp_component_secret
2019-04-06 07:29:48 +00:00
@logger . info " Connecting.. "
2019-04-06 06:12:54 +00:00
begin
2019-04-07 11:58:07 +00:00
@@transport = Jabber :: Component . new ( @config [ :jid ] )
@@transport . connect ( @config [ :host ] , @config [ :port ] )
@@transport . auth ( @config [ :secret ] )
2019-04-06 06:12:54 +00:00
@@transport . add_message_callback do | msg | msg . first_element_text ( 'body' ) ? self . message_handler ( msg ) : nil end
2019-04-07 11:58:07 +00:00
@@transport . add_presence_callback do | presence | self . presence_handler ( presence ) end
@@transport . add_iq_callback do | iq | self . iq_handler ( iq ) end
2019-04-06 07:29:48 +00:00
@logger . info " Connection established "
2019-04-07 11:58:07 +00:00
self . load_db ( )
@logger . info 'Found %s sessions in database.' % @sessions . count
@sessions . each do | jid , session |
@logger . info " Sending presence to %s " % jid
p = Jabber :: Presence . new ( )
p . to = jid
p . from = @@transport . jid
p . type = :subscribe
@logger . debug p
@@transport . send ( p )
end
2019-04-06 06:12:54 +00:00
Thread . stop ( )
rescue Exception = > e
2019-04-06 07:29:48 +00:00
@logger . info 'Connection failed: %s' % e
2019-04-07 11:58:07 +00:00
@db . close
2019-04-06 06:12:54 +00:00
exit 1
end
end
2019-04-06 08:23:14 +00:00
#############################
#### Callback handlers #####
#############################
2019-04-06 06:12:54 +00:00
# new message to XMPP component #
def message_handler ( msg )
2019-04-06 07:29:48 +00:00
@logger . info 'New message from [%s] to [%s]' % [ msg . from , msg . to ]
2019-04-07 11:58:07 +00:00
return self . process_internal_command ( msg . from . bare . to_s , 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 . to_s ] . queue_message ( msg . to . to_s , msg . first_element_text ( 'body' ) ) if @sessions . key? msg . from . bare . to_s and @sessions [ msg . from . bare . to_s ] . online? # queue message for processing session is active for jid from
end
def presence_handler ( presence )
@logger . info " New presence iq received "
@logger . debug ( presence )
if presence . type == :subscribe then reply = presence . answer ( false ) ; reply . type = :subscribed ; @@transport . send ( reply ) ; end # send "subscribed" reply to "subscribe" presence
if presence . to == @@transport . jid and @sessions . key? presence . from . bare . to_s and presence . type == :unavailable then @sessions [ presence . from . bare . to_s ] . offline! ; return ; end # go offline when received offline presence from jabber user
if presence . to == @@transport . jid and @sessions . key? presence . from . bare . to_s then @sessions [ presence . from . bare . to_s ] . connect ( ) ; return ; end # connect if we have session
end
def iq_handler ( iq )
@logger . info " New iq received "
@logger . debug ( iq )
2019-04-06 06:12:54 +00:00
end
2019-04-06 08:23:14 +00:00
#############################
#### Command handlers #####
#############################
2019-04-06 06:12:54 +00:00
# process internal /command #
def process_internal_command ( jfrom , body )
case body . split [ 0 ] # /command argument = [command, argument]
2019-04-07 11:58:07 +00:00
when '/login' # creating new session if not exists and connect if user already has session
@sessions [ jfrom ] = XMPPSession . new ( jfrom , body . split [ 1 ] ) if not @sessions . key? jfrom
@sessions [ jfrom ] . connect ( )
self . update_db ( jfrom )
when '/code' , '/password' # pass auth data if we have session
typ = body . split [ 0 ] [ 1 .. 8 ]
data = body . split [ 1 ]
@sessions [ jfrom ] . enter_auth_data ( typ , data ) if @sessions . key? jfrom
when '/disconnect' # going offline without destroying a session
@sessions [ jfrom ] . offline! if @sessions . key? jfrom
when '/logout' # destroying session
@sessions [ jfrom ] . offline! if @sessions . key? jfrom
self . update_db ( jfrom , true )
@sessions . delete ( jfrom )
else # unknown command -- display help #
msg = Jabber :: Message . new
msg . from = @@transport . jid
msg . to = jfrom
msg . body = :: HELP_MESSAGE
msg . type = :chat
@@transport . send ( msg )
2019-04-06 06:12:54 +00:00
end
end
2019-04-06 08:23:14 +00:00
2019-04-06 06:12:54 +00:00
end
#############################
## XMPP Session Class #######
#############################
class XMPPSession < XMPPComponent
2019-04-06 08:23:14 +00:00
attr_reader :user_jid , :tg_login , :tg_auth_data , :message_queue
attr_accessor :online
2019-04-06 06:12:54 +00:00
# start XMPP user session and Telegram client instance #
def initialize ( jid , tg_login )
2019-04-07 11:58:07 +00:00
@logger = Logger . new ( STDOUT ) ; @logger . progname = '[XMPPSession: %s/%s]' % [ jid , tg_login ] # init logger
@logger . info " Initializing new XMPPSession... "
@user_jid , @tg_login , @tg_auth_data , @message_queue = jid , tg_login , { code : nil , password : nil } , Queue . new ( ) # init class variables
end
# connect to tg #
def connect ( )
return if self . online?
2019-04-06 07:29:48 +00:00
@logger . info " Starting Telegram session "
2019-04-07 11:58:07 +00:00
@online = nil
@telegram_thr = Thread . new { TelegramClient . new ( self , @tg_login ) } # init tg instance in new thread
2019-04-06 06:12:54 +00:00
end
2019-04-06 08:23:14 +00:00
###########################################
# send message to current user via XMPP #
2019-04-06 06:12:54 +00:00
def send_message ( from = nil , body = '' )
2019-04-06 07:29:48 +00:00
@logger . info " Incoming message from Telegram network <- %s " % from . to_s
2019-04-07 11:58:07 +00:00
reply = Jabber :: Message . new
reply . type = :chat
reply . from = from . nil? ? @@transport . jid : from . to_s + '@' + @@transport . jid . to_s
reply . to = @user_jid
reply . body = body
@logger . debug reply
2019-04-06 06:12:54 +00:00
@@transport . send ( reply )
end
2019-04-07 11:58:07 +00:00
# subscription request to current user via XMPP #
def subscription_req ( from , nickname = nil )
@logger . info " Subscription request from %s.. " % from . to_s
req = Jabber :: Presence . new ( )
req . from = from . nil? ? @@transport . jid : from . to_s + '@' + @@transport . jid . to_s # presence <from>
req . to = @user_jid # presence <to>
req . type = :subscribe
req . add_element ( 'nick' , { 'xmlns' = > 'http://jabber.org/protocol/nick' } ) . add_text ( nickname ) unless nickname . nil?
@logger . debug req
@@transport . send ( req )
end
# presence update #
def presence_update ( from , status , message , type = nil )
@logger . info " Presence update request from %s.. " % from . to_s
req = Jabber :: Presence . new ( )
req . from = from . nil? ? @@transport . jid : from . to_s + '@' + @@transport . jid . to_s # presence <from>
req . to = @user_jid # presence <to>
req . show = status unless status . nil? # presence <show>
req . type = type unless type . nil? # pres. type
req . status = message # presence message
@logger . debug req
@@transport . send ( req )
end
###########################################
2019-04-06 06:12:54 +00:00
# queue message (we will share this queue within :message_queue to Telegram client thread) #
def queue_message ( to , text = '' )
2019-04-06 07:29:48 +00:00
@logger . info " Queuing message to be sent to Telegram network user -> " % to
2019-04-06 06:12:54 +00:00
@message_queue << { to : to . split ( '@' ) [ 0 ] , text : text }
end
# enter auth data (we will share this data within :tg_auth_data to Telegram client thread ) #
def enter_auth_data ( typ , data )
2019-04-07 11:58:07 +00:00
@logger . info " Authorizing in Telegram network with :%s " % typ
2019-04-06 07:29:48 +00:00
@tg_auth_data [ typ . to_sym ] = data
2019-04-06 06:12:54 +00:00
end
2019-04-06 07:46:29 +00:00
2019-04-06 08:23:14 +00:00
###########################################
2019-04-06 07:46:29 +00:00
# session status #
2019-04-06 08:23:14 +00:00
def online? ( ) @online end
2019-04-08 22:28:26 +00:00
def online! ( ) @online = true ; @tg_auth = { } ; self . subscription_req ( nil ) ; self . presence_update ( nil , nil , " Logged in as " + @tg_login . to_s ) end
2019-04-07 11:58:07 +00:00
def offline! ( ) @online = false ; self . presence_update ( nil , nil , " Logged out " , :unavailable ) ; end
2019-04-06 06:12:54 +00:00
end