Example #1
0
static enum v7_err jsBotLoadFile(struct v7* v7, v7_val_t* res) {
	v7_val_t file = v7_arg(v7, 0);
	if (!v7_is_string(file)) {
		return V7_OK;
	}
	TString fullPath = _activeBot->getWorkDir();
	TString fileName = v7_to_cstring(v7, &file);
	fullPath += fileName.GetAsWChar();
	if (!fexists(fullPath)) {
		return V7_OK;
	}
	v7_val_t result;
	enum v7_err ress = V7_OK;
	ress = v7_exec_file(v7, fullPath.GetAsChar(), &result);

	//v7_val_t* v7Obj = _activeBot->getV7Obj();
	//*v7Obj = v7_mk_object(v7);

	if (ress != V7_OK) {
		if (result == V7_SYNTAX_ERROR) MessageBox(NULL, "script fail syntax error", "Nooo", 0);
		else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0);
		else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0);
		else if (result == V7_AST_TOO_LARGE) MessageBox(NULL, "script fail, ast too large", "Nooo", 0);
	}

	return V7_OK;
}
Example #2
0
void Bot::onIm(TString& nick, TString& text) {
	v7_val_t theNick = v7_mk_string(v7, nick.GetAsChar(), ~0, 1);
	v7_val_t theText = v7_mk_string(v7, text.GetAsChar(), ~0, 1);
	v7_val_t* func;
	v7_val_t args;

	long i;
	long l = _onMotd.GetLength();

	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onIm.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, theNick);
		v7_array_push(v7, args, theText);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Example #3
0
inline bool EnsureDirExists(const wchar_t* fullDirPath)
{
	HWND hwnd = NULL;
	const SECURITY_ATTRIBUTES *psa = NULL;
	TString dir = fullDirPath;
	int retval = SHCreateDirectoryEx(hwnd, dir.GetAsChar(), psa);
	if (retval == ERROR_SUCCESS || retval == ERROR_FILE_EXISTS || retval == ERROR_ALREADY_EXISTS) return true;

	return false;
}
Example #4
0
void Bot::say(TString& text, TString& font, unsigned char fontSize, TString& color) {

	if (font.IsEQ(L"")) font = _charset.font;
	if (fontSize < 10) fontSize = _charset.size;
	COLORREF coll = _charset.color;
	if (!color.IsEQ(L"")) coll = GetCol(color.GetAsWChar());

	bot_exchange_format p(PLUGIN_EVENT_ROOM_TEXT);
	p << bot_value(1, text.GetAsChar());
	p << bot_value(2, _charset.attributes);
	p << bot_value(3, fontSize);
	p << bot_value(4, coll);
	p << bot_value(5, _charset.effect);
	p << bot_value(6, _charset.charset);
	p << bot_value(7, _charset.pitch);
	p << bot_value(8, font.GetAsChar());

	std::string d = p.data();

	_mgr->deliver_event(_name.GetAsChar(), d.c_str(), (int)d.size());
}
Example #5
0
void Bot::onMotd(TString& motd) {
	v7_val_t theMotd = v7_mk_string(v7, motd.GetAsChar(), ~0, 1);
	v7_val_t* func;
	v7_val_t args;

	long i;
	long l = _onMotd.GetLength();

	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onMotd.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, theMotd);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Example #6
0
void Bot::onLeft(TString& nick) {
	long l = _onLeft.GetLength();
	if (l == 0) return;

	long i;
	v7_val_t* func;
	v7_val_t args;
	v7_val_t userName = v7_mk_string(v7, nick.GetAsChar(), ~0, 1);
	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onLeft.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, userName);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Example #7
