Skip to content

Instantly share code, notes, and snippets.

@JanneSalokoski
Last active December 19, 2015 08:59
Show Gist options
  • Save JanneSalokoski/5929234 to your computer and use it in GitHub Desktop.
Save JanneSalokoski/5929234 to your computer and use it in GitHub Desktop.
ExpBot II (Experimental Bot V2.1) - An simple IRC bot written in Lua - the programming language.
-- ExpBot II (Experimental Bot V2.1) - An simple IRC bot written in Lua - the programming language.
-- jenn4 - salokoski.janne@gmail.com
print("<< Please, enter your password >>")
require "socket"
s = socket.tcp()
-- Passwords are received from the standard input.
passLine = io.read()
-- Configuration.
network = "chat.eu.freenode.net"
nick = "ExpBot"
user = "jenn4"
mode = "8"
realname = "ExpBot"
channels = "#jenn4"
prefix = "!!"
-- Automatically send newlines '\r\n'.
function send(str)
s:send(str .. "\r\n")
end
-- Tell the IRC server about us.
-- See RFC 1459 for extra info.
function sendInitialConfig()
print("<< Sending configuration >>")
send("PASS " .. passLine)
send("NICK " .. nick)
send("USER " .. user .. " " .. mode .. " * :" .. realname)
send("JOIN " .. channels)
end
--Interprets the data, handles PINGs.
function handleRawIRC()
st = output
function handler()
input = {}
table.insert(input, st:match("%s:(.+)"))
if st:match(":.* :") then
st = st:match(":(.*) :")
end
for word in st:gmatch("%S+") do
table.insert(input, word)
end
if input[2]:match("%S*!") then
table.insert(input, input[2]:match("^(%S*)!"))
input[2] = input[2]:match("!(.*)")
end
return(input)
end
table.foreach(handler(), print) -- Debug!
end
--Listens for commands and does what they require.
function listenCommands()
function pingPong()
-- PING :leguin.freenode.net
-- 1 leguin.freenode.net
-- 2 PING
-- 3 :leguin.freenode.net
if input[2]:match("PING") then
--send("PONG :" .. output:match("^PING :(.*)"))
send("PONG " .. input[3])
end
--"Load" commands. (Call their functions.)
pingPong()
end
end
--Actually connect to the server.
function init()
s:connect(network, 6667)
sendInitialConfig()
while true do
output = s:receive("*l")
print(output) -- Debug!
handleRawIRC()
listenCommands()
end
end
--Launch.
init()
st = ":jenn4!~jenn4@unaffiliated/jenn4 PRIVMSG #powder :Hi, all!"
function handler()
input = {}
table.insert(input, st:match("%s:(.+)"))
if st:match(":.* :") then
st = st:match(":(.*) :")
end
for word in st:gmatch("%S+") do
table.insert(input, word)
end
if input[2]:match("%S*!") then
table.insert(input, input[2]:match("^(%S*)!"))
input[2] = input[2]:match("!(.*)")
end
return(input)
end
table.foreach(handler(), print) -- Debug!
io.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment