* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
* See: http://www.inspircd.org/wiki/index.php/Credits
*
* This program is free but copyrighted software; see
*/
#include "inspircd.h"
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
#include "httpd.h"
#include "rpc.h"
#include <exception>
private:
std::string _what;
public:
- JsonException(const std::string &what)
- : _what(what)
+ JsonException(const std::string &swhat)
+ : _what(swhat)
{
}
-
+
virtual ~JsonException() throw() { }
-
+
virtual const char *what() const throw()
{
return _what.c_str();
class ModuleRpcJson : public Module
{
private:
-
+
public:
- ModuleRpcJson(InspIRCd *Me)
- : Module::Module(Me)
+ ModuleRpcJson(InspIRCd *Me) : Module(Me)
{
ServerInstance->Modules->PublishInterface("RPC", this);
+ Implementation eventlist[] = { I_OnEvent };
+ ServerInstance->Modules->Attach(eventlist, this, 1);
}
-
+
virtual ~ModuleRpcJson()
{
ServerInstance->Modules->UnpublishInterface("RPC", this);
}
-
+
virtual Version GetVersion()
{
- return Version(1, 1, 0, 0, VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
- }
-
- void Implements(char *List)
- {
- List[I_OnEvent] = 1;
+ return Version("$Id$", VF_SERVICEPROVIDER | VF_VENDOR, API_VERSION);
}
-
+
+
virtual void OnEvent(Event *event)
{
if (event->GetEventID() == "httpd_url")
{
HTTPRequest *req = (HTTPRequest*) event->GetData();
-
+
if ((req->GetURI() == "/rpc/json") || (req->GetURI() == "/rpc/json/"))
{
std::stringstream data;
-
+
RPCValue *reqobj = NULL;
-
+
try
{
reqobj = this->JSONParse(req->GetPostData());
-
+
if (!reqobj || (reqobj->GetType() != RPCObject))
throw JsonException("RPC requests must be in the form of a single object");
-
+
RPCValue *method = reqobj->GetObject("method");
if (!method || method->GetType() != RPCString)
throw JsonException("RPC requests must have a 'method' string field");
-
+
RPCValue *params = reqobj->GetObject("params");
if (!params || params->GetType() != RPCArray)
throw JsonException("RPC requests must have a 'params' array field");
-
+
RPCRequest modreq("json", method->GetString(), params);
Event mev((char*) &modreq, this, "RPCMethod");
mev.Send(ServerInstance);
-
+
if (!modreq.claimed)
throw JsonException("Unrecognized method");
-
+
if (!modreq.error.empty())
{
data << "{\"result\":null,\"error\":\"" << modreq.error << "\"";
this->JSONSerialize(modreq.result, data);
data << ",\"error\":null";
}
-
+
if (reqobj->GetObject("id"))
{
data << ",\"id\":";
this->JSONSerialize(reqobj->GetObject("id"), data);
}
data << "}";
-
+
delete reqobj;
reqobj = NULL;
}
delete reqobj;
data << "{\"result\":null,\"error\":\"" << e.what() << "\"}";
}
-
+
HTTPDocument response(req->sock, &data, 200);
response.headers.SetHeader("X-Powered-By", "m_rpc_json.so");
response.headers.SetHeader("Content-Type", "application/json");
response.headers.SetHeader("Connection", "Keep-Alive");
-
+
Request rreq((char*) &response, (Module*) this, event->GetSource());
rreq.Send();
}
}
}
-
+
void AttachToParent(RPCValue *parent, RPCValue *child, const std::string &key = "")
{
if (!parent || !child)
return;
-
+
if (parent->GetType() == RPCArray)
parent->ArrayAdd(child);
else if (parent->GetType() == RPCObject)
else
throw JsonException("Cannot add a value to a non-container");
}
-
+
void AttachToParentReset(RPCValue *parent, RPCValue *&child, std::string &key)
{
AttachToParent(parent, child, key);
child = NULL;
key.clear();
}
-
+
RPCValue *JSONParse(const std::string &data)
{
bool pisobject = false;
std::string pvkey;
RPCValue *aparent = NULL;
RPCValue *value = NULL;
-
+
for (std::string::const_iterator i = data.begin(); i != data.end(); i++)
{
if (instring)
if (*i == '"')
{
instring = false;
-
+
if (pisobject && vkey.empty())
vkey = stmp;
else
value = new RPCValue(stmp);
-
+
stmp.clear();
}
else
stmp += *i;
-
+
continue;
}
-
+
if ((*i == ' ') || (*i == '\t') || (*i == '\r') || (*i == '\n'))
continue;
-
+
if (*i == '{')
{
// Begin object
if ((value) || (pisobject && vkey.empty()))
throw JsonException("Unexpected begin object token ('{')");
-
+
RPCValue *nobj = new RPCValue(RPCObject, aparent);
aparent = nobj;
pvkey = vkey;
// End object
if ((!aparent) || (!pisobject) || (!vkey.empty() && !value))
throw JsonException("Unexpected end object token ('}')");
-
+
// End value
if (value)
AttachToParentReset(aparent, value, vkey);
-
+
if (!aparent->parent)
return aparent;
-
+
value = aparent;
aparent = aparent->parent;
vkey = pvkey;
// Begin string
if (value)
throw JsonException("Unexpected begin string token ('\"')");
-
+
instring = true;
}
else if (*i == ':')
{
if ((!aparent) || (!value) || ((pisobject) && (vkey.empty())))
throw JsonException("Unexpected value seperator token (',')");
-
+
AttachToParentReset(aparent, value, vkey);
}
else if (*i == '[')
// Begin array
if ((value) || (pisobject && vkey.empty()))
throw JsonException("Unexpected begin array token ('[')");
-
+
RPCValue *nar = new RPCValue(RPCArray, aparent);
aparent = nar;
pvkey = vkey;
// End array (also an end value delimiter)
if (!aparent || pisobject)
throw JsonException("Unexpected end array token (']')");
-
+
if (value)
AttachToParentReset(aparent, value, vkey);
-
+
if (!aparent->parent)
return aparent;
-
+
value = aparent;
aparent = aparent->parent;
vkey = pvkey;
{
std::string ds = std::string(i, data.end());
char *eds = NULL;
-
+
errno = 0;
double v = strtod(ds.c_str(), &eds);
-
+
if (errno != 0)
throw JsonException("Error parsing numeric value");
-
+
value = new RPCValue(v);
-
+
i += eds - ds.c_str() - 1;
}
else
throw JsonException("Unknown data in value portion");
}
}
-
+
if (instring)
throw JsonException("Unterminated string");
-
+
if (aparent && pisobject)
throw JsonException("Unterminated object");
else if (aparent && !pisobject)
throw JsonException("Unterminated array");
-
+
if (value)
return value;
else
throw JsonException("No JSON data found");
}
-
+
void JSONSerialize(RPCValue *value, std::stringstream &re)
{
int ac;
}
};
-MODULE_INIT(ModuleRpcJson);
+MODULE_INIT(ModuleRpcJson)