-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathirc.lua
284 lines (232 loc) · 9.04 KB
/
irc.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-- -------------------------------------------------------------------------- --
-- Aurora - irc.lua - Handles the lowermost irc network connection details. --
-- -------------------------------------------------------------------------- --
-- Copyright (C) 2010 Julius Roob --
-- This program is free software; you can redistribute it and/or modify it --
-- under the terms of the GNU General Public License as published by the --
-- Free Software Foundation; either version 3 of the License, --
-- or (at your option) any later version. --
-- --
-- This program is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. --
-- --
-- You should have received a copy of the GNU General Public License along --
-- with this program; if not, see <http://www.gnu.org/licenses/>. --
-- -------------------------------------------------------------------------- --
-- Our dependencies:
--local luarocks_loader = require("luarocks.loader")
local socket = require("socket")
local pcre = require("rex_pcre")
-- Local Constants - these should not matter outside the module
-- TODO: Fill these with values and translate them when received!
local num_replies = {}
local err_replies = {}
-- Debugging "toolkit"
local function dump_message(data)
if(data.sender.server) then
print("'" .. data.command .. "' from '" .. data.sender.server .. "'")
elseif(data.sender.nick) then
print("'" .. data.command .. "' from '" .. data.sender.nick .. "'")
else
print("'" .. data.command .. "' without sender")
end
for bla, str in pairs(data.parameters) do
print(" " .. str)
end
print()
end
-- Class definition, I preferred the factory method because I like to keep
-- the external interface clean
function irc(name, copas)
-- Local variables
local nickname = nil
local irc_socket = nil
local exit = false
-- 'interface' will be passed out later at the end of our function
local interface = {}
-- These are called first for every incoming message and should,
-- after some preprocessing, pass them to…
local internal_handlers = {}
-- …the external handlers - which are responsible for every functionality
-- not directly concerned with the protocol.
local external_handlers = {}
-- --------------- --
-- Local functions --
-- --------------- --
local function call_external_handler(command, ...)
if log then log:debug("IRC[" .. name .. "]: call_external_handler(" .. tostring(command) .. ", ...)") end
command = string.upper(command)
if(external_handlers[command] ~= nil) then
for i, v in pairs(external_handlers[command]) do
succ, err = pcall(v, interface, ...)
if not succ and log then log:error(string.format("Handler for \"%s\" returned error: '%s'", command, err)) end
end
end
end
local function parse_message(message)
if log then log:debug("IRC[" .. name .. "]: parse_message(\"" .. tostring(message) .. "\")") end
local result = {sender = {}}
assert(type(message) == "string", "'parse_message' requires a string as first parameter, got '" .. type(message) .. "'")
result.sender.nick, result.sender.ident, result.sender.host, result.sender.server, result.command, result.parameters
= pcre.match(message, "^(?::(?:([^ ]+)!([^ ]+)@([^ ]+) |([^ ]+) )|^)([^ ]+)($|.*)$")
local iterator = pcre.gmatch(result.parameters, "[^: ]{1}[^ ]*|:.*")
result.parameters = {}
for param in iterator do
if(string.sub(param, 1, 1) == ":") then
table.insert(result.parameters, string.sub(param, 2))
else
table.insert(result.parameters, param)
end
end
return result
end
local function handle(message)
if log then log:debug("IRC[" .. name .. "]: handle(\"" .. tostring(message) .. "\")") end
local data = parse_message(message)
if(internal_handlers[string.upper(data.command)] == nil) then
call_external_handler(data.command, data.sender, unpack(data.parameters))
else
internal_handlers[string.upper(data.command)](data.sender, unpack(data.parameters))
end
end
-- ---------------- --
-- Public functions --
-- ---------------- --
interface.run = function()
if log then log:debug("IRC[" .. name .. "]: run()") end
assert(irc_socket, "[" .. name .. "] OMG TEH SOCKET IS ASPLODE!")
exit = false
current_socket = irc_socket
while not exit and irc_socket do
local line, err, partial = irc_socket:receive("*l")
--local line, err, partial = copas.receive(irc_socket, "*l")
if(line == nil) then
-- Did another method reconnect while we were waiting for data?
if irc_socket == current_socket then
-- If not, handle.
irc_socket = nil
call_external_handler("disconnect", false, err)
end
exit = true
return line, err, partial
else
handle(line)
end
end
end
interface.send = function (...)
if log then log:debug("IRC[" .. name .. "]: entered send()") end
if not irc_socket then
return nil, "There is no open socket to send to!"
end
message = ""
for key, value in pairs(arg) do
-- Make line breaks not allow injecting irc commands
value = string.gsub(value, "[\r\n]", " ")
if(key ~= "n") then
if(string.find(value, " ")) then
message = message .. ":" .. value .. " "
else
message = message .. value .. " "
end
end
end
if log then log:debug("IRC[" .. name .. "]: send() produced message: " .. message) end
--local succ, err copas.send(irc_socket, message .. "\r\n")
local succ, err = irc_socket:send(message .. "\r\n")
if not succ then
irc_socket = nil
call_external_handler("disconnect", false, err)
end
return succ
end
interface.connect = function(nick, server, port, password)
if log then log:debug("IRC[" .. name .. "]: connect(\"" .. tostring(nick) .. "\", \"" .. tostring(server) .. "\", \"" .. tostring(port) .. "\", \"" .. tostring(password) .. "\")") end
--irc_socket = assert(socket.tcp())
local new_socket, err = socket.connect(server, port)
if new_socket == nil then
return nil, err
else
irc_socket = new_socket
assert(irc_socket:settimeout(1))
irc_socket:setoption("keepalive", true)
irc_socket = assert(copas.wrap(irc_socket))
if(password) then assert(interface.send("PASS", password)) end
assert(interface.send("NICK", nick))
nickname = nick
assert(interface.send("USER", "0", "0", "0", "0"))
return true
end
end
function interface.register_handler(event, handler)
if log then log:debug("IRC[" .. name .. "]: Registering handler " .. tostring(handler) .. " for \"" .. event .. "\".") end
event = string.upper(event)
if(external_handlers[event] == nil) then
external_handlers[event] = {handler}
elseif(type(external_handlers[event]) == "table") then
table.insert(external_handlers[event], handler)
end
end
function interface.unregister_handler(to_remove)
for event, list in pairs(external_handlers) do
if type(list) == "table" then
for key, handler in pairs(external_handlers[event]) do
if handler == to_remove then
if log then log:debug("IRC[" .. name .. "]: Unregistering handler " .. tostring(handler) .. " for \"" .. event .. "\".") end
external_handlers[event][key] = nil
end
end
end
end
end
function interface.disconnect(wanted)
if log then
if wanted then
log:debug("IRC[" .. name .. "]: Disconnect(do want!)")
else
log:debug("IRC[" .. name .. "]: Disconnect(not want!)")
end
end
irc_socket = nil
call_external_handler("disconnect", wanted, err)
end
function interface.name() return name end
function interface.nick() return nickname end
-- ------------------------- --
-- Internal message handlers --
-- ------------------------- --
internal_handlers.PING = function(sender, param)
if log then log:debug("IRC[" .. name .. "]: PING \"" .. tostring(param) .. "\"") end
interface.send("PONG", param)
end
internal_handlers.NICK = function(sender, new)
if sender.nick == nickname then
nickname = new
end
call_external_handler("nick", sender, new)
end
internal_handlers.PRIVMSG = function(sender, channel, message)
-- Checking whether the channel is our nick - in which case the message is private - and overwrite the channel with our sender's nickname.
if(string.upper(channel) == string.upper(nickname)) then
channel = sender.nick
end
-- Did we receive a CTCP?
local cmd, param = pcre.match(message, "\001([^ ]+)(?: (.*))\001")
if(cmd) then
call_external_handler("ctcp", sender, channel, cmd, param)
else
call_external_handler("privmsg", sender, channel, message)
end
end
internal_handlers["376"] = function(...)
call_external_handler("connected")
call_external_handler("376", ...)
end
-- ----- --
-- Fini! --
-- ----- --
return interface
end
return irc