| 12
 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
 1001
 1002
 1003
 1004
 1005
 1006
 1007
 1008
 1009
 1010
 1011
 1012
 1013
 1014
 1015
 1016
 1017
 1018
 1019
 1020
 1021
 1022
 1023
 1024
 1025
 1026
 1027
 1028
 1029
 1030
 1031
 1032
 1033
 1034
 1035
 1036
 1037
 1038
 1039
 1040
 1041
 1042
 1043
 1044
 1045
 1046
 1047
 1048
 1049
 1050
 1051
 1052
 1053
 1054
 1055
 1056
 1057
 1058
 1059
 1060
 1061
 1062
 1063
 1064
 1065
 1066
 1067
 1068
 1069
 1070
 1071
 1072
 1073
 1074
 1075
 1076
 1077
 1078
 1079
 1080
 1081
 1082
 1083
 1084
 1085
 1086
 1087
 1088
 1089
 1090
 1091
 1092
 1093
 1094
 1095
 1096
 1097
 1098
 1099
 1100
 1101
 1102
 1103
 1104
 1105
 1106
 1107
 1108
 1109
 1110
 1111
 1112
 1113
 1114
 1115
 1116
 1117
 1118
 
 | /***************************************************************************
 *   Copyright (C) 2005-2019 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   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) version 3.                                           *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include "expressionmatch.h"
#include <QChar>
#include <QDebug>
#include <QString>
#include <QStringList>
#if QT_VERSION >= 0x050000
#include <QRegularExpression>
#else
#include <QRegExp>
#endif
#include "logmessage.h"
ExpressionMatch::ExpressionMatch(const QString &expression, MatchMode mode, bool caseSensitive)
{
    // Store the original parameters for later reference
    _sourceExpression = expression;
    _sourceMode = mode;
    _sourceCaseSensitive = caseSensitive;
    // Calculate the internal regex
    //
    // Do this now instead of on-demand to provide immediate feedback on errors when editing
    // highlight and ignore rules.
    cacheRegEx();
}
bool ExpressionMatch::match(const QString &string, bool matchEmpty) const
{
    // Handle empty expression strings
    if (_sourceExpressionEmpty) {
        // Match found if matching empty is allowed, otherwise no match found
        return matchEmpty;
    }
    if (!isValid()) {
        // Can't match on an invalid rule
        return false;
    }
    // We have "_matchRegEx", "_matchInvertRegEx", or both due to isValid() check above
    // If specified, first check inverted rules
    if (_matchInvertRegExActive && _matchInvertRegEx.isValid()) {
        // Check inverted match rule
        // See _matchRegEx section below for explanations of QRegExp vs. QRegularExpression
        if (
#if QT_VERSION >= 0x050000
                _matchInvertRegEx.match(string).hasMatch()
#else
                _matchInvertRegEx.indexIn(string) != -1
#endif
                ) {
            // Inverted rule matched, the rest of the rule cannot match
            return false;
        }
    }
    if (_matchRegExActive && _matchRegEx.isValid()) {
        // Check regular match rule
    #if QT_VERSION >= 0x050000
        // QRegularExpression does partial matching by default (exact matching requires anchoring
        // expressions to be added)
        // See https://doc.qt.io/qt-5/qregularexpression.html#porting-from-qregexp-exactmatch
        return _matchRegEx.match(string).hasMatch();
    #else
        // QRegExp partial matching is done via indexIn
        // See https://doc.qt.io/qt-5/qregexp.html#indexIn
        return (_matchRegEx.indexIn(string) != -1);
    #endif
    } else {
        // If no valid regular rules exist, due to the isValid() check there must be valid inverted
        // rules that did not match.  Count this as properly matching (implicit wildcard).
        return true;
    }
}
QString ExpressionMatch::trimMultiWildcardWhitespace(const QString &originalRule)
{
    // This gets handled in two steps:
    //
    // 1.  Break apart ";"-separated list into components
    // 2.  Combine whitespace-trimmed components into wildcard expression
    //
    // Let's start by making the list...
    // Convert a ";"-separated list into an actual list, splitting on newlines and unescaping
    // escaped characters
    // Escaped list rules (where "[\n]" represents newline):
    // ---------------
    // Token  | Outcome
    // -------|--------
    // ;      | Split
    // \;     | Keep as "\;"
    // \\;    | Split (keep as "\\")
    // \\\    | Keep as "\\" + "\", set consecutive slashes to 1
    // [\n]   | Split
    // \[\n]  | Split (keep as "\")
    // \\[\n] | Split (keep as "\\")
    // ...    | Keep as "..."
    // \...   | Keep as "\..."
    // \\...  | Keep as "\\..."
    //
    // Strings are forced to end with "\n", always applying "\..." and "\\..." rules
    // "..." also includes another "\" character
    //
    // All whitespace is trimmed from each component
    // "\\" and "\" are not downconverted to allow for other escape codes to be detected in
    // ExpressionMatch::wildcardToRegex
    // Example:
    //
    // > Wildcard rule
    // norm; norm-space ; newline-space [\n] ;escape \; sep ; slash-end-split\\; quad\\\\norm;
    // newline-split-slash\\[\n] slash-at-end\\                       [line does not continue]
    //
    // > Components
    //   norm
    //   norm-space
    //   newline-space
    //   escape \; sep
    //   slash-end-split\\          [line does not continue]
    //   quad\\\\norm
    //   newline-split-slash\\      [line does not continue]
    //   slash-at-end\\             [line does not continue]
    //
    // > Trimmed wildcard rule
    // norm; norm-space; newline-space[\n]escape \; sep; slash-end-split\\; quad\\\\norm;
    // newline-split-slash\\[\n]slash-at-end\\                        [line does not continue]
    //
    // (Newlines are encoded as "[\n]".  Ignore linebreaks for the sake of comment wrapping.)
    // Note: R"(\\)" results in the literal of "\\", two backslash characters.  Anything inside the
    // brackets is treated as a literal.  Outside the brackets but inside the quotes is still
    // escaped.
    //
    // See https://en.cppreference.com/w/cpp/language/string_literal
    // Prepare to loop!
    QString rule(originalRule);
    // Force a termination at the end of the string to trigger a split
    // Don't check for ";" splits as they may be escaped
    if (!rule.endsWith("\n")) {
        rule.append("\n");
    }
    // Result
    QString result = {};
    // Current character
    QChar curChar = {};
    // Current string
    QString curString = {};
    // Max length
    int sourceLength = rule.length();
    // Consecutive "\" characters
    int consecutiveSlashes = 0;
    // We know it's going to be the same length or smaller, so reserve the same size as the string
    result.reserve(sourceLength);
    // For every character...
    for (int i = 0; i < sourceLength; i++) {
        // Get the character
        curChar = rule.at(i);
        // Check if it's on the list of special list characters, converting to Unicode for use
        // in the switch statement
        //
        // See https://doc.qt.io/qt-5/qchar.html#unicode
        switch (curChar.unicode()) {
        case ';':
            // Separator found
            switch (consecutiveSlashes) {
            case 0:
            case 2:
                // ";"   -> Split
                // ...or...
                // "\\;" -> Split (keep as "\\")
                // Not escaped separator, split into a new item
                // Apply the additional "\\" if needed
                if (consecutiveSlashes == 2) {
                    // "\\;" -> Split (keep as "\\")
                    curString.append(R"(\\)");
                }
                // Remove any whitespace, e.g. "item1; item2" -> " item2" -> "item2"
                curString = curString.trimmed();
                // Skip empty items
                if (!curString.isEmpty()) {
                    // Add to list with the same separator used
                    result.append(curString + "; ");
                }
                // Reset the current list item
                curString.clear();
                break;
            case 1:
                // "\;" -> Keep as "\;"
                curString.append(R"(\;)");
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring" << curChar << "character!";
                break;
            }
            consecutiveSlashes = 0;
            break;
        case '\\':
            // Split escape
            // Increase consecutive slash count
            consecutiveSlashes++;
            // Check if we've reached "\\\"...
            if (consecutiveSlashes == 3) {
                // "\\\" -> Keep as "\\" + "\"
                curString.append(R"(\\)");
                // Set consecutive slashes to 1, recognizing the trailing "\"
                consecutiveSlashes = 1;
            }
            else if (consecutiveSlashes > 3) {
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring" << curChar << "character!";
                break;
            }
            break;
        case '\n':
            // Newline found
            // Preserve the characters as they are now
            // "[\n]"   -> Split
            // "\[\n]"  -> Split (keep as "\")
            // "\\[\n]" -> Split (keep as "\\")
            switch (consecutiveSlashes) {
            case 0:
                // Keep string as is
                break;
            case 1:
            case 2:
                // Apply the additional "\" or "\\"
                curString.append(QString(R"(\)").repeated(consecutiveSlashes));
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), applying newline split anyways!";
                break;
            }
            // Remove any whitespace, e.g. "item1; item2" -> " item2" -> "item2"
            curString = curString.trimmed();
            // Skip empty items
            if (!curString.isEmpty()) {
                // Add to list with the same separator used
                result.append(curString + "\n");
            }
            // Reset the current list item
            curString.clear();
            consecutiveSlashes = 0;
            break;
        default:
            // Preserve the characters as they are now
            switch (consecutiveSlashes) {
            case 0:
                // "..."   -> Keep as "..."
                curString.append(curChar);
                break;
            case 1:
            case 2:
                // "\..."  -> Keep as "\..."
                // "\\..." -> Keep as "\\..."
                curString.append(QString("\\").repeated(consecutiveSlashes) + curChar);
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring " << curChar << "char escape!";
                break;
            }
            consecutiveSlashes = 0;
            break;
        }
    }
    // Remove any trailing separators
    if (result.endsWith("; ")) {
        result.chop(2);
    }
    // Remove any trailing whitespace
    return result.trimmed();
}
void ExpressionMatch::cacheRegEx()
{
    _matchRegExActive = false;
    _matchInvertRegExActive = false;
    _sourceExpressionEmpty = _sourceExpression.isEmpty();
    if (_sourceExpressionEmpty) {
        // No need to calculate anything for empty strings
        return;
    }
    // Convert the given expression to a regular expression based on the mode
    switch (_sourceMode) {
    case MatchMode::MatchPhrase:
        // Match entire phrase, noninverted
        // Don't trim whitespace for phrase matching as someone might want to match on " word ", a
        // more-specific request than "word".
        _matchRegEx = regExFactory("(?:^|\\W)" + regExEscape(_sourceExpression) + "(?:\\W|$)",
                                   _sourceCaseSensitive);
        _matchRegExActive = true;
        break;
    case MatchMode::MatchMultiPhrase:
        // Match multiple entire phrases, noninverted
        // Convert from multiple-phrase rules
        _matchRegEx = regExFactory(convertFromMultiPhrase(_sourceExpression), _sourceCaseSensitive);
        _matchRegExActive = true;
        break;
    case MatchMode::MatchWildcard:
        // Match as wildcard expression
        // Convert from wildcard rules for a single wildcard
        if (_sourceExpression.startsWith("!")) {
            // Inverted rule: take the remainder of the string
            // "^" + invertComponents.at(0) + "$"
            _matchInvertRegEx = regExFactory("^" + wildcardToRegEx(_sourceExpression.mid(1)) + "$",
                                             _sourceCaseSensitive);
            _matchInvertRegExActive = true;
        }
        else {
            // Normal rule: take the whole string
            // Account for any escaped "!" (i.e. "\!") by skipping past the "\", but don't skip past
            // escaped "\" (i.e. "\\!")
            _matchRegEx =
                    regExFactory("^" + wildcardToRegEx(_sourceExpression.startsWith("\\!")
                                                       ? _sourceExpression.mid(1)
                                                       : _sourceExpression) + "$",
                                 _sourceCaseSensitive);
            _matchRegExActive = true;
        }
        break;
    case MatchMode::MatchMultiWildcard:
        // Match as multiple wildcard expressions
        // Convert from wildcard rules for multiple wildcards
        // (The generator function handles setting matchRegEx/matchInvertRegEx)
        generateFromMultiWildcard(_sourceExpression, _sourceCaseSensitive);
        break;
    case MatchMode::MatchRegEx:
        // Match as regular expression
        if (_sourceExpression.startsWith("!")) {
            // Inverted rule: take the remainder of the string
            _matchInvertRegEx = regExFactory(_sourceExpression.mid(1), _sourceCaseSensitive);
            _matchInvertRegExActive = true;
        }
        else {
            // Normal rule: take the whole string
            // Account for any escaped "!" (i.e. "\!") by skipping past the "\", but don't skip past
            // escaped "\" (i.e. "\\!")
            _matchRegEx =
                    regExFactory(_sourceExpression.startsWith("\\!") ? _sourceExpression.mid(1)
                                                                     : _sourceExpression,
                                 _sourceCaseSensitive);
            _matchRegExActive = true;
        }
        break;
    default:
        // This should never happen if you keep the above consistent
        qWarning() << Q_FUNC_INFO << "Unknown MatchMode" << (int)_sourceMode << "!";
        break;
    }
    if (!_sourceExpressionEmpty && !isValid()) {
        // This can happen with invalid regex, so make it a bit more user-friendly.  Set it to Info
        // level as ideally someone's not just going to leave a broken match rule around.  For
        // MatchRegEx, they probably need to fix their regex rule.  For the other modes, there's
        // probably a bug in the parsing routines (which should also be fixed).
        quInfo() << "Could not parse expression match rule"
                 << _sourceExpression << "(match mode:" << (int)_sourceMode
                 << "), this rule will be ignored";
    }
}
#if QT_VERSION >= 0x050000
QRegularExpression ExpressionMatch::regExFactory(const QString ®ExString,
                                                 bool caseSensitive)
#else
QRegExp ExpressionMatch::regExFactory(const QString ®ExString, bool caseSensitive)
#endif
{
    // Construct the regular expression object, setting case sensitivity as appropriate
#if QT_VERSION >= 0x050000
    QRegularExpression newRegEx =
            QRegularExpression(regExString, caseSensitive ?
                                   QRegularExpression::PatternOption::NoPatternOption
                                 : QRegularExpression::PatternOption::CaseInsensitiveOption );
#else
    QRegExp newRegEx = QRegExp(regExString, caseSensitive ?
                                   Qt::CaseSensitivity::CaseSensitive
                                 : Qt::CaseSensitivity::CaseInsensitive);
#endif
    // Check if rule is valid
    if (!newRegEx.isValid()) {
        // This can happen with invalid regex, so make it a bit more user-friendly.  Keep this
        // distinct from the main info-level message for easier debugging in case a regex component
        // in Wildcard or Phrase mode breaks.
        qDebug() << "Internal regular expression component" << regExString
                 << "is invalid and will be ignored";
    }
    // Qt 5.4 (QT_VERSION >= 0x050400) offers explicit control over when QRegularExpression objects
    // get optimized.  By default, patterns are only optimized after some number of uses as defined
    // within Qt internals.
    //
    // In the context of ExpressionMatch, some regular expressions might go unused, e.g. a highlight
    // rule might never match a channel pattern, resulting in the contents pattern being untouched.
    // It should be safe to let Qt handle optimization, taking a non-deterministic, one-off
    // performance penalty on optimization for the sake of saving memory usage on patterns that
    // don't get used.
    //
    // If profiling shows expressions are generally used and/or the automatic optimization
    // interferes incurs too high of a penalty (unlikely given we've created regular expression
    // objects willy-nilly before now), this can be revisited to explicitly call...
    //
    // else {
    //     // Optimize regex now
    // #if QT_VERSION >= 0x050400
    //     newRegEx.optimize();
    // #endif
    //
    // }
    //
    // NOTE: This should only be called if the expression is valid!  Apply within an "else" of the
    // inverted isValid() check above.
    //
    // See https://doc.qt.io/qt-5/qregularexpression.html#optimize
    return newRegEx;
}
QString ExpressionMatch::regExEscape(const QString &phrase)
{
    // Escape the given phrase of any special regular expression characters
#if QT_VERSION >= 0x050000
    return QRegularExpression::escape(phrase);
#else
    return QRegExp::escape(phrase);
#endif
}
QString ExpressionMatch::convertFromMultiPhrase(const QString &originalRule)
{
    // Convert the multi-phrase rule into regular expression format
    // Split apart the original rule into components
    // Use QStringList instead of std::vector<QString> to make use of Qt's built-in .join() method
    QStringList components = {};
    // Split on "\n"
    for (auto &&component : originalRule.split("\n", QString::SkipEmptyParts)) {
        // Don't trim whitespace to maintain consistency with single phrase matching
        // As trimming is not performed, empty components will already be skipped.  This means " "
        // is considered a valid matching phrase.
        // Take the whole string, escaping any regex
        components.append(regExEscape(component));
    }
    // Create full regular expression by...
    // > Enclosing within a non-capturing group to avoid overhead of text extraction, "(?:...)"
    // > Flattening normal and inverted rules using the regex OR character "...|..."
    //
    // Before: [foo, bar, baz]
    // After:  (?:^|\W)(?:foo|bar|baz)(?:\W|$)
    if (components.count() == 1) {
        // Single item, skip the noncapturing group
        return "(?:^|\\W)" + components.at(0) + "(?:\\W|$)";
    }
    else {
        return "(?:^|\\W)(?:" + components.join("|") + ")(?:\\W|$)";
    }
}
void ExpressionMatch::generateFromMultiWildcard(const QString &originalRule, bool caseSensitive)
{
    // Convert the wildcard rule into regular expression format
    // First, reset the existing match expressions
    _matchRegEx = {};
    _matchInvertRegEx = {};
    _matchRegExActive = false;
    _matchInvertRegExActive = false;
    // This gets handled in three steps:
    //
    // 1.  Break apart ";"-separated list into components
    // 2.  Convert components from wildcard format into regular expression format
    // 3.  Combine normal/invert components into normal/invert regular expressions
    //
    // Let's start by making the list...
    // Convert a ";"-separated list into an actual list, splitting on newlines and unescaping
    // escaped characters
    // Escaped list rules (where "[\n]" represents newline):
    // ---------------
    // Token  | Outcome
    // -------|--------
    // ;      | Split
    // \;     | Replace with ";"
    // \\;    | Split (keep as "\\")
    // !      | At start: mark as inverted
    // \!     | At start: replace with "!"
    // \\!    | At start: keep as "\\!" (replaced with "\!" in wildcard conversion)
    // !      | Elsewhere: keep as "!"
    // \!     | Elsewhere: keep as "\!"
    // \\!    | Elsewhere: keep as "\\!"
    // \\\    | Keep as "\\" + "\", set consecutive slashes to 1
    // [\n]   | Split
    // \[\n]  | Split (keep as "\")
    // \\[\n] | Split (keep as "\\")
    // ...    | Keep as "..."
    // \...   | Keep as "\..."
    // \\...  | Keep as "\\..."
    //
    // Strings are forced to end with "\n", always applying "\..." and "\\..." rules
    // "..." also includes another "\" character
    //
    // All whitespace is trimmed from each component
    // "\\" and "\" are not downconverted to allow for other escape codes to be detected in
    // ExpressionMatch::wildcardToRegex
    // Example:
    //
    // > Wildcard rule
    // norm;!invert; norm-space ; !invert-space ;;!;\!norm-escaped;\\!slash-invert;\\\\double;
    // escape\;sep;slash-end-split\\;quad\\\\!noninvert;newline-split[\n]newline-split-slash\\[\n]
    // slash-at-end\\               [line does not continue]
    //
    // (Newlines are encoded as "[\n]".  Ignore linebreaks for the sake of comment wrapping.)
    //
    //
    // > Normal components without wildcard conversion
    //   norm
    //   norm-space
    //   !norm-escaped
    //   \\!slash-invert
    //   \\\\double
    //   escape;sep
    //   slash-end-split\\          [line does not continue]
    //   quad\\\\!noninvert
    //   newline-split
    //   newline-split-slash\\      [line does not continue]
    //   slash-at-end\\             [line does not continue]
    //
    // > Inverted components without wildcard conversion
    //   invert
    //   invert-space
    //
    //
    // > Normal components with wildcard conversion
    //   norm
    //   norm\-space
    //   \!norm\-escaped
    //   \\\!slash\-invert
    //   \\\\double
    //   escape\;sep
    //   slash\-end\-split\\        [line does not continue]
    //   quad\\\\\!noninvert
    //   newline\-split
    //   newline\-split\-slash\\    [line does not continue]
    //   slash\-at\-end\\           [line does not continue]
    //
    // > Inverted components with wildcard conversion
    //   invert
    //   invert\-space
    //
    //
    // > Normal wildcard-converted regex
    // ^(?:norm|norm\-space|\!norm\-escaped|\\\!slash\-invert|\\\\double|escape\;sep|
    // slash\-end\-split\\|quad\\\\\!noninvert|newline\-split|newline\-split\-slash\\|
    // slash\-at\-end\\)$
    //
    // > Inverted wildcard-converted regex
    // ^(?:invert|invert\-space)$
    // Note: R"(\\)" results in the literal of "\\", two backslash characters.  Anything inside the
    // brackets is treated as a literal.  Outside the brackets but inside the quotes is still
    // escaped.
    //
    // See https://en.cppreference.com/w/cpp/language/string_literal
    // Prepare to loop!
    QString rule(originalRule);
    // Force a termination at the end of the string to trigger a split
    // Don't check for ";" splits as they may be escaped
    if (!rule.endsWith("\n")) {
        rule.append("\n");
    }
    // Result, sorted into normal and inverted rules
    // Use QStringList instead of std::vector<QString> to make use of Qt's built-in .join() method
    QStringList normalComponents = {}, invertComponents = {};
    // Current character
    QChar curChar = {};
    // Current string
    QString curString = {};
    // Max length
    int sourceLength = rule.length();
    // Consecutive "\" characters
    int consecutiveSlashes = 0;
    // Whether or not this marks an inverted rule
    bool isInverted = false;
    // Whether or not we're at the beginning of the rule (for detecting "!" and "\!")
    bool isRuleStart = true;
    // We know it's going to have ";"-count items or less, so reserve ";"-count items for both.
    // Without parsing it's not easily possible to tell which are escaped or not, and among the
    // non-escaped entries, which are inverted or not.  These get destroyed once out of scope of
    // this function, so balancing towards performance over memory usage should be okay, hopefully.
    int separatorCount = rule.count(";");
    normalComponents.reserve(separatorCount);
    invertComponents.reserve(separatorCount);
    // For every character...
    for (int i = 0; i < sourceLength; i++) {
        // Get the character
        curChar = rule.at(i);
        // Check if it's on the list of special list characters, converting to Unicode for use
        // in the switch statement
        //
        // See https://doc.qt.io/qt-5/qchar.html#unicode
        switch (curChar.unicode()) {
        case ';':
            // Separator found
            switch (consecutiveSlashes) {
            case 0:
            case 2:
                // ";"   -> Split
                // ...or...
                // "\\;" -> Split (keep as "\\")
                // Not escaped separator, split into a new item
                // Apply the additional "\\" if needed
                if (consecutiveSlashes == 2) {
                    // "\\;" -> Split (keep as "\\")
                    curString.append(R"(\\)");
                }
                // Remove any whitespace, e.g. "item1; item2" -> " item2" -> "item2"
                curString = curString.trimmed();
                // Skip empty items
                if (!curString.isEmpty()) {
                    // Add to inverted/normal list
                    if (isInverted) {
                        invertComponents.append(wildcardToRegEx(curString));
                    }
                    else {
                        normalComponents.append(wildcardToRegEx(curString));
                    }
                }
                // Reset the current list item
                curString.clear();
                isInverted = false;
                isRuleStart = true;
                break;
            case 1:
                // "\;" -> Replace with ";"
                curString.append(";");
                isRuleStart = false;
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring" << curChar << "character!";
                isRuleStart = false;
                break;
            }
            consecutiveSlashes = 0;
            break;
        case '!':
            // Rule inverter found
            if (isRuleStart) {
                // Apply the inverting logic
                switch (consecutiveSlashes) {
                case 0:
                    // "!"   -> At start: mark as inverted
                    isInverted = true;
                    // Don't include the "!" character
                    break;
                case 1:
                    // "\!"  -> At start: replace with "!"
                    curString.append("!");
                    break;
                case 2:
                    // "\\!" -> At start: keep as "\\!" (replaced with "\!" in wildcard conversion)
                    curString.append(R"(\\!)");
                    break;
                default:
                    // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                    qWarning() << Q_FUNC_INFO << "Wildcard rule"
                             << rule << "resulted in rule component"
                             << curString << "with unexpected count of consecutive '\\' ("
                             << consecutiveSlashes << "), ignoring" << curChar << "character!";
                    break;
                }
            }
            else {
                // Preserve the characters as they are now
                switch (consecutiveSlashes) {
                case 0:
                    // "!"    -> Elsewhere: keep as "!"
                    curString.append("!");
                    break;
                case 1:
                case 2:
                     // "\!"  -> Elsewhere: keep as "\!"
                     // "\\!" -> Elsewhere: keep as "\\!"
                    curString.append(QString(R"(\)").repeated(consecutiveSlashes) + "!");
                    break;
                default:
                    // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                    qWarning() << Q_FUNC_INFO << "Wildcard rule"
                               << rule << "resulted in rule component"
                               << curString << "with unexpected count of consecutive '\\' ("
                               << consecutiveSlashes << "), ignoring" << curChar << "character!";
                    break;
                }
            }
            isRuleStart = false;
            consecutiveSlashes = 0;
            break;
        case '\\':
            // Split escape
            // Increase consecutive slash count
            consecutiveSlashes++;
            // Check if we've reached "\\\"...
            if (consecutiveSlashes == 3) {
                // "\\\" -> Keep as "\\" + "\"
                curString.append(R"(\\)");
                // No longer at the rule start
                isRuleStart = false;
                // Set consecutive slashes to 1, recognizing the trailing "\"
                consecutiveSlashes = 1;
            }
            else if (consecutiveSlashes > 3) {
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring" << curChar << "character!";
                break;
            }
            // Don't set "isRuleStart" here as "\" is used in escape sequences
            break;
        case '\n':
            // Newline found
            // Preserve the characters as they are now
            // "[\n]"   -> Split
            // "\[\n]"  -> Split (keep as "\")
            // "\\[\n]" -> Split (keep as "\\")
            switch (consecutiveSlashes) {
            case 0:
                // Keep string as is
                break;
            case 1:
            case 2:
                // Apply the additional "\" or "\\"
                curString.append(QString(R"(\)").repeated(consecutiveSlashes));
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), applying newline split anyways!";
                break;
            }
            // Remove any whitespace, e.g. "item1; item2" -> " item2" -> "item2"
            curString = curString.trimmed();
            // Skip empty items
            if (!curString.isEmpty()) {
                // Add to inverted/normal list
                if (isInverted) {
                    invertComponents.append(wildcardToRegEx(curString));
                }
                else {
                    normalComponents.append(wildcardToRegEx(curString));
                }
            }
            // Reset the current list item
            curString.clear();
            isInverted = false;
            isRuleStart = true;
            consecutiveSlashes = 0;
            break;
        default:
            // Preserve the characters as they are now
            switch (consecutiveSlashes) {
            case 0:
                // "..."   -> Keep as "..."
                curString.append(curChar);
                break;
            case 1:
            case 2:
                // "\..."  -> Keep as "\..."
                // "\\..." -> Keep as "\\..."
                curString.append(QString("\\").repeated(consecutiveSlashes) + curChar);
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << rule << "resulted in rule component"
                           << curString << "with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring " << curChar << "char escape!";
                break;
            }
            // Don't mark as past rule start for whitespace (whitespace gets trimmed)
            if (!curChar.isSpace()) {
                isRuleStart = false;
            }
            consecutiveSlashes = 0;
            break;
        }
    }
    // Clean up any duplicates
    normalComponents.removeDuplicates();
    invertComponents.removeDuplicates();
    // Create full regular expressions by...
    // > Anchoring to start and end of string to mimic QRegExp's .exactMatch() handling, "^...$"
    // > Enclosing within a non-capturing group to avoid overhead of text extraction, "(?:...)"
    // > Flattening normal and inverted rules using the regex OR character "...|..."
    //
    // Before: [foo, bar, baz]
    // After:  ^(?:foo|bar|baz)$
    //
    // See https://doc.qt.io/qt-5/qregularexpression.html#porting-from-qregexp-exactmatch
    // And https://regex101.com/
    // Any empty/invalid regex are handled within ExpressionMatch::match()
    if (!normalComponents.isEmpty()) {
        // Create normal match regex
        if (normalComponents.count() == 1) {
            // Single item, skip the noncapturing group
            _matchRegEx = regExFactory("^" + normalComponents.at(0) + "$", caseSensitive);
        }
        else {
            _matchRegEx = regExFactory("^(?:" + normalComponents.join("|") + ")$", caseSensitive);
        }
        _matchRegExActive = true;
    }
    if (!invertComponents.isEmpty()) {
        // Create invert match regex
        if (invertComponents.count() == 1) {
            // Single item, skip the noncapturing group
            _matchInvertRegEx = regExFactory("^" + invertComponents.at(0) + "$", caseSensitive);
        }
        else {
            _matchInvertRegEx =
                    regExFactory("^(?:" + invertComponents.join("|") + ")$", caseSensitive);
        }
        _matchInvertRegExActive = true;
    }
}
QString ExpressionMatch::wildcardToRegEx(const QString &expression)
{
    // Convert the wildcard expression into regular expression format
    // We're taking a little bit different of a route...
    //
    // Original QRegExp::Wildcard rules:
    // --------------------------
    // Wildcard | Regex | Outcome
    // ---------|-------|--------
    // *        | .*    | zero or more of any character
    // ?        | .     | any single character
    //
    // NOTE 1: This is QRegExp::Wildcard, not QRegExp::WildcardUnix
    //
    // NOTE 2: We are ignoring the "[...]" character-class matching functionality of
    // QRegExp::Wildcard as that feature's a bit more complex and can be handled with full-featured
    // regexes.
    //
    // See https://doc.qt.io/qt-5/qregexp.html#wildcard-matching
    //
    // Quassel originally did not use QRegExp::WildcardUnix, which prevented escaping "*" and "?" in
    // messages.  Unfortunately, spam messages might decide to use both, so offering a way to escape
    // makes sense.
    //
    // On the flip-side, that means to match "\" requires escaping as "\\", breaking backwards
    // compatibility.
    //
    // Quassel's Wildcard rules
    // ------------------------------------------
    // Wildcard | Regex escaped | Regex | Outcome
    // ---------|---------------|-------|--------
    // *        | \*            | .*    | zero or more of any character
    // ?        | \?            | .     | any single character
    // \*       | \\\*          | \*    | literal "*"
    // \?       | \\\?          | \?    | literal "?"
    // \[...]   | \\[...]       | [...] | invalid escape, ignore it
    // \\       | \\\\          | \\    | literal "\"
    //
    // In essence, "*" and "?" need changed only if not escaped, "\\" collapses into "\", "\" gets
    // ignored; other characters escape normally.
    //
    // Example:
    //
    // > Wildcard rule
    // never?gonna*give\*you\?up\\test|y\yeah\\1\\\\2\\\1inval
    //
    // ("\\\\" represents "\\", "\\" represents "\", and "\\\" is valid+invalid, "\")
    //
    // > Regex escaped wildcard rule
    // never\?gonna\*give\\\*you\\\?up\\\\test\|y\\yeah\\\\1\\\\\\\\2\\\\\\1inval
    //
    // > Expected correct regex
    // never.gonna.*give\*you\?up\\test\|yyeah\\1\\\\2\\1inval
    //
    // > Undoing regex escaping of "\" as "\\" (i.e. simple replace, with special escapes intact)
    // never.gonna.*give\*you\?up\test\|yyeah\1\\2\1inval
    // Escape string according to regex
    QString regExEscaped(regExEscape(expression));
    // Fix up the result
    //
    // NOTE: In theory, regular expression lookbehind could solve this.  Unfortunately, QRegExp does
    // not support lookbehind, and it's theoretically inefficient, anyways.  Just use an approach
    // similar to that taken by QRegExp's official wildcard mode.
    //
    // Lookbehind example (that we can't use):
    // (?<!abc)test    Negative lookbehind - don't match if "test" is proceeded by "abc"
    //
    // See https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qregexp.cpp
    //
    // NOTE: We don't copy QRegExp's mode as QRegularExpression has more special characters.  We
    // can't use the same escaping code, hence calling the appropriate QReg[...]::escape() above.
    // Prepare to loop!
    // Result
    QString result = {};
    // Current character
    QChar curChar = {};
    // Max length
    int sourceLength = regExEscaped.length();
    // Consecutive "\" characters
    int consecutiveSlashes = 0;
    // We know it's going to be the same length or smaller, so reserve the same size as the string
    result.reserve(sourceLength);
    // For every character...
    for (int i = 0; i < sourceLength; i++) {
        // Get the character
        curChar = regExEscaped.at(i);
        // Check if it's on the list of special wildcard characters, converting to Unicode for use
        // in the switch statement
        //
        // See https://doc.qt.io/qt-5/qchar.html#unicode
        switch (curChar.unicode()) {
        case '?':
            // Wildcard "?"
            switch (consecutiveSlashes) {
            case 1:
                // "?" -> "\?" -> "."
                // Convert from regex escaped "?" to regular expression
                result.append(".");
                break;
            case 3:
                // "\?" -> "\\\?" -> "\?"
                // Convert from regex escaped "\?" to literal string
                result.append(R"(\?)");
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << expression << "resulted in escaped regular expression string"
                           << regExEscaped << " with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring" << curChar << "character!";
                break;
            }
            consecutiveSlashes = 0;
            break;
        case '*':
            // Wildcard "*"
            switch (consecutiveSlashes) {
            case 1:
                // "*" -> "\*" -> ".*"
                // Convert from regex escaped "*" to regular expression
                result.append(".*");
                break;
            case 3:
                // "\*" -> "\\\*" -> "\*"
                // Convert from regex escaped "\*" to literal string
                result.append(R"(\*)");
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << expression << "resulted in escaped regular expression string"
                           << regExEscaped << " with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring" << curChar << "character!";
                break;
            }
            consecutiveSlashes = 0;
            break;
        case '\\':
            // Wildcard escape
            // Increase consecutive slash count
            consecutiveSlashes++;
            // Check if we've hit an escape sequence
            if (consecutiveSlashes == 4) {
                // "\\" -> "\\\\" -> "\\"
                // Convert from regex escaped "\\" to literal string
                result.append(R"(\\)");
                // Reset slash count
                consecutiveSlashes = 0;
            }
            break;
        default:
            // Any other character
            switch (consecutiveSlashes) {
            case 0:
            case 2:
                // "[...]"  -> "[...]"   -> "[...]"
                // ...or...
                // "\[...]" -> "\\[...]" -> "[...]"
                // Either just print the character itself, or convert from regex-escaped invalid
                // wildcard escape sequence to the character itself
                //
                // Both mean doing nothing, the actual character [...] gets appended below
                break;
            case 1:
                // "[...]" -> "\[...]" -> "\"
                // Keep regex-escaped special character "[...]" as literal string
                // (Where "[...]" represents any non-wildcard regex special character)
                result.append(R"(\)");
                // The actual character [...] gets appended below
                break;
            default:
                // This shouldn't ever happen (even with invalid wildcard rules), log a warning
                qWarning() << Q_FUNC_INFO << "Wildcard rule"
                           << expression << "resulted in escaped regular expression string"
                           << regExEscaped << " with unexpected count of consecutive '\\' ("
                           << consecutiveSlashes << "), ignoring" << curChar << "char escape!";
                break;
            }
            consecutiveSlashes = 0;
            // Add the character itself
            result.append(curChar);
            break;
        }
    }
    // Anchoring to simulate QRegExp::exactMatch() is handled in
    // ExpressionMatch::convertFromWildcard()
    return result;
}
 |