/
ircclient.cpp
275 lines (242 loc) · 8.81 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
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
#include "ircclient.h"
#include "pcrecpp.h"
using namespace std;
int IRCClient::Cid = 0;
IRCClient::IRCClient(QObject *parent, const QString &server, const int port)
: QTcpSocket(parent)
{
// Setup default values for various variables incase they aren't passed in raw 005
this->cid = Cid++;
this->prefix = "@%+";
this->nickmode = "ohv";
this->chanmodes = "bIe,k,l";
this->chantypes = "#&";
this->modespl = 3;
this->usenamesx = false;
this->useuhnames = false;
this->away = false;
// Connect the signals and slots
connect(this, SIGNAL(readyRead()), this, SLOT(dataReceived()));
connect(this, SIGNAL(connected()), this, SLOT(connectedToIRCHost()));
connect(this, SIGNAL(disconnected()), this, SLOT(disconnectedFromIRCHost()));
//connect(this, SIGNAL(error(SocketError)), this, SLOT(networkError(SocketError)));
if (server != 0) {
// TODO: Connect to the server
}
}
void IALAdd (const QString &nick);
void IALDel (const QString &nick);
void IALRen (const QString &nick, const QString &newnick);
void IALChanAdd(const QString &nick, const QString &chan, QString &prefix);
void IALChanDel(const QString &nick, const QString &chan);
IALItem* GetIAL(const QString &nick);
//==================== IAL Handler functions =======================
void IRCClient::IALAdd(const QString &nick)
{
if (this->ial.contains(nick) == false) {
IALItem pushme;
this->ial.insert(nick,pushme);
}
}
void IRCClient::IALDel(const QString &nick)
{
if (this->ial.contains(nick)) {
this->ial.remove(nick);
}
}
void IRCClient::IALRen(const QString &nick, const QString &newnick)
{
if (this->ial.contains(nick)) {
this->ial.insert(newnick,this->ial.take(nick));
this->ial.remove(nick);
}
}
void IRCClient::IALChanAdd(const QString &nick, const QString &chan, QString &prefix) {
IALAdd(nick);
IALItem *entry = GetIAL(nick);
entry->channels[chan] = prefix;
}
void IRCClient::IALChanDel(const QString &nick, const QString &chan) {
IALAdd(nick);
IALItem *entry = GetIAL(nick);
entry->channels.remove(chan);
}
IALItem* IRCClient::GetIAL(const QString &nick) {
return &(this->ial[nick]);
}
//==================================================================
void IRCClient::changeNick(const QString &newNick)
{
QString changeNickString("NICK ");
changeNickString.append(newNick);
this->write(changeNickString.toAscii());
}
void IRCClient::sendRawMessage(const QString &rawMessage)
{
QString message = QString(rawMessage);
message.append("\n");
this->write(message.toAscii());
}
void IRCClient::connectedToIRCHost()
{
emit connected(this);
}
void IRCClient::disconnectedFromIRCHost()
{
emit disconnected(this);
}
void IRCClient::networkError(SocketError error)
{
emit ircError(this, error);
}
void IRCClient::dataReceived()
{
string NorS;
string Event;
string args;
string Extra;
string Action;
string JoinPrefix;
string JoinNick;
string JoinAddress;
string word;
QStringList Args;
QString line;
pcrecpp::RE ParseLine("^(?:\\x3a(\\S+) )?(\\d{3}|[a-zA-Z]+)(?: ((?:[^\\x00\\x0a\\x0d\\x20\\x3a][^\\x00\\x0a\\x0d\\x20]*)(?: [^\\x00\\x0a\\x0d\\x20\\x3a][^\\x00\\x0a\\x0d\\x20]*)*))? ?(?:\\x3a([^\\x00\\x0a\\x0d]*))?\\x20*$");
pcrecpp::RE NickOrServer("^[^!@]+$");
pcrecpp::RE NickUser("^([^!]+)!(.*)$");
pcrecpp::RE IsAction("^ACTION (.*)$");
while (this->canReadLine()) {
line = this->readLine();
line.remove('\r');
line.remove('\n');
if (line.isEmpty()) { return; }
else {
if (ParseLine.FullMatch(line.toStdString(), &NorS, &Event, &args, &Extra)) {
//qDebug() << "Split Data: NorS(" + QString::fromStdString(NorS) + ") Event(" + QString::fromStdString(Event) + ") args(" + QString::fromStdString(args) + ") Extra(" + QString::fromStdString(Extra) + ")";
Args.clear();
Args = QString::fromStdString(args).split(" ");
if (NorS == "") {
if (Event == "PING") { this->sendRawMessage(QString::fromStdString("PONG " + Extra)); }
}
else {
if (NickOrServer.PartialMatch(NorS)) {
/*
Server Message
trigger RAW event
*/
if (Event == "001") {
//set server NorS
//find out our userhost info
Me = Args.at(0).toStdString();
this->sendRawMessage(QString::fromStdString("USERHOST " + Me));
}
else if (Event == "005") {
pcrecpp::RE ChanModes("^CHANMODES=([^ ]+)$");
pcrecpp::RE ChanTypes("^CHANTYPES=([^ ]+)$");
pcrecpp::RE Modes("^MODES=([\\d]+)$");
pcrecpp::RE Network("^NETWORK=([^ ]+)$");
pcrecpp::RE Prefix("^PREFIX=\\((\\w+)\\)([^ ]+)$");
for (int x = 0; x < Args.size(); x++) {
ChanModes.FullMatch(Args.at(x).toStdString(),&chanmodes);
ChanTypes.FullMatch(Args.at(x).toStdString(),&chantypes);
Modes.FullMatch(Args.at(x).toStdString(),&modespl);
Network.FullMatch(Args.at(x).toStdString(),&network);
Prefix.FullMatch(Args.at(x).toStdString(),&nickmode,&prefix);
if (Args.at(x) == "NAMESX") {
usenamesx = true;
this->sendRawMessage("PROTOCTL NAMESX");
}
if (Args.at(x) == "UHNAMES") {
useuhnames = true;
this->sendRawMessage("PROTOCTL UHNAMES");
}
}
}
else if (Event == "302") {
string unick,uaddy;
pcrecpp::RE ParseUhost("^([^=*]+)(?:\\*)?=(?:[+-])([^ ]+)$");
if (ParseUhost.FullMatch(Extra,&unick,&uaddy)) {
IALAdd(QString::fromStdString(unick));
IALItem *entry = &this->ial[QString::fromStdString(unick)];
entry->address = QString::fromStdString(uaddy);
qDebug() << "test set of addy: " << entry->address;
}
//userhost return raw use to set ial of ourself
}
else if (Event == "305") {
//set back from being away
}
else if (Event == "306") {
//we set ourself away
}
else if (Event == "353") {
QString channel = Args.at(Args.size() - 1);
QStringList nicklist;
if (this->tempChannelJoins.contains(channel)) {
nicklist = this->tempChannelJoins.take(channel);
}
pcrecpp::StringPiece ExData(Extra);
pcrecpp::RE ParseJoin("(?:^| )([" + this->prefix + "]+)?([^! ]+)!?([^ ]+)?");
while (ParseJoin.Consume(&ExData,&JoinPrefix,&JoinNick,&JoinAddress)) {
nicklist << QString::fromStdString(JoinPrefix + JoinNick);
// emit channelJoined(this,QString::fromStdString(Args.at(Args.size() - 1)),QString::fromStdString(JoinNick));
}
if (!this->tempChannelJoins.contains(channel)) {
this->tempChannelJoins.insert(channel, nicklist);
}
} else if (Event == "366") {
QString channel = Args.at(Args.size() - 1);
if (this->tempChannelJoins.contains(channel)) {
QStringList nicklist = this->tempChannelJoins.take(channel);
emit channelJoinCompleteNickList(this, channel, nicklist);
}
}
emit incomingData(this, line);
}
else {
pcrecpp::RE IsChan("^[" + this->chantypes + "]");
//Nickname Message
string Nick;
string Address;
if (NickUser.PartialMatch(NorS, &Nick, &Address)) {
IALAdd(QString::fromStdString(Nick));
IALItem *entry = &this->ial[QString::fromStdString(Nick)];
entry->address = QString::fromStdString(Address);
qDebug() << "test set of addy: " << entry->address;
if (Event == "JOIN" || Event == "PART") {
emit channelMessageReceived(this, (IsChan.PartialMatch(Extra) > 0 ? QString::fromStdString(Extra) : Args.at(0)),QString::fromStdString(Event),QString::fromStdString(Nick),QString::fromStdString(Address),QString::fromStdString(Extra));
if (Event == "JOIN") {
emit channelJoined(this,(IsChan.PartialMatch(Extra) > 0 ? QString::fromStdString(Extra) : Args.at(0)),QString::fromStdString(Nick));
}
else {
emit channelParted(this,(IsChan.PartialMatch(Extra) > 0 ? QString::fromStdString(Extra) : Args.at(0)),QString::fromStdString(Nick));
}
}
else if (Event == "NICK") {
if (Nick == Me) { Me = Extra; }
//todo move ial entry to newnick
}
else if (Event == "PRIVMSG") {
if (IsChan.PartialMatch(Args.at(0).toStdString())) {
if (IsAction.PartialMatch(Extra,&Action)) {
emit channelMessageReceived(this, Args.at(0),QString::fromStdString("ACTION"),QString::fromStdString(Nick),QString::fromStdString(Address),QString::fromStdString(Action));
}
else {
emit channelMessageReceived(this, Args.at(0),QString::fromStdString(Event),QString::fromStdString(Nick),QString::fromStdString(Address),QString::fromStdString(Extra));
}
}
else {
emit privateMessageReceived(this, QString::fromStdString(Nick),QString::fromStdString(Address),QString::fromStdString(Extra));
}
}
else { emit incomingData(this, line); }
}
}
}
}
}
}
//QByteArray data = this->readAll();
//emit incomingData(this, QString(data));
}