]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remoteinclude_http.cpp
Update m_filter* to 1.2 API (it was still using Implements()), also implement <exempt...
[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                 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;
45                 char buffer[65536];
46
47                 /* Ours? */
48                 if (filename.substr(0, 7) != "http://")
49                         return 0;
50
51                 portno = 80;
52                 server = gethostbyname("neuron.brainbox.winbot.co.uk");
53
54                 sockfd = socket(AF_INET, SOCK_STREAM, 0);
55                 if (sockfd < 0)
56                 {
57                         ServerInstance->Log(DEBUG,"Failed to socket()");
58                         return 0;
59                 }
60
61                 if (server == NULL) 
62                 {
63                         ServerInstance->Log(DEBUG,"No such host");
64                         return 0;
65                 }
66
67                 memset(&serv_addr, sizeof(serv_addr), 0);
68
69                 serv_addr.sin_family = AF_INET;
70
71                 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
72                 serv_addr.sin_port = htons(portno);
73
74                 if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) 
75                 {
76                         ServerInstance->Log(DEBUG,"Failed to connect()");
77                         return 0;
78                 }
79
80                 ServerInstance->Log(DEBUG,"Connected to brainbox");
81
82                 n = this->SockSend(sockfd, "GET / HTTP/1.1\r\nConnection: close\r\nHost: neuron.brainbox.winbot.co.uk\r\n\r\n");
83                 if (n < 0) 
84                 {
85                         ServerInstance->Log(DEBUG,"Failed to send()");
86                         return 0;
87                 }
88
89                 ServerInstance->Log(DEBUG,"Sent GET request");
90
91                 while (((n = read(sockfd,buffer,65535)) > 0))
92                 {
93                         std::string output(buffer, 0, n);
94                         (*(gotfile)) << output;
95                 }
96
97                 ServerInstance->Log(DEBUG,"Read page");
98
99                 std::string version, result;
100                 (*(gotfile)) >> version;
101                 (*(gotfile)) >> result;
102
103                 /* HTTP/1.1 200 OK */
104
105                 ServerInstance->Log(DEBUG,"Result: %s", result.c_str());
106
107                 return (result == "200");
108         }
109
110         int SockSend(int sock, const std::string &data)
111         {
112                 return send(sock, data.data(), data.length(), 0);
113         }
114
115         virtual Version GetVersion()
116         {
117                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
118         }
119 };
120
121
122 MODULE_INIT(ModuleRemoteIncludeHttp)
123