summaryrefslogtreecommitdiff
path: root/bocelli/base.rb
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-08-17 23:31:45 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-08-17 23:31:45 -0700
commite633d656e00fdac51f077eff827518848235e916 (patch)
tree0b40a60f95b219545d40fe709e11583ab9e3df6b /bocelli/base.rb
downloadbocelli-e633d656e00fdac51f077eff827518848235e916.tar.gz
bocelli-e633d656e00fdac51f077eff827518848235e916.tar.xz
Initial commit.
Diffstat (limited to 'bocelli/base.rb')
-rw-r--r--bocelli/base.rb83
1 files changed, 83 insertions, 0 deletions
diff --git a/bocelli/base.rb b/bocelli/base.rb
new file mode 100644
index 0000000..dfb7875
--- /dev/null
+++ b/bocelli/base.rb
@@ -0,0 +1,83 @@
+require_relative 'core/irc'
+
+module Bocelli
+ class Base
+ def initialize(str, route, block, metadata)
+ @bocelli = {
+ str: str,
+ route: route,
+ block: block,
+ metadata: metadata
+ }
+ end
+
+ def execute
+ instance_eval(&@bocelli[:block])
+ end
+
+ def out(message)
+ self.class.privmsg(@bocelli[:metadata][:channel], message)
+ end
+
+ class << self
+ include Bocelli::Core::IRC
+
+ def setup
+ @routes = {}
+ @modules = {}
+ end
+
+ def inherited(subclass)
+ super
+
+ subclass.setup
+ end
+
+ def on(route, &block)
+ name = "route #{route.inspect}"
+
+ @routes[route] = block
+ end
+
+ def register(mod)
+ @modules[mod.name[/[^:]+$/].downcase.intern] ||= mod
+ end
+
+ def match(str, route)
+ case route
+ when Regexp
+ str =~ route
+ when String
+ str == route
+ end
+ end
+
+ def process(str)
+ routes = @modules.inject([]) { |m, (_, v)| m.push(*v.routes) }
+ routes.push(*@routes)
+
+ if str =~ /(\S+) PRIVMSG (\S+) :?(.*)/
+ metadata = {
+ user: $1,
+ channel: $2,
+ message: $3
+ }
+
+ if (match = routes.detect { |k, _| match(metadata[:message], k) })
+ route, block = match
+
+ new(str, route, block, metadata).execute
+ end
+ end
+ end
+
+ def run
+ while (str = sgets)
+ pong($1) if str =~ /\APING (.*)\z/
+
+ process(str)
+ end
+ end
+ end
+ end
+end