/* Copyright (C) 2006,2007 by Marc Maurer * * 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #include "ut_path.h" #include #include #include #include #include "AccountEvent.h" #include "AccountHandler.h" #include "SessionEvent.h" const string AccountHandler::getProperty(const string& key) { PropertyMap::iterator pos = m_properties.find(key); if (pos != m_properties.end()) return pos->second; else return ""; } bool AccountHandler::autoConnect() { const std::string autoconnect = getProperty("autoconnect"); return strcmp(autoconnect.c_str(), "true") == 0; } bool AccountHandler::operator==(AccountHandler & rhHandler) { // Backends should override this if they need specific properties blacklisted beyond just autoconnect PropertyMap::iterator iter = m_properties.begin(); PropertyMap::iterator otherMapIter; PropertyMap::iterator end = m_properties.end(); // If different number of properties, then they can't be the same // Assumes that no items on the blacklist might be missing - this could be a false assumption. bool returnval=(m_properties.size() == rhHandler.m_properties.size()); while (returnval && iter != end) { // Check to see if property is on the blacklist // TODO: replace this with a vector if we ever need more than one on the blacklist. if ((iter->first) != "autoconnect") { // not on the blacklist if ((otherMapIter = rhHandler.m_properties.find((iter->first))) != rhHandler.m_properties.end()) { // if the other property map has this property returnval = ((iter->second) == (otherMapIter->second)); } } iter++; } return returnval; } void AccountHandler::addBuddy(Buddy* buddy) { m_vecBuddies.push_back(buddy); // signal all listeners we have a new buddy AccountAddBuddyEvent event; // TODO: fill the event AbiCollabSessionManager::getManager()->signal(event); } Buddy* AccountHandler::getBuddy(const UT_UTF8String& name) { for (UT_sint32 i = 0; i < m_vecBuddies.getItemCount(); i++) { Buddy* pBuddy = m_vecBuddies.getNthItem(i); if (pBuddy->getName() == name) return pBuddy; } UT_DEBUGMSG(("Getting buddy (%s) failed\n", name.utf8_str())); return 0; } void AccountHandler::getSessionsAsync() { for (UT_sint32 i = 0; i < m_vecBuddies.getItemCount(); i++) { const Buddy* pBuddy = m_vecBuddies.getNthItem(i); getSessionsAsync(*pBuddy); } } void AccountHandler::getSessionsAsync(const Buddy& buddy) { GetSessionsEvent event; send(&event, buddy); } void AccountHandler::joinSessionAsync(const Buddy& buddy, DocHandle& docHandle) { JoinSessionRequestEvent event( docHandle.getSessionId() ); send(&event, buddy); } void AccountHandler::signal(const Event& event, const Buddy* pSource) { UT_DEBUGMSG(("AccountHandler::signal()\n")); // broadcast this event over our network (if applicable for each message type) UT_GenericVector vRecipients = (event.isBroadcast() ? getBuddies() : event.getRecipients()); for (UT_sint32 i = 0; i < vRecipients.getItemCount(); i++) { Buddy* pRecipient = vRecipients.getNthItem(i); if (pRecipient) { if (!pSource || (pSource && pRecipient->getName() != pSource->getName()) ) { send(&event, *pRecipient); } else { // the event came originated at this buddy, so make sure not to send it // back to him, as it would result in a broadcast storm and // kill the network really fast } } } } void AccountHandler::handleMessage(const RawPacket& pRp) { UT_return_if_fail(pRp.buddy); // create archive IStrArchive isa( pRp.packet ); // serialize version UT_uint8 version; isa << version; if (version!=ABICOLLAB_PROTOCOL_VERSION_UINT8) { UT_DEBUGMSG(("Discarding packet, wrong version %d (expected %d)", version, ABICOLLAB_PROTOCOL_VERSION)); return; } // serialize class id and attempt to reconstruct UT_uint8 classId; isa << classId; Packet* newPacket = Packet::createPacket( (PClassType)classId ); if (!newPacket) { UT_DEBUGMSG(("Discarding packet, got unknown class %d", classId)); return; } // debug const char* className = Packet::getPacketClassname( (PClassType)classId ); UT_DEBUGMSG(("PACKET RECEIVED: [%s] %u bytes in serialized string\n", className?className:"unknown", isa.Size())); // serialize packet isa << *newPacket; // handle packet: // NOTE: uwog didn't want a "packet->execute" like construction, // so we're checking the packet type here PClassType pct = newPacket->getClassType(); _handlePacket( newPacket, pRp.buddy ); // cleanup DELETEP(newPacket); } void AccountHandler::_createPacketStream( std::string& sString, const Packet* pPacket ) { UT_return_if_fail( pPacket ); // create archive OStrArchive osa; // serialize version UT_uint8 version = ABICOLLAB_PROTOCOL_VERSION_UINT8; osa << version; // serialize class id UT_uint8 classId = pPacket->getClassType(); osa << classId; // serialize packet osa << const_cast( *pPacket ); // it's a writing archive, so cast is safe sString = osa.getData(); // debug const char* className = Packet::getPacketClassname( (PClassType)classId ); UT_DEBUGMSG(("PACKET SENT: [%s] %u bytes in serialized string\n", className?className:"unknown", osa.Size())); } void AccountHandler::_handlePacket( Packet* packet, Buddy* buddy ) { // packet and buddy must always be set UT_return_if_fail(packet); UT_return_if_fail(buddy); // as must the session manager AbiCollabSessionManager* pManager = AbiCollabSessionManager::getManager(); UT_return_if_fail(pManager); // see if manager can handle this if (!pManager->processPacket( *this, packet, buddy->getName() )) { // manager didn't handle it, see what we can do switch (packet->getClassType()) { case PCT_JoinSessionRequestEvent: { JoinSessionRequestEvent* jse = static_cast( packet ); // lookup session AbiCollab* pSession = pManager->getSessionFromSessionId( jse->getSessionId() ); UT_return_if_fail(pSession); // lookup exporter ABI_Collab_Export* pExport = pSession->getExport(); UT_return_if_fail(pExport); // lookup adjusts const UT_GenericVector* pExpAdjusts = pExport->getAdjusts(); UT_return_if_fail(pExpAdjusts); // TODO: ask the user to authorize this request bool bAuthorized = true; if (bAuthorized) { PD_Document* pDoc = pSession->getDocument(); // serialize entire document into string JoinSessionRequestResponseEvent jsre( jse->getSessionId() ); if (AbiCollabSessionManager::serializeDocument(pDoc, jsre.m_sZABW, false /* no base64 */) == UT_OK) { // send to buddy! send( &jsre, *buddy ); } // set more document properties jsre.m_iRev = (pExpAdjusts->getItemCount() > 0 ? pExpAdjusts->getNthItem(pExpAdjusts->getItemCount()-1)->m_iCRNumber : 0); jsre.m_sDocumentId = pDoc->getDocUUIDString(); if (pDoc->getFilename()) jsre.m_sDocumentName = UT_go_basename_from_uri(pDoc->getFilename()); // check if we already know this buddy Buddy* existing = getBuddy(buddy->getName()); if (!existing) { // we don't know this buddy yet; add this one as a volatile buddy buddy->setVolatile(true); addBuddy(buddy); } // add this buddy to the collaboration session pSession->addCollaborator(buddy); } break; } case PCT_JoinSessionRequestResponseEvent: { JoinSessionRequestResponseEvent* jsre = static_cast( packet ); PD_Document* pDoc = 0; if (AbiCollabSessionManager::deserializeDocument(&pDoc, jsre->m_sZABW, false) == UT_OK) { if (pDoc) { // NOTE: we could adopt the same document name here, but i'd // rather not at the moment - MARCM pDoc->forceDirty(); pManager->joinSession( jsre->getSessionId(), pDoc, jsre->m_sDocumentId, jsre->m_iRev, buddy ); } else UT_DEBUGMSG(("XMPPAccountHandler::_handleJoinSessionResponse() - deserializing document failed!\n")); } break; } case PCT_GetSessionsEvent: { GetSessionsResponseEvent gsre; const UT_GenericVector sessions = pManager->getSessions(); for (UT_sint32 i = 0; i < sessions.getItemCount(); i++) { AbiCollab* pSession = sessions.getNthItem(i); if (pSession && pSession->isLocallyControlled()) { const PD_Document * pDoc = pSession->getDocument(); if (pDoc) { // determine name UT_UTF8String documentBaseName; if (pDoc->getFilename()) documentBaseName = UT_go_basename_from_uri(pDoc->getFilename()); // set session info gsre.m_Sessions[ pSession->getSessionId() ] = documentBaseName; } else UT_ASSERT(UT_SHOULD_NOT_HAPPEN); } } send(&gsre, *buddy); break; } case PCT_GetSessionsResponseEvent: { GetSessionsResponseEvent* gsre = static_cast( packet ); UT_GenericVector vDocHandles; for (std::map::iterator it=gsre->m_Sessions.begin(); it!=gsre->m_Sessions.end(); ++it) { DocHandle* pDocHandle = new DocHandle((*it).first, (*it).second); vDocHandles.addItem(pDocHandle); } pManager->setDocumentHandles( *buddy, vDocHandles ); break; } } } } #ifdef WIN32 BOOL AccountHandler::_onCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) { return 1; // not handled by default } #endif