summaryrefslogtreecommitdiff
path: root/main.lua
blob: 3f93cd933f1f7e4c6df0c015e514877879483ec1 (plain)
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
local cqueues = require'cqueues'
local config = require'config'
local Channel = require'channel'

local pylon_classes = {
	irc = require'irc.pylon',
	xmpp = require'xmpp.pylon',
	discord = require'discord.pylon',
	nanochat_send = require'nanochat_send.pylon',
}

local Wilson = {}
function Wilson.make(conf)
	local self = {
		pylons={},
		busses={},
		cq = cqueues.new(),
	}
	for name, pylon_conf in pairs(conf.pylon) do
		pylon_conf.name = name
		local pylon_class = pylon_classes[pylon_conf.type]
		assert(pylon_class, "no such pylon type "..pylon_conf.type)
		self.pylons[name] = pylon_class.make(self, pylon_conf)
		print("constructed pylon",name,pylon_conf.type)
	end
	for name, bus_conf in pairs(conf.bus) do
		local bus = {}
		for _, item in ipairs(bus_conf) do
			local pylon_name, channel_descriptor = item[1], item[2]
			local pylon = self.pylons[pylon_name]
			assert(pylon,"no such pylon named "..pylon_name)
			table.insert(bus, Channel(pylon, channel_descriptor))
		end
		print("constructed bus  ",name,'with '..#bus_conf..' channels')
		self.busses[name] = bus
	end
	return setmetatable(self, {__index=Wilson})
end

-- find bus containing given channel
function Wilson._find_bus(self, channel)
	for name, bus in pairs(self.busses) do
		for _, q in ipairs(bus) do if q == channel then return bus end end
	end
	print("warning: unfound bus for",channel)
end
function Wilson.deliver(self, source_channel, message)
	local bus = self:_find_bus(source_channel)
	if bus then
		for _, dest_channel in ipairs(bus) do
			if source_channel ~= dest_channel then
				dest_channel.pylon:post(dest_channel, message)
			end
		end
	end
end
function Wilson.run(self)
	for pylonname, pylon in pairs(self.pylons) do
		self.cq:wrap(pylon.run, pylon)
		print("now running pylon", pylonname)
	end
	for busname, bus in pairs(self.busses) do
		for _, channel in ipairs(bus) do
			channel.pylon:add_channel(channel)
		end
	end
	print('toplevel',self.cq:loop())
end

local config_file = assert(io.open("wilson.ini","r"))
local conf = config.parse(config_file)
config_file:close()
local wilson = Wilson.make(conf)
wilson:run()