/
TwitchIRC.cpp
301 lines (236 loc) · 5.38 KB
/
TwitchIRC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "stdafx.h"
#include "TwitchIRC.h"
bool setup = false;
char* port;
using namespace std;
recursive_timed_mutex mtx;
TwitchIRC::TwitchIRC()
{
channel = "";
channelSave = "";
nick = "";
usr = "";
password = "";
mySocket = 0;
}
TwitchIRC::TwitchIRC(char * _nick, char * _usr, char * _password)
{
nick = _nick;
usr = _usr;
password = _password;
channel = "";
channelSave = "";
mySocket = 0;
}
TwitchIRC::~TwitchIRC()
{
closesocket(mySocket);
}
void TwitchIRC::start()
{
struct addrinfo hints, *servinfo;
setup = true;
port = "6667";
//Ensure that servinfo is clear
memset(&hints, 0, sizeof hints); //make sure the struct is empty
//setup hints
hints.ai_family = AF_UNSPEC; //IPv4 or IPv6 doesnt matter
hints.ai_socktype = SOCK_STREAM; //TCP stream sockets
//Start WSA to be able to make DNS lookup
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
//Setup the structs if error, print why
int res;
if ((res = getaddrinfo("irc.twitch.tv", port, &hints, &servinfo)) != 0)
{
setup = false;
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
WSACleanup();
}
//setup socket
if ((mySocket = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)))
{
perror("client: socket");
}
//Connect
if (connect(mySocket, servinfo->ai_addr, servinfo->ai_addrlen) == -1)
{
closesocket(mySocket);
perror("Client Connected");
}
//We dont need this anymore
freeaddrinfo(servinfo);
u_long mode = -1;
int ret = ioctlsocket(mySocket, FIONBIO, &mode);
int count = 0;
while (count < 5)
{
count++;
switch (count)
{
case 1:
//after 3 receives send data to server (as per IRC prot)
sendData(password);
sendData(nick);
sendData(usr);
break;
case 2:
//Join a channel after we connect
sendData(channel);
break;
default:
break;
}
}
msg = "bread";
}
bool TwitchIRC::isConnected(char *buf)
{
if (charSearch(buf, "/MOTD"))
return true;
return false;
}
char * TwitchIRC::timeNow()
{
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
return asctime(timeinfo);
}
bool TwitchIRC::sendData(const char *msg)
{
int len = strlen(msg);
int bytes_sent = send(mySocket, msg, len, NULL);
if (bytes_sent == 0)
return false;
return true;
}
bool TwitchIRC::sendChat(const char *msg, const char *channel)
{
string c = channel;
string s = "PRIVMSG #" + c + " :"+msg + "\r\n";
return sendData(s.c_str());
}
void TwitchIRC::msgHandle(char * buf)
{
if (charSearch(buf, "hay"))
{
sendData("PRIVMSG #ubuntu :I am awake\r\n");
}
}
void TwitchIRC::sendPong(char *buf)
{
//Get the reply address
//loop through bug and find the location of PING
//Search through each char in toSearch
char * toSearch = "PING ";
for (int i = 0; i < strlen(buf); i++)
{
//If the active char is equal to the first search item then search toSearch
if (buf[i] == toSearch[0])
{
bool found = true;
//search the char array for search field
for (int x = 1; x < 4; x++)
{
if (buf[i + x] != toSearch[x])
{
found = false;
}
}
//if found return true;
if (found == true)
{
int count = 0;
//Count the chars
for (int x = (i + strlen(toSearch)); x < strlen(buf); x++)
{
count++;
}
//Create the new char array
char* returnHost = new char[count + 5];
returnHost[0] = 'P';
returnHost[1] = 'O';
returnHost[2] = 'N';
returnHost[3] = 'G';
returnHost[4] = ' ';
count = 0;
//set the hostname data
for (int x = (i + strlen(toSearch)); x < strlen(buf); x++)
{
returnHost[count + 5] = buf[x];
count++;
}
//send the pong
if (sendData(returnHost))
{
cout << timeNow() << " Ping Pong" << endl;
}
delete returnHost;
}
}
}
}
bool TwitchIRC::charSearch(char *toSearch, char *searchFor)
{
int len = strlen(toSearch);
int forLen = strlen(searchFor); //The length of the searchfor field
//Search through each char in toSearch
for (int i = 0; i < len; i++)
{
bool found = true;
//search the char array for search field
for (int a = 0; a < forLen; a++)
{
if (searchFor[a] != toSearch[i + a])
{
found = false;
}
}
if (found)
return 1;
}
return 0;
}
void TwitchIRC::checkOnce() {
//Recv some data
int numbytes;
char buf[MAXDATASIZE];
mtx.lock();
//Recv & print Data
numbytes = recv(mySocket, buf, MAXDATASIZE - 1, 0);
// msg = "**********************************************toast\n";
if (numbytes > 0) {
buf[numbytes] = '\0';
cout << buf;
}
if (numbytes > 3) {
msg = buf;
//v.push_back(msg);
}
else {
return;
}
//buf is the data that is received
//pass buf to the message handler
msgHandle(buf);
//if ping received
if (charSearch(buf, "PING"))
{
sendPong(buf);
}
//break if connection closed
if (numbytes == 0)
{
cout << "---------------------CONNECTION CLOSED---------------------";
cout << timeNow() << endl;
}
mtx.unlock();
}
/*int main()
{
TwitchIRC bot = TwitchIRC("NICK prabot\r\n", "USER prabot\r\n", "PASS oauth:f4pij3ehsuaypw1yrdh2me6y1djjhy\r\n");
bot.start();
return 0;
}*/