-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIRCClient.h
130 lines (101 loc) · 3.49 KB
/
IRCClient.h
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
/*
* This program has been developed by Luca Tringali, using code from
* Fredi Machado <https://github.com/Fredi> for the IRC client.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _IRCCLIENT_H
#define _IRCCLIENT_H
#include <string>
#include <vector>
#include <list>
#include "IRCSocket.h"
class IRCClient;
extern std::vector<std::string> split(std::string const&, char);
struct IRCCommandPrefix
{
void Parse(std::string data)
{
if (data == "")
return;
prefix = data.substr(1, data.find(" ") - 1);
std::vector<std::string> tokens;
if (prefix.find("@") != std::string::npos)
{
tokens = split(prefix, '@');
nick = tokens.at(0);
host = tokens.at(1);
}
if (nick != "" && nick.find("!") != std::string::npos)
{
tokens = split(nick, '!');
nick = tokens.at(0);
user = tokens.at(1);
}
};
std::string prefix;
std::string nick;
std::string user;
std::string host;
};
struct IRCMessage
{
IRCMessage();
IRCMessage(std::string cmd, IRCCommandPrefix p, std::vector<std::string> params) :
command(cmd), prefix(p), parameters(params) {};
std::string command;
IRCCommandPrefix prefix;
std::vector<std::string> parameters;
};
struct IRCCommandHook
{
IRCCommandHook() : function(NULL) {};
std::string command;
void (*function)(IRCMessage /*message*/, IRCClient* /*client*/);
};
class IRCClient
{
public:
IRCClient() : _debug(false) {};
bool InitSocket();
bool Connect(const char* /*host*/, int /*port*/);
void Disconnect();
bool Connected() { return _socket.Connected(); };
std::string master;
bool SendIRC(std::string /*data*/);
bool Login(std::string /*nick*/, std::string /*user*/);
void ReceiveData();
void HookIRCCommand(std::string /*command*/, void (*function)(IRCMessage /*message*/, IRCClient* /*client*/));
void Parse(std::string /*data*/);
void HandleCTCP(IRCMessage /*message*/);
// Default internal handlers
void HandlePrivMsg(IRCMessage /*message*/);
void HandleNotice(IRCMessage /*message*/);
void HandleChannelJoinPart(IRCMessage /*message*/);
void HandleUserNickChange(IRCMessage /*message*/);
void HandleUserQuit(IRCMessage /*message*/);
void HandleChannelNamesList(IRCMessage /*message*/);
void HandleNicknameInUse(IRCMessage /*message*/);
void HandleServerMessage(IRCMessage /*message*/);
void Debug(bool debug) { _debug = debug; };
private:
void HandleCommand(IRCMessage /*message*/);
void CallHook(std::string /*command*/, IRCMessage /*message*/);
IRCSocket _socket;
std::list<IRCCommandHook> _hooks;
std::string _nick;
std::string _user;
bool _debug;
};
#endif