1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
16 /* $ModDesc: Remote includes HTTP scheme */
18 using irc::sockets::OpenTCPSocket;
20 class ModuleRemoteIncludeHttp : public Module
24 ModuleRemoteIncludeHttp(InspIRCd* Me)
27 // The constructor just makes a copy of the server class
30 Implementation eventlist[] = { I_OnDownloadFile };
31 ServerInstance->Modules->Attach(eventlist, this, 1);
34 virtual ~ModuleRemoteIncludeHttp()
38 virtual int OnDownloadFile(const std::string &filename, std::istream* &filedata)
40 std::stringstream* gotfile = (std::stringstream*)filedata;
41 ServerInstance->Log(DEBUG,"OnDownloadFile in m_remoteinclude_http");
42 int sockfd, portno, n;
43 struct sockaddr_in serv_addr;
44 struct hostent *server;
48 if (filename.substr(0, 7) != "http://")
52 server = gethostbyname("neuron.brainbox.winbot.co.uk");
54 sockfd = socket(AF_INET, SOCK_STREAM, 0);
57 ServerInstance->Log(DEBUG,"Failed to socket()");
63 ServerInstance->Log(DEBUG,"No such host");
67 memset(&serv_addr, sizeof(serv_addr), 0);
69 serv_addr.sin_family = AF_INET;
71 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
72 serv_addr.sin_port = htons(portno);
74 if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
76 ServerInstance->Log(DEBUG,"Failed to connect()");
80 ServerInstance->Log(DEBUG,"Connected to brainbox");
82 n = this->SockSend(sockfd, "GET / HTTP/1.1\r\nConnection: close\r\nHost: neuron.brainbox.winbot.co.uk\r\n\r\n");
85 ServerInstance->Log(DEBUG,"Failed to send()");
89 ServerInstance->Log(DEBUG,"Sent GET request");
91 while (((n = read(sockfd,buffer,65535)) > 0))
93 std::string output(buffer, 0, n);
94 (*(gotfile)) << output;
97 ServerInstance->Log(DEBUG,"Read page");
99 std::string version, result;
100 (*(gotfile)) >> version;
101 (*(gotfile)) >> result;
103 /* HTTP/1.1 200 OK */
105 ServerInstance->Log(DEBUG,"Result: %s", result.c_str());
107 return (result == "200");
110 int SockSend(int sock, const std::string &data)
112 return send(sock, data.data(), data.length(), 0);
115 virtual Version GetVersion()
117 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
122 MODULE_INIT(ModuleRemoteIncludeHttp)