Skip to content
Browse files

autorun support

  • Loading branch information...
1 parent 19471dc commit 5dd0b39e992dbaf32b11f7809f25c63a9d129828 @raggi committed
Showing with 43 additions and 12 deletions.
  1. +4 −6 bin/talker_bot
  2. +16 −0 examples/autorun.bot
  3. +13 −0 examples/cat.bot
  4. +0 −6 lib/talker_bot.rb
  5. +10 −0 lib/talker_bot/autorun.rb
View
10 bin/talker_bot
@@ -14,9 +14,7 @@ end
require 'talker_bot'
-EM.run {
- bot = TalkerBot.new :room => ARGV.first
- bot.instance_eval(File.read(config), config, 0)
- bot.connect
- bot.run
-}
+bot = TalkerBot.new :room => ARGV.first
+bot.instance_eval(File.read(config), config, 0)
+trap(:INT) { EM.stop if EM.reactor_running?; exit }
+EM.run { bot.run }
View
16 examples/autorun.bot
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby
+require 'talker_bot/autorun'
+
+class Inspector
+ def respond_to?(name)
+ true if name.to_s =~ /^on_/
+ end
+
+ def method_missing(name, *args)
+ p [name, args]
+ end
+end
+
+# We can do inline configuration here...
+config :room, 40
+use Inspector.new
View
13 examples/cat.bot
@@ -0,0 +1,13 @@
+class Cat
+ def client=(client)
+ Thread.new do
+ while true
+ client.send_message $stdin.readline.chomp
+ end
+ end
+ rescue EOFError
+ exit!
+ end
+end
+
+use Cat.new
View
6 lib/talker_bot.rb
@@ -50,10 +50,4 @@ def connect
def event(type, *args)
@plugin_callbacks[type].each { |plugin| plugin.__send__(type, *args) }
end
-
- def run_filter_actions(list, *args)
- list.each do |filter, action|
- action.call(*args) if filter === event
- end
- end
end
View
10 lib/talker_bot/autorun.rb
@@ -0,0 +1,10 @@
+require 'talker_bot'
+
+@bot = TalkerBot.new
+
+require 'forwardable'
+include SingleForwardable
+
+def_delegators :@bot, *TalkerBot.instance_methods(false)
+
+at_exit { EM.run { @bot.run } }

0 comments on commit 5dd0b39

Please sign in to comment.
Something went wrong with that request. Please try again.