<commit_before>/**
 * Copyright (C) 2014-2015 <NAME>.
 *
 * This license must be preserved.
 * Any applications, libraries, or code which make any use of any
 * component of this program must not be commercial, unless explicit
 * permission is granted from the original author. The use of this
 * program for non-profit purposes is permitted.
 *
 * 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.
 *
 * In the event that this license restricts you from making desired use of this program, contact the original author.
 * Written by <NAME> <<EMAIL>>
 */

#include "Jupiter/IRC_Client.h"
#include "Jupiter/INIFile.h"
#include "RenX_Announcements.h"
#include "RenX_Core.h"
#include "RenX_Server.h"
#include "RenX_Tags.h"

RenX_AnnouncementsPlugin pluginInstance;

void announce_(unsigned int x)
{
	pluginInstance.announce(x);
}

void RenX_AnnouncementsPlugin::announce(unsigned int)
{
	if (RenX_AnnouncementsPlugin::random == false)
	{
		RenX_AnnouncementsPlugin::lastLine++;
		if (RenX_AnnouncementsPlugin::lastLine == RenX_AnnouncementsPlugin::announcementsFile.getLineCount()) RenX_AnnouncementsPlugin::lastLine = 0;
	}
	else
	{
		unsigned int trand;
		do trand = rand() % RenX_AnnouncementsPlugin::announcementsFile.getLineCount();
		while (trand == RenX_AnnouncementsPlugin::lastLine);
		RenX_AnnouncementsPlugin::lastLine = trand;
	}
	Jupiter::StringS announcement = RenX_AnnouncementsPlugin::announcementsFile.getLine(RenX_AnnouncementsPlugin::lastLine);
	RenX::sanitizeTags(announcement);
	
	Jupiter::String msg;
	RenX::Core *core = RenX::getCore();
	RenX::Server *server;
	for (unsigned int i = 0; i != RenX::getCore()->getServerCount(); i++)
	{
		server = core->getServer(i);
		if (server->players.size() != 0)
		{
			msg = announcement;
			RenX::processTags(msg, server);
			server->sendMessage(msg);
		}
	}
}

int RenX_AnnouncementsPlugin::OnRehash()
{
	RenX_AnnouncementsPlugin::announcementsFile.unload();
	return RenX_AnnouncementsPlugin::init();
}

