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 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other) {
33 SyncableObject::operator=(other);
34 _ignoreList = other._ignoreList;
38 int IgnoreListManager::indexOf(const QString &ignore) const {
39 for(int i = 0; i < _ignoreList.count(); i++) {
40 if(_ignoreList[i].ignoreRule == ignore)
46 QVariantMap IgnoreListManager::initIgnoreList() const {
47 QVariantMap ignoreListMap;
48 QVariantList ignoreTypeList;
49 QStringList ignoreRuleList;
50 QStringList scopeRuleList;
51 QVariantList isRegExList;
52 QVariantList scopeList;
53 QVariantList strictnessList;
54 QVariantList isActiveList;
56 for(int i = 0; i < _ignoreList.count(); i++) {
57 ignoreTypeList << _ignoreList[i].type;
58 ignoreRuleList << _ignoreList[i].ignoreRule;
59 scopeRuleList << _ignoreList[i].scopeRule;
60 isRegExList << _ignoreList[i].isRegEx;
61 scopeList << _ignoreList[i].scope;
62 strictnessList << _ignoreList[i].strictness;
63 isActiveList << _ignoreList[i].isActive;
66 ignoreListMap["ignoreType"] = ignoreTypeList;
67 ignoreListMap["ignoreRule"] = ignoreRuleList;
68 ignoreListMap["scopeRule"] = scopeRuleList;
69 ignoreListMap["isRegEx"] = isRegExList;
70 ignoreListMap["scope"] = scopeList;
71 ignoreListMap["strictness"] = strictnessList;
72 ignoreListMap["isActive"] = isActiveList;
76 void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList) {
77 QVariantList ignoreType = ignoreList["ignoreType"].toList();
78 QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
79 QStringList scopeRule = ignoreList["scopeRule"].toStringList();
80 QVariantList isRegEx = ignoreList["isRegEx"].toList();
81 QVariantList scope = ignoreList["scope"].toList();
82 QVariantList strictness = ignoreList["strictness"].toList();
83 QVariantList isActive = ignoreList["isActive"].toList();
85 int count = ignoreRule.count();
86 if(count != scopeRule.count() || count != isRegEx.count() ||
87 count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
88 qWarning() << "Corrupted IgnoreList settings! (Count missmatch)";
93 for(int i = 0; i < ignoreRule.count(); i++) {
94 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
95 static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
96 scopeRule[i], isActive[i].toBool());
100 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
101 addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
104 void IgnoreListManager::addIgnoreListItem(IgnoreType type, const QString &ignoreRule, bool isRegEx, StrictnessType strictness,
105 ScopeType scope, const QString &scopeRule, bool isActive) {
106 if(contains(ignoreRule)) {
110 _ignoreList << IgnoreListItem(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
112 emit ignoreAdded(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
115 IgnoreListManager::StrictnessType IgnoreListManager::match(const Message &msg, const QString &network) {
116 if(!(msg.type() & (Message::Plain | Message::Notice | Message::Action)))
117 return UnmatchedStrictness;
119 foreach(IgnoreListItem item, _ignoreList) {
122 if(item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network)) ||
123 (item.scope == ChannelScope && scopeMatch(item.scopeRule, msg.bufferInfo().bufferName()))) {
126 if(item.type == MessageIgnore)
127 str = msg.contents();
131 QRegExp ruleRx = QRegExp(item.ignoreRule);
132 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
134 ruleRx.setPatternSyntax(QRegExp::Wildcard);
137 // qDebug() << "IgnoreListManager::match: ";
138 // qDebug() << "string: " << str;
139 // qDebug() << "pattern: " << ruleRx.pattern();
140 // qDebug() << "scopeRule: " << item.scopeRule;
141 // qDebug() << "now testing";
142 if((!item.isRegEx && ruleRx.exactMatch(str)) ||
143 (item.isRegEx && ruleRx.indexIn(str) != -1)) {
144 // qDebug() << "MATCHED!";
145 return item.strictness;
149 return UnmatchedStrictness;
152 bool IgnoreListManager::scopeMatch(const QString &scopeRule, const QString &string) {
153 foreach(QString rule, scopeRule.split(";")) {
154 QRegExp ruleRx = QRegExp(rule.trimmed());
155 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
156 ruleRx.setPatternSyntax(QRegExp::Wildcard);
157 if(ruleRx.exactMatch(string)) {