0
void Bot::onTalk(TString& nick, unsigned char flag) {
	long l = _onTalk.GetLength();
	if (l == 0) return;

	long i;
	v7_val_t* func;
	v7_val_t args;
	v7_val_t userName = v7_mk_string(v7, nick.GetAsChar(), ~0, 1);
	v7_val_t theFlag = v7_mk_number(flag);

	for (i = 0; i < l; i++) {
		func = (v7_val_t*)_onTalk.GetAt(i);
		args = v7_mk_array(v7);
		v7_array_push(v7, args, userName);
		v7_array_push(v7, args, theFlag);

		v7_apply(v7, *func, *v7Obj, args, NULL);
	}
}
Example #8
0
void Bot::setFontStyle(TString& fontName, int fontSize, COLORREF fontColor) {
	if (!fontName.IsEQ(L"")) memcpy(_charset.font, fontName.GetAsChar(), fontName.GetLength() * sizeof(char));
	if (fontSize != NULL) _charset.size = fontSize;
	if (fontColor != NULL) _charset.color = fontColor;
}
Example #9
0
bool fexists(TString& fileName) {
	std::ifstream ifile(fileName.GetAsChar());
	return (bool)ifile;
}
Example #10
0
Bot::Bot(bot_manager* mgr, const char* name, const char* workDir) {

	_mgr = mgr;
	_name = name;
	/*_workDir = std::string(ExePath()).c_str();
	_workDir += L"\\plugins\\BMP\\";
	TString botNamee = name;
	botNamee.ToUpper();
	_workDir += botNamee.GetAsWChar();

	MessageBox(NULL, _workDir.GetAsChar(), "Work Dir", 0);*/

	setActive();

	_workDir = workDir;
	_workDir += L"bmp\\";

	bool isScriptOk = true;
	if (!EnsureDirExists(_workDir.GetAsWChar())) {
		isScriptOk = false;
	}

	_charset.color = RGB(0xff, 0x99, 0x00);
	_activeBot = this;

	_scriptFile = _workDir.GetAsWChar();
	_scriptFile += L"main.js";

	if (isScriptOk && !FileExists(std::string(_scriptFile.GetAsChar()))) {
		TString strSay = L"BMP Plugin will not work, please put file 'main.js' into work dir. (";
		strSay += _scriptFile.GetAsWChar();
		strSay += L")";
		//say(strSay);
		MessageBox(NULL, strSay.GetAsChar(), "BMP Bot Alert", 0);
		isScriptOk = false;
	}

	v7 = v7_create();
	enum v7_err rcode = V7_OK;
	v7_val_t result;

	//events function define
	v7_set_method(v7, v7_get_global(v7), "on", (v7_cfunction_t*)&jsBotEvent);

	//utilities functions
	v7_set_method(v7, v7_get_global(v7), "say", (v7_cfunction_t*)&jsBotSay);
	v7_set_method(v7, v7_get_global(v7), "setTimeout", (v7_cfunction_t*)&jsSetTimeout);
	v7_set_method(v7, v7_get_global(v7), "setInterval", (v7_cfunction_t*)&jsSetInterval);
	v7_set_method(v7, v7_get_global(v7), "clearTimeout", (v7_cfunction_t*)&jsClearTimeout);
	v7_set_method(v7, v7_get_global(v7), "clearInterval", (v7_cfunction_t*)&jsClearInterval);

	botObj = new v7_val_t;
	v7_own(v7, botObj);
	*botObj = v7_mk_object(v7);
	v7_val_t botName = v7_mk_string(v7, name, ~0, 1);
	v7_set(v7, *botObj, "name", ~0, botName);
	v7_set(v7, v7_get_global(v7), "BOT", ~0, *botObj);
	v7_set_method(v7, *botObj, "setFontStyle", (v7_cfunction_t*)&jsBotFontStyle);
	v7_set_method(v7, *botObj, "load", (v7_cfunction_t*)&jsBotLoadFile);

	//rcode = v7_exec(v7, "var a = \"aaa\"; say(a); print(\"Yeah\");", &result);
	if (isScriptOk) {
		rcode = v7_exec_file(v7, _scriptFile.GetAsChar(), &result);

		if (rcode != V7_OK) {
			if (result == V7_SYNTAX_ERROR) MessageBox(NULL, "script fail syntax error", "Nooo", 0);
			else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0);
			else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0);
			else if (result == V7_AST_TOO_LARGE) MessageBox(NULL, "script fail, ast too large", "Nooo", 0);
		}
		else {
			v7_val_t vObj;
			v7_own(v7, &vObj);
			vObj = v7_mk_object(v7);
			v7Obj = &vObj;
		}
	}

}