~ubuntu-branches/ubuntu/trusty/quassel/trusty-proposed

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
/***************************************************************************
 *   Copyright (C) 2005-2014 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 "ignorelistmanager.h"

#include <QtCore>
#include <QDebug>
#include <QStringList>
#include <QRegExp>

INIT_SYNCABLE_OBJECT(IgnoreListManager)
IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other)
{
    if (this == &other)
        return *this;

    SyncableObject::operator=(other);
    _ignoreList = other._ignoreList;
    return *this;
}


int IgnoreListManager::indexOf(const QString &ignore) const
{
    for (int i = 0; i < _ignoreList.count(); i++) {
        if (_ignoreList[i].ignoreRule == ignore)
            return i;
    }
    return -1;
}


QVariantMap IgnoreListManager::initIgnoreList() const
{
    QVariantMap ignoreListMap;
    QVariantList ignoreTypeList;
    QStringList ignoreRuleList;
    QStringList scopeRuleList;
    QVariantList isRegExList;
    QVariantList scopeList;
    QVariantList strictnessList;
    QVariantList isActiveList;

    for (int i = 0; i < _ignoreList.count(); i++) {
        ignoreTypeList << _ignoreList[i].type;
        ignoreRuleList << _ignoreList[i].ignoreRule;
        scopeRuleList << _ignoreList[i].scopeRule;
        isRegExList << _ignoreList[i].isRegEx;
        scopeList << _ignoreList[i].scope;
        strictnessList << _ignoreList[i].strictness;
        isActiveList << _ignoreList[i].isActive;
    }

    ignoreListMap["ignoreType"] = ignoreTypeList;
    ignoreListMap["ignoreRule"] = ignoreRuleList;
    ignoreListMap["scopeRule"] = scopeRuleList;
    ignoreListMap["isRegEx"] = isRegExList;
    ignoreListMap["scope"] = scopeList;
    ignoreListMap["strictness"] = strictnessList;
    ignoreListMap["isActive"] = isActiveList;
    return ignoreListMap;
}


void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList)
{
    QVariantList ignoreType = ignoreList["ignoreType"].toList();
    QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
    QStringList scopeRule = ignoreList["scopeRule"].toStringList();
    QVariantList isRegEx = ignoreList["isRegEx"].toList();
    QVariantList scope = ignoreList["scope"].toList();
    QVariantList strictness = ignoreList["strictness"].toList();
    QVariantList isActive = ignoreList["isActive"].toList();

    int count = ignoreRule.count();
    if (count != scopeRule.count() || count != isRegEx.count() ||
        count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
        qWarning() << "Corrupted IgnoreList settings! (Count missmatch)";
        return;
    }

    _ignoreList.clear();
    for (int i = 0; i < ignoreRule.count(); i++) {
        _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
            static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
            scopeRule[i], isActive[i].toBool());
    }
}


/* since overloaded methods aren't syncable (yet?) we can't use that anymore
void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
  addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
}
*/
void IgnoreListManager::addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
    int scope, const QString &scopeRule, bool isActive)
{
    if (contains(ignoreRule)) {
        return;
    }

    IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
        static_cast<ScopeType>(scope), scopeRule, isActive);
    _ignoreList << newItem;

    SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
}


IgnoreListManager::StrictnessType IgnoreListManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName)
{
    // We method don't rely on a proper Message object to make this method more versatile.
    // This allows us to use it in the core with unprocessed Messages or in the Client
    // with properly preprocessed Messages.
    if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
        return UnmatchedStrictness;

    foreach(IgnoreListItem item, _ignoreList) {
        if (!item.isActive || item.type == CtcpIgnore)
            continue;
        if (item.scope == GlobalScope
            || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))
            || (item.scope == ChannelScope && scopeMatch(item.scopeRule, bufferName))) {
            QString str;
            if (item.type == MessageIgnore)
                str = msgContents;
            else
                str = msgSender;

            QRegExp ruleRx = QRegExp(item.ignoreRule);
            ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
            if (!item.isRegEx) {
                ruleRx.setPatternSyntax(QRegExp::Wildcard);
            }

//      qDebug() << "IgnoreListManager::match: ";
//      qDebug() << "string: " << str;
//      qDebug() << "pattern: " << ruleRx.pattern();
//      qDebug() << "scopeRule: " << item.scopeRule;
//      qDebug() << "now testing";
            if ((!item.isRegEx && ruleRx.exactMatch(str)) ||
                (item.isRegEx && ruleRx.indexIn(str) != -1)) {
//        qDebug() << "MATCHED!";
                return item.strictness;
            }
        }
    }
    return UnmatchedStrictness;
}


bool IgnoreListManager::scopeMatch(const QString &scopeRule, const QString &string) const
{
    foreach(QString rule, scopeRule.split(";")) {
        QRegExp ruleRx = QRegExp(rule.trimmed());
        ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
        ruleRx.setPatternSyntax(QRegExp::Wildcard);
        if (ruleRx.exactMatch(string)) {
            return true;
        }
    }
    return false;
}


void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule)
{
    removeAt(indexOf(ignoreRule));
    SYNC(ARG(ignoreRule))
}


void IgnoreListManager::toggleIgnoreRule(const QString &ignoreRule)
{
    int idx = indexOf(ignoreRule);
    if (idx == -1)
        return;
    _ignoreList[idx].isActive = !_ignoreList[idx].isActive;
    SYNC(ARG(ignoreRule))
}


bool IgnoreListManager::ctcpMatch(const QString sender, const QString &network, const QString &type)
{
    foreach(IgnoreListItem item, _ignoreList) {
        if (!item.isActive)
            continue;
        if (item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))) {
            QString sender_;
            QStringList types = item.ignoreRule.split(QRegExp("\\s+"), QString::SkipEmptyParts);

            sender_ = types.takeAt(0);

            QRegExp ruleRx = QRegExp(sender_);
            ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
            if (!item.isRegEx)
                ruleRx.setPatternSyntax(QRegExp::Wildcard);
            if ((!item.isRegEx && ruleRx.exactMatch(sender)) ||
                (item.isRegEx && ruleRx.indexIn(sender) != -1)) {
                if (types.isEmpty() || types.contains(type, Qt::CaseInsensitive))
                    return true;
            }
        }
    }
    return false;
}