]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_rpc_json.cpp
Fix potential for duplicate SID if the SID is auto generated.
[user/henk/code/inspircd.git] / src / modules / m_rpc_json.cpp
index 960a06b9404e6072b40876dd4cb3fca380334ddf..35557fa143282275324300f1b59d247b9f07ed89 100644 (file)
 #include <utility>
 
 #include "inspircd.h"
-#include "users.h"
-#include "channels.h"
-#include "configreader.h"
-#include "modules.h"
 #include "inspsocket.h"
 #include "httpd.h"
 #include "json.h"
 
 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;
-        }
+       void MthModuleVersion (HTTPRequest *http, json::Value &request, json::Value &response)
+       {
+               Version v = this->GetVersion();
+               std::string result = ConvToStr(v.Major) + "." + ConvToStr(v.Minor) + "." + ConvToStr(v.Revision) + "." + ConvToStr(v.Build);
+               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::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);
+               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)
@@ -80,8 +78,10 @@ class ModuleRpcJson : public Module
                                }
 
                                /* Send the document back to m_httpd */
-                               HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_rpc_json.so\r\n"
-                                                                             "Content-Type: application/json; charset=iso-8859-1\r\n");
+                               HTTPDocument response(http->sock, &data, 200);
+                               response.headers.SetHeader("X-Powered-By", "m_rpc_json.so");
+                               response.headers.SetHeader("Content-Type", "application/json; charset=iso-8859-1");
+                               response.headers.SetHeader("Connection", "Keep-Alive");
                                Request req((char*)&response, (Module*)this, event->GetSource());
                                req.Send();
                        }
@@ -95,6 +95,7 @@ class ModuleRpcJson : public Module
 
        virtual ~ModuleRpcJson()
        {
+               ServerInstance->UnpublishInterface("JSON-RPC", this);
        }
 
        virtual Version GetVersion()
@@ -103,37 +104,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 ()
@@ -885,6 +855,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));
@@ -2086,13 +2060,13 @@ namespace json
       
       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;
-        }
+       {
+         mfp m = mthit->second;
+         Module *mod = new Module (*m.mod);
+         method mth = m.mth;
+         (mod->*mth) (http, request, response);
+         delete mod;
+       }
     }
     
     void
@@ -2104,9 +2078,14 @@ namespace json
       Value response (objectValue);
       Reader r;
       Writer w;
-  
+      
+      response["error"] = Value(nullValue);
+      response["result"] = Value(nullValue);
+      
       parse_success = r.parse (request_text, request_text + strlen (request_text), request);
-  
+      
+      response["id"] = request["id"];
+      
       service (http, request, response);
   
       text = w.write (response);