Last active
August 29, 2015 13:57
-
-
Save JanneSalokoski/9502483 to your computer and use it in GitHub Desktop.
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
local socket = require "socket" | |
local s = socket.tcp() | |
local debug = true | |
local nick = "Chen-4" | |
local user = "jenn4" | |
local realname = "ExpBot" | |
local passLine = io.read() --Passwords are received from the standard input. | |
local channels = "##jenn4" | |
local mode = "8" | |
local prefix = "!" | |
function logError(n) | |
if n == 400 then | |
if debug then | |
print("Error 400 - Bad request.") | |
end | |
end | |
end | |
function send(str) | |
s:send(str .. "\r\n") | |
end | |
function sendConfiguration() | |
if debug then | |
print("<< Sending configuration >>") | |
end | |
send("PASS " .. passLine) | |
send("NICK " .. nick) | |
send("USER " .. user .. " " .. mode .. " * :" .. realname) | |
send("JOIN " .. channels) | |
end | |
function parser() | |
local x = {} | |
--[[function listenCommands(str) | |
function echo(str, channel) | |
channel = channel or x.channel | |
if str:match("^" .. prefix .. "echo ") then | |
print("DEBUG: works") | |
end | |
end | |
echo(str) | |
end --]] | |
function listenCommands(str) | |
if debug then | |
print("Str: " .. str) | |
end | |
local plugin = str:match("^(.-)%s") | |
local attributeList = str:match("^.-%s(.*)") | |
local attributes = {} | |
if attributeList ~= nil then | |
for i in attributeList:match("%s+") do -- DO A NULLCHECK. FOR EVERYTHING! | |
table.insert(attributes, i) | |
attributes.exist = true | |
end | |
else | |
logError(400) | |
attributes.exist = false | |
end | |
if debug and attributes.exist then | |
print("Attributes for the cslled plugin " .. plugin .. " are " .. attributeList) | |
end | |
local commands = {} | |
table.insert(commands, "echo") | |
table.insert(commands, "quit") | |
for _, value in pairs(commands) do | |
if value:match(plugin) then | |
-- implement | |
end | |
end | |
end | |
if raw:match("%sPRIVMSG%s") then | |
x.senderNick = raw:match(":(.-)!") | |
if debug then | |
print("Sender nickname: " .. x.senderNick) | |
end | |
x.senderIdent = raw:match("!(.-)%s") | |
if debug then | |
print("Sender ident: " .. x.senderIdent) | |
end | |
x.messageType = "PRIVMSG" | |
if debug then | |
print("Message type: " .. x.messageType) | |
end | |
x.channel = raw:match("(#.-)%s") | |
if debug then | |
print("Channel: " .. x.channel) | |
end | |
x.message = raw:match("%s:(.*)") | |
if debug then | |
print(x.senderNick .. " from " .. x.channel .. " says: " .. x.message) | |
end | |
if x.message and x.message:match(prefix) then | |
local command = x.message:match("!(.*)") | |
listenCommands(command) | |
if debug then | |
print("Command is: " .. command) | |
end | |
end | |
end | |
end | |
function init() | |
s:connect("chat.eu.freenode.net", 6667) | |
sendConfiguration() | |
i = 0 | |
while true do | |
local output = s:receive("*l") | |
i = i + 1 | |
if debug then | |
print(i .. ": " .. output) | |
end | |
raw = output | |
parser() | |
if debug then | |
io.write("\n") | |
end | |
end | |
end | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment