<commit_before>/*
 * Copyright (C) 2008-2010 The QXmpp developers
 *
 * Authors:
 *  <NAME>
 *  <NAME>
 *
 * 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 "QXmppClient.h"
#include "QXmppPresence.h"
#include "QXmppRosterIq.h"
#include "QXmppRosterManager.h"
#include "QXmppUtils.h"

/// Constructs a roster manager.

QXmppRosterManager::QXmppRosterManager(QXmppClient* client)
    : m_isRosterReceived(false)
{
    bool check = QObject::connect(client, SIGNAL(connected()),
        this, SLOT(connected()));
    Q_ASSERT(check);

    check = QObject::connect(client, SIGNAL(disconnected()),
        this, SLOT(disconnected()));
    Q_ASSERT(check);

    check = QObject::connect(client, SIGNAL(presenceReceived(const QXmppPresence&)),
        this, SLOT(presenceReceived(const QXmppPresence&)));
    Q_ASSERT(check);
}

/// Upon XMPP connection, request the roster.
///
void QXmppRosterManager::connected()
{
    QXmppRosterIq roster;
    roster.setType(QXmppIq::Get);
    roster.setFrom(client()->configuration().jid());
    m_rosterReqId = roster.id();
    client()->sendPacket(roster);
}

void QXmppRosterManager::disconnected()
{
    m_entries.clear();
    m_presences.clear();
    m_isRosterReceived = false;
}

bool QXmppRosterManager::handleStanza(const QDomElement &element)
{
    if(element.tagName() == "iq" && QXmppRosterIq::isRosterIq(element))
    {
        QXmppRosterIq rosterIq;
        rosterIq.parse(element);

        // Security check: only server should send this iq
        // from() should be either empty or bareJid of the user
        QString fromJid = rosterIq.from();
        if(fromJid.isEmpty() ||
           jidToBareJid(fromJid) == client()->configuration().jidBare())
        {
            rosterIqReceived(rosterIq);
            return true;
        }
    }

    return false;
}

void QXmppRosterManager::presenceReceived(const QXmppPresence& presence)
{
    QString jid = presence.from();
    QString bareJid = jidToBareJid(jid);
    QString resource = jidToResource(jid);

    if (bareJid.isEmpty())
        return;

    switch(presence.type())
    {
    case QXmppPresence::Available:
        m_presences[bareJid][resource] = presence;
        emit presenceChanged(bareJid, resource);
        break;
    case QXmppPresence::Unavailable:
        m_presences[bareJid].remove(resource);
        emit presenceChanged(bareJid, resource);
        break;
    case QXmppPresence::Subscribe:
        if (client()->configuration().autoAcceptSubscriptions())
        {
            QXmppPresence presence;
            presence.setTo(jid);
            presence.setType(QXmppPresence::Subscribed);
            client()->sendPacket(presence);
        }
        break;
    default:
        break;
    }
}

void QXmppRosterManager::rosterIqReceived(const QXmppRosterIq& rosterIq)
{
    bool isInitial = (m_rosterReqId == rosterIq.id());

    switch(rosterIq.type())
    {
    case QXmppIq::Set:
        {
            // send result iq
            QXmppIq returnIq(QXmppIq::Result);
            returnIq.setId(rosterIq.id());
            client()->sendPacket(returnIq);

            // store updated entries and notify changes
            const QList<QXmppRosterIq::Item> items = rosterIq.items();
            for (int i = 0; i < items.count(); i++)
            {
                QString bareJid = items.at(i).bareJid();
                m_entries[bareJid] = items.at(i);
                emit rosterChanged(bareJid);
            }

            // when contact subscribes user...user sends 'subscribed' presence 
            // then after recieving following iq user requests contact for subscription
            
            // check the "from" is newly added in the roster...and remove this ask thing...and do this for all items
            QXmppRosterIq::Item item = items.at(0);
            if (!item.bareJid().isEmpty() &&
                item.subscriptionType() == QXmppRosterIq::Item::From &&
                item.subscriptionStatus().isEmpty())
            {
                QXmppPresence presence;
                presence.setTo(item.bareJid());
                presence.setType(QXmppPresence::Subscribe);
                client()->sendPacket(presence);
            }<commit_msg>* avoid crash when receiving an empty roster push * only create reciprocal subscriptions when the appropriate option is set<commit_after>/*
 * Copyright (C) 2008-2010 The QXmpp developers
 *
 * Authors:
 *  <NAME>
 *  <NAME>
 *
 * 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 "QXmppClient.h"
#include "QXmppPresence.h"
#include "QXmppRosterIq.h"
#include "QXmppRosterManager.h"
#include "QXmppUtils.h"

/// Constructs a roster manager.

QXmppRosterManager::QXmppRosterManager(QXmppClient* client)
    : m_isRosterReceived(false)
{
    bool check = QObject::connect(client, SIGNAL(connected()),
        this, SLOT(connected()));
    Q_ASSERT(check);

    check = QObject::connect(client, SIGNAL(disconnected()),
        this, SLOT(disconnected()));
    Q_ASSERT(check);

    check = QObject::connect(client, SIGNAL(presenceReceived(const QXmppPresence&)),
        this, SLOT(presenceReceived(const QXmppPresence&)));
    Q_ASSERT(check);
}

/// Upon XMPP connection, request the roster.
///
void QXmppRosterManager::connected()
{
    QXmppRosterIq roster;
    roster.setType(QXmppIq::Get);
    roster.setFrom(client()->configuration().jid());
    m_rosterReqId = roster.id();
    client()->sendPacket(roster);
}

void QXmppRosterManager::disconnected()
{
    m_entries.clear();
    m_presences.clear();
    m_isRosterReceived = false;
}

bool QXmppRosterManager::handleStanza(const QDomElement &element)
{
    if(element.tagName() == "iq" && QXmppRosterIq::isRosterIq(element))
    {
        QXmppRosterIq rosterIq;
        rosterIq.parse(element);

        // Security check: only server should send this iq
        // from() should be either empty or bareJid of the user
        QString fromJid = rosterIq.from();
        if(fromJid.isEmpty() ||
           jidToBareJid(fromJid) == client()->configuration().jidBare())
        {
            rosterIqReceived(rosterIq);
            return true;
        }
    }

    return false;
}

void QXmppRosterManager::presenceReceived(const QXmppPresence& presence)
{
    QString jid = presence.from();
    QString bareJid = jidToBareJid(jid);
    QString resource = jidToResource(jid);

    if (bareJid.isEmpty())
        return;

    switch(presence.type())
    {
    case QXmppPresence::Available:
        m_presences[bareJid][resource] = presence;
        emit presenceChanged(bareJid, resource);
        break;
    case QXmppPresence::Unavailable:
        m_presences[bareJid].remove(resource);
        emit presenceChanged(bareJid, resource);
        break;
    case QXmppPresence::Subscribe:
        if (client()->configuration().autoAcceptSubscriptions())
        {
            // accept subscription request
            QXmppPresence presence;
            presence.setTo(jid);
            presence.setType(QXmppPresence::Subscribed);
            client()->sendPacket(presence);

            // ask for reciprocal subscription
            presence.setTo(bareJid);
            presence.setType(QXmppPresence::Subscribe);
            client()->sendPacket(presence);
        }
        break;
    default:
        break;
    }
}

void QXmppRosterManager::rosterIqReceived(const QXmppRosterIq& rosterIq)
{
    bool isInitial = (m_rosterReqId == rosterIq.id());

    switch(rosterIq.type())
    {
    case QXmppIq::Set:
        {
            // send result iq
            QXmppIq returnIq(QXmppIq::Result);
            returnIq.setId(rosterIq.id());
            client()->sendPacket(returnIq);

            // store updated entries and notify changes
            const QList<QXmppRosterIq::Item> items = rosterIq.items();
            for (int i = 0; i < items.count(); i++)
            {
                QString bareJid = items.at(i).bareJid();
                m_entries[bareJid] = items.at(i);
                emit rosterChanged(bareJid);
            }
{'fraction_non_alphanumeric': 0.11344126915136368, 'fraction_numerical': 0.0024991850483537977, 'mean_word_length': 2.7399431125558715, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5739494', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2878, 'n_words': 650}