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
|
#pragma once
#include <QDateTime>
#include <QString>
#include <QStringList>
#include <QTcpSocket>
#include <CRSMConfig.hpp>
#include "ClientInterface.hpp"
class ManagementConnection {
public:
ManagementConnection() {}
ManagementConnection(QTcpSocket* socket, const QString& name = "") : socket(socket), name(name) {}
QTcpSocket* socket = 0;
QString name = "";
};
class ClientInfo
{
public:
ClientInterface interface = Clonk;
QString nick = "";
int CUID = 0;
QString pcName = "";
bool activated = false;
QList<QDateTime> antiFloodList;
QString target = "";
ManagementConnection management;
static inline ClientInfo ircClient(QString nick, QString target = "")
{
ClientInfo ret;
ret.interface = IRC;
ret.nick = nick;
if(target.isEmpty())
{
target = nick;
}
ret.target = target;
return ret;
}
static inline ClientInfo clonkClient(QString nick, QString pcName, int CUID, bool activated = false)
{
ClientInfo ret;
ret.interface = Clonk;
ret.nick = nick;
ret.pcName = pcName;
ret.CUID = CUID;
ret.activated = activated;
return ret;
}
static inline ClientInfo managementClient(ManagementConnection conn)
{
ClientInfo ret;
ret.interface = Management;
ret.management = conn;
ret.nick = conn.name;
return ret;
}
static inline ClientInfo autoClient()
{
ClientInfo ret;
ret.interface = Auto;
return ret;
}
inline bool operator==(const ClientInfo& other) const
{
return other.interface == interface && other.nick == nick && (interface == Clonk ? other.pcName == pcName && other.CUID == CUID : true);
}
inline bool operator!=(const ClientInfo& other) const
{
return !operator==(other);
}
inline bool operator<(const ClientInfo& other) const
{
return toString() < other.toString();
}
inline QString toString(bool asChatNick = false) const
{
if(asChatNick)
{
return interface == Auto ? "<~auto~>" : interface == Management ? "{CLI}<" + management.name + ">" : interface == IRC ? "[IRC]<" + nick + ">" : "<" + nick + ">";
}
else
{
return interface == Auto ? "~auto~" : interface == Management ? management.name + " {CLI}" : (!nick.isEmpty() ? nick + (interface == Clonk ? " (" + pcName + ")" : " [IRC]") : "");
}
}
inline bool floodCheck(int maxCount, int floodTimeSecs, QDateTime newDateTime = QDateTime::currentDateTime())
{
foreach(const QDateTime& dateTime, antiFloodList)
{
if(dateTime.secsTo(newDateTime) > floodTimeSecs)
{
antiFloodList.removeAll(dateTime);
}
}
antiFloodList.push_back(newDateTime);
return antiFloodList.size() > maxCount;
}
inline bool empty()
{
return *this == ClientInfo();
}
inline void clear()
{
*this = ClientInfo();
}
};
template<>
class ConfigValue<ClientInfo> : public ConfigValueBase {
ClientInfo& info;
public:
ConfigValue(ClientInfo& info) : info(info) {}
void setValue(const QString& string)
{
info = ClientInfo();
if(string == "empty") return;
QStringList parts(Util::splitEscaped(string, ':', '|'));
ClientInterface interface = static_cast<ClientInterface>(parts.first().toUInt(0, 36));
if(interface > ClientInterface::Last || interface < ClientInterface::First)
{
throw ConfigException("Cannot read ClientInfo with unknown ClientInterface: " + string.toStdString());
}
switch(interface)
{
case Auto: break;
case Clonk:
{
if(parts.length() != 5) throw ConfigException("Cannot read corrupt ClientInfo with Clonk-ClientInterface: " + string.toStdString());
info.pcName = parts.at(1);
info.CUID = parts.at(2).toUInt();
info.nick = parts.at(3);
info.activated = ConfigValueBase::getValue<bool>(parts.at(4));
break;
}
case IRC:
{
if(parts.length() != 2) throw ConfigException("Cannot read corrupt ClientInfo with IRC-ClientInterface: " + string.toStdString());
info.nick = parts.at(1);
break;
}
case Management:
{
if(parts.length() != 2) throw ConfigException("Cannot read corrupt ClientInfo with Management-ClientInterface: " + string.toStdString());
info.management.name = parts.at(1);
break;
}
}
info.interface = interface;
}
QString value()
{
if(info.empty()) return "empty";
QStringList ret;
ret.append(QString::number(info.interface, 36));
switch(info.interface)
{
case Auto: break;
case Clonk: ret.append(info.pcName); ret.append(QString::number(info.CUID)); ret.append(info.nick); ret.append(ConfigValueBase::getStringValue(info.activated)); break;
case IRC: ret.append(info.nick); break;
case Management: ret.append(info.management.name); break;
}
return Util::joinEscape(ret, ':', '|');
}
};
|