Пример #1
0
bool cConsole::cfFilesPythonScript::operator()()
{
	DIR *dir = opendir(GetPI()->mScriptDir.c_str());
	if (!dir) {
		(*mOS) << autosprintf(_("Failed loading directory: %s"), GetPI()->mScriptDir.c_str());
		return false;
	}
	(*mOS) << autosprintf(_("Python scripts found in: %s"), GetPI()->mScriptDir.c_str()) << "\r\n\r\n";
	(*mOS) << "\t" << _("ID");
	(*mOS) << "\t" << _("Script") << "\r\n";
	(*mOS) << "\t" << string(40, '-') << "\r\n\r\n";
	string filename;
	struct dirent *dent = NULL;
	vector<string> filenames;

	while (NULL != (dent = readdir(dir))) {
		filename = dent->d_name;
		if ((filename.size() > 3) && (StrCompare(filename, filename.size() - 3, 3, ".py") == 0))
			filenames.push_back(filename);
	}
	sort(filenames.begin(), filenames.end());

	for (size_t i = 0; i < filenames.size(); i++)
		(*mOS) << "\t" << i << "\t" << filenames[i] << "\r\n";

	closedir(dir);
	return true;
}
Пример #2
0
bool cConsole::cfDelReplacer::operator ( )()
{
    string word, word_backup;
    GetParStr(1,word_backup);
    cDCProto::UnEscapeChars(word_backup, word);

    bool isInList=false;

    for(int i = 0; i < GetPI()->mReplacer->Size(); i++)
        if((* GetPI()->mReplacer)[i]->mWord == word)
            isInList = true;

    if(!isInList) {
        (*mOS) << autosprintf(_("Word '%s' does not exist."), word_backup.c_str()) << " ";
        return false;
    }

    cReplacerWorker FWord;
    FWord.mWord = word;

    GetPI()->mReplacer->DelReplacer(FWord);
    (*mOS) << autosprintf(_("Word '%s' deleted."), word_backup.c_str()) << " ";

    GetPI()->mReplacer->LoadAll();
    return true;
}
Пример #3
0
void cIPLog::GetLastLogin(const string &who, bool isNick, int limit, ostream &os)
{
	string ip;
	if(isNick)
		os << autosprintf(_("Nick %s has lately been in the hub with the following IP"), who.c_str()) << "\n";
	else
		os << autosprintf(_("IP %s has lately been in the hub with following nicknames"), who.c_str()) << "\n";

	MakeSearchQuery(who, isNick, 1, limit);
	SetBaseTo(&mModel);

	os << "\n ";
	os << setw(25) << setiosflags(ios::left) << toUpper(_("Date"));
	os << (isNick ? "IP" : toUpper(_("Nickname"))) << "\n";
	os << " " << string(25+20,'=') << endl;

	db_iterator it;
	for(it = db_begin(); it != db_end(); ++it) {
		cBanList::Num2Ip(mModel.mIP, ip);
		os << " " << setw(25) << setiosflags(ios::left) << cTime(mModel.mDate,0).AsDate();
		os << (isNick ? ip : mModel.mNick) << endl;
	}

	mQuery.Clear();
}
Пример #4
0
bool cTriggerConsole::CheckData(cfBase *cmd, cTrigger &data)
{
	if(data.mDefinition.empty()) {
		*cmd->mOS << _("The definition is empty or not specified. Please define it with -d option.");
		return false;
	}
	size_t pos = data.mDefinition.rfind("dbconfig");
	if(pos != string::npos) {
		*cmd->mOS << _("It's not allowed to define dbconfig file as trigger.") << "\n";
		cConnDC *conn = (cConnDC *) cmd->mConn;
		ostringstream message;
		message << autosprintf(_("User '%s' tried to define dbconfig as trigger"), conn->mpUser->mNick.c_str());
		mOwner->mServer->ReportUserToOpchat(conn, message.str());
		return false;
	}
	FilterPath(data.mDefinition);
	string vPath(mOwner->mServer->mConfigBaseDir), triggerPath, triggerName;
	ExpandPath(vPath);
	GetPath(data.mDefinition, triggerPath, triggerName);
	ReplaceVarInString(triggerPath, "CFG", triggerPath, vPath);
	ExpandPath(triggerPath);

	if ((triggerPath.substr(0, vPath.length()) != vPath)) {
		(*cmd->mOS) << autosprintf(_("The file %s for the trigger %s must be located in %s configuration folder, use %%[CFG] variable, for example: %%[CFG]/%s"), data.mDefinition.c_str(), data.mCommand.c_str(), HUB_VERSION_NAME, triggerName.c_str());
		return false;
	}

	return true;
}
Пример #5
0
bool cConsole::cfFilesLuaScript::operator()()
{
	DIR *dir = opendir(GetPI()->mScriptDir.c_str());

	if (!dir) {
		(*mOS) << autosprintf(_("Failed loading directory: %s"), GetPI()->mScriptDir.c_str());
		return false;
	}

	(*mOS) << autosprintf(_("Lua scripts found in: %s"), GetPI()->mScriptDir.c_str()) << "\r\n\r\n ";
	(*mOS) << setw(6) << setiosflags(ios::left) << _("ID");
	(*mOS) << toUpper(_("Script")) << "\r\n";
	(*mOS) << " " << string(6 + 20, '=') << "\r\n";
	string filename;
	struct dirent *dent = NULL;
	int i = 0;

	while (NULL != (dent = readdir(dir))) {
		filename = dent->d_name;

		if ((filename.size() > 4) && (StrCompare(filename, filename.size() - 4, 4, ".lua") == 0)) {
			(*mOS) << " " << setw(6) << setiosflags(ios::left) << i << filename << "\r\n";
			i++;
		}
	}

	closedir(dir);
	return true;
}
Пример #6
0
void cServerDC::SendHeaders(cConnDC * conn, int where)
{
	/*
	* 0 = dont send headers
	* 1 = send headers on login
	* 2 = send headers on connection
	*/

	if (((mC.host_header > 2) ? 2 : mC.host_header) == where) {
		ostringstream os;
		cTime runtime;
		runtime -= mStartTime;

		if (mC.extended_welcome_message) {
			mStatus = _("Not available");

			if (mFrequency.mNumFill > 0) {
				if (mSysLoad == eSL_RECOVERY) mStatus = _("Recovery mode");
				else if (mSysLoad == eSL_CAPACITY) mStatus = _("Near capacity");
				else if (mSysLoad == eSL_PROGRESSIVE) mStatus = _("Progressive mode");
				else if (mSysLoad == eSL_NORMAL) mStatus = _("Normal mode");
			}

			os << "<" << mC.hub_security << "> " << autosprintf(_("Running %s %s build %s"), HUB_VERSION_NAME, VERSION, HUB_VERSION_CLASS) << "|";
			os << "<" << mC.hub_security << "> " << autosprintf(_("Runtime: %s"), runtime.AsPeriod().AsString().c_str()) << "|";
			os << "<" << mC.hub_security << "> " << autosprintf(_("User count: %d"), mUserCountTot) << "|";
			os << "<" << mC.hub_security << "> " << autosprintf(_("System status: %s"), mStatus.c_str()) << "|";
			if (!mC.hub_version_special.empty()) os << "<" << mC.hub_security << "> " << mC.hub_version_special << "|";
		} else
			os << "<" << mC.hub_security << "> " << autosprintf(_("Running %s %s build %s%s ][ Runtime: %s ][ User count: %d"), HUB_VERSION_NAME, VERSION, HUB_VERSION_CLASS, mC.hub_version_special.c_str(), runtime.AsPeriod().AsString().c_str(), mUserCountTot) << "|";

		string res = os.str();
		conn->Send(res, false);
	}
}
Пример #7
0
void cIPLog::GetHistory(const string &who, bool isNick, int limit, ostream &os)
{
	string ip;
	if(isNick)
		os << autosprintf(_("Last %d events of nick %s:"), limit, who.c_str()) << "\r\n";
	else
		os << autosprintf(_("Last %d events of IP %s:"), limit, who.c_str()) << "\r\n";

	MakeSearchQuery(who, isNick, -1, limit);
	SetBaseTo(&mModel);

	const char *Actions[]={_("connect"),_("login"),_("logout"),_("disconnect")};
	const char *Infos[]={
	    "--",
		_("bad nick or nick temporarily banned"),
		_("used different nick in chat"),
		_("kicked"),
		_("redirected"),
		_("exit from the hub"),
		_("critical hub load"),
		_("timeout"),
		_("user did nothing for too long time"),
		_("hub full"),
		_("share limit"),
		_("no tag or not valid"),
		_("tag breaks hub rules"),
		_("wrong password"),
		_("error in login sequence"),
		_("syntax error in some messages"),
		_("invalid key")
	};

	os << "\n ";
	os << setw(20) << setiosflags(ios::left) << toUpper(_("Date"));
	os << setw(20) << setiosflags(ios::left) << toUpper(_("Action"));
	os << setw(15) << setiosflags(ios::left) << (isNick ? "IP" : toUpper(_("Nickname")));
	os << toUpper(_("Info")) << "\n";
	os << " " << string(20+20+15+25,'=') << endl;

	db_iterator it;
	for(it = db_begin(); it != db_end(); ++it) {
		cBanList::Num2Ip(mModel.mIP, ip);
		os << " " <<  setw(20) << setiosflags(ios::left) << cTime(mModel.mDate,0).AsDate();
		os << setw(20) << setiosflags(ios::left);
		if(mModel.mType < 4)
			os << Actions[mModel.mType];
		else
			os << mModel.mType;
		os << setw(15) << setiosflags(ios::left) << (isNick ? ip : mModel.mNick.substr(0,14));
		if(mModel.mInfo < 16) {
			if(strlen(Infos[mModel.mInfo]) > 0)
				os << Infos[mModel.mInfo];
		} else
			os << mModel.mInfo;
		os << endl;
	}

	mQuery.Clear();
}
Пример #8
0
bool cConsole::cfReloadLuaScript::operator()()
{
	string scriptfile;
	GetParStr(1, scriptfile);
	bool number = false;
	int num = 0;

	if (GetPI()->IsNumber(scriptfile.c_str())) {
		num = atoi(scriptfile.c_str());
		number = true;
	} else if (scriptfile.find_first_of('/') == string::npos) {
		scriptfile = GetPI()->mScriptDir + scriptfile;
	}

	vector<cLuaInterpreter *>::iterator it;
	cLuaInterpreter *li;
	bool found = false;
	int i = 0;

	for (it = GetPI()->mLua.begin(); it != GetPI()->mLua.end(); ++it, ++i) {
		li = *it;

		if ((number && (num == i)) || (!number && (StrCompare(li->mScriptName, 0, li->mScriptName.size(), scriptfile) == 0))) {
			found = true;
			(*mOS) << autosprintf(_("Script stopped: %s"), li->mScriptName.c_str());
			scriptfile = li->mScriptName;
			delete li;
			GetPI()->mLua.erase(it);
			break;
		}
	}

	if (!found) {
		if (number)
			(*mOS) << autosprintf(_("Script not stopped because it's not loaded: #%d"), num);
		else
			(*mOS) << autosprintf(_("Script not stopped because it's not loaded: %s"), scriptfile.c_str());
	}

	cLuaInterpreter *ip = new cLuaInterpreter(scriptfile);

	if (!ip) {
		(*mOS) << " " << _("Failed to allocate new Lua interpreter.");
		return false;
	}

	if (ip->Init()) {
		(*mOS) << " " << autosprintf(_("Script is now loaded: %s"), scriptfile.c_str());
		GetPI()->AddData(ip);
		ip->Load();
		return true;
	} else {
		(*mOS) << " " << autosprintf(_("Script not found or couldn't be parsed: %s"), scriptfile.c_str());
		delete ip;
		return false;
	}
}
Пример #9
0
ostream &operator << (ostream &os, const cPenaltyList::sPenalty &penalty)
{
	cTime Now = cTime().Sec();

	if ((penalty.mStartChat > Now) && (penalty.mStartPM > Now))
		os << autosprintf(_("Setting main and private chat rights for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStartChat - Now).AsPeriod().AsString().c_str());

	if ((penalty.mStartChat > Now) && (penalty.mStartPM < Now))
		os << autosprintf(_("Setting main chat right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStartChat - Now).AsPeriod().AsString().c_str());

	if ((penalty.mStartPM > Now) && (penalty.mStartChat < Now))
		os << autosprintf(_("Setting private chat right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStartPM - Now).AsPeriod().AsString().c_str());

	if (penalty.mStartSearch > Now)
		os << autosprintf(_("Setting search right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStartSearch - Now).AsPeriod().AsString().c_str());

	if (penalty.mStartCTM > Now)
		os << autosprintf(_("Setting download right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStartCTM - Now).AsPeriod().AsString().c_str());

	if (penalty.mStopKick > Now)
		os << autosprintf(_("Setting kick right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStopKick - Now).AsPeriod().AsString().c_str());

	if (penalty.mStopShare0 > Now)
		os << autosprintf(_("Setting hidden share right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStopShare0 - Now).AsPeriod().AsString().c_str());

	if (penalty.mStopReg > Now)
		os << autosprintf(_("Setting registering right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStopReg - Now).AsPeriod().AsString().c_str());

	if (penalty.mStopOpchat > Now)
		os << autosprintf(_("Setting operator chat right for %s to %s."), penalty.mNick.c_str(), cTime(penalty.mStopOpchat - Now).AsPeriod().AsString().c_str());

	return os;
}
Пример #10
0
bool cConsole::cfAddPerlScript::operator()()
{
	string scriptfile, pathname, filename;
	bool number = false;
	int num = 0;
	GetParStr(1, scriptfile);
	vector<cPerlInterpreter *>::iterator it;
	cPerlInterpreter *pi;

	if (IsNumber(scriptfile.c_str())) {
		num = atoi(scriptfile.c_str());
		number = true;
	}
	cServerDC *server= cServerDC::sCurrentServer;
	pathname = server->mConfigBaseDir;


	if(number) {
		DIR *dir = opendir(pathname.c_str());
		int i = 0;
		if(!dir) {
			(*mOS) << autosprintf(_("Failed loading directory %s."), pathname.c_str()) << " ";
			return false;
		}
		struct dirent *dent = NULL;

		while(NULL != (dent=readdir(dir))) {

			filename = dent->d_name;
			if((filename.size() > 4) && (StrCompare(filename,filename.size()-3,3,".pl")==0)) {
				if(i == num)
					scriptfile = pathname + "/" + filename;
				i++;
			}

		}
		closedir(dir);
	}
	char *argv[] = { (char*)"", (char*)scriptfile.c_str(), NULL };
	for(it = GetPI()->mPerl.mPerl.begin(); it != GetPI()->mPerl.mPerl.end(); ++it) {
		pi = *it;
		if (StrCompare(pi->mScriptName,0,pi->mScriptName.size(),scriptfile)==0) {
			(*mOS) << autosprintf(_("Script %s is already running."), scriptfile.c_str()) << " ";
			return false;
		}
	}
	GetPI()->mPerl.Parse(2, argv);
	(*mOS) << autosprintf(_("Script %s is now running."), scriptfile.c_str()) << " ";
	return true;
}
Пример #11
0
int cForbiddenWorker::DoIt(const string & cmd_line, cConnDC *conn, cServerDC *server, int mask)
{
	string text;

	if(mReason.size()) {
		// User is kick user by hub security
		ostringstream os;
		cUser *OP = server->mUserList.GetUserByNick(server->mC.hub_security);
		server->DCKickNick(&os, OP, conn->mpUser->mNick, mReason, eKCK_Drop | eKCK_Reason | eKCK_PM | eKCK_TBAN);
	}

	// Notify it in opchat
	if (eNOTIFY_OPS & mCheckMask) {

		ostringstream os;
		os << autosprintf(_("FORBID: User sent forbidden words in %s: %s"), (eCHECK_CHAT & mask) ? _("PUBLIC chat") : _("PRIVATE chat"), cmd_line.c_str());
		text = os.str();
		server->ReportUserToOpchat(conn, text, false);

		/* Send to the sender only :) */
		/* Don't display to public if its a PM! Client already did it */
		if(eCHECK_CHAT & mask)
			server->DCPublic(conn->mpUser->mNick, cmd_line, conn);
	}
	return 1;
}
Пример #12
0
		ostream &operator << (ostream &os, cDCClient &tr)
		{
			os << "\t" << tr.mName << "\t\t\t" << tr.mTagID << "\t\t";

 			if ((tr.mMinVersion < 0) && (tr.mMaxVersion < 0))
 				os << _("Any");
 			else if ((tr.mMinVersion >= 0) && (tr.mMaxVersion < 0))
 				os << autosprintf(_("Minimum: %.4f"), tr.mMinVersion);
 			else if ((tr.mMinVersion < 0) && (tr.mMaxVersion >= 0))
				os << autosprintf(_("Maximum: %.4f"), tr.mMaxVersion);
 			else
 				os << autosprintf("%.4f", tr.mMinVersion) << " - " << autosprintf("%.4f", tr.mMaxVersion);

			os << "\t\t\t" << (tr.mBan ? _("Yes") : _("No")) << "\t" << (tr.mEnable ? _("On") : _("Off"));
			return os;
		}
Пример #13
0
bool cConsole::cfErrLuaScript::operator()()
{
	int eclass;

	if (GetParInt(1, eclass)) {
		stringstream ss;
		ss << GetPI()->err_class;
		string oldValue = ss.str();
		ss.str("");
		ss << eclass;
		string newValue = ss.str();
		GetPI()->SetErrClass(eclass);
		(*mOS) << autosprintf(_("Updated configuration %s.%s from '%s' to '%s'."), "pi_lua", "err_class", oldValue.c_str(), newValue.c_str());
	} else {
		(*mOS) << autosprintf(_("Current error class: %d"), GetPI()->err_class);
	}

	return true;
}
Пример #14
0
bool cConsole::cfLogLuaScript::operator()()
{
	int level;

	if (GetParInt(1, level)) {
		stringstream ss;
		ss << GetPI()->log_level;
		string oldValue = ss.str();
		ss.str("");
		ss << level;
		string newValue = ss.str();
		GetPI()->SetLogLevel(level);
		(*mOS) << autosprintf(_("Updated configuration %s.%s from '%s' to '%s'."), "pi_lua", "log_level", oldValue.c_str(), newValue.c_str());
	} else {
		(*mOS) << autosprintf(_("Current log level: %d"), GetPI()->log_level);
	}

	return true;
}
Пример #15
0
bool cConsole::cfMessageRead::operator ( )()
{
	int messages = this->GetMessanger()->mMsgs->CountMessages(((cConnDC*) this->mExtra)->mpUser->mNick, false);
	if (messages) {
		(*mOS) << autosprintf(ngettext("You have %d message in your box.", "You have %d messages in your box.", messages), messages) << "\r\n";
		this->GetMessanger()->mMsgs->PrintSubjects(*mOS, ((cConnDC*) this->mExtra)->mpUser->mNick, false);
	}
	else
		(*mOS) << _("You have no new message in your box.");
	return true;
}
Пример #16
0
ostream & operator << (ostream &os, sMessage &Msg)
{
	cTime date_sent(Msg.mDateSent,0);
	switch (Msg.mPrintType) {
		case sMessage::AS_SUBJECT:
			os << autosprintf(_("From: %s To: %s\nDate: %s\nSubject: %s\n"), Msg.mSender.c_str(), Msg.mReceiver.c_str(), date_sent.AsDate().AsString().c_str(), Msg.mSubject.c_str());
		break;
		case sMessage::AS_BODY:
			os << autosprintf(_("From: %s To: %s\nDate: %s\nSubject: %s\n%s\n----\n"), Msg.mSender.c_str(), Msg.mReceiver.c_str(), date_sent.AsDate().AsString().c_str(), Msg.mSubject.c_str(), Msg.mBody.c_str());
		break;
		case sMessage::AS_DELIVERY:
			os << "\r\n" << autosprintf(_("#OFFLINE MESSAGE# [%s]\nSubject: %s -------------------------\n%s"), date_sent.AsDate().AsString().c_str(), Msg.mSubject.c_str(), Msg.mBody.c_str());
		break;
		case sMessage::AS_ONLINE:
			os << Msg.mBody;
		break;
		default: break;
	}
	return os;
}
Пример #17
0
bool cConsole::cfDelPythonScript::operator()()
{
	if (!GetPI()->online) {
		(*mOS) << _("Python interpreter is not loaded.");
		return false;
	}

	string scriptfile;
	GetParStr(1, scriptfile);
	bool number = false;
	int num = 0;

	if (GetPI()->IsNumber(scriptfile.c_str())) {
		num = atoi(scriptfile.c_str());
		number = true;
	} else if (scriptfile.find_first_of('/') == string::npos) {
		scriptfile = GetPI()->mScriptDir + scriptfile;
	}

	vector<cPythonInterpreter *>::iterator it;
	cPythonInterpreter *li;

	for (it = GetPI()->mPython.begin(); it != GetPI()->mPython.end(); ++it) {
		li = *it;

		if ((number && (num == li->id))
		|| (!number && (StrCompare(li->mScriptName, 0, li->mScriptName.size(), scriptfile) == 0))) {
			(*mOS) << autosprintf(_("Script #%d stopped: %s"), li->id, li->mScriptName.c_str());
			delete li;
			GetPI()->mPython.erase(it);
			return true;
		}
	}

	if (number)
		(*mOS) << autosprintf(_("Script not stopped because it's not loaded: #%d"), num);
	else
		(*mOS) << autosprintf(_("Script not stopped because it's not loaded: %s"), scriptfile.c_str());

	return false;
}
Пример #18
0
bool cConsole::cfReloadPerlScript::operator()()
{
	string scriptfile;
	bool found = false, number = false;
	int i, num;
	i = num = 0;

	GetParStr(1,scriptfile);
	if (IsNumber(scriptfile.c_str())) {
		num = atoi(scriptfile.c_str());
		number = true;
	}

	cPerlInterpreter *pi;
	vector<cPerlInterpreter *>::iterator it;
	for(it = GetPI()->mPerl.mPerl.begin(); it != GetPI()->mPerl.mPerl.end(); ++it, ++i) {
		pi = *it;
		if ((number && num == i) || (!number && StrCompare(pi->mScriptName,0,pi->mScriptName.size(),scriptfile)==0)) {
			found = true;
			(*mOS) << autosprintf(_("Script %s stopped."), pi->mScriptName.c_str()) << " ";
			scriptfile = pi->mScriptName;
			delete pi;
			GetPI()->mPerl.mPerl.erase(it);
			break;
		}
	}

	if(!found) {
		if(number)
			(*mOS) << autosprintf(_("Script #%s not stopped because it is not running."), scriptfile.c_str()) << " ";
		else
			(*mOS) << autosprintf(_("Script %s not stopped  because it is not running."), scriptfile.c_str()) << " ";
		return false;
	} else {
		char *argv[] = { (char*)"", (char*)scriptfile.c_str(), NULL };
		GetPI()->mPerl.Parse(2, argv);
		(*mOS) << autosprintf(_("Script %s is now running."), scriptfile.c_str()) << " ";
		return true;
	}
}
Пример #19
0
void cBan::DisplayComplete(ostream &os)
{
	DisplayUser(os);
	os << " [*] " << setw(PADDING) << setiosflags(ios::left) << _("OP") << mNickOp.c_str() << "\r\n";
	os << " [*] " << setw(PADDING) << setiosflags(ios::left) << _("Ban type") << this->GetBanType() << "\r\n";
	os << " [*] " << setw(PADDING) << setiosflags(ios::left) << _("Last hit");

	if (!mLastHit)
		os << _("Never");
	else
		os << autosprintf(_("%s ago"), cTime(cTime().Sec() - mLastHit).AsPeriod().AsString().c_str());

	os << "\r\n";
}
Пример #20
0
bool cConsole::cfLogPythonScript::operator()()
{
	if (!GetPI()->online) {
		(*mOS) << _("Python interpreter is not loaded.");
		return true;
	}

	string level;
	GetParStr(1, level);
	ostringstream ss;
	ss << cpiPython::log_level;
	string oldValue = ss.str();
	ss.str("");
	ss << level;
	string newValue = ss.str();
	(*mOS) << autosprintf(_("Updated configuration %s.%s: %s -> %s"), "pi_python", "log_level", oldValue.c_str(), newValue.c_str());
	cpiPython::me->LogLevel(atoi(level.c_str()));
	return true;
}
Пример #21
0
int cCommand::sCmdFunc::StringToIntFromList(const string &str, const char *stringlist[], const int intlist[], int item_count)
{
	int theInt = -1;
	int i;

	for(i = 0; i < item_count; i++) {
		if(str == stringlist[i]) {
			theInt = intlist[i];
			return theInt;
		}
	}

	if(theInt == -1) {
		(*mOS) << autosprintf(_("Command suffix %s is not implemented, known suffixes are"), str.c_str()) << ":";
		for(i = 0; i < item_count; i++)
			(*mOS) << " " << stringlist[i];
	}
	return theInt;
}
Пример #22
0
bool cConsole::cfMessageSend::operator ( )()
{
	sMessage msg;
	cUser * receiver;
	msg.mSender = ((cConnDC*)this->mExtra)->mpUser->mNick;
	msg.mDateSent = cTime().Sec();
	msg.mDateExpires = msg.mDateSent + 7 * 24* 3600;
	msg.mSenderIP = ((cConnDC*)this->mExtra)->AddrIP();
	this->GetParStr(1,msg.mReceiver);
	this->GetParStr(2,msg.mSubject);
	this->GetParStr(4,msg.mBody);

	receiver = GetMessanger()->mServer->mUserList.GetUserByNick(msg.mReceiver);
	if ((receiver != NULL) && (receiver->mxConn != NULL)) {
		GetMessanger()->mMsgs->DeliverOnline(receiver, msg);
		(*mOS) << autosprintf(_("The message has been sent to %s because he is online."), msg.mReceiver.c_str());
	} else  {
		GetMessanger()->mMsgs->AddMessage(msg);
		(*mOS) << _("Message saved.");
	}
	return true;
}
Пример #23
0
void cBan::DisplayKick(ostream &os)
{
	os << "\t\t";

	if (mDateEnd) {
		cTime HowLong(mDateEnd - cTime().Sec(), 0);

		if (HowLong.Sec() < 0) {
			os << autosprintf(_("Expired %s"), cTime(mDateEnd, 0).AsDate().AsString().c_str());
		} else {
			os << HowLong.AsPeriod().AsString().c_str();
		}
	} else {
		os << _("Permanent");
	}

	os << "\t\t";

	if (!mLastHit)
		os << _("Never");
	else
		os << cTime(cTime().Sec() - mLastHit).AsPeriod().AsString();
}
Пример #24
0
int cServerDC::ValidateUser(cConnDC *conn, const string &nick, int &closeReason)
{
	// first validate the IP and host if any
	stringstream errmsg,os;
	// Default close reason
	closeReason = eCR_INVALID_USER;
	if (!conn) return 0;
	//time_t n;
	bool close=false;

	// phase 1: test nick validity
	// phase 2: test ip/host ban (registered users pass)
	// phase 3: test nickban
	// then we're done

	static cRegUserInfo *sRegInfo = new cRegUserInfo;
	if ((nick.size() < mC.max_nick * 2 ) && mR->FindRegInfo(*sRegInfo,nick) && !conn->mRegInfo ) {
		conn->mRegInfo = sRegInfo;
		sRegInfo = new cRegUserInfo;
	}

	// Validate nick
	tVAL_NICK vn = ValidateNick(nick, (conn->GetTheoricalClass() >= eUC_REGUSER ));
	if(vn != eVN_OK) {
		close=true;
		if(vn == eVN_BANNED) {
			errmsg << _("Do not reconnect too fast.") << " ";
			closeReason = eCR_RECONNECT;
		}
		else
			errmsg << _("Bad nickname") << ": ";
		if (conn->Log(2))
			conn->LogStream() << "Bad nick: '" << nick << "' (" << vn << ")" << endl;
	}
	switch(vn) {
		case eVN_OK:
		break;
		case eVN_CHARS:
			errmsg << _("Unallowed characters in your nick.");
			if(mC.nick_chars.size())
				 errmsg << autosprintf(_("use these: %s"), mC.nick_chars.c_str());
		break;
		case eVN_SHORT:
			errmsg << _("Your nick is too short.");
		break;
		case eVN_LONG:
			errmsg << _("Your nick is too long.");
		break;
		case eVN_USED: // never happens
			errmsg << _("Your nick is already in use.");
		break;
		case eVN_PREFIX:
			errmsg << autosprintf(_("Invalid nick prefix. Use: %s"), mC.nick_prefix.c_str());
		break;
		case eVN_NOT_REGED_OP:
			errmsg << _("Operator not registered.");
		break;
		case eVN_BANNED:
			errmsg << autosprintf(_("Wait %s before reconnecting."), cTime(mBanList->IsNickTempBanned(nick) - cTime().Sec()).AsPeriod().AsString().c_str());
		break;
		default:
			errmsg << _("Unknown error.");
		break;
	}

	if (close) {
		if (vn == eVN_USED) {
			static string omsg;
			omsg = "$ValidateDenide";
			conn->Send(omsg);
		}

		DCPublicHS(errmsg.str(), conn);
		if (conn->Log(3)) conn->LogStream() << "Bad nick: " << errmsg.str() << endl;
		return 0;
	}

	cBan Ban(this);
	bool banned = false;

	if (conn->GetTheoricalClass() < eUC_MASTER) { // master class is immune
		// here we cant check share ban because user hasnt sent $MyINFO yet
		if (conn->GetTheoricalClass() == eUC_NORMUSER)
			banned = mBanList->TestBan(Ban, conn, nick, eBF_NICK | eBF_NICKIP | eBF_RANGE | eBF_HOST2 | eBF_HOST1 | eBF_HOST3 | eBF_HOSTR1 | eBF_PREFIX);
		else // registered users avoid prefix ban check because we might actually ban a prefix for unregistered users, but let registered users to use it
			banned = mBanList->TestBan(Ban, conn, nick, eBF_NICK | eBF_NICKIP | eBF_RANGE | eBF_HOST2 | eBF_HOST1 | eBF_HOST3 | eBF_HOSTR1);
	}

	if (banned) {
		errmsg << _("You are banned from this hub.") << "\r\n";
		Ban.DisplayUser(errmsg);
		DCPublicHS(errmsg.str(), conn);
		if (conn->Log(1)) conn->LogStream() << "Unallowed user (" << Ban.mType << "), closing" << endl;
		return 0;
	}

	if (mC.nick_prefix_cc) {
		if (conn->mCC.size() && conn->mCC != "--") {
			string Prefix("[");
			Prefix += conn->mCC;
			Prefix += "]";

			if (StrCompare(nick, 0, 4, Prefix) != 0) {
				errmsg << autosprintf(_("Please add %s in front of your nick."), Prefix.c_str());
				close = conn->GetTheoricalClass() < eUC_REGUSER;
			}
		}
	}

	if (close) {
		DCPublicHS(errmsg.str(), conn);
		return 0;
	}

	return 1;
}
Пример #25
0
void cServerDC::DCKickNick(ostream *use_os,cUser *OP, const string &Nick, const string &Reason, int flags)
{
	ostringstream ostr;
	cUser *user = mUserList.GetUserByNick(Nick);
	string NewReason(Reason);
	cKick OldKick;

	// Check if it is possible to kick the user
	if(user && user->mxConn &&
	    (user->mClass + (int) mC.classdif_kick <= OP->mClass) &&
	    (OP->Can(eUR_KICK, mTime.Sec())) &&
		//mClass >= eUC_OPERATOR) &&
	    (OP->mNick != Nick))
	{
		if (user->mProtectFrom < OP->mClass) {
			if(flags & eKCK_Reason) {
				user->mToBan = false;
				// auto kickban
				if(mP.mKickBanPattern.Exec(Reason) >= 0) {
					unsigned u = 0;
					string bantime;
					mP.mKickBanPattern.Extract(1,Reason,bantime);
					if(bantime.size()) {
						ostringstream os;
						if(!(u=Str2Period(bantime,os))) DCPublicHS(os.str(),OP->mxConn);
					}
					if (u > mC.tban_max)
						u = mC.tban_max;
					if (
						(!u && OP->Can(eUR_PBAN, this->mTime)) ||
						((u > 0) &&
						 ((u > mC.tban_kick) && OP->Can(eUR_TBAN, this->mTime)) ||
						 (u <= mC.tban_kick)
						)
					  )
						user->mToBan = true;

					user->mBanTime = u;
					if ( mC.msg_replace_ban.size())
						mP.mKickBanPattern.Replace(0, NewReason, mC.msg_replace_ban);
				}
				mKickList->AddKick(user->mxConn, OP->mNick, &Reason, OldKick);

				if(Reason.size()) {
					string omsg;
					ostr << OP->mNick << " is kicking " << Nick << " because: " << NewReason;
					omsg = ostr.str();
					if(!mC.hide_all_kicks && !OP->mHideKick)
						SendToAll(omsg, OP->mHideKicksForClass ,int(eUC_MASTER));

					if(flags & eKCK_PM) {
						ostr.str(mEmpty);
						ostr << autosprintf(_("You have been kicked because %s"), NewReason.c_str());
						DCPrivateHS(ostr.str(), user->mxConn, &OP->mNick);
					}
				}
			}

			if(flags & eKCK_Drop) {
				// Send the message to the kicker
				ostr.str(mEmpty);
				if(flags & eKCK_TBAN)
					ostr << autosprintf(_("Kicked user %s"), Nick.c_str());
				else
					ostr << autosprintf(_("Dropped user %s"), Nick.c_str());
				ostr << " (IP: " << user->mxConn->AddrIP();
				if(user->mxConn->AddrHost().length())
					ostr << ", host: " << user->mxConn->AddrHost();
				ostr << ")";

				if(user->mxConn->Log(2))
					user->mxConn->LogStream() << "Kicked by " << OP->mNick << ", ban " << mC.tban_kick << "s"<< endl;
				if(OP->Log(3))
					OP->LogStream() << "Kicking " << Nick << endl;

				bool Disconnect = true;
				mKickList->AddKick(user->mxConn, OP->mNick, NULL, OldKick);
				if (OldKick.mReason.size()) {
					#ifndef WITHOUT_PLUGINS
					Disconnect = mCallBacks.mOnOperatorKicks.CallAll(OP, user, &OldKick.mReason);
					#endif
				} else {
					#ifndef WITHOUT_PLUGINS
					Disconnect = mCallBacks.mOnOperatorDrops.CallAll(OP, user);
					#endif
				}
				if (Disconnect) {
					user->mxConn->CloseNice(1000, eCR_KICKED);
					if (!(flags & eKCK_TBAN)) {
						string msg(OP->mNick);
						msg += " ";
						msg += _("dropped");
						ReportUserToOpchat(user->mxConn,msg, mC.dest_drop_chat);
					}
				} else
					ostr << "\n" << _("User cannot be kicked.");

				// temp ban kicked user
				if (flags & eKCK_TBAN) {
					cBan Ban(this);
					cKick Kick;

					mKickList->FindKick(Kick, user->mNick, OP->mNick, 30, true, true);

					if(user->mToBan) {
						mBanList->NewBan(Ban, Kick, user->mBanTime, eBF_NICKIP);
						ostr << " " << _("and banned") << " ";
						Ban.DisplayKick(ostr);
					} else {
						mBanList->NewBan(Ban, Kick, mC.tban_kick, eBF_NICKIP);
						ostr << " " << _("and banned") << " ";
						Ban.DisplayKick(ostr);
					}
					mBanList->AddBan(Ban);
				}
				if (!use_os)
					DCPublicHS(ostr.str(),OP->mxConn);
				else
					(*use_os) << ostr.str();
			}
		} else if(flags & eKCK_Drop) {
			ostr.str(mEmpty);
			ostr << autosprintf(_("Error kicking user %s because he is protected against all classes below %d"), Nick.c_str(), user->mProtectFrom);
			DCPublicHS(ostr.str(),OP->mxConn);
		}
	}
}
Пример #26
0
bool cConsole::cfAddPythonScript::operator()()
{
	if (!GetPI()->online) {
		(*mOS) << _("Python interpreter is not loaded.");
		return false;
	}

	string scriptfile, filename;
	GetParStr(1, scriptfile);
	bool number = false;
	int num = 0;

	if (GetPI()->IsNumber(scriptfile.c_str())) {
		num = atoi(scriptfile.c_str());
		number = true;
	} else if (scriptfile.find_first_of('/') == string::npos) {
		scriptfile = GetPI()->mScriptDir + scriptfile;
	}

	if (number) {
		DIR *dir = opendir(GetPI()->mScriptDir.c_str());
		if (!dir) {
			(*mOS) << autosprintf(_("Failed loading directory: %s"), GetPI()->mScriptDir.c_str());
			return false;
		}
		string filename;
		struct dirent *dent = NULL;
		vector<string> filenames;

		while (NULL != (dent = readdir(dir))) {
			filename = dent->d_name;
			if ((filename.size() > 3) && (StrCompare(filename, filename.size() - 3, 3, ".py") == 0))
				filenames.push_back(filename);
		}
		closedir(dir);
		sort(filenames.begin(), filenames.end());

		if (num < 0 || (unsigned)num >= filenames.size()) {
			(*mOS) << autosprintf(_("Number %d is out of range. Get the right number using !pyfiles command or specify the script path instead."), num);
			return false;
		}
		scriptfile = GetPI()->mScriptDir + filenames[num];
	}

	vector<cPythonInterpreter *>::iterator it;
	cPythonInterpreter *li;

	for (it = GetPI()->mPython.begin(); it != GetPI()->mPython.end(); ++it) {
		li = *it;

		if (StrCompare(li->mScriptName, 0, li->mScriptName.size(), scriptfile) == 0) {
			(*mOS) << autosprintf(_("Script #%d is already loaded: %s"), li->id, scriptfile.c_str());
			return false;
		}
	}

	cPythonInterpreter *ip = new cPythonInterpreter(scriptfile);

	if (!ip) {
		(*mOS) << _("Failed to allocate new Python interpreter.");
		return false;
	}

	GetPI()->AddData(ip);
	if (ip->Init()) {
		(*mOS) << autosprintf(_("Script #%d is now loaded: %s"), ip->id, scriptfile.c_str());
		return true;
	} else {
		(*mOS) << autosprintf(_("Script not found or couldn't be parsed: %s"), scriptfile.c_str());
		GetPI()->RemoveByName(ip->mScriptName);
		return false;
	}
}
Пример #27
0
int cServerDC::DoRegisterInHublist(string host, int port, string NickForReply)
{
	ostringstream os, os2;
	char pipe='|';
#ifndef _WIN32
	unsigned long long buf = GetTotalShareSize();
#else
	char buf[32];
	sprintf(buf,"%ui64d",GetTotalShareSize());
#endif

	istringstream is(host);
	string CurHost;
	string lock, key;
	size_t pos_space;
	cAsyncConn *pHubList;

	os2 << _("Hublist registration results:") << "\n";
	while (CurHost = "", is >> CurHost, CurHost.size() > 0) {
		os2 << autosprintf(_("Sending to %s:%d..."), CurHost.c_str(), port);
		pHubList = new cAsyncConn(CurHost,port);

		if(!pHubList->ok) {
			os2 << " " << _("connection failed") << "\n";
			pHubList->Close();
			delete pHubList;
			pHubList = 0;
			continue;
		}

		key = "";
		pHubList->SetLineToRead(&lock,pipe,1024);
		pHubList->ReadAll();
		pHubList->ReadLineLocal();
		if(lock.size() > 6) {
			pos_space = lock.find(' ',6);
			if (pos_space != lock.npos) pos_space -= 6;
			lock = lock.substr(6,pos_space);
			cDCProto::Lock2Key(lock, key);
		}

		// Create the registration string
		os.str(mEmpty);
		os << "$Key " << key << pipe <<  mC.hub_name // removed pipe before name
			<< pipe << mC.hub_host
			<< pipe;
		__int64 hl_minshare = mC.min_share;
		if (mC.min_share_use_hub > hl_minshare)
			hl_minshare = mC.min_share_use_hub;
		if (mC.hublist_send_minshare)
			os << "[MINSHARE:" << StringFrom(hl_minshare) << "MB] ";
		os << mC.hub_desc
			<< pipe << mUserList.Size()
			<< pipe << buf
			<< pipe;


		// send it
		if(Log(2))
			LogStream() << os.str() << endl;
		pHubList->Write(os.str(), true);
		if(!pHubList->ok)
			os2 << " " << _("error sending info") << endl;
		pHubList->Close();
		delete pHubList;
		pHubList = NULL;
		os2 << " " << _(".. OK") << "\n";
	}

	os2 << _("Done");
	CurHost = os2.str();
	if (NickForReply.size() > 0) {
		cUser * user = mUserList.GetUserByNick(NickForReply);
		if(user && user->mxConn)
			DCPublicHS(CurHost, user->mxConn);
	}
	return 1;
}
Пример #28
0
bool cConsole::cfReloadPythonScript::operator()()
{
	if (!GetPI()->online) {
		(*mOS) << _("Python interpreter is not loaded.");
		return false;
	}

	string scriptfile;
	GetParStr(1, scriptfile);
	bool number = false;
	int num = 0;
	int position = 0;

	if (GetPI()->IsNumber(scriptfile.c_str())) {
		num = atoi(scriptfile.c_str());
		number = true;
	} else if (scriptfile.find_first_of('/') == string::npos) {
		scriptfile = GetPI()->mScriptDir + scriptfile;
	}

	vector<cPythonInterpreter *>::iterator it;
	cPythonInterpreter *li;
	bool found = false;

	for (it = GetPI()->mPython.begin(); it != GetPI()->mPython.end(); ++it, ++position) {
		li = *it;

		if ((number && (num == li->id))
		|| (!number && (StrCompare(li->mScriptName, 0, li->mScriptName.size(), scriptfile) == 0))) {
			found = true;
			scriptfile = li->mScriptName;
			(*mOS) << autosprintf(_("Script #%d stopped: %s"), li->id, li->mScriptName.c_str());
			delete li;
			GetPI()->mPython.erase(it);
			break;
		}
	}

	if (!found) {
		if (number)
			(*mOS) << autosprintf(_("Script not stopped because it's not loaded: #%d"), num);
		else
			(*mOS) << autosprintf(_("Script not stopped because it's not loaded: %s"), scriptfile.c_str());
	}

	cPythonInterpreter *ip = new cPythonInterpreter(scriptfile);

	if (!ip) {
		(*mOS) << " " << _("Failed to allocate new Python interpreter.");
		return false;
	}

	GetPI()->AddData(ip, position);
	if (ip->Init()) {
		(*mOS) << " " << autosprintf(_("Script #%d is now loaded: %s"), ip->id, scriptfile.c_str());
		return true;
	} else {
		(*mOS) << " " << autosprintf(_("Script not found or couldn't be parsed: %s"), scriptfile.c_str());
		GetPI()->RemoveByName(ip->mScriptName);
		return false;
	}
}
Пример #29
0
std::ostream & operator<< (std::ostream &os, const cTime &t)
{
	#ifdef _WIN32
		static char *buf;
	#else
		#define CTIME_BUFFSIZE 26
		static char buf[CTIME_BUFFSIZE + 1];
	#endif

	long n, rest, i;
	ostringstream ostr;

	switch (t.mPrintType) {
		case 1:
			#ifdef _WIN32
				buf = ctime((const time_t*) & (t.tv_sec));
			#else
				strftime(buf, CTIME_BUFFSIZE + 1, "%Y-%m-%d %H:%M:%S", localtime((const time_t*) & (t.tv_sec)));
			#endif

			buf[strlen(buf)] = 0;
			os << buf;
			break;
		case 2:
			rest = t.tv_sec;
			i = 0;
			n = rest / (24 * 3600 * 7);
			rest %= (24 * 3600 * 7);

			if ((n > 0) && (++i <= 2))
				ostr << " " << autosprintf(ngettext("%ld week", "%ld weeks", n), n);

			n = rest / (24 * 3600);
			rest %= (24 * 3600);

			if ((n > 0) && (++i <= 2))
				ostr << " " << autosprintf(ngettext("%ld day", "%ld days", n), n);

			n = rest / 3600;
			rest %= 3600;

			if ((n > 0) && (++i <= 2))
				ostr << " " << autosprintf(ngettext("%ld hour", "%ld hours", n), n);

			n = rest / 60;
			rest %= 60;

			if ((n > 0) && (++i <= 2))
				ostr << " " << autosprintf(ngettext("%ld min", "%ld mins", n), n);

			n = rest;
			rest = 0;

			if ((n > 0) && (++i <= 2))
				ostr << " " << autosprintf(ngettext("%ld sec", "%ld secs", n), n);

			n = (int)t.tv_usec / 1000;

			if ((n > 0) && (++i <= 2))
				ostr << " " << autosprintf(ngettext("%ld msec", "%ld msecs", n), n);

			//n = (int)t.tv_usec % 1000;

			//if ((n > 0) && (++i <= 2))
				//ostr << " " << autosprintf(ngettext("%ld usec", "%ld usecs", n), n);

			if (ostr.str().empty())
				os << autosprintf(_("%d msecs"), 0); // _("%d usecs")
			else
				os << ostr.str().substr(1); // strip space

			break;
		default:
			os << " " << autosprintf(ngettext("%ld sec", "%ld secs", t.tv_sec), t.tv_sec) << " " << autosprintf(ngettext("%ld usec", "%ld usecs", t.tv_usec), t.tv_usec);
			break;
	}

	return os;
};
Пример #30
0
bool cConsole::cfAddLuaScript::operator()()
{
	string scriptfile;
	GetParStr(1, scriptfile);
	bool number = false;
	int num = 0;

	if (GetPI()->IsNumber(scriptfile.c_str())) {
		num = atoi(scriptfile.c_str());
		number = true;
	} else if (scriptfile.find_first_of('/') == string::npos) {
		scriptfile = GetPI()->mScriptDir + scriptfile;
	}

	if (number) {
		DIR *dir = opendir(GetPI()->mScriptDir.c_str());

		if (!dir) {
			(*mOS) << autosprintf(_("Failed loading directory: %s"), GetPI()->mScriptDir.c_str());
			return false;
		}

		string filename;
		struct dirent *dent = NULL;
		int i = 0;

		while (NULL != (dent = readdir(dir))) {
			filename = dent->d_name;

			if ((filename.size() > 4) && (StrCompare(filename, filename.size() - 4, 4, ".lua") == 0)) {
				if (i == num) {
					scriptfile = GetPI()->mScriptDir + filename;
					break;
				}

				i++;
			}
		}

		closedir(dir);
	}

	vector<cLuaInterpreter *>::iterator it;
	cLuaInterpreter *li;

	for (it = GetPI()->mLua.begin(); it != GetPI()->mLua.end(); ++it) {
		li = *it;

		if (StrCompare(li->mScriptName, 0, li->mScriptName.size(), scriptfile) == 0) {
			(*mOS) << autosprintf(_("Script is already loaded: %s"), scriptfile.c_str());
			return false;
		}
	}

	cLuaInterpreter *ip = new cLuaInterpreter(scriptfile);

	if (!ip) {
		(*mOS) << _("Failed to allocate new Lua interpreter.");
		return false;
	}

	if (ip->Init()) {
		(*mOS) << autosprintf(_("Script is now loaded: %s"), scriptfile.c_str());
		GetPI()->AddData(ip);
		ip->Load();
		return true;
	} else {
		(*mOS) << autosprintf(_("Script not found or couldn't be parsed: %s"), scriptfile.c_str());
		delete ip;
		return false;
	}
}