Adapt to SignalProxy changes
[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 INIT_SYNCABLE_OBJECT(IgnoreListManager)
30
31 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other) {
32   if(this == &other)
33     return *this;
34
35   SyncableObject::operator=(other);
36   _ignoreList = other._ignoreList;
37   return *this;
38 }
39
40 int IgnoreListManager::indexOf(const QString &ignore) const {
41   for(int i = 0; i < _ignoreList.count(); i++) {
42     if(_ignoreList[i].ignoreRule == ignore)
43       return i;
44   }
45   return -1;
46 }
47
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;
57
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;
66   }
67
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;
75   return ignoreListMap;
76 }
77
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();
86
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)";
91     return;
92   }
93
94   _ignoreList.clear();
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());
99   }
100 }
101
102 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
103   addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
104 }
105
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)) {
109     return;
110   }
111
112   _ignoreList << IgnoreListItem(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
113
114   emit ignoreAdded(type, ignoreRule, isRegEx, strictness, scope, scopeRule, isActive);
115 }
116
117 IgnoreListManager::StrictnessType IgnoreListManager::match(const Message &msg, const QString &network) {
118   if(!(msg.type() & (Message::Plain | Message::Notice | Message::Action)))
119     return UnmatchedStrictness;
120
121   foreach(IgnoreListItem item, _ignoreList) {
122     if(!item.isActive)
123       continue;
124     if(item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network)) ||
125        (item.scope == ChannelScope && scopeMatch(item.scopeRule, msg.bufferInfo().bufferName()))) {
126
127       QString str;
128       if(item.type == MessageIgnore)
129         str = msg.contents();
130       else
131         str = msg.sender();
132
133       QRegExp ruleRx = QRegExp(item.ignoreRule);
134       ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
135       if(!item.isRegEx) {
136         ruleRx.setPatternSyntax(QRegExp::Wildcard);
137       }
138
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;
148       }
149     }
150   }
151   return UnmatchedStrictness;
152 }
153
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)) {
160       return true;
161     }
162   }
163   return false;
164 }