Fix includes
[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 <QtCore>
24 #include <QDebug>
25 #include <QStringList>
26 #include <QRegExp>
27
28 INIT_SYNCABLE_OBJECT(IgnoreListManager)
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 /* 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);
103 }
104 */
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)) {
108     return;
109   }
110
111   IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
112                                           static_cast<ScopeType>(scope), scopeRule, isActive);
113   _ignoreList << newItem;
114
115   SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
116 }
117
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;
124
125   foreach(IgnoreListItem item, _ignoreList) {
126     if(!item.isActive || item.type == CtcpIgnore)
127       continue;
128     if(item.scope == GlobalScope
129        || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))
130        || (item.scope == ChannelScope && scopeMatch(item.scopeRule, bufferName))) {
131
132       QString str;
133       if(item.type == MessageIgnore)
134         str = msgContents;
135       else
136         str = msgSender;
137
138       QRegExp ruleRx = QRegExp(item.ignoreRule);
139       ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
140       if(!item.isRegEx) {
141         ruleRx.setPatternSyntax(QRegExp::Wildcard);
142       }
143
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;
153       }
154     }
155   }
156   return UnmatchedStrictness;
157 }
158
159
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)) {
166       return true;
167     }
168   }
169   return false;
170 }
171
172 void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule) {
173   removeAt(indexOf(ignoreRule));
174   SYNC(ARG(ignoreRule))
175 }
176
177 void IgnoreListManager::toggleIgnoreRule(const QString &ignoreRule) {
178   int idx = indexOf(ignoreRule);
179   if(idx == -1)
180     return;
181   _ignoreList[idx].isActive = !_ignoreList[idx].isActive;
182   SYNC(ARG(ignoreRule))
183 }
184
185 bool IgnoreListManager::ctcpMatch(const QString sender, const QString &network, const QString &type) {
186   foreach(IgnoreListItem item, _ignoreList) {
187     if(!item.isActive)
188       continue;
189     if(item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))) {
190       QString sender_;
191       QStringList types = item.ignoreRule.split(QRegExp("\\s+"), QString::SkipEmptyParts);
192
193       sender_ = types.takeAt(0);
194
195       QRegExp ruleRx = QRegExp(sender_);
196       ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
197       if(!item.isRegEx)
198         ruleRx.setPatternSyntax(QRegExp::Wildcard);
199       if((!item.isRegEx && ruleRx.exactMatch(sender)) ||
200           (item.isRegEx && ruleRx.indexIn(sender) != -1)) {
201
202         if(types.isEmpty() || types.contains(type, Qt::CaseInsensitive))
203           return true;
204       }
205     }
206   }
207   return false;
208 }