forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TriggerUnit.cpp
416 lines (376 loc) · 11.5 KB
/
TriggerUnit.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.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 "TriggerUnit.h"
#include "Host.h"
#include "TConsole.h"
#include "TTrigger.h"
void TriggerUnit::initStats()
{
statsTriggerTotal = 0;
statsTempTriggers = 0;
statsActiveTriggers = 0;
statsActiveTriggersMax = 0;
statsActiveTriggersMin = 0;
statsActiveTriggersAverage = 0;
statsTempTriggersCreated = 0;
statsTempTriggersKilled = 0;
statsAverageLineProcessingTime = 0;
statsMaxLineProcessingTime = 0;
statsMinLineProcessingTime = 0;
statsRegexTriggers = 0;
}
void TriggerUnit::_uninstall(TTrigger* pChild, const QString& packageName)
{
std::list<TTrigger*>* childrenList = pChild->mpMyChildrenList;
for (auto trigger : *childrenList) {
_uninstall(trigger, packageName);
uninstallList.append(trigger);
}
}
void TriggerUnit::uninstall(const QString& packageName)
{
for (auto rootTrigger : mTriggerRootNodeList) {
if (rootTrigger->mPackageName == packageName) {
_uninstall(rootTrigger, packageName);
uninstallList.append(rootTrigger);
}
}
for (auto& trigger : uninstallList) {
unregisterTrigger(trigger);
}
uninstallList.clear();
}
void TriggerUnit::removeAllTempTriggers()
{
for (auto trigger : mTriggerRootNodeList) {
if (trigger->isTemporary()) {
trigger->setIsActive(false);
markCleanup(trigger);
}
}
}
void TriggerUnit::addTriggerRootNode(TTrigger* pT, int parentPosition, int childPosition, bool moveTrigger)
{
if (!pT) {
return;
}
if (!pT->getID()) {
pT->setID(getNewID());
}
if ((parentPosition == -1) || (childPosition >= static_cast<int>(mTriggerRootNodeList.size()))) {
mTriggerRootNodeList.push_back(pT);
} else {
// insert item at proper position
int cnt = 0;
for (auto it = mTriggerRootNodeList.begin(); it != mTriggerRootNodeList.end(); it++) {
if (cnt >= childPosition) {
mTriggerRootNodeList.insert(it, pT);
break;
}
cnt++;
}
}
if (!moveTrigger) {
mTriggerMap.insert(pT->getID(), pT);
}
}
void TriggerUnit::reParentTrigger(int childID, int oldParentID, int newParentID, int parentPosition, int childPosition)
{
TTrigger* pOldParent = getTriggerPrivate(oldParentID);
TTrigger* pNewParent = getTriggerPrivate(newParentID);
TTrigger* pChild = getTriggerPrivate(childID);
if (!pChild) {
return;
}
if (pOldParent) {
pOldParent->popChild(pChild);
} else {
mTriggerRootNodeList.remove(pChild);
}
if (pNewParent) {
pNewParent->addChild(pChild, parentPosition, childPosition);
pChild->setParent(pNewParent);
} else {
pChild->Tree<TTrigger>::setParent(nullptr);
addTriggerRootNode(pChild, parentPosition, childPosition, true);
}
}
void TriggerUnit::removeTriggerRootNode(TTrigger* pT)
{
if (!pT) {
return;
}
if (!pT->isTemporary()) {
mLookupTable.remove(pT->mName, pT);
} else {
mLookupTable.remove(pT->getName());
}
mTriggerMap.remove(pT->getID());
mTriggerRootNodeList.remove(pT);
}
TTrigger* TriggerUnit::getTrigger(int id)
{
QMutexLocker locker(&mTriggerUnitLock);
if (mTriggerMap.find(id) != mTriggerMap.end()) {
return mTriggerMap.value(id);
} else {
return nullptr;
}
}
TTrigger* TriggerUnit::getTriggerPrivate(int id)
{
if (mTriggerMap.find(id) != mTriggerMap.end()) {
return mTriggerMap.value(id);
} else {
return nullptr;
}
}
bool TriggerUnit::registerTrigger(TTrigger* pT)
{
if (!pT) {
return false;
}
if (pT->getParent()) {
addTrigger(pT);
return true;
} else {
addTriggerRootNode(pT);
return true;
}
}
void TriggerUnit::unregisterTrigger(TTrigger* pT)
{
if (!pT) {
return;
}
if (pT->getParent()) {
removeTrigger(pT);
return;
} else {
removeTriggerRootNode(pT);
return;
}
}
void TriggerUnit::addTrigger(TTrigger* pT)
{
if (!pT) {
return;
}
if (!pT->getID()) {
pT->setID(getNewID());
}
mTriggerMap.insert(pT->getID(), pT);
}
void TriggerUnit::removeTrigger(TTrigger* pT)
{
if (!pT) {
return;
}
if (!pT->isTemporary()) {
mLookupTable.remove(pT->mName, pT);
} else {
mLookupTable.remove(pT->getName());
}
mTriggerMap.remove(pT->getID());
}
// trigger matching order is permanent trigger objects first, temporary objects second
// after package import or module sync this order needs to be reset
void TriggerUnit::reorderTriggersAfterPackageImport()
{
QList<TTrigger*> tempList;
for (auto trigger : mTriggerRootNodeList) {
if (trigger->isTemporary()) {
tempList.push_back(trigger);
}
}
for (auto& trigger : tempList) {
mTriggerRootNodeList.remove(trigger);
}
for (auto& trigger : tempList) {
mTriggerRootNodeList.push_back(trigger);
}
}
int TriggerUnit::getNewID()
{
return ++mMaxID;
}
void TriggerUnit::processDataStream(const QString& data, int line)
{
if (!data.isEmpty()) {
#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(data.toUtf8().data()) + 1);
strcpy(subject, data.toUtf8().data());
#else
char* subject = strndup(data.toUtf8().constData(), strlen(data.toUtf8().constData()));
#endif
for (auto trigger : mTriggerRootNodeList) {
trigger->match(subject, data, line);
}
free(subject);
for (auto& trigger : mCleanupList) {
delete trigger;
}
mCleanupList.clear();
}
}
void TriggerUnit::compileAll()
{
for (auto trigger : mTriggerRootNodeList) {
if (trigger->isActive()) {
trigger->compileAll();
}
}
}
void TriggerUnit::stopAllTriggers()
{
for (auto trigger : mTriggerRootNodeList) {
trigger->disableFamily();
}
}
void TriggerUnit::reenableAllTriggers()
{
for (auto trigger : mTriggerRootNodeList) {
trigger->enableFamily();
}
}
TTrigger* TriggerUnit::findTrigger(const QString& name)
{
QMap<QString, TTrigger*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTrigger* pT = it.value();
return pT;
}
return nullptr;
}
bool TriggerUnit::enableTrigger(const QString& name)
{
bool found = false;
QMap<QString, TTrigger*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTrigger* pT = it.value();
pT->setIsActive(true);
++it;
found = true;
}
return found;
}
bool TriggerUnit::disableTrigger(const QString& name)
{
bool found = false;
QMap<QString, TTrigger*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTrigger* pT = it.value();
pT->setIsActive(false);
++it;
found = true;
}
return found;
}
void TriggerUnit::setTriggerStayOpen(const QString& name, int lines)
{
QMap<QString, TTrigger*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTrigger* pT = it.value();
pT->mKeepFiring = lines;
++it;
}
}
bool TriggerUnit::killTrigger(const QString& name)
{
QMap<QString, TTrigger*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTrigger* pT = it.value();
if (pT->isTemporary()) //this function is only defined for tempTriggers, permanent objects cannot be removed
{
// there can only be a single tempTrigger by this name and this function ignores non-tempTriggers by definition
markCleanup(pT);
return true;
}
it++;
}
return false;
}
void TriggerUnit::_assembleReport(TTrigger* pChild)
{
std::list<TTrigger*>* childrenList = pChild->mpMyChildrenList;
for (auto trigger : *childrenList) {
_assembleReport(trigger);
if (trigger->isActive()) {
statsActiveTriggers++;
}
if (trigger->isTemporary()) {
statsTempTriggers++;
}
statsPatterns += trigger->mRegexCodeList.size();
statsTriggerTotal++;
}
}
QString TriggerUnit::assembleReport()
{
statsActiveTriggers = 0;
statsTriggerTotal = 0;
statsTempTriggers = 0;
statsPatterns = 0;
for (auto rootTrigger : mTriggerRootNodeList) {
if (rootTrigger->isActive()) {
statsActiveTriggers++;
}
if (rootTrigger->isTemporary()) {
statsTempTriggers++;
}
statsPatterns += rootTrigger->mRegexCodeList.size();
statsTriggerTotal++;
std::list<TTrigger*>* childrenList = rootTrigger->mpMyChildrenList;
for (auto childTrigger : *childrenList) {
_assembleReport(childTrigger);
if (childTrigger->isActive()) {
statsActiveTriggers++;
}
if (childTrigger->isTemporary()) {
statsTempTriggers++;
}
statsPatterns += childTrigger->mRegexCodeList.size();
statsTriggerTotal++;
}
}
QStringList msg;
msg << "triggers current total: " << QString::number(statsTriggerTotal) << "\n"
<< "trigger patterns total: " << QString::number(statsPatterns) << "\n"
<< "tempTriggers current total: " << QString::number(statsTempTriggers) << "\n"
<< "active triggers: " << QString::number(statsActiveTriggers) << "\n";
return msg.join("");
}
void TriggerUnit::doCleanup()
{
for (auto trigger : mCleanupList) {
delete trigger;
}
mCleanupList.clear();
}
void TriggerUnit::markCleanup(TTrigger* pT)
{
for (auto trigger : mCleanupList) {
if (trigger == pT) {
return;
}
}
mCleanupList.push_back(pT);
}