Split into modules

This commit is contained in:
Jakob Ovrum 2010-04-04 16:55:12 +02:00
parent d992dcc9ae
commit cc156f8a20
3 changed files with 152 additions and 0 deletions

25
LICENSE.txt Normal file
View File

@ -0,0 +1,25 @@
--[[
Lua IRC library
Copyright (c) 2010 Jakob Ovrum
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.]]

46
asyncoperations.lua Normal file
View File

@ -0,0 +1,46 @@
local table = table
module "irc"
local meta = _META
function meta:send(fmt, ...)
self.socket:send(fmt:format(...) .. "\r\n")
end
function meta:sendChat(channel, msg)
toChannel = table.concat{"PRIVMSG ", channel, " :"}
for line in msg:gmatch("[^\r\n]+") do
self.socket:send(table.concat{toChannel, line, "\r\n"})
end
end
function meta:join(channel, key)
if key then
self:send("JOIN %s :%s", channel, key)
else
self:send("JOIN %s", channel)
end
end
function meta:part(channel)
self:send("PART %s", channel)
end
function meta:setMode(t)
local target = t.target or self.nick
local mode = ""
local add, rem = t.add, t.remove
assert(add or rem, "table contains neither 'add' nor 'remove'")
if add then
mode = table.concat{"+", add}
end
if rem then
mode = table.concat{mode, "-", rem}
end
self:send("MODE %s %s", target, mode)
end

81
util.lua Normal file
View File

@ -0,0 +1,81 @@
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)
local colonsplit = line:find(":", 2)
local last
if colonsplit then
last = line:sub(colonsplit+1)
line = line:sub(1, colonsplit-2)
end
local prefix
if line:sub(1,1) == ":" then
local space = line:find(" ")
prefix = line:sub(2, space-1)
line = line:sub(space)
end
local params = {}
local it, state, init = line:gmatch("(%S+)")
local cmd = it(state, init)
for sub in it, state, init do
params[#params + 1] = sub
end
if last then params[#params + 1] = last end
return prefix, cmd, params
end
function parsePrefix(prefix)
local user = {}
if prefix then
user.nick, user.username, user.host = prefix:match("(.*)!(.*)@(.*)")
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