diff --git a/asyncoperations.lua b/asyncoperations.lua index 8dfe035..682b410 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -8,13 +8,21 @@ function meta:send(fmt, ...) self.socket:send(fmt:format(...) .. "\r\n") end -function meta:sendChat(channel, msg) - toChannel = table.concat{"PRIVMSG ", channel, " :"} +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"}) end end +function meta:sendChat(target, msg) + sendByMethod(self, "PRIVMSG", target, msg) +end + +function meta:sendNotice(target, msg) + sendByMethod(self, "NOTICE", target, msg) +end + function meta:join(channel, key) if key then self:send("JOIN %s :%s", channel, key) @@ -27,6 +35,15 @@ function meta:part(channel) self:send("PART %s", channel) end +function meta:trackUsers(b) + self.track_users = b + if not b then + for k,v in pairs(self.channels) do + self.channels[k] = nil + end + end +end + function meta:setMode(t) local target = t.target or self.nick local mode = "" diff --git a/init.lua b/init.lua index 75a2a94..7cd829a 100644 --- a/init.lua +++ b/init.lua @@ -35,6 +35,7 @@ function new(user) username = user.username or "lua"; realname = user.realname or "Lua owns"; hooks = {}; + track_users = true; } return setmetatable(o, meta_preconnect) end @@ -43,13 +44,17 @@ 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 @@ -76,6 +81,8 @@ function meta_preconnect:connect(server, port, timeout) self:send("USER %s 0 * :%s", self.username, self.realname) self:send("NICK %s", self.nick) + self.channels = {} + s:settimeout(0) repeat self:think() @@ -139,11 +146,47 @@ handlers["NOTICE"] = function(o, prefix, channel, message) end handlers["JOIN"] = function(o, prefix, channel) - o:invoke("OnJoin", parsePrefix(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) - o:invoke("OnPart", parsePrefix(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 + +--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 + users[nick] = {} + 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 handlers["ERROR"] = function(o, prefix, message) diff --git a/util.lua b/util.lua index 11d13cb..df7de07 100644 --- a/util.lua +++ b/util.lua @@ -39,7 +39,7 @@ end function parsePrefix(prefix) local user = {} if prefix then - user.nick, user.username, user.host = prefix:match("(.*)!(.*)@(.*)") + user.access, user.nick, user.username, user.host = prefix:match("^([%+@]?)(.+)!(.+)@(.+)$") end return user end