From 9cf5513b7c15cdf254179e3ab4432ca58050e4b5 Mon Sep 17 00:00:00 2001 From: "Robert \"ar\" Gerus" Date: Thu, 30 May 2013 08:38:37 +0200 Subject: [PATCH] Some documentation + example config file --- config.example.rb | 17 +++++++++++++++++ repost.rb | 5 +++++ run.rb | 7 +++++++ 3 files changed, 29 insertions(+) create mode 100644 config.example.rb diff --git a/config.example.rb b/config.example.rb new file mode 100644 index 0000000..1f6e00f --- /dev/null +++ b/config.example.rb @@ -0,0 +1,17 @@ +Config = { + + :server => { + :host => "irc.freenode.net", + :port => 6667, + :realname => "repost", + }, + + :client => { + :nick => "repost", + :channels => ["#repost-test"] + }, + + # Space separated list of plugin names + :plugins => %w() + +} diff --git a/repost.rb b/repost.rb index e3d5721..9b265c3 100644 --- a/repost.rb +++ b/repost.rb @@ -5,11 +5,16 @@ class Repost < EventMachine::IRC::Client def dispatch_raw_message(message = {}) self.triggers = [] if self.triggers.nil? self.triggers.each do |trigger| + # Having per-command lists of blocks of code to call would be + # faster, but it's not a problem for now. Might refactor this loop + # and register_trigger() method if it comes to that. trigger[:code].call(message) if message[:command] == trigger[:command] end end def register_trigger(command, &code) + # Make sure the Array of triggers is not empty and then append to it a + # Hash containing the command it should be called at and the code self.triggers = [] if self.triggers.nil? self.triggers << {:command => command, :code => code} end diff --git a/run.rb b/run.rb index 41ca0f8..4cb42c4 100755 --- a/run.rb +++ b/run.rb @@ -3,6 +3,7 @@ require 'rubygems' require 'eventmachine' require 'em-irc' +# Our config file is just a single Hash, nothing too fancy. require_relative 'config' require_relative 'repost' @@ -23,12 +24,18 @@ repost = Repost.new do end end + # The provided triggers/callbacks for actions are limited - there are no + # quit/part callbacks, for example - and we'd have to catch raw messages + # anyway. And since we're doing that, we can just go ahead and hook up to + # raw callbacks exclusively. on :raw do |message| self.dispatch_raw_message message end end +# Loading plugins here, because we need the repost object in scope to be able +# to do anything. Config[:plugins].each do |plugin| puts "Loading plugin #{plugin}" eval File.read(File.dirname($0) + "/plugins/#{plugin}.rb")