www.pudn.com > loseserver.rar > GS_Chat.cpp


/* 
Copyright (C) 2004  freeplay.dk 
 
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. 
 
You may contact me for further information on admin@freeplay.dk 
*/ 
 
#define WIN32_LEAN_AND_MEAN 
 
// disable map warnings for std::map 
#pragma warning (disable: 4786) 
#pragma warning (disable: 4788) 
 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include "GS_Chat.h" 
#include "resource.h" 
#include "Logging.h" 
#include "CConnection.h" 
#include "Types.h" 
#include "Database.h" 
#include "ComWindows.h" 
 
#include "LoginMap.h" 
#include "CharMap.h" 
#include "OpCodes.h" 
 
using namespace std; 
 
//******************************************* 
// class defines 
//******************************************* 
extern database logindb; 
extern ComWindows com; 
extern LoginMap dblmap; 
extern charmap chm; 
 
//*********************************************************** 
// Chatting system. (global ect) 
//*********************************************************** 
void GS_Chat::chat(char * chatstring, DWORD ptr) 
{ 
	CConnection* c = reinterpret_cast  (ptr); 
	char			cip[15]; 
	unsigned int	cp = 0; 
	c->PeerInfo (&cip[0], 15, &cp); 
 
	unsigned long userobjx = chm.getObjectid(cp); 
	int16 mylocx = chm.getLocx(cp); 
	int16 mylocy = chm.getLocy(cp); 
 
	switch(chatstring[0]) 
	{ 
		case 0x00: 
			{ 
				int8 opcode = g_sc_chat; 
				int8 type = 0x00; 
				chm.sendtoall(chatstring, cp, opcode, type); 
			} 
		break; 
		case 0x02: 
			{ 
				int8 opcode = g_sc_chat; 
				int8 type = 0x02; 
				chm.sendtoall(chatstring, cp, opcode, type); 
			} 
		break; 
		case 0x03: 
			{ 
				int8 opcode = g_sc_chatglobal; 
				int8 type = 0x03; 
				chm.sendtoall(chatstring, cp, opcode, type); 
			} 
		break; 
		default: 
			{ 
				// dunno 
			} 
		break; 
	} 
} 
 
//*********************************************************** 
// Chatting system (whisper) 
//*********************************************************** 
void GS_Chat::whisper(char * chatstring, DWORD ptr) 
{ 
	CConnection* c = reinterpret_cast  (ptr); 
	char			cip[15]; 
	unsigned int	cp = 0; 
	c->PeerInfo (&cip[0], 15, &cp); 
 
	string us = chm.getCharname(cp); 
	char * charname = strdup(us.c_str()); 
 
	char * name = (char *)&chatstring[0]; 
	int namesize = strlen(name); 
	char * message = (char *)&chatstring[namesize+1]; 
	string nametofind = (string)name; 
 
	DWORD otherptr; 
	otherptr = chm.findUser(cp, nametofind); 
 
	if( otherptr > 0 ) 
	{ 
		// sends a copy to yourself. 
		int8 opcode = g_sc_chatwhisper; 
		int8 type = 0x09; 
		int8 endbyte = 0x00; 
		int size = strlen(message); 
 
		char buf0[120]; 
		char* buf_ptr0 = buf0; 
 
		char* sendername0 = new char[50]; 
		sprintf(sendername0,"-> (%s) ", name); 
		 
		int sendersize0 = strlen(name)+6; 
 
		memcpy(buf_ptr0,(char*)&opcode,1); 
		buf_ptr0+=1; 
		memcpy(buf_ptr0,(char*)&type,1); 
		buf_ptr0+=1; 
 
		memcpy(buf_ptr0, sendername0, sendersize0); 
		buf_ptr0+=sendersize0; 
		memcpy(buf_ptr0, message, size); 
		buf_ptr0+=size; 
		memcpy(buf_ptr0,(char*)&endbyte,1); 
 
		int fullsize0 = strlen(message)+strlen(name)+9; 
		sendpacket( buf0, fullsize0, ptr ); 
 
		// sends to user if he is on. 
		int8 outopcode = g_sc_chatwhisperto; 
 
		char buf1[120]; 
		char* buf_ptr = buf1; 
 
		memcpy(buf_ptr,(char*)&outopcode,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr, charname, strlen(charname)); 
		buf_ptr+=strlen(charname); 
		memcpy(buf_ptr,(char*)&endbyte,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr, message, size); 
		buf_ptr+=size; 
		memcpy(buf_ptr,(char*)&endbyte,1); 
 
		int fullsize = strlen(message)+strlen(charname)+3; 
		sendpacket( buf1, fullsize, otherptr ); 
	} 
	else if( otherptr <= 0) 
	{ 
		// username not found. 
		int8 opcode = g_sc_chatstring; 
		int8 type = 0x49; 
		int8 unk1 = 0x01; 
		int8 endbyte = 0x00; 
 
		unsigned char unk2[] = { 
		0x3C, 0xFE, 0x91, 0xC1 
		}; 
		 
		char buf1[120]; 
		char* buf_ptr = buf1; 
 
		memcpy(buf_ptr,(char*)&opcode,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr,(char*)&type,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr,(char*)&endbyte,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr,(char*)&unk1,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr, name, namesize); 
		buf_ptr+=namesize; 
		memcpy(buf_ptr,(char*)&endbyte,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr,(char*)&unk2,4); 
		buf_ptr+=4; 
 
		int fullsize = strlen(name)+9; 
		sendpacket( buf1, fullsize, ptr ); 
	} 
} 
 
//*********************************************************** 
// Chatting system command (/who "charname"). 
//*********************************************************** 
void GS_Chat::who(char * chatstring, DWORD ptr) 
{ 
	CConnection* c = reinterpret_cast  (ptr); 
	char			cip[15]; 
	unsigned int	cp = 0; 
	c->PeerInfo (&cip[0], 15, &cp); 
 
	char * name = (char *)&chatstring[0]; 
	int namesize = strlen(name); 
	string nametofind = (string)name; 
 
	DWORD user; 
	user = chm.findUser(cp, nametofind); 
 
	if(user > 0) 
	{ 
		CConnection* c1 = reinterpret_cast  (user); 
		char			cip1[15]; 
		unsigned int	cp1 = 0; 
		c1->PeerInfo (&cip1[0], 15, &cp1);	 
 
		string chname = chm.getCharname(cp1); 
		char * charname = strdup(chname.c_str()); 
		string clname = chm.getClanname(cp1); 
		char * claname = strdup(clname.c_str()); 
		string ttle = chm.getTitle(cp1); 
		char * title = strdup(ttle.c_str()); 
		 
		int16 lawful = chm.getLawful(cp1); 
		 
		char * lawfulness; 
		 
		if(lawful < 0) 
		{ 
			lawfulness = "(Chaotic)"; 
		} 
		else if(lawful >= 0 && lawful < 500) 
		{ 
			lawfulness = "(Neutral)"; 
		} 
		else if(lawful >= 500) 
		{ 
			lawfulness = "(Lawful)"; 
		} 
 
		// found 
		int8 opcode = g_sc_chatglobal; 
		int8 type = 0x09; 
		int8 endbyte = 0x00; 
		int8 space = 0x20; 
 
		char buf1[120]; 
		char* buf_ptr = buf1; 
 
		memcpy(buf_ptr,(char*)&opcode,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr,(char*)&type,1); 
		buf_ptr+=1; 
 
		if(strlen(title) > 0) 
		{ 
			memcpy(buf_ptr, title, strlen(title)); 
			buf_ptr+=strlen(title); 
			memcpy(buf_ptr,(char*)&space,1); 
			buf_ptr+=1; 
		} 
		memcpy(buf_ptr, charname, strlen(charname)); 
		buf_ptr+=strlen(charname); 
		memcpy(buf_ptr,(char*)&space,1); 
		buf_ptr+=1; 
		memcpy(buf_ptr, lawfulness, strlen(lawfulness)); 
		buf_ptr+=strlen(lawfulness); 
		 
		char* clanname = new char[14]; 
		if(strlen(claname) > 0) 
		{ 
			sprintf(clanname,"[%s]", claname); 
 
			memcpy(buf_ptr,(char*)&space,1); 
			buf_ptr+=1; 
			memcpy(buf_ptr, clanname, strlen(claname)+2); 
			buf_ptr+=strlen(claname)+2; 
		} 
		memcpy(buf_ptr,(char*)&endbyte,1); 
 
		int fullsize = 1+1+strlen(title)+strlen(charname)+strlen(lawfulness)+2; 
		 
		if(strlen(title) > 0) 
		{ 
			fullsize++; 
		} 
		 
		int totalsize = 0; 
 
		if(strlen(clanname) > 0) 
		{ 
			fullsize++; 
			totalsize = fullsize+strlen(clanname)+2; 
		} 
		 
		sendpacket( buf1, totalsize, ptr ); 
	} 
 
	// not found / also if found 
	int8 endbyte = 0x00; 
	char userson[4]; 
	itoa(chm.amountOnline(), userson, 4); 
 
	char buf2[120]; 
	char* buf_ptr1 = buf2; 
 
	int8 opcode2 = g_sc_chatstring; 
	 
	unsigned char data[] = { 
	0x51, 0x00, 0x01 
	}; 
	 
	memcpy(buf_ptr1,(char*)&opcode2,1); 
	buf_ptr1+=1; 
	memcpy(buf_ptr1,(char*)&data,3); 
	buf_ptr1+=3; 
	memcpy(buf_ptr1, (char*)&userson, strlen(userson)); 
	buf_ptr1+=strlen(userson); 
	memcpy(buf_ptr1,(char*)&endbyte,1); 
		 
	int usersonsize = 4+strlen(userson)+1; 
	sendpacket( buf2, usersonsize, ptr ); 
}