dvyjones-tinybot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Henrik Hodne
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = tinybot
2
+
3
+ This is a tiny IRC bot inspired by Sinatra. It requires Ruby 1.9.1 or later.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Henrik Hodne. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "tinybot"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "henrik.hodne@gmail.com"
10
+ gem.homepage = "http://github.com/dvyjones/tinybot"
11
+ gem.authors = ["Henrik Hodne"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "tinybot #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,17 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
2
+
3
+ require "tinybot"
4
+
5
+ #
6
+ # To use this, change the host, port and nick and then invite this bot into a
7
+ # channel. Then, type "say hello" in the channel, and the bot replies "hello"
8
+ #
9
+
10
+ set :auto_join_on_invite, true
11
+
12
+ privmsg /^say (.*)/i do |say|
13
+ reply say
14
+ end
15
+
16
+ connect(host: "irc.cluenet.org", port: 6667, nick: "HelloWorldBot") do
17
+ end
data/lib/tinybot.rb ADDED
@@ -0,0 +1,117 @@
1
+ require "singleton"
2
+ require "socket"
3
+
4
+ class TinyBot
5
+ include Singleton
6
+
7
+ attr_accessor :settings
8
+
9
+ def initialize
10
+ @settings = {}
11
+ end
12
+
13
+ def raw(line)
14
+ puts line if @settings[:debug]
15
+ @socket.puts("#{line}\x0D\x0A")
16
+ end
17
+
18
+ def connect(options)
19
+ @socket = TCPSocket.new(options[:host], options[:port])
20
+ @options = options
21
+ @options[:ident] ||= @options[:nick].downcase
22
+ @options[:gecos] ||= @options[:nick]
23
+ raw "USER #{@options[:ident]} ! ! :#{@options[:gecos]}"
24
+ raw "NICK #{@options[:nick]}"
25
+
26
+ at_exit do
27
+ # Start listen loop
28
+ until @socket.closed?
29
+ begin
30
+ line = @socket.gets.chomp
31
+ rescue IOError
32
+ break
33
+ end
34
+ puts line if @settings[:debug]
35
+ args = line.split(/ /)
36
+ prefix = ''
37
+ if args[0][0] == ?:
38
+ prefix = args.shift
39
+ end
40
+ command = args.shift.downcase
41
+ if command == "ping"
42
+ raw("PONG #{args.join(" ")}")
43
+ end
44
+ case command
45
+ when "privmsg"
46
+ @from = prefix[1..-1].split('!').first
47
+ @to = args.shift
48
+ @message = args.join(" ")[1..-1]
49
+ @privmsg.each do |regex, blocks|
50
+ if (match = regex.match(@message))
51
+ blocks.each do |block|
52
+ self.instance_exec(*(match.captures), &block)
53
+ end
54
+ end
55
+ end
56
+ when "invite"
57
+ if @settings[:auto_join_on_invite]
58
+ who, chan = args.shift, args.shift
59
+ join(chan)
60
+ end
61
+ when "376", "422"
62
+ @connected.each do |block|
63
+ self.instance_eval(&block)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ def listen_privmsg(regex, &block)
71
+ @privmsg ||= Hash.new { |h,k| h[k] = [] }
72
+ @privmsg[regex] << block
73
+ end
74
+
75
+ def listen_connected(&block)
76
+ @connected ||= []
77
+ @connected << block
78
+ end
79
+
80
+ def reply(msg)
81
+ if @to[0] == ?# # Channel
82
+ privmsg(@to, msg)
83
+ else # Privmsg
84
+ privmsg(@from, msg)
85
+ end
86
+ end
87
+
88
+ def privmsg(to, mesg)
89
+ raw "PRIVMSG #{to} :#{mesg}"
90
+ end
91
+
92
+ def join(chan)
93
+ raw "JOIN #{chan}"
94
+ end
95
+
96
+ def kick(kickee, channel=nil, reason)
97
+ raw "KICK #{channel||@to} #{kickee} :#{reason}"
98
+ end
99
+ end
100
+
101
+ def bot
102
+ TinyBot.instance
103
+ end
104
+
105
+ def set(option, value)
106
+ bot.settings[option] = value
107
+ end
108
+
109
+ def connect(options, &block)
110
+ puts "Connecting with options: #{options.inspect}"
111
+ bot.connect(options)
112
+ bot.listen_connected(&block)
113
+ end
114
+
115
+ def privmsg(regex, &block)
116
+ bot.listen_privmsg(regex, &block)
117
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'tinybot'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class TinybotTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dvyjones-tinybot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Henrik Hodne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: henrik.hodne@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - examples/helloworld.rb
33
+ - lib/tinybot.rb
34
+ - test/test_helper.rb
35
+ - test/tinybot_test.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/dvyjones/tinybot
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: TODO
62
+ test_files:
63
+ - test/test_helper.rb
64
+ - test/tinybot_test.rb
65
+ - examples/helloworld.rb