Last active
December 13, 2015 19:58
-
-
Save JanneSalokoski/4965977 to your computer and use it in GitHub Desktop.
A lightweight IRC bot made in Lua using LuaSockets. Can be ran even on Android devices. utility commands are still under development, but the bot answers PING messages, answers to "Hi!" 's, and parses PRIVMSG's.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" --luaBot | |
s = socket.tcp() | |
passLine = io.read() | |
--[initial configuration] | |
network = "chat.eu.freenode.net" | |
nick = "jenn4|luabot" | |
user = nick mode = "8" | |
realname = "luaBot" | |
channels = "#jenn4" -- You can stack channels like #chan1,chan2 with no spaces! | |
prefix = "!!" | |
msg = {} | |
--[/initial configuration] | |
function sendInitialConfig() | |
print("<< Sending configuration >>") | |
s:send("PASS " .. passLine .. "\r\n") | |
s:send("NICK " .. nick .. "\r\n") -- send nickname | |
s:send("USER " .. user .. " " .. mode .. " * " .. " :" .. realname .. "\r\n") -- send USER command | |
s:send("JOIN " .. channels .. "\r\n") -- join the channels | |
end | |
function handleRawIRC() | |
n if output:match " PRIVMSG " then | |
--Parse channel: | |
if output:match "#" then | |
i = string.find(output, "#") | |
j = string.find(output, " ", i) | |
msg.channel = string.sub(output, 1, j) | |
msg.channel = string.sub(msg.channel, i) | |
print(msg.channel) | |
end | |
--Parse sender: | |
i = string.find(output, ":") | |
j = string.find(output, "!", i) | |
msg.sender = string.sub(output, 1, j-1) | |
msg.sender = string.sub(msg.sender, i+1) | |
-- print(msg.sender) | |
--Parse host: | |
i = string.find(output, "~") | |
j = string.find(output, " ", i) | |
msg.host = string.sub(output, 1, j-1) | |
msg.host = string.sub(msg.host, i+1) | |
-- print(msg.host) | |
--Parse message: | |
i = string.find(output, ":", 2) | |
msg.message = string.sub(output, i+1) | |
print(msg.message) | |
end | |
if output:match "PING :" then | |
s:send("PONG :" .. output:match"^PING :(.*)") -- Thank you, boxmein! | |
end | |
-- Lets just parse the msg.message here, in case of commands and their variables. And then re-write all the plugins made this far. Yay! Reformating... | |
if msg.message ~= nil then | |
if msg.message:sub(0, 2) == prefix then -- CHANGE THE '2' TO '0' IF STARTING TO USE ONE CHARACTER PREFIX! | |
msg.type = "command" | |
end | |
if msg.type == "command" then | |
j = string.find(msg.message, " ", 1) | |
msg.command = string.sub(msg.message, 3, j) | |
print(msg.command) | |
if string.find(msg.message, "#", j+2) | |
--do the stuff! | |
end | |
end | |
end | |
end | |
--[commands] | |
function greet() | |
if output:match "Hi!" then | |
s:send("PRIVMSG " .. msg.channel .. ":Hi, " .. msg.sender .. "\r\n") | |
end | |
end | |
function echo() | |
if msg.message ~= nil then | |
if msg.message:sub(1, 6) == "!!echo" then | |
if msg.message:sub(8) == "#" then | |
-- i, j = string.find(msg.message, "echo") | |
-- msg.arg1 = string.sub(msg.message, j) | |
i = string.find(msg.message, "#") | |
print(i) | |
j = string.find(msg.message, " ", i) | |
print(j) | |
msg.arg2 = string.sub(msg.message, 1, j) | |
msg.arg2 = string.sub(msg.arg2, i) | |
print(msg.arg2) | |
msg.arg1 = string.sub(msg.message, j+1) | |
s:send("PRIVMSG " .. msg.arg2 .. " :" .. msg.arg1 .. "\r\n") | |
print("PRIVMSG " .. msg.arg2 .. " :" .. msg.arg1) | |
else | |
i, j = string.find(msg.message, "echo") | |
msg.arg1 = string.sub(msg.message, j+2) | |
s:send("PRIVMSG " .. msg.channel .. " :" .. msg.arg1 .. "\r\n") | |
end | |
end | |
end | |
end | |
function raw() | |
if msg.message ~= nil then | |
if msg.message:sub(1, 5) == "!!raw" then | |
s:send(msg.message:sub(7) .. "\r\n") | |
print(msg.message:sub(7)) | |
end | |
end | |
end | |
function join() | |
if msg.message ~= nil then | |
if msg.message:sub(1, 6) == "!!join" then | |
i = string.find(msg.message, "#") | |
msg.arg1 = string.sub(msg.message, i) | |
s:send("JOIN " .. msg.arg1 .. "\r\n") | |
end | |
end | |
end | |
--[/commands] | |
function listenForCommands() | |
greet() | |
--echo() | |
--raw() | |
--join() | |
end | |
function init() | |
s:connect(network, 6667) | |
sendInitialConfig() | |
while true do | |
output = s:receive("*l*") | |
print(output) | |
handleRawIRC() | |
listenForCommands() | |
end | |
end | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment