Beispiel #1
0
void YahooClient::moveBuddy(YahooUserData *data, const char *grp)
{
    if (getState() != Connected)
        return;
    if (data->Group.ptr == NULL) {
        if ((grp == NULL) || (*grp == 0))
            return;
        set_str(&data->Group.ptr, grp);
        addBuddy(data);
        return;
    }
    if ((grp == NULL) || (*grp == 0)) {
        removeBuddy(data);
        return;
    }
    if (!strcmp(data->Group.ptr, grp))
        return;
    addParam(1, getLogin().utf8());
    addParam(7, data->Login.ptr);
    addParam(65, grp);
    sendPacket(YAHOO_SERVICE_ADDBUDDY);
    addParam(1, getLogin().utf8());
    addParam(7, data->Login.ptr);
    addParam(65, data->Group.ptr ? data->Group.ptr : "");
    sendPacket(YAHOO_SERVICE_REMBUDDY);
    set_str(&data->Group.ptr, grp);
}
void TelepathyAccountHandler::addContact(TpContact* contact)
{
	UT_DEBUGMSG(("TelepathyAccountHandler::addContact()\n"));
	UT_return_if_fail(contact);

	TelepathyBuddyPtr pBuddy = boost::shared_ptr<TelepathyBuddy>(new TelepathyBuddy(this, contact));
	TelepathyBuddyPtr pExistingBuddy = _getBuddy(pBuddy);
	if (!pExistingBuddy)
		addBuddy(pBuddy);
}
Beispiel #3
0
YahooUserData *YahooClient::findContact(const char *id, const char *grpname, Contact *&contact, bool bSend)
{
    ContactList::ContactIterator it;
    while ((contact = ++it) != NULL) {
        YahooUserData *data;
        ClientDataIterator itd(contact->clientData);
        while ((data = (YahooUserData*)(++itd)) != NULL) {
            if (data->Login.ptr && !strcmp(id, data->Login.ptr))
                return data;
        }
    }
    it.reset();
    while ((contact = ++it) != NULL) {
        if (contact->getName() == id) {
            YahooUserData *data = (YahooUserData*)contact->clientData.createData(this);
            set_str(&data->Login.ptr, id);
            set_str(&data->Group.ptr, grpname);
            Event e(EventContactChanged, contact);
            e.process();
            return data;
        }
    }
    if (grpname == NULL)
        return NULL;
    Group *grp = NULL;
    if (*grpname) {
        ContactList::GroupIterator it;
        while ((grp = ++it) != NULL)
            if (grp->getName() == QString::fromLocal8Bit(grpname))
                break;
        if (grp == NULL) {
            grp = getContacts()->group(0, true);
            grp->setName(QString::fromLocal8Bit(grpname));
            Event e(EventGroupChanged, grp);
            e.process();
        }
    }
    if (grp == NULL)
        grp = getContacts()->group(0);
    contact = getContacts()->contact(0, true);
    YahooUserData *data = (YahooUserData*)(contact->clientData.createData(this));
    set_str(&data->Login.ptr, id);
    contact->setName(id);
    contact->setGroup(grp->id());
    Event e(EventContactChanged, contact);
    e.process();
    if (bSend)
        addBuddy(data);
    return data;
}
void TCPAccountHandler::_handleAccept(IOServerHandler* pHandler, boost::shared_ptr<Session> session)
{
	UT_DEBUGMSG(("TCPAccountHandler::handleAccept\n"));
	UT_return_if_fail(pHandler);
	UT_return_if_fail(session);
	
	// store this buddy/session
	UT_UTF8String name;
	UT_UTF8String_sprintf(name, "%s:%d", session->getRemoteAddress().c_str(), session->getRemotePort());
	TCPBuddyPtr pBuddy = boost::shared_ptr<TCPBuddy>(new TCPBuddy(this, 
								session->getRemoteAddress(), 
								boost::lexical_cast<std::string>(session->getRemotePort())));
	addBuddy(pBuddy);
	m_clients.insert(std::pair<TCPBuddyPtr, boost::shared_ptr<Session> >(pBuddy, session));
	
	// accept a new buddy/session
	pHandler->asyncAccept();	
}
void XMPPAccountHandler::handleMessage(const gchar* packet_data, const std::string& from_address)
{
	UT_return_if_fail(packet_data);
	UT_return_if_fail(from_address.size() > 0);
	
	XMPPBuddyPtr pBuddy = _getBuddy(from_address);
	if (!pBuddy)
	{
		// yay, a message from a new buddy
		pBuddy = XMPPBuddyPtr(new XMPPBuddy(this, from_address.c_str()));
		addBuddy(pBuddy);
	}

	// construct the packet
	// NOTE: all packets are base64 encoded when sent over this backend, so we need to decode them
	// FIXME: inefficient copying of data
	std::string packet_str = packet_data;
	size_t len = gsf_base64_decode_simple((guint8*)(&packet_str[0]), packet_str.size());
	packet_str.resize(len);
	Packet* pPacket = _createPacket(packet_str, pBuddy);
	UT_return_if_fail(pPacket);

	AccountHandler::handleMessage(pPacket, pBuddy);
}
ConnectResult TCPAccountHandler::connect()
{
	UT_DEBUGMSG(("TCPAccountHandler::connect()\n"));

	AbiCollabSessionManager* pManager = AbiCollabSessionManager::getManager();
	UT_return_val_if_fail(pManager, CONNECT_INTERNAL_ERROR);

	UT_return_val_if_fail(!m_pDelegator, CONNECT_INTERNAL_ERROR);
	UT_return_val_if_fail(!m_bConnected, CONNECT_ALREADY_CONNECTED);
	UT_return_val_if_fail(!m_thread, CONNECT_INTERNAL_ERROR);
	m_io_service.reset();
	m_thread = new asio::thread(boost::bind(&asio::io_service::run, &m_io_service));

	// set up the connection
	if (getProperty("server") == "")
	{
		UT_sint32 port = _getPort(getProperties());
		UT_DEBUGMSG(("Start accepting connections on port %d...\n", port));

		try
		{
			IOServerHandler* pDelegator = new IOServerHandler(port, 
						boost::bind(&TCPAccountHandler::_handleAccept, this, _1, _2),
						boost::bind(&TCPAccountHandler::handleEvent, this, _1), m_io_service);
			m_pDelegator = pDelegator;
			m_bConnected = true; // todo: ask it to the acceptor
			pDelegator->run();
		}
		catch (asio::system_error se)
		{
			UT_DEBUGMSG(("Failed to start accepting connections: %s\n", se.what()));
			_teardownAndDestroyHandler();
			return CONNECT_FAILED;
		}
		catch (...)
		{
			UT_DEBUGMSG(("Caught unhandled server exception!\n"));
			_teardownAndDestroyHandler();
			return CONNECT_FAILED;
		}
	}
	else
	{
		UT_DEBUGMSG(("Connecting to server %s on port %d...\n", getProperty("server").c_str(), _getPort(getProperties())));

		try
		{
			asio::ip::tcp::resolver resolver(m_io_service);
			asio::ip::tcp::resolver::query query(getProperty("server"), getProperty("port"));
			asio::ip::tcp::resolver::iterator iterator(resolver.resolve(query));

			bool connected = false;
			boost::shared_ptr<Session> session_ptr(new Session(m_io_service, boost::bind(&TCPAccountHandler::handleEvent, this, _1)));
			while (iterator != tcp::resolver::iterator())
			{
				try
				{
					UT_DEBUGMSG(("Attempting to connect...\n"));
					session_ptr->connect(iterator);
					UT_DEBUGMSG(("Connected!\n"));
					connected = true;
					break;
				}
				catch (asio::system_error se)
				{
					UT_DEBUGMSG(("Connection attempt failed: %s\n", se.what()));
					// make sure we close the socket after a failed attempt, as it
					// may have been opened by the connect() call.
					try { session_ptr->getSocket().close(); } catch(...) {}
				}
				iterator++;
			}

			if (!connected)
			{
				UT_DEBUGMSG(("Giving up to connecting to server!\n"));
				_teardownAndDestroyHandler();
				return CONNECT_FAILED;
			}

			session_ptr->asyncReadHeader();
			m_bConnected = true; // todo: ask it to the socket
			// Add a buddy
			TCPBuddyPtr pBuddy = boost::shared_ptr<TCPBuddy>(new TCPBuddy(this, 
						session_ptr->getRemoteAddress(),
						boost::lexical_cast<std::string>(session_ptr->getRemotePort())));
			addBuddy(pBuddy);
			m_clients.insert(std::pair<TCPBuddyPtr, boost::shared_ptr<Session> >(pBuddy, session_ptr));
		}
		catch (asio::system_error se)
		{
			UT_DEBUGMSG(("Failed to resolve %s:%d: %s\n", getProperty("server").c_str(), _getPort(getProperties()), se.what()));
			_teardownAndDestroyHandler();
			return CONNECT_FAILED;
		}
	}
	
	if (!m_bConnected)
		return CONNECT_FAILED;

	// we are connected now, time to start sending out messages (such as events)
	pManager->registerEventListener(this);
	// signal all listeners we are logged in
	AccountOnlineEvent event;
	// TODO: fill the event
	AbiCollabSessionManager::getManager()->signal(event);

	return CONNECT_SUCCESS;
}