Exemplo n.º 1
0
extern "C" int CGC_API DiaQuit(const cgcRequest::pointer & request, cgcResponse::pointer response, cgcSession::pointer session)
{
	/////////////////////////////////
	// Request
	const tstring & sAccountId = request->getParameterValue(_T("AccountId"), _T(""));
	if (sAccountId.empty()) return -1;
	long dialogId = request->getParameterValue(_T("DID"), 0);

	/////////////////////////////////
	// Process
	CAccountInfo::pointer accountInfo = CGC_POINTER_CAST<CAccountInfo>(gApplication->getAttribute(BMT_ACCOUNTIDS, sAccountId));
	if (accountInfo.get() == NULL)
	{
		// Un register.
		return 14;
	}else if (session->getId().compare(accountInfo->getSessionId()) != 0)
	{
		// SessionId Error
		return -3;
	}

	CDialogInfo::pointer dialogInfo = CGC_POINTER_CAST<CDialogInfo>(gApplication->getAttribute(BMT_DIALOGS, dialogId));
	if (dialogInfo.get() == NULL)
	{
		// Dialog id not exist.
		return 71;
	}

	if (!dialogInfo->m_members.remove(accountInfo->getUserinfo()->getAccount()))
	{
		// Member not exist.
		return 73;
	}

	CLockMap<std::string, CUserInfo::pointer>::const_iterator iterUserInfo;
	boost::mutex::scoped_lock lockUserInfo(const_cast<boost::mutex&>(dialogInfo->m_members.mutex()));
	for (iterUserInfo=dialogInfo->m_members.begin(); iterUserInfo!=dialogInfo->m_members.end(); iterUserInfo++)
	{
		CUserInfo::pointer memberUserInfo = iterUserInfo->second;

		// Not register.
		CAccountInfo::pointer memberAccountInfo = CGC_POINTER_CAST<CAccountInfo>(gApplication->getAttribute(BMT_ACCOUNTS, memberUserInfo->getAccount()));
		if (memberAccountInfo.get() == NULL)
		{
			// offline event
			CMessageInfo::pointer messageInfo = CMessageInfo::create(0, 0, CConversationInfo::CT_QUIT, true);
			COfflineEvent::pointer offlineEvent = COfflineEvent::create(602, CFromInfo::create(dialogInfo), accountInfo->getUserinfo(), messageInfo);
			offlineEvent->toAccount(memberUserInfo);
			gAVSProxy->addOffEvent(memberUserInfo, offlineEvent);
			continue;
		}

		sendDiaQuit(memberAccountInfo, accountInfo->getUserinfo(), dialogInfo);
	}
	lockUserInfo.unlock();

	// Response
	return 0;
}
Exemplo n.º 2
0
void CAVSProxy::addOffEvent(CUserInfo::pointer touserInfo, COfflineEvent::pointer offlineEvent)
{
	BOOST_ASSERT (touserInfo.get() != 0);
	BOOST_ASSERT (offlineEvent.get() != 0);
	BOOST_ASSERT (offlineEvent->fromInfo().get() != 0);

	touserInfo->m_offevents.add(offlineEvent);

	CMessageInfo::pointer messageInfo = offlineEvent->getMessage();
	if (messageInfo.get() == 0)
	{
		return;
	}

	struct timeb tbNow;
	ftime(&tbNow);
	tbNow.time -= (tbNow.timezone*60);
	offlineEvent->getMessage()->msgtime(tbNow.time);

	if (m_bodbHandler->isopen())
	{
		long nFromId = 0;
		if (offlineEvent->fromInfo()->fromType() == CFromInfo::FromDialogInfo)
		{
			nFromId = offlineEvent->fromInfo()->fromDialog()->dialogId();
		}
		char sql[10*1024];
		memset(sql, 0, sizeof(sql));
		switch (offlineEvent->getEvent())
		{
		case 601:	// diainvite
		case 602:	// diaquit
			{
				sprintf(sql, "INSERT INTO offevents_t (event,fromtype,fromid,fromaccount,toaccount,message,msgtype) \
							 VALUES(%d,%d,%ld,'%s','%s','%s',%d)",
							 offlineEvent->getEvent(), (int)offlineEvent->fromInfo()->fromType(), nFromId, offlineEvent->getFromAccount()->getAccount().c_str(),
							 touserInfo->getAccount().c_str(), offlineEvent->toAccount()->getAccount().c_str(), messageInfo->type()
							 );
			}break;
		default:
			{
				switch (messageInfo->type())
				{
				case 3:
					{
						// image
						sprintf(sql, "INSERT INTO offevents_t (event,fromtype,fromid,fromaccount,toaccount,msgtype,newflag,message,msgsize,width,height) \
									 VALUES(%d,%d,%ld,'%s','%s',%d, %s, '%s', %d, %d, %d)",
									 offlineEvent->getEvent(), (int)offlineEvent->fromInfo()->fromType(), nFromId, offlineEvent->getFromAccount()->getAccount().c_str(),
									 touserInfo->getAccount().c_str(), messageInfo->type(),
									 messageInfo->newflag() ? "true" : "false",
									 messageInfo->getdata(), messageInfo->filesize(),
									 messageInfo->imageWidth(), messageInfo->imageHeight()
									 );
					}break;
				case 11:
					{
						// ??
						// Do not support to save the file offevent.
						return;
					}break;
				default:
					{

						sprintf(sql, "INSERT INTO offevents_t (event,fromtype,fromid,fromaccount,toaccount,msgtype,newflag,message,msgsize)\
									 VALUES(%d,%d,%ld,'%s','%s',%d, %s, '%s', %ld)",
									 offlineEvent->getEvent(), (int)offlineEvent->fromInfo()->fromType(), nFromId, offlineEvent->getFromAccount()->getAccount().c_str(),
									 touserInfo->getAccount().c_str(), messageInfo->type(),
									 messageInfo->newflag() ? "true" : "false",
									 messageInfo->getdata(), messageInfo->total()
									 );
					}break;
				}
			}break;
		}

		m_bodbHandler->execsql(sql);

	}
Exemplo n.º 3
0
extern "C" int CGC_API DiaInvite(const cgcRequest::pointer & request, cgcResponse::pointer response, cgcSession::pointer session)
{
	/////////////////////////////////
	// Request
	const tstring & sAccountId = request->getParameterValue(_T("AccountId"), _T(""));
	if (sAccountId.empty()) return -1;
	long dialogId = request->getParameterValue(_T("DID"), 0);

	/////////////////////////////////
	// Process
	CAccountInfo::pointer accountInfo = CGC_POINTER_CAST<CAccountInfo>(gApplication->getAttribute(BMT_ACCOUNTIDS, sAccountId));
	if (accountInfo.get() == NULL)
	//if (!gAVSProxy->m_accountids.find(sAccountId, accountInfo))
	{
		// Un register.
		return 14;
	}else if (session->getId().compare(accountInfo->getSessionId()) != 0)
	{
		// SessionId Error
		return -3;
	}

	CDialogInfo::pointer dialogInfo;
	if (dialogId == 0)
	{
		// New Dialog
		dialogInfo = CDialogInfo::create(gAVSProxy->getNextDialogId(), accountInfo->getUserinfo());
		dialogInfo->m_members.insert(accountInfo->getUserinfo()->getAccount(), accountInfo->getUserinfo());
		gApplication->setAttribute(BMT_DIALOGS, dialogInfo->dialogId(), dialogInfo);
		//gAVSProxy->m_dialogs.insert(dialogInfo->dialogId(), dialogInfo);

		response->setParameter(cgcParameter::create(_T("DID"), dialogInfo->dialogId()));
		response->sendResponse();
	}else
	{
		dialogInfo = CGC_POINTER_CAST<CDialogInfo>(gApplication->getAttribute(BMT_DIALOGS, dialogId));
		if (dialogInfo.get() == NULL)
		//if (!gAVSProxy->m_dialogs.find(dialogId, dialogInfo))
		{
			// Dialog id not exist.
			return 71;
		}/*else if (dialogInfo->manager().get() != accountInfo->getUserinfo().get())
		{
			// ?? Not the manager error.
			return 72;
		}*/
	}

	int index = 0;
	while (true)
	{
		char buffer[20];
		sprintf(buffer, "UA%d", index++);

		const tstring & sInviteUserAccount = request->getParameterValue(buffer, _T(""));
		if (sInviteUserAccount.empty())
		{
			break;
		}

		if (dialogInfo->m_members.exist(sInviteUserAccount))
		{
			// already exist in the dialog.
			continue;
		}

		CUserInfo::pointer inviteUserInfo;
		if (!getUserInfo(accountInfo, sInviteUserAccount, inviteUserInfo))
		{
			// ? continue;
			break;
		}

		// 1 Send the dialog members that invite account event.
		CLockMap<std::string, CUserInfo::pointer>::const_iterator iterUserInfo;
		boost::mutex::scoped_lock lockUserInfo(const_cast<boost::mutex&>(dialogInfo->m_members.mutex()));
		for (iterUserInfo=dialogInfo->m_members.begin(); iterUserInfo!=dialogInfo->m_members.end(); iterUserInfo++)
		{
			CUserInfo::pointer memberUserInfo = iterUserInfo->second;

			// Not register.
			CAccountInfo::pointer memberAccountInfo = CGC_POINTER_CAST<CAccountInfo>(gApplication->getAttribute(BMT_ACCOUNTS, memberUserInfo->getAccount()));
			if (memberAccountInfo.get() == NULL)
			//if (!gAVSProxy->m_accounts.find(memberUserInfo->getAccount(), memberAccountInfo))
			{
				// offline event
				CMessageInfo::pointer messageInfo = CMessageInfo::create(0, 0, CConversationInfo::CT_INVITE, true);
				COfflineEvent::pointer offlineEvent = COfflineEvent::create(601, CFromInfo::create(dialogInfo), accountInfo->getUserinfo(), messageInfo);
				offlineEvent->toAccount(inviteUserInfo);
				gAVSProxy->addOffEvent(memberUserInfo, offlineEvent);
				continue;
			}

			sendDiaInvite(memberAccountInfo, accountInfo->getUserinfo(), dialogInfo, inviteUserInfo);
		}
		lockUserInfo.unlock();

		// 2 Send invite useraccount the dialog members info.
		CAccountInfo::pointer inviteAccountInfo = CGC_POINTER_CAST<CAccountInfo>(gApplication->getAttribute(BMT_ACCOUNTS, sInviteUserAccount));
		if (inviteAccountInfo.get() != NULL)
		{
			// 
			sendDiaMembers(inviteAccountInfo, accountInfo->getUserinfo(), dialogInfo);
		}else
		{
			// Not register.
			// offline event
			CLockMap<std::string, CUserInfo::pointer>::const_iterator iterUserInfo;
			boost::mutex::scoped_lock lockUserInfo(const_cast<boost::mutex&>(dialogInfo->m_members.mutex()));
			for (iterUserInfo=dialogInfo->m_members.begin(); iterUserInfo!=dialogInfo->m_members.end(); iterUserInfo++)
			{
				CUserInfo::pointer memberUserInfo = iterUserInfo->second;

				CMessageInfo::pointer messageInfo = CMessageInfo::create(0, 0, CConversationInfo::CT_INVITE, true);
				COfflineEvent::pointer offlineEvent = COfflineEvent::create(601, CFromInfo::create(dialogInfo), accountInfo->getUserinfo(), messageInfo);
				offlineEvent->toAccount(memberUserInfo);
				gAVSProxy->addOffEvent(inviteUserInfo, offlineEvent);
			}
		}

		dialogInfo->m_members.insert(inviteUserInfo->getAccount(), inviteUserInfo);		
	}

	// Response
	return 0;
}
Exemplo n.º 4
0
// sendOfflineEvent
bool sendOfflineEvent(cgcResponse::pointer response, COfflineEvent::pointer offlineEvent)
{
	BOOST_ASSERT (response.get() != 0);
	BOOST_ASSERT (offlineEvent.get() != 0);

	if (response->isInvalidate()) return false;

	switch (offlineEvent->getEvent())
	{
	case 501:
		{
			CMessageInfo::pointer messageInfo = offlineEvent->getMessage();
			BOOST_ASSERT (messageInfo.get() != 0);

			long messageid = messageInfo->messageid();
			short msgtype = messageInfo->type();
			bool newflag = messageInfo->newflag();
			size_t sizeSended = 0;
			size_t tosendSize = 0;

			switch (msgtype)
			{
			case 1:
				{
					size_t toSendSizeTotal = messageInfo->total();
					cgc::cgcAttachment::pointer attach(cgcAttachment::create());
					attach->setName("text");
					attach->setTotal(toSendSizeTotal);
					while (sizeSended < toSendSizeTotal)
					{
						tosendSize = (short)(toSendSizeTotal-sizeSended) > MAX_PACKET_SIZE ? MAX_PACKET_SIZE : (toSendSizeTotal-sizeSended);
						attach->setAttach((const unsigned char*)messageInfo->getdata()+sizeSended, tosendSize);
						attach->setIndex(sizeSended);

						if (attach->getIndex() == 0)
						{
							if (!sendMsg(response, offlineEvent->getFromAccount(), offlineEvent->fromInfo(), messageid, msgtype, attach, newflag, offlineEvent->getMessage()->msgtime()))
							{
								return false;
							}
						}else
						{
							if (!sendMsg(response, offlineEvent->getFromAccount(), offlineEvent->fromInfo(), messageid, attach))
							{
								return false;
							}
						}
						sizeSended += tosendSize;

#ifdef WIN32
						Sleep(5);
#else
						usleep(5000);
#endif
					}
				}break;
			case 3:
				{
					std::string filename = messageInfo->tostring();
					size_t nFilesize = messageInfo->filesize();
					char filepath[256];
					sprintf(filepath, "%s/File/%s", gApplication->getAppConfPath().c_str(), filename.c_str());
					FILE * hfile = fopen(filepath, "rb");
					if (hfile == NULL)
					{
						return false;
					}
					unsigned char * imageBuffer = new unsigned char[nFilesize+1];
					size_t imageSize = fread(imageBuffer, 1, nFilesize, hfile);
					fclose(hfile);
					namespace fs = boost::filesystem;
					fs::path pathFile(filepath, fs::native);
					fs::remove(pathFile);

					if (imageSize == 0)
					{
						delete[] imageBuffer;
						return false;
					}

					cgc::cgcAttachment::pointer attach(cgcAttachment::create());
					attach->setName("image");
					attach->setTotal(imageSize);
					while (sizeSended < imageSize)
					{
						tosendSize = (short)(imageSize-sizeSended) > MAX_PACKET_SIZE ? MAX_PACKET_SIZE : (imageSize-sizeSended);
						attach->setAttach((const unsigned char*)imageBuffer+sizeSended, tosendSize);
						attach->setIndex(sizeSended);

						if (attach->getIndex() == 0)
						{
							long nWidth = messageInfo->imageWidth();
							long nHeight = messageInfo->imageHeight();
							if (!sendMsgImage(response, offlineEvent->getFromAccount(), offlineEvent->fromInfo(), messageid, nWidth, nHeight, msgtype, attach, newflag, offlineEvent->getMessage()->msgtime()))
							{
								delete[] imageBuffer;
								return false;
							}
						}else
						{
							if (!sendMsg(response, offlineEvent->getFromAccount(), offlineEvent->fromInfo(), messageid, attach))
							{
								delete[] imageBuffer;
								return false;
							}
						}
						sizeSended += tosendSize;

#ifdef WIN32
						Sleep(5);
#else
						usleep(5000);
#endif
					}
					delete[] imageBuffer;
				}break;
			default:
				break;
			}

		}break;
	default:
		{
			if (offlineEvent->fromInfo()->fromType() == CFromInfo::FromDialogInfo)
			{
				response->setParameter(cgcParameter::create(_T("DID"), offlineEvent->fromInfo()->fromDialog()->dialogId()));
			}
			response->setParameter(cgcParameter::create(_T("FromAccount"), offlineEvent->getFromAccount()->getAccount()));
			response->sendResponse(0, offlineEvent->getEvent());
		}break;
	}
	return true;
}