summaryrefslogtreecommitdiff
path: root/modules/core/gl_main.cpp
blob: f52be08717e1a35332293dc4c417cfcc4df1c17b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Global core functions
 *
 * (C) 2003-2011 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 */

/*************************************************************************/

#include "module.h"
#include "global.h"

static BotInfo *Global = NULL;

class MyGlobalService : public GlobalService
{
	 void ServerGlobal(Server *s, const Anope::string &message)
	 {
	 	if (s != Me && !s->HasFlag(SERVER_JUPED))
			notice_server(Config->s_Global, s, "%s", message.c_str());
		for (unsigned i = 0, j = s->GetLinks().size(); i < j; ++i)
			this->ServerGlobal(s->GetLinks()[i], message);
	 }

 public:
	MyGlobalService(Module *m) : GlobalService(m) { }

	BotInfo *Bot()
	{
		return Global;
	}

	void SendGlobal(BotInfo *sender, const Anope::string &source, const Anope::string &message)
	{
		if (Me->GetLinks().empty())
			return;

		Anope::string rmessage;

		if (!source.empty() && !Config->AnonymousGlobal)
			rmessage = "[" + source + "] " + message;
		else
			rmessage = message;

		this->ServerGlobal(Me->GetLinks().front(), rmessage);
	}
};

class GlobalCore : public Module
{
	MyGlobalService myglobal;

 public:
	GlobalCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE), myglobal(this)
	{
		this->SetAuthor("Anope");

		Implementation i[] = { I_OnRestart, I_OnShutdown, I_OnNewServer };
		ModuleManager::Attach(i, this, 3);

		ModuleManager::RegisterService(&this->myglobal);
		
		Global = new BotInfo(Config->s_Global, Config->ServiceUser, Config->ServiceHost, Config->desc_Global);
		Global->SetFlag(BI_CORE);

		spacesepstream coreModules(Config->GlobalCoreModules);
		Anope::string module;
		while (coreModules.GetToken(module))
			ModuleManager::LoadModule(module, NULL);
	}

	~GlobalCore()
	{
		spacesepstream coreModules(Config->GlobalCoreModules);
		Anope::string module;
		while (coreModules.GetToken(module))
		{
			Module *m = ModuleManager::FindModule(module);
			if (m != NULL)
				ModuleManager::UnloadModule(m, NULL);
		}

		delete Global;
	}

	void OnRestart()
	{
		if (Config->GlobalOnCycle)
			global->SendGlobal(global->Bot(), "", Config->GlobalOnCycleMessage);
	}
	
	void OnShutdown()
	{
		if (Config->GlobalOnCycle)
			global->SendGlobal(global->Bot(), "", Config->GlobalOnCycleMessage);
	}

	void OnNewServer(Server *s)
	{
		if (Config->GlobalOnCycle && !Config->GlobalOnCycleUP.empty())
			notice_server(Config->s_Global, s, "%s", Config->GlobalOnCycleUP.c_str());
	}
};

MODULE_INIT(GlobalCore)