Skip to content

Commit

Permalink
Added captcha support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Nigmatullin committed Mar 24, 2012
1 parent adad27d commit 8bf9e59
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/captcha.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/****************************************************************************
**
** Jreen
**
** Copyright © 2012 Ruslan Nigmatullin <euroelessar@yandex.ru>
**
*****************************************************************************
**
** $JREEN_BEGIN_LICENSE$
** 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 http://www.gnu.org/licenses/.
** $JREEN_END_LICENSE$
**
****************************************************************************/

#include "captcha.h"

namespace Jreen {

class CaptchaPrivate
{
public:
DataForm::Ptr form;
};

Captcha::Captcha(DataForm::Ptr form) : d_ptr(new CaptchaPrivate)
{
d_func()->form = form;
}

Captcha::~Captcha()
{
}

DataForm::Ptr Captcha::form() const
{
return d_func()->form;
}

void Captcha::setForm(const DataForm::Ptr &form)
{
d_func()->form = form;
}

} // namespace Jreen
52 changes: 52 additions & 0 deletions src/captcha.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/****************************************************************************
**
** Jreen
**
** Copyright © 2012 Ruslan Nigmatullin <euroelessar@yandex.ru>
**
*****************************************************************************
**
** $JREEN_BEGIN_LICENSE$
** 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 http://www.gnu.org/licenses/.
** $JREEN_END_LICENSE$
**
****************************************************************************/

#ifndef JREEN_CAPTCHA_H
#define JREEN_CAPTCHA_H

#include "dataform.h"

namespace Jreen {

class CaptchaPrivate;

class JREEN_EXPORT Captcha : public Payload
{
J_PAYLOAD(Jreen::Captcha)
Q_DECLARE_PRIVATE(Captcha)
public:
Captcha(DataForm::Ptr form = DataForm::Ptr());
~Captcha();

DataForm::Ptr form() const;
void setForm(const DataForm::Ptr &form);

private:
QScopedPointer<CaptchaPrivate> d_ptr;
};

} // namespace Jreen

#endif // JREEN_CAPTCHA_H
96 changes: 96 additions & 0 deletions src/captchafactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/****************************************************************************
**
** Jreen
**
** Copyright © 2012 Ruslan Nigmatullin <euroelessar@yandex.ru>
**
*****************************************************************************
**
** $JREEN_BEGIN_LICENSE$
** 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 http://www.gnu.org/licenses/.
** $JREEN_END_LICENSE$
**
****************************************************************************/

#include "captchafactory_p.h"

#define NS_CAPTCHA QLatin1String("urn:xmpp:captcha")

namespace Jreen {

CaptchaFactory::CaptchaFactory() : m_depth(0), m_atFactory(0)
{
}

QStringList CaptchaFactory::features() const
{
return QStringList();
}

bool CaptchaFactory::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
{
Q_UNUSED(attributes);
return name == QLatin1String("captcha") && uri == NS_CAPTCHA;
}

void CaptchaFactory::handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
{
++m_depth;

if (m_depth == 1)
m_captcha.reset(new Captcha);
else if (m_depth == 2 && m_factory.canParse(name, uri, attributes))
m_atFactory = true;

if (m_atFactory)
m_factory.handleStartElement(name, uri, attributes);
}

void CaptchaFactory::handleEndElement(const QStringRef &name, const QStringRef &uri)
{
if (m_atFactory) {
m_factory.handleEndElement(name, uri);
if (m_depth == 2) {
DataForm::Ptr form = m_factory.createPayload().staticCast<DataForm>();
if (form->typeName() == NS_CAPTCHA)
m_captcha->setForm(form);
m_atFactory = false;
}
}

--m_depth;
}

void CaptchaFactory::handleCharacterData(const QStringRef &text)
{
if (m_atFactory)
m_factory.handleCharacterData(text);
}

void CaptchaFactory::serialize(Payload *extension, QXmlStreamWriter *writer)
{
Captcha *captcha = payload_cast<Captcha*>(extension);
writer->writeStartElement(QLatin1String("captcha"));
writer->writeDefaultNamespace(NS_CAPTCHA);
if (captcha->form())
m_factory.serialize(captcha->form().data(), writer);
writer->writeEndElement();
}

Payload::Ptr CaptchaFactory::createPayload()
{
return Payload::Ptr(m_captcha.take());
}

} // namespace Jreen
56 changes: 56 additions & 0 deletions src/captchafactory_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/****************************************************************************
**
** Jreen
**
** Copyright © 2012 Ruslan Nigmatullin <euroelessar@yandex.ru>
**
*****************************************************************************
**
** $JREEN_BEGIN_LICENSE$
** 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 http://www.gnu.org/licenses/.
** $JREEN_END_LICENSE$
**
****************************************************************************/

#ifndef JREEN_CAPTCHAFACTORY_P_H
#define JREEN_CAPTCHAFACTORY_P_H

#include "captcha.h"
#include "dataformfactory_p.h"

namespace Jreen {

class JREEN_AUTOTEST_EXPORT CaptchaFactory : public PayloadFactory<Captcha>
{
public:
CaptchaFactory();

QStringList features() const;
bool canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes);
void handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes);
void handleEndElement(const QStringRef &name, const QStringRef &uri);
void handleCharacterData(const QStringRef &text);
void serialize(Payload *extension, QXmlStreamWriter *writer);
Payload::Ptr createPayload();

private:
int m_depth : 31;
int m_atFactory : 1;
DataFormFactory m_factory;
QScopedPointer<Captcha> m_captcha;
};

} // namespace Jreen

#endif // JREEN_CAPTCHAFACTORY_P_H
2 changes: 2 additions & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "errorfactory_p.h"
#include "registrationqueryfactory_p.h"
#include "bitsofbinaryfactory_p.h"
#include "captchafactory_p.h"

// Features
#include "nonsaslauth_p.h"
Expand Down Expand Up @@ -170,6 +171,7 @@ void ClientPrivate::init()
q_ptr->registerPayload(new PrivacyQueryFactory);
q_ptr->registerPayload(new RegistrationQueryFactory);
q_ptr->registerPayload(new BitsOfBinaryFactory);
q_ptr->registerPayload(new CaptchaFactory);
// client->registerPayload(new PubSub::EventFactory);
// client->registerPayload(new PubSub::PublishFacatory);
//client->registerPayload(new PrivateXml::QueryFactory);
Expand Down
5 changes: 3 additions & 2 deletions src/messagefactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ void MessageFactory::serialize(Stanza *stanza, QXmlStreamWriter *writer)
if (message->subtype() == Message::Invalid)
return;

QString subtype = enumToStr(message->subtype(),message_types);
QLatin1String subtype = enumToStr(message->subtype(),message_types);

writer->writeStartElement(QLatin1String("message"));
writeAttributes(stanza, writer);
writer->writeAttribute(QLatin1String("type"),subtype);
if (subtype != QLatin1String(""))
writer->writeAttribute(QLatin1String("type"), subtype);
writeLangMap(QLatin1String("subject"),message->subject(),writer);
writeLangMap(QLatin1String("body"),message->body(),writer);
if(!message->thread().isEmpty())
Expand Down

0 comments on commit 8bf9e59

Please sign in to comment.