File: evalbot.nb

package info (click to toggle)
nadoka 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 492 kB
  • sloc: ruby: 3,154; makefile: 8; sh: 1
file content (49 lines) | stat: -rw-r--r-- 1,108 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*-ruby-*-
#
# Copyright (c) 2004-2005 SASADA Koichi <ko1 at atdot.net>
#
# This program is free software with ABSOLUTELY NO WARRANTY.
# You can re-distribute and/or modify this program under
# the same terms of the Ruby's license.
#
#
# ruby evaluator on IRC
#
# $Id$
#
require 'timeout'
class EvalBot < Nadoka::NDK_Bot
  EvalNick = 'nadoka_eval'
  
  def on_client_privmsg client, ch, message
    if ch == EvalNick
      ans = eval_message(message)
      msg = Cmd.privmsg(@state.nick, 'ans: ' + ans)
      client.send_to_client client.add_prefix(msg, EvalNick)
      raise ::Nadoka::NDK_BotSendCancel
    end
  end

  def on_nadoka_command client, command, *params
    if command == 'eval'
      msg = Cmd.privmsg(@state.nick, 'Hello, this is ruby evaluator')
      client.send_to_client client.add_prefix(msg, EvalNick)
      raise ::Nadoka::NDK_BotSendCancel
    end
  end

  def eval_message message
    begin
      ans = Thread.new{
        $SAFE = 4
        timeout(3){
          Kernel.eval(message)
        }
      }.value.to_s
    rescue Exception => e
      ans = e.message
    end
  end
end