moonbot 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in MoonBot.gemspec
4
+ gemspec
data/MoonBot.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "MoonBot/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "moonbot"
7
+ s.version = MoonBot::VERSION
8
+ s.authors = ["Blake Williams"]
9
+ s.email = ["blake@baristadesign.com"]
10
+ s.homepage = "https://github.com/BlakeWilliams/MoonBot"
11
+ s.summary = %q{Amazingly simple IRC bot.}
12
+ s.description = %q{Amazingly simple IRC bot that handles everything via plugins with the exception of connecting.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ # s.add_development_dependency "rspec"
21
+ s.add_runtime_dependency "eventmachine"
22
+ end
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,31 @@
1
+ module MoonBot
2
+ class Bot
3
+ attr_reader :host, :port, :plugins, :client
4
+
5
+ def initialize host, port
6
+ @host = host
7
+ @port = port
8
+
9
+ @plugins = {}
10
+ end
11
+
12
+ def start
13
+ EventMachine.run do
14
+ @client = EventMachine.connect @host, @port, Client, Proc.new { |m| self.handle m }
15
+ end
16
+ end
17
+
18
+ def handle message
19
+ @plugins.each do |name, instance|
20
+ instance.send('on_' + message[:command].downcase, message) rescue nil
21
+ instance.send('any', message) if instance.respond_to? 'any'
22
+ end
23
+ end
24
+
25
+ def register plugin
26
+ @plugins[plugin.to_s] = plugin.new(self)
27
+ puts "Loading #{plugin.to_s}"
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ module MoonBot
2
+ class Client < EventMachine::Connection
3
+
4
+ def initialize on_message
5
+ @on_message = on_message
6
+ end
7
+
8
+ def receive_data(data)
9
+ data = data.split "\r\n"
10
+ data.each do |d|
11
+ message = Parser::parse(d)
12
+ @on_message.call(message) if @on_message
13
+ end
14
+ end
15
+
16
+ def derp x
17
+ puts "herp"
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module MoonBot
2
+ class Parser
3
+ def self.parse data
4
+ message = {}
5
+ # if not empty
6
+ if !data.scan(/^:/).empty?
7
+ separated = data.split ':'
8
+ body = separated[1].split ' '
9
+ message[:prefix] = body.shift
10
+ message[:command] = body.shift
11
+ message[:params] = body.shift
12
+ 2.times { separated.shift }
13
+ message[:trailing] = separated.join(":")
14
+ else
15
+ body = data.split ' '
16
+ message[:command] = body.shift
17
+ message[:args] = body
18
+ end
19
+ message
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ # Require all files from plugins directory
2
+ Dir[File.join(File.dirname(__FILE__), 'plugins', '*.rb')].map {|f| require f }
@@ -0,0 +1,24 @@
1
+ module MoonBot::Plugin
2
+ class Auth
3
+ def initialize bot
4
+ @bot = bot
5
+ end
6
+
7
+ def on_notice message
8
+ send_auth if message[:trailing] == '*** No Ident response'
9
+ end
10
+
11
+ def set_user nick, login, name = nil
12
+ @nick = nick
13
+ @login = login
14
+ @name = name || login
15
+ end
16
+
17
+ def send_auth
18
+ puts "Sending User Data"
19
+ auth = "NICK #{@nick}\r\n"
20
+ auth += "USER #{@login} 8 * : #{@name}\r\n"
21
+ @bot.client.send_data auth
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module MoonBot::Plugin
2
+ class Ping
3
+ def initialize bot
4
+ @bot = bot
5
+ end
6
+
7
+ def on_ping message
8
+ pong = "PONG #{message[:args]}"
9
+ @bot.client.send_data pong
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module MoonBot::Plugin
2
+ class Verbose
3
+ def initialize bot
4
+ end
5
+
6
+ def any message
7
+ puts message
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module MoonBot
2
+ VERSION = "0.0.2"
3
+ end
data/lib/MoonBot.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'eventmachine'
2
+ require "fileutils"
3
+ require 'yaml'
4
+
5
+ require "MoonBot/version"
6
+ require 'MoonBot/parser'
7
+ require 'MoonBot/client'
8
+ require 'MoonBot/bot'
data/readme.md ADDED
@@ -0,0 +1,57 @@
1
+ # MoonBot: The stupid modular bot
2
+ MoonBot is my bot "framework" where the bot does nothing but connect to the server, not even play ping pong.
3
+
4
+ ## What does it do?
5
+ Nothing with the exception of the default plugins that you can choose to use. To do anything with the bot you have to write a plugin that responds to the proper command. Right now the built in plugins are Ping, Auth, and Verbose.
6
+
7
+ ## How do I write a plugin?
8
+ Plugins are incredibly simple, all you do is make a class, add an initializer that takes one parameter (bot), and then define methods that respond to the command the server sends. All methods are the command the server sends with "on_" prefixed. So if you want to check every PRIVMSG and respond accordingly you would define a method called "on_privmsg".
9
+
10
+ ```ruby
11
+ class Ping
12
+ def initialize bot
13
+ @bot = bot
14
+ end
15
+
16
+ def on_ping message
17
+ pong = "PONG #{message[:args]}"
18
+ @bot.client.send_data pong
19
+ end
20
+ end
21
+ ```
22
+
23
+ The initializer takes the bot as a paramater so you can access the client to send data as well as access plugins so you can call methods from them (for example you have an authorization plugin that you want to use on the user). There is also the "any" method, if your plugin has this method it will be called every time the server sends back data.
24
+
25
+ ## How do I get started?
26
+ First, Add moonbot to your Gemfile.
27
+
28
+ ```ruby
29
+ source "http://rubygems.org"
30
+ gem 'moonbot'
31
+ ```
32
+
33
+ Second, create a file and start hacking. Here is a simple bot to get you started. If you're making your own plugins in the same directory as your bot, I suggest making a plugins directory and putting them there because in the future MoonBot might have helper commands for loading plugins. Plugins as gems are also a nice decision if you want to share them easily.
34
+
35
+ ```ruby
36
+ require 'moonbot'
37
+ require 'moonbot/plugin'
38
+
39
+ @bot = MoonBot::Bot.new 'irc.server.tld', 6667
40
+ @bot.register MoonBot::Plugin::Ping
41
+ @bot.register MoonBot::Plugin::Verbose
42
+ @bot.register MoonBot::Plugin::Auth
43
+ @bot.plugins["MoonBot::Plugin::Auth"].set_user "Nick", "Name"
44
+ @bot.start
45
+ ```
46
+
47
+ ## Why?
48
+ It was just a stupid idea that I wanted to play with and it turned out not being as stupid as I thought (but is still pretty dumb).
49
+
50
+ ## License
51
+ Copyright (c) 2012 Blake Williams
52
+
53
+ 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:
54
+
55
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
56
+
57
+ 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.
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moonbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Blake Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-12 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: eventmachine
16
+ requirement: &70302262152900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70302262152900
25
+ description: Amazingly simple IRC bot that handles everything via plugins with the
26
+ exception of connecting.
27
+ email:
28
+ - blake@baristadesign.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - MoonBot.gemspec
36
+ - Rakefile
37
+ - lib/MoonBot.rb
38
+ - lib/MoonBot/bot.rb
39
+ - lib/MoonBot/client.rb
40
+ - lib/MoonBot/parser.rb
41
+ - lib/MoonBot/plugin.rb
42
+ - lib/MoonBot/plugins/auth.rb
43
+ - lib/MoonBot/plugins/ping.rb
44
+ - lib/MoonBot/plugins/verbose.rb
45
+ - lib/MoonBot/version.rb
46
+ - readme.md
47
+ homepage: https://github.com/BlakeWilliams/MoonBot
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.12
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Amazingly simple IRC bot.
71
+ test_files: []