From cc156f8a20a9500679e919e64be8cba77fce3ad9 Mon Sep 17 00:00:00 2001 From: Jakob Ovrum Date: Sun, 4 Apr 2010 16:55:12 +0200 Subject: [PATCH] Split into modules --- LICENSE.txt | 25 ++++++++++++++ asyncoperations.lua | 46 +++++++++++++++++++++++++ util.lua | 81 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 LICENSE.txt create mode 100644 asyncoperations.lua create mode 100644 util.lua diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..bd1b7dd --- /dev/null +++ b/LICENSE.txt @@ -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.]] \ No newline at end of file diff --git a/asyncoperations.lua b/asyncoperations.lua new file mode 100644 index 0000000..8dfe035 --- /dev/null +++ b/asyncoperations.lua @@ -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 diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..11d13cb --- /dev/null +++ b/util.lua @@ -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