3 local irc = require 'irc'
10 local function create_env()
14 collectgarbage = collectgarbage,
17 getmetatable = getmetatable,
19 loadstring = loadstring,
28 setmetatable = setmetatable,
34 coroutine = coroutine,
42 eval = function(channel, from, code)
43 code = code:gsub("^=", "return ")
44 local fn, err = loadstring(code)
46 irc.say(channel.name, from .. ": Error loading code: " .. code .. err:match(".*(:.-)$"))
49 setfenv(fn, envs[from])
50 local result = {pcall(fn)}
51 local success = table.remove(result, 1)
53 irc.say(channel.name, from .. ": Error running code: " .. code .. result[1]:match(".*(:.-)$"))
55 if result[1] == nil then
56 irc.say(channel.name, from .. ": nil")
58 irc.say(channel.name, from .. ": " .. table.concat(result, ", "))
63 clear = function(channel, from)
64 irc.say(channel.name, from .. ": Clearing your environment")
65 envs[from] = create_env()
67 help = function(channel, from, arg)
69 irc.say(channel.name, from .. ": Commands: !clear, !eval, !help")
70 elseif arg == "eval" then
71 irc.say(channel.name, from .. ": Evaluates a Lua statement in your own persistent environment")
72 elseif arg == "clear" then
73 irc.say(channel.name, from .. ": Clears your personal environment")
78 irc.register_callback("connect", function()
82 irc.register_callback("channel_msg", function(channel, from, message)
83 message = message:gsub("^" .. nick .. "[:,>] ", "!eval ")
84 local is_cmd, cmd, arg = message:match("^(!)([%w_]+) ?(.-)$")
85 if is_cmd and commands[cmd] then
86 envs[from] = envs[from] or create_env()
87 commands[cmd](channel, from, arg)
91 irc.register_callback("nick_change", function(from, old_nick)
92 if envs[old_nick] and not envs[from] then
93 envs[from] = envs[old_nick]
98 irc.connect{network = "irc.freenode.net", nick = nick}