]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remoteinclude.cpp
3ad9b7ef2fa6beb49ade00c4b75e5437a47760b4
[user/henk/code/inspircd.git] / src / modules / m_remoteinclude.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "httpclient.h"
16
17 /* $ModDesc: The base module for remote includes */
18
19 class ModuleRemoteInclude : public Module
20 {
21         std::map<std::string, std::stringstream*> assoc;
22
23  public:
24         ModuleRemoteInclude(InspIRCd* Me)
25                 : Module(Me)
26         {
27                 ServerInstance->Modules->Attach(I_OnDownloadFile, this);
28                 ServerInstance->Modules->Attach(I_OnRequest, this);
29         }
30         
31         virtual ~ModuleRemoteInclude()
32         {
33         }
34         
35         virtual Version GetVersion()
36         {
37                 // this method instantiates a class of type Version, and returns
38                 // the modules version information using it.
39         
40                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
41         }
42
43         char* OnRequest(Request* req)
44         {
45                 if (!strcmp(req->GetId(), HTTP_CLIENT_RESPONSE))
46                 {
47                         HTTPClientResponse* resp = (HTTPClientResponse*)req;
48                         ServerInstance->Log(DEBUG, "Got http file for %s", resp->GetURL().c_str());
49
50                         std::map<std::string, std::stringstream*>::iterator n = assoc.find(resp->GetURL());
51
52                         if (n == assoc.end())
53                                 ServerInstance->Config->Complete(resp->GetURL(), true);
54                 
55                         std::string responsestr;
56                         if (resp->GetResponse(responsestr) == 200)
57                         {
58                                 *(n->second) << resp->GetData();
59
60                                 ServerInstance->Log(DEBUG, "Got data: %s", resp->GetData().c_str());
61
62                                 ServerInstance->Log(DEBUG, "Flag file complete without error");
63                                 ServerInstance->Config->Complete(resp->GetURL(), false);
64                         }
65                         else
66                                 ServerInstance->Config->Complete(resp->GetURL(), true);
67
68                         /* Erase from our association map, but dont delete the pointer.
69                          * the core will want to access this pointer for the file data.
70                          */
71                         assoc.erase(n);
72                 }
73                 else if (!strcmp(req->GetId(), HTTP_CLIENT_ERROR))
74                 {
75                         HTTPClientError* resp = (HTTPClientError*)req;
76
77                         ServerInstance->Log(DEBUG, "Got http error when accessing %s", resp->GetURL().c_str());
78                         ServerInstance->Config->Complete(resp->GetURL(), true);
79
80                         std::map<std::string, std::stringstream*>::iterator n = assoc.find(resp->GetURL());
81
82                         if (n != assoc.end())
83                                 assoc.erase(n);
84                 }
85                 return NULL;
86         }
87
88         int OnDownloadFile(const std::string &name, std::istream* &filedata)
89         {
90                 if (name.substr(0, 7) == "http://")
91                 {
92                         Module* target = ServerInstance->Modules->Find("m_http_client.so");
93                         if (target)
94                         {
95                                 ServerInstance->Log(DEBUG,"Claiming schema http://, making fetch request");
96
97                                 HTTPClientRequest req(ServerInstance, this, target, name);
98                                 req.Send();
99
100                                 assoc[name] = new std::stringstream();
101                                 delete filedata;
102                                 filedata = assoc[name];
103
104                                 return true;
105                         }
106                 }
107
108                 return false;
109         }
110 };
111
112
113 MODULE_INIT(ModuleRemoteInclude)
114