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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* InspIRCd: (C) 2002-2011 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 "protocol.h"
#include "account.h"
/* $ModDesc: Povides support for accounts. */
struct AccountItem
{
irc::string account;
std::string tag;
AccountItem(const irc::string& a) : account(a) {}
AccountItem(const irc::string& a, const std::string& t) : account(a), tag(t) {}
std::string metastr()
{
if (!this)
return "";
if (tag.empty())
return account;
return std::string(account) + " " + tag;
}
};
class ServicesExtItem : public SimpleExtItem<AccountItem>
{
public:
ServicesExtItem(Module* owner) : SimpleExtItem<AccountItem>(EXTENSIBLE_USER, "accountname", owner) {}
std::string serialize(SerializeFormat format, const Extensible* container, void* itemv) const
{
return static_cast<AccountItem*>(itemv)->metastr();
}
void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
{
std::string::size_type spc = value.find(' ');
if (value.empty())
unset(container);
else if (spc == std::string::npos)
set(container, new AccountItem(value));
else
set(container, new AccountItem(value.substr(0, spc), value.substr(spc + 1)));
}
};
class ServicesAccountProvider : public AccountProvider
{
public:
ServicesExtItem ext;
ServicesAccountProvider(Module* mod)
: AccountProvider(mod, "account/services_account"), ext(mod)
{
}
bool IsRegistered(User* user)
{
return ext.get(user);
}
irc::string GetAccountName(User* user)
{
AccountItem* account = ext.get(user);
if (account)
return account->account;
return "";
}
std::string GetAccountTag(User* user)
{
AccountItem* account = ext.get(user);
if (account)
return account->tag;
return "";
}
void DoLogin(User* user, const irc::string& acct, const std::string& tag)
{
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());
AccountItem* item = NULL;
if (acct.empty())
{
ext.unset(user);
}
else
{
item = new AccountItem(acct, tag);
ext.set(user, item);
}
if (user->registered == REG_ALL)
ServerInstance->PI->SendMetaData(user, "accountname", item->metastr());
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, sizeof(eventlist)/sizeof(Implementation));
}
/* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
void OnWhois(User* source, User* dest)
{
irc::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")
{
irc::string acct = account.GetAccountName(dest);
if (IS_LOCAL(dest) && !acct.empty())
dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
dest->nick.c_str(), dest->GetFullHost().c_str(), acct.c_str(), acct.c_str());
AccountEvent(this, dest, acct).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)
|