diff tiny-nix.lua @ 0:0d4d8b432980 default tip master

Initial commit.
author Kooda <kooda@upyum.com>
date Fri, 27 Aug 2010 17:00:32 +0200
parents
children
line wrap: on
line diff
new file mode 100755
--- /dev/null
+++ b/tiny-nix.lua
@@ -0,0 +1,58 @@
+require("libs.utils");
+local verse = require("verse");
+require("verse.client");
+
+local config = config_load();
+room_map = {
+	[config.muc1.jid] = config.muc2.jid;
+	[config.muc2.jid] = config.muc1.jid;
+};
+local bot = verse.new();
+local occupants = {
+	[config.muc1.jid] = {};
+	[config.muc2.jid] = {};
+};
+
+function new_clone(table, room, nick, presence)
+	table[room][nick] = verse.new();
+	table[room][nick]:connect_client(config.jid.."/"..nick, config.password);
+	table[room][nick]:hook("ready", function()
+		table[room][nick]:send(presence);
+	end);
+end;
+
+function stanza_split(stanza)
+	return jid.bare(stanza.attr.from),
+	room_map[jid.bare(stanza.attr.from)],
+	select(3, jid.split(stanza.attr.from));
+end
+
+bot:hook("ready", function()
+	room_join(bot, config.muc1, config.nickname);
+	room_join(bot, config.muc2, config.nickname);
+end);
+
+bot:hook("stanza", function(stanza)
+	local room_from, room_to, nick = stanza_split(stanza);
+	if not room_from or not room_to or not nick or nick == config.nickname then return; end;
+	if occupants[room_from][nick] then return; end;
+	if stanza.name == "message" and stanza.attr.type ~= "groupchat" then return; end;
+
+	local reply = stanza; reply.attr.from = nil;
+	reply.attr.to = room_to;
+	if stanza.name == "presence" then 
+		reply.attr.to = room_to.."/"..nick;
+		if not occupants[room_to][nick] then new_clone(occupants, room_to, nick, reply); end;
+		if stanza.attr.type == "unavailable" then
+			occupants[room_to][nick]:send(reply);
+			occupants[room_to][nick]:close();
+			occupants[room_to][nick] = nil;
+			occupants[room_from][nick] = nil;
+			return;
+		end
+	end
+	if occupants[room_to][nick] then occupants[room_to][nick]:send(reply); end;
+end);
+
+bot:connect_client(config.jid, config.password);
+verse.loop();