int RenX_AnnouncementsPlugin::init()
{
	RenX_AnnouncementsPlugin::random = Jupiter::IRC::Client::Config->getBool(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Random"));

	RenX_AnnouncementsPlugin::announcementsFile.load(Jupiter::IRC::Client::Config->get(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("File"), STRING_LITERAL_AS_REFERENCE("Announcements.txt")));
	if (RenX_AnnouncementsPlugin::announcementsFile.getLineCount() == 0)
	{
		fputs("[RenX.Announcements] ERROR: No announcements loaded." ENDL, stderr);
		return -1;
	}
	time_t delay = Jupiter::IRC::Client::Config->getInt(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Delay"), 60);
	RenX_AnnouncementsPlugin::timer = new Jupiter::Timer(0, delay, announce_);
	if (RenX_AnnouncementsPlugin::random == false)
		RenX_AnnouncementsPlugin::lastLine = RenX_AnnouncementsPlugin::announcementsFile.getLineCount() - 1;
	return 0;
}

RenX_AnnouncementsPlugin::~RenX_AnnouncementsPlugin()
{
	RenX_AnnouncementsPlugin::timer->kill();
	RenX_AnnouncementsPlugin::announcementsFile.unload();
}

extern "C" __declspec(dllexport) bool load()
{
	return pluginInstance.init() == 0;
}

extern "C" __declspec(dllexport) Jupiter::Plugin *getPlugin()
{
	return &pluginInstance;
}
<commit_msg>Bug fix: Announcements timer is now properly killed when rehashed.<commit_after>/**
 * Copyright (C) 2014-2015 <NAME>.
 *
 * This license must be preserved.
 * Any applications, libraries, or code which make any use of any
 * component of this program must not be commercial, unless explicit
 * permission is granted from the original author. The use of this
 * program for non-profit purposes is permitted.
 *
 * 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.
 *
 * In the event that this license restricts you from making desired use of this program, contact the original author.
 * Written by <NAME> <<EMAIL>>
 */

#include "Jupiter/IRC_Client.h"
#include "Jupiter/INIFile.h"
#include "RenX_Announcements.h"
#include "RenX_Core.h"
#include "RenX_Server.h"
#include "RenX_Tags.h"

RenX_AnnouncementsPlugin pluginInstance;

void announce_(unsigned int x)
{
	pluginInstance.announce(x);
}

void RenX_AnnouncementsPlugin::announce(unsigned int)
{
	if (RenX_AnnouncementsPlugin::random == false)
	{
		RenX_AnnouncementsPlugin::lastLine++;
		if (RenX_AnnouncementsPlugin::lastLine == RenX_AnnouncementsPlugin::announcementsFile.getLineCount()) RenX_AnnouncementsPlugin::lastLine = 0;
	}
	else
	{
		unsigned int trand;
		do trand = rand() % RenX_AnnouncementsPlugin::announcementsFile.getLineCount();
		while (trand == RenX_AnnouncementsPlugin::lastLine);
		RenX_AnnouncementsPlugin::lastLine = trand;
	}
	Jupiter::StringS announcement = RenX_AnnouncementsPlugin::announcementsFile.getLine(RenX_AnnouncementsPlugin::lastLine);
	RenX::sanitizeTags(announcement);
	
	Jupiter::String msg;
	RenX::Core *core = RenX::getCore();
	RenX::Server *server;
	for (unsigned int i = 0; i != RenX::getCore()->getServerCount(); i++)
	{
		server = core->getServer(i);
		if (server->players.size() != 0)
		{
			msg = announcement;
			RenX::processTags(msg, server);
			server->sendMessage(msg);
		}
	}
}

int RenX_AnnouncementsPlugin::OnRehash()
{
	RenX_AnnouncementsPlugin::timer->kill();
	RenX_AnnouncementsPlugin::announcementsFile.unload();
	return RenX_AnnouncementsPlugin::init();
}

int RenX_AnnouncementsPlugin::init()
{
	RenX_AnnouncementsPlugin::random = Jupiter::IRC::Client::Config->getBool(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Random"));

	RenX_AnnouncementsPlugin::announcementsFile.load(Jupiter::IRC::Client::Config->get(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("File"), STRING_LITERAL_AS_REFERENCE("Announcements.txt")));
	if (RenX_AnnouncementsPlugin::announcementsFile.getLineCount() == 0)
	{
		fputs("[RenX.Announcements] ERROR: No announcements loaded." ENDL, stderr);
		return -1;
	}
	time_t delay = Jupiter::IRC::Client::Config->getInt(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Delay"), 60);
	RenX_AnnouncementsPlugin::timer = new Jupiter::Timer(0, delay, announce_);
	if (RenX_AnnouncementsPlugin::random == false)
		RenX_AnnouncementsPlugin::lastLine = RenX_AnnouncementsPlugin::announcementsFile.getLineCount() - 1;
	return 0;
}

RenX_AnnouncementsPlugin::~RenX_AnnouncementsPlugin()
{
	RenX_AnnouncementsPlugin::timer->kill();
	RenX_AnnouncementsPlugin::announcementsFile.unload();
}

extern "C" __declspec(dllexport) bool load()
{
	return pluginInstance.init() == 0;
}

extern "C" __declspec(dllexport) Jupiter::Plugin *getPlugin()
{
	return &pluginInstance;
}

{'fraction_non_alphanumeric': 0.12210556594275852, 'fraction_numerical': 0.005465266791313103, 'mean_word_length': 6.920273348519363, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6707106', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2308, 'n_words': 478}