~ubuntu-branches/ubuntu/oneiric/psi/oneiric

« back to all changes in this revision

Viewing changes to src/plugins/generic/antievil/antievil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * (c) 2007-2008 Maciej Niedzielski
 
3
 */
 
4
 
 
5
#include <QObject>
 
6
#include <QTextStream>
 
7
#include <QDebug>
 
8
 
 
9
#include "psiplugin.h"
 
10
#include "stanzafilter.h"
 
11
#include "stanzasender.h"
 
12
#include "stanzasendinghost.h"
 
13
 
 
14
class AntiEvilPlugin: public QObject, public PsiPlugin, public StanzaFilter, public StanzaSender
 
15
{
 
16
        Q_OBJECT;
 
17
        Q_INTERFACES(PsiPlugin StanzaFilter StanzaSender);
 
18
 
 
19
public:
 
20
        AntiEvilPlugin()
 
21
                : stanzaSender(0)
 
22
        {
 
23
        }
 
24
 
 
25
 
 
26
        //-- PsiPlugin -------------------------------------------
 
27
 
 
28
        virtual QString name() const
 
29
        {
 
30
                // this will be displayed
 
31
                return "Machekku's Evil Blocker Plugin";
 
32
        }
 
33
 
 
34
        virtual QString shortName() const
 
35
        {
 
36
                // internal name, no spaces please!
 
37
                return "antievil";
 
38
        }
 
39
 
 
40
        virtual QString version() const
 
41
        {
 
42
                return "0.1";
 
43
        }
 
44
 
 
45
        virtual QWidget* options() const
 
46
        {
 
47
                return 0;
 
48
        }
 
49
 
 
50
        virtual bool enable()
 
51
        {
 
52
                if (stanzaSender) {
 
53
                        enabled = true;
 
54
                }
 
55
                return enabled;
 
56
        }
 
57
 
 
58
        virtual bool disable()
 
59
        {
 
60
                enabled = false;
 
61
                return true;
 
62
        }
 
63
 
 
64
 
 
65
        //-- StanzaFilter ----------------------------------------
 
66
 
 
67
        virtual bool incomingStanza(int account, const QDomElement& stanza)
 
68
        {
 
69
                bool blocked = false;
 
70
 
 
71
                if (enabled) {
 
72
                        for (QDomNode n = stanza.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
73
                                QDomElement i = n.toElement();
 
74
                                if (!i.isNull() && i.tagName() == "evil" && i.attribute("xmlns") == "http://jabber.org/protocol/evil") {
 
75
                                        qDebug("evil blocked! ;)");
 
76
 
 
77
                                        if (stanza.tagName() == "iq") {
 
78
                                                qDebug("sending 'forbidden' error");
 
79
                                                QString sender = stanza.attribute("from");
 
80
                                                QString reply = QString("<iq type='error' %1><error type='modify'><bad-request xmlns='urn:ietf:params:xml:xmpp-stanzas'/></error></iq>")
 
81
                                                        .arg(sender.isEmpty() ? "" : QString("to='%1'").arg(sender));
 
82
 
 
83
                                                stanzaSender->sendStanza(account, reply);
 
84
                                        }
 
85
 
 
86
                                        blocked = true; // stop processing this stanza
 
87
                                        break;
 
88
                                }
 
89
                        }
 
90
                }
 
91
 
 
92
                return blocked;
 
93
        }
 
94
        
 
95
 
 
96
        //-- StanzaSender ----------------------------------------
 
97
        
 
98
        virtual void setStanzaSendingHost(StanzaSendingHost *host)
 
99
        {
 
100
                stanzaSender = host;
 
101
        }
 
102
 
 
103
 
 
104
private:
 
105
        StanzaSendingHost* stanzaSender;
 
106
        bool enabled;
 
107
};
 
108
 
 
109
Q_EXPORT_PLUGIN2(antievil, AntiEvilPlugin)
 
110
 
 
111
#include "antievil.moc"