summaryrefslogtreecommitdiff
path: root/src/modules/m_services_account.cpp
blob: bf34a7c820d8ff25daf7ad23e93006435676ba7b (about) (plain) (blame)
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
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
 * See: http://wiki.inspircd.org/Credits
 *
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "inspircd.h"
#include "account.h"

/* $ModDesc: Povides support for accounts. */

class ServicesAccountProvider : public AccountProvider
{
 public:
	StringExtItem ext;
	ServicesAccountProvider(Module* mod)
		: AccountProvider(mod, "account/services_account"), ext("accountname", mod)
	{
	}

	bool IsRegistered(User* user)
	{
		return ext.get(user);
	}

	std::string GetAccountName(User* user)
	{
		std::string* account = ext.get(user);
		if (account)
			return *account;
		return "";
	}

	void DoLogin(User* user, const std::string& acct)
	{
		if (IS_LOCAL(user) && !acct.empty())
			user->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
				user->nick.c_str(), user->GetFullHost().c_str(), acct.c_str(), acct.c_str());

		if (acct.empty())
			ext.unset(user);
		else
			ext.set(user, acct);

		if (user->registered == REG_ALL)
			ServerInstance->PI->SendMetaData(user, "accountname", acct);

		AccountEvent(creator, user, acct).Send();
	}
};

class ModuleServicesAccount : public Module
{
	ServicesAccountProvider account;
 public:
	ModuleServicesAccount() : account(this)
	{
	}

	void init()
	{
		ServerInstance->Modules->AddService(account);
		ServerInstance->Modules->AddService(account.ext);
		Implementation eventlist[] = {
			I_OnWhois, I_OnDecodeMetaData, I_OnSetConnectClass
		};
		ServerInstance->Modules->Attach(eventlist, this, 3);
	}

	/* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
	void OnWhois(User* source, User* dest)
	{
		std::string acctname = account.GetAccountName(dest);

		if (!acctname.empty())
		{
			ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as",
				source->nick.c_str(), dest->nick.c_str(), acctname.c_str());
		}
	}

	// Whenever the linking module receives metadata from another server and doesnt know what
	// to do with it (of course, hence the 'meta') it calls this method, and it is up to each
	// module in turn to figure out if this metadata key belongs to them, and what they want
	// to do with it.
	// In our case we're only sending a single string around, so we just construct a std::string.
	// Some modules will probably get much more complex and format more detailed structs and classes
	// in a textual way for sending over the link.
	void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
	{
		User* dest = IS_USER(target);
		// check if its our metadata key, and its associated with a user
		if (dest && extname == "accountname" && !extdata.empty())
		{
			if (IS_LOCAL(dest))
				dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
					dest->nick.c_str(), dest->GetFullHost().c_str(), extdata.c_str(), extdata.c_str());

			AccountEvent(this, dest, extdata).Send();
		}
	}

	ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
	{
		if (myclass->config->getBool("requireaccount") && !account.IsRegistered(user))
			return MOD_RES_DENY;
		return MOD_RES_PASSTHRU;
	}

	Version GetVersion()
	{
		return Version("Povides support for accounts.",VF_OPTCOMMON|VF_VENDOR);
	}
};

MODULE_INIT(ModuleServicesAccount)