Guest User

Untitled

a guest
Apr 26th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #-- vim:sw=2:et
  2. #++
  3. #
  4. # :title: Shell Plugin for RBot
  5. #
  6. # Author:: Yaohan Chen <yaohan.chen@gmail.com>
  7. # Copyright:: (C) 2008 Yaohan Chen
  8. # License:: GPLv2
  9.  
  10.  
  11. require 'treetop'
  12.  
  13. Treetop.load_string <<END_TREETOP_CODE
  14.  
  15. grammar Shell
  16. rule command
  17. interpolation /
  18. simple {
  19. def execute(&block)
  20. yield text_value
  21. end
  22. }
  23. end
  24.  
  25. rule interpolation
  26. before:simple? "$(" command ")" after:simple? {
  27. def depth
  28. command.depth + 1
  29. end
  30.  
  31. def execute(&block)
  32. yield before.text_value + command.execute(&block) + after.text_value
  33. end
  34. }
  35. end
  36.  
  37. rule simple
  38. (!"$(" !")" .)+ {
  39. def depth
  40. 0
  41. end
  42. }
  43. end
  44. end
  45.  
  46. END_TREETOP_CODE
  47.  
  48.  
  49. class ShellPlugin < Plugin
  50. def initialize
  51. @parser = ShellParser.new
  52. end
  53.  
  54. def shell(m, params)
  55. result = @parser.parse(params[:command]).execute do |s|
  56. replies = []
  57. m = fake_message(s, :delegate => false)
  58. m.send(:define_method, :reply) do |s|
  59. replies << s
  60. end
  61. replies.join(' ')
  62. end
  63. m.reply result
  64. end
  65. end
  66.  
  67.  
  68. plugin = ShellPlugin.new
  69. plugin.map 'shell *command'
Add Comment
Please, Sign In to add comment