-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.cpp
141 lines (136 loc) · 4.41 KB
/
irc.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
#include "irc.hpp"
Irc::Irc(QObject * parent) : QObject(parent)
{
sent = 0;
timer = new QTimer();
timer->setInterval(60000);
timer->start();
limit = 10;
connect(timer, SIGNAL(timeout()), this, SLOT(resetSent()));
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()),
this, SLOT(parse()));
}
void Irc::connectTo(QString server, QString channel, QString nick)
{
this->channel = channel.toAscii();
this->nick = nick.toAscii();
socket->connectToHost(server, 6667);
QByteArray ba = QByteArray::number(socket->localPort());
qDebug() << ba;
socket->write("user "
+ this->nick
+ " enkidu "
+ server.toAscii()
+ " :battalbot\n");
socket->write("nick " + this->nick + "\n");
}
void Irc::parse()
{
QByteArray ba = socket->readLine();
while (ba != "")
{
qDebug() << ba;
if (ba.startsWith("PING"))
{
ba = ba.mid(4).prepend("PONG");
socket->write(ba);
}
else if (ba.startsWith(":"))
{
ba = ba.mid(1);
if (ba.startsWith(this->nick) && ba.contains("MODE " + nick) && ba.contains("+i"))
socket->write("join " + this->channel + "\n");
else if (ba.contains(":!wall "))
{
QString str(ba);
QStringList strings = str.split(":!wall ");
if (strings.count() == 2)
{
QStringList parts = strings[0].split(" ");
if (parts.count() > 2)
{
QString sender = parts[0];
QString ident = strings[1];
emit wallRequest(sender, ident);
}
}
}
else if (ba.contains(":!dmg "))
{
QString str(ba);
QStringList strings = str.split(":!dmg ");
if (strings.count() == 2)
{
QStringList parts = strings[0].split(" ");
if (parts.count() > 2)
{
QString ident = strings[1];
if (sent <= limit)
{
emit playerRequest(ident);
sent++;
}
}
}
}
else if (ba.contains(":!auth "))
{
QString str(ba);
QStringList strings = str.split(":!auth ");
qDebug() << strings[0];
qDebug() << strings[1];
if (strings.count() == 2)
{
QStringList parts = strings[0].split(" ");
if (parts.count() > 2)
{
QString sender = parts[0];
parts = strings[1].split(" ");
if (parts.count() == 2)
{
QString nick = parts[0];
QString pass = parts[1];
emit authRequest(sender, nick, pass);
}
}
}
}
else if (ba.contains(":!addauth"))
{
QString str(ba);
QStringList strings = str.split(":!addauth ");
qDebug() << strings[0];
qDebug() << strings[1];
if (strings.count() == 2)
{
QStringList parts = strings[0].split(" ");
if (parts.count() > 2)
{
QString sender = parts[0];
parts = strings[1].split(" ");
if (parts.count() == 2)
{
QString nick = parts[0];
QString pass = parts[1];
emit authAdd(sender, nick, pass);
}
}
}
}
}
ba = socket->readLine();
}
}
void Irc::write(QString text)
{
socket->write("PRIVMSG " + this->channel + " :" + text.toAscii() + "\n");
}
void Irc::setTopic(QString topic)
{
socket->write("TOPIC " + this->channel + " :" + topic.toAscii() + "\n");
}
void Irc::resetSent()
{
sent = 0;
}