commands.lua

1
-- Here we process special commands, for example, those with more than one object specified
2
 
3
commands = {};
4
 
5
function commands:_parse(person, command)
6
	if person.group.perm_edit and command:match("^@.*$") then
7
		local verb, param = command:match("^@(%a+)%s?(.*)$");
8
	end
9
	local verb, object = command:match("^(%a+%s%a+)%s(.+)$");
10
	if verb and object and #verb > 0 and #object > 0 and person.room:_findobject(object) then
11
		return verb, object;
12
	end
13
	verb, object = command:match("^(%a+)%s?(.*)$");
14
	if verb and object and #verb > 0 and #object > 0 then
15
		return verb, object;
16
	end
17
	if verb and ((not object) or (object == "")) then return verb, nil; end
18
end
19
 
20
function commands:_handle(person, action, object)
21
	-- if not (person and action) then print("Invalid parameters!"); return; end
22
	function callobjectmethod(object)
23
		local o = person.room._objects:findobject(object) or person._contents["in"]:findobject(object);
24
		if o and o[action] then o[action](o, {room = person.room, person = person, nick = person.nick }); return true; end
25
		return false;
26
	end
27
	
28
	if commands[action] then commands[action](person, action, object);
29
	elseif object and object ~= "" and callobjectmethod(object) then return true;
30
	elseif occupant[action] then occupant[action](person, { room = person.room, person = person }); return true;
31
	elseif action and type(person.room[action]) == "function" then person.room[action](person.room, { room = person.room, person = person, jid = person.jid, nick = person.nick, param = object }); return true;
32
	else return false; -- Unable to understand command
33
	end
34
end
35
 
36
function commands.get(person, action, param)
37
	-- Decide if we are taking it from a container
38
	obj1, obj2 = param:match("^([%a%s]+)%sfrom%s([%a%s]+)$");
39
	if obj1 and obj2 then -- We are taking from a container
40
		local container = person:_findobject(obj2) or person.room:_findobject(obj2);
41
		if not container then person:_tell(string.format("I don't see any '%s' here.", obj2 or "")); return; end
42
		if not (container._contents and container._contents["in"]) then person:_tell(string.format("The %s is not a container.", obj2 or "")); return; end
43
		object = container:_findobject(obj1);
44
		if not object then
45
			if object == false then person:_tell(string.format("Sorry, the %s is locked.", obj2)); 
46
			else person:_tell(string.format("I can't find any %s in there.", obj1)); end
47
			return;
48
		end
49
		if person:_get({ object = object, container = container, name = obj1 }) then
50
			person:_say(string.format("/me gets %s from %s", object._properties.longname or object._properties.name or "something", container._properties.longname or container._properties.name or "something"));
51
		else
52
			person:_tell("You can't get that");
53
		end
54
		return;
55
	end
56
	obj1 = param:match("^([%a%s]+)$");
57
	if not obj1 then person:_tell("uh?"); return; end
58
	-- So we are getting an object just in the room
59
	local container = person.room;
60
	local object = container:_findobject(obj1);
61
	if not object then person:_tell(string.format("I can't find any %s in here.", obj1)); return; end
62
	if person:_get({ object = object, container = container, name = obj1}) then
63
		person:_say(string.format("/me picks up %s", object._properties.longname or object._properties.name or "something"));
64
	else
65
		person:_tell("You can't get that");
66
	end
67
end
68
 
69
function commands.put(person, action, param)
70
	obj1, obj2 = param:match("^([%a%s]+)%sin%s([%a%s]+)$");
71
	if not (obj1 and obj2) then return end
72
	local oldcontainer = person; -- Where the object currently is
73
	local object = person:_findobject(obj1);
74
	if not object then object = person.room:_findobject(obj1); oldcontainer = person.room; end
75
	if not object then person:_tell(string.format("Sorry, I can't find the '%s'.", obj1)); return; end
76
	local newcontainer = person.room:_findobject(obj2);
77
	if not newcontainer then person:_tell(string.format("I don't see any '%s' here.", obj2 or "")); return; end
78
	if not (newcontainer._contents and newcontainer._contents["in"]) then person:_tell(string.format("The %s is not a container.", obj2 or "")); return; end
79
	
80
	if newcontainer:_get({ object = object, container = oldcontainer }) then
81
		person:_say(string.format("/me puts %s in %s", object._properties.longname or object._properties.name or "something", newcontainer._properties.longname or newcontainer._properties.name or "something"));
82
	else
83
		person:_tell("You can't put that in there");
84
	end
85
end
86
 
87
function commands.drop(person, action, param)
88
	 local o = person:_findobject(param);
89
	 if not o then person:_tell("You don't seem to have any "..param); return false; end
90
	if person.room:_get({ object = o, container = person }) then
91
		person:_say("/me drops "..(o._properties.longname or o._properties.name));
92
	end
93
end
94
 
95
function commands.give(person, action, param)
96
	if not param then person:_tell("Give what to whom?"); return; end
97
	local objname, tonick = param:match("([%a%s]-)%s* to (.*)");
98
	if not objname or not tonick then person:_tell("Give what to whom?"); return; end
99
	local o = person:_findobject(objname);
100
	 if not o then person:_tell("You don't seem to have any "..objname); return false; end
101
	if not person.room.people[tonick] then person:_tell("Sorry, I can't find "..(tonick or "that person").."in this room..."); return; end
102
	if person.room.people[tonick]:_get({object = o, container = person}) then
103
		person:_action("gives "..(o._properties.longname or ((o._properties.prepend or "the").." "..o._properties.name)).." to "..param);
104
	end
105
end
106
 
107
function commands.help(person, action, param)
108
	
109
end
110
 
111
function commands.loadclass(person, action, param)
112
	local subenv = { classes = { } }
113
	local classdefinition = loadfile("objects/"..param..".lua");
114
	setfenv(classdefinition, subenv);
115
	local success, message = pcall(classdefinition);
116
	if not success then person:_tell(string.format("Error loading %s: %s", param, message));
117
	else
118
		for k, v in pairs(classes[param]) do classes[param][k] = nil; end -- Clear current class
119
		for k, v in pairs(subenv.classes[param]) do classes[param][k] = v; end -- Fill with data from new definition
120
		person:_tell("Loaded");
121
	end
122
end
123
 
124
function commands.dump(person, action, param)
125
	table.print(param, person.room:_findobject(param) or person:_findobject(param) or classes.default or {});
126
end
127
 
128
function commands.dumproom(person, action, param)
129
	table.print(person.room.name or "room", person.room);
130
end
131
 
132
function commands.loadroom(person, action, param)
133
	if not rooms[param] then person:_tell("You should go into the room, it has not even been created yet."); end
134
	local oldpeople = rooms[param].people;
135
	rooms[param]:_create{ room = param, server = GetJIDParts(person.occjid).server}
136
	rooms[param].people = oldpeople;
137
end
138
 
139
commands["@create"] = function (person, action, param)
140
	local chunk = loadstring(param);
141
	if not chunk then person:_tell("There was an error in your syntax. Please read the manual"); return; end
142
	setfenv(chunk, 1);
143
	local success, message = pcall(chunk);
144
	if not success then person:_tell(message.."\nThere was an error processing the command"); return; end
145
	local t = message;
146
	local newobj = classes[t.class]:_create(t);
147
	if newobj then
148
		person._contents["in"]:add(newobj);
149
		person:_action("waves his hands, and conjures something from thin air!");
150
	else
151
		person:_tell("For some reason, the object was not created");
152
	end
153
end