Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-- vim:sw=2:et
- #++
- #
- # :title: Shell Plugin for RBot
- #
- # Author:: Yaohan Chen <yaohan.chen@gmail.com>
- # Copyright:: (C) 2008 Yaohan Chen
- # License:: GPLv2
- require 'treetop'
- Treetop.load_string <<END_TREETOP_CODE
- grammar Shell
- rule command
- interpolation /
- simple {
- def execute(&block)
- yield text_value
- end
- }
- end
- rule interpolation
- before:simple? "$(" command ")" after:simple? {
- def depth
- command.depth + 1
- end
- def execute(&block)
- yield before.text_value + command.execute(&block) + after.text_value
- end
- }
- end
- rule simple
- (!"$(" !")" .)+ {
- def depth
- 0
- end
- }
- end
- end
- END_TREETOP_CODE
- class ShellPlugin < Plugin
- def initialize
- @parser = ShellParser.new
- end
- def shell(m, params)
- result = @parser.parse(params[:command]).execute do |s|
- replies = []
- m = fake_message(s, :delegate => false)
- m.send(:define_method, :reply) do |s|
- replies << s
- end
- replies.join(' ')
- end
- m.reply result
- end
- end
- plugin = ShellPlugin.new
- plugin.map 'shell *command'
Add Comment
Please, Sign In to add comment