From e2be03e254a956024c9d67b19b8a809c9692b6f1 Mon Sep 17 00:00:00 2001 From: Manjeet Dahiya Date: Tue, 20 Oct 2009 11:43:45 +0000 Subject: Using QXmlStreamWriter for directly writing to the socket. This will avoid string concatenations and problems with XML escaping rules. and Fix for Issue 19: XMPP Version < 1.0 send NonSASL Auth query --- source/QXmppBind.cpp | 16 +++++----- source/QXmppBind.h | 2 +- source/QXmppInformationRequestResult.cpp | 26 +++++++--------- source/QXmppInformationRequestResult.h | 2 +- source/QXmppIq.cpp | 31 ++++++++----------- source/QXmppIq.h | 5 +-- source/QXmppMessage.cpp | 38 +++++++++-------------- source/QXmppMessage.h | 3 +- source/QXmppNonSASLAuth.cpp | 29 +++++++++--------- source/QXmppNonSASLAuth.h | 4 +-- source/QXmppPacket.h | 3 +- source/QXmppPresence.cpp | 33 +++++++++----------- source/QXmppPresence.h | 2 +- source/QXmppRosterIq.cpp | 38 +++++++++-------------- source/QXmppRosterIq.h | 4 +-- source/QXmppSession.cpp | 15 +++------ source/QXmppSession.h | 2 +- source/QXmppStanza.cpp | 35 +++++++++------------ source/QXmppStanza.h | 4 ++- source/QXmppStream.cpp | 52 +++++++++++++++++++++++--------- source/QXmppStream.h | 2 ++ source/QXmppUtils.cpp | 23 ++++++-------- source/QXmppUtils.h | 10 +++--- source/QXmppVCard.cpp | 26 ++++++---------- source/QXmppVCard.h | 2 +- 25 files changed, 190 insertions(+), 217 deletions(-) (limited to 'source') diff --git a/source/QXmppBind.cpp b/source/QXmppBind.cpp index f7282151..1d37e025 100644 --- a/source/QXmppBind.cpp +++ b/source/QXmppBind.cpp @@ -27,6 +27,7 @@ #include "QXmppConstants.h" #include +#include QXmppBind::QXmppBind(QXmppIq::Type type) : QXmppIq(type) @@ -61,18 +62,15 @@ void QXmppBind::setResource(const QString& str) m_resource = str; } -QByteArray QXmppBind::toXmlElementFromChild() const +void QXmppBind::toXmlElementFromChild(QXmlStreamWriter *writer) const { QString data; QTextStream stream(&data); - stream << ""; - helperToXmlAddElement(stream, "jid", getJid()); - helperToXmlAddElement(stream, "resource", getResource()); - stream << ""; - - return data.toAscii(); + writer->writeStartElement("bind"); + helperToXmlAddAttribute(writer, "xmlns", ns_bind); + helperToXmlAddTextElement(writer, "jid", getJid() ); + helperToXmlAddTextElement(writer, "resource", getResource()); + writer->writeEndElement(); } diff --git a/source/QXmppBind.h b/source/QXmppBind.h index 2a15b48e..46baf91f 100644 --- a/source/QXmppBind.h +++ b/source/QXmppBind.h @@ -42,7 +42,7 @@ public: private: QString m_jid; QString m_resource; - QByteArray toXmlElementFromChild() const; + void toXmlElementFromChild(QXmlStreamWriter *writer) const; }; #endif // QXMPPBIND_H diff --git a/source/QXmppInformationRequestResult.cpp b/source/QXmppInformationRequestResult.cpp index 86236ff6..058612f1 100644 --- a/source/QXmppInformationRequestResult.cpp +++ b/source/QXmppInformationRequestResult.cpp @@ -1,24 +1,20 @@ #include "QXmppInformationRequestResult.h" #include "QXmppConstants.h" +#include QXmppInformationRequestResult::QXmppInformationRequestResult() : QXmppIq(QXmppIq::Result) { } -QByteArray QXmppInformationRequestResult::toXmlElementFromChild() const +void QXmppInformationRequestResult::toXmlElementFromChild(QXmlStreamWriter *writer) const { - QByteArray resultXml; - - resultXml += ""; - resultXml += ""; - resultXml += ""; - resultXml += ""; - - return resultXml; + writer->writeStartElement("query"); + writer->writeAttribute("xmlns", ns_disco_info ); + writer->writeStartElement("feature"); + writer->writeAttribute("var", ns_disco_info ); + writer->writeEndElement(); + writer->writeStartElement("feature"); + writer->writeAttribute("var", ns_ibb ); + writer->writeEndElement(); + writer->writeEndElement(); } diff --git a/source/QXmppInformationRequestResult.h b/source/QXmppInformationRequestResult.h index c3fa20a1..44f128ae 100644 --- a/source/QXmppInformationRequestResult.h +++ b/source/QXmppInformationRequestResult.h @@ -7,7 +7,7 @@ class QXmppInformationRequestResult : public QXmppIq { public: QXmppInformationRequestResult(); - virtual QByteArray toXmlElementFromChild() const; + virtual void toXmlElementFromChild(QXmlStreamWriter *writer) const; }; #endif // QXMPPINFORMATIONREQUESTRESULT_H diff --git a/source/QXmppIq.cpp b/source/QXmppIq.cpp index 68857c0d..3eeef4af 100644 --- a/source/QXmppIq.cpp +++ b/source/QXmppIq.cpp @@ -25,7 +25,7 @@ #include "QXmppUtils.h" #include "QXmppIq.h" -#include +#include QXmppIq::QXmppIq(QXmppIq::Type type) : QXmppStanza(), m_type(type) @@ -55,30 +55,25 @@ void QXmppIq::setType(QXmppIq::Type type) m_type = type; } -QByteArray QXmppIq::toXml() const +void QXmppIq::toXml( QXmlStreamWriter *xmlWriter ) const { - QString data; - QTextStream stream(&data); + xmlWriter->writeStartElement("iq"); - stream << ""; - stream << toXmlElementFromChild(); - stream << getError().toXml(); - stream << ""; - - return data.toAscii(); + helperToXmlAddAttribute(xmlWriter, "type", getTypeStr()); + toXmlElementFromChild(xmlWriter); + getError().toXml(xmlWriter); + xmlWriter->writeEndElement(); } -QByteArray QXmppIq::toXmlElementFromChild() const +void QXmppIq::toXmlElementFromChild( QXmlStreamWriter *writer ) const { - return ""; + Q_UNUSED(writer); } QString QXmppIq::getTypeStr() const diff --git a/source/QXmppIq.h b/source/QXmppIq.h index d79b080a..54903d15 100644 --- a/source/QXmppIq.h +++ b/source/QXmppIq.h @@ -26,6 +26,7 @@ #define QXMPPIQ_H #include "QXmppStanza.h" +class QXmlStreamWriter; class QXmppIq : public QXmppStanza { @@ -47,8 +48,8 @@ public: void setType(QXmppIq::Type); void setTypeFromStr(const QString& str); - QByteArray toXml() const; - virtual QByteArray toXmlElementFromChild() const; + void toXml( QXmlStreamWriter *writer ) const; + virtual void toXmlElementFromChild( QXmlStreamWriter *writer ) const; private: Type m_type; diff --git a/source/QXmppMessage.cpp b/source/QXmppMessage.cpp index 54f599bb..65d5bece 100644 --- a/source/QXmppMessage.cpp +++ b/source/QXmppMessage.cpp @@ -24,7 +24,7 @@ #include "QXmppMessage.h" #include "QXmppUtils.h" -#include +#include QXmppMessage::QXmppMessage(const QString& from, const QString& to, const QString& body, const QString& thread) @@ -108,30 +108,20 @@ void QXmppMessage::setTypeFromStr(const QString& str) } } -QByteArray QXmppMessage::toXml() const +void QXmppMessage::toXml(QXmlStreamWriter *xmlWriter) const { -// yet to take care of escaping xml chars -// and what if there are multiple bodies in diff langs -// also error - - // why not qbytearray getback - // or use bytearray without text stream..using append - QString data; - QTextStream stream(&data); - - stream << ""; - helperToXmlAddElement(stream, "subject", escapeString(getSubject())); - helperToXmlAddElement(stream, "body", escapeString(getBody())); - helperToXmlAddElement(stream, "thread", getThread()); - stream << getError().toXml(); - stream << ""; - return data.toAscii(); + + xmlWriter->writeStartElement("message"); + helperToXmlAddAttribute(xmlWriter, "xml:lang", getLang()); + helperToXmlAddAttribute(xmlWriter, "id", getId()); + helperToXmlAddAttribute(xmlWriter, "to", getTo()); + helperToXmlAddAttribute(xmlWriter, "from", getFrom()); + helperToXmlAddAttribute(xmlWriter, "type", getTypeStr()); + helperToXmlAddTextElement(xmlWriter, "subject", getSubject()); + helperToXmlAddTextElement(xmlWriter,"body", getBody()); + helperToXmlAddTextElement(xmlWriter,"thread", getThread()); + getError().toXml(xmlWriter); + xmlWriter->writeEndElement(); } QString QXmppMessage::getBody() const diff --git a/source/QXmppMessage.h b/source/QXmppMessage.h index ed99eb0e..252d2ba1 100644 --- a/source/QXmppMessage.h +++ b/source/QXmppMessage.h @@ -55,7 +55,8 @@ public: QString getThread() const; void setThread(const QString&); - QByteArray toXml() const; + void toXml(QXmlStreamWriter *writer) const; + private: Type m_type; diff --git a/source/QXmppNonSASLAuth.cpp b/source/QXmppNonSASLAuth.cpp index 883bdc0d..7f9c9791 100644 --- a/source/QXmppNonSASLAuth.cpp +++ b/source/QXmppNonSASLAuth.cpp @@ -1,6 +1,7 @@ #include "QXmppNonSASLAuth.h" #include "QXmppUtils.h" #include +#include QXmppNonSASLAuthTypesRequestIq::QXmppNonSASLAuthTypesRequestIq() : QXmppIq(QXmppIq::Get) { @@ -12,13 +13,12 @@ void QXmppNonSASLAuthTypesRequestIq::setUsername( const QString &username ) m_username = username; } -QByteArray QXmppNonSASLAuthTypesRequestIq::toXmlElementFromChild() const +void QXmppNonSASLAuthTypesRequestIq::toXmlElementFromChild(QXmlStreamWriter *writer) const { - QByteArray resultingXml; - resultingXml += ""; - resultingXml += "" + escapeString(m_username).toUtf8() + ""; - resultingXml += ""; - return resultingXml; + writer->writeStartElement("query"); + writer->writeAttribute( "xmlns","jabber:iq:auth"); + writer->writeTextElement("username", m_username ); + writer->writeEndElement(); } QXmppNonSASLAuthIq::QXmppNonSASLAuthIq() : QXmppIq(QXmppIq::Set), m_useplaintext(false) @@ -26,23 +26,22 @@ QXmppNonSASLAuthIq::QXmppNonSASLAuthIq() : QXmppIq(QXmppIq::Set), m_useplaintext } -QByteArray QXmppNonSASLAuthIq::toXmlElementFromChild() const +void QXmppNonSASLAuthIq::toXmlElementFromChild(QXmlStreamWriter *writer) const { - QByteArray resultingXml; - resultingXml += ""; - resultingXml += "" + escapeString(m_username).toUtf8() + ""; + writer->writeStartElement("query"); + writer->writeAttribute( "xmlns","jabber:iq:auth"); + writer->writeTextElement("username", m_username ); if ( m_useplaintext ) - resultingXml += "" + escapeString(m_password).toUtf8() + ""; + writer->writeTextElement("password", m_password ); else {//SHA1(concat(sid, password)). QByteArray textSid = m_sid.toUtf8(); QByteArray encodedPassword = m_password.toUtf8(); QByteArray digest = QCryptographicHash::hash(textSid + encodedPassword, QCryptographicHash::Sha1 ).toHex(); - resultingXml += "" + digest + ""; + writer->writeTextElement("digest", digest ); } - resultingXml += "" + escapeString(m_resource).toUtf8() + ""; - resultingXml += ""; - return resultingXml; + writer->writeTextElement("resource", m_resource ); + writer->writeEndElement(); } void QXmppNonSASLAuthIq::setUsername( const QString &username ) diff --git a/source/QXmppNonSASLAuth.h b/source/QXmppNonSASLAuth.h index bf071584..6de1063f 100644 --- a/source/QXmppNonSASLAuth.h +++ b/source/QXmppNonSASLAuth.h @@ -8,7 +8,7 @@ class QXmppNonSASLAuthTypesRequestIq : public QXmppIq public: QXmppNonSASLAuthTypesRequestIq(); void setUsername( const QString &username ); - virtual QByteArray toXmlElementFromChild() const; + virtual void toXmlElementFromChild(QXmlStreamWriter *writer) const; private: QString m_username; }; @@ -17,7 +17,7 @@ class QXmppNonSASLAuthIq : public QXmppIq { public: QXmppNonSASLAuthIq(); - virtual QByteArray toXmlElementFromChild() const; + virtual void toXmlElementFromChild(QXmlStreamWriter *writer) const; void setUsername( const QString &username ); void setPassword( const QString &password ); void setResource( const QString &resource ); diff --git a/source/QXmppPacket.h b/source/QXmppPacket.h index bb96c790..95f698fb 100644 --- a/source/QXmppPacket.h +++ b/source/QXmppPacket.h @@ -27,6 +27,7 @@ #include +class QXmlStreamWriter; class QXmppPacket { public: @@ -34,7 +35,7 @@ public: ~QXmppPacket(); QByteArray getXmlReceived() const; - virtual QByteArray toXml() const = 0; + virtual void toXml( QXmlStreamWriter *writer ) const = 0; private: QByteArray m_xmlReceived; diff --git a/source/QXmppPresence.cpp b/source/QXmppPresence.cpp index f6348a51..ea347af6 100644 --- a/source/QXmppPresence.cpp +++ b/source/QXmppPresence.cpp @@ -25,6 +25,7 @@ #include "QXmppPresence.h" #include "QXmppUtils.h" #include +#include QXmppPresence::QXmppPresence(QXmppPresence::Type type, const QXmppPresence::Status& status) @@ -63,29 +64,25 @@ void QXmppPresence::setStatus(const QXmppPresence::Status& status) m_status = status; } -QByteArray QXmppPresence::toXml() const +void QXmppPresence::toXml(QXmlStreamWriter *xmlWriter ) const { - QString data; - - QTextStream stream(&data); - stream << ""; - helperToXmlAddElement(stream, "status", getStatus().getStatusText()); + xmlWriter->writeStartElement("presence"); + helperToXmlAddAttribute(xmlWriter,"xml:lang", getLang()); + helperToXmlAddAttribute(xmlWriter,"id", getId()); + helperToXmlAddAttribute(xmlWriter,"to", getTo()); + helperToXmlAddAttribute(xmlWriter,"from", getFrom()); + helperToXmlAddAttribute(xmlWriter,"type", getTypeStr()); + + helperToXmlAddTextElement(xmlWriter,"status", getStatus().getStatusText()); if(getStatus().getPriority() != 0) - helperToXmlAddElement(stream, "priority", getStatus().getPriority()); - helperToXmlAddElement(stream, "show", getStatus().getTypeStr()); + helperToXmlAddNumberElement(xmlWriter,"priority", getStatus().getPriority()); + helperToXmlAddTextElement(xmlWriter,"show", getStatus().getTypeStr()); - stream << getError().toXml(); + getError().toXml(xmlWriter); - stream << ""; - - return data.toAscii(); + xmlWriter->writeEndElement(); + } QString QXmppPresence::getTypeStr() const diff --git a/source/QXmppPresence.h b/source/QXmppPresence.h index 526fbe20..23c570a7 100644 --- a/source/QXmppPresence.h +++ b/source/QXmppPresence.h @@ -87,7 +87,7 @@ public: QXmppPresence::Status& getStatus(); const QXmppPresence::Status& getStatus() const; void setStatus(const QXmppPresence::Status&); - QByteArray toXml() const; + void toXml( QXmlStreamWriter *writer ) const; private: Type m_type; diff --git a/source/QXmppRosterIq.cpp b/source/QXmppRosterIq.cpp index 44708ded..bcc675ce 100644 --- a/source/QXmppRosterIq.cpp +++ b/source/QXmppRosterIq.cpp @@ -25,7 +25,7 @@ #include "QXmppRosterIq.h" #include "QXmppConstants.h" #include "QXmppUtils.h" -#include +#include QXmppRosterIq::QXmppRosterIq(QXmppIq::Type type) : QXmppIq(type) @@ -53,19 +53,14 @@ QList QXmppRosterIq::getItems() const return m_items; } -QByteArray QXmppRosterIq::toXmlElementFromChild() const +void QXmppRosterIq::toXmlElementFromChild(QXmlStreamWriter *writer) const { - QString data; - QTextStream stream(&data); + writer->writeStartElement("query"); + writer->writeAttribute( "xmlns", ns_roster); - stream << ""; for(int i = 0; i < m_items.count(); ++i) - stream << m_items.at(i).toXml(); - stream << ""; - - return data.toAscii(); + m_items.at(i).toXml(writer); + writer->writeEndElement(); } QXmppRosterIq::Item::SubscriptionType @@ -159,24 +154,19 @@ void QXmppRosterIq::Item::setSubscriptionTypeFromStr(const QString& type) qWarning("QXmppRosterIq::Item::setTypeFromStr(): invalid type"); } -QString QXmppRosterIq::Item::toXml() const +void QXmppRosterIq::Item::toXml(QXmlStreamWriter *writer) const { - QString data; - QTextStream stream(&data); - - stream << ""; + writer->writeStartElement("item"); + helperToXmlAddAttribute(writer,"jid", m_bareJid); + helperToXmlAddAttribute(writer,"name", m_name); + helperToXmlAddAttribute(writer,"subscription", getSubscriptionTypeStr()); + helperToXmlAddAttribute(writer, "ask", getSubscriptionStatus()); QSet::const_iterator i = m_groups.constBegin(); while(i != m_groups.constEnd()) { - helperToXmlAddElement(stream, "group", *i); + helperToXmlAddTextElement(writer,"group", *i); ++i; } - stream << ""; - return data.toAscii(); + writer->writeEndElement(); } diff --git a/source/QXmppRosterIq.h b/source/QXmppRosterIq.h index 06aec148..c750946e 100644 --- a/source/QXmppRosterIq.h +++ b/source/QXmppRosterIq.h @@ -58,7 +58,7 @@ public: void setSubscriptionType(SubscriptionType); QString getSubscriptionTypeStr() const; void setSubscriptionTypeFromStr(const QString&); - QString toXml() const; + void toXml(QXmlStreamWriter *writer) const; private: QString m_bareJid; @@ -75,7 +75,7 @@ public: void addItem(const Item&); QList getItems() const; - QByteArray toXmlElementFromChild() const; + void toXmlElementFromChild(QXmlStreamWriter *writer) const; private: QList m_items; diff --git a/source/QXmppSession.cpp b/source/QXmppSession.cpp index 4ba0380e..abd1dadc 100644 --- a/source/QXmppSession.cpp +++ b/source/QXmppSession.cpp @@ -25,7 +25,7 @@ #include "QXmppSession.h" #include "QXmppConstants.h" #include "QXmppUtils.h" -#include +#include QXmppSession::QXmppSession(QXmppIq::Type type) : QXmppIq(type) @@ -41,15 +41,10 @@ QXmppSession::~QXmppSession() { } -QByteArray QXmppSession::toXmlElementFromChild() const +void QXmppSession::toXmlElementFromChild(QXmlStreamWriter *writer) const { - QString data; - QTextStream stream(&data); - - stream << ""; - - return data.toAscii(); + writer->writeStartElement("session");; + writer->writeAttribute( "xmlns", ns_session); + writer->writeEndElement(); } diff --git a/source/QXmppSession.h b/source/QXmppSession.h index 02098e2f..1845bf6d 100644 --- a/source/QXmppSession.h +++ b/source/QXmppSession.h @@ -35,7 +35,7 @@ public: ~QXmppSession(); private: - QByteArray toXmlElementFromChild() const; + void toXmlElementFromChild( QXmlStreamWriter *writer) const; }; diff --git a/source/QXmppStanza.cpp b/source/QXmppStanza.cpp index 039dafe9..c83bfaa8 100644 --- a/source/QXmppStanza.cpp +++ b/source/QXmppStanza.cpp @@ -26,7 +26,7 @@ #include "QXmppUtils.h" #include "QXmppConstants.h" -#include +#include int QXmppStanza::s_uniqeIdNo = 0; @@ -217,38 +217,33 @@ void QXmppStanza::Error::setConditionFromStr(const QString& type) setCondition(static_cast(-1)); } -QString QXmppStanza::Error::toXml() const +void QXmppStanza::Error::toXml( QXmlStreamWriter *writer ) const { - QString data; QString cond = getConditionStr(); QString type = getTypeStr(); if(cond.isEmpty() && type.isEmpty()) - return data; + return; - QTextStream stream(&data); - - stream << ""; + writer->writeStartElement("error"); + helperToXmlAddAttribute(writer,"type", type); if(!cond.isEmpty()) { - stream << "<" << cond; - helperToXmlAddAttribute(stream, "xmlns", ns_stanza); - stream << "/>"; + writer->writeStartElement(cond); + helperToXmlAddAttribute(writer,"xmlns", ns_stanza); + writer->writeEndElement(); } if(!m_text.isEmpty()) { - stream << ""; - stream << m_text; - stream << ""; + writer->writeStartElement("text"); + helperToXmlAddAttribute(writer,"xml:lang", "en"); + helperToXmlAddAttribute(writer,"xmlns", ns_stanza); + writer->writeCharacters(m_text); + writer->writeEndElement(); } - stream << ""; - return data; + + writer->writeEndElement(); } diff --git a/source/QXmppStanza.h b/source/QXmppStanza.h index e50607c0..58aedb57 100644 --- a/source/QXmppStanza.h +++ b/source/QXmppStanza.h @@ -28,6 +28,8 @@ #include "QXmppPacket.h" #include +class QXmlStreamWriter; + class QXmppStanza : public QXmppPacket { public: @@ -81,7 +83,7 @@ public: QString getText() const; Condition getCondition() const; Type getType() const; - QString toXml() const; + void toXml( QXmlStreamWriter *writer ) const; QString getConditionStr() const; QString getTypeStr() const; diff --git a/source/QXmppStream.cpp b/source/QXmppStream.cpp index c6006aa9..65b4e843 100644 --- a/source/QXmppStream.cpp +++ b/source/QXmppStream.cpp @@ -37,11 +37,13 @@ #include "QXmppVCard.h" #include "QXmppNonSASLAuth.h" #include "QXmppInformationRequestResult.h" +#include "QXmppLogger.h" #include #include #include #include +#include static const QByteArray streamRootElementStart = "\n"; static const QByteArray streamRootElementEnd = ""; @@ -157,6 +159,15 @@ void QXmppStream::socketReadReady() parser(data); } +void QXmppStream::sendNonSASLAuthQuery( const QString &to ) +{ + QXmppNonSASLAuthTypesRequestIq authQuery; + authQuery.setTo(to); + authQuery.setUsername(getConfiguration().getUser()); + + sendPacket(authQuery); +} + void QXmppStream::parser(const QByteArray& data) { QDomDocument doc; @@ -189,6 +200,20 @@ void QXmppStream::parser(const QByteArray& data) QDomElement streamElement = doc.documentElement(); if(m_streamId.isEmpty()) m_streamId = streamElement.attribute("id"); + if(m_XMPPVersion.isEmpty()) + { + m_XMPPVersion = streamElement.attribute("version"); + if(m_XMPPVersion.isEmpty()) + { + // no version specified, signals XMPP Version < 1.0. + // switch to old auth mechanism + sendNonSASLAuthQuery(doc.documentElement().attribute("from")); + } + } + } + else + { + //TODO: Make a login error here. } while(!nodeRecv.isNull()) @@ -214,15 +239,7 @@ void QXmppStream::parser(const QByteArray& data) if((saslAvailable && nonSaslAvailable && !useSasl) || (!saslAvailable && nonSaslAvailable)) { - // Non-SASL Authentication - QDomElement streamElement = doc.documentElement(); - QString to = streamElement.attribute("from"); - - QXmppNonSASLAuthTypesRequestIq authQuery; - authQuery.setTo(to); - authQuery.setUsername(getConfiguration().getUser()); - - sendPacket(authQuery); + sendNonSASLAuthQuery(doc.documentElement().attribute("from")); } else if(saslAvailable) { @@ -483,10 +500,9 @@ void QXmppStream::sendStartStream() sendToServer(data); } -void QXmppStream::sendToServer(const QByteArray& data) +void QXmppStream::sendToServer(const QByteArray& packet) { - log("CLIENT:" + data); - m_socket.write(data); + m_socket.write( packet ); } bool QXmppStream::hasStartStreamElement(const QByteArray& data) @@ -604,7 +620,16 @@ QXmppRoster& QXmppStream::getRoster() void QXmppStream::sendPacket(const QXmppPacket& packet) { - sendToServer(packet.toXml()); + if(QXmppLogger::getLogger()->getLoggingType() != QXmppLogger::NONE) + { + QByteArray logPacket; + QXmlStreamWriter xmlStreamLog(&logPacket); + packet.toXml(&xmlStreamLog); + log("CLIENT: "+ logPacket); + } + + QXmlStreamWriter xmlStream(&m_socket); + packet.toXml(&xmlStream); } void QXmppStream::processPresence(const QXmppPresence& presence) @@ -639,7 +664,6 @@ void QXmppStream::processPresence(const QXmppPresence& presence) void QXmppStream::processMessage(const QXmppMessage& message) { - QString debug = message.toXml(); emit messageReceived(message); } diff --git a/source/QXmppStream.h b/source/QXmppStream.h index 6ba95d5d..5fc8f676 100644 --- a/source/QXmppStream.h +++ b/source/QXmppStream.h @@ -103,6 +103,7 @@ private: QAbstractSocket::SocketError m_socketError; QString m_streamId; QString m_nonSASLAuthId; + QString m_XMPPVersion; // m_xmppStreamError; // m_xmppStanzaError; @@ -115,6 +116,7 @@ private: void sendEndStream(); void sendStartTls(); void sendNonSASLAuth(bool); + void sendNonSASLAuthQuery( const QString &to ); void sendAuthPlain(); void sendBindIQ(); void sendSessionIQ(); diff --git a/source/QXmppUtils.cpp b/source/QXmppUtils.cpp index b34f9902..c4b50fe0 100644 --- a/source/QXmppUtils.cpp +++ b/source/QXmppUtils.cpp @@ -25,7 +25,7 @@ #include "QXmppUtils.h" #include "QXmppLogger.h" #include -#include +#include #include #include #include @@ -41,30 +41,25 @@ QString jidToBareJid(const QString& jid) return jid.left(jid.indexOf(QChar('/'))); } -void helperToXmlAddAttribute(QTextStream& stream, const QString& name, +void helperToXmlAddAttribute(QXmlStreamWriter* stream, const QString& name, const QString& value) { if(!value.isEmpty()) - stream << " " << name <<"='" << value << "'"; + stream->writeAttribute(name,value); } -void helperToXmlAddElement(QTextStream& stream, const QString& name, int value) +void helperToXmlAddNumberElement(QXmlStreamWriter* stream, const QString& name, int value) { - stream << "<" << name << ">" << value << ""; + stream->writeTextElement( name, QString::number(value)); } -void helperToXmlAddElement(QTextStream& stream, const QString& name, +void helperToXmlAddTextElement(QXmlStreamWriter* stream, const QString& name, const QString& value) { if(!value.isEmpty()) - stream << "<" << name << ">" << value << ""; -} - -void helperToXmlAddElement(QTextStream& stream, const QString& name, - const QByteArray& value) -{ - if(!value.isEmpty()) - stream << "<" << name << ">" << value << ""; + stream->writeTextElement( name, value); + else + stream->writeEmptyElement(name); } void log(const QString& str) diff --git a/source/QXmppUtils.h b/source/QXmppUtils.h index cb753003..146057c7 100644 --- a/source/QXmppUtils.h +++ b/source/QXmppUtils.h @@ -26,7 +26,7 @@ #define QXMPPUTILS_H -class QTextStream; +class QXmlStreamWriter; class QByteArray; class QString; class QImage; @@ -34,13 +34,11 @@ class QImage; QString jidToResource(const QString& jid); QString jidToBareJid(const QString& jid); -void helperToXmlAddAttribute(QTextStream& stream, const QString& name, +void helperToXmlAddAttribute(QXmlStreamWriter* stream, const QString& name, const QString& value); -void helperToXmlAddElement(QTextStream& stream, const QString& name, +void helperToXmlAddTextElement(QXmlStreamWriter* stream, const QString& name, const QString& value); -void helperToXmlAddElement(QTextStream& stream, const QString& name, - const QByteArray& value); -void helperToXmlAddElement(QTextStream& stream, const QString& name, +void helperToXmlAddNumberElement(QXmlStreamWriter* stream, const QString& name, int value); void log(const QString& str); diff --git a/source/QXmppVCard.cpp b/source/QXmppVCard.cpp index caf47ef8..8a5f4b0f 100644 --- a/source/QXmppVCard.cpp +++ b/source/QXmppVCard.cpp @@ -26,7 +26,7 @@ #include "QXmppUtils.h" #include "QXmppConstants.h" -#include +#include #include #include @@ -85,27 +85,21 @@ void QXmppVCard::parse(const QDomElement& nodeRecv) setPhoto(QByteArray::fromBase64(base64data)); } -QByteArray QXmppVCard::toXmlElementFromChild() const +void QXmppVCard::toXmlElementFromChild(QXmlStreamWriter *writer) const { - QString data; - QTextStream stream(&data); - - stream << ""; - helperToXmlAddElement(stream, "FN", getFullName()); + writer->writeStartElement("vCard"); + helperToXmlAddAttribute(writer,"xmlns", ns_vcard); + helperToXmlAddTextElement(writer, "FN", getFullName()); if(!getPhoto().isEmpty()) { - stream << ""; - helperToXmlAddElement(stream, "TYPE", getImageType(getPhoto())); - helperToXmlAddElement(stream, "BINVAL", getPhoto().toBase64()); - stream << ""; + writer->writeStartElement("PHOTO"); + helperToXmlAddTextElement(writer, "TYPE", getImageType(getPhoto())); + helperToXmlAddTextElement(writer, "BINVAL", getPhoto().toBase64()); + writer->writeEndElement(); } - stream << ""; - - return data.toAscii(); + writer->writeEndElement(); } QImage QXmppVCard::getPhotoAsImage() const diff --git a/source/QXmppVCard.h b/source/QXmppVCard.h index 9495ccc3..20a26fcb 100644 --- a/source/QXmppVCard.h +++ b/source/QXmppVCard.h @@ -47,7 +47,7 @@ public: void parse(const QDomElement&); private: - QByteArray toXmlElementFromChild() const; + void toXmlElementFromChild(QXmlStreamWriter *writer) const; QString m_fullName; -- cgit v1.2.3