Skip to content

Commit 0e82f48

Browse files
committed
Cleanup to not use module()
The only strange thing was how init.lua has async.lua fill in the metatable.
1 parent 76a621d commit 0e82f48

File tree

4 files changed

+59
-49
lines changed

4 files changed

+59
-49
lines changed

asyncoperations.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ local error = error
44
local select = select
55
local pairs = pairs
66

7-
module "irc"
8-
9-
local meta = _META
7+
local meta = {}
108

119
function meta:send(msg, ...)
1210
if select("#", ...) > 0 then
@@ -87,3 +85,5 @@ function meta:setMode(t)
8785

8886
self:send("MODE %s %s", verify(target, 3), mode)
8987
end
88+
89+
return meta

handlers.lua

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ local error = error
33
local tonumber = tonumber
44
local table = table
55

6-
module "irc"
6+
local util = require "irc.util"
77

8-
handlers = {}
8+
local handlers = {}
99

1010
handlers["PING"] = function(o, prefix, query)
1111
o:send("PONG :%s", query)
@@ -17,15 +17,15 @@ handlers["001"] = function(o, prefix, me)
1717
end
1818

1919
handlers["PRIVMSG"] = function(o, prefix, channel, message)
20-
o:invoke("OnChat", parsePrefix(prefix), channel, message)
20+
o:invoke("OnChat", util.parsePrefix(prefix), channel, message)
2121
end
2222

2323
handlers["NOTICE"] = function(o, prefix, channel, message)
24-
o:invoke("OnNotice", parsePrefix(prefix), channel, message)
24+
o:invoke("OnNotice", util.parsePrefix(prefix), channel, message)
2525
end
2626

2727
handlers["JOIN"] = function(o, prefix, channel)
28-
local user = parsePrefix(prefix)
28+
local user = util.parsePrefix(prefix)
2929
if o.track_users then
3030
if user.nick == o.nick then
3131
o.channels[channel] = {users = {}}
@@ -38,7 +38,7 @@ handlers["JOIN"] = function(o, prefix, channel)
3838
end
3939

4040
handlers["PART"] = function(o, prefix, channel, reason)
41-
local user = parsePrefix(prefix)
41+
local user = util.parsePrefix(prefix)
4242
if o.track_users then
4343
if user.nick == o.nick then
4444
o.channels[channel] = nil
@@ -50,7 +50,7 @@ handlers["PART"] = function(o, prefix, channel, reason)
5050
end
5151

5252
handlers["QUIT"] = function(o, prefix, msg)
53-
local user = parsePrefix(prefix)
53+
local user = util.parsePrefix(prefix)
5454
if o.track_users then
5555
for channel, v in pairs(o.channels) do
5656
v.users[user.nick] = nil
@@ -60,7 +60,7 @@ handlers["QUIT"] = function(o, prefix, msg)
6060
end
6161

6262
handlers["NICK"] = function(o, prefix, newnick)
63-
local user = parsePrefix(prefix)
63+
local user = util.parsePrefix(prefix)
6464
if o.track_users then
6565
for channel, v in pairs(o.channels) do
6666
local users = v.users
@@ -97,7 +97,7 @@ handlers["353"] = function(o, prefix, me, chanType, channel, names)
9797

9898
local users = o.channels[channel].users
9999
for nick in names:gmatch("(%S+)") do
100-
local access, name = parseNick(nick)
100+
local access, name = util.parseNick(nick)
101101
users[name] = {access = access}
102102
end
103103
end
@@ -130,7 +130,7 @@ handlers["333"] = function(o, prefix, me, channel, nick, time)
130130
end
131131

132132
handlers["KICK"] = function(o, prefix, channel, kicked, reason)
133-
o:invoke("OnKick", channel, kicked, parsePrefix(prefix), reason)
133+
o:invoke("OnKick", channel, kicked, util.parsePrefix(prefix), reason)
134134
end
135135

136136
--RPL_UMODEIS
@@ -164,11 +164,13 @@ handlers["MODE"] = function(o, prefix, target, modes, ...)
164164
end
165165
end
166166
end
167-
o:invoke("OnModeChange", parsePrefix(prefix), target, modes, ...)
167+
o:invoke("OnModeChange", util.parsePrefix(prefix), target, modes, ...)
168168
end
169169

170170
handlers["ERROR"] = function(o, prefix, message)
171171
o:invoke("OnDisconnect", message, true)
172172
o:shutdown()
173173
error(message, 3)
174174
end
175+
176+
return handlers

init.lua

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@ local unpack = unpack
77
local pairs = pairs
88
local assert = assert
99
local require = require
10-
local tonumber = tonumber
1110
local type = type
1211
local pcall = pcall
1312

14-
module "irc"
15-
1613
local meta = {}
1714
meta.__index = meta
18-
_META = meta
1915

20-
require "irc.util"
21-
require "irc.asyncoperations"
22-
require "irc.handlers"
16+
local util = require "irc.util"
17+
for k, v in pairs(require "irc.asyncoperations") do meta[k] = v end
18+
local handlers = require "irc.handlers"
2319

