2006-01-31

Instant Bot

I was bored, result: Low-byte-count lua-bot, to meet the specs on http://www.we11er.co.uk/ircbots/.

This could be made shorter by removing quite a lot of the whitespace, including, possibly the \rs. It could be made even shorter by shortening the variables names. It could be made faster by nesting the if-end blocks. It could (quickly) be made more flexible by not hardcoding the command and it's response.

Possibly the easiest way to make it shorter would be to export some of the code to a library (such as perl's MNet::IRC), which is, imho, cheating.

Etc.

Anyway, the code:

socket=require("socket")
tcp=socket.tcp()
tcp:connect("irc.uwcs.co.uk", 6667)
tcp:send("USER luabot 8 * :Faux's LuaBOT\r\nNICK LuaBOT\r\nPRIVMSG NickServ IDENTIFY password")

while true do line = tcp:receive() if line == nil then break end

,, pongkey = line:find("^PING(.*)$") if pongkey ~= nil then tcp:send("PONG" .. pongkey .. "\r\n") end

,, code = line:find("^[^ ]+ ([0-9]+) ") if code == "376" then tcp:send("JOIN #luabot\r\n") end

,, channel, args = line:find("^:[^ ]+ [A-Z0-9]+( [^ ]+ ):?!say (.*)$") if (args ~= nil) then tcp:send("PRIVMSG" .. channel .. ":" .. args .. "\r\n") end end

Have fun.


Commenting is disabled for this post.

Read more of Faux' blog