Debian Codegraph @

plugins/collab/core/account/xp/AccountHandler.cpp

raw
1 /* Copyright (C) 2006,2007 by Marc Maurer <[email protected]>
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA.
17 */
18
19 #include "ut_path.h"
20
21 #include <plugin/xp/AbiCollab_Plugin.h>
22 #include <session/xp/AbiCollab.h>
23 #include <session/xp/AbiCollab_Export.h>
24 #include <session/xp/AbiCollabSessionManager.h>
25 #include "AccountEvent.h"
26 #include "AccountHandler.h"
27 #include "SessionEvent.h"
28 #include "xap_App.h"
29 #include "xap_Frame.h"
30 #include "xap_Dlg_MessageBox.h"
31 #include "pp_Author.h"
32 #include <set>
33
34 bool AccountHandler::hasProperty(const std::string& key)
35 {
36 return m_properties.find(key) != m_properties.end();
37 }
38
39 const std::string AccountHandler::getProperty(const std::string& key)
40 {
41 PropertyMap::iterator pos = m_properties.find(key);
42 if (pos != m_properties.end())
43 return pos->second;
44 else
45 return "";
46 }
47
48 bool AccountHandler::autoConnect()
49 {
50 const std::string autoconnect = getProperty("autoconnect");
51 return strcmp(autoconnect.c_str(), "true") == 0;
52 }
53
54 bool AccountHandler::operator==(AccountHandler & rhHandler) {
55 // Backends should override this if they need specific properties blacklisted beyond just autoconnect
56 PropertyMap::iterator iter = m_properties.begin();
57 PropertyMap::iterator otherMapIter;
58 PropertyMap::iterator end = m_properties.end();
59
60 // If different number of properties, then they can't be the same
61 // Assumes that no items on the blacklist might be missing - this could be a false assumption.
62 bool returnval=(m_properties.size() == rhHandler.m_properties.size());
63 while (returnval && iter != end)
64 {
65 // Check to see if property is on the blacklist
66 // TODO: replace this with a vector if we ever need more than one on the blacklist.
67 if ((iter->first) != "autoconnect")
68 {
69 // not on the blacklist
70 if ((otherMapIter = rhHandler.m_properties.find((iter->first))) != rhHandler.m_properties.end())
71 {
72 // if the other property map has this property
73 returnval = ((iter->second) == (otherMapIter->second));
74 }
75 }
76 iter++;
77 }
78 return returnval;
79 }
80
81 void AccountHandler::addBuddy(BuddyPtr pBuddy)
82 {
83 UT_return_if_fail(pBuddy);
84 m_vBuddies.push_back(pBuddy);
85
86 // signal all listeners we have a new buddy
87 AccountAddBuddyEvent event;
88 // TODO: fill the event
89 AbiCollabSessionManager::getManager()->signal(event);
90 }
91
92 void AccountHandler::deleteBuddy(BuddyPtr pBuddy)
93 {
94 UT_return_if_fail(pBuddy);
95 for (std::vector<BuddyPtr>::iterator it = m_vBuddies.begin(); it != m_vBuddies.end(); it++)
96 {
97 BuddyPtr pB = *it;
98 UT_continue_if_fail(pB);
99 if (pB == pBuddy)
100 {
101 m_vBuddies.erase(it);
102 return;
103 }
104 }
105 UT_ASSERT_HARMLESS(UT_NOT_REACHED);
106 }
107
108 void AccountHandler::deleteBuddies()
109 {
110 m_vBuddies.clear();
111 }
112
113 bool AccountHandler::hasAccess(const std::vector<std::string>& vAcl, BuddyPtr pBuddy)
114 {
115 UT_return_val_if_fail(pBuddy, false);
116
117 for (UT_uint32 i = 0; i < vAcl.size(); i++)
118 if (vAcl[i] == pBuddy->getDescriptor(false))
119 return true;
120
121 return false;
122 }
123
124 void AccountHandler::getSessionsAsync()
125 {
126 for (std::vector<BuddyPtr>::iterator it = m_vBuddies.begin(); it != m_vBuddies.end(); it++)
127 getSessionsAsync(*it);
128 }
129
130 void AccountHandler::getSessionsAsync(BuddyPtr pBuddy)
131 {
132 GetSessionsEvent event;
133 send(&event, pBuddy);
134 }
135
136 void AccountHandler::joinSessionAsync(BuddyPtr pBuddy, DocHandle& docHandle)
137 {
138 JoinSessionRequestEvent event( docHandle.getSessionId() );
139 send(&event, pBuddy);
140 }
141
142 bool AccountHandler::hasSession(const UT_UTF8String& sSessionId)
143 {
144 for (std::vector<BuddyPtr>::iterator it = m_vBuddies.begin(); it != m_vBuddies.end(); it++)
145 {
146 BuddyPtr pBuddy = *it;
147 UT_continue_if_fail(pBuddy);
148 if (pBuddy->getDocHandle(sSessionId))
149 return true;
150 }
151 return false;
152 }
153
154 void AccountHandler::signal(const Event& event, BuddyPtr pSource)
155 {
156 UT_DEBUGMSG(("AccountHandler::signal()\n"));
157
158 // we will not forward an event over this account that came from another
159 // account: if you do that, then you very easily get packets running around
160 // forever.
161 if (pSource && pSource->getHandler() != this)
162 return;
163
164 // broadcast this event over our network (if applicable for each message type)
165 const std::vector<BuddyPtr> vRecipients =
166 (event.isBroadcast() ? getBuddies() : event.getRecipients());
167
168 for (std::vector<BuddyPtr>::const_iterator cit = vRecipients.begin(); cit != vRecipients.end(); cit++)
169 {
170 BuddyPtr pRecipient = *cit;
171 UT_continue_if_fail(pRecipient);
172
173 if (!pSource || (pSource != pRecipient))
174 {
175 send(&event, pRecipient);
176 }
177 else
178 {
179 // the event originated from this buddy, so make sure not to send it
180 // back to him, as it would result in a broadcast storm and
181 // kill the network really fast
182 }
183 }
184 }
185
186 void AccountHandler::handleMessage(Packet* pPacket, BuddyPtr pBuddy)
187 {
188 UT_return_if_fail(pPacket);
189 UT_return_if_fail(pBuddy);
190
191 AbiCollabSessionManager* pManager = AbiCollabSessionManager::getManager();
192 UT_return_if_fail(pManager);
193
194 //
195 // handle the incoming packet: first check for a protocol error, then ask the
196 // session manager to handle it, then try to handle it ourselves
197 //
198 if (_handleProtocolError(pPacket, pBuddy) ||
199 pManager->processPacket(*this, pPacket, pBuddy))
200 {
201 DELETEP(pPacket);
202 return;
203 }
204
205 // it seems we need to handle the packet ourselves
206 _handlePacket(pPacket, pBuddy);
207 DELETEP(pPacket);
208 }
209
210 Packet* AccountHandler::_createPacket(const std::string& packet, BuddyPtr pBuddy)
211 {
212 UT_return_val_if_fail(pBuddy, NULL);
213
214 // create archive
215 IStrArchive isa( packet );
216
217 // serialize version
218 int version;
219 isa << COMPACT_INT(version);
220 if (version != ABICOLLAB_PROTOCOL_VERSION)
221 {
222 if (version > 0)
223 {
224 UT_DEBUGMSG(("Discarding packet, wrong version %d (expected %d)\n", version, ABICOLLAB_PROTOCOL_VERSION));
225 _sendProtocolError(pBuddy, PE_Invalid_Version);
226 return NULL;
227 }
228 else
229 {
230 UT_DEBUGMSG(("Got error packet (hopefully), revision=%d\n", version));
231 // if it's a version 0 message, handle normally, picked up in _handlePacket
232 }
233 }
234
235 // serialize class id and attempt to reconstruct
236 UT_uint8 classId;
237 isa << classId;
238 Packet* newPacket = Packet::createPacket( (PClassType)classId );
239 if (!newPacket)
240 {
241 UT_DEBUGMSG(("Discarding packet, got unknown class %d\n", classId));
242 return NULL;
243 }
244
245 // debug
246 UT_DEBUGMSG(("PACKET DESERIALIZED: [%s] %u bytes in serialized string\n", Packet::getPacketClassname( (PClassType)classId ), isa.Size()));
247
248 // serialize packet
249 isa << *newPacket;
250
251 return newPacket;
252 }
253
254 void AccountHandler::_createPacketStream( std::string& sString, const Packet* pPacket )
255 {
256 UT_return_if_fail( pPacket );
257
258 // create archive
259 OStrArchive osa;
260
261 // serialize version
262 int version = pPacket->getProtocolVersion();
263 osa << COMPACT_INT(version);
264
265 // serialize class id
266 UT_uint8 classId = pPacket->getClassType();
267 osa << classId;
268
269 // serialize packet
270 osa << const_cast<Packet&>( *pPacket ); // it's a writing archive, so cast is safe
271
272 sString = osa.getData();
273
274 // debug
275 UT_DEBUGMSG(("PACKET SENT: [%s] %u bytes in serialized string\n", Packet::getPacketClassname( (PClassType)classId ), osa.Size()));
276 }
277
278 bool AccountHandler::_handleProtocolError(Packet* packet, BuddyPtr buddy)
279 {
280 // packet and buddy must always be set
281 UT_return_val_if_fail(packet, false);
282 UT_return_val_if_fail(buddy, false);
283
284 // error report?
285 if (packet->getClassType() != PCT_ProtocolErrorPacket)
286 return false;
287
288 // we have an error!
289 ProtocolErrorPacket* pee = static_cast<ProtocolErrorPacket*>(packet);
290 // report the error
291 _reportProtocolError(pee->getRemoteVersion(), pee->getErrorEnum(), buddy);
292 // and remove buddy
293 forceDisconnectBuddy(buddy);
294 return true;
295 }
296
297 void AccountHandler::_handlePacket(Packet* packet, BuddyPtr buddy)
298 {
299 // packet and buddy must always be set
300 UT_return_if_fail(packet);
301 UT_return_if_fail(buddy);
302
303 // as must the session manager
304 AbiCollabSessionManager* pManager = AbiCollabSessionManager::getManager();
305 UT_return_if_fail(pManager);
306
307 // manager didn't handle it, see what we can do
308 switch (packet->getClassType())
309 {
310 case PCT_JoinSessionRequestEvent:
311 {
312 JoinSessionRequestEvent* jse = static_cast<JoinSessionRequestEvent*>(packet);
313
314 // lookup session
315 AbiCollab* pSession = pManager->getSessionFromSessionId(jse->getSessionId());
316 UT_return_if_fail(pSession);
317
318 // check if this buddy is allowed to access this document
319 // TODO: this should be done for every session packet, not just join session packets
320 if (!hasAccess(pSession->getAcl(), buddy))
321 {
322 // we should only reach this point if someone is brute forcing trying
323 // out session IDs while not being on the ACL. Ban this uses.
324 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
325 return;
326 }
327
328 // lookup exporter
329 ABI_Collab_Export* pExport = pSession->getExport();
330 UT_return_if_fail(pExport);
331
332 // lookup adjusts
333 const UT_GenericVector<ChangeAdjust *>* pExpAdjusts = pExport->getAdjusts();
334 UT_return_if_fail(pExpAdjusts);
335
336 PD_Document* pDoc = pSession->getDocument();
337
338 // add this author to the document if we don't recognize him
339 UT_sint32 iAuthorId = -1;
340 UT_UTF8String buddyDescriptor = buddy->getDescriptor();
341 UT_GenericVector<pp_Author*> authors = pDoc->getAuthors();
342 UT_DEBUGMSG(("Scanning %d authors to see if we recognize this buddy\n", authors.getItemCount()));
343 for (UT_sint32 i = 0; i < authors.getItemCount(); i++)
344 {
345 pp_Author* pAuthor = authors.getNthItem(i);
346 UT_continue_if_fail(pAuthor);
347
348 const gchar* szDescriptor = NULL;
349 pAuthor->getProperty("abicollab-descriptor", szDescriptor);
350 if (!szDescriptor)
351 continue;
352
353 if (buddyDescriptor != szDescriptor)
354 continue;
355
356 // yay, we know this author!
357 iAuthorId = pAuthor->getAuthorInt();
358 UT_DEBUGMSG(("Found known author with descriptior %s, id %d!\n", buddyDescriptor.utf8_str(), iAuthorId));
359 break;
360 }
361
362 if (iAuthorId == -1)
363 {
364 // we don't know this author yet, create a new author object for him
365 iAuthorId = pDoc->findFirstFreeAuthorInt();
366 pp_Author * pA = pDoc->addAuthor(iAuthorId);
367 PP_AttrProp * pPA = pA->getAttrProp();
368 pPA->setProperty("abicollab-descriptor", buddyDescriptor.utf8_str());
369 pDoc->sendAddAuthorCR(pA);
370 UT_DEBUGMSG(("Added a new author to the documument with descriptor %s, id %d\n", buddyDescriptor.utf8_str(), iAuthorId));
371 }
372
373 // serialize entire document into string
374 JoinSessionRequestResponseEvent jsre(jse->getSessionId(), iAuthorId);
375 if (AbiCollabSessionManager::serializeDocument(pDoc, jsre.m_sZABW, false /* no base64 */) == UT_OK)
376 {
377 // set more document properties
378 jsre.m_iRev = pDoc->getCRNumber();
379 jsre.m_sDocumentId = pDoc->getDocUUIDString();
380 if (pDoc->getFilename())
381 jsre.m_sDocumentName = UT_go_basename_from_uri(pDoc->getFilename());
382
383 // send to buddy!
384 send(&jsre, buddy);
385
386 // add this buddy to the collaboration session
387 pSession->addCollaborator(buddy);
388 }
389 break;
390 }
391
392 case PCT_JoinSessionRequestResponseEvent:
393 {
394 JoinSessionRequestResponseEvent* jsre = static_cast<JoinSessionRequestResponseEvent*>( packet );
395 PD_Document* pDoc = 0;
396 if (AbiCollabSessionManager::deserializeDocument(&pDoc, jsre->m_sZABW, false) == UT_OK)
397 {
398 if (pDoc)
399 {
400 // NOTE: we could adopt the same document name here, but i'd
401 // rather not at the moment - MARCM
402 pDoc->forceDirty();
403 if (jsre->m_sDocumentName.size() > 0)
404 {
405 gchar* fname = g_strdup(jsre->m_sDocumentName.utf8_str());
406 pDoc->setFilename(fname);
407 }
408 // The default ownership when joining is FALSE, as that seems
409 // to make sense for the generic case. The person sharing the
410 // document by default owns the document (and is thus allowed
411 // to modify the ACL).
412 pManager->joinSession(jsre->getSessionId(), pDoc, jsre->m_sDocumentId, jsre->m_iRev, jsre->getAuthorId(), buddy, this, false, NULL);
413 }
414 else
415 {
416 UT_DEBUGMSG(("AccountHandler::_handlePacket() - deserializing document failed!\n"));
417 }
418 }
419 break;
420 }
421
422 case PCT_GetSessionsEvent:
423 {
424 GetSessionsResponseEvent gsre;
425 const UT_GenericVector<AbiCollab *> sessions = pManager->getSessions();
426 for (UT_sint32 i = 0; i < sessions.getItemCount(); i++)
427 {
428 AbiCollab* pSession = sessions.getNthItem(i);
429 if (pSession && pSession->isLocallyControlled())
430 {
431 // check if the buddy has access to this session
432 if (!hasAccess(pSession->getAcl(), buddy))
433 {
434 UT_DEBUGMSG(("Buddy %s denied access to session %s by ALC\n", buddy->getDescriptor(true).utf8_str(), pSession->getSessionId().utf8_str()));
435 continue;
436 }
437
438 const PD_Document * pDoc = pSession->getDocument();
439 UT_continue_if_fail(pDoc);
440
441 // determine name
442 UT_UTF8String documentBaseName;
443 if (pDoc->getFilename())
444 documentBaseName = UT_go_basename_from_uri(pDoc->getFilename());
445 // set session info
446 gsre.m_Sessions[ pSession->getSessionId() ] = documentBaseName;
447 }
448 }
449 send(&gsre, buddy);
450 break;
451 }
452
453 case PCT_GetSessionsResponseEvent:
454 {
455 GetSessionsResponseEvent* gsre = static_cast<GetSessionsResponseEvent*>( packet );
456 UT_GenericVector<DocHandle*> vDocHandles;
457 for (std::map<UT_UTF8String,UT_UTF8String>::iterator it=gsre->m_Sessions.begin(); it!=gsre->m_Sessions.end(); ++it) {
458 DocHandle* pDocHandle = new DocHandle((*it).first, (*it).second);
459 vDocHandles.addItem(pDocHandle);
460 }
461 pManager->setDocumentHandles(buddy, vDocHandles);
462 break;
463 }
464
465 default:
466 {
467 UT_DEBUGMSG(("Unhandled packet class: 0x%x\n", packet->getClassType()));
468 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
469 break;
470 }
471 }
472 }
473
474 void AccountHandler::_reportProtocolError(UT_sint32 remoteVersion, UT_sint32 errorEnum, BuddyPtr pBuddy)
475 {
476 #ifndef DEBUG
477 UT_UNUSED(remoteVersion);
478 UT_UNUSED(errorEnum);
479 #endif
480 UT_DEBUGMSG(("_reportProtocolError: remoteVersion=%d errorEnum=%d\n", remoteVersion, errorEnum));
481 UT_return_if_fail(pBuddy);
482
483 static std::set<std::string> reportedBuddies;
484 if (reportedBuddies.insert( pBuddy->getDescriptor(false).utf8_str() ).second)
485 {
486 UT_UTF8String msg;
487 switch (errorEnum) {
488 case PE_Invalid_Version:
489 msg = UT_UTF8String_sprintf("Your buddy %s is using version %d of AbiCollab, while you are using version %d.\n"
490 "Please make sure you are using the same AbiWord version.",
491 pBuddy->getDescription().utf8_str(),
492 remoteVersion, ABICOLLAB_PROTOCOL_VERSION);
493 break;
494 default:
495 msg = UT_UTF8String_sprintf("An unknown error code %d was reported by buddy %s.", errorEnum,
496 pBuddy->getDescription().utf8_str());
497 break;
498 }
499 XAP_App::getApp()->getLastFocussedFrame()->showMessageBox(
500 msg.utf8_str(),
501 XAP_Dialog_MessageBox::b_O,
502 XAP_Dialog_MessageBox::a_OK);
503 }
504 }
505
506 void AccountHandler::_sendProtocolError(BuddyPtr pBuddy, UT_sint32 errorEnum)
507 {
508 UT_return_if_fail(pBuddy);
509 ProtocolErrorPacket event(errorEnum);
510 send(&event, pBuddy);
511 }
512
513 /*
514 * ProtocolErrorPacket
515 */
516 REGISTER_PACKET(ProtocolErrorPacket);
517
518 ProtocolErrorPacket::ProtocolErrorPacket()
519 : m_errorEnum(0)
520 , m_remoteVersion(0)
521 {
522 }
523
524 ProtocolErrorPacket::ProtocolErrorPacket( UT_sint32 errorEnum )
525 : m_errorEnum( errorEnum )
526 , m_remoteVersion( ABICOLLAB_PROTOCOL_VERSION )
527 {
528 }
529
530 void ProtocolErrorPacket::serialize(Archive & ar)
531 {
532 Packet::serialize( ar );
533 ar << COMPACT_INT(m_errorEnum) << COMPACT_INT(m_remoteVersion);
534 }