Refactoring nasty send methods.

Adding a local clean method to sanitise strings for sending.
Removing sendByMethod, made obsolete by changes to send calls.
This commit is contained in:
Joshua Simmons 2010-07-17 23:16:01 +10:00
parent eac1f0d668
commit bb020fdad4
1 changed files with 15 additions and 10 deletions

View File

@ -11,30 +11,35 @@ function meta:send(fmt, ...)
return return
end end
local function sendByMethod(self, method, target, msg) if err ~= "timeout" and err ~= "wantwrite" then
local toChannel = table.concat({method, target, ":"}, " ") self:invoke("OnDisconnect", err, true)
for line in msg:gmatch("[^\r\n]+") do self:shutdown()
self.socket:send(table.concat{toChannel, line, "\r\n"}) error(err, errlevel)
end end
end end
local function clean(str)
return str:gsub("[\r\n:]", "")
end
function meta:sendChat(target, msg) function meta:sendChat(target, msg)
sendByMethod(self, "PRIVMSG", target, msg) self:send("PRIVMSG %s :%s", clean(target), clean(msg))
end end
function meta:sendNotice(target, msg) function meta:sendNotice(target, msg)
sendByMethod(self, "NOTICE", target, msg) self:send("NOTICE %s :%s", clean(target), clean(msg))
end end
function meta:join(channel, key) function meta:join(channel, key)
if key then if key then
self:send("JOIN %s :%s", channel, key) self:send("JOIN %s :%s", clean(channel), clean(key))
else else
self:send("JOIN %s", channel) self:send("JOIN %s", clean(channel))
end end
end end
function meta:part(channel) function meta:part(channel)
channel = clean(channel)
self:send("PART %s", channel) self:send("PART %s", channel)
if self.track_users then if self.track_users then
self.channels[channel] = nil self.channels[channel] = nil
@ -65,5 +70,5 @@ function meta:setMode(t)
mode = table.concat{mode, "-", rem} mode = table.concat{mode, "-", rem}
end end
self:send("MODE %s %s", target, mode) self:send("MODE %s %s", clean(target), clean(mode))
end end