diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2012-03-26 19:12:45 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2012-03-26 19:12:45 +0000 |
| commit | 494ea46f004f493b1158b545c679b8e8d80abdec (patch) | |
| tree | 6936b0edb623a737e16a73fe800fc553dd2bd1fe | |
| parent | 18e9af256bdd9f3c9e90fea540bdfdb8890de278 (diff) | |
| download | qxmpp-494ea46f004f493b1158b545c679b8e8d80abdec.tar.gz | |
remove some plugins, they are too specialised
| -rw-r--r-- | src/server/QXmppServer.cpp | 3 | ||||
| -rw-r--r-- | src/server/mod_ping.cpp | 81 | ||||
| -rw-r--r-- | src/server/mod_ping.h | 42 | ||||
| -rw-r--r-- | src/server/mod_proxy65.cpp | 582 | ||||
| -rw-r--r-- | src/server/mod_proxy65.h | 112 | ||||
| -rw-r--r-- | src/server/mod_stats.cpp | 307 | ||||
| -rw-r--r-- | src/server/mod_stats.h | 68 | ||||
| -rw-r--r-- | src/server/server.pri | 6 |
8 files changed, 0 insertions, 1201 deletions
diff --git a/src/server/QXmppServer.cpp b/src/server/QXmppServer.cpp index c8343d2c..852b6a64 100644 --- a/src/server/QXmppServer.cpp +++ b/src/server/QXmppServer.cpp @@ -45,10 +45,7 @@ // Core plugins Q_IMPORT_PLUGIN(mod_disco) -Q_IMPORT_PLUGIN(mod_ping) Q_IMPORT_PLUGIN(mod_presence) -Q_IMPORT_PLUGIN(mod_proxy65) -Q_IMPORT_PLUGIN(mod_stats) Q_IMPORT_PLUGIN(mod_time) Q_IMPORT_PLUGIN(mod_version) diff --git a/src/server/mod_ping.cpp b/src/server/mod_ping.cpp deleted file mode 100644 index 36622b07..00000000 --- a/src/server/mod_ping.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2008-2011 The QXmpp developers - * - * Author: - * Jeremy Lainé - * - * Source: - * http://code.google.com/p/qxmpp - * - * This file is a part of QXmpp library. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - */ - -#include <QDomElement> - -#include "QXmppConstants.h" -#include "QXmppPingIq.h" -#include "QXmppServer.h" -#include "QXmppServerPlugin.h" -#include "QXmppStream.h" - -#include "mod_ping.h" - -QStringList QXmppServerPing::discoveryFeatures() const -{ - return QStringList() << ns_ping; -} - -bool QXmppServerPing::handleStanza(const QDomElement &element) -{ - if (element.attribute("to") != server()->domain()) - return false; - - // XEP-0199: XMPP Ping - if (element.tagName() == "iq" && QXmppPingIq::isPingIq(element)) - { - QXmppPingIq request; - request.parse(element); - - QXmppIq response(QXmppIq::Result); - response.setId(request.id()); - response.setFrom(request.to()); - response.setTo(request.from()); - server()->sendPacket(response); - return true; - } - - return false; -} - -// PLUGIN - -class QXmppServerPingPlugin : public QXmppServerPlugin -{ -public: - QXmppServerExtension *create(const QString &key) - { - if (key == QLatin1String("ping")) - return new QXmppServerPing; - else - return 0; - }; - - QStringList keys() const - { - return QStringList() << QLatin1String("ping"); - }; -}; - -Q_EXPORT_STATIC_PLUGIN2(mod_ping, QXmppServerPingPlugin) - diff --git a/src/server/mod_ping.h b/src/server/mod_ping.h deleted file mode 100644 index 960c17f6..00000000 --- a/src/server/mod_ping.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2008-2011 The QXmpp developers - * - * Author: - * Jeremy Lainé - * - * Source: - * http://code.google.com/p/qxmpp - * - * This file is a part of QXmpp library. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - */ - -#ifndef QXMPP_SERVER_PING_H -#define QXMPP_SERVER_PING_H - -#include "QXmppServerExtension.h" - -/// \brief QXmppServer extension for XEP-0199: XMPP Ping. -/// - -class QXmppServerPing : public QXmppServerExtension -{ - Q_OBJECT - Q_CLASSINFO("ExtensionName", "ping"); - -public: - QStringList discoveryFeatures() const; - bool handleStanza(const QDomElement &element); -}; - -#endif diff --git a/src/server/mod_proxy65.cpp b/src/server/mod_proxy65.cpp deleted file mode 100644 index 374adc79..00000000 --- a/src/server/mod_proxy65.cpp +++ /dev/null @@ -1,582 +0,0 @@ -/* - * Copyright (C) 2008-2011 The QXmpp developers - * - * Author: - * Jeremy Lainé - * - * Source: - * http://code.google.com/p/qxmpp - * - * This file is a part of QXmpp library. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - */ - -#include <QCoreApplication> -#include <QCryptographicHash> -#include <QDomElement> -#include <QHostInfo> -#include <QSettings> -#include <QTimer> - -#include "QXmppByteStreamIq.h" -#include "QXmppConfiguration.h" -#include "QXmppConstants.h" -#include "QXmppDiscoveryIq.h" -#include "QXmppPingIq.h" -#include "QXmppServer.h" -#include "QXmppServerPlugin.h" -#include "QXmppSocks.h" -#include "QXmppStream.h" -#include "QXmppUtils.h" - -#include "mod_proxy65.h" - -const int blockSize = 16384; - -static QString streamHash(const QString &sid, const QString &initiatorJid, const QString &targetJid) -{ - QCryptographicHash hash(QCryptographicHash::Sha1); - QString str = sid + initiatorJid + targetJid; - hash.addData(str.toAscii()); - return hash.result().toHex(); -} - -QTcpSocketPair::QTcpSocketPair(const QString &hash, QObject *parent) - : QXmppLoggable(parent), - key(hash), - transfer(0), - target(0), - source(0) -{ -} - -bool QTcpSocketPair::activate() -{ - if (!source || !target) { - warning("Both source and target sockets are needed to activate " + key); - return false; - } - time.start(); - connect(target, SIGNAL(bytesWritten(qint64)), this, SLOT(sendData())); - connect(source, SIGNAL(readyRead()), this, SLOT(sendData())); - return true; -} - -void QTcpSocketPair::addSocket(QTcpSocket *socket) -{ - if (source) - { - warning("Unexpected connection for " + key); - socket->deleteLater(); - return; - } - - if (target) - { - debug(QString("Opened source connection for %1 %2:%3").arg( - key, - socket->peerAddress().toString(), - QString::number(socket->peerPort()))); - source = socket; - source->setReadBufferSize(4 * blockSize); - connect(source, SIGNAL(disconnected()), this, SLOT(disconnected())); - } - else - { - debug(QString("Opened target connection for %1 %2:%3").arg( - key, - socket->peerAddress().toString(), - QString::number(socket->peerPort()))); - target = socket; - connect(target, SIGNAL(disconnected()), this, SLOT(disconnected())); - } - socket->setParent(this); -} - -void QTcpSocketPair::disconnected() -{ - QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender()); - if (!socket) - return; - - if (target == socket) - { - debug("Closed target connection for " + key); - emit finished(); - } else if (source == socket) { - debug("Closed source connection for " + key); - if (!target || !target->isOpen()) - emit finished(); - } -} - -void QTcpSocketPair::sendData() -{ - // don't saturate the outgoing socket - if (target->bytesToWrite() >= 2 * blockSize) - return; - - // check for completion - if (!source->isOpen()) - { - if (!target->bytesToWrite()) - target->close(); - return; - } - - char buffer[blockSize]; - qint64 length = source->read(buffer, blockSize); - if (length < 0) - { - if (!target->bytesToWrite()) - target->close(); - return; - } - if (length > 0) - { - target->write(buffer, length); - transfer += length; - } -} - -struct TransferStats -{ - QDateTime date; - qint64 size; - int elapsed; -}; - -class QXmppServerProxy65Private -{ -public: - // configuration - QString advertisedName; - QHostAddress advertisedAddress; - QStringList allowedDomains; - QString jid; - QString hostName; - quint16 port; - - // state - QHash<QString, QTcpSocketPair*> pairs; - QXmppSocksServer *server; - - // statistics - QList<TransferStats> recent; - QTimer *statisticsTimer; - quint64 totalBytes; - quint64 totalTransfers; -}; - -QXmppServerProxy65::QXmppServerProxy65() - : d(new QXmppServerProxy65Private) -{ - bool check; - Q_UNUSED(check); - - d->port = 7777; - d->server = new QXmppSocksServer(this); - - d->statisticsTimer = new QTimer(this); - d->statisticsTimer->setInterval(300 * 1000); - d->totalBytes = 0; - d->totalTransfers = 0; - - check = connect(d->server, SIGNAL(newConnection(QTcpSocket*,QString,quint16)), - this, SLOT(slotSocketConnected(QTcpSocket*,QString,quint16))); - Q_ASSERT(check); - - check = connect(d->statisticsTimer, SIGNAL(timeout()), - this, SLOT(slotUpdateStatistics())); - Q_ASSERT(check); -} - -QXmppServerProxy65::~QXmppServerProxy65() -{ - delete d; -} - -/// Returns the host which is advertised to clients for SOCKS5 connections. -/// -/// You should only care about this if the host which the clients will see is -/// different from \a host(). - -QString QXmppServerProxy65::advertisedHost() const -{ - return d->advertisedName; -} - -/// Sets the host which is advertised to clients for SOCKS5 connections. -/// -/// You should only care about this if the host which the clients will see is -/// different from \a host(). - -void QXmppServerProxy65::setAdvertisedHost(const QString &advertisedHost) -{ - d->advertisedName = advertisedHost; -} - -/// Returns the XMPP domains which are allowed to use the proxy. -/// - -QStringList QXmppServerProxy65::allowedDomains() const -{ - return d->allowedDomains; -} - -/// Sets the XMPP domains which are allowed to use the proxy. -/// -/// If not defined, defaults to the server's domain. -/// -/// \param allowedDomains - -void QXmppServerProxy65::setAllowedDomains(const QStringList &allowedDomains) -{ - d->allowedDomains = allowedDomains; -} - -/// Returns the proxy server's JID. -/// - -QString QXmppServerProxy65::jid() const -{ - return d->jid; -} - -/// Set the proxy server's JID. -/// -/// \param jid - -void QXmppServerProxy65::setJid(const QString &jid) -{ - d->jid = jid; -} - -/// Returns the host on which to listen for SOCKS5 connections. -/// - -QString QXmppServerProxy65::host() const -{ - return d->hostName; -} - -/// Sets the host on which to listen for SOCKS5 connections. -/// -/// If not defined, defaults to the server's domain. -/// -/// \param host - -void QXmppServerProxy65::setHost(const QString &host) -{ - d->hostName = host; -} - -/// Returns the port on which to listen for SOCKS5 connections. -/// - -quint16 QXmppServerProxy65::port() const -{ - return d->port; -} - -/// Sets the port on which to listen for SOCKS5 connections. -/// -/// If not defined, defaults to 7777. -/// -/// \param port - -void QXmppServerProxy65::setPort(quint16 port) -{ - d->port = port; -} - -QStringList QXmppServerProxy65::discoveryItems() const -{ - return QStringList() << d->jid; -} - -bool QXmppServerProxy65::handleStanza(const QDomElement &element) -{ - if (element.attribute("to") != d->jid) - return false; - - if (element.tagName() == "iq" && QXmppDiscoveryIq::isDiscoveryIq(element)) - { - QXmppDiscoveryIq discoIq; - discoIq.parse(element); - - if (discoIq.type() == QXmppIq::Get) - { - QXmppDiscoveryIq responseIq; - responseIq.setTo(discoIq.from()); - responseIq.setFrom(discoIq.to()); - responseIq.setId(discoIq.id()); - responseIq.setType(QXmppIq::Result); - responseIq.setQueryType(discoIq.queryType()); - - if (discoIq.queryType() == QXmppDiscoveryIq::InfoQuery) - { - QStringList features = QStringList() << ns_disco_info << ns_disco_items << ns_bytestreams; - - QList<QXmppDiscoveryIq::Identity> identities; - QXmppDiscoveryIq::Identity identity; - identity.setCategory("proxy"); - identity.setType("bytestreams"); - identity.setName("SOCKS5 Bytestreams"); - identities.append(identity); - - responseIq.setFeatures(features); - responseIq.setIdentities(identities); - } - - server()->sendPacket(responseIq); - return true; - } - } - else if (element.tagName() == "iq" && QXmppByteStreamIq::isByteStreamIq(element)) - { - QXmppByteStreamIq bsIq; - bsIq.parse(element); - - if (bsIq.type() == QXmppIq::Get) - { - QXmppByteStreamIq responseIq; - responseIq.setType(QXmppIq::Result); - responseIq.setTo(bsIq.from()); - responseIq.setFrom(bsIq.to()); - responseIq.setId(bsIq.id()); - - QList<QXmppByteStreamIq::StreamHost> streamHosts; - - QXmppByteStreamIq::StreamHost streamHost; - streamHost.setJid(d->jid); - streamHost.setHost(d->advertisedAddress); - streamHost.setPort(d->port); - streamHosts.append(streamHost); - - responseIq.setStreamHosts(streamHosts); - server()->sendPacket(responseIq); - } - else if (bsIq.type() == QXmppIq::Set) - { - QString hash = streamHash(bsIq.sid(), bsIq.from(), bsIq.activate()); - QTcpSocketPair *pair = d->pairs.value(hash); - - QXmppIq responseIq; - responseIq.setTo(bsIq.from()); - responseIq.setFrom(bsIq.to()); - responseIq.setId(bsIq.id()); - - if (pair && - d->allowedDomains.contains(jidToDomain(bsIq.from()))) - { - if (pair->activate()) { - info(QString("Activated connection %1 by %2").arg(hash, bsIq.from())); - responseIq.setType(QXmppIq::Result); - } else { - warning(QString("Failed to activate connection %1 by %2").arg(hash, bsIq.from())); - responseIq.setType(QXmppIq::Error); - } - } else { - warning(QString("Not activating connection %1 by %2").arg(hash, bsIq.from())); - responseIq.setType(QXmppIq::Error); - } - server()->sendPacket(responseIq); - } - return true; - } - return false; -} - -bool QXmppServerProxy65::start() -{ - // determine allowed domains - if (d->allowedDomains.isEmpty()) - d->allowedDomains << server()->domain(); - - // determine jid - if (d->jid.isEmpty()) - d->jid = "proxy." + server()->domain(); - - // determine bind address - if (d->hostName.isEmpty()) - d->hostName = server()->domain(); - QHostAddress bindAddress; - if (!bindAddress.setAddress(d->hostName)) - { - QHostInfo hostInfo = QHostInfo::fromName(d->hostName); - if (hostInfo.addresses().isEmpty()) - { - warning(QString("Could not lookup host %1").arg(d->hostName)); - return false; - } - bindAddress = hostInfo.addresses().first(); - } - - // determine advertised address - if (d->advertisedName.isEmpty()) { - d->advertisedName = d->hostName; - d->advertisedAddress = bindAddress; - } - else if (!d->advertisedAddress.setAddress(d->advertisedName)) - { - QHostInfo hostInfo = QHostInfo::fromName(d->advertisedName); - if (hostInfo.addresses().isEmpty()) - { - warning(QString("Could not lookup host %1").arg(d->hostName)); - return false; - } - d->advertisedAddress = hostInfo.addresses().first(); - } - - // start listening - if (!d->server->listen(bindAddress, d->port)) - return false; - - // start statistics update - d->statisticsTimer->start(); - return true; -} - -void QXmppServerProxy65::stop() -{ - // refuse incoming connections - d->server->close(); - - // close socket pairs - foreach (QTcpSocketPair *pair, d->pairs) - delete pair; - d->pairs.clear(); - - // stop statistics update - d->statisticsTimer->stop(); -} - -QVariantMap QXmppServerProxy65::statistics() const -{ - // calculate stats - qint64 minimumSpeed = -1; - qint64 maximumSpeed = 0; - qint64 totalSize = 0; - qint64 totalElapsed = 0; - for (int i = d->recent.size() - 1; i >= 0; --i) - { - if (d->recent[i].elapsed > 0) - { - qint64 speed = (1000 * d->recent[i].size) / d->recent[i].elapsed; - if (speed > maximumSpeed) - maximumSpeed = speed; - if (minimumSpeed < 0 || speed < minimumSpeed) - minimumSpeed = speed; - } - totalSize += d->recent[i].size; - totalElapsed += d->recent[i].elapsed; - } - if (minimumSpeed < 0) - minimumSpeed = 0; - qint64 averageSpeed = totalElapsed > 0 ? (1000 * totalSize) / totalElapsed : 0; - - // store stats - QVariantMap stats; - stats["total-bytes"] = d->totalBytes; - stats["total-transfers"] = d->totalTransfers; - stats["hourly-bytes"] = totalSize; - stats["hourly-transfers"] = d->recent.size(); - stats["hourly-average-speed"] = averageSpeed; - stats["hourly-minimum-speed"] = minimumSpeed; - stats["hourly-maximum-speed"] = maximumSpeed; - return stats; -} - -void QXmppServerProxy65::setStatistics(const QVariantMap &statistics) -{ - d->totalBytes = statistics.value("total-bytes").toULongLong(); - d->totalTransfers = statistics.value("total-transfers").toULongLong(); -} - -void QXmppServerProxy65::slotSocketConnected(QTcpSocket *socket, const QString &hostName, quint16 port) -{ - Q_UNUSED(port); - - QTcpSocketPair *pair = d->pairs.value(hostName); - if (!pair) - { - bool check; - Q_UNUSED(check); - - pair = new QTcpSocketPair(hostName, this); - check = connect(pair, SIGNAL(finished()), - this, SLOT(slotPairFinished())); - Q_ASSERT(check); - d->pairs.insert(hostName, pair); - } - pair->addSocket(socket); -} - -void QXmppServerProxy65::slotPairFinished() -{ - QTcpSocketPair *pair = qobject_cast<QTcpSocketPair*>(sender()); - if (!pair) - return; - - info(QString("Data transfered for %1 %2").arg(pair->key, QString::number(pair->transfer))); - - // store information for speed statistics - TransferStats stats; - stats.date = QDateTime::currentDateTime(); - stats.size = pair->transfer; - stats.elapsed = pair->time.elapsed(); - d->recent.prepend(stats); - slotUpdateStatistics(); - - // update totals - d->totalBytes += pair->transfer; - d->totalTransfers++; - - // remove socket pair - d->pairs.remove(pair->key); - pair->deleteLater(); -} - -/// Prune obsolete statistics. -/// - -void QXmppServerProxy65::slotUpdateStatistics() -{ - QDateTime cutoff = QDateTime::currentDateTime().addSecs(-3600); - for (int i = d->recent.size() - 1; i >= 0; --i) - if (d->recent[i].date < cutoff) - d->recent.removeAt(i); -} - -// PLUGIN - -class QXmppServerProxy65Plugin : public QXmppServerPlugin -{ -public: - QXmppServerExtension *create(const QString &key) - { - if (key == QLatin1String("proxy65")) - return new QXmppServerProxy65; - else - return 0; - }; - - QStringList keys() const - { - return QStringList() << QLatin1String("proxy65"); - }; -}; - -Q_EXPORT_STATIC_PLUGIN2(mod_proxy65, QXmppServerProxy65Plugin) - diff --git a/src/server/mod_proxy65.h b/src/server/mod_proxy65.h deleted file mode 100644 index e891963b..00000000 --- a/src/server/mod_proxy65.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (C) 2008-2011 The QXmpp developers - * - * Author: - * Jeremy Lainé - * - * Source: - * http://code.google.com/p/qxmpp - * - * This file is a part of QXmpp library. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - */ - -#ifndef QXMPP_SERVER_PROXY65_H -#define QXMPP_SERVER_PROXY65_H - -#include <QStringList> -#include <QTime> - -#include "QXmppServerExtension.h" - -class QTcpSocket; - -class QTcpSocketPair : public QXmppLoggable -{ - Q_OBJECT - -public: - QTcpSocketPair(const QString &hash, QObject *parent = 0); - - bool activate(); - void addSocket(QTcpSocket *socket); - - QString key; - QTime time; - qint64 transfer; - -signals: - void finished(); - -private slots: - void disconnected(); - void sendData(); - -private: - QTcpSocket *target; - QTcpSocket *source; -}; - -class QXmppServerProxy65Private; - -/// \brief QXmppServer extension for XEP-0065: SOCKS5 Bytestreams. -/// - -class QXmppServerProxy65 : public QXmppServerExtension -{ - Q_OBJECT - Q_CLASSINFO("ExtensionName", "proxy65"); - Q_PROPERTY(QString advertisedHost READ advertisedHost WRITE setAdvertisedHost); - Q_PROPERTY(QStringList allowedDomains READ allowedDomains WRITE setAllowedDomains); - Q_PROPERTY(QString jid READ jid WRITE setJid); - Q_PROPERTY(QString host READ host WRITE setHost); - Q_PROPERTY(quint16 port READ port WRITE setPort); - -public: - QXmppServerProxy65(); - ~QXmppServerProxy65(); - - QString advertisedHost() const; - void setAdvertisedHost(const QString &advertisedHost); - - QStringList allowedDomains() const; - void setAllowedDomains(const QStringList &allowedDomains); - - QString jid() const; - void setJid(const QString &jid); - - QString host() const; - void setHost(const QString &host); - - quint16 port() const; - void setPort(quint16 port); - - /// \cond - QStringList discoveryItems() const; - bool handleStanza(const QDomElement &element); - bool start(); - void stop(); - QVariantMap statistics() const; - void setStatistics(const QVariantMap &statistics); - /// \endcond - -private slots: - void slotPairFinished(); - void slotSocketConnected(QTcpSocket *socket, const QString &hostName, quint16 port); - void slotUpdateStatistics(); - -private: - QXmppServerProxy65Private * const d; -}; - -#endif diff --git a/src/server/mod_stats.cpp b/src/server/mod_stats.cpp deleted file mode 100644 index 4a3fbfce..00000000 --- a/src/server/mod_stats.cpp +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Copyright (C) 2008-2011 The QXmpp developers - * - * Author: - * Jeremy Lainé - * - * Source: - * http://code.google.com/p/qxmpp - * - * This file is a part of QXmpp library. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - */ - -#include <QCoreApplication> -#include <QDomElement> -#include <QSettings> -#include <QTimer> - -#include "QXmppConstants.h" -#include "QXmppDiscoveryIq.h" -#include "QXmppServer.h" -#include "QXmppServerPlugin.h" -#include "QXmppUtils.h" - -#include "mod_stats.h" - -static QXmppServerExtension *findExtension(QXmppServer *server, const QString &name) -{ - foreach (QXmppServerExtension *extension, server->extensions()) - if (extension->extensionName() == name) - return extension; - return 0; -} - -class QXmppServerStatsPrivate -{ -public: - QString jid; - - int incomingClients; - int incomingServers; - int outgoingServers; - - QSettings *statistics; - QString statisticsFile; - QTimer *statisticsTimer; -}; - -/// Read statistics from file. -/// -void QXmppServerStats::readStatistics() -{ - if (!d->statistics) - return; - - foreach (QXmppServerExtension *extension, server()->extensions()) - { - QVariantMap stats; - - // FIXME : remove hard-coded name - const QString group = (extension == this) ? "xmpp-server" : extension->extensionName(); - d->statistics->beginGroup(group); - foreach (const QString &key, d->statistics->childKeys()) - stats[key] = d->statistics->value(key); - d->statistics->endGroup(); - extension->setStatistics(stats); - } -} - -/// Write statistics to file. -/// -void QXmppServerStats::writeStatistics() -{ - if (!d->statistics) - return; - - foreach (QXmppServerExtension *extension, server()->extensions()) - { - const QVariantMap stats = extension->statistics(); - if (stats.isEmpty()) - continue; - - // FIXME : remove hard-coded name - const QString group = (extension == this) ? "xmpp-server" : extension->extensionName(); - d->statistics->beginGroup(group); - foreach (const QString &key, stats.keys()) - d->statistics->setValue(key, stats.value(key)); - d->statistics->endGroup(); - } -} - -QXmppServerStats::QXmppServerStats() - : d(new QXmppServerStatsPrivate) -{ - d->incomingClients = 0; - d->incomingServers = 0; - d->outgoingServers = 0; - d->statistics = 0; - d->statisticsTimer = new QTimer(this); - d->statisticsTimer->setInterval(30 * 1000); - - bool check = connect(d->statisticsTimer, SIGNAL(timeout()), - this, SLOT(writeStatistics())); - Q_ASSERT(check); - Q_UNUSED(check); -} - -QXmppServerStats::~QXmppServerStats() -{ - delete d; -} - -/// Returns the path of the file to which the statistics are written. -/// - -QString QXmppServerStats::file() const -{ - return d->statisticsFile; -} - -/// Sets the path of the file to which the statistics are written. -/// -/// \param file - -void QXmppServerStats::setFile(const QString &file) -{ - if (d->statistics) - { - delete d->statistics; - d->statistics = 0; - } - d->statisticsFile = file; - if (!file.isEmpty()) - { - d->statistics = new QSettings(file, QSettings::IniFormat, this); - readStatistics(); - writeStatistics(); - } -} - -/// Returns the JID from which statistics are served using Service Discovery. -/// - -QString QXmppServerStats::jid() const -{ - return d->jid; -} - -/// Sets the JID from which statistics are served using Service Discovery. -/// -/// \param jid - -void QXmppServerStats::setJid(const QString &jid) -{ - d->jid = jid; -} - -QStringList QXmppServerStats::discoveryItems() const -{ - return QStringList() << d->jid; -} - -QVariantMap QXmppServerStats::statistics() const -{ - return server()->statistics(); -} - -bool QXmppServerStats::handleStanza(const QDomElement &element) -{ - if (element.attribute("to") != d->jid) - return false; - - if (element.tagName() == "iq" && QXmppDiscoveryIq::isDiscoveryIq(element)) - { - QXmppDiscoveryIq discoIq; - discoIq.parse(element); - - if (discoIq.type() == QXmppIq::Get) - { - QXmppDiscoveryIq responseIq; - responseIq.setTo(discoIq.from()); - responseIq.setFrom(discoIq.to()); - responseIq.setId(discoIq.id()); - responseIq.setType(QXmppIq::Result); - responseIq.setQueryType(discoIq.queryType()); - responseIq.setQueryNode(discoIq.queryNode()); - - // check queried node - const QString queryNode = discoIq.queryNode(); - QXmppServerExtension *extension = 0; - QString key; - if (!queryNode.isEmpty()) - { - extension = findExtension(server(), queryNode.split("/").first()); - if (!extension || queryNode.count("/") > 1) - { - responseIq.setType(QXmppIq::Error); - const QXmppStanza::Error error(QXmppStanza::Error::Cancel, - QXmppStanza::Error::ServiceUnavailable); - responseIq.setError(error); - server()->sendPacket(responseIq); - return true; - } - if (queryNode.count("/")) - key = queryNode.split("/").last(); - } - - if (discoIq.queryType() == QXmppDiscoveryIq::InfoQuery) - { - // features - QStringList features = QStringList() << ns_disco_info << ns_disco_items; - responseIq.setFeatures(features); - - // identity - QList<QXmppDiscoveryIq::Identity> identities; - QXmppDiscoveryIq::Identity identity; - identity.setCategory("directory"); - identity.setType("statistics"); - if (!extension) - identity.setName("Server Statistics"); - else if (key.isEmpty()) - identity.setName(QString("%1 module").arg(extension->extensionName())); - else - identity.setName(QString("%1: %2").arg(key, extension->statistics().value(key).toString())); - identities.append(identity); - responseIq.setIdentities(identities); - } else { - QList<QXmppDiscoveryIq::Item> items; - if (!extension) - { - foreach (QXmppServerExtension *extension, server()->extensions()) - { - if (extension->statistics().isEmpty()) - continue; - QXmppDiscoveryIq::Item item; - item.setJid(d->jid); - item.setNode(extension->extensionName()); - items.append(item); - } - } else if (key.isEmpty()) { - QVariantMap stats = extension->statistics(); - foreach (const QString &key, stats.keys()) - { - QXmppDiscoveryIq::Item item; - item.setJid(d->jid); - item.setNode(extension->extensionName() + "/" + key); - items.append(item); - } - } - responseIq.setItems(items); - } - - server()->sendPacket(responseIq); - return true; - } - } - - return false; -} - -bool QXmppServerStats::start() -{ - // determine jid - if (d->jid.isEmpty()) - d->jid = "statistics." + server()->domain(); - - d->statisticsTimer->start(); - - return true; -} - -void QXmppServerStats::stop() -{ - d->statisticsTimer->stop(); -} - -// PLUGIN - -class QXmppServerStatsPlugin : public QXmppServerPlugin -{ -public: - QXmppServerExtension *create(const QString &key) - { - if (key == QLatin1String("stats")) - return new QXmppServerStats; - else - return 0; - }; - - QStringList keys() const - { - return QStringList() << QLatin1String("stats"); - }; -}; - -Q_EXPORT_STATIC_PLUGIN2(mod_stats, QXmppServerStatsPlugin) - diff --git a/src/server/mod_stats.h b/src/server/mod_stats.h deleted file mode 100644 index bc324159..00000000 --- a/src/server/mod_stats.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2008-2011 The QXmpp developers - * - * Author: - * Jeremy Lainé - * - * Source: - * http://code.google.com/p/qxmpp - * - * This file is a part of QXmpp library. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - */ - -#ifndef QXMPP_SERVER_STATS_H -#define QXMPP_SERVER_STATS_H - -#include "QXmppServerExtension.h" - -class QSettings; -class QXmppServerStatsPrivate; - -/// \brief QXmppServer extension for statistics. -/// - -class QXmppServerStats : public QXmppServerExtension -{ - Q_OBJECT - Q_CLASSINFO("ExtensionName", "stats"); - Q_PROPERTY(QString file READ file WRITE setFile); - Q_PROPERTY(QString jid READ jid WRITE setJid); - -public: - QXmppServerStats(); - ~QXmppServerStats(); - - QString file() const; - void setFile(const QString &file); - - QString jid() const; - void setJid(const QString &jid); - - /// \cond - QStringList discoveryItems() const; - bool handleStanza(const QDomElement &element); - QVariantMap statistics() const; - bool start(); - void stop(); - /// \endcond - -private slots: - void writeStatistics(); - -private: - void readStatistics(); - QXmppServerStatsPrivate * const d; -}; - -#endif diff --git a/src/server/server.pri b/src/server/server.pri index 8df3ef5a..db854eb0 100644 --- a/src/server/server.pri +++ b/src/server/server.pri @@ -23,16 +23,10 @@ SOURCES += \ # Plugins HEADERS += \ server/mod_disco.h \ - server/mod_ping.h \ - server/mod_proxy65.h \ - server/mod_stats.h \ server/mod_time.h \ server/mod_version.h SOURCES += \ server/mod_disco.cpp \ - server/mod_ping.cpp \ server/mod_presence.cpp \ - server/mod_proxy65.cpp \ - server/mod_stats.cpp \ server/mod_time.cpp \ server/mod_version.cpp |
