Skip to content

Commit

Permalink
plugin api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aktowns committed Oct 28, 2011
1 parent 5a3e00f commit 122894a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.markdown
Expand Up @@ -49,12 +49,12 @@ Files: [ helloworld.rb ]
~~~~~ ruby
class HelloworldPlugin < Mirai::Plugin
def on_register # Called when the plugin is initialized
add_channel_handler(/^#{@trigger}talktome/, :test_handler) # ^talktome
add_channel_handler(/^#{@trigger}talktome (.*)$/, :test_handler) # ^talktome
add_web_handler(/^\/helloworld/, :test_web_handler) # http://0.0.0.0:3000/helloworld
end

def test_handler(userhash, channel, matches)
privmsg channel, "Well, Hello World! #{userhash[:nick]}"
def test_handler(info, what)
reply "Well, Hello World! #{info[:nick]}. I don't know what #{what} is!"
end

def test_web_handler(env)
Expand Down
2 changes: 2 additions & 0 deletions config.yml
Expand Up @@ -16,6 +16,8 @@ Plugins:
Restrict: ashleyis!oohbouncy@*
- Plugin: regex-plugin # 'core' plugins are stored in mirai/plugins/
Update: core
# - Plugin: ey-plugin
# Update: core
# - Plugin: test-http # a plugin can auto update from a website..
# Update: http
# URL: http://www.google.com
Expand Down
2 changes: 1 addition & 1 deletion mirai/core.rb
Expand Up @@ -93,7 +93,7 @@ def on_channelmessage userhash, channel, message
puts "#{userhash[:nick]}->#{channel} >> #{message}"
@handlers.each do |handle, val|
if (message.match(handle))
val[:obj].send(val[:method], userhash, channel, message.match(handle))
val[:obj].send(:mirror_handle, val[:method], userhash, channel, message.match(handle))
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions mirai/plugin.rb
Expand Up @@ -23,5 +23,17 @@ def add_channel_handler handler, callback
def add_web_handler handler, callback
@handler.register_web_handler(handler, self, callback)
end

def reply message
privmsg @channel, message
end

def mirror_handle to, userhash, channel, matches
@channel = channel
userhash[:channel] = channel
userhash[:raw] = matches[0]
userhash[:matches] = matches
send to, userhash, *matches[1..-1]
end
end
end
12 changes: 6 additions & 6 deletions plugins/regex-plugin/regex.rb
Expand Up @@ -4,14 +4,14 @@ def on_register
add_channel_handler(/^s\/(.*)([^\\\/]*\/)(.*)([^\\\/]*\/)$/, :regex_handler)
add_channel_handler(/^(?!^s\/)(.*)$/, :buffer_handler)
end
def buffer_handler userhash, channel, matches
@buffer << {:nick => userhash[:nick], :message => matches[1], :channel => channel}
def buffer_handler info, all
@buffer << {:nick => info[:nick], :message => all, :channel => info[:channel]}
@buffer = @buffer[@buffer.length-50..-1] if @buffer.length > 50
end
def regex_handler userhash, channel, matches
targetline = @buffer.reverse.find {|line| line[:message].match(/#{matches[1]}/) && line[:channel] == channel }
def regex_handler info, one, two, three, four
targetline = @buffer.reverse.find {|line| line[:message].match(/#{one}/) && line[:channel] == info[:channel] }
return if targetline.nil?
targetline[:message].gsub!(/#{matches[1]}/, matches[3])
privmsg channel, "<#{targetline[:nick]}> #{targetline[:message]}"
targetline[:message].gsub!(/#{one}/, three)
reply "<#{targetline[:nick]}> #{targetline[:message]}"
end
end
4 changes: 2 additions & 2 deletions plugins/test-plugin/test.rb
Expand Up @@ -3,8 +3,8 @@ def on_register
add_channel_handler(/^#{@trigger}test/, :test_handler)
add_web_handler(/^\/testing/, :test_web_handler)
end
def test_handler(userhash, channel, matches)
privmsg channel, "Yes #{userhash[:nick]} this is a test."
def test_handler(info)
reply "Yes #{info[:nick]} this is a test."
end

def test_web_handler(env)
Expand Down

0 comments on commit 122894a

Please sign in to comment.