1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "httpclient.h"
17 /* $ModDesc: The base module for remote includes */
19 class ModuleRemoteInclude : public Module
21 std::map<std::string, std::stringstream*> assoc;
24 ModuleRemoteInclude(InspIRCd* Me)
27 ServerInstance->Modules->Attach(I_OnDownloadFile, this);
28 ServerInstance->Modules->Attach(I_OnRequest, this);
31 virtual ~ModuleRemoteInclude()
35 virtual Version GetVersion()
37 // this method instantiates a class of type Version, and returns
38 // the modules version information using it.
40 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
43 char* OnRequest(Request* req)
45 if (!strcmp(req->GetId(), HTTP_CLIENT_RESPONSE))
47 HTTPClientResponse* resp = (HTTPClientResponse*)req;
48 ServerInstance->Log(DEBUG, "Got http file for %s", resp->GetURL().c_str());
50 std::map<std::string, std::stringstream*>::iterator n = assoc.find(resp->GetURL());
53 ServerInstance->Config->Complete(resp->GetURL(), true);
55 std::string responsestr;
56 if (resp->GetResponse(responsestr) == 200)
58 *(n->second) << resp->GetData();
60 ServerInstance->Log(DEBUG, "Got data: %s", resp->GetData().c_str());
62 ServerInstance->Log(DEBUG, "Flag file complete without error");
63 ServerInstance->Config->Complete(resp->GetURL(), false);
66 ServerInstance->Config->Complete(resp->GetURL(), true);
68 /* Erase from our association map, but dont delete the pointer.
69 * the core will want to access this pointer for the file data.
73 else if (!strcmp(req->GetId(), HTTP_CLIENT_ERROR))
75 HTTPClientError* resp = (HTTPClientError*)req;
77 ServerInstance->Log(DEBUG, "Got http error when accessing %s", resp->GetURL().c_str());
78 ServerInstance->Config->Complete(resp->GetURL(), true);
80 std::map<std::string, std::stringstream*>::iterator n = assoc.find(resp->GetURL());
88 int OnDownloadFile(const std::string &name, std::istream* &filedata)
90 if (name.substr(0, 7) == "http://")
92 Module* target = ServerInstance->Modules->Find("m_http_client.so");
95 ServerInstance->Log(DEBUG,"Claiming schema http://, making fetch request");
97 HTTPClientRequest req(ServerInstance, this, target, name);
100 assoc[name] = new std::stringstream();
102 filedata = assoc[name];
113 MODULE_INIT(ModuleRemoteInclude)