]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_rpc_json.cpp
Fix new millisec /map to compile on windows, by ifndef gettimeofday out reverting...
[user/henk/code/inspircd.git] / src / modules / m_rpc_json.cpp
index 1cbbcf2547931fb738bb95092ae362fd470b1ad6..0502c7cbdcb4b13dfef60f0f589fcb665655a709 100644 (file)
 
 class ModuleRpcJson : public Module
 {
+       void MthModuleVersion (HTTPRequest *http, json::Value &request, json::Value &response)
+       {
+               std::string result = "GetVersion().ToString()";
+               response["result"] = result;
+       }
+
+       void system_list_methods (HTTPRequest *http, json::Value &request, json::Value &response)
+       {
+               unsigned i = 0;
+               json::Value method_list (json::arrayValue);
+               
+               json::rpc::method_map::iterator it;
+               for (it = json::rpc::methods.begin(); it != json::rpc::methods.end(); ++it)
+               {
+                       method_list[i] = json::Value (it->first);
+                       i++;
+               }
+               
+               response["result"] = method_list;
+       }
+
  public:
        ModuleRpcJson(InspIRCd* Me) : Module(Me)
        {
-               json::rpc::init ();
+               ServerInstance->PublishInterface("JSON-RPC", this);
+               json::rpc::add_method ("system.listMethods", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::system_list_methods);
+               json::rpc::add_method ("ircd.moduleVersion", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::MthModuleVersion);
        }
 
        void OnEvent(Event* event)
@@ -46,16 +69,16 @@ class ModuleRpcJson : public Module
 
                        if (http->GetURI() == "/jsonrpc" && http->GetType() == "POST")
                        {
-                               std::string response_text;
-                                try
-                                {
-                                        json::rpc::process (http, response_text, http->GetPostData().c_str());
-                                }
-                                catch (std::runtime_error &)
-                                {
-                                        // ignore
-                                }
-                               data << response_text;
+                               try
+                               {
+                                       std::string response_text;
+                                       json::rpc::process (http, response_text, http->GetPostData().c_str());
+                                       data << response_text;
+                               }
+                               catch (std::runtime_error &)
+                               {
+                                       data << "{ \"result\": \"JSON Fault\", \"error\": \"Invalid RPC call\", \"id\": 1}";
+                               }
 
                                /* Send the document back to m_httpd */
                                HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_rpc_json.so\r\n"
@@ -73,6 +96,7 @@ class ModuleRpcJson : public Module
 
        virtual ~ModuleRpcJson()
        {
+               ServerInstance->UnpublishInterface("JSON-RPC", this);
        }
 
        virtual Version GetVersion()
@@ -81,37 +105,6 @@ class ModuleRpcJson : public Module
        }
 };
 
-static void
-unreachable_internal (char const *file, int line, char const *function)
-{
-  char buf[1024];
-  snprintf (buf, 1024, "%s (%d) [%s] critical: Unreachable line reached.",
-           file, line, function);
-
-  throw std::runtime_error (buf);
-}
-
-static void
-throw_unless_internal (char const *file, int line, char const *function, char const *condition)
-{
-  char buf[1024];
-  snprintf (buf, 1024, "%s (%d) [%s] critical: Assertion `%s' failed.",
-           file, line, function, condition);
-
-  throw std::runtime_error (buf);
-}
-
-static void
-throw_msg_unless_internal (char const *file, int line, char const *function, char const *message)
-{
-  char buf[1024];
-  snprintf (buf, 1024, "%s (%d) [%s] critical: %s.",
-           file, line, function, message);
-
-  throw std::runtime_error (buf);
-}
-
-
 namespace json
 {
   ValueIteratorBase::ValueIteratorBase ()
@@ -863,6 +856,10 @@ namespace json
   
     throw std::runtime_error (buf);
   }
+
+#define throw_unreachable                       unreachable_internal (__FILE__, __LINE__, CURFUNC)
+#define throw_unless(condition)                 if (!expect_false (condition)) throw_unless_internal (__FILE__, __LINE__, CURFUNC, #condition)
+#define throw_msg_unless(condition, message)    if (!expect_false (condition)) throw_msg_unless_internal (__FILE__, __LINE__, CURFUNC, message)
   
   const Value Value::null;
   const int Value::minInt = int (~ (unsigned (-1)/2));
@@ -2048,36 +2045,13 @@ namespace json
 {
   namespace rpc
   {
-    typedef std::map<std::string, void (*) (HTTPRequest *, Value &, Value &)> method_map;
-  
     method_map methods;
   
     void
-    add_method (char *name, method mth)
-    {
-      methods[name] = mth;
-    }
-  
-    void
-    system_list_methods (HTTPRequest *http, Value &request, Value &response)
-    {
-      unsigned i = 0;
-      Value method_list (arrayValue);
-    
-      method_map::iterator it;
-      for (it = methods.begin(); it != methods.end(); ++it)
-       {
-         method_list[i] = Value (it->first);
-         i++;
-       }
-    
-      response["result"] = method_list;
-    }
-
-    void
-    init ()
+    add_method (char *name, Module const *mod, method mth)
     {
-      add_method ("system.listMethods", &system_list_methods);
+      mfp m = { mod, mth };
+      methods[name] = m;
     }
   
     void
@@ -2085,9 +2059,15 @@ namespace json
     {
       char const *methodName = static_cast<char const *> (request["method"]);
       
-      method_map::iterator mth = methods.find (methodName);
-      if (mth != methods.end ())
-       (*mth->second) (http, request, response);
+      method_map::iterator mthit = methods.find (methodName);
+      if (mthit != methods.end ())
+       {
+         mfp m = mthit->second;
+         Module *mod = new Module (*m.mod);
+         method mth = m.mth;
+         (mod->*mth) (http, request, response);
+         delete mod;
+       }
     }
     
     void