1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "ignorelistmanager.h"
24 #include <QStringList>
29 INIT_SYNCABLE_OBJECT(IgnoreListManager)
31 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other) {
35 SyncableObject::operator=(other);
36 _ignoreList = other._ignoreList;
40 int IgnoreListManager::indexOf(const QString &ignore) const {
41 for(int i = 0; i < _ignoreList.count(); i++) {
42 if(_ignoreList[i].ignoreRule == ignore)
48 QVariantMap IgnoreListManager::initIgnoreList() const {
49 QVariantMap ignoreListMap;
50 QVariantList ignoreTypeList;
51 QStringList ignoreRuleList;
52 QStringList scopeRuleList;
53 QVariantList isRegExList;
54 QVariantList scopeList;
55 QVariantList strictnessList;
56 QVariantList isActiveList;
58 for(int i = 0; i < _ignoreList.count(); i++) {
59 ignoreTypeList << _ignoreList[i].type;
60 ignoreRuleList << _ignoreList[i].ignoreRule;
61 scopeRuleList << _ignoreList[i].scopeRule;
62 isRegExList << _ignoreList[i].isRegEx;
63 scopeList << _ignoreList[i].scope;
64 strictnessList << _ignoreList[i].strictness;
65 isActiveList << _ignoreList[i].isActive;
68 ignoreListMap["ignoreType"] = ignoreTypeList;
69 ignoreListMap["ignoreRule"] = ignoreRuleList;
70 ignoreListMap["scopeRule"] = scopeRuleList;
71 ignoreListMap["isRegEx"] = isRegExList;
72 ignoreListMap["scope"] = scopeList;
73 ignoreListMap["strictness"] = strictnessList;
74 ignoreListMap["isActive"] = isActiveList;
78 void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList) {
79 QVariantList ignoreType = ignoreList["ignoreType"].toList();
80 QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
81 QStringList scopeRule = ignoreList["scopeRule"].toStringList();
82 QVariantList isRegEx = ignoreList["isRegEx"].toList();
83 QVariantList scope = ignoreList["scope"].toList();
84 QVariantList strictness = ignoreList["strictness"].toList();
85 QVariantList isActive = ignoreList["isActive"].toList();
87 int count = ignoreRule.count();
88 if(count != scopeRule.count() || count != isRegEx.count() ||
89 count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
90 qWarning() << "Corrupted IgnoreList settings! (Count missmatch)";
95 for(int i = 0; i < ignoreRule.count(); i++) {
96 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
97 static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
98 scopeRule[i], isActive[i].toBool());
102 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
103 addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
106 void IgnoreListManager::addIgnoreListItem(IgnoreType type, const QString &ignoreRule, bool isRegEx, StrictnessType strictness,
107 ScopeType scope, const QString &scopeRule, bool isActive) {
108 if(contains(ignoreRule)) {
112 _ignoreList << IgnoreListItem(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
114 emit ignoreAdded(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
117 IgnoreListManager::StrictnessType IgnoreListManager::match(const Message &msg, const QString &network) {
118 if(!(msg.type() & (Message::Plain | Message::Notice | Message::Action)))
119 return UnmatchedStrictness;
121 foreach(IgnoreListItem item, _ignoreList) {
124 if(item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network)) ||
125 (item.scope == ChannelScope && scopeMatch(item.scopeRule, msg.bufferInfo().bufferName()))) {
128 if(item.type == MessageIgnore)
129 str = msg.contents();
133 QRegExp ruleRx = QRegExp(item.ignoreRule);
134 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
136 ruleRx.setPatternSyntax(QRegExp::Wildcard);
139 // qDebug() << "IgnoreListManager::match: ";
140 // qDebug() << "string: " << str;
141 // qDebug() << "pattern: " << ruleRx.pattern();
142 // qDebug() << "scopeRule: " << item.scopeRule;
143 // qDebug() << "now testing";
144 if((!item.isRegEx && ruleRx.exactMatch(str)) ||
145 (item.isRegEx && ruleRx.indexIn(str) != -1)) {
146 // qDebug() << "MATCHED!";
147 return item.strictness;
151 return UnmatchedStrictness;
154 bool IgnoreListManager::scopeMatch(const QString &scopeRule, const QString &string) {
155 foreach(QString rule, scopeRule.split(";")) {
156 QRegExp ruleRx = QRegExp(rule.trimmed());
157 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
158 ruleRx.setPatternSyntax(QRegExp::Wildcard);
159 if(ruleRx.exactMatch(string)) {