File: clanbot.cpp

package info (click to toggle)
clanlib 1.0~svn3827-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,600 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (252 lines) | stat: -rw-r--r-- 7,698 bytes parent folder | download | duplicates (7)
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

#include "clanbot.h"

ClanBotConnection app;

int ClanBotConnection::main(int argc, char **argv)
{
	// Create a console window for text-output if not available
	CL_ConsoleWindow console("Console");
	console.redirect_stdio();

	CL_SetupCore setup_core;
	CL_SetupNetwork setup_network;

	CL_Slot slot_log = CL_Log::sig_log().connect(this, &ClanBotConnection::on_log);

	connected = false;
	server = "irc.freenode.net";

	try
	{
		connection = new CL_IRCConnection(server);
		connection->send_nick("ClanBot");
		connection->send_user("clanbot", "localhost", "localhost", "ClanLib Channel Bot");

		slots.connect(connection->sig_command_received(), this, &ClanBotConnection::on_connection_command_received);
		slots.connect(connection->sig_unknown_command_received(), this, &ClanBotConnection::on_connection_unknown_command_received);
		slots.connect(connection->sig_numeric_reply(), this, &ClanBotConnection::on_connection_numeric_reply);
		slots.connect(connection->sig_name_reply(), this, &ClanBotConnection::on_connection_name_reply);
		slots.connect(connection->sig_nick(), this, &ClanBotConnection::on_connection_nick);
		slots.connect(connection->sig_join(), this, &ClanBotConnection::on_connection_join);
		slots.connect(connection->sig_part(), this, &ClanBotConnection::on_connection_part);
		slots.connect(connection->sig_mode(), this, &ClanBotConnection::on_connection_mode);
		slots.connect(connection->sig_topic(), this, &ClanBotConnection::on_connection_topic);
		slots.connect(connection->sig_invite(), this, &ClanBotConnection::on_connection_invite);
		slots.connect(connection->sig_kick(), this, &ClanBotConnection::on_connection_kick);
		slots.connect(connection->sig_privmsg(), this, &ClanBotConnection::on_connection_privmsg);
		slots.connect(connection->sig_notice(), this, &ClanBotConnection::on_connection_notice);
		slots.connect(connection->sig_privmsg_ctcp(), this, &ClanBotConnection::on_connection_privmsg_ctcp);
		slots.connect(connection->sig_notice_ctcp(), this, &ClanBotConnection::on_connection_notice_ctcp);
		slots.connect(connection->sig_ping(), this, &ClanBotConnection::on_connection_ping);
	}
	catch (CL_Error err)
	{
		CL_Log::log("system", "Unable to connect to server %1", server);

		// Display console close message and wait for a key
		console.display_close_message();

		return 1;
	}

	bool quit = false;
	while (!quit)
	{
		try
		{
			CL_System::keep_alive(500);
		}
		catch (CL_Error error)
		{
			CL_Log::log("error", "Fatal error processing keep_alive: %1", error.message);

			// Display console close message and wait for a key
			console.display_close_message();
		}
	}
	
	delete connection;

	return 0;
}

void ClanBotConnection::on_log(const std::string &id, int severity, const std::string &message)
{
	std::cout << "[" << id << "] " << message << std::endl;
}

void ClanBotConnection::on_connection_command_received(const std::string &prefix, const std::string &command, const std::vector<std::string> &params)
{
}

void ClanBotConnection::on_connection_unknown_command_received(const std::string &prefix, const std::string &command, const std::vector<std::string> &params)
{
}

void ClanBotConnection::on_connection_numeric_reply(const std::string &prefix, int command, const std::vector<std::string> &params)
{
	std::string str_params;
	for (unsigned int i=0; i<params.size(); i++) str_params += " " + params[i];
	CL_Log::log(server, "%1 [%2]:%3", prefix, command, str_params);
	
	if (command == RPL_WELCOME)
	{
		connection->send_join("#clanlib");
		connected = true;
	}
}

void ClanBotConnection::on_connection_name_reply(const std::string &self, const std::string &channel, const std::vector<std::string> &users)
{
}

void ClanBotConnection::on_connection_nick(const std::string &old_nick, const std::string &new_nick)
{
}

void ClanBotConnection::on_connection_join(const std::string &nick, const std::string &channel)
{
	if (nick.find("ClanBot") != std::string::npos)
	{
		CL_Log::log(server, "Joined channel %1", channel);
	}
}

