From 41cbab629fbb70a77034f982c90897df4e9d15bd Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sat, 17 Jul 2010 21:12:42 +0800 Subject: [PATCH 01/10] Adding TLS/SSL support via LuaSec --- asyncoperations.lua | 7 +++- init.lua | 89 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 72 insertions(+), 24 deletions(-) diff --git a/asyncoperations.lua b/asyncoperations.lua index 7aff528..f678d38 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -5,8 +5,11 @@ module "irc" local meta = _META function meta:send(fmt, ...) - self.socket:send(fmt:format(...) .. "\r\n") -end + local bytes, err = self.socket:send(fmt:format(...) .. "\r\n") + + if bytes then + return + end local function sendByMethod(self, method, target, msg) local toChannel = table.concat({method, target, ":"}, " ") diff --git a/init.lua b/init.lua index bd657e0..8d24df9 100644 --- a/init.lua +++ b/init.lua @@ -6,8 +6,10 @@ local rawget = rawget local unpack = unpack local pairs = pairs local assert = assert -local require = require +local require = require local tonumber = tonumber +local type = type +local pcall = pcall module "irc" @@ -62,28 +64,69 @@ function meta:invoke(name, ...) local hooks = self.hooks[name] if hooks then for id,f in pairs(hooks) do - f(...) + if f(...) then + return true + end end end end -function meta_preconnect:connect(server, port, timeout) +function meta_preconnect:connect(_host, _port) + local host, port, password, secure, timeout + + if type(_host) == "table" then + host = _host.host + port = _host.port + timeout = _host.timeout + password = _host.password + secure = _host.secure + else + host = _host + port = _port + end + + host = host or error("host name required to connect", 2) port = port or 6667 local s = socket.tcp() - self.socket = s + s:settimeout(timeout or 30) - assert(s:connect(server, port)) - + assert(s:connect(host, port)) + + if secure then + local work, ssl = pcall(require, "ssl") + if not work then + error("LuaSec required for secure connections", 2) + end + + local params + if type(secure) == "table" then + params = secure + else + params = {mode="client", protocol="tlsv1"} + end + + s = ssl.wrap(s, params) + success, errmsg = s:dohandshake() + if not success then + error(("could not make secure connection %s"):format(errmsg), 2) + end + end + + self.socket = s setmetatable(self, meta) + if password then + self:send("PASS %s", password) + end + self:send("USER %s 0 * :%s", self.username, self.realname) self:send("NICK %s", self.nick) self.channels = {} - s:settimeout(0) - + s:settimeout(0) + repeat self:think() until self.authed @@ -99,21 +142,22 @@ function meta:disconnect(message) end function meta:shutdown() - self.socket:shutdown() self.socket:close() setmetatable(self, nil) end local function getline(self, errlevel) - line, err = self.socket:receive("*l") - - if not line and err ~= "timeout" then - self:invoke("OnDisconnect", err, true) - self:shutdown() + local line, err = self.socket:receive("*l") + + if line then + return line + end + + if err ~= "timeout" and err ~= "wantread" then + self:invoke("OnDisconnect", err, true) + self:close() error(err, errlevel) end - - return line end function meta:think() @@ -259,7 +303,7 @@ local whoisHandlers = { ["330"] = "account"; -- Freenode ["307"] = "registered"; -- Unreal } - + function meta:whois(nick) self:send("WHOIS %s", nick) @@ -282,7 +326,7 @@ function meta:whois(nick) end if result.account then - result.account = result.account[3] + result.account = result.account[3] elseif result.registered then result.account = result.registered[2] @@ -290,7 +334,8 @@ function meta:whois(nick) return result end -function meta:topic(channel) - self:send("TOPIC %s", channel) -end - + +function meta:topic(channel) + self:send("TOPIC %s", channel) +end + From 7604bb2a5139ffb59b88beef6c12033f889ec065 Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sat, 17 Jul 2010 21:16:01 +0800 Subject: [PATCH 02/10] 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 From b392b69f8706220597d8f51116b00fbaa169a2f3 Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sat, 17 Jul 2010 21:19:07 +0800 Subject: [PATCH 03/10] Adding ability for hooks to return, and OnRaw hook By adding the ability for hooks to return, we can stop the processing of a line at any point. This is used by the OnRaw hook such that any non-nil return value will cause the irc library to assume that line has been handled. --- init.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 8d24df9..86ee879 100644 --- a/init.lua +++ b/init.lua @@ -163,7 +163,10 @@ end function meta:think() while true do local line = getline(self, 3) - if line then self:handle(parse(line)) + if line then + if not self:invoke("OnRaw", line) then + self:handle(parse(line)) + end else break end @@ -292,7 +295,7 @@ end function meta:handle(prefix, cmd, params) local handler = handlers[cmd] if handler then - handler(self, prefix, unpack(params)) + return handler(self, prefix, unpack(params)) end end From 9bf809f99a84db896e2a3312711c1d45c212e691 Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sun, 18 Jul 2010 15:24:13 +0800 Subject: [PATCH 04/10] Fixing regressions in sendChat and sendNotice. Fixing: Incorrect handling of newlines in message. Fixing: Cleaning ':' from message. --- asyncoperations.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/asyncoperations.lua b/asyncoperations.lua index 8d5827c..fc6f746 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -23,11 +23,17 @@ local function clean(str) end function meta:sendChat(target, msg) - self:send("PRIVMSG %s :%s", clean(target), clean(msg)) + -- Split the message into segments if it includes newlines. + for line in msg:gmatch("([^\r\n]+)") + self:send("PRIVMSG %s :%s", clean(target), msg) + end end function meta:sendNotice(target, msg) - self:send("NOTICE %s :%s", clean(target), clean(msg)) + -- Split the message into segments if it includes newlines. + for line in msg:gmatch("([^\r\n]+)") + self:send("NOTICE %s :%s", clean(target), msg) + end end function meta:join(channel, key) From f07a3cec222a21ce6aa7bcac881a51d68c12070a Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sun, 18 Jul 2010 16:26:04 +0800 Subject: [PATCH 05/10] Changing clean to verify for sending of parameters Send methods will now error if you pass invalid characters --- asyncoperations.lua | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/asyncoperations.lua b/asyncoperations.lua index fc6f746..dfcde18 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -18,34 +18,38 @@ function meta:send(fmt, ...) end end -local function clean(str) - return str:gsub("[\r\n:]", "") +local function verify(str, errLevel) + if str:find("^:") or find("%s%z") then + error(("bad characters in '%s'"):format(str), errLevel) + end + + return str end function meta:sendChat(target, msg) -- Split the message into segments if it includes newlines. for line in msg:gmatch("([^\r\n]+)") - self:send("PRIVMSG %s :%s", clean(target), msg) + self:send("PRIVMSG %s :%s", verify(target, 2), msg) end end function meta:sendNotice(target, msg) -- Split the message into segments if it includes newlines. for line in msg:gmatch("([^\r\n]+)") - self:send("NOTICE %s :%s", clean(target), msg) + self:send("NOTICE %s :%s", verify(target, 2), msg) end end function meta:join(channel, key) if key then - self:send("JOIN %s :%s", clean(channel), clean(key)) + self:send("JOIN %s :%s", verify(channel, 2), verify(key, 2)) else - self:send("JOIN %s", clean(channel)) + self:send("JOIN %s", verify(channel, 2)) end end function meta:part(channel) - channel = clean(channel) + channel = verify(channel, 2) self:send("PART %s", channel) if self.track_users then self.channels[channel] = nil @@ -76,5 +80,5 @@ function meta:setMode(t) mode = table.concat{mode, "-", rem} end - self:send("MODE %s %s", clean(target), clean(mode)) + self:send("MODE %s %s", verify(target, 2), verify(mode, 2)) end From 30253c5a2a931f54403aebfb8ca26800f471010b Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sun, 18 Jul 2010 19:13:38 +0800 Subject: [PATCH 06/10] Changing verify error message --- asyncoperations.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncoperations.lua b/asyncoperations.lua index dfcde18..a9cb87b 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -20,7 +20,7 @@ end local function verify(str, errLevel) if str:find("^:") or find("%s%z") then - error(("bad characters in '%s'"):format(str), errLevel) + error(("malformed parameter '%s' to irc command"):format(str), errLevel) end return str From 481210b440fd0198cc5b35bcf4048a8eab89076e Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sun, 18 Jul 2010 19:55:02 +0800 Subject: [PATCH 07/10] Updating documentation Adding TLS/SSL documentation Adding OnUserModeIs documentation Adding OnChannelModeIs documentation Adding OnRaw documentation --- doc/irc.luadoc | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/irc.luadoc b/doc/irc.luadoc index ddd7b76..1a98b88 100644 --- a/doc/irc.luadoc +++ b/doc/irc.luadoc @@ -44,10 +44,13 @@ function irc:hook(name, id, f) function irc:unhook(name, id) --- Connect irc to an IRC server. --- @param server Server address. +-- @param host Host address. -- @param port Server port. [default 6667] --- @param timeout Connection timeout value in seconds. [default 30] -function irc:connect(server, port, timeout) +function irc:connect(server, port) + +-- @param table Table of connection details +-- @see Connection +function irc:connect(table) --- Disconnect irc from the server. -- @param message Quit message. @@ -108,8 +111,21 @@ function irc:invoke(name, ...) function irc:handle(prefix, cmd, params) function irc:shutdown() +--- Table with connection information. +--
    +--
  • host - Server host name.
  • +--
  • port - Server port. [defaults to 6667]
  • +--
  • timeout - Connect timeout. [defaults to 30]
  • +--
  • password - Server password.
  • +--
  • secure - Boolean to enable TLS connection, pass a params table (described, [luasec]) to control
  • +--
