-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRCClient.cpp
183 lines (157 loc) · 4.71 KB
/
IRCClient.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
/*
* Copyright (C) 2011 Fredi Machado <https://github.com/fredimachado>
* IRCClient is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* http://www.gnu.org/licenses/lgpl.html
*/
#include <iostream>
#include <algorithm>
#include "IRCSocket.h"
#include "IRCClient.h"
#include "IRCHandler.h"
std::vector<std::string> split(std::string const& text, char sep)
{
std::vector<std::string> tokens;
size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos)
{
tokens.push_back(text.substr(start, end - start));
start = end + 1;
}
tokens.push_back(text.substr(start));
return tokens;
}
bool IRCClient::InitSocket()
{
return _socket.Init();
}
bool IRCClient::Connect(char* host, int port)
{
return _socket.Connect(host, port);
}
void IRCClient::Disconnect()
{
_socket.Disconnect();
}
bool IRCClient::SendIRC(std::string data)
{
data.append("\n");
return _socket.SendData(data.c_str());
}
bool IRCClient::Login(std::string nick, std::string user, std::string password)
{
_nick = nick;
_user = user;
if (SendIRC("HELLO"))
{
if (!password.empty() && !SendIRC("PASS "+password))
return false;
if (SendIRC("NICK " + nick))
if (SendIRC("USER " + user + " 8 * :Cpp IRC Client"))
return true;
}
return false;
}
void IRCClient::ReceiveData()
{
std::string buffer = _socket.ReceiveData();
std::string line;
std::istringstream iss(buffer);
while(getline(iss, line))
{
if (line.find("\r") != std::string::npos)
line = line.substr(0, line.size() - 1);
Parse(line);
}
}
void IRCClient::Parse(std::string data)
{
std::string original(data);
IRCCommandPrefix cmdPrefix;
// if command has prefix
if (data.substr(0, 1) == ":")
{
cmdPrefix.Parse(data);
data = data.substr(data.find(" ") + 1);
}
std::string command = data.substr(0, data.find(" "));
std::transform(command.begin(), command.end(), command.begin(), towupper);
if (data.find(" ") != std::string::npos)
data = data.substr(data.find(" ") + 1);
else
data = "";
std::vector<std::string> parameters;
if (data != "")
{
if (data.substr(0, 1) == ":")
parameters.push_back(data.substr(1));
else
{
size_t pos1 = 0, pos2;
while ((pos2 = data.find(" ", pos1)) != std::string::npos)
{
parameters.push_back(data.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
if (data.substr(pos1, 1) == ":")
{
parameters.push_back(data.substr(pos1 + 1));
break;
}
}
if (parameters.empty())
parameters.push_back(data);
}
}
if (command == "ERROR")
{
std::cout << original << std::endl;
Disconnect();
return;
}
if (command == "PING")
{
std::cout << "Ping? Pong!" << std::endl;
SendIRC("PONG :" + parameters.at(0));
return;
}
IRCMessage ircMessage(command, cmdPrefix, parameters);
// Default handler
int commandIndex = GetCommandHandler(command);
if (commandIndex < NUM_IRC_CMDS)
{
IRCCommandHandler& cmdHandler = ircCommandTable[commandIndex];
(this->*cmdHandler.handler)(ircMessage);
}
else if (_debug)
std::cout << original << std::endl;
// Try to call hook (if any matches)
CallHook(command, ircMessage);
}
void IRCClient::HookIRCCommand(std::string command, void (*function)(IRCMessage /*message*/, IRCClient* /*client*/))
{
IRCCommandHook hook;
hook.command = command;
hook.function = function;
_hooks.push_back(hook);
}
void IRCClient::CallHook(std::string command, IRCMessage message)
{
if (_hooks.empty())
return;
for (std::list<IRCCommandHook>::const_iterator itr = _hooks.begin(); itr != _hooks.end(); ++itr)
{
if (itr->command == command)
{
(*(itr->function))(message, this);
break;
}
}
}