File: shellbot.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 (59 lines) | stat: -rw-r--r-- 1,259 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
50
51
52
53
54
55
56
57
58
59
# -*-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.
#
#
# shell command bot
#
# $Id$
#

require 'timeout'
require 'kconv'

class ShellBot < Nadoka::NDK_Bot
  ShellNick = 'nadoka_shell'
  
  def on_client_privmsg client, ch, message
    
    if ch == ShellNick
      ans = exec_shell(message)
      ans.each{|line|
        msg = Cmd.privmsg(@state.nick, 'ans: ' + line)
        client.send_to_client client.add_prefix(msg, ShellNick)
      }
      raise ::Nadoka::NDK_BotSendCancel
    end
  end

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

  def exec_shell message
    begin
      ans = Thread.new{
        begin
          timeout(3){
            str = `#{message}`.to_s
            str.tojis
          }
        rescue Exception => e
          e.message
        end
        }.value
    rescue Exception => e
      ans = e.message
    end
  end
end