Browse files

Refactoring bot initialization.

  * Moved all server commands to the Commands module.
  * Removed auto join functionality until I'm really convinced it should
    be here.
  * Only use config hash for argument entry, but not for actual config
    storage.
  • Loading branch information...
1 parent 1969021 commit 165c60070350d6192b525a61d6808ddc34fa1559 @david committed Jun 22, 2008
Showing with 73 additions and 49 deletions.
  1. +16 −1 lib/minibot/commands.rb
  2. +22 −21 lib/minibot/daemon.rb
  3. +18 −0 spec/commands_spec.rb
  4. +17 −27 spec/daemon_spec.rb
View
17 lib/minibot/commands.rb
@@ -21,10 +21,25 @@ def topic(channel)
[ topic, author, timestamp && Time.at(timestamp.to_i) ]
end
+ def say(target, message)
+ end
+
+ def set_user(username, realname)
+ @server.write "USER #{username} 0 xxx :#{realname}"
+ end
+
+ def set_nick(nick)
+ @server.write "NICK #{nick}"
+ end
+
+ def nickserv(command)
+ @server.write "NICKSERV #{command}"
+ end
+
private
def pong
- write "PONG #{@host_name}"
+ @server.write "PONG #{@host_name}"
end
end
end
View
43 lib/minibot/daemon.rb
@@ -5,18 +5,13 @@ class Daemon
include Events
include Commands
- attr_reader :config, :server
-
- DEFAULTS = {
- :port => 6667,
- :channels => []
- }
+ attr_reader :server
def run
begin
- connect @config[:server], @config[:port]
- authenticate @config[:nick], @config[:username], @config[:realname]
- join *@config[:channels]
+ connect @servername, @port
+ login @username, @realname
+ identify @nick, @nickserv_pwd
main_loop
ensure
disconnect
@@ -25,31 +20,37 @@ def run
private
- def connect(server, port)
+ def initialize(config)
+ @servername = config[:server]
+ @port = config[:port] || 6667
+ @nick = config[:nick]
+ @nickserv_pwd = config[:nickserv_pwd]
+ @username = config[:username] || @nick
+ @realname = config[:realname] || @nick
+ end
+
+ def connect(server, port = 6667)
@server = Server.connect server, port
end
- def disconnect
- @server.disconnect
+ def login(username, realname)
+ set_user username, realname
end
- def initialize(config)
- @config = DEFAULTS.merge config
+ def identify(nick, password = nil)
+ set_nick(nick)
+ nickserv("identify #{password}") if password
+ end
- @config[:username] ||= @config[:nick]
- @config[:realname] ||= @config[:nick]
+ def disconnect
+ @server.disconnect
end
def main_loop
while msg = @server.read
dispatch msg
end
end
-
- def authenticate(nick, username, realname)
- @server.write "USER #{username} 0 xxx :#{realname}"
- @server.write "NICK #{nick}"
- end
end
end
View
18 spec/commands_spec.rb
@@ -66,4 +66,22 @@ def initialize(server = nil)
time.should be_nil
end
end
+
+ it "should send the NICKSERV command" do
+ bot = CommandBot.new(mock "server")
+ bot.server.should_receive(:write).with("NICKSERV identify hullo")
+ bot.nickserv "identify hullo"
+ end
+
+ it "should send the USER command" do
+ bot = CommandBot.new(mock "server")
+ bot.server.should_receive(:write).with("USER dude 0 xxx :Real name")
+ bot.set_user "dude", "Real name"
+ end
+
+ it "should send the NICK command" do
+ bot = CommandBot.new(mock "server")
+ bot.server.should_receive(:write).with("NICK a_nick")
+ bot.set_nick "a_nick"
+ end
end
View
44 spec/daemon_spec.rb
@@ -17,21 +17,14 @@ def daemon(opts = {})
socket = mock("socket", :null_object => true)
d = daemon
- d.instance_variable_get("@config")[:port].should == 6667
+ d.instance_variable_get("@port").should == 6667
end
it "should use the nick when the username is not specified" do
socket = mock("socket", :null_object => true)
d = daemon(:username => nil)
- d.instance_variable_get("@config")[:username].should == 'nick'
- end
-
- it "should return an empty array when no channels are specified" do
- socket = mock("socket", :null_object => true)
-
- d = daemon
- d.instance_variable_get("@config")[:channels].should == []
+ d.instance_variable_get("@username").should == 'nick'
end
end
@@ -41,28 +34,25 @@ def daemon(opts = {})
d.send :connect, 'server', 'port'
end
- it "should authenticate" do
- server = mock("server", :null_object => true)
- server.should_receive(:write).with("USER spec 0 xxx :Spec User").ordered
- server.should_receive(:write).with("NICK nick").ordered
-
+ it "should login" do
d = daemon
- d.instance_variable_set("@server", server)
- d.should_receive(:server).any_number_of_times.and_return(server)
- d.send(:authenticate, 'nick', 'spec', 'Spec User')
+ d.should_receive(:set_user).with('user', 'real name')
+ d.send :login, 'user', 'real name'
end
- describe "running" do
- it "should auto join channels" do
- channels = %w{#one #two}
- d = daemon({ :channels => channels })
- d.stub!(:connect)
- d.stub!(:authenticate)
- d.stub!(:main_loop)
- d.stub!(:disconnect)
+ describe "should identify" do
+ it "with a password" do
+ d = daemon
+ d.should_receive(:set_nick).with('nick')
+ d.should_receive(:nickserv).with('identify hello')
+ d.send :identify, 'nick', 'hello'
+ end
- d.should_receive(:join).with("#one", "#two")
- d.run
+ it "without a password" do
+ d = daemon
+ d.should_receive(:set_nick).with('nick')
+ d.should_not_receive(:nickserv)
+ d.send :identify, 'nick'
end
end
end

0 comments on commit 165c600

Please sign in to comment.