1 /***************************************************************************
2 * Copyright (C) 2005-2013 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "ignorelistmanager.h"
25 #include <QStringList>
28 INIT_SYNCABLE_OBJECT(IgnoreListManager)
29 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other)
34 SyncableObject::operator=(other);
35 _ignoreList = other._ignoreList;
40 int IgnoreListManager::indexOf(const QString &ignore) const
42 for (int i = 0; i < _ignoreList.count(); i++) {
43 if (_ignoreList[i].ignoreRule == ignore)
50 QVariantMap IgnoreListManager::initIgnoreList() const
52 QVariantMap ignoreListMap;
53 QVariantList ignoreTypeList;
54 QStringList ignoreRuleList;
55 QStringList scopeRuleList;
56 QVariantList isRegExList;
57 QVariantList scopeList;
58 QVariantList strictnessList;
59 QVariantList isActiveList;
61 for (int i = 0; i < _ignoreList.count(); i++) {
62 ignoreTypeList << _ignoreList[i].type;
63 ignoreRuleList << _ignoreList[i].ignoreRule;
64 scopeRuleList << _ignoreList[i].scopeRule;
65 isRegExList << _ignoreList[i].isRegEx;
66 scopeList << _ignoreList[i].scope;
67 strictnessList << _ignoreList[i].strictness;
68 isActiveList << _ignoreList[i].isActive;
71 ignoreListMap["ignoreType"] = ignoreTypeList;
72 ignoreListMap["ignoreRule"] = ignoreRuleList;
73 ignoreListMap["scopeRule"] = scopeRuleList;
74 ignoreListMap["isRegEx"] = isRegExList;
75 ignoreListMap["scope"] = scopeList;
76 ignoreListMap["strictness"] = strictnessList;
77 ignoreListMap["isActive"] = isActiveList;
82 void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList)
84 QVariantList ignoreType = ignoreList["ignoreType"].toList();
85 QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
86 QStringList scopeRule = ignoreList["scopeRule"].toStringList();
87 QVariantList isRegEx = ignoreList["isRegEx"].toList();
88 QVariantList scope = ignoreList["scope"].toList();
89 QVariantList strictness = ignoreList["strictness"].toList();
90 QVariantList isActive = ignoreList["isActive"].toList();
92 int count = ignoreRule.count();
93 if (count != scopeRule.count() || count != isRegEx.count() ||
94 count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
95 qWarning() << "Corrupted IgnoreList settings! (Count missmatch)";
100 for (int i = 0; i < ignoreRule.count(); i++) {
101 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
102 static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
103 scopeRule[i], isActive[i].toBool());
108 /* since overloaded methods aren't syncable (yet?) we can't use that anymore
109 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
110 addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
113 void IgnoreListManager::addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
114 int scope, const QString &scopeRule, bool isActive)
116 if (contains(ignoreRule)) {
120 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
121 static_cast<ScopeType>(scope), scopeRule, isActive);
122 _ignoreList << newItem;
124 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
128 IgnoreListManager::StrictnessType IgnoreListManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName)
130 // We method don't rely on a proper Message object to make this method more versatile.
131 // This allows us to use it in the core with unprocessed Messages or in the Client
132 // with properly preprocessed Messages.
133 if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
134 return UnmatchedStrictness;
136 foreach(IgnoreListItem item, _ignoreList) {
137 if (!item.isActive || item.type == CtcpIgnore)
139 if (item.scope == GlobalScope
140 || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))
141 || (item.scope == ChannelScope && scopeMatch(item.scopeRule, bufferName))) {
143 if (item.type == MessageIgnore)
148 QRegExp ruleRx = QRegExp(item.ignoreRule);
149 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
151 ruleRx.setPatternSyntax(QRegExp::Wildcard);
154 // qDebug() << "IgnoreListManager::match: ";
155 // qDebug() << "string: " << str;
156 // qDebug() << "pattern: " << ruleRx.pattern();
157 // qDebug() << "scopeRule: " << item.scopeRule;
158 // qDebug() << "now testing";
159 if ((!item.isRegEx && ruleRx.exactMatch(str)) ||
160 (item.isRegEx && ruleRx.indexIn(str) != -1)) {
161 // qDebug() << "MATCHED!";
162 return item.strictness;
166 return UnmatchedStrictness;
170 bool IgnoreListManager::scopeMatch(const QString &scopeRule, const QString &string) const
172 foreach(QString rule, scopeRule.split(";")) {
173 QRegExp ruleRx = QRegExp(rule.trimmed());
174 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
175 ruleRx.setPatternSyntax(QRegExp::Wildcard);
176 if (ruleRx.exactMatch(string)) {
184 void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule)
186 removeAt(indexOf(ignoreRule));
187 SYNC(ARG(ignoreRule))
191 void IgnoreListManager::toggleIgnoreRule(const QString &ignoreRule)
193 int idx = indexOf(ignoreRule);
196 _ignoreList[idx].isActive = !_ignoreList[idx].isActive;
197 SYNC(ARG(ignoreRule))
201 bool IgnoreListManager::ctcpMatch(const QString sender, const QString &network, const QString &type)
203 foreach(IgnoreListItem item, _ignoreList) {
206 if (item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))) {
208 QStringList types = item.ignoreRule.split(QRegExp("\\s+"), QString::SkipEmptyParts);
210 sender_ = types.takeAt(0);
212 QRegExp ruleRx = QRegExp(sender_);
213 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
215 ruleRx.setPatternSyntax(QRegExp::Wildcard);
216 if ((!item.isRegEx && ruleRx.exactMatch(sender)) ||
217 (item.isRegEx && ruleRx.indexIn(sender) != -1)) {
218 if (types.isEmpty() || types.contains(type, Qt::CaseInsensitive))