[!] Changed config format — tdlib config moved to :tdlib section
[FIX] Small fixes in probing stored sessions
This commit is contained in:
parent
f74a03623e
commit
9689bda5f9
|
@ -1,29 +1,38 @@
|
||||||
::HELP_GATE_CMD= "/login phone — sign in\n" \
|
HELP_GATE_CMD = %q{Available commands:
|
||||||
"/logout — sign out\n" \
|
/login phone — sign in
|
||||||
"/code — check one-time code\n" \
|
/logout — sign out
|
||||||
"/password — check 2fa password\n" \
|
/code — check one-time code
|
||||||
"/setusername username — update @username\n" \
|
/password — check 2fa password
|
||||||
"/setname first last — update name\n" \
|
/setusername username — update @username
|
||||||
"/setbio — update about\n" \
|
/setname first last — update name
|
||||||
"/setpassword [old] [new] — set or remove password"
|
/setbio — update about
|
||||||
::HELP_CHAT_CMD= "/d [n] — delete your last message(s)\n" \
|
/setpassword [old] [new] — set or remove password
|
||||||
"/s regexp replace — edit your last message\n" \
|
/config [param] [value] — view or update configuration options
|
||||||
"/search string [limit] — search <string> in current chat\n" \
|
Configuration options
|
||||||
"/history [limit] — get last [limit] messages from current chat\n" \
|
timezone 00:00 — adjust timezone for Telegram user statuses
|
||||||
"/add @username — add @username to your chat list\n" \
|
}
|
||||||
"/join https://t.me/invite_link — join to chat via invite link\n" \
|
|
||||||
"/supergroup title description — create new supergroup «title» with «description»\n" \
|
HELP_CHAT_CMD= %q{Available commands:
|
||||||
"/channel title description — create new channel «title» with «description»\n" \
|
/d [n] — delete your last message(s)
|
||||||
"/secret — create secretchat with current user\n" \
|
/s regexp replace — edit your last message
|
||||||
"/group title — create groupchat «title» with current user\n" \
|
/add @username — add @username to your chat list
|
||||||
"/block — blacklist current user\n" \
|
/join https://t.me/invite_link — join to chat via invite link
|
||||||
"/unblock — unblacklist current user\n" \
|
/group title — create groupchat «title» with current user
|
||||||
"/invite id or @username — add user to current chat\n" \
|
/supergroup title description — create new supergroup «title» with «description»
|
||||||
"/kick id or @username — remove user from current chat\n" \
|
/channel title description — create new channel «title» with «description»
|
||||||
"/ban id or @username [hours] — restrict @username from current chat for [hours] or forever\n" \
|
/secret — create secretchat with current user
|
||||||
"/leave — leave current chat\n" \
|
/search string [limit] — search <string> in current chat
|
||||||
"/close — close current secret chat\n" \
|
/history [limit] — get last [limit] messages from current chat
|
||||||
"/delete — delete current chat from chat list"
|
/block — blacklist current user
|
||||||
|
/unblock — unblacklist current user
|
||||||
|
/invite id or @username — add user to current chat
|
||||||
|
/kick id or @username — remove user from current chat
|
||||||
|
/ban id or @username [hours] — restrict @username from current chat for [hours] or forever
|
||||||
|
/leave — leave current chat
|
||||||
|
/close — close current secret chat
|
||||||
|
/delete — delete current chat from chat list
|
||||||
|
/members [query] — search members [by optional query] in current chat (requires admin rights)
|
||||||
|
}
|
||||||
|
|
||||||
class TelegramClient
|
class TelegramClient
|
||||||
|
|
||||||
|
@ -33,9 +42,8 @@ class TelegramClient
|
||||||
## configure tdlib (when valid tdlib params specified) or zhabogram
|
## configure tdlib (when valid tdlib params specified) or zhabogram
|
||||||
def self.configure(**config)
|
def self.configure(**config)
|
||||||
@@config = @@config.merge(config)
|
@@config = @@config.merge(config)
|
||||||
TD.config[:lib_path] = @@config[:lib_path]
|
TD.config.update(config[:tdlib])
|
||||||
TD.configure do |config| @@config[:client].each do |key,value| config.client[key] = value if config.client.key? key end; end
|
TD::Api.set_log_verbosity_level(@@config[:tdlib_verbosity])
|
||||||
TD::Api.set_log_verbosity_level(@@config[:verbosity])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
## initialize telegram client instance (xmpp = XMPP stream, jid = user's jid , login = user's telegram login (for now, it is phone number)
|
## initialize telegram client instance (xmpp = XMPP stream, jid = user's jid , login = user's telegram login (for now, it is phone number)
|
||||||
|
@ -45,8 +53,6 @@ class TelegramClient
|
||||||
@jid = jid
|
@jid = jid
|
||||||
@session = session
|
@session = session
|
||||||
@cache = {chats: {nil => []}, users: {}}
|
@cache = {chats: {nil => []}, users: {}}
|
||||||
@xmpp.send_presence(@jid, nil, :subscribe)
|
|
||||||
@xmpp.send_presence(@jid, nil, :probe)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
## connect telegram client
|
## connect telegram client
|
||||||
|
@ -98,17 +104,11 @@ class TelegramClient
|
||||||
@logger.warn 'Waiting for 2FA password..'
|
@logger.warn 'Waiting for 2FA password..'
|
||||||
@xmpp.send_message(@jid, nil, 'Please, enter 2FA passphrase via /password 12345')
|
@xmpp.send_message(@jid, nil, 'Please, enter 2FA passphrase via /password 12345')
|
||||||
when TD::Types::AuthorizationState::Ready # stage 3: auth completed
|
when TD::Types::AuthorizationState::Ready # stage 3: auth completed
|
||||||
|
@session[:login] ||= @me.phone_number
|
||||||
@logger.warn 'Authorization successful!'
|
@logger.warn 'Authorization successful!'
|
||||||
@telegram.get_me.then{|me| @me = me}.wait
|
@telegram.get_me.then{|me| @me = me}.wait
|
||||||
@telegram.get_chats(limit=999).wait
|
@telegram.get_chats(limit=999).wait
|
||||||
@session[:login] ||= @me.phone_number
|
|
||||||
@xmpp.send_presence(@jid, nil, nil, nil, "Logged in %s" % @session[:login])
|
@xmpp.send_presence(@jid, nil, nil, nil, "Logged in %s" % @session[:login])
|
||||||
when TD::Types::AuthorizationState::Closing, TD::Types::AuthorizationState::Closed # disconnecting
|
|
||||||
@logger.warn 'Closing session..'
|
|
||||||
self.disconnect()
|
|
||||||
when TD::Types::AuthorizationState::LoggingOut # logout
|
|
||||||
@logger.warn 'Logging out..'
|
|
||||||
@session[:login] = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -257,7 +257,8 @@ class TelegramClient
|
||||||
when '/setname' then @telegram.set_name(args[0] || '', args[1] || '') # set My Name
|
when '/setname' then @telegram.set_name(args[0] || '', args[1] || '') # set My Name
|
||||||
when '/setbio' then @telegram.set_bio(args[0] || '') # set About
|
when '/setbio' then @telegram.set_bio(args[0] || '') # set About
|
||||||
when '/setpassword' then @telegram.set_password((args[1] ? args[0] : ''), args[1]) # set password
|
when '/setpassword' then @telegram.set_password((args[1] ? args[0] : ''), args[1]) # set password
|
||||||
else @xmpp.send_message(@jid, nil, ::HELP_GATE_CMD)
|
when '/config' then @xmpp.send_message(@jid, nil, args[1] ? "%s set to %s" % [args[0], @session.store(args[0].to_sym, args[1])] : @session.map{|attr| "%s is set to %s" % attr}.join("\n"))
|
||||||
|
when '/help' then @xmpp.send_message(@jid, nil, HELP_GATE_COMMANDS)
|
||||||
end
|
end
|
||||||
return true # stop executing
|
return true # stop executing
|
||||||
else # chat commands
|
else # chat commands
|
||||||
|
@ -269,18 +270,19 @@ class TelegramClient
|
||||||
when '/supergroup' then @telegram.create_new_supergroup_chat(args[0], args[1..-1].join(' '), is_channel: false) # create new supergroup
|
when '/supergroup' then @telegram.create_new_supergroup_chat(args[0], args[1..-1].join(' '), is_channel: false) # create new supergroup
|
||||||
when '/channel' then @telegram.create_new_supergroup_chat(args[0], args[1..-1].join(' '), is_channel: true) # create new channel
|
when '/channel' then @telegram.create_new_supergroup_chat(args[0], args[1..-1].join(' '), is_channel: true) # create new channel
|
||||||
when '/secret' then @telegram.create_new_secret_chat(chat.id) if user # create secret chat with current user
|
when '/secret' then @telegram.create_new_secret_chat(chat.id) if user # create secret chat with current user
|
||||||
when '/group' then @telegram.create_new_basic_group_chat(chat.id, args[0]) if user # create group chat with current user
|
when '/group' then @telegram.create_new_basic_group_chat(chat.id, args[0]) if chat.id > 0 # create group chat with current user
|
||||||
when '/block' then @telegram.block_user(chat.id) if user # blacklists current user
|
when '/block' then @telegram.block_user(chat.id) if chat.id > 0 # blacklists current user
|
||||||
when '/unblock' then @telegram.unblock_user(chat.id) if user # unblacklists current user
|
when '/unblock' then @telegram.unblock_user(chat.id) if chat.id > 0 # unblacklists current user
|
||||||
when '/invite' then @telegram.add_chat_member(chat.id, (args[0].to_i == 0 ? @telegram.search_public_chat(args[0]).value.id : args[0].to_i)) if chat.id < 0 # invite @username to current groupchat
|
when '/invite' then @telegram.add_chat_member(chat.id, (args[0].to_i == 0 ? @telegram.search_public_chat(args[0]).value.id : args[0].to_i)) if chat.id < 0 # invite @username to current groupchat
|
||||||
when '/kick' then @telegram.set_chat_member_status(chat.id, (args[0].to_i == 0 ? @telegram.search_public_chat(args[0]).value.id : args[0].to_i)) if chat.id < 0 # kick @username from current group chat
|
when '/kick' then @telegram.set_chat_member_status(chat.id, (args[0].to_i == 0 ? @telegram.search_public_chat(args[0]).value.id : args[0].to_i)) if chat.id < 0 # kick @username from current group chat
|
||||||
when '/ban' then @telegram.set_chat_member_status(chat.id, (args[0].to_i == 0 ? @telegram.search_public_chat(args[0]).value.id : args[0].to_i), TD::Types::ChatMemberStatus::Banned(banned_until_date: (args[1].to_i > 0 ? Time.now.getutc.to_i+(args[1].to_i*3600) : 0))) if chat.id < 0 # ban @username from current chat [for N hours]
|
when '/ban' then @telegram.set_chat_member_status(chat.id, (args[0].to_i == 0 ? @telegram.search_public_chat(args[0]).value.id : args[0].to_i), TD::Types::ChatMemberStatus::Banned(banned_until_date: (args[1].to_i > 0 ? Time.now.getutc.to_i+(args[1].to_i*3600) : 0))) if chat.id < 0 # ban @username from current chat [for N hours]
|
||||||
when '/leave' then @telegram.leave_chat(chat.id).then{|result| @xmpp.send_presence(@jid, chat_id, :unsubscribed)} if chat.type.instance_of? TD::Types::ChatType::Supergroup or chat.type.instance_of? TD::Types::ChatType::BasicGroup # leave current chat
|
when '/leave' then @telegram.leave_chat(chat.id).then{@xmpp.send_presence(@jid, chat_id, :unsubscribed)} if chat.type.instance_of? TD::Types::ChatType::Supergroup or chat.type.instance_of? TD::Types::ChatType::BasicGroup # leave current chat
|
||||||
when '/close' then @telegram.close_secret_chat(chat.type.secret_chat_id).then{|result| @xmpp.send_presence(@jid, chat_id, :unsubscribed)} if chat.type.instance_of? TD::Types::ChatType::Secret # close secret chat
|
when '/close' then @telegram.close_secret_chat(chat.type.secret_chat_id).then{@xmpp.send_presence(@jid, chat_id, :unsubscribed)} if chat.type.instance_of? TD::Types::ChatType::Secret # close secret chat
|
||||||
when '/delete' then @telegram.delete_chat_history(chat.id, true).then{|result| @xmpp.send_presence(@jid, chat_id, :unsubscribed)} # delete current chat
|
when '/delete' then @telegram.delete_chat_history(chat.id, true).then{@xmpp.send_presence(@jid, chat_id, :unsubscribed)} # delete current chat
|
||||||
when '/search' then @telegram.search_chat_messages(chat.id, 0, args[1]||10, query: args[0]||nil, filter: TD::Types::SearchMessagesFilter::Empty.new).value.messages.reverse.each do |msg| @xmpp.send_message(@jid, chat_id, self.format_message(nil,nil,nil,msg)) end # search messages within current chat
|
when '/search' then @telegram.search_chat_messages(chat.id, 0, args[1]||10, query: args[0]||nil, filter: TD::Types::SearchMessagesFilter::Empty.new).value.messages.reverse.each do |msg| @xmpp.send_message(@jid, chat_id, self.format_message(nil,nil,nil,msg)) end # search messages within current chat
|
||||||
when '/history' then @telegram.get_chat_history(chat.id, 0, 0, args[0]||10).value.messages.reverse.each do |msg| @xmpp.send_message(@jid, chat_id, self.format_message(nil,nil,msg)) end # get latest entries from history
|
when '/history' then @telegram.get_chat_history(chat.id, 0, 0, args[0]||10).value.messages.reverse.each do |msg| @xmpp.send_message(@jid, chat_id, self.format_message(nil,nil,msg)) end # get latest entries from history
|
||||||
when '/help' then @xmpp.send_message(@jid, chat_id, ::HELP_CHAT_CMD)
|
when '/members' then @telegram.search_chat_members(chat.id,9999,query:args[0],filter: TD::Types::ChatMembersFilter::Members.new).then{|members| @xmpp.send_message(@jid, chat_id, (members.members.map do |user| "%s | role: %s" % [self.fmt_chat(user.user_id), user.status.class] end).join("\n")) } # members list (for admins)
|
||||||
|
when '/help' then @xmpp.send_message(@jid, chat_id, HELP_CHAT_CMD)
|
||||||
else return # continue executing
|
else return # continue executing
|
||||||
end
|
end
|
||||||
return true # stop executing
|
return true # stop executing
|
||||||
|
|
|
@ -12,7 +12,6 @@ class XMPPComponent
|
||||||
|
|
||||||
## connect to XMPP server
|
## connect to XMPP server
|
||||||
def connect()
|
def connect()
|
||||||
Jabber::debug = @config[:debug]
|
|
||||||
begin
|
begin
|
||||||
@component = Jabber::Component.new(@config[:jid]) # init XMPP component
|
@component = Jabber::Component.new(@config[:jid]) # init XMPP component
|
||||||
@component.connect(@config[:host], @config[:port]) # connect to XMPP server
|
@component.connect(@config[:host], @config[:port]) # connect to XMPP server
|
||||||
|
@ -24,6 +23,7 @@ class XMPPComponent
|
||||||
@component.add_iq_callback do |stanza| self.handle_vcard_iq(stanza) if stanza.type == :get and stanza.vcard end # vcards handler
|
@component.add_iq_callback do |stanza| self.handle_vcard_iq(stanza) if stanza.type == :get and stanza.vcard end # vcards handler
|
||||||
@logger.warn 'Connected to XMPP server'
|
@logger.warn 'Connected to XMPP server'
|
||||||
@db.transaction do @db[:sessions].each do |jid, session| @sessions[jid] = TelegramClient.new(self, jid, session) end end # probe all known sessions
|
@db.transaction do @db[:sessions].each do |jid, session| @sessions[jid] = TelegramClient.new(self, jid, session) end end # probe all known sessions
|
||||||
|
@sessions.each_key do |jid| self.send_presence(jid,nil,:probe) end
|
||||||
Thread.new { while @component.is_connected? do sleep 60; @queue.delete_if {|_, presence| @component.send(presence) || true } end } # status updater thread
|
Thread.new { while @component.is_connected? do sleep 60; @queue.delete_if {|_, presence| @component.send(presence) || true } end } # status updater thread
|
||||||
Thread.stop() # stop main thread loop
|
Thread.stop() # stop main thread loop
|
||||||
rescue Exception => error
|
rescue Exception => error
|
||||||
|
|
Reference in a new issue