forked from thoth-medievia/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTAlias.cpp
336 lines (297 loc) · 10.5 KB
/
TAlias.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2017 by Stephen Lyons - slysven@virginmedia.com *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "TAlias.h"
#include "Host.h"
#include "TDebug.h"
#include "mudlet.h"
TAlias::TAlias(TAlias* parent, Host* pHost)
: Tree<TAlias>( parent )
, mpHost( pHost )
, mNeedsToBeCompiled( true )
, mModuleMember(false)
, mModuleMasterFolder(false)
, exportItem(true)
{
}
TAlias::TAlias(const QString& name, Host* pHost)
: Tree<TAlias>(nullptr)
, mName( name )
, mpHost( pHost )
, mNeedsToBeCompiled( true )
, mModuleMember(false)
, mModuleMasterFolder(false)
, exportItem(true)
{
}
TAlias::~TAlias()
{
if (!mpHost) {
return;
}
mpHost->getAliasUnit()->unregisterAlias(this);
}
void TAlias::setName(const QString& name)
{
if (!isTemporary()) {
mpHost->getAliasUnit()->mLookupTable.remove(mName, this);
}
mName = name;
mpHost->getAliasUnit()->mLookupTable.insertMulti(name, this);
}
bool TAlias::match(const QString& toMatch)
{
if (!isActive()) {
if (isFolder()) {
if (shouldBeActive()) {
bool matchCondition = false;
for (auto alias : *mpMyChildrenList) {
if (alias->match(toMatch)) {
matchCondition = true;
}
}
return matchCondition;
}
}
return false;
}
bool matchCondition = false;
//bool ret = false;
//bool conditionMet = false;
QSharedPointer<pcre> re = mpRegex;
if (re == nullptr) {
return false; //regex compile error
}
#if defined(Q_OS_WIN32)
// strndup(3) - a safe strdup(3) does not seem to be available on mingw32 with GCC-4.9.2
char* subject = (char*)malloc(strlen(toMatch.toUtf8().constData()) + 1);
strcpy(subject, toMatch.toUtf8().constData());
#else
char* subject = strndup(toMatch.toUtf8().constData(), strlen(toMatch.toUtf8().constData()));
#endif
unsigned char* name_table;
int namecount;
int name_entry_size;
int subject_length = strlen(subject);
int rc, i;
std::list<std::string> captureList;
std::list<int> posList;
int ovector[300]; // 100 capture groups max (can be increase nbGroups=1/3 ovector
//cout <<" LINE="<<subject<<endl;
if (mRegexCode.size() > 0) {
rc = pcre_exec(re.data(), nullptr, subject, subject_length, 0, 0, ovector, 100);
} else {
goto MUD_ERROR;
}
if (rc < 0) {
goto MUD_ERROR;
} else if (rc == 0) {
qDebug() << "CRITICAL ERROR: SHOULD NOT HAPPEN->pcre_info() got wrong num of cap groups ovector only has room for %d captured substrings\n";
} else {
if (mudlet::debugMode) {
TDebug(QColor(Qt::cyan), QColor(Qt::black)) << "Alias name=" << mName << "(" << mRegexCode << ") matched.\n" >> 0;
}
}
matchCondition = true; // alias has matched
for (i = 0; i < rc; i++) {
char* substring_start = subject + ovector[2 * i];
int substring_length = ovector[2 * i + 1] - ovector[2 * i];
std::string match;
if (substring_length < 1) {
captureList.push_back(match);
posList.push_back(-1);
continue;
}
match.append(substring_start, substring_length);
captureList.push_back(match);
posList.push_back(ovector[2 * i]);
if (mudlet::debugMode) {
TDebug(QColor(Qt::darkCyan), QColor(Qt::black)) << "Alias: capture group #" << (i + 1) << " = " >> 0;
TDebug(QColor(Qt::darkMagenta), QColor(Qt::black)) << "<" << match.c_str() << ">\n" >> 0;
}
}
pcre_fullinfo(re.data(), nullptr, PCRE_INFO_NAMECOUNT, &namecount);
if (namecount <= 0) {
//cout << "no named substrings detected" << endl;
} else {
unsigned char* tabptr;
pcre_fullinfo(re.data(), nullptr, PCRE_INFO_NAMETABLE, &name_table);
pcre_fullinfo(re.data(), nullptr, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size);
tabptr = name_table;
for (i = 0; i < namecount; i++) {
//int n = (tabptr[0] << 8) | tabptr[1];
tabptr += name_entry_size;
}
}
//TODO: add named groups seperately later as Lua::namedGroups
for (;;) {
int options = 0;
int start_offset = ovector[1];
if (ovector[0] == ovector[1]) {
if (ovector[0] >= subject_length) {
goto END;
}
options = PCRE_NOTEMPTY | PCRE_ANCHORED;
}
rc = pcre_exec(re.data(), nullptr, subject, subject_length, start_offset, options, ovector, 30);
if (rc == PCRE_ERROR_NOMATCH) {
if (options == 0) {
break;
}
ovector[1] = start_offset + 1;
continue;
} else if (rc < 0) {
goto END;
} else if (rc == 0) {
qDebug() << "CRITICAL ERROR: SHOULD NOT HAPPEN->pcre_info() got wrong num of cap groups ovector only has room for %d captured substrings\n";
}
for (i = 0; i < rc; i++) {
char* substring_start = subject + ovector[2 * i];
int substring_length = ovector[2 * i + 1] - ovector[2 * i];
std::string match;
if (substring_length < 1) {
captureList.push_back(match);
posList.push_back(-1);
continue;
}
match.append(substring_start, substring_length);
captureList.push_back(match);
posList.push_back(ovector[2 * i]);
if (mudlet::debugMode) {
TDebug(QColor(Qt::darkCyan), QColor(Qt::black)) << "capture group #" << (i + 1) << " = " >> 0;
TDebug(QColor(Qt::darkMagenta), QColor(Qt::black)) << "<" << match.c_str() << ">\n" >> 0;
}
}
}
END : {
TLuaInterpreter* pL = mpHost->getLuaInterpreter();
pL->setCaptureGroups(captureList, posList);
// call lua trigger function with number of matches and matches itselves as arguments
execute();
pL->clearCaptureGroups();
}
MUD_ERROR:
for (auto childAlias : *mpMyChildrenList) {
if (childAlias->match(toMatch)) {
matchCondition = true;
}
}
free(subject);
return matchCondition;
}
static void pcre_deleter(pcre* pointer)
{
pcre_free(pointer);
}
void TAlias::setRegexCode(const QString& code)
{
mRegexCode = code;
compileRegex();
}
void TAlias::compileRegex()
{
const char* error;
int erroffset;
// PCRE_UTF8 needed to run compile in UTF-8 mode
// PCRE_UCP needed for \d, \w etc. to use Unicode properties:
QSharedPointer<pcre> re(pcre_compile(mRegexCode.toUtf8().constData(), PCRE_UTF8 | PCRE_UCP, &error, &erroffset, nullptr), pcre_deleter);
if (re == nullptr) {
mOK_init = false;
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::red)) << "REGEX ERROR: failed to compile, reason:\n" << error << "\n" >> 0;
TDebug(QColor(Qt::red), QColor(Qt::gray)) << R"(in: ")" << mRegexCode << "\"\n" >> 0;
}
setError(QStringLiteral("<b><font color='blue'>%1</font></b>").arg(tr(R"(Error: in "Pattern:", faulty regular expression, reason: "%1".)", error)));
} else {
mOK_init = true;
}
mpRegex = re;
}
bool TAlias::registerAlias()
{
if (!mpHost) {
qDebug() << "ERROR: TAlias::registerTrigger() pHost=0";
return false;
}
return mpHost->getAliasUnit()->registerAlias(this);
}
void TAlias::compileAll()
{
mNeedsToBeCompiled = true;
if (!compileScript()) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::red)) << "ERROR: Lua compile error. compiling script of alias:" << mName << "\n" >> 0;
}
mOK_code = false;
}
compileRegex(); // Effectively will repost the error if there was a problem in the regex
for (auto alias : *mpMyChildrenList) {
alias->compileAll();
}
}
void TAlias::compile()
{
if (mNeedsToBeCompiled) {
if (!compileScript()) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::red)) << "ERROR: Lua compile error. compiling script of alias:" << mName << "\n" >> 0;
}
mOK_code = false;
}
}
for (auto alias : *mpMyChildrenList) {
alias->compile();
}
}
bool TAlias::setScript(const QString& script)
{
mScript = script;
mNeedsToBeCompiled = true;
mOK_code = compileScript();
return mOK_code;
}
bool TAlias::compileScript()
{
QString code = QStringLiteral("function Alias%1() %2\nend").arg(QString::number(mID), mScript);
QString aliasName = QStringLiteral("Alias: %1").arg(getName());
mFuncName = QStringLiteral("Alias%1").arg(QString::number(mID));
QString error;
if (mpHost->mLuaInterpreter.compile(code, error, aliasName)) {
mNeedsToBeCompiled = false;
mOK_code = true;
return true;
} else {
mOK_code = false;
setError(error);
return false;
}
}
void TAlias::execute()
{
if (mCommand.size() > 0) {
mpHost->send(mCommand);
}
if (mNeedsToBeCompiled) {
if (!compileScript()) {
return;
}
}
mpHost->mLuaInterpreter.call(mFuncName, mName);
}