repost/plugins/commands.rb

91 lines
2.7 KiB
Ruby
Raw Permalink Normal View History

2013-06-04 19:31:27 +00:00
include EventMachine::IRC::Commands
# yay, monkey patching
class Repost
attr_accessor :commands
def register_command(keyword, &code)
2013-06-09 13:19:06 +00:00
begin
access_level = Config[:commands][keyword.to_sym][:access_level].nil? ? 5 : Config[keyword.to_sym][:access_level]
rescue NoMethodError
access_level = 5
end
self.commands = [] if self.commands.nil?
self.commands << {
:keyword => keyword,
:access_level => access_level,
:code => code
}
end
2014-03-25 06:19:40 +00:00
def unregister_command(keyword)
2014-03-25 06:21:49 +00:00
self.commands.reject! { |item| item[:keyword] == keyword }
2014-03-25 06:19:40 +00:00
end
2013-06-09 13:19:06 +00:00
# just a stub for now
def access_level(user, target)
99
end
2013-06-09 16:21:55 +00:00
def load_commands
2013-06-09 14:45:30 +00:00
self.commands = []
Dir.glob(File.dirname($0) + "/plugins/commands/*.rb") { |filename|
load filename
}
end
end
2013-06-09 16:21:55 +00:00
Client.load_commands
Client.register_trigger("PRIVMSG") { |msg|
2013-06-04 22:48:50 +00:00
# who sent the message
2013-06-04 19:31:27 +00:00
src_nick = msg[:prefix].split('!').first
2013-06-04 22:48:50 +00:00
# what was it
2013-06-04 19:31:27 +00:00
message = msg[:params][1]
2013-06-04 22:48:50 +00:00
# Was the message sent directly to us...
2013-06-04 19:31:27 +00:00
if msg[:params][0] == Config[:client][:nick] then
target = src_nick
reply_prefix = ""
2013-06-04 22:48:50 +00:00
# or to a channel (probably)
2013-06-04 19:31:27 +00:00
else
target = msg[:params][0]
reply_prefix = "#{src_nick}: "
end
2013-06-04 22:48:50 +00:00
# Did the message contain our nick with ":" "completion character"?
# If so, use the second word as a command and everything else as arguments
# array
2013-06-04 19:31:27 +00:00
if message.split.first == Config[:client][:nick] + ":" then
command = message.split[1]
arguments = message.split[2..-1]
else
if not message.split.first.nil? then
# did the message start with a :?
# If so, use everything after ":" from the first word as a command and
# all the other words as arguments array
if message.split.first[0] == ":" then
command = message.split.first[1..-1]
arguments = message.split[1..-1]
end
else
command = ""
arguments = ""
2013-06-04 19:31:27 +00:00
end
end
2013-06-04 22:48:50 +00:00
# don't do anything if there are no commands registered or this isn't a
# command call
Client.commands.each do |cmd|
2013-06-04 22:48:50 +00:00
# couldn't inline the if into second argument of Client.privmsg() call
if command.downcase == cmd[:keyword] then
2013-06-09 13:19:06 +00:00
next if cmd[:access_level] > Client.access_level(msg[:prefix], target)
retval = cmd[:code].call(arguments)
# if the command returned a String, we want to show it
Client.privmsg(target, reply_prefix + retval) if retval.instance_of? String
end
end if not ( Client.commands.nil? or command.nil? )
2013-06-04 19:31:27 +00:00
}