+-- [luasec]: http://www.inf.puc-rio.br/~brunoos/luasec/reference.html +-- @name Connection +-- @class table + --- List of hooks you can use with irc:hook. The parameter list describes the parameters passed to the callback function. --
    +--
  • OnRaw(line) - (any non false/nil return value assumes line handled and will not be further processed)
  • --
  • OnDisconnect(message, errorOccurred)
  • --
  • OnChat(user, channel, message)
  • --
  • OnNotice(user, channel, message)
  • @@ -121,6 +137,8 @@ function irc:shutdown() --
  • OnTopic(channel, topic)
  • --
  • OnTopicInfo(channel, creator, timeCreated)
  • --
  • OnKick(channel, nick, kicker, reason)* (kicker is a user table)
  • +--
  • OnUserModeIs(modes)
  • +--
  • OnChannelModeIs(user, channel, modes)
  • --
-- * Event also invoked for yourself. -- † Channel passed only when user tracking is enabled From afbe9d91ed02bb708e277519c6c6ed04c2b070d7 Mon Sep 17 00:00:00 2001 From: Jakob Ovrum Date: Mon, 19 Jul 2010 06:52:31 +0900 Subject: [PATCH 08/10] added init.lua --- init.lua | 654 +++++++++++++++++++++++++++---------------------------- 1 file changed, 327 insertions(+), 327 deletions(-) diff --git a/init.lua b/init.lua index 86ee879..5c75a48 100644 --- a/init.lua +++ b/init.lua @@ -1,280 +1,280 @@ -local socket = require "socket" - -local error = error -local setmetatable = setmetatable -local rawget = rawget -local unpack = unpack -local pairs = pairs -local assert = assert -local require = require -local tonumber = tonumber -local type = type -local pcall = pcall - -module "irc" - -local meta = {} -meta.__index = meta -_META = meta - -require "irc.util" -require "irc.asyncoperations" - -local meta_preconnect = {} -function meta_preconnect.__index(o, k) - local v = rawget(meta_preconnect, k) - - if not v and meta[k] then - error("field '"..k.."' is not accessible before connecting", 2) - end - return v -end - -function new(user) - local o = { - nick = assert(user.nick, "Field 'nick' is required"); - username = user.username or "lua"; - realname = user.realname or "Lua owns"; - hooks = {}; - track_users = true; - } - return setmetatable(o, meta_preconnect) -end - -function meta:hook(name, id, f) - f = f or id - self.hooks[name] = self.hooks[name] or {} - self.hooks[name][id] = f - return id or f -end -meta_preconnect.hook = meta.hook - - -function meta:unhook(name, id) - local hooks = self.hooks[name] - - assert(hooks, "no hooks exist for this event") - assert(hooks[id], "hook ID not found") - - hooks[id] = nil -end -meta_preconnect.unhook = meta.unhook - -function meta:invoke(name, ...) - local hooks = self.hooks[name] - if hooks then - for id,f in pairs(hooks) do - if f(...) then - return true - end - end - end -end - -function meta_preconnect:connect(_host, _port) - local host, port, password, secure, timeout - - if type(_host) == "table" then - host = _host.host - port = _host.port - timeout = _host.timeout - password = _host.password - secure = _host.secure - else - host = _host - port = _port - end - - host = host or error("host name required to connect", 2) - port = port or 6667 - - local s = socket.tcp() - - s:settimeout(timeout or 30) - assert(s:connect(host, port)) - - if secure then - local work, ssl = pcall(require, "ssl") - if not work then - error("LuaSec required for secure connections", 2) - end - - local params - if type(secure) == "table" then - params = secure - else - params = {mode="client", protocol="tlsv1"} - end - - s = ssl.wrap(s, params) - success, errmsg = s:dohandshake() - if not success then - error(("could not make secure connection %s"):format(errmsg), 2) - end - end - - self.socket = s - setmetatable(self, meta) - - if password then - self:send("PASS %s", password) - end - - self:send("USER %s 0 * :%s", self.username, self.realname) - self:send("NICK %s", self.nick) - - self.channels = {} - - s:settimeout(0) - - repeat - self:think() - until self.authed -end - -function meta:disconnect(message) - local message = message or "Bye!" - - self:invoke("OnDisconnect", message, false) - self:send("QUIT :%s", message) - - self:shutdown() -end - -function meta:shutdown() - self.socket:close() - setmetatable(self, nil) -end - -local function getline(self, errlevel) - local line, err = self.socket:receive("*l") - - if line then - return line - end - - if err ~= "timeout" and err ~= "wantread" then - self:invoke("OnDisconnect", err, true) - self:close() - error(err, errlevel) - end -end - -function meta:think() - while true do - local line = getline(self, 3) - if line then - if not self:invoke("OnRaw", line) then - self:handle(parse(line)) - end - else - break - end - end -end - -local handlers = {} - -handlers["PING"] = function(o, prefix, query) - o:send("PONG :%s", query) -end - -handlers["001"] = function(o) - o.authed = true -end - -handlers["PRIVMSG"] = function(o, prefix, channel, message) - o:invoke("OnChat", parsePrefix(prefix), channel, message) -end - -handlers["NOTICE"] = function(o, prefix, channel, message) - o:invoke("OnNotice", parsePrefix(prefix), channel, message) -end - -handlers["JOIN"] = function(o, prefix, channel) - local user = parsePrefix(prefix) - if o.track_users then - if user.nick == o.nick then - o.channels[channel] = {users = {}} - else - o.channels[channel].users[user.nick] = user - end - end - - o:invoke("OnJoin", user, channel) -end - -handlers["PART"] = function(o, prefix, channel, reason) - local user = parsePrefix(prefix) - if o.track_users then - if user.nick == o.nick then - o.channels[channel] = nil - else - o.channels[channel].users[user.nick] = nil - end - end - o:invoke("OnPart", user, channel, reason) -end - -handlers["QUIT"] = function(o, prefix, msg) - local user = parsePrefix(prefix) - if o.track_users then - for channel, v in pairs(o.channels) do - v.users[user.nick] = nil - end - end - o:invoke("OnQuit", user, msg) -end - -handlers["NICK"] = function(o, prefix, newnick) - local user = parsePrefix(prefix) - if o.track_users then - for channel, v in pairs(o.channels) do - local users = v.users - local oldinfo = users[user.nick] - if oldinfo then - users[newnick] = oldinfo - users[user.nick] = nil - o:invoke("NickChange", user, newnick, channel) - end - end - else - o:invoke("NickChange", user, newnick) - end -end - ---NAMES list -handlers["353"] = function(o, prefix, me, chanType, channel, names) - if o.track_users then - o.channels[channel] = o.channels[channel] or {users = {}, type = chanType} - - local users = o.channels[channel].users - for nick in names:gmatch("(%S+)") do - local access, name = parseNick(nick) - users[name] = {type = access} - end - end -end - ---end of NAMES -handlers["366"] = function(o, prefix, me, channel, msg) - if o.track_users then - o:invoke("NameList", channel, msg) - end -end +local socket = require "socket" + +local error = error +local setmetatable = setmetatable +local rawget = rawget +local unpack = unpack +local pairs = pairs +local assert = assert +local require = require +local tonumber = tonumber +local type = type +local pcall = pcall + +module "irc" + +local meta = {} +meta.__index = meta +_META = meta + +require "irc.util" +require "irc.asyncoperations" + +local meta_preconnect = {} +function meta_preconnect.__index(o, k) + local v = rawget(meta_preconnect, k) + + if not v and meta[k] then + error("field '"..k.."' is not accessible before connecting", 2) + end + return v +end + +function new(user) + local o = { + nick = assert(user.nick, "Field 'nick' is required"); + username = user.username or "lua"; + realname = user.realname or "Lua owns"; + hooks = {}; + track_users = true; + } + return setmetatable(o, meta_preconnect) +end + +function meta:hook(name, id, f) + f = f or id + self.hooks[name] = self.hooks[name] or {} + self.hooks[name][id] = f + return id or f +end +meta_preconnect.hook = meta.hook + + +function meta:unhook(name, id) + local hooks = self.hooks[name] + + assert(hooks, "no hooks exist for this event") + assert(hooks[id], "hook ID not found") + + hooks[id] = nil +end +meta_preconnect.unhook = meta.unhook + +function meta:invoke(name, ...) + local hooks = self.hooks[name] + if hooks then + for id,f in pairs(hooks) do + if f(...) then + return true + end + end + end +end + +function meta_preconnect:connect(_host, _port) + local host, port, password, secure, timeout + + if type(_host) == "table" then + host = _host.host + port = _host.port + timeout = _host.timeout + password = _host.password + secure = _host.secure + else + host = _host + port = _port + end + + host = host or error("host name required to connect", 2) + port = port or 6667 + + local s = socket.tcp() + + s:settimeout(timeout or 30) + assert(s:connect(host, port)) + + if secure then + local work, ssl = pcall(require, "ssl") + if not work then + error("LuaSec required for secure connections", 2) + end + + local params + if type(secure) == "table" then + params = secure + else + params = {mode="client", protocol="tlsv1"} + end + + s = ssl.wrap(s, params) + success, errmsg = s:dohandshake() + if not success then + error(("could not make secure connection %s"):format(errmsg), 2) + end + end + + self.socket = s + setmetatable(self, meta) + + if password then + self:send("PASS %s", password) + end + + self:send("USER %s 0 * :%s", self.username, self.realname) + self:send("NICK %s", self.nick) + + self.channels = {} + + s:settimeout(0) + + repeat + self:think() + until self.authed +end + +function meta:disconnect(message) + local message = message or "Bye!" + + self:invoke("OnDisconnect", message, false) + self:send("QUIT :%s", message) + + self:shutdown() +end + +function meta:shutdown() + self.socket:close() + setmetatable(self, nil) +end + +local function getline(self, errlevel) + local line, err = self.socket:receive("*l") + + if line then + return line + end + + if err ~= "timeout" and err ~= "wantread" then + self:invoke("OnDisconnect", err, true) + self:close() + error(err, errlevel) + end +end + +function meta:think() + while true do + local line = getline(self, 3) + if line then + if not self:invoke("OnRaw", line) then + self:handle(parse(line)) + end + else + break + end + end +end + +local handlers = {} + +handlers["PING"] = function(o, prefix, query) + o:send("PONG :%s", query) +end + +handlers["001"] = function(o) + o.authed = true +end + +handlers["PRIVMSG"] = function(o, prefix, channel, message) + o:invoke("OnChat", parsePrefix(prefix), channel, message) +end + +handlers["NOTICE"] = function(o, prefix, channel, message) + o:invoke("OnNotice", parsePrefix(prefix), channel, message) +end + +handlers["JOIN"] = function(o, prefix, channel) + local user = parsePrefix(prefix) + if o.track_users then + if user.nick == o.nick then + o.channels[channel] = {users = {}} + else + o.channels[channel].users[user.nick] = user + end + end + + o:invoke("OnJoin", user, channel) +end + +handlers["PART"] = function(o, prefix, channel, reason) + local user = parsePrefix(prefix) + if o.track_users then + if user.nick == o.nick then + o.channels[channel] = nil + else + o.channels[channel].users[user.nick] = nil + end + end + o:invoke("OnPart", user, channel, reason) +end + +handlers["QUIT"] = function(o, prefix, msg) + local user = parsePrefix(prefix) + if o.track_users then + for channel, v in pairs(o.channels) do + v.users[user.nick] = nil + end + end + o:invoke("OnQuit", user, msg) +end + +handlers["NICK"] = function(o, prefix, newnick) + local user = parsePrefix(prefix) + if o.track_users then + for channel, v in pairs(o.channels) do + local users = v.users + local oldinfo = users[user.nick] + if oldinfo then + users[newnick] = oldinfo + users[user.nick] = nil + o:invoke("NickChange", user, newnick, channel) + end + end + else + o:invoke("NickChange", user, newnick) + end +end + +--NAMES list +handlers["353"] = function(o, prefix, me, chanType, channel, names) + if o.track_users then + o.channels[channel] = o.channels[channel] or {users = {}, type = chanType} + + local users = o.channels[channel].users + for nick in names:gmatch("(%S+)") do + local access, name = parseNick(nick) + users[name] = {type = access} + end + end +end + +--end of NAMES +handlers["366"] = function(o, prefix, me, channel, msg) + if o.track_users then + o:invoke("NameList", channel, msg) + end +end --no topic handlers["331"] = function(o, prefix, me, channel) o:invoke("OnTopic", channel, nil) end ---new topic -handlers["TOPIC"] = function(o, prefix, channel, topic) - o:invoke("OnTopic", channel, topic) +--new topic +handlers["TOPIC"] = function(o, prefix, channel, topic) + o:invoke("OnTopic", channel, topic) end -handlers["332"] = function(o, prefix, me, channel, topic) - o:invoke("OnTopic", channel, topic) +handlers["332"] = function(o, prefix, me, channel, topic) + o:invoke("OnTopic", channel, topic) end --topic creation info @@ -284,61 +284,61 @@ end handlers["KICK"] = function(o, prefix, channel, kicked, reason) o:invoke("OnKick", channel, kicked, parsePrefix(prefix), reason) -end - -handlers["ERROR"] = function(o, prefix, message) - o:invoke("OnDisconnect", message, true) - o:shutdown() - error(message, 3) -end - -function meta:handle(prefix, cmd, params) - local handler = handlers[cmd] - if handler then - return handler(self, prefix, unpack(params)) - end -end - -local whoisHandlers = { - ["311"] = "userinfo"; - ["312"] = "node"; - ["319"] = "channels"; - ["330"] = "account"; -- Freenode - ["307"] = "registered"; -- Unreal -} - -function meta:whois(nick) - self:send("WHOIS %s", nick) - - local result = {} - - while true do - local line = getline(self, 3) - if line then - local prefix, cmd, args = parse(line) - - local handler = whoisHandlers[cmd] - if handler then - result[handler] = args - elseif cmd == "318" then - break - else - self:handle(prefix, cmd, args) - end - end - end - - if result.account then - result.account = result.account[3] - - elseif result.registered then - result.account = result.registered[2] - end - - return result -end - -function meta:topic(channel) - self:send("TOPIC %s", channel) -end - +end + +handlers["ERROR"] = function(o, prefix, message) + o:invoke("OnDisconnect", message, true) + o:shutdown() + error(message, 3) +end + +function meta:handle(prefix, cmd, params) + local handler = handlers[cmd] + if handler then + return handler(self, prefix, unpack(params)) + end +end + +local whoisHandlers = { + ["311"] = "userinfo"; + ["312"] = "node"; + ["319"] = "channels"; + ["330"] = "account"; -- Freenode + ["307"] = "registered"; -- Unreal +} + +function meta:whois(nick) + self:send("WHOIS %s", nick) + + local result = {} + + while true do + local line = getline(self, 3) + if line then + local prefix, cmd, args = parse(line) + + local handler = whoisHandlers[cmd] + if handler then + result[handler] = args + elseif cmd == "318" then + break + else + self:handle(prefix, cmd, args) + end + end + end + + if result.account then + result.account = result.account[3] + + elseif result.registered then + result.account = result.registered[2] + end + + return result +end + +function meta:topic(channel) + self:send("TOPIC %s", channel) +end + From 145aa5b3eb35a5f6743cf0ebb826eda039fda70a Mon Sep 17 00:00:00 2001 From: Jakob Ovrum Date: Mon, 19 Jul 2010 07:24:19 +0900 Subject: [PATCH 09/10] Fixed some merge bugs --- asyncoperations.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/asyncoperations.lua b/asyncoperations.lua index 2b51224..f2dd79a 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -19,7 +19,7 @@ function meta:send(fmt, ...) end local function verify(str, errLevel) - if str:find("^:") or find("%s%z") then + if str:find("^:") or str:find("%s%z") then error(("malformed parameter '%s' to irc command"):format(str), errLevel) end @@ -28,14 +28,14 @@ end function meta:sendChat(target, msg) -- Split the message into segments if it includes newlines. - for line in msg:gmatch("([^\r\n]+)") + for line in msg:gmatch("([^\r\n]+)") do self:send("PRIVMSG %s :%s", verify(target, 3), msg) end end function meta:sendNotice(target, msg) -- Split the message into segments if it includes newlines. - for line in msg:gmatch("([^\r\n]+)") + for line in msg:gmatch("([^\r\n]+)") do self:send("NOTICE %s :%s", verify(target, 3), msg) end end From a37a7618cea3273489ca95608388b6f28dc3bf21 Mon Sep 17 00:00:00 2001 From: Jakob Ovrum Date: Mon, 19 Jul 2010 07:46:55 +0900 Subject: [PATCH 10/10] Changed a bit of style stuff, tweaked verify calls here and there --- asyncoperations.lua | 14 ++++---------- init.lua | 6 +++--- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/asyncoperations.lua b/asyncoperations.lua index f2dd79a..9bf2550 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -7,11 +7,7 @@ local meta = _META function meta:send(fmt, ...) local bytes, err = self.socket:send(fmt:format(...) .. "\r\n") - if bytes then - return - end - - if err ~= "timeout" and err ~= "wantwrite" then + if not bytes and err ~= "timeout" and err ~= "wantwrite" then self:invoke("OnDisconnect", err, true) self:shutdown() error(err, errlevel) @@ -27,14 +23,12 @@ local function verify(str, errLevel) end function meta:sendChat(target, msg) - -- Split the message into segments if it includes newlines. for line in msg:gmatch("([^\r\n]+)") do self:send("PRIVMSG %s :%s", verify(target, 3), msg) end end function meta:sendNotice(target, msg) - -- Split the message into segments if it includes newlines. for line in msg:gmatch("([^\r\n]+)") do self:send("NOTICE %s :%s", verify(target, 3), msg) end @@ -73,12 +67,12 @@ function meta:setMode(t) assert(add or rem, "table contains neither 'add' nor 'remove'") if add then - mode = table.concat{"+", add} + mode = table.concat{"+", verify(add, 3)} end if rem then - mode = table.concat{mode, "-", rem} + mode = table.concat{mode, "-", verify(rem, 3)} end - self:send("MODE %s %s", verify(target, 3), verify(mode, 3)) + self:send("MODE %s %s", verify(target, 3), mode) end diff --git a/init.lua b/init.lua index bb23ab5..ca18f95 100644 --- a/init.lua +++ b/init.lua @@ -103,13 +103,13 @@ function meta_preconnect:connect(_host, _port) if type(secure) == "table" then params = secure else - params = {mode="client", protocol="tlsv1"} + params = {mode = "client", protocol = "tlsv1"} end s = ssl.wrap(s, params) success, errmsg = s:dohandshake() if not success then - error(("could not make secure connection %s"):format(errmsg), 2) + error(("could not make secure connection: %s"):format(errmsg), 2) end end @@ -133,7 +133,7 @@ function meta_preconnect:connect(_host, _port) end function meta:disconnect(message) - local message = message or "Bye!" + message = message or "Bye!" self:invoke("OnDisconnect", message, false) self:send("QUIT :%s", message)