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"
25 #include <QStringList>
28 INIT_SYNCABLE_OBJECT(IgnoreListManager)
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 /* since overloaded methods aren't syncable (yet?) we can't use that anymore
101 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
102 addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
105 void IgnoreListManager::addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
106 int scope, const QString &scopeRule, bool isActive) {
107 if(contains(ignoreRule)) {
111 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
112 static_cast<ScopeType>(scope), scopeRule, isActive);
113 _ignoreList << newItem;
115 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
118 IgnoreListManager::StrictnessType IgnoreListManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName) {
119 // We method don't rely on a proper Message object to make this method more versatile.
120 // This allows us to use it in the core with unprocessed Messages or in the Client
121 // with properly preprocessed Messages.
122 if(!(msgType & (Message::Plain | Message::Notice | Message::Action)))
123 return UnmatchedStrictness;
125 foreach(IgnoreListItem item, _ignoreList) {
126 if(!item.isActive || item.type == CtcpIgnore)
128 if(item.scope == GlobalScope
129 || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))
130 || (item.scope == ChannelScope && scopeMatch(item.scopeRule, bufferName))) {
133 if(item.type == MessageIgnore)
138 QRegExp ruleRx = QRegExp(item.ignoreRule);
139 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
141 ruleRx.setPatternSyntax(QRegExp::Wildcard);
144 // qDebug() << "IgnoreListManager::match: ";
145 // qDebug() << "string: " << str;
146 // qDebug() << "pattern: " << ruleRx.pattern();
147 // qDebug() << "scopeRule: " << item.scopeRule;
148 // qDebug() << "now testing";
149 if((!item.isRegEx && ruleRx.exactMatch(str)) ||
150 (item.isRegEx && ruleRx.indexIn(str) != -1)) {
151 // qDebug() << "MATCHED!";
152 return item.strictness;
156 return UnmatchedStrictness;
160 bool IgnoreListManager::scopeMatch(const QString &scopeRule, const QString &string) const {
161 foreach(QString rule, scopeRule.split(";")) {
162 QRegExp ruleRx = QRegExp(rule.trimmed());
163 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
164 ruleRx.setPatternSyntax(QRegExp::Wildcard);
165 if(ruleRx.exactMatch(string)) {
172 void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule) {
173 removeAt(indexOf(ignoreRule));
174 SYNC(ARG(ignoreRule))
177 void IgnoreListManager::toggleIgnoreRule(const QString &ignoreRule) {
178 int idx = indexOf(ignoreRule);
181 _ignoreList[idx].isActive = !_ignoreList[idx].isActive;
182 SYNC(ARG(ignoreRule))
185 bool IgnoreListManager::ctcpMatch(const QString sender, const QString &network, const QString &type) {
186 foreach(IgnoreListItem item, _ignoreList) {
189 if(item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))) {
191 QStringList types = item.ignoreRule.split(QRegExp("\\s+"), QString::SkipEmptyParts);
193 sender_ = types.takeAt(0);
195 QRegExp ruleRx = QRegExp(sender_);
196 ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
198 ruleRx.setPatternSyntax(QRegExp::Wildcard);
199 if((!item.isRegEx && ruleRx.exactMatch(sender)) ||
200 (item.isRegEx && ruleRx.indexIn(sender) != -1)) {
202 if(types.isEmpty() || types.contains(type, Qt::CaseInsensitive))