| 1 | --[[ Lua XMPP MOO Component by Matthew Wild (MattJ) ]] |
| 2 | |
| 3 | function onMessage(msg) |
| 4 | success, message = pcall(handleMessage, msg); |
| 5 | if not success then print("\nERROR: "..message.."\n"); end |
| 6 | end |
| 7 | |
| 8 | function handleMessage(msg) |
| 9 | local action, object, room, server, person; |
| 10 | person = people[msg.fromFull] |
| 11 | room = people["local"].room._properties.name; |
| 12 | if not person then print("No such person as "..msg.fromFull, tostring(people[msg.fromFull]).."\n"); end |
| 13 | if not rooms[room] then return; end |
| 14 | |
| 15 | person:_processCommand(msg.body); |
| 16 | |
| 17 | return nil; |
| 18 | end |
| 19 | |
| 20 | function GetOccupantNick(jid) |
| 21 | local pjid, p |
| 22 | for pjid, p in pairs(people) do |
| 23 | if CompareJIDs(pjid, jid) then return p.nick; end |
| 24 | end |
| 25 | return "user"; |
| 26 | end |
| 27 | |
| 28 | function CompareJIDs(j1, j2) |
| 29 | return j1 == j2; |
| 30 | end |
| 31 | |
| 32 | function MoveToRoom(person, room) |
| 33 | if not ( person and room) then print("Error, can't move person to room, invalid args"); return; end |
| 34 | if type(room) ~= "table" then |
| 35 | if not rooms[room] then rooms[room] = CreateObject(classes.obj_room, room); rooms[room]:_create{ room = room, server = "" }; end |
| 36 | room = rooms[room]; |
| 37 | end |
| 38 | |
| 39 | if person.room ~= room then |
| 40 | if person.room then |
| 41 | person.room.people[person.nick] = nil; -- Remove from room |
| 42 | end |
| 43 | end |
| 44 | person.room = room; |
| 45 | --table.print("rooms", rooms); |
| 46 | room.people[person.nick] = person; |
| 47 | room:_enter{ room = person.room, person = person, nick = person.nick }; |
| 48 | end |
| 49 | |
| 50 | function RemoveFromRoom(room, jid) |
| 51 | if not ( room and jid) then return; end |
| 52 | for id, t in pairs(room.people) do |
| 53 | room.people[id] = nil; |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | function BroadcastMessage(people_, from, text, mtype, sender_sees) |
| 58 | print("\n|>>"..from..": "..text); |
| 59 | end |
| 60 | |
| 61 | function FSay(room, fromnick, text) |
| 62 | if text and fromnick then print("\n|>>"..fromnick..": "..text); end |
| 63 | end |
| 64 | |
| 65 | function SendMessage(to, from, text, type) |
| 66 | if to == "local" then print("\n>>"..from..": "..text); |
| 67 | else print("\n|>>"..(from or "")..": "..text); end |
| 68 | end |
| 69 | |
| 70 | function PSay(room, tonick, fromnick, text) |
| 71 | print("\n>>"..fromnick..": "..text); |
| 72 | end |
| 73 | |
| 74 | dofile("common.lua"); |
| 75 | |
| 76 | rooms = rooms or { }; |
| 77 | people = people or { ["local"] = setmetatable({ nick = "user", group = {} }, { __index = occupant }); }; |
| 78 | people["local"]._contents = { ["in"] = createcontainertable(); }; |
| 79 | |
| 80 | |
| 81 | -- Single user -- |
| 82 | |
| 83 | if not people["local"].room then MoveToRoom(people["local"],"lounge"); end |
| 84 | io.write("\n> "); |
| 85 | for line in io.lines() do |
| 86 | onMessage({ fromFull = "local", body = line }); |
| 87 | io.write("\n> "); |
| 88 | end |