]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remoteinclude_http.cpp
Test framework connects now
[user/henk/code/inspircd.git] / src / modules / m_remoteinclude_http.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
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                 ServerInstance->Log(DEBUG,"OnDownloadFile in m_remoteinclude_http");
41                 int sockfd, portno, n;
42                 struct sockaddr_in serv_addr;
43                 struct hostent *server;
44                 char buffer[65536];
45
46                 portno = 80;
47                 server = gethostbyname("neuron.brainbox.winbot.co.uk");
48
49                 sockfd = socket(AF_INET, SOCK_STREAM, 0);
50                 if (sockfd < 0)
51                 {
52                         ServerInstance->Log(DEBUG,"Failed to socket()");
53                         return 0;
54                 }
55
56                 if (server == NULL) 
57                 {
58                         ServerInstance->Log(DEBUG,"No such host");
59                         return 0;
60                 }
61
62                 memset(&serv_addr, sizeof(serv_addr), 0);
63
64                 serv_addr.sin_family = AF_INET;
65
66                 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
67                 serv_addr.sin_port = htons(portno);
68
69                 if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) 
70                 {
71                         ServerInstance->Log(DEBUG,"Failed to connect()");
72                         return 0;
73                 }
74
75                 ServerInstance->Log(DEBUG,"Connected to brainbox");
76
77                 n = send(sockfd, "GET / HTTP/1.0\r\n\r\n", 18, 0);
78                 if (n < 0) 
79                 {
80                         ServerInstance->Log(DEBUG,"Failed to send()");
81                         return 0;
82                 }
83
84                 ServerInstance->Log(DEBUG,"Sent GET request");
85
86                 n = read(sockfd,buffer,1);
87
88                 if (n < 1) 
89                 {
90                         ServerInstance->Log(DEBUG,"Failed to read()");
91                         return 0;
92                 }
93
94                 ServerInstance->Log(DEBUG,"Read one byte");
95
96                 return 1;
97         }
98
99         virtual Version GetVersion()
100         {
101                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
102         }
103 };
104
105
106 MODULE_INIT(ModuleRemoteIncludeHttp)
107