2010-04-04 16:55:12 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local char = string.char
|
|
|
|
local table = table
|
|
|
|
local assert = assert
|
|
|
|
local tostring = tostring
|
|
|
|
local type = type
|
|
|
|
|
|
|
|
module "irc"
|
|
|
|
|
|
|
|
--protocol parsing
|
|
|
|
function parse(line)
|
2010-06-28 06:00:20 +02:00
|
|
|
local prefix
|
|
|
|
local lineStart = 1
|
|
|
|
if line:sub(1,1) == ":" then
|
|
|
|
local space = line:find(" ")
|
|
|
|
prefix = line:sub(2, space-1)
|
|
|
|
lineStart = space
|
|
|
|
end
|
|
|
|
|
2010-07-03 05:23:56 +02:00
|
|
|
local trailToken = line:find(":", lineStart)
|
2010-06-28 06:00:20 +02:00
|
|
|
local lineStop = -1
|
|
|
|
local trailing
|
2010-07-03 05:23:56 +02:00
|
|
|
if trailToken then
|
|
|
|
trailing = line:sub(trailToken + 1)
|
|
|
|
lineStop = trailToken - 2
|
2010-06-28 06:00:20 +02:00
|
|
|
end
|
2010-04-04 16:55:12 +02:00
|
|
|
|
2010-06-28 06:00:20 +02:00
|
|
|
local params = {}
|
2010-04-04 16:55:12 +02:00
|
|
|
|
2010-06-28 06:00:20 +02:00
|
|
|
local _, cmdEnd, cmd = line:find("(%S+)", lineStart)
|
|
|
|
local pos = cmdEnd + 1
|
|
|
|
while true do
|
|
|
|
local _, stop, param = line:find("(%S+)", pos)
|
2010-07-03 05:23:56 +02:00
|
|
|
|
|
|
|
if stop >= lineStop then
|
2010-06-28 06:00:20 +02:00
|
|
|
break
|
|
|
|
end
|
2010-07-03 05:23:56 +02:00
|
|
|
|
|
|
|
pos = stop + 1
|
|
|
|
params[#params + 1] = param
|
2010-06-28 06:00:20 +02:00
|
|
|
end
|
2010-04-04 16:55:12 +02:00
|
|
|
|
2010-06-28 06:00:20 +02:00
|
|
|
if trailing then
|
|
|
|
params[#params + 1] = trailing
|
|
|
|
end
|
|
|
|
|
|
|
|
return prefix, cmd, params
|
2010-04-04 16:55:12 +02:00
|
|
|
end
|
|
|
|
|
2010-06-01 14:18:51 +02:00
|
|
|
function parseNick(nick)
|
|
|
|
return nick:match("^([%+@]?)(.+)$")
|
|
|
|
end
|
|
|
|
|
2010-04-04 16:55:12 +02:00
|
|
|
function parsePrefix(prefix)
|
|
|
|
local user = {}
|
|
|
|
if prefix then
|
2010-06-01 01:39:34 +02:00
|
|
|
user.access, user.nick, user.username, user.host = prefix:match("^([%+@]?)(.+)!(.+)@(.+)$")
|
2010-04-04 16:55:12 +02:00
|
|
|
end
|
|
|
|
return user
|
|
|
|
end
|
|
|
|
|
|
|
|
--mIRC markup scheme (de-facto standard)
|
|
|
|
color = {
|
|
|
|
black = 1,
|
|
|
|
blue = 2,
|
|
|
|
green = 3,
|
|
|
|
red = 4,
|
|
|
|
lightred = 5,
|
|
|
|
purple = 6,
|
|
|
|
brown = 7,
|
|
|
|
yellow = 8,
|
|
|
|
lightgreen = 9,
|
|
|
|
navy = 10,
|
|
|
|
cyan = 11,
|
|
|
|
lightblue = 12,
|
|
|
|
violet = 13,
|
|
|
|
gray = 14,
|
|
|
|
lightgray = 15,
|
|
|
|
white = 16
|
|
|
|
}
|
|
|
|
|
|
|
|
local colByte = char(3)
|
|
|
|
setmetatable(color, {__call = function(_, text, colornum)
|
|
|
|
colornum = type(colornum) == "string" and assert(color[colornum], "Invalid color '"..colornum.."'") or colornum
|
|
|
|
return table.concat{colByte, tostring(colornum), text, colByte}
|
|
|
|
end})
|
|
|
|
|
|
|
|
local boldByte = char(2)
|
|
|
|
function bold(text)
|
|
|
|
return boldByte..text..boldByte
|
|
|
|
end
|
|
|
|
|
|
|
|
local underlineByte = char(31)
|
|
|
|
function underline(text)
|
|
|
|
return underlineByte..text..underlineByte
|
|
|
|
end
|