diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4b064d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bot.yml \ No newline at end of file diff --git a/Readme.rdoc b/Readme.rdoc index 2e8fa9c..211de6a 100644 --- a/Readme.rdoc +++ b/Readme.rdoc @@ -4,33 +4,22 @@ A community built chat bot, just for fun. Send pull requests! === Running it -It runs with straight Ruby. You'll need the Cinch and Cinch-Identify gems and Ruby 1.9, and you'll want to specify the nick and channel you want to use in a YAML file: +It runs with straight Ruby. You'll need the Cinch, Cinch-Identify and ActiveSupport gems and Ruby 1.9, and you'll want to specify the nick and channel you want to use in a YAML file: - bot.yml +[bot.yml]: settings: - nick: my_bot - channel: ecruby - nickserv_pass: sekrit - -If you plan to use the Karma plugin, you'll need a bit more: - - bot.yml - settings: - nick: my_bot - channel: ecruby - nickserv_pass: sekrit - persistence_url: ecruby.org - username: some_user - password: sekrit - -Then just run it with + server: 'irc.freenode.org' + nick: &nick 'my_bot' + channel: '#ecruby' + plugins: + "Repeater": + "OverAchieverListener": + "Cinch::Plugins::Identify": + require: 'cinch/plugins/identify' + nick: *nick + nickserv_pass: sekrit + type: nickserv + +Then just run it with: ruby chatbot.rb - - -== Things we can do - -* More games -* Database persistence -* Record links to Tumblr? -* Whatever else sounds like fun diff --git a/chatbot.rb b/chatbot.rb index 06708ba..9b81847 100644 --- a/chatbot.rb +++ b/chatbot.rb @@ -1,38 +1,27 @@ require 'rubygems' require 'cinch' -require 'cinch/plugins/identify' require 'yaml' +require './helpers' -begin - $settings = YAML.load(File.read("bot.yml")) -rescue - puts "create bot.yml and populate it with values. See the readme file!" -end - -$help_messages = [] +raise "create bot.yml and populate it with values. See the readme file!" unless File.exists?("bot.yml") -require './plugins/karma' -require './plugins/link_catcher' -require './plugins/repeater' +initialize_globals! +cinch_plugins = include_plugins($settings["settings"]['plugins']) -@irc = Cinch::Bot.new do - +@irc = Cinch::Bot.new do configure do |c| - c.server = "irc.freenode.org" - c.nick = $settings["settings"]["nick"] - c.channels = [$settings["settings"]["channel"]] - c.plugins.plugins = [Karma, LinkCatcher, Repeater, Cinch::Plugins::Identify] - c.plugins.options[Cinch::Plugins::Identify] = { - :username => $settings['settings']['nick'], - :password => $settings['settings']['nickserv_pass'], - :type => :nickserv - } + c.server = $settings["settings"]["server"] + c.nick = $settings["settings"]["nick"] + c.channels = [$settings["settings"]["channel"]] + c.plugins.plugins = cinch_plugins.keys + cinch_plugins.each do |plugin_class, options| + c.plugins.options[plugin_class] = options + end end - + on :message, /^!help/ do |m| - $help_messages.each{|message| m.user.send message } + $help_messages.each{ |message| m.user.send message } end - end @irc.start diff --git a/helpers.rb b/helpers.rb new file mode 100644 index 0000000..5688082 --- /dev/null +++ b/helpers.rb @@ -0,0 +1,26 @@ +require 'active_support' + +def include_plugins(plugin_list = {}) + plugin_list ||= {} + + plugins = plugin_list.inject({}) do |plugin_classes, plugin| + plugin_name, plugin_options = plugin + underscored_name = ActiveSupport::Inflector.underscore(plugin_name) + if plugin_options && path = plugin_options.delete('require') + require path + else + require "./plugins/#{underscored_name}" + end + plugin_class = ActiveSupport::Inflector.constantize(plugin_name) + plugin_classes[plugin_class] = (plugin_options || {}) + + plugin_classes + end + + return plugins +end + +def initialize_globals! + $settings = YAML.load(File.read("bot.yml")) + $help_messages = [] +end diff --git a/plugins/over_achiever.rb b/plugins/over_achiever.rb new file mode 100644 index 0000000..4b91ad1 --- /dev/null +++ b/plugins/over_achiever.rb @@ -0,0 +1,22 @@ +require 'net/http' +require "uri" + +module OverAchiever + def self.generate_badge(points, msg) + email = "m8r-pgofgf@mailinator.com" + + uri = URI.parse("http://www.justachieveit.com/jai/code.jsp") + response = Net::HTTP.post_form(uri, { type: 1, email: email, text: msg, score: points }) + + if response.is_a?(Net::HTTPRedirection) && response['location'] + badge_response = Net::HTTP.get_response(URI.parse(response['location'])) + if badge_response.is_a?(Net::HTTPOK) && matches = badge_response.body.match(/src="(http:\/\/cdn\.justachieveit\.com\/[^"]+)"/) + matches[1] + else + "Sorry {{user}} I can't find the badge's URL" + end + else + "Sorry {{user}} I couldn't figure out where to get the badge's URL" + end + end +end diff --git a/plugins/over_achiever_listener.rb b/plugins/over_achiever_listener.rb new file mode 100644 index 0000000..6fc4da7 --- /dev/null +++ b/plugins/over_achiever_listener.rb @@ -0,0 +1,21 @@ +require_relative './over_achiever' + +class OverAchieverListener + include Cinch::Plugin + + $help_messages << "achieve: Generate an achievement with points" + $help_messages << "achievement: Generate an achievement with points" + $help_messages << "achievement unlocked: Generate an achievement with points" + + listen_to :channel + + def listen(m) + if matches = m.message.match(/^(?:ach[ie]{2}ve(?:ment)?|ach[ie]{2}vement unlocked): (\d*)(\D.*)/) + points = matches[1].empty? ? 5 : matches[1] + msg = matches[2].strip + if badge = OverAchiever.generate_badge(points, msg) + m.reply badge.gsub('{{user}}', m.user.nick) + end + end + end +end