diff options
Diffstat (limited to 'irc.lua')
-rw-r--r-- | irc.lua | 138 |
1 files changed, 64 insertions, 74 deletions
@@ -1,13 +1,24 @@ local cqueues = require'cqueues' local socket = require'cqueues.socket' -local condition = require'cqueues.condition' local pprint = require'pprint' local rirc = require'rirc' +local Queue = require'queue' local Irc = {} -function Irc.makepylon(pylonname, conf) - local self = {pylonname=pylonname} +function Irc._send(self, args) + args.source = args.source or self.nodename + local sent = rirc.send(self.sock, args) + print('>', sent) +end + +function Irc.makepylon(pylonname, conf, cq, network) + local self = { + pylonname=pylonname, + cq = cq, + network = network, + inbox = Queue.make(), + } local function conf_var(name) assert(conf[name] ~= nil, 'missing conf field '..name) self[name] = conf[name] @@ -17,21 +28,20 @@ function Irc.makepylon(pylonname, conf) conf_var 'password' conf_var 'nodename' - self.outqueue = {} - self.outcv = condition.new() setmetatable(self, {__index=Irc}) return self end -function Irc.send(self, args) - args.source = args.source or self.nodename - rirc.send(self.sock, args) +function Irc._connect(self) + self.sock = assert(socket.connect(self.host, self.port)) + self:_send{'PASS', self.password, '0210-IRC', 'wilson|'} + self:_send{'SERVER', self.nodename, '1', 'i am wilson'} end -function Irc.connect(self) - self.sock = assert(socket.connect(self.host, self.port)) - self:send{'PASS', self.password, '0210-IRC', 'wilson|'} - self:send{'SERVER', self.nodename, '1', 'i am wilson'} +function Irc.run(self) + self:_connect() + self.cq:wrap(self.recving, self) + self.cq:wrap(self.sending, self) end function Irc.recving(self) @@ -39,20 +49,15 @@ function Irc.recving(self) print('<', line) local msg = rirc.parse(line) if msg.op == 'PING' then - self:send{'PONG', msg.args[1]} + self:_send{'PONG', msg.args[1]} elseif msg.op == 'PRIVMSG' then - local ch = msg.args[1] + local channel = msg.args[1] local body = msg.args[2] local source = msg.source - local neighbors = self.network:neighbors_of(self.pylonname, ch) - pprint('gt neighbors', neighbors) - for _,other in ipairs(neighbors) do - other.pylon:enqueue { - channel = other.channel, - body = body, - source = source..'[i]' - } - end + self.network:post(self.pylonname, channel, { + body = body, + source = source..'[i]' + }) end end end @@ -61,69 +66,54 @@ function Irc.sending(self) local nicks_channels = {} local function ensure_joined(nick, channel) if not nicks_channels[nick] then - self:send{'NICK', nick, 1, 'username', 'host', 1, '+', 'realname'} + self:_send{'NICK', nick, 1, 'username', 'host', 1, '+', 'realname'} nicks_channels[nick] = {} end if not nicks_channels[nick][channel] then - self:send{source=nick, 'JOIN', channel} + self:_send{source=nick, 'JOIN', channel} nicks_channels[nick][channel] = true end end local function say(nick, channel, body) ensure_joined(nick, channel) - self:send{source=nick, 'PRIVMSG', channel, body} + self:_send{source=nick, 'PRIVMSG', channel, body} end say('WILSON', '#test', 'i am wilson') - while true do - while #self.outqueue > 0 do - local queue = self.outqueue - self.outqueue = {} - for _, outmsg in pairs(queue) do - assert(say(outmsg.source, outmsg.channel, outmsg.body)) - end - end - self.outcv:wait() + for ch, msg in self.inbox:iter() do + say(msg.source, ch, msg.body) end end -function Irc.enqueue(self, message) - table.insert(self.outqueue, message) - self.outcv:signal() -end - -function Irc.run(self, cq, network) - self.network = network - self:connect() - cq:wrap(self.recving, self) - cq:wrap(self.sending, self) -end - - -local cq = cqueues.new() -local conf = { - host = 'localhost', - port = '6667', - password = 'mypassword', - nodename = 'wilson.ubq323', -} - -local dummy_pylon = { - enqueue = function(self, msg) - print(string.format( - "%s <%s> | %s", - msg.channel, msg.source, msg.body)) - end -} - -local network = {neighbors_of = function(self, pylonname, channel) - print('looking for neighbors of '..pylonname..', '..channel) - return { - {pylon = dummy_pylon, channel='#bridge'} - } -end} - -local pylon = Irc.makepylon('test', conf) -pylon:run(cq, network) -pprint(cq:loop()) +return Irc + +-- local cq = cqueues.new() +-- local conf = { +-- host = 'localhost', +-- port = '6667', +-- password = 'mypassword', +-- nodename = 'wilson.ubq323', +-- } + +-- local dummy_network = { +-- post = function(self, pylonname, channel, message) +-- pprint(pylonname, channel, message) +-- end +-- } + +-- local pylon = Irc.makepylon('test', conf, cq, dummy_network) +-- pylon:run() +-- cq:wrap(function() +-- local i = 0 +-- while true do +-- cqueues.sleep(1) +-- pylon.inbox:enqueue{ +-- source = 'helen', +-- channel = '#test', +-- body = 'i am helen '..i, +-- } +-- i = i + 1 +-- end +-- end) +-- pprint('cheese', cq:loop()) |