mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-01-10 10:00:28 +01:00
Added channel names tracking
This commit is contained in:
parent
cc156f8a20
commit
b104df15cc
@ -8,13 +8,21 @@ function meta:send(fmt, ...)
|
|||||||
self.socket:send(fmt:format(...) .. "\r\n")
|
self.socket:send(fmt:format(...) .. "\r\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta:sendChat(channel, msg)
|
local function sendByMethod(self, method, target, msg)
|
||||||
toChannel = table.concat{"PRIVMSG ", channel, " :"}
|
local toChannel = table.concat({method, target, ":"}, " ")
|
||||||
for line in msg:gmatch("[^\r\n]+") do
|
for line in msg:gmatch("[^\r\n]+") do
|
||||||
self.socket:send(table.concat{toChannel, line, "\r\n"})
|
self.socket:send(table.concat{toChannel, line, "\r\n"})
|
||||||
end
|
end
|
||||||
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)
|
function meta:join(channel, key)
|
||||||
if key then
|
if key then
|
||||||
self:send("JOIN %s :%s", channel, key)
|
self:send("JOIN %s :%s", channel, key)
|
||||||
@ -27,6 +35,15 @@ function meta:part(channel)
|
|||||||
self:send("PART %s", channel)
|
self:send("PART %s", channel)
|
||||||
end
|
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)
|
function meta:setMode(t)
|
||||||
local target = t.target or self.nick
|
local target = t.target or self.nick
|
||||||
local mode = ""
|
local mode = ""
|
||||||
|
47
init.lua
47
init.lua
@ -35,6 +35,7 @@ function new(user)
|
|||||||
username = user.username or "lua";
|
username = user.username or "lua";
|
||||||
realname = user.realname or "Lua owns";
|
realname = user.realname or "Lua owns";
|
||||||
hooks = {};
|
hooks = {};
|
||||||
|
track_users = true;
|
||||||
}
|
}
|
||||||
return setmetatable(o, meta_preconnect)
|
return setmetatable(o, meta_preconnect)
|
||||||
end
|
end
|
||||||
@ -43,13 +44,17 @@ function meta:hook(name, id, f)
|
|||||||
f = f or id
|
f = f or id
|
||||||
self.hooks[name] = self.hooks[name] or {}
|
self.hooks[name] = self.hooks[name] or {}
|
||||||
self.hooks[name][id] = f
|
self.hooks[name][id] = f
|
||||||
|
return id or f
|
||||||
end
|
end
|
||||||
meta_preconnect.hook = meta.hook
|
meta_preconnect.hook = meta.hook
|
||||||
|
|
||||||
|
|
||||||
function meta:unhook(name, id)
|
function meta:unhook(name, id)
|
||||||
local hooks = self.hooks[name]
|
local hooks = self.hooks[name]
|
||||||
|
|
||||||
|
assert(hooks, "no hooks exist for this event")
|
||||||
assert(hooks[id], "hook ID not found")
|
assert(hooks[id], "hook ID not found")
|
||||||
|
|
||||||
hooks[id] = nil
|
hooks[id] = nil
|
||||||
end
|
end
|
||||||
meta_preconnect.unhook = meta.unhook
|
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("USER %s 0 * :%s", self.username, self.realname)
|
||||||
self:send("NICK %s", self.nick)
|
self:send("NICK %s", self.nick)
|
||||||
|
|
||||||
|
self.channels = {}
|
||||||
|
|
||||||
s:settimeout(0)
|
s:settimeout(0)
|
||||||
repeat
|
repeat
|
||||||
self:think()
|
self:think()
|
||||||
@ -139,11 +146,47 @@ handlers["NOTICE"] = function(o, prefix, channel, message)
|
|||||||
end
|
end
|
||||||
|
|
||||||
handlers["JOIN"] = function(o, prefix, channel)
|
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
|
end
|
||||||
|
|
||||||
handlers["PART"] = function(o, prefix, channel, reason)
|
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
|
end
|
||||||
|
|
||||||
handlers["ERROR"] = function(o, prefix, message)
|
handlers["ERROR"] = function(o, prefix, message)
|
||||||
|
2
util.lua
2
util.lua
@ -39,7 +39,7 @@ end
|
|||||||
function parsePrefix(prefix)
|
function parsePrefix(prefix)
|
||||||
local user = {}
|
local user = {}
|
||||||
if prefix then
|
if prefix then
|
||||||
user.nick, user.username, user.host = prefix:match("(.*)!(.*)@(.*)")
|
user.access, user.nick, user.username, user.host = prefix:match("^([%+@]?)(.+)!(.+)@(.+)$")
|
||||||
end
|
end
|
||||||
return user
|
return user
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user