File: messagefilter.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (182 lines) | stat: -rw-r--r-- 5,159 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * messagefilter.cpp - plugin main class
 *
 * Copyright (C) 2015  Ivan Romanov <drizt@land.ru>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "messagefilter.h"

#include "options.h"

#include <accountinfoaccessinghost.h>
#include <activetabaccessinghost.h>
#include <iconfactoryaccessinghost.h>
#include <optionaccessinghost.h>
#include <psiaccountcontrollinghost.h>
#include <stanzasendinghost.h>

#include <QCursor>
#include <QDomElement>
#include <QFile>
#include <QMenu>
#include <QMessageBox>
#include <QRegExp>

MessageFilter::MessageFilter() :
    _enabled(false), _optionsForm(nullptr), _accountHost(nullptr), _optionHost(nullptr), _stanzaSending(nullptr),
    _accountInfo(nullptr), _rules(QList<Rule>())
{
}

MessageFilter::~MessageFilter() { }

QWidget *MessageFilter::options()
{
    if (!_enabled) {
        return nullptr;
    }

    loadRules();
    _optionsForm = new Options(_rules);
    _optionsForm->setOptionAccessingHost(_optionHost);
    return qobject_cast<QWidget *>(_optionsForm);
}

bool MessageFilter::enable()
{
    _enabled = true;
    loadRules();
    return true;
}

bool MessageFilter::disable()
{
    _enabled = false;
    return true;
}

void MessageFilter::applyOptions()
{
    _optionsForm->saveSettings();
    loadRules();
}

void MessageFilter::restoreOptions() { }

QString MessageFilter::pluginInfo()
{
    return tr("Can drop incoming stanzas according to various filters like source/destination address or specific "
              "message contents");
}

bool MessageFilter::incomingStanza(int account, const QDomElement &stanza)
{
    Q_UNUSED(account);

    if (!_enabled) {
        return false;
    }

    if (stanza.tagName() != "message") {
        return false;
    }

    QString message   = stanza.firstChildElement("body").text();
    QString from_full = stanza.attribute("from");
    QString from      = from_full.split("/").takeFirst();
    QString to_full   = stanza.attribute("to");
    QString to        = to_full.split("/").takeFirst();

    for (const Rule &rule : _rules) {
        bool match = true;
        for (const Condition &condition : rule.conditions) {
            QString val;
            switch (condition.type) {
            case From:
                val = from;
                break;
            case To:
                val = to;
                break;
            case FromFull:
                val = from_full;
                break;
            case ToFull:
                val = to_full;
                break;
            case Message:
                val = message;
                break;
            }

            switch (condition.comparison) {
            case Equal:
                if (val != condition.text)
                    match = false;
                break;

            case NotEqual:
                if (val == condition.text)
                    match = false;
                break;

            case Contains:
                if (!val.contains(QRegExp(condition.text)))
                    match = false;
                break;

            case NotContains:
                if (val.contains(QRegExp(condition.text)))
                    match = false;
                break;
            }

            if (!match)
                break;
        }

        if (match)
            return !rule.showMessage;
    }

    return false;
}

void MessageFilter::loadRules()
{
    if (!_optionHost || !_enabled)
        return;

    _rules.clear();
    int rulesSize = _optionHost->getPluginOption("rules.size", 0).toInt();
    for (int i = 0; i < rulesSize; ++i) {
        QString optionName = QString("rules.l%1.").arg(i);
        Rule    rule;
        rule.name          = _optionHost->getPluginOption(optionName + "name").toString();
        rule.showMessage   = _optionHost->getPluginOption(optionName + "show-message").toBool();
        int conditionsSize = _optionHost->getPluginOption(optionName + "conditions.size").toInt();
        for (int j = 0; j < conditionsSize; ++j) {
            QString   optionName1 = QString("%1conditions.l%2.").arg(optionName).arg(j);
            Condition condition;
            condition.type = static_cast<ConditionType>(_optionHost->getPluginOption(optionName1 + "type").toInt());
            condition.comparison
                = static_cast<Comparison>(_optionHost->getPluginOption(optionName1 + "comparison").toInt());
            condition.text = _optionHost->getPluginOption(optionName1 + "text").toString();
            rule.conditions << condition;
        }
        _rules << rule;
    }
}