IRC Bot troubles

Ok, I have a new problem for you all. This time it has to do with IRC's and networking. I am trying to make an IRC bot in C++ using SDL_NET. So far I can get connected to the server and... that's about it. I can't register my nick or join the channel or anything beyond that. My code looks like this:

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
#include <iostream>
#include <SDL.h>
#include <SDL_net.h>
#include <string.h>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <Windows.h>
#include <conio.h>

using namespace std;

const int BUFF_SIZE = 516;
bool killbot = true;

int main(int argc, char** argv)
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDLNet_Init();

	IPaddress ip;

	string server = "irc.afternet.org";
	char buffer[BUFF_SIZE];

	//create socket set
	SDLNet_SocketSet socketset = SDLNet_AllocSocketSet(1);

	SDLNet_ResolveHost(&ip, server.c_str(), 6667);

	Uint8 * dotQuad = (Uint8*)&ip.host;
	cout << "Successfully resolved host to IP: " << (unsigned short)dotQuad[0] << "." << (unsigned short)dotQuad[1] << "." << (unsigned short)dotQuad[2] << "." << (unsigned short)dotQuad[3];
	cout << " port " << SDLNet_Read16(&ip.port) << endl << endl;

	TCPsocket client = SDLNet_TCP_Open(&ip);
	SDLNet_TCP_AddSocket(socketset, client);

	//wait 10 seconds for server response
	int activesockets = SDLNet_CheckSockets(socketset, 10000);

	int gotresponse = SDLNet_SocketReady(client);
	
	int messagenum = 0;
	while (messagenum < 2)
	{
		if (gotresponse != 0)
		{
			int serverbytecount = SDLNet_TCP_Recv(client, buffer, BUFF_SIZE);
			string received = buffer;

			if (received.length() > serverbytecount)
				received.erase(serverbytecount);

			if (received.find("PING") != string::npos)
			{
				received.replace(1, 1, "O");
				strcpy_s(buffer, received.c_str());
				SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
				cout << "sent: " << received.c_str() << endl;
			}

			cout << received.c_str() << endl;
			//killbot = false
			messagenum++;
		}
	}
	killbot = false;
	
	for (int i = 0; i < 3; i++)
	{
		if (i == 0)
		{
			strcpy_s(buffer, "NICK Bot_Test\r\n");
			SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
		}

		if (i == 1)
		{
			strcpy_s(buffer, "USER Bot_Test 0 * :MyBot\r\n");
			SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
		}

		if (i == 2)
		{
			strcpy_s(buffer, "JOIN :#ludumdare\r\n");
			SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
		}
		int activesockets = SDLNet_CheckSockets(socketset, 1000);
		int gotresponse = SDLNet_SocketReady(client);
		if (gotresponse != 0)
		{
			strcpy_s(buffer, "");
			int serverbytecount = SDLNet_TCP_Recv(client, buffer, BUFF_SIZE);
			string received = buffer;

			if (received.length() > serverbytecount)
				received.erase(serverbytecount);

			cout << received.c_str() << endl;

			if (received.find("PING") != string::npos)
			{
				received.replace(1, 1, "O");
				received = "\r\n" + received + "\r\n";
				strcpy_s(buffer, received.c_str());
				SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
				cout << "sent: " << received.c_str() << endl;
			}

		}
	}
	

	/*strcpy_s(buffer, "PRIVMSG #ludumdare :hello world\r\n");
	SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
	cout << "sent: " << buffer << endl;
	*/

	cout << "end loop" << endl;
	int timer = 0;
	while (!killbot)
	{
		int socketactive = SDLNet_CheckSockets(socketset, 0);

		if (socketactive != 0)
		{
			//see if we got response from server
			int response = SDLNet_SocketReady(client);

			if (response != 0)
			{

				strcpy_s(buffer, "");
				int responsebytes = SDLNet_TCP_Recv(client, buffer, BUFF_SIZE);
				string received = buffer;
				if (received.length() > responsebytes)
				{
					received.erase(responsebytes);
				}
				cout << received.c_str() << endl;

				if (received.find("PING") != string::npos)
				{
					received.replace(1, 1, "O");
					strcpy_s(buffer, received.c_str());
					SDLNet_TCP_Send(client, (void*)buffer, strlen(buffer) + 1);
					cout << "sent: " << received.c_str() << endl;

					
				}

				if (responsebytes <= 0)
				{
					cout << "Sir, the server has been hit! We're going down!" << endl;
					cout << "Press ENTER to continue..." << endl;
					cin.get();
					killbot = true;
				}
			}
		}

		
		
		timer++;
	}
	return 1;
}


The output looks like this:

Successfully resolved host to IP: 192.198.81.195 port 6667

NOTICE AUTH :*** Looking up your hostname

NOTICE AUTH :*** Found your hostname

PING :1634001615

sent:
PONG :1634001615


end loop
ERROR :Closing Link: Bot_Test by FractalRealities.US.AfterNET.Org (Registration Timeout)

Sir, the server has been hit! We're going down!
Press ENTER to continue...



A quick runthrough of what I am doing is: Resolving the host to the irc server, connecting and waiting for response, having a while loop that waits for the two NOTICE AUTH messages to appear, then we loop through three times to send the NICK, USER, and JOIN commands and waiting for some response each time. The server usually sends that PING message after I send the NICK command, to which I respond with the necessary PONG. After that we enter our main loop where we listen to the server for PINGS or anything really. It just never sends anything then eventually sends that ERROR message. I don't know what I am doing wrong. I am new to the IRC protocol and everything but I think I have all my commands correct. Thanks in advance for the help!
Last edited on
Ok, I got it working. All commands needed \r\n in front of them. For example \r\nNICK Bot_test\r\n
Topic archived. No new replies allowed.