diff options
Diffstat (limited to 'irc.lua')
-rw-r--r-- | irc.lua | 82 |
1 files changed, 29 insertions, 53 deletions
@@ -12,23 +12,20 @@ function Irc._send(self, args) print('>', sent) end -function Irc.makepylon(pylonname, conf, cq, network) - local self = { - pylonname=pylonname, - cq = cq, - network = network, +function Irc.check_config(self, vars) + for x in vars:gmatch("%S+") do + assert(self[x], "missing conf field "..x) end end + +function Irc.make(wilson, conf) + local self = setmetatable(conf, {__index=Irc}) + + for k,v in pairs { + wilson = wilson, inbox = Queue.make(), - } - local function conf_var(name) - assert(conf[name] ~= nil, 'missing conf field '..name) - self[name] = conf[name] - end - conf_var 'host' - conf_var 'port' - conf_var 'password' - conf_var 'nodename' + } do self[k] = v end + + self:check_config "host port password nodename" - setmetatable(self, {__index=Irc}) return self end @@ -39,9 +36,11 @@ function Irc._connect(self) end function Irc.run(self) + local cq = cqueues.new() self:_connect() - self.cq:wrap(self.recving, self) - self.cq:wrap(self.sending, self) + cq:wrap(self.recving, self) + cq:wrap(self.sending, self) + print('irc',cq:loop()) end function Irc.recving(self) @@ -51,13 +50,15 @@ function Irc.recving(self) if msg.op == 'PING' then self:_send{'PONG', msg.args[1]} elseif msg.op == 'PRIVMSG' then - local channel = msg.args[1] + local channel_name = msg.args[1] local body = msg.args[2] - local source = msg.source - self.network:post(self.pylonname, channel, { + local sender = msg.source + self.wilson:deliver(Channel(self, channel_name), { body = body, - source = source..'[i]' + sender = sender..'[i]' }) + elseif msg.op == 'ERROR' then + error(msg.args[1]) end end end @@ -81,39 +82,14 @@ function Irc.sending(self) say('WILSON', '#test', 'i am wilson') - for ch, msg in self.inbox:iter() do - say(msg.source, ch, msg.body) + for dest_channel, message in self.inbox:iter() do + local channel_name = dest_channel.descriptor + say(message.sender, channel_name, message.body) end end -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 --- } +function Irc.post(self, dest_channel, message) + self.inbox.enqueue(dest_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()) +return Irc |