summaryrefslogtreecommitdiffstats
blob: 5c36a8e7c0f5e3e451c14edbc5712ef63e4387bc (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
75
76
77
78
79
80
/*
 * Copyright (c) 2013 Remko Tronçon
 * Licensed under the GNU General Public License.
 * See the COPYING file for more information.
 */

#include <Sluift/Response.h>

#include <lua.hpp>

#include <Sluift/globals.h>
#include <Swiften/Elements/ErrorPayload.h>
#include <Sluift/Lua/LuaUtils.h>

using namespace Swift;
using namespace Swift::Sluift;

static std::string getErrorString(boost::shared_ptr<ErrorPayload> error) {
	// Copied from ChatControllerBase.
	// TODO: Share this code;
	std::string defaultMessage = "Error sending message";
	if (!error->getText().empty()) {
		return error->getText();
	}
	else {
		switch (error->getCondition()) {
			case ErrorPayload::BadRequest: return "Bad request";
			case ErrorPayload::Conflict: return "Conflict";
			case ErrorPayload::FeatureNotImplemented: return "This feature is not implemented";
			case ErrorPayload::Forbidden: return "Forbidden";
			case ErrorPayload::Gone: return "Recipient can no longer be contacted";
			case ErrorPayload::InternalServerError: return "Internal server error";
			case ErrorPayload::ItemNotFound: return "Item not found";
			case ErrorPayload::JIDMalformed: return "JID Malformed";
			case ErrorPayload::NotAcceptable: return "Message was rejected";
			case ErrorPayload::NotAllowed: return "Not allowed";
			case ErrorPayload::NotAuthorized: return "Not authorized";
			case ErrorPayload::PaymentRequired: return "Payment is required";
			case ErrorPayload::RecipientUnavailable: return "Recipient is unavailable";
			case ErrorPayload::Redirect: return "Redirect";
			case ErrorPayload::RegistrationRequired: return "Registration required";
			case ErrorPayload::RemoteServerNotFound: return "Recipient's server not found";
			case ErrorPayload::RemoteServerTimeout: return "Remote server timeout";
			case ErrorPayload::ResourceConstraint: return "The server is low on resources";
			case ErrorPayload::ServiceUnavailable: return "The service is unavailable";
			case ErrorPayload::SubscriptionRequired: return "A subscription is required";
			case ErrorPayload::UndefinedCondition: return "Undefined condition";
			case ErrorPayload::UnexpectedRequest: return "Unexpected request";
		}
	}
	assert(false);
	return defaultMessage;
}

Response::~Response() {
}

int Response::convertToLuaResult(lua_State* L) {
	if (error) {
		lua_pushnil(L);
		lua_pushstring(L, getErrorString(error).c_str());
		bool converted = Sluift::globals.elementConvertor.convertToLuaUntyped(L, error);
		assert(converted);
		Lua::registerTableToString(L, -1);
		return 3;
	}
	else {
		if (result) {
			bool converted = Sluift::globals.elementConvertor.convertToLuaUntyped(L, result);
			assert(converted);
			Lua::registerTableToString(L, -1);
		}
		else {
			lua_pushboolean(L, 1);
		}
		return 1;
	}

}