]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remoteinclude_http.cpp
Sorta update this.. won't give a full file list as it's now kinda huge.
[user/henk/code/inspircd.git] / src / modules / m_remoteinclude_http.cpp
1 /*         +------------------------------------+
2  *         | Inspire Internet Relay Chat Daemon |
3  *         +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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
16 /* $ModDesc: Remote includes HTTP scheme */
17
18 using irc::sockets::OpenTCPSocket;
19
20 class ModuleRemoteIncludeHttp : public Module
21 {
22  public:
23
24         ModuleRemoteIncludeHttp(InspIRCd* Me)
25                 : Module(Me)
26         {
27                 // The constructor just makes a copy of the server class
28         
29                 
30                 Implementation eventlist[] = { I_OnDownloadFile };
31                 ServerInstance->Modules->Attach(eventlist, this, 1);
32         }
33
34         virtual ~ModuleRemoteIncludeHttp()
35         {
36         }
37
38         virtual int OnDownloadFile(const std::string &filename, std::istream* &filedata)
39         {
40 #ifdef WIN32
41                 return 0;
42 #else
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;
48                 char buffer[65536];
49
50                 /* Ours? */
51                 if (filename.substr(0, 7) != "http://")
52                         return 0;
53
54                 portno = 80;
55                 server = gethostbyname("neuron.brainbox.winbot.co.uk");
56
57                 sockfd = socket(AF_INET, SOCK_STREAM, 0);
58                 if (sockfd < 0)
59                 {
60                         ServerInstance->Log(DEBUG,"Failed to socket()");
61                         return 0;
62                 }
63
64                 if (server == NULL) 
65                 {
66                         ServerInstance->Log(DEBUG,"No such host");
67                         return 0;
68                 }
69
70                 memset(&serv_addr, sizeof(serv_addr), 0);
71
72                 serv_addr.sin_family = AF_INET;
73
74                 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
75                 serv_addr.sin_port = htons(portno);
76
77                 if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) 
78                 {
79                         ServerInstance->Log(DEBUG,"Failed to connect()");
80                         return 0;
81                 }
82
83                 ServerInstance->Log(DEBUG,"Connected to brainbox");
84
85                 n = this->SockSend(sockfd, "GET / HTTP/1.1\r\nConnection: close\r\nHost: neuron.brainbox.winbot.co.uk\r\n\r\n");
86                 if (n < 0) 
87                 {
88                         ServerInstance->Log(DEBUG,"Failed to send()");
89                         return 0;
90                 }
91
92                 ServerInstance->Log(DEBUG,"Sent GET request");
93
94                 while (((n = read(sockfd,buffer,65535)) > 0))
95                 {
96                         std::string output(buffer, 0, n);
97                         (*(gotfile)) << output;
98                 }
99
100                 ServerInstance->Log(DEBUG,"Read page");
101
102                 std::string version, result;
103                 (*(gotfile)) >> version;
104                 (*(gotfile)) >> result;
105
106                 /* HTTP/1.1 200 OK */
107
108                 ServerInstance->Log(DEBUG,"Result: %s", result.c_str());
109
110                 return (result == "200");
111 #endif
112         }
113
114         int SockSend(int sock, const std::string &data)
115         {
116                 return send(sock, data.data(), data.length(), 0);
117         }
118
119         virtual Version GetVersion()
120         {
121                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
122         }
123 };
124
125
126 MODULE_INIT(ModuleRemoteIncludeHttp)
127