a9fb4c68e7fd656d7efa8d1b2df3e99d20f6bf5c
[quassel.git] / src / common / ignorelistmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "ignorelistmanager.h"
22
23 #include <QDebug>
24 #include <QStringList>
25 #include <QRegExp>
26
27 #include "message.h"
28
29 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other) {
30   if(this == &other)
31     return *this;
32
33   SyncableObject::operator=(other);
34   _ignoreList = other._ignoreList;
35   return *this;
36 }
37
38 int IgnoreListManager::indexOf(const QString &ignore) const {
39   for(int i = 0; i < _ignoreList.count(); i++) {
40     if(_ignoreList[i].ignoreRule == ignore)
41       return i;
42   }
43   return -1;
44 }
45
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;
55
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;
64   }
65
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;
73   return ignoreListMap;
74 }
75
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();
84
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)";
89     return;
90   }
91
92   _ignoreList.clear();
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());
97   }
98 }
99
100 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
101   addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
102 }
103
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)) {
107     return;
108   }
109
110   _ignoreList << IgnoreListItem(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
111
112   emit ignoreAdded(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
113 }
114
115 IgnoreListManager::StrictnessType IgnoreListManager::match(const Message &msg, const QString &network) {
116   if(!(msg.type() & (Message::Plain | Message::Notice | Message::Action)))
117     return UnmatchedStrictness;
118
119   foreach(IgnoreListItem item, _ignoreList) {
120     if(!item.isActive)
121       continue;
122     if(item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network)) ||
123        (item.scope == ChannelScope && scopeMatch(item.scopeRule, msg.bufferInfo().bufferName()))) {
124
125       QString str;
126       if(item.type == MessageIgnore)
127         str = msg.contents();
128       else
129         str = msg.sender();
130
131       QRegExp ruleRx = QRegExp(item.ignoreRule);
132       ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
133       if(!item.isRegEx) {
134         ruleRx.setPatternSyntax(QRegExp::Wildcard);
135       }
136
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;
146       }
147     }
148   }
149   return UnmatchedStrictness;
150 }
151
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)) {
158       return true;
159     }
160   }
161   return false;
162 }