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