1 /*
2 * echoplugin.cpp - Psi plugin to echo messages
3 * Copyright (C) 2006 Kevin Smith
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * You can also redistribute and/or modify this program under the
11 * terms of the Psi License, specified in the accompanied COPYING
12 * file, as published by the Psi Project; either dated January 1st,
13 * 2005, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26 #include <QtCore>
27
28 #include "psiplugin.h"
29 #include "eventfilter.h"
30 #include "stanzasender.h"
31 #include "stanzasendinghost.h"
32 #include "optionaccessor.h"
33 #include "optionaccessinghost.h"
34
35 class EchoPlugin : public QObject, public PsiPlugin, public EventFilter, public StanzaSender, public OptionAccessor
36 {
37 Q_OBJECT
38 Q_INTERFACES(PsiPlugin EventFilter StanzaSender OptionAccessor)
39
40 public:
41 EchoPlugin();
42
43 // PsiPlugin
44 virtual QString name() const;
45 virtual QString shortName() const;
46 virtual QString version() const;
47 virtual QWidget* options() const;
48 virtual bool enable();
49 virtual bool disable();
50
51 // EventFilter
52 virtual bool processEvent(int account, const QDomElement& e);
53 virtual bool processMessage(int account, const QString& fromJid, const QString& body, const QString& subject);
54
55 // StanzaSender
56 virtual void setStanzaSendingHost(StanzaSendingHost *host);
57
58 // OptionAccessor
59 virtual void setOptionAccessingHost(OptionAccessingHost* host);
60 virtual void optionChanged(const QString& option);
61
62
63
64 private:
65 bool enabled;
66 OptionAccessingHost* psiOptions;
67 StanzaSendingHost* sender;
68 };
69
70 Q_EXPORT_PLUGIN(EchoPlugin);
71
EchoPlugin()72 EchoPlugin::EchoPlugin()
73 : enabled(false), psiOptions(0), sender(0)
74 {
75 }
76
77 //-- PsiPlugin ------------------------------------------------------
78
name() const79 QString EchoPlugin::name() const
80 {
81 return "Echo Plugin";
82 }
83
shortName() const84 QString EchoPlugin::shortName() const
85 {
86 return "echo";
87 }
88
version() const89 QString EchoPlugin::version() const
90 {
91 return "0.1";
92 }
93
options() const94 QWidget* EchoPlugin::options() const
95 {
96 return 0;
97 }
98
enable()99 bool EchoPlugin::enable()
100 {
101 if (psiOptions && sender) {
102 enabled = true;
103 }
104
105 return enabled;
106 }
107
disable()108 bool EchoPlugin::disable()
109 {
110 enabled = false;
111 return true;
112 }
113
114 //-- EventFilter ----------------------------------------------------
115
processEvent(int account,const QDomElement & e)116 bool EchoPlugin::processEvent(int account, const QDomElement& e)
117 {
118 Q_UNUSED(account);
119 Q_UNUSED(e);
120 return false; // don't stop processing
121 }
122
processMessage(int account,const QString & fromJid,const QString & body,const QString & subject)123 bool EchoPlugin::processMessage(int account, const QString& fromJid, const QString& body, const QString& subject)
124 {
125 if (enabled) {
126 qWarning(qPrintable(QString("Received message '%1', echoing back to %2").arg(body).arg(fromJid)));
127 QVariant option = psiOptions->getGlobalOption(body);
128 QString reply;
129 QString type;
130 if (option.isValid()) {
131 reply = QString("Option %1 = %2").arg(body).arg(option.toString());
132 type = "chat";
133 } else {
134 reply = QString("What you say? %1 ?").arg(body);
135 type = "normal";
136 }
137 QString replySubject = QString("Re: %1").arg(subject);
138
139 sender->sendMessage(account, fromJid, reply, replySubject, type);
140 }
141
142 return false;
143 }
144
145 //-- StanzaSender ---------------------------------------------------
146
setStanzaSendingHost(StanzaSendingHost * host)147 void EchoPlugin::setStanzaSendingHost(StanzaSendingHost *host)
148 {
149 sender = host;
150 }
151
152 //-- OptionAccessor -------------------------------------------------
153
setOptionAccessingHost(OptionAccessingHost * host)154 void EchoPlugin::setOptionAccessingHost(OptionAccessingHost* host)
155 {
156 psiOptions = host;
157 }
158
optionChanged(const QString & option)159 void EchoPlugin::optionChanged(const QString& option)
160 {
161 Q_UNUSED(option);
162 }
163
164
165 #include "echoplugin.moc"
166