From bb020fdad443521dd1c3ac4c74da4cf51e097145 Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sat, 17 Jul 2010 23:16:01 +1000 Subject: [PATCH] Refactoring nasty send methods. Adding a local clean method to sanitise strings for sending. Removing sendByMethod, made obsolete by changes to send calls. --- asyncoperations.lua | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/asyncoperations.lua b/asyncoperations.lua index f678d38..8d5827c 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -11,30 +11,35 @@ function meta:send(fmt, ...) return end -local function sendByMethod(self, method, target, msg) - local toChannel = table.concat({method, target, ":"}, " ") - for line in msg:gmatch("[^\r\n]+") do - self.socket:send(table.concat{toChannel, line, "\r\n"}) + if err ~= "timeout" and err ~= "wantwrite" then + self:invoke("OnDisconnect", err, true) + self:shutdown() + error(err, errlevel) end end - + +local function clean(str) + return str:gsub("[\r\n:]", "") +end + function meta:sendChat(target, msg) - sendByMethod(self, "PRIVMSG", target, msg) + self:send("PRIVMSG %s :%s", clean(target), clean(msg)) end function meta:sendNotice(target, msg) - sendByMethod(self, "NOTICE", target, msg) + self:send("NOTICE %s :%s", clean(target), clean(msg)) end function meta:join(channel, key) if key then - self:send("JOIN %s :%s", channel, key) + self:send("JOIN %s :%s", clean(channel), clean(key)) else - self:send("JOIN %s", channel) + self:send("JOIN %s", clean(channel)) end end function meta:part(channel) + channel = clean(channel) self:send("PART %s", channel) if self.track_users then self.channels[channel] = nil @@ -65,5 +70,5 @@ function meta:setMode(t) mode = table.concat{mode, "-", rem} end - self:send("MODE %s %s", target, mode) + self:send("MODE %s %s", clean(target), clean(mode)) end