void ClanBotConnection::on_connection_part(const std::string &nick, const std::string &channel, const std::string &reason)
{
}

void ClanBotConnection::on_connection_mode(const std::string &prefix, const std::string &receiver, const std::string &mode, const std::vector<std::string> &params)
{
}

void ClanBotConnection::on_connection_topic(const std::string &prefix, const std::string &channel, const std::string &topic)
{
}

void ClanBotConnection::on_connection_invite(const std::string &prefix, const std::string &nick, const std::string &channel)
{
}

void ClanBotConnection::on_connection_kick(const std::string &prefix, const std::string &channel, const std::string &user, const std::string &comment)
{
}

void ClanBotConnection::on_connection_privmsg(const std::string &prefix, const std::string &receiver, const std::string &text)
{
	if (receiver[0] != '#')
	{
		connection->send_privmsg("#clanlib", text);
	}
	
	CL_Log::log(server, "%1 privmsg %2: %3", prefix, receiver, text);
}

void ClanBotConnection::on_connection_notice(const std::string &prefix, const std::string &receiver, const std::string &text)
{
//	if (receiver[0] != '#')
		CL_Log::log(server, "%1 notice %2: %3", prefix, receiver, text);
}

void ClanBotConnection::on_connection_privmsg_ctcp(const std::string &prefix, const std::string &receiver, const std::string &command, const std::string &data)
{
	std::string nick = prefix.substr(0,prefix.find("!"));

	if (command == "DCC")
	{
		CL_Log::log(server, "%1 DCC line: %2", nick, data);

		std::vector<std::string> tokens = CL_String::tokenize(data, " ");
		if (tokens.size() >= 4 && tokens[0] == "SEND")
		{
		}
		else if (tokens.size() == 4 && tokens[0] == "CHAT" && tokens[1] == "chat")
		{
		}
	}
	else if (command == "FINGER")
	{
		if (data.empty())
		{
			connection->send_privmsg_ctcp(nick, "FINGER", ":ClanLib's Example IRC Client");
		}
		else
		{
			CL_Log::log(server, "%1 FINGER REPLY: %2", nick, data.substr(1));
		}
	}
	else if (command == "VERSION")
	{
		if (data.empty())
		{
#ifdef WIN32
			connection->send_privmsg_ctcp(nick, "VERSION", "ClanBot:0.1:Windows");
#else
			connection->send_privmsg_ctcp(nick, "VERSION", "ClanBot:0.1:Unix");
#endif
		}
		else
		{
			CL_Log::log(server, "%1 VERSION REPLY: %2", nick, data);
		}
	}
	else if (command == "USERINFO")
	{
		if (data.empty())
		{
			connection->send_privmsg_ctcp(nick, "USERINFO", ":A ClanLib User");
		}
		else
		{
			CL_Log::log(server, "%1 USERINFO REPLY: %2", nick, data.substr(1));
		}
	}
	else if (command == "CLIENTINFO")
	{
		if (data.empty())
		{
			connection->send_privmsg_ctcp(nick, "CLIENTINFO", "ACTION DCC FINGER VERSION USERINFO CLIENTINFO PING TIME");
		}
		else
		{
			CL_Log::log(server, "%1 CLIENTINFO REPLY: %2", nick, data.substr(1));
		}
	}
	else if (command == "PING")
	{
		connection->send_notice_ctcp(nick, "PING", data);
	}
	else if (command == "TIME")
	{
		if (data.empty())
		{
			connection->send_privmsg_ctcp(nick, "TIME", "I havn't got the slightest clue what the time is!");
		}
		else
		{
			CL_Log::log(server, "%1 TIME REPLY: %2", nick, data.substr(1));
		}
	}
	else
	{
		CL_Log::log(server, "%1 CTCP %2: %3", nick, command, data);
	}
}

void ClanBotConnection::on_connection_notice_ctcp(const std::string &prefix, const std::string &receiver, const std::string &command, const std::string &data)
{
	if (command == "PING")
	{
		CL_Log::log(server, "%1 PING: %2 ms", prefix, CL_String::from_int(CL_String::to_int(data)-CL_System::get_time()));
	}
	else
	{
		on_connection_privmsg_ctcp(prefix, receiver, command, data);
	}
}

void ClanBotConnection::on_connection_ping(const std::string &daemon1, const std::string &daemon2)
{
	connection->send_pong(daemon1, daemon2);
}