Exemple #1
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);

	}
extern "C" int CGC_API MsgData(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 mid = request->getParameterValue(_T("MID"), 0);
	const tstring & sFriendAccount = request->getParameterValue(_T("SendTo"), _T(""));
	if (sFriendAccount.empty()) return -1;
	if (!request->isHasAttachInfo()) return -1;

	/////////////////////////////////
	// 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;
	}

	CUserInfo::pointer sendtoUserInfo;
	if (!getUserInfo(accountInfo, sFriendAccount, sendtoUserInfo))
	{
		// Friend not exist.
		return 16;
	}

	cgcAttachment::pointer attach = request->getAttachment();
	BOOST_ASSERT (attach.get() != NULL);

	CMessageInfo::pointer messageInfo = CGC_POINTER_CAST<CMessageInfo>(gApplication->getAttribute(BMT_OFFLINEMSG, mid));
	//gAVSProxy->m_offlinemsg.find(mid, messageInfo);

	CAccountInfo::pointer friendAccountInfo = CGC_POINTER_CAST<CAccountInfo>(gApplication->getAttribute(BMT_ACCOUNTS, sFriendAccount));
	if (friendAccountInfo.get() == NULL || messageInfo.get() != NULL)
//	if (!gAVSProxy->m_accounts.find(sFriendAccount, friendAccountInfo) || messageInfo.get() != NULL)
	{
		// Friend is offline state.
		if (messageInfo.get() != NULL && messageInfo->total() != attach->getTotal())
		{
			gApplication->removeAttribute(BMT_OFFLINEMSG, mid);
			//gAVSProxy->m_offlinemsg.remove(mid);
			messageInfo.reset();
		}

		if (messageInfo.get() == NULL)
		{
			if (attach->getIndex() == 0)
			{
				long type = request->getParameterValue(_T("Type"), 0);
				bool bNew = request->getParameterValue2(_T("New"), true);
				messageInfo = CMessageInfo::create(mid, attach->getTotal(), (short)type, bNew);

				if (type == 3)
				{
					long imageWidth = request->getParameterValue(_T("Width"), 0);
					long imageHeight = request->getParameterValue(_T("Height"), 0);
					messageInfo->imageWH(imageWidth, imageHeight);
				}
			}else
			{
				messageInfo = CMessageInfo::create(mid, attach->getTotal());
			}

			if (attach->getIndex() + attach->getAttachSize() == attach->getTotal())
			{
				if (attach->getIndex() > 0)
					messageInfo->m_indexs.insert(attach->getIndex(), true);
				messageInfo->m_indexs.insert(attach->getTotal(), true);
			}else
			{
				int indexCount = attach->getTotal() / attach->getAttachSize();
				if (attach->getTotal() % attach->getAttachSize() > 0)
					indexCount += 1;

				for (int i=1; i<= indexCount; i++)
				{
					unsigned long index = (i==indexCount) ? attach->getTotal() : i*attach->getAttachSize();
					messageInfo->m_indexs.insert(index, true);
				}
			}

			gApplication->setAttribute(BMT_OFFLINEMSG, mid, messageInfo);
			//gAVSProxy->m_offlinemsg.insert(mid, messageInfo);
		}else if (attach->getIndex() == 0)
		{
			long type = request->getParameterValue(_T("Type"), 0);
			bool bNew = request->getParameterValue2(_T("New"), true);

			messageInfo->type((short)type);
			messageInfo->newflag(bNew);
			if (type == 3)
			{
				long imageWidth = request->getParameterValue(_T("Width"), 0);
				long imageHeight = request->getParameterValue(_T("Height"), 0);
				messageInfo->imageWH(imageWidth, imageHeight);
			}
		}
		messageInfo->setdata((const char*)attach->getAttachData(), attach->getAttachSize(), attach->getIndex());
		messageInfo->m_indexs.remove(attach->getIndex() + attach->getAttachSize());
		if (!messageInfo->m_indexs.empty())
		{
			// Already had data.
			return 17;
		}
		gApplication->removeAttribute(BMT_OFFLINEMSG, mid);
		//gAVSProxy->m_offlinemsg.remove(mid);

		switch (messageInfo->type())
		{
		case 3:
			{
				// Save the image.
				char * filename = new char[50];
#ifdef WIN32
				sprintf(filename, "%I64d%08ld", time(0), mid);
#else
				sprintf(filename, "%ld%08ld", time(0), mid);
#endif
				char filepath[256];
				sprintf(filepath, "%s/File/%s", gApplication->getAppConfPath().c_str(), filename);
				FILE * hfile = fopen(filepath, "wb");
				if (hfile == NULL)
				{
					// System error.
					delete[] filename;
					return -2;
				}
				fwrite(messageInfo->getdata(), 1, messageInfo->total(), hfile);
				fclose(hfile);

				messageInfo->filesize(messageInfo->total());
				messageInfo->setdata(filename, strlen(filename));
			}break;
		default:
			break;
		}

		COfflineEvent::pointer offlineEvent = COfflineEvent::create(501, CFromInfo::create(accountInfo->getUserinfo()), accountInfo->getUserinfo(), messageInfo);
		gAVSProxy->addOffEvent(sendtoUserInfo, offlineEvent);
		return 17;
	}

	CDialogInfo::pointer nullDialogInfo;
	bool ret = false;
	if (attach->getIndex() == 0)
	{
		long nType = request->getParameterValue(_T("Type"), 0);
		bool bNew = request->getParameterValue2(_T("New"), true);

		if (nType == 3)
		{
			long nWidth = request->getParameterValue(_T("Width"), 0);
			long nHeight = request->getParameterValue(_T("Height"), 0);
			ret = sendMsgImage(friendAccountInfo, accountInfo->getUserinfo(), CFromInfo::create(accountInfo->getUserinfo()), mid, nWidth, nHeight, nType, attach, bNew);
		}else
		{
			ret = sendMsg(friendAccountInfo, accountInfo->getUserinfo(), CFromInfo::create(accountInfo->getUserinfo()), mid, nType, attach, bNew);
		}
	}else
	{
		ret = sendMsg(friendAccountInfo, accountInfo->getUserinfo(), CFromInfo::create(accountInfo->getUserinfo()), mid, attach);
	}

	// Response
	return 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;
}
int sendtoMsg(const cgcRequest::pointer & request, cgcAttachment::pointer attach, CAccountInfo::pointer fromAccount, CFromInfo::pointer fromInfo, CUserInfo::pointer sendtoUserInfo, long mid)
{
	BOOST_ASSERT (attach.get() != NULL);
	BOOST_ASSERT (fromAccount.get() != NULL);
	BOOST_ASSERT (fromInfo.get() != NULL);
	BOOST_ASSERT (sendtoUserInfo.get() != NULL);

	CMessageInfo::pointer offlineMessageInfo = CGC_POINTER_CAST<CMessageInfo>(gApplication->getAttribute(sendtoUserInfo->getAccount(), mid));
	CAccountInfo::pointer friendAccountInfo = CGC_POINTER_CAST<CAccountInfo>(gApplication->getAttribute(BMT_ACCOUNTS, sendtoUserInfo->getAccount()));
	if (friendAccountInfo.get() == NULL || offlineMessageInfo.get() != NULL)
	{
		// Friend is offline state.
		if (offlineMessageInfo.get() != NULL && offlineMessageInfo->total() != attach->getTotal())
		{
			gApplication->removeAttribute(sendtoUserInfo->getAccount(), mid);
			offlineMessageInfo.reset();
		}

		if (offlineMessageInfo.get() == NULL)
		{
			if (attach->getIndex() == 0)
			{
				long type = request->getParameterValue(_T("Type"), 0);
				bool bNew = request->getParameterValue2(_T("New"), true);
				offlineMessageInfo = CMessageInfo::create(mid, attach->getTotal(), (short)type, bNew);

				if (type == 3)
				{
					long imageWidth = request->getParameterValue(_T("Width"), 0);
					long imageHeight = request->getParameterValue(_T("Height"), 0);
					offlineMessageInfo->imageWH(imageWidth, imageHeight);
				}
			}else
			{
				offlineMessageInfo = CMessageInfo::create(mid, attach->getTotal());
			}

			if (attach->getIndex() + attach->getAttachSize() == attach->getTotal())
			{
				if (attach->getIndex() > 0)
					offlineMessageInfo->m_indexs.insert(attach->getIndex(), true);
				offlineMessageInfo->m_indexs.insert(attach->getTotal(), true);
			}else
			{
				int indexCount = attach->getTotal() / attach->getAttachSize();
				if (attach->getTotal() % attach->getAttachSize() > 0)
					indexCount += 1;

				for (int i=1; i<= indexCount; i++)
				{
					unsigned long index = (i==indexCount) ? attach->getTotal() : i*attach->getAttachSize();
					offlineMessageInfo->m_indexs.insert(index, true);
				}
			}

			gApplication->setAttribute(sendtoUserInfo->getAccount(), mid, offlineMessageInfo);
		}else if (attach->getIndex() == 0)
		{
			long type = request->getParameterValue(_T("Type"), 0);
			bool bNew = request->getParameterValue2(_T("New"), true);

			offlineMessageInfo->type((short)type);
			offlineMessageInfo->newflag(bNew);
			if (type == 3)
			{
				long imageWidth = request->getParameterValue(_T("Width"), 0);
				long imageHeight = request->getParameterValue(_T("Height"), 0);
				offlineMessageInfo->imageWH(imageWidth, imageHeight);
			}
		}
		offlineMessageInfo->setdata((const char*)attach->getAttachData(), attach->getAttachSize(), attach->getIndex());
		offlineMessageInfo->m_indexs.remove(attach->getIndex() + attach->getAttachSize());
		if (!offlineMessageInfo->m_indexs.empty())
		{
			// Already had data.
			return 1;
		}

		gApplication->removeAttribute(sendtoUserInfo->getAccount(), mid);

		switch (offlineMessageInfo->type())
		{
		case 3:
			{
				// Save the image.
				char * filename = new char[50];
#ifdef WIN32
				sprintf(filename, "%I64d%08ld%d", time(0), mid, ++gFileId);
#else
				sprintf(filename, "%ld%08ld%d", time(0), mid, ++gFileId);
#endif
				char filepath[256];
				sprintf(filepath, "%s/File/%s", gApplication->getAppConfPath().c_str(), filename);
				FILE * hfile = fopen(filepath, "wb");
				if (hfile == NULL)
				{
					// System error.
					delete[] filename;
					return -2;
				}
				fwrite(offlineMessageInfo->getdata(), 1, offlineMessageInfo->total(), hfile);
				fclose(hfile);

				offlineMessageInfo->filesize(offlineMessageInfo->total());
				offlineMessageInfo->setdata(filename, strlen(filename));
			}break;
		default:
			break;
		}

		COfflineEvent::pointer offlineEvent = COfflineEvent::create(501, fromInfo, fromAccount->getUserinfo(), offlineMessageInfo);
		gAVSProxy->addOffEvent(sendtoUserInfo, offlineEvent);
		return 2;
	}

	bool ret = false;
	if (attach->getIndex() == 0)
	{
		long nType = request->getParameterValue(_T("Type"), 0);
		bool bNew = request->getParameterValue2(_T("New"), true);

		if (nType == 3)
		{
			long nWidth = request->getParameterValue(_T("Width"), 0);
			long nHeight = request->getParameterValue(_T("Height"), 0);
			ret = sendMsgImage(friendAccountInfo, fromAccount->getUserinfo(), fromInfo, mid, nWidth, nHeight, nType, attach, bNew);
		}else
		{
			ret = sendMsg(friendAccountInfo, fromAccount->getUserinfo(), fromInfo, mid, nType, attach, bNew);
		}
	}else
	{
		ret = sendMsg(friendAccountInfo, fromAccount->getUserinfo(), fromInfo, mid, attach);
	}

	return 0;
}