Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial import.

  • Loading branch information...
commit 17b55b4f09c38968f3c628499289913d63ac7786 0 parents
@david authored
2  .gitignore
@@ -0,0 +1,2 @@
+.*.swp
+.*.swo
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2007 David Leal
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 README
@@ -0,0 +1,4 @@
+templates
+=================
+
+A plugin for the Merb framework that provides ....
37 Rakefile
@@ -0,0 +1,37 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+
+GEM = "new_gem"
+VERSION = "0.0.1"
+AUTHOR = "David Leal"
+EMAIL = "dgleal@gmail.com"
+HOMEPAGE = "http://davidleal.com"
+SUMMARY = "A mini Ruby IRC bot framework"
+
+spec = Gem::Specification.new do |s|
+ s.name = GEM
+ s.version = VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["README", "LICENSE"]
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.author = AUTHOR
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+
+ # Uncomment this to add a dependency
+ # s.add_dependency "foo"
+
+ s.require_path = 'lib'
+ s.autorequire = GEM
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{lib,specs}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+task :install => [:package] do
+ sh %{sudo gem install pkg/#{GEM}-#{VERSION}}
+end
1  lib/minibot.rb
@@ -0,0 +1 @@
+require File.join(File.dirname(__FILE__), 'minibot', 'daemon.rb')
75 lib/minibot/daemon.rb
@@ -0,0 +1,75 @@
+require 'socket'
+
+module MiniBot
+ class Daemon
+ DEFAULTS = {
+ :join => []
+ }
+
+ 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]
+
+ main_loop
+ ensure
+ close
+ end
+ end
+
+ def event(sym, *args, &block)
+ @event_handlers[sym] << block
+ end
+
+ def join(channel)
+ write "JOIN #{channel}"
+ end
+
+ private
+
+ def join_channels(channels)
+ channels.each { |channel| join channel }
+ end
+
+ def initialize(options)
+ @options = DEFAULTS.merge options
+ @event_handlers = Hash.new { |h, k| h[k] = [] }
+ end
+
+ def main_loop
+ loop do
+ dispatch(@socket.readline)
+ end
+ end
+
+ def init_socket(server, port)
+ @socket = TCPSocket.new(server, port)
+ end
+
+ def write(str)
+ @socket.print "#{str}\r\n"
+ 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]
+ end
+ end
+
+ def handle_event(event, *args)
+ @event_handlers[event].each { |handler| handler.call self, *args }
+ end
+ end
+end
70 spec/daemon_spec.rb
@@ -0,0 +1,70 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe MiniBot::Daemon do
+ def daemon(options = {})
+ MiniBot::Daemon.new(
+ { :server => 'irc.freenode.net',
+ :username => 'spec',
+ :nick => 'nick',
+ :realname => 'Spec User' }.merge(options)
+ )
+ end
+
+ it "should connect to a server" do
+ socket = mock("socket")
+ socket.should_receive(:print).with("NICK nick\r\n").ordered
+ socket.should_receive(:print).with("USER spec xxx xxx :Spec User\r\n").ordered
+
+ TCPSocket.should_receive(:new).with('irc.freenode.net', 6667).and_return(socket)
+
+ d = daemon
+ d.should_receive(:main_loop)
+ d.should_receive(:close)
+
+ d.run
+ end
+
+ describe "using" 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 }
+
+ d.stub! :main_loop
+ d.should_receive(:join).with("#testch")
+ d.should_receive(:join).with("#anothertest")
+
+ d.run
+ 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
+
+ d.send :dispatch, ":ior3k!n=david@89.152.220.123 INVITE nnn :#ior3k"
+ 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
+ end
+ end
+end
2  spec/spec_helper.rb
@@ -0,0 +1,2 @@
+$TESTING=true
+require File.join(File.dirname(__FILE__), '..', 'lib', 'minibot')
Please sign in to comment.
Something went wrong with that request. Please try again.