soo.lua

changeset 0
cc66ad6b0d75
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/soo.lua	Sat Mar 27 17:43:08 2010 +0000
@@ -0,0 +1,88 @@
+--[[ Lua XMPP MOO Component by Matthew Wild (MattJ)					]]
+
+function onMessage(msg)
+	success, message = pcall(handleMessage, msg);
+	if not success then print("\nERROR: "..message.."\n"); end
+end
+
+function handleMessage(msg)
+	local action, object, room, server, person;
+	person = people[msg.fromFull]
+	room = people["local"].room._properties.name;
+	if not person then print("No such person as "..msg.fromFull, tostring(people[msg.fromFull]).."\n"); end
+	if not rooms[room] then return; end
+
+	person:_processCommand(msg.body);
+	
+	return nil;
+end
+
+function GetOccupantNick(jid)
+	local pjid, p
+	for pjid, p in pairs(people) do
+		if CompareJIDs(pjid, jid) then return p.nick; end
+	end
+	return "user";
+end
+
+function CompareJIDs(j1, j2)
+	return j1 == j2;
+end
+
+function MoveToRoom(person, room)
+	if not ( person and room) then print("Error, can't move person to room, invalid args"); return; end
+	if type(room) ~= "table" then
+		if not rooms[room] then rooms[room] = CreateObject(classes.obj_room, room); rooms[room]:_create{ room = room, server = "" }; end
+		 room = rooms[room];
+	end
+	
+	if person.room ~= room then
+		if person.room then
+			person.room.people[person.nick] = nil; -- Remove from room
+		end
+	end
+	person.room = room;
+	--table.print("rooms", rooms);
+	room.people[person.nick] = person;
+	room:_enter{ room = person.room, person = person, nick = person.nick };
+end
+
+function RemoveFromRoom(room, jid)
+	if not ( room and jid) then return; end
+	for id, t in pairs(room.people) do
+		room.people[id] = nil;
+	end
+end
+
+function BroadcastMessage(people_, from, text, mtype, sender_sees)
+	print("\n|>>"..from..": "..text);
+end
+
+function FSay(room, fromnick, text)
+	if text and fromnick then print("\n|>>"..fromnick..": "..text); end
+end
+
+function SendMessage(to, from, text, type)
+	if to == "local" then print("\n>>"..from..": "..text);
+	else print("\n|>>"..(from or "")..": "..text); end
+end
+
+function PSay(room, tonick, fromnick, text)
+		print("\n>>"..fromnick..": "..text);
+end
+
+dofile("common.lua");
+
+rooms = rooms or { };
+people = people or { ["local"] = setmetatable({ nick = "user", group = {} }, { __index = occupant }); };
+people["local"]._contents = { ["in"] = createcontainertable(); };
+
+
+-- Single user --
+
+if not people["local"].room then MoveToRoom(people["local"],"lounge"); end
+io.write("\n> ");
+for line in io.lines() do
+	onMessage({ fromFull = "local", body = line });
+io.write("\n> ");
+end

mercurial