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)
43 std::stringstream* gotfile = (std::stringstream*)filedata;
44 ServerInstance->Log(DEBUG,"OnDownloadFile in m_remoteinclude_http");
45 int sockfd, portno, n;
46 struct sockaddr_in serv_addr;
47 struct hostent *server;
51 if (filename.substr(0, 7) != "http://")
55 server = gethostbyname("neuron.brainbox.winbot.co.uk");
57 sockfd = socket(AF_INET, SOCK_STREAM, 0);
60 ServerInstance->Log(DEBUG,"Failed to socket()");
66 ServerInstance->Log(DEBUG,"No such host");
70 memset(&serv_addr, sizeof(serv_addr), 0);
72 serv_addr.sin_family = AF_INET;
74 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
75 serv_addr.sin_port = htons(portno);
77 if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
79 ServerInstance->Log(DEBUG,"Failed to connect()");
83 ServerInstance->Log(DEBUG,"Connected to brainbox");
85 n = this->SockSend(sockfd, "GET / HTTP/1.1\r\nConnection: close\r\nHost: neuron.brainbox.winbot.co.uk\r\n\r\n");
88 ServerInstance->Log(DEBUG,"Failed to send()");
92 ServerInstance->Log(DEBUG,"Sent GET request");
94 while (((n = read(sockfd,buffer,65535)) > 0))
96 std::string output(buffer, 0, n);
97 (*(gotfile)) << output;
100 ServerInstance->Log(DEBUG,"Read page");
102 std::string version, result;
103 (*(gotfile)) >> version;
104 (*(gotfile)) >> result;
106 /* HTTP/1.1 200 OK */
108 ServerInstance->Log(DEBUG,"Result: %s", result.c_str());
110 return (result == "200");
114 int SockSend(int sock, const std::string &data)
116 return send(sock, data.data(), data.length(), 0);
119 virtual Version GetVersion()
121 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
126 MODULE_INIT(ModuleRemoteIncludeHttp)