1
0

irb.tcl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #
  2. # 0.2 - ???
  3. # - fix error when rehash/restart if socket isnt open
  4. #
  5. # 0.1 - May 15 2010
  6. # - initial release
  7. #
  8. # by horgh (www.summercat.com)
  9. #
  10. # A _VERY UNSAFE_ wrapper for irb <-> irc via eggdrop
  11. #
  12. # Setup:
  13. # - make sure you set/check the 3 variables (channel, command char, irb path)
  14. #
  15. # Usage:
  16. # - {command_char}reset to get a fresh irb session
  17. #
  18. # - any commands prefixed with command_char are sent to irb and the result is
  19. # posted to the channel
  20. # - e.g.
  21. # <@horgh> 'test
  22. # <@Yorick> Starting new irb session...
  23. # <@Yorick> => ArgumentError: wrong number of arguments
  24. # <@Yorick> => from (irb):1:in `test'
  25. # <@Yorick> => from (irb):1
  26. #
  27. # BUGS:
  28. # - since "=>" isn't shown from the open call for some reason (perhaps it goes
  29. # to stderr or something, i'm not sure), some results that print on same line
  30. # do not display nicely, such as:
  31. # '5.times { print "*" }
  32. # results in "=> *****5" whereas it should be "*****=> 5" from the prompt
  33. #
  34. namespace eval irb {
  35. # Settings
  36. # channel to respond to irb commands / send output
  37. set channel #YOUR_CHANNEL
  38. # system path to irb binary
  39. set irb {/usr/local/bin/irb}
  40. # prefix character for sending data to irb
  41. set command_char "'"
  42. #set output_cmd cd::putnow
  43. set output_cmd putserv
  44. # You shouldn't need to edit anything below here
  45. set irb_chan []
  46. # store commands entered here so we don't output them
  47. # they are deleted as they come up from reading irb output
  48. set cmd_cache []
  49. bind pubm -|- "*" irb::put
  50. bind pub -|- "${command_char}reset" irb::reset
  51. bind evnt -|- "prerestart" irb::end
  52. bind evnt -|- "prerehash" irb::end
  53. }
  54. proc irb::put {nick uhost hand chan argv} {
  55. if {$chan != $irb::channel} { return }
  56. if {[string index $argv 0] != $irb::command_char} { return}
  57. set cmd [string range $argv 1 end]
  58. if {$cmd == "reset" } { return }
  59. if {$cmd == ""} { return }
  60. if {$irb::irb_chan == []} {
  61. setup_irb
  62. }
  63. lappend irb::cmd_cache $cmd
  64. puts $irb::irb_chan $cmd
  65. }
  66. proc irb::reset {nick uhost hand chan argv} {
  67. $irb::output_cmd "PRIVMSG $irb::channel :Closing irb session."
  68. irb::end
  69. }
  70. proc irb::setup_irb {} {
  71. $irb::output_cmd "PRIVMSG $irb::channel :Starting new irb session..."
  72. set irb::irb_chan [open "|${irb::irb}" r+]
  73. fconfigure $irb::irb_chan -blocking 1 -buffering line
  74. # call irb::output when data to be read
  75. fileevent $irb::irb_chan readable irb::output
  76. }
  77. proc irb::output {} {
  78. set output [gets $irb::irb_chan]
  79. set output [string map {\t " "} $output]
  80. # check if it is a command sent to irb rather than a result (to not print)
  81. set index [lsearch -exact $irb::cmd_cache $output]
  82. if {$index >= 0} {
  83. set irb::cmd_cache [lreplace $irb::cmd_cache $index $index]
  84. } else {
  85. $irb::output_cmd "PRIVMSG $irb::channel :=> $output"
  86. }
  87. }
  88. # We close channel before restart/rehash
  89. proc irb::end {args} {
  90. if {$irb::irb_chan == ""} { return }
  91. close $irb::irb_chan
  92. set irb::irb_chan []
  93. }
  94. putlog "irb.tcl loaded"