summaryrefslogtreecommitdiff
path: root/modules/core/hs_main.cpp
blob: c4b1433c83f30703843b47cc16a5c10ea18b8155 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
/* HostServ 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 "hostserv.h"

static BotInfo *HostServ = NULL;

class MyHostServService : public HostServService
{
 public:
	MyHostServService(Module *m) : HostServService(m) { }

	BotInfo *Bot()
	{
		return HostServ;
	}

	void Sync(NickAlias *na)
	{
		if (!na || !na->hostinfo.HasVhost())
			return;
	
		for (std::list<NickAlias *>::iterator it = na->nc->aliases.begin(), it_end = na->nc->aliases.end(); it != it_end; ++it)
		{
			NickAlias *nick = *it;
			nick->hostinfo.SetVhost(na->hostinfo.GetIdent(), na->hostinfo.GetHost(), na->hostinfo.GetCreator());
		}
	}
};

class HostServCore : public Module
{
	MyHostServService myhostserv;

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

		if (!ircd->vhost)
			throw ModuleException("Your IRCd does not suppor vhosts");

		ModuleManager::RegisterService(&this->myhostserv);
		
		HostServ = new BotInfo(Config->s_HostServ, Config->ServiceUser, Config->ServiceHost, Config->desc_HostServ);
		HostServ->SetFlag(BI_CORE);

		Implementation i[] = { I_OnNickIdentify, I_OnNickUpdate };
		ModuleManager::Attach(i, this, 2);

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

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

		delete HostServ;
	}

	void OnNickIdentify(User *u)
	{
		HostInfo *ho = NULL;
		NickAlias *na = findnick(u->nick);
		if (na && na->hostinfo.HasVhost())
			ho = &na->hostinfo;
		else
		{
			na = findnick(u->Account()->display);
			if (na && na->hostinfo.HasVhost())
				ho = &na->hostinfo;
		}
		if (ho == NULL)
			return;

		if (u->vhost.empty() || !u->vhost.equals_cs(na->hostinfo.GetHost()) || (!na->hostinfo.GetIdent().empty() && !u->GetVIdent().equals_cs(na->hostinfo.GetIdent())))
		{
			ircdproto->SendVhost(u, na->hostinfo.GetIdent(), na->hostinfo.GetHost());
			if (ircd->vhost)
			{
				u->vhost = na->hostinfo.GetHost();
				u->UpdateHost();
			}
			if (ircd->vident && !na->hostinfo.GetIdent().empty())
				u->SetVIdent(na->hostinfo.GetIdent());

			if (!na->hostinfo.GetIdent().empty())
				u->SendMessage(HostServ, _("Your vhost of \002%s\002@\002%s\002 is now activated."), na->hostinfo.GetIdent().c_str(), na->hostinfo.GetHost().c_str());
			else
				u->SendMessage(HostServ, _("Your vhost of \002%s\002 is now activated."), na->hostinfo.GetHost().c_str());
		}
	}

	void OnNickUpdate(User *u)
	{
		this->OnNickIdentify(u);
	}
};

MODULE_INIT(HostServCore)