Permalink
Browse files

Several refactorings.

  * Made close() private.
  * Removed autojoin capability. This is not something the framework
    should decide, at least, not the way it was implemented.
  * Moved authentication to its own method.
  * Added a default event, for commands that we don't know how to
    handle.
  * Renamed init_socket to connect. It sounds better.
  * Added a connect event, which is run after authentication.
  • Loading branch information...
1 parent fcb1255 commit ba5c0254311bc09df08457139d4efeb4f5673f77 @david committed Jun 3, 2008
Showing with 33 additions and 49 deletions.
  1. +17 −21 lib/minibot/daemon.rb
  2. +16 −28 spec/daemon_spec.rb
View
@@ -3,22 +3,15 @@
module MiniBot
class Daemon
DEFAULTS = {
- :join => []
+ :join => [],
+ :port => 6667
}
- def close
- @socket.close if @socket
- end
-
def run
begin
- init_socket(@options[:server], 6667)
-
- write "NICK #{@options[:nick]}"
- write "USER #{@options[:username]} xxx xxx :#{@options[:realname]}"
-
- join_channels @options[:join]
-
+ connect(@options[:server], @options[:port])
+ authenticate(@options[:nick], @options[:username], @options[:realname])
+ handle_event :connect
main_loop
ensure
close
@@ -35,8 +28,13 @@ def join(channel)
private
- def join_channels(channels)
- channels.each { |channel| join channel }
+ def authenticate(nick, username, realname)
+ write "NICK #{nick}"
+ write "USER #{username} xxx xxx :#{realname}"
+ end
+
+ def close
+ @socket.close if @socket
end
def initialize(options)
@@ -45,12 +43,10 @@ def initialize(options)
end
def main_loop
- loop do
- dispatch(@socket.readline)
- end
+ loop { dispatch(@socket.readline) }
end
- def init_socket(server, port)
+ def connect(server, port)
@socket = TCPSocket.new(server, port)
end
@@ -59,17 +55,17 @@ def write(str)
end
def dispatch(command)
- puts command
-
if match = (/:(\w+)!.+ INVITE \w+ :(#\w+)/.match command)
handle_event :invite, match[2], match[1]
elsif match = (/:(\w+)!.+ PRIVMSG (#\w+) :(.+)/.match command)
handle_event :message, match[2], match[1], match[3]
+ else
+ handle_event :default, command
end
end
def handle_event(event, *args)
- @event_handlers[event].each { |handler| handler.call self, *args }
+ @event_handlers[event].each { |handler| handler.call *args }
end
end
end
View
@@ -24,47 +24,35 @@ def daemon(options = {})
d.run
end
- describe "using" do
+ describe "#dispatch" do
before do
@socket = mock("socket", :null_object => true)
TCPSocket.stub!(:new).and_return(@socket)
end
- it "should join the given channels" do
- d = daemon :join => %w{ #testch #anothertest }
+ it "should dispatch invites" do
+ d = daemon
- d.stub! :main_loop
- d.should_receive(:join).with("#testch")
- d.should_receive(:join).with("#anothertest")
+ tester = mock("tester")
+ tester.should_receive(:call).with("#ior3k", "ior3k")
+ d.event :invite do |channel, user|
+ tester.call channel, user
+ end
- d.run
+ d.send :dispatch, ":ior3k!n=david@89.152.220.123 INVITE nnn :#ior3k"
end
- describe "#dispatch" do
- it "should dispatch invites" do
- d = daemon
-
- tester = mock("tester")
- tester.should_receive(:call).with(d, "#ior3k", "ior3k")
- d.event :invite do |bot, channel, user|
- tester.call bot, channel, user
- end
+ it "should dispatch messages, unconditionally" do
+ d = daemon
- d.send :dispatch, ":ior3k!n=david@89.152.220.123 INVITE nnn :#ior3k"
+ tester = mock("tester")
+ tester.should_receive(:call).with("#ior3k", "ior3k", "This is a test message!")
+ d.event :message do |channel, user, message|
+ tester.call channel, user, message
end
- it "should dispatch messages, unconditionally" do
- d = daemon
-
- tester = mock("tester")
- tester.should_receive(:call).with(d, "#ior3k", "ior3k", "This is a test message!")
- d.event :message do |bot, channel, user, message|
- tester.call bot, channel, user, message
- end
-
- d.send :dispatch, ":ior3k!n=david@89.152.220.123 PRIVMSG #ior3k :This is a test message!"
- end
+ d.send :dispatch, ":ior3k!n=david@89.152.220.123 PRIVMSG #ior3k :This is a test message!"
end
end
end

0 comments on commit ba5c025

Please sign in to comment.