forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTrigger.cpp
More file actions
1338 lines (1232 loc) · 43.9 KB
/
TTrigger.cpp
File metadata and controls
1338 lines (1232 loc) · 43.9 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2016 by Chris Leacy - cleacy1972@gmail.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 "TTrigger.h"
#include "Host.h"
#include "TConsole.h"
#include "TDebug.h"
#include "TMatchState.h"
#include "mudlet.h"
#include "pre_guard.h"
#include <QRegExp>
#include "post_guard.h"
#include <assert.h>
#include <sstream>
using namespace std;
const QString nothing = "";
TTrigger::TTrigger( TTrigger * parent, Host * pHost )
: Tree<TTrigger>( parent )
, mTriggerContainsPerlRegex( false )
, mPerlSlashGOption( false )
, mFilterTrigger( false )
, mSoundTrigger( false )
, mStayOpen( 0 )
, mColorTrigger( false )
, mColorTriggerFg( false )
, mColorTriggerBg( false )
, mKeepFiring( 0 )
, mpHost( pHost )
, exportItem(true)
, mModuleMasterFolder(false)
, mNeedsToBeCompiled(true)
, mTriggerType(REGEX_SUBSTRING)
, mIsLineTrigger(false)
, mStartOfLineDelta(0)
, mLineDelta(3)
, mIsMultiline(false)
, mConditionLineDelta(0)
, mpLua(mpHost->getLuaInterpreter())
, mFgColor(QColor(Qt::red))
, mBgColor(QColor(Qt::yellow))
, mIsColorizerTrigger(false)
, mModuleMember(false)
, mColorTriggerFgAnsi()
, mColorTriggerBgAnsi()
{
}
TTrigger::TTrigger(const QString& name, QStringList regexList, QList<int> regexProperyList, bool isMultiline, Host* pHost)
: Tree<TTrigger>(0)
, mTriggerContainsPerlRegex( false )
, mPerlSlashGOption( false )
, mFilterTrigger( false )
, mSoundTrigger( false )
, mStayOpen( 0 )
, mColorTrigger( false )
, mColorTriggerFg( false )
, mColorTriggerBg( false )
, mKeepFiring( 0 )
, mpHost( pHost )
, mName( name )
, mRegexCodeList( regexList )
, exportItem(true)
, mModuleMasterFolder(false)
, mRegexCodePropertyList(regexProperyList)
, mNeedsToBeCompiled(true)
, mTriggerType(REGEX_SUBSTRING)
, mIsLineTrigger(false)
, mStartOfLineDelta(0)
, mLineDelta(3)
, mIsMultiline(isMultiline)
, mConditionLineDelta(0)
, mpLua(mpHost->getLuaInterpreter())
, mFgColor(QColor(Qt::red))
, mBgColor(QColor(Qt::yellow))
, mIsColorizerTrigger(false)
, mModuleMember(false)
, mColorTriggerFgAnsi()
, mColorTriggerBgAnsi()
{
setRegexCodeList(regexList, regexProperyList);
}
TTrigger::~TTrigger()
{
if (!mpHost) {
return;
}
mpHost->getTriggerUnit()->unregisterTrigger(this);
}
void TTrigger::setName(const QString& name)
{
if( ! isTemporary() )
{
mpHost->getTriggerUnit()->mLookupTable.remove( mName, this );
}
mName = name;
mpHost->getTriggerUnit()->mLookupTable.insertMulti(name, this);
}
static void pcre_deleter(pcre* pointer)
{
pcre_free(pointer);
}
//FIXME: sperren, wenn code nicht compiliert werden kann *ODER* regex falsch
bool TTrigger::setRegexCodeList(QStringList regexList, QList<int> propertyList)
{
regexList.replaceInStrings("\n", "");
mRegexCodeList.clear();
mRegexMap.clear();
mRegexCodePropertyList.clear();
mLuaConditionMap.clear();
mColorPatternList.clear();
mTriggerContainsPerlRegex = false;
if (propertyList.size() != regexList.size()) {
//FIXME: ronny hat das irgendwie geschafft
qDebug() << "[CRITICAL ERROR (plz report):] Trigger name=" << mName << " aborting reason: propertyList.size() != regexList.size()";
}
if ((propertyList.size() < 1) && (!isFolder()) && (!mColorTrigger)) {
setError("No patterns defined.");
mOK_init = false;
return false;
}
bool state = true;
for (int i = 0; i < regexList.size(); i++) {
if (regexList[i].size() < 1) {
continue;
}
mRegexCodeList.append(regexList[i]);
mRegexCodePropertyList.append(propertyList[i]);
if (propertyList[i] == REGEX_PERL) {
const char* error;
const QByteArray& local8Bit = regexList[i].toLocal8Bit();
int erroffset;
QSharedPointer<pcre> re(pcre_compile(local8Bit.constData(), 0, &error, &erroffset, 0), pcre_deleter);
if (!re) {
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: ")" << local8Bit.constData() << "\"\n" >> 0;
}
setError(QStringLiteral("<b><font color='blue'>%1</font></b>")
.arg(tr(R"(Error: in item %1, perl regex: "%2", it failed to compile, reason: "%3".)").arg(QString::number(i), local8Bit.constData(), error)));
state = false;
} else {
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::darkGreen)) << "[OK]: REGEX_COMPILE OK\n" >> 0;
}
}
mRegexMap[i] = re;
mTriggerContainsPerlRegex = true;
}
if (propertyList.at(i) == REGEX_LUA_CODE) {
std::string funcName;
std::stringstream func;
func << "trigger" << mID << "condition" << i;
funcName = func.str();
QString code = QStringLiteral("function %1()\n%2\nend\n").arg(funcName.c_str(), regexList[i]);
QString error;
if (!mpLua->compile(code, error, QString::fromStdString(funcName))) {
setError(QStringLiteral("<b><font color='blue'>%1</font></b>")
.arg(tr(R"(Error: in item %1, lua condition function "%2" failed to compile, reason:%3.)").arg(QString::number(i), regexList.at(i), error)));
state = false;
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::red)) << "LUA ERROR: failed to compile, reason:\n" << error << "\n" >> 0;
TDebug(QColor(Qt::red), QColor(Qt::gray)) << R"(in lua condition function: ")" << regexList.at(i) << "\"\n" >> 0;
}
} else {
mLuaConditionMap[i] = funcName;
}
}
if (propertyList[i] == REGEX_COLOR_PATTERN) {
QRegExp regex = QRegExp(R"(FG(\d+)BG(\d+))");
int _pos = regex.indexIn(regexList[i]);
if (_pos == -1) {
mColorPatternList.push_back(0);
state = false;
continue;
}
int ansiFg = regex.cap(1).toInt();
int ansiBg = regex.cap(2).toInt();
if (!setupColorTrigger(ansiFg, ansiBg)) {
mColorPatternList.push_back(0);
state = false;
continue;
}
} else {
mColorPatternList.push_back(0);
}
}
if (!state) {
mOK_init = false;
} else {
mOK_init = true;
}
return state;
}
bool TTrigger::match_perl(char* subject, const QString& toMatch, int regexNumber, int posOffset)
{
assert(mRegexMap.contains(regexNumber));
QSharedPointer<pcre> re = mRegexMap[regexNumber];
if (!re) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::red)) << "ERROR:" << 0;
TDebug(QColor(Qt::darkRed), QColor(Qt::darkGray)) << " the regex of trigger " << mName
<< " does not compile. Please correct the expression. This trigger will never match until it is fixed.\n"
>> 0;
}
return false; //regex compile error
}
int numberOfCaptureGroups = 0;
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
rc = pcre_exec(re.data(), 0, subject, subject_length, 0, 0, ovector, 100);
if (rc < 0) {
return false;
}
if (rc > 0) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::blue), QColor(Qt::black)) << "Trigger name=" << mName << "(" << mRegexCodeList.value(regexNumber) << ") matched.\n" >> 0;
}
}
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] + posOffset);
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;
}
}
pcre_fullinfo(re.data(), NULL, PCRE_INFO_NAMECOUNT, &namecount);
if (namecount <= 0) {
;
} else {
unsigned char* tabptr;
pcre_fullinfo(re.data(), NULL, PCRE_INFO_NAMETABLE, &name_table);
pcre_fullinfo(re.data(), NULL, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size);
tabptr = name_table;
for (i = 0; i < namecount; i++) {
tabptr += name_entry_size;
}
}
//TODO: add named groups seperately later as Lua::namedGroups
if (mIsColorizerTrigger || mFilterTrigger) {
numberOfCaptureGroups = captureList.size();
}
for (; mPerlSlashGOption;) {
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(), NULL, subject, subject_length, start_offset, options, ovector, 30);
if (rc == PCRE_ERROR_NOMATCH) {
if (options == 0) {
break;
}
ovector[1] = start_offset + 1;
continue;
}
if (rc < 0) {
goto END;
}
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] + posOffset);
if (mudlet::debugMode) {
TDebug(QColor(Qt::darkCyan), QColor(Qt::black)) << "<regex mode: match all> capture group #" << (i + 1) << " = " >> 0;
TDebug(QColor(Qt::darkMagenta), QColor(Qt::black)) << "<" << match.c_str() << ">\n" >> 0;
}
}
}
END : {
if (mIsColorizerTrigger) {
int r1 = mBgColor.red();
int g1 = mBgColor.green();
int b1 = mBgColor.blue();
int r2 = mFgColor.red();
int g2 = mFgColor.green();
int b2 = mFgColor.blue();
int total = captureList.size();
TConsole* pC = mpHost->mpConsole;
pC->deselect();
auto its = captureList.begin();
auto iti = posList.begin();
for (int i = 1; iti != posList.end(); ++iti, ++its, i++) {
int begin = *iti;
std::string& s = *its;
int length = s.size();
if (total > 1) {
// skip complete match in Perl /g option type of triggers
// to enable people to highlight capture groups if there are any
// otherwise highlight complete expression match
if (i % numberOfCaptureGroups != 1) {
pC->selectSection(begin, length);
pC->setBgColor(r1, g1, b1);
pC->setFgColor(r2, g2, b2);
}
} else {
pC->selectSection(begin, length);
pC->setBgColor(r1, g1, b1);
pC->setFgColor(r2, g2, b2);
}
}
pC->reset();
}
if (mIsMultiline) {
updateMultistates(regexNumber, captureList, posList);
return true;
} else {
TLuaInterpreter* pL = mpHost->getLuaInterpreter();
pL->setCaptureGroups(captureList, posList);
execute();
pL->clearCaptureGroups();
if (mFilterTrigger) {
if (captureList.size() > 1) {
int total = captureList.size();
auto its = captureList.begin();
auto iti = posList.begin();
for (int i = 1; iti != posList.end(); ++iti, ++its, i++) {
int begin = *iti;
std::string& s = *its;
if (total > 1) {
// skip complete match in Perl /g option type of triggers
// to enable people to highlight capture groups if there are any
// otherwise highlight complete expression match
if (i % numberOfCaptureGroups != 1) {
filter(s, begin);
}
} else {
filter(s, begin);
}
}
}
}
return true;
}
}
return true;
}
bool TTrigger::match_begin_of_line_substring(const QString& toMatch, const QString& regex, int regexNumber, int posOffset)
{
if (toMatch.startsWith(regex)) {
std::list<std::string> captureList;
std::list<int> posList;
captureList.push_back(regex.toLatin1().data());
posList.push_back(0 + posOffset);
if (mudlet::debugMode) {
TDebug(QColor(Qt::darkCyan), QColor(Qt::black)) << "Trigger name=" << mName << "(" << mRegexCodeList.value(regexNumber) << ") matched.\n" >> 0;
}
if (mIsColorizerTrigger) {
int r1 = mBgColor.red();
int g1 = mBgColor.green();
int b1 = mBgColor.blue();
int r2 = mFgColor.red();
int g2 = mFgColor.green();
int b2 = mFgColor.blue();
TConsole* pC = mpHost->mpConsole;
auto its = captureList.begin();
for (auto iti = posList.begin(); iti != posList.end(); ++iti, ++its) {
int begin = *iti;
std::string& s = *its;
int length = s.size();
pC->selectSection(begin, length);
pC->setBgColor(r1, g1, b1);
pC->setFgColor(r2, g2, b2);
}
pC->reset();
}
if (mIsMultiline) {
updateMultistates(regexNumber, captureList, posList);
return true;
} else {
TLuaInterpreter* pL = mpHost->getLuaInterpreter();
pL->setCaptureGroups(captureList, posList);
// call lua trigger function with number of matches and matches itselves as arguments
execute();
pL->clearCaptureGroups();
if (mFilterTrigger) {
if (captureList.size() > 0) {
filter(captureList.front(), posList.front());
}
}
return true;
}
}
return false;
}
inline void TTrigger::updateMultistates(int regexNumber, std::list<std::string>& captureList, std::list<int>& posList)
{
if (regexNumber == 0) {
// wird automatisch auf #1 gesetzt
auto pCondition = new TMatchState(mRegexCodeList.size(), mConditionLineDelta);
mConditionMap[pCondition] = pCondition;
pCondition->multiCaptureList.push_back(captureList);
pCondition->multiCapturePosList.push_back(posList);
if (mudlet::debugMode) {
TDebug(QColor(Qt::darkYellow), QColor(Qt::black)) << "match state " << mConditionMap.size() << "/" << mConditionMap.size() << " condition #" << regexNumber << "=true (" << regexNumber
<< "/" << mRegexCodeList.size() << ") regex=" << mRegexCodeList[regexNumber] << "\n"
>> 0;
}
} else {
int k = 0;
for (auto& matchStatePair : mConditionMap) {
k++;
if (matchStatePair.second->nextCondition() == regexNumber) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::darkYellow), QColor(Qt::black)) << "match state " << k << "/" << mConditionMap.size() << " condition #" << regexNumber << "=true (" << regexNumber << "/"
<< mRegexCodeList.size() << ") regex=" << mRegexCodeList[regexNumber] << "\n"
>> 0;
}
matchStatePair.second->conditionMatched();
matchStatePair.second->multiCaptureList.push_back(captureList);
matchStatePair.second->multiCapturePosList.push_back(posList);
}
}
}
}
inline void TTrigger::filter(std::string& capture, int& posOffset)
{
if (capture.size() < 1) {
return;
}
char* filterSubject = (char*)malloc(capture.size() + 2048);
if (filterSubject) {
strcpy(filterSubject, capture.c_str());
} else {
return;
}
QString text = capture.c_str();
for (auto& trigger : *mpMyChildrenList) {
trigger->match(filterSubject, text, -1, posOffset);
}
free(filterSubject);
}
bool TTrigger::match_substring(const QString& toMatch, const QString& regex, int regexNumber, int posOffset)
{
int where = toMatch.indexOf(regex);
if (where != -1) {
std::list<std::string> captureList;
std::list<int> posList;
captureList.push_back(regex.toLatin1().data());
posList.push_back(where + posOffset);
if (mPerlSlashGOption) {
while ((where = toMatch.indexOf(regex, where + 1)) != -1) {
captureList.push_back(regex.toLatin1().data());
posList.push_back(where + posOffset);
}
}
if (mudlet::debugMode) {
TDebug(QColor(Qt::cyan), QColor(Qt::black)) << "Trigger name=" << mName << "(" << mRegexCodeList.value(regexNumber) << ") matched.\n" >> 0;
}
if (mIsColorizerTrigger) {
int r1 = mBgColor.red();
int g1 = mBgColor.green();
int b1 = mBgColor.blue();
int r2 = mFgColor.red();
int g2 = mFgColor.green();
int b2 = mFgColor.blue();
TConsole* pC = mpHost->mpConsole;
pC->deselect();
auto its = captureList.begin();
for (auto iti = posList.begin(); iti != posList.end(); ++iti, ++its) {
int begin = *iti;
std::string& s = *its;
int length = s.size();
pC->selectSection(begin, length);
pC->setBgColor(r1, g1, b1);
pC->setFgColor(r2, g2, b2);
}
pC->reset();
}
if (mIsMultiline) {
updateMultistates(regexNumber, captureList, posList);
return true;
} else {
TLuaInterpreter* pL = mpHost->getLuaInterpreter();
pL->setCaptureGroups(captureList, posList);
// call lua trigger function with number of matches and matches itselves as arguments
execute();
pL->clearCaptureGroups();
if (mFilterTrigger) {
if (captureList.size() > 0) {
filter(captureList.front(), posList.front());
}
}
return true;
}
}
return false;
}
bool TTrigger::match_color_pattern(int line, int regexNumber)
{
if (regexNumber >= mColorPatternList.size()) {
return false;
}
if (line == -1) {
return false;
}
bool canExecute = false;
std::list<std::string> captureList;
std::list<int> posList;
if (line >= static_cast<int>(mpHost->mpConsole->buffer.buffer.size())) {
return false;
}
std::deque<TChar>& bufferLine = mpHost->mpConsole->buffer.buffer[line];
QString& lineBuffer = mpHost->mpConsole->buffer.lineBuffer[line];
int pos = 0;
int matchBegin = -1;
bool matching = false;
TColorTable* pCT = mColorPatternList[regexNumber];
if (!pCT) {
return false; //no color pattern created
}
int mFgR = pCT->fgR;
int mFgG = pCT->fgG;
int mFgB = pCT->fgB;
int mBgR = pCT->bgR;
int mBgG = pCT->bgG;
int mBgB = pCT->bgB;
for (auto it = bufferLine.begin(); it != bufferLine.end(); it++, pos++) {
if (((*it).fgR == mFgR) && ((*it).fgG == mFgG) && ((*it).fgB == mFgB) && ((*it).bgR == mBgR) && ((*it).bgG == mBgG) && ((*it).bgB == mBgB)) {
if (matchBegin == -1) {
matchBegin = pos;
}
matching = true;
} else {
matching = false;
}
if ((!matching) || (matching && (pos + 1 >= static_cast<int>(bufferLine.size())))) {
if (matchBegin > -1) {
std::string got;
if (matching) {
got = lineBuffer.mid(matchBegin, pos - matchBegin + 1).toLatin1().data();
} else {
got = lineBuffer.mid(matchBegin, pos - matchBegin).toLatin1().data();
}
captureList.push_back(got);
posList.push_back(matchBegin);
matchBegin = -1;
canExecute = true;
matching = false;
}
}
}
if (canExecute) {
if (mIsColorizerTrigger) {
int r1 = mBgColor.red();
int g1 = mBgColor.green();
int b1 = mBgColor.blue();
int r2 = mFgColor.red();
int g2 = mFgColor.green();
int b2 = mFgColor.blue();
TConsole* pC = mpHost->mpConsole;
pC->deselect();
auto its = captureList.begin();
for (auto iti = posList.begin(); iti != posList.end(); ++iti, ++its) {
int begin = *iti;
std::string& s = *its;
cout << "CTgot<" << s << "> bis:" << s.size() << endl;
int length = s.size();
pC->selectSection(begin, length);
pC->setBgColor(r1, g1, b1);
pC->setFgColor(r2, g2, b2);
}
pC->reset();
}
if (mIsMultiline) {
updateMultistates(regexNumber, captureList, posList);
return true;
} else {
TLuaInterpreter* pL = mpHost->getLuaInterpreter();
pL->setCaptureGroups(captureList, posList);
// call lua trigger function with number of matches and matches itselves as arguments
execute();
pL->clearCaptureGroups();
if (mFilterTrigger) {
if (captureList.size() > 0) {
auto it1 = captureList.begin();
auto it2 = posList.begin();
for (; it1 != captureList.end(); it1++, it2++) {
filter(*it1, *it2);
}
}
}
return true;
}
}
return false;
}
bool TTrigger::match_line_spacer(int regexNumber)
{
if (mIsMultiline) {
int k = 0;
for (auto& matchStatePair : mConditionMap) {
k++;
if (matchStatePair.second->nextCondition() == regexNumber) {
if (matchStatePair.second->lineSpacerMatch(mRegexCodeList.value(regexNumber).toInt())) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::yellow), QColor(Qt::black)) << "Trigger name=" << mName << "(" << mRegexCodeList.value(regexNumber) << ") condition #" << regexNumber << "=true " >> 0;
TDebug(QColor(Qt::darkYellow), QColor(Qt::black)) << "match state " << k << "/" << mConditionMap.size() << " condition #" << regexNumber << "=true (" << regexNumber + 1 << "/"
<< mRegexCodeList.size() << ") line spacer=" << mRegexCodeList.value(regexNumber) << "lines\n"
>> 0;
}
matchStatePair.second->conditionMatched();
std::list<string> captureList;
std::list<int> posList;
matchStatePair.second->multiCaptureList.push_back(captureList);
matchStatePair.second->multiCapturePosList.push_back(posList);
}
}
}
}
return true; //line spacers don't make sense outside of AND triggers -> ignore them
}
bool TTrigger::match_lua_code(int regexNumber)
{
if (mLuaConditionMap.find(regexNumber) == mLuaConditionMap.end()) {
return false;
}
if (mpLua->callConditionFunction(mLuaConditionMap[regexNumber], mName)) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::yellow), QColor(Qt::black)) << "Trigger name=" << mName << "(" << mRegexCodeList.value(regexNumber) << ") matched.\n" >> 0;
}
if (mIsMultiline) {
std::list<std::string> captureList;
std::list<int> posList;
updateMultistates(regexNumber, captureList, posList);
return true;
}
execute();
return true;
}
return false;
}
bool TTrigger::match_exact_match(const QString& toMatch, const QString& line, int regexNumber, int posOffset)
{
QString text = toMatch;
if (text.endsWith(QChar('\n'))) {
text.chop(1); //TODO: speed optimization
}
if (text == line) {
std::list<std::string> captureList;
std::list<int> posList;
captureList.push_back(line.toLatin1().data());
posList.push_back(0 + posOffset);
if (mudlet::debugMode) {
TDebug(QColor(Qt::yellow), QColor(Qt::black)) << "Trigger name=" << mName << "(" << mRegexCodeList.value(regexNumber) << ") matched.\n" >> 0;
}
if (mIsColorizerTrigger) {
int r1 = mBgColor.red();
int g1 = mBgColor.green();
int b1 = mBgColor.blue();
int r2 = mFgColor.red();
int g2 = mFgColor.green();
int b2 = mFgColor.blue();
TConsole* pC = mpHost->mpConsole;
auto its = captureList.begin();
for (auto iti = posList.begin(); iti != posList.end(); ++iti, ++its) {
int begin = *iti;
std::string& s = *its;
int length = s.size();
pC->selectSection(begin, length);
pC->setBgColor(r1, g1, b1);
pC->setFgColor(r2, g2, b2);
}
pC->reset();
}
if (mIsMultiline) {
updateMultistates(regexNumber, captureList, posList);
return true;
} else {
TLuaInterpreter* pL = mpHost->getLuaInterpreter();
pL->setCaptureGroups(captureList, posList);
// call lua trigger function with number of matches and matches itselves as arguments
execute();
pL->clearCaptureGroups();
if (mFilterTrigger) {
if (captureList.size() > 0) {
filter(captureList.front(), posList.front());
}
}
return true;
}
}
return false;
}
bool TTrigger::match(char* subject, const QString& toMatch, int line, int posOffset)
{
bool ret = false;
if (isActive()) {
if (mIsLineTrigger) {
if (--mStartOfLineDelta < 0) {
execute();
if (--mLineDelta <= 0) {
deactivate();
mpHost->getTriggerUnit()->markCleanup(this);
}
return true;
}
return false;
}
if (toMatch.size() < 1) {
return false;
}
bool conditionMet = false;
int highestCondition = 0;
if (mIsMultiline) {
for (auto& matchStatePair : mConditionMap) {
matchStatePair.second->newLineArrived();
int next = matchStatePair.second->nextCondition();
if (next > highestCondition) {
highestCondition = next;
}
}
}
int size = mRegexCodePropertyList.size();
for (int i = 0;; i++) {
if (i >= size) {
break;
}
ret = false;
switch (mRegexCodePropertyList.value(i)) {
case REGEX_SUBSTRING:
ret = match_substring(toMatch, mRegexCodeList[i], i, posOffset);
break;
case REGEX_PERL:
ret = match_perl(subject, toMatch, i, posOffset);
break;
case REGEX_BEGIN_OF_LINE_SUBSTRING:
ret = match_begin_of_line_substring(toMatch, mRegexCodeList[i], i, posOffset);
break;
case REGEX_EXACT_MATCH:
ret = match_exact_match(toMatch, mRegexCodeList[i], i, posOffset);
break;
case REGEX_LUA_CODE:
ret = match_lua_code(i);
break;
case REGEX_LINE_SPACER:
ret = match_line_spacer(i);
break;
case REGEX_COLOR_PATTERN:
ret = match_color_pattern(line, i);
break;
}
// policy: one match is enough to fire on OR-trigger, but in the case of
// an AND-trigger all conditions have to be met in order to fire the trigger
if (!mIsMultiline) {
if (ret) {
conditionMet = true;
mKeepFiring = mStayOpen;
break;
}
} else {
if ((!ret) && (i >= highestCondition)) {
break;
}
}
}
// in the case of multiline triggers: check our state
if (mIsMultiline) {
int k = 0;
conditionMet = false; //invalidate conditionMet as it has no meaning for multiline triggers
list<TMatchState*> removeList;
for (auto& matchStatePair : mConditionMap) {
k++;
if (matchStatePair.second->isComplete()) {
mKeepFiring = mStayOpen;
if (mudlet::debugMode) {
TDebug(QColor(Qt::yellow), QColor(Qt::darkMagenta)) << "multiline trigger name=" << mName << " *FIRES* all conditons are fullfilled. Executing script.\n" >> 0;
}
removeList.push_back(matchStatePair.first);
conditionMet = true;
TLuaInterpreter* pL = mpHost->getLuaInterpreter();
pL->setMultiCaptureGroups(matchStatePair.second->multiCaptureList, matchStatePair.second->multiCapturePosList);
execute();
pL->clearCaptureGroups();
if (mFilterTrigger) {
std::list<std::list<std::string>> multiCaptureList;
multiCaptureList = matchStatePair.second->multiCaptureList;
if (multiCaptureList.size() > 0) {
for (auto mit = multiCaptureList.begin(); mit != multiCaptureList.end(); mit++, k++) {
int total = (*mit).size();
auto its = (*mit).begin();
for (int i = 1; its != (*mit).end(); ++its, i++) {
std::string s = *its;
int p = 0;
if (total > 1) {
if (i % total != 1) {
filter(s, p);
}
} else {
filter(s, p);
}
}
}
}
}
}
if (!matchStatePair.second->newLine()) {
removeList.push_back(matchStatePair.first);
}
}
for (auto& matchState : removeList) {
if (mConditionMap.find(matchState) != mConditionMap.end()) {
delete mConditionMap[matchState];
if (mudlet::debugMode) {
TDebug(QColor(Qt::darkBlue), QColor(Qt::black)) << "removing condition from conditon table.\n" >> 0;
}
mConditionMap.erase(matchState);
}
}
}
// definition trigger chain: a folder is part of a trigger chain if it has a regex defined
// a trigger chain only lets data pass if the condition matches or in case of multiline all
// all conditions are fullfilled
//
// a folder can also be a simple structural element in which case all data passes through
// if at least one regex is defined a folder is considered a trigger chain otherwise a structural element
if (!mFilterTrigger) {
if (conditionMet || (mRegexCodeList.size() < 1)) {
for (auto trigger : *mpMyChildrenList) {
ret = trigger->match(subject, toMatch, line);
if (ret) {
conditionMet = true;
}
}
}
}
if ((mKeepFiring > 0) && (!conditionMet)) {
mKeepFiring--;
if ((mKeepFiring == mStayOpen) || (mpMyChildrenList->size() == 0)) {
execute();
}
for (auto trigger : *mpMyChildrenList) {
ret = trigger->match(subject, toMatch, line);
if (ret) {
conditionMet = true;
}
}
return true;
}
return conditionMet;
}
return false;
}
// Die Musternummer wird ID im color-pattern lookup table
TColorTable* TTrigger::createColorPattern(int ansiFg, int ansiBg)
{
/* Mudlet simplified ANSI color codes
-----------------------------------
0 default text color
1 light black
2 dark black
3 light red
4 dark red
5 light green
6 dark green
7 light yellow
8 dark yellow
9 light blue
10 dark blue
11 light magenta
12 dark magenta
13 light cyan
14 dark cyan
15 light white
16 dark white */