Revision 06f77931
Added by Marc Dequènes about 14 years ago
- ID 06f77931318bb489eca93b67514ccc34db6f788e
lib/cyborghood/cyborg.rb | ||
---|---|---|
include I18nTranslation
|
||
bindtextdomain("cyborghood", {:path => Config::L10N_DIR, :charset => "UTF-8"})
|
||
|
||
attr_reader :name, :system_notification, :user_notification
|
||
attr_reader :name
|
||
|
||
def initialize
|
||
@name = self.class.name.split("::").last
|
||
... | ... | |
end
|
||
end
|
||
|
||
@channels = {}
|
||
|
||
setup
|
||
|
||
logger.info "Bot '#{self.human_name}' loaded"
|
||
end
|
||
|
||
def setup
|
||
@system_notification = EventMachine::Channel.new
|
||
@user_notification = EventMachine::Channel.new
|
||
# #^.*/system$# categories are reserved
|
||
# but you may enhance or break the system as you wish
|
||
def get_channel(name)
|
||
return @channels[name] if @channels.has_key? name
|
||
@channels[name] = EventMachine::Channel.new
|
||
end
|
||
|
||
def drop_channel(name)
|
||
@channels.delete(name)
|
||
end
|
||
|
||
def setup
|
||
@system_notification_name = 'global/system'
|
||
@system_notification = get_channel(@system_notification_name)
|
||
@system_notification_processing = @system_notification.subscribe do |msg|
|
||
process_system_notification(msg)
|
||
end
|
||
... | ... | |
|
||
def try_stop
|
||
if ready_to_stop?
|
||
@system_notification.unsubscribe(@system_notification_processing)
|
||
@system_notification_processing = nil
|
||
drop_channel(@system_notification_name)
|
||
|
||
EventMachine.stop_event_loop
|
||
else
|
||
EventMachine.next_tick { try_stop }
|
lib/cyborghood/cyborg/conversation.rb | ||
---|---|---|
MAXIMUM_LINES = 1024
|
||
|
||
attr_reader :bot, :peer_name, :peer_capabilities, :protocol
|
||
attr_reader :system_notification, :user_notification
|
||
|
||
def initialize(bot, block = nil)
|
||
@bot = bot
|
||
... | ... | |
@peer_name = nil
|
||
@peer_capabilities = []
|
||
|
||
@system_notification = EventMachine::Channel.new
|
||
@user_notification = EventMachine::Channel.new
|
||
|
||
@protocol = BotProtocol.new(self)
|
||
|
||
@pending_thread_close = Set.new
|
||
# we don't know the peer name yet
|
||
@system_notification_name = "peer/#{identifier}/system"
|
||
@system_notification = @bot.get_channel(@system_notification_name)
|
||
@system_notification_processing = @system_notification.subscribe do |msg|
|
||
process_system_notification(msg)
|
||
end
|
||
... | ... | |
def unbind
|
||
logger.info "Conversation finished with #{identifier} (#{@peer_name})"
|
||
@bot.unregister_communication @peer_name unless @peer_name.nil?
|
||
@bot.drop_channel(@system_notification_name)
|
||
@conv_threads.each_value {|s| s.close(false) }
|
||
@conv_threads = {}
|
||
@conv_threads_index = {}
|
||
... | ... | |
|
||
# if only the system thread remains, notify idleness
|
||
if @conv_threads.size == 1
|
||
@bot.system_notification << { :topic => 'CONVERSATION IDLE', :peer => peer_name }
|
||
@bot.get_channel('global/system') << {
|
||
:topic => 'CONVERSATION IDLE',
|
||
:peer => peer_name
|
||
}
|
||
end
|
||
end
|
||
|
||
... | ... | |
end
|
||
end
|
||
|
||
def identifier
|
||
"#{@bot.identifier_prefix}/#{@signature}"
|
||
end
|
||
|
||
protected
|
||
|
||
def clear_receive_info
|
||
... | ... | |
@split_data = []
|
||
end
|
||
|
||
def identifier
|
||
"#{@bot.identifier_prefix}/#{@signature}"
|
||
end
|
||
|
||
def process_system_notification(msg)
|
||
return unless msg[:topic] == 'THREAD IDLE'
|
||
|
lib/cyborghood/cyborg/protocol.rb | ||
---|---|---|
end
|
||
|
||
def receive_notify_event(message)
|
||
@conversation.user_notification << {
|
||
@conversation.bot.get_channel("peer/#{@conversation.peer_name}/incoming") << {
|
||
:from => message.conversation.peer_id,
|
||
:topic => message.action_parameters[:name],
|
||
:info => message.action_parameters[:info]
|
||
... | ... | |
|
||
def check_thread_idle(conv_thread)
|
||
return if @action_followup.has_key? conv_thread.id
|
||
|
||
@conversation.system_notification << { :topic => 'THREAD IDLE', :thread => conv_thread.name }
|
||
@conversation.bot.get_channel("peer/#{@conversation.identifier}/system") << {
|
||
:topic => 'THREAD IDLE',
|
||
:thread => conv_thread.name
|
||
}
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
[evol] reworked notification system a bit (global registration)