Created
June 15, 2011 10:01
-
-
Save smiler/1026827 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class Plugin | |
attr_reader :triggers | |
attr_reader :hooks | |
def self.subclasses() | |
classes = [] | |
ObjectSpace.each_object(Class) do |c| | |
next unless c.ancestors.include?(self) and (c != self) | |
classes << c | |
end | |
classes | |
end | |
def name | |
self.class.name | |
end | |
end | |
class Plugin_Handler | |
def initialize | |
@events = [:on_msg, :on_privmsg, :on_ctcp_version, :on_ctcp_ping, :on_ctcp, | |
:on_join, :on_part, :on_quit, :on_kick, :on_mode, :on_topic] | |
@loaded_plugins = [] # list of loaded plugins | |
load_plugins() | |
end | |
def load_plugin(file) | |
# load file | |
begin | |
load(file) | |
rescue Exception => e | |
puts "Failed to load " + file + ":" | |
puts e.message() | |
nil | |
end | |
end | |
def load_plugins() | |
# load all files in plugin directory | |
files = Dir.glob('plugins/*.rb') | |
files.each { |file| | |
load_plugin(file) | |
} | |
# create and init each loaded plugin class | |
Plugin.subclasses().each { |plugin| | |
begin | |
p = plugin.new | |
p.on_load() if p.respond_to?("on_load") | |
rescue Exception => e | |
puts "Error when loading plugin " + p.name + ":" | |
puts " " + e.message() | |
puts e.backtrace.join("\n") | |
puts p.name + " not loaded." | |
next | |
end | |
@loaded_plugins << p | |
puts "Loaded " + p.name | |
} | |
end | |
def unload_plugins() | |
@loaded_plugins.map! { |plugin| | |
name = plugin.name | |
begin | |
plugin.on_unload() if p.respond_to?("on_unload") | |
rescue Exception => e | |
puts "Error when unloading plugin " + plugin.name + ":" | |
puts " " + e.message() | |
puts plugin.name + " not cleanly unloaded" | |
next | |
end | |
puts "Unloaded " + name | |
nil | |
} | |
@loaded_plugins.compact! | |
end | |
def event(type, args) | |
case type | |
when :on_ctcp_ping | |
handle_ctcp_ping(args[:nick], args[:user], args[:host], args[:message]) | |
when :on_ctcp_version | |
handle_ctcp_version(args[:nick], args[:user], args[:host]) | |
when :on_privmsg | |
handle_privmsg(args[:nick], args[:user], args[:host], args[:message]) | |
when :on_msg | |
handle_msg(args[:nick], args[:user], args[:host], args[:channel], | |
args[:message]) | |
when :on_join | |
handle_join(args[:nick], args[:user], args[:host], args[:channel]) | |
when :on_part | |
handle_part(args[:nick], args[:user], args[:host], args[:channel], | |
args[:message]) | |
when :on_kick | |
handle_kick(args[:nick], args[:user], args[:host], args[:channel], | |
args[:knick], args[:message]) | |
when :on_quit | |
handle_quit(args[:nick], args[:user], args[:host], args[:message]) | |
when :on_mode | |
handle_mode(args[:nick], args[:user], args[:host], args[:channel], | |
args[:mode], args[:args]) | |
when :on_smode | |
handle_smode(args[:server], args[:channel], args[:mode], args[:args]) | |
else | |
puts "Recived unknown event #{type} with argument " + args.to_s | |
end | |
end | |
def handle_ctcp_ping(nick, user, host, message) | |
puts "[ CTCP PING from #{nick}!#{user}@#{host} ]" | |
$irc.putserv "NOTICE #{nick} :\001PING #{message}\001" | |
end | |
def handle_ctcp_version(nick, user, host) | |
puts "[ CTCP VERSION from #{nick}!#{user}@#{host} ]" | |
$irc.putserv "NOTICE #{nick} :\001VERSION Foobot v0.42\001" | |
end | |
def handle_privmsg(nick, user, host, target, message) | |
puts "[ PRIVMSG from #{nick}!#{user}@#{host}: #{message} ]" | |
end | |
def handle_msg(nick, user, host, channel, message) | |
puts "[ MSG to #{channel} from #{nick}!#{user}@#{host}: #{message} ]" | |
# Get possible command by | |
command = message.split[0] | |
if command == ($irc.nick + ":") | |
command = message.split[1] # command is prefixed by my nick | |
end | |
# Handle commands if in command channel only | |
if command == ".reload" and $control_channels.include?(channel) | |
puts "[ Reloading plugins ]" | |
unload_plugins() | |
load_plugins() | |
$irc.putchan(channel, "Plugins reloaded") | |
else | |
# Execute any registered handlers on message | |
@loaded_plugins.each { |p| | |
begin | |
if p.hooks and p.hooks[:on_msg] | |
p.send(p.hooks[:on_msg], nick, user, host, channel, message) | |
end | |
rescue => e | |
puts "Error handling on_msg in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
begin | |
if p.triggers and p.triggers[command] | |
# remove command part from message before sending it | |
message = message.split.drop(1).join(" ") | |
p.send(p.triggers[command], nick, user, host, channel, message) | |
end | |
rescue => e | |
puts "Error handling command \"#{command}\" in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
} | |
end | |
end | |
def handle_join(nick, user, host, channel) | |
puts "[ JOIN to #{channel} from #{nick}!#{user}@#{host} ]" | |
@loaded_plugins.each { |p| | |
begin | |
if p.hooks and p.hooks[:on_join] | |
p.send(p.hooks[:on_join], nick, user, host, channel) | |
end | |
rescue => e | |
puts "Error handling on_join in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
} | |
end | |
def handle_part(nick, user, host, channel, message) | |
if message == nil | |
puts "[ PART from #{channel} by #{nick}!#{user}@#{host} ]" | |
else | |
puts "[ PART from #{channel} by #{nick}!#{user}@#{host} (#{message}) ]" | |
end | |
@loaded_plugins.each { |p| | |
begin | |
if p.hooks and p.hooks[:on_part] | |
p.send(p.hooks[:on_part], nick, user, host, channel) | |
end | |
rescue => e | |
puts "Error handling on_part in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
} | |
end | |
def handle_quit(nick, user, host, message) | |
if message == nil | |
puts "[ QUIT by #{nick}!#{user}@#{host} ]" | |
else | |
puts "[ QUIT by #{nick}!#{user}@#{host} (#{message}) ]" | |
end | |
@loaded_plugins.each { |p| | |
begin | |
if p.hooks and p.hooks[:on_quit] | |
p.send(p.hooks[:on_quit], nick, user, host, message) | |
end | |
rescue => e | |
puts "Error handling on_quit in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
} | |
end | |
def handle_kick(nick, user, host, channel, knick, message) | |
puts "[ KICK: #{knick} from #{channel} (#{message}) by #{nick}!#{user}@#{host} ]" | |
@loaded_plugins.each { |p| | |
begin | |
if p.hooks and p.hooks[:on_kick] | |
p.send(p.hooks[:on_kick], nick, user, host, knick, channel, message) | |
end | |
rescue => e | |
puts "Error handling on_kick in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
} | |
end | |
def handle_mode(nick, user, host, channel, mode, args) | |
if args == nil | |
puts "[ MODE on #{channel} from #{nick}!#{user}@#{host}: #{mode} ]" | |
else | |
puts "[ MODE on #{channel} from #{nick}!#{user}@#{host}: #{mode} #{args} ]" | |
end | |
@loaded_plugins.each { |p| | |
begin | |
if p.hooks and p.hooks[:on_mode] | |
p.send(p.hooks[:on_mode], nick, user, host, channel, mode, args) | |
end | |
rescue => e | |
puts "Error handling on_mode in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
} | |
end | |
def handle_smode(server, channel, mode, args) | |
if args == nil | |
puts "[ SMODE on #{channel} from #{server}: #{mode} ]" | |
else | |
puts "[ SMODE on #{channel} from #{server}: #{mode} #{args} ]" | |
end | |
@loaded_plugins.each { |p| | |
begin | |
if p.hooks and p.hooks[:on_smode] | |
p.send(p.hooks[:on_smode], server, channel, mode, args) | |
end | |
rescue => e | |
puts "Error handling on_smode in plugin " + p.name + ":" | |
puts e.message() | |
puts e.backtrace.join("\n") | |
end | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment