Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}