PageRenderTime 210ms CodeModel.GetById 81ms app.highlight 44ms RepoModel.GetById 77ms app.codeStats 1ms

/util.lua

http://github.com/JakobOvrum/LuaIRC
Lua | 136 lines | 110 code | 19 blank | 7 comment | 14 complexity | 256b550cc21d5827dcd7350dae3bb99c MD5 | raw file
  1local setmetatable = setmetatable
  2local sub = string.sub
  3local byte = string.byte
  4local char = string.char
  5local table = table
  6local assert = assert
  7local tostring = tostring
  8local type = type
  9local random = math.random
 10
 11module "irc"
 12
 13--protocol parsing
 14function parse(line)
 15	local prefix
 16	local lineStart = 1
 17	if line:sub(1,1) == ":" then
 18		local space = line:find(" ")
 19		prefix = line:sub(2, space-1)
 20		lineStart = space
 21	end
 22
 23	local _, trailToken = line:find("%s+:", lineStart)
 24	local lineStop = line:len()
 25	local trailing
 26	if trailToken then
 27		trailing = line:sub(trailToken + 1)
 28		lineStop = trailToken - 2
 29	end
 30
 31	local params = {}
 32
 33	local _, cmdEnd, cmd = line:find("(%S+)", lineStart)
 34	local pos = cmdEnd + 1
 35	while true do
 36		local _, stop, param = line:find("(%S+)", pos)
 37		
 38		if not param or stop > lineStop then
 39			break
 40		end
 41
 42		pos = stop + 1
 43		params[#params + 1] = param
 44	end
 45
 46	if trailing then 
 47		params[#params + 1] = trailing 
 48	end
 49
 50	return prefix, cmd, params
 51end
 52
 53function parseNick(nick)
 54	local access, name = nick:match("^([%+@]*)(.+)$")
 55	return parseAccess(access or ""), name
 56end
 57
 58function parsePrefix(prefix)
 59	local user = {}
 60	if prefix then
 61		user.access, user.nick, user.username, user.host = prefix:match("^([%+@]*)(.+)!(.+)@(.+)$")
 62	end
 63	user.access = parseAccess(user.access or "")
 64	return user
 65end
 66
 67function parseAccess(accessString)
 68	local access = {op = false, halfop = false, voice = false}
 69	for c in accessString:gmatch(".") do
 70		if     c == "@" then access.op = true
 71		elseif c == "%" then access.halfop = true
 72		elseif c == "+" then access.voice = true
 73		end
 74	end
 75	return access
 76end
 77
 78--mIRC markup scheme (de-facto standard)
 79color = {
 80	black = 1,
 81	blue = 2,
 82	green = 3,
 83	red = 4,
 84	lightred = 5,
 85	purple = 6,
 86	brown = 7,
 87	yellow = 8,
 88	lightgreen = 9,
 89	navy = 10,
 90	cyan = 11,
 91	lightblue = 12,
 92	violet = 13,
 93	gray = 14,
 94	lightgray = 15,
 95	white = 16
 96}
 97
 98local colByte = char(3)
 99setmetatable(color, {__call = function(_, text, colornum)
100	colornum = type(colornum) == "string" and assert(color[colornum], "Invalid color '"..colornum.."'") or colornum
101	return table.concat{colByte, tostring(colornum), text, colByte}
102end})
103
104local boldByte = char(2)
105function bold(text)
106	return boldByte..text..boldByte
107end
108
109local underlineByte = char(31)
110function underline(text)
111	return underlineByte..text..underlineByte
112end
113
114function checkNick(nick)
115	return nick:find("^[a-zA-Z_%-%[|%]%^{|}`][a-zA-Z0-9_%-%[|%]%^{|}`]*$") ~= nil
116end
117
118function defaultNickGenerator(nick)
119	-- LuaBot -> LuaCot -> LuaCou -> ...
120	-- We change a random charachter rather than appending to the
121	-- nickname as otherwise the new nick could exceed the ircd's
122	-- maximum nickname length.
123	local randindex = random(1, #nick)
124	local randchar = sub(nick, randindex, randindex)
125	local b = byte(randchar)
126	b = b + 1
127	if b < 65 or b > 125 then
128		b = 65
129	end
130	-- Get the halves before and after the changed character
131	local first = sub(nick, 1, randindex - 1)
132	local last = sub(nick, randindex + 1, #nick)
133	nick = first..char(b)..last  -- Insert the new charachter
134	return nick
135end
136