2420
local meta_preconnect = {}
2521
function meta_preconnect.__index(o, k)
@@ -31,16 +27,16 @@ function meta_preconnect.__index(o, k)
3127
return v
3228
end
3329

34-
function new(data)
30+
local function new(data)
3531
local o = {
3632
nick = assert(data.nick, "Field 'nick' is required");
3733
username = data.username or "lua";
3834
realname = data.realname or "Lua owns";
39-
nickGenerator = data.nickGenerator or defaultNickGenerator;
35+
nickGenerator = data.nickGenerator or util.defaultNickGenerator;
4036
hooks = {};
4137
track_users = true;
4238
}
43-
assert(checkNick(o.nick), "Erroneous nickname passed to irc.new")
39+
assert(util.checkNick(o.nick), "Erroneous nickname passed to irc.new")
4440
return setmetatable(o, meta_preconnect)
4541
end
4642

@@ -172,16 +168,14 @@ function meta:think()
172168
local line = getline(self, 3)
173169
if line and #line > 0 then
174170
if not self:invoke("OnRaw", line) then
175-
self:handle(parse(line))
171+
self:handle(util.parse(line))
176172
end
177173
else
178174
break
179175
end
180176
end
181177
end
182178

183-
local handlers = handlers
184-
185179
function meta:handle(prefix, cmd, params)
186180
local handler = handlers[cmd]
187181
if handler then
@@ -205,7 +199,7 @@ function meta:whois(nick)
205199
while true do
206200
local line = getline(self, 3)
207201
if line then
208-
local prefix, cmd, args = parse(line)
202+
local prefix, cmd, args = util.parse(line)
209203

210204
local handler = whoisHandlers[cmd]
211205
if handler then
@@ -231,3 +225,7 @@ function meta:topic(channel)
231225
self:send("TOPIC %s", channel)
232226
end
233227

228+
return {
229+
_META = meta;
230+
new = new;
231+
}

util.lua

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ local tostring = tostring
88
local type = type
99
local random = math.random
1010

11-
module "irc"
12-
1311
--protocol parsing
14-
function parse(line)
12+
local function parse(line)
1513
local prefix
1614
local lineStart = 1
1715
if line:sub(1,1) == ":" then
@@ -50,12 +48,23 @@ function parse(line)
5048
return prefix, cmd, params
5149
end
5250

53-
function parseNick(nick)
51+
local function parseAccess(accessString)
52+
local access = {op = false, halfop = false, voice = false}
53+
for c in accessString:gmatch(".") do
54+
if c == "@" then access.op = true
55+
elseif c == "%" then access.halfop = true
56+
elseif c == "+" then access.voice = true
57+
end
58+
end
59+
return access
60+
end
61+
62+
local function parseNick(nick)
5463
local access, name = nick:match("^([%+@]*)(.+)$")
5564
return parseAccess(access or ""), name
5665
end
5766

58-
function parsePrefix(prefix)
67+
local function parsePrefix(prefix)
5968
local user = {}
6069
if prefix then
6170
user.access, user.nick, user.username, user.host = prefix:match("^([%+@]*)(.+)!(.+)@(.+)$")
@@ -64,19 +73,8 @@ function parsePrefix(prefix)
6473
return user
6574
end
6675

67-
function 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
76-
end
77-
7876
--mIRC markup scheme (de-facto standard)
79-
color = {
77+
local color = {
8078
black = 1,
8179
blue = 2,
8280
green = 3,
@@ -102,20 +100,20 @@ setmetatable(color, {__call = function(_, text, colornum)
102100
end})
103101

104102
local boldByte = char(2)
105-
function bold(text)
103+
local function bold(text)
106104
return boldByte..text..boldByte
107105
end
108106

109107
local underlineByte = char(31)
110-
function underline(text)
108+
local function underline(text)
111109
return underlineByte..text..underlineByte
112110
end
113111

114-
function checkNick(nick)
112+
local function checkNick(nick)
115113
return nick:find("^[a-zA-Z_%-%[|%]%^{|}`][a-zA-Z0-9_%-%[|%]%^{|}`]*$") ~= nil
116114
end
117115

118-
function defaultNickGenerator(nick)
116+
local function defaultNickGenerator(nick)
119117
-- LuaBot -> LuaCot -> LuaCou -> ...
120118
-- We change a random charachter rather than appending to the
121119
-- nickname as otherwise the new nick could exceed the ircd's
@@ -134,3 +132,15 @@ function defaultNickGenerator(nick)
134132
return nick
135133
end
136134

135+
return {
136+
parse = parse;
137+
parseNick = parseNick;
138+
parsePrefix = parsePrefix;
139+
parseAccess = parseAccess;
140+
141+
color = color;
142+
bold = bold;
143+
underline = underline;
144+
checkNick = checkNick;
145+
defaultNickGenerator = defaultNickGenerator;
146+
}

0 commit comments

Comments
 (0)