-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
account.h
431 lines (378 loc) · 13.1 KB
/
account.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* +------------------------------------+
* | 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.
*
* ---------------------------------------------------
*/
#ifndef ACCOUNT_H
#define ACCOUNT_H
class AccountEvent : public Event
{
public:
User* const user;
const irc::string account;
AccountEvent(Module* me, User* u, const irc::string& name)
: Event(me, "account_login"), user(u), account(name)
{
}
};
/** AccountProvider: use dynamic_reference<AccountProvider> acct("account") to access. */
class AccountProvider : public DataProvider
{
public:
AccountProvider(Module* mod, const std::string& Name) : DataProvider(mod, Name) {}
/** Is the user registered? */
virtual bool IsRegistered(User* user) = 0;
/**
* Get the account name that a user is using
* @param user The user to query
* @return The account name, or "" if not logged in
*/
virtual irc::string GetAccountName(User* user) = 0;
/**
* Log the user in to an account.
*
* @param user The user to log in
* @param name The account name to log them in with. Empty to log out.
* @param tag A hidden tag on the account, for recording freshness or login method
*/
virtual void DoLogin(User* user, const irc::string& name, const std::string& tag = "") = 0;
};
class AccountDBEntry : public Extensible
{
public:
const irc::string name;
const time_t ts;
time_t hash_password_ts;
std::string hash, password;
AccountDBEntry(const irc::string& nameref, time_t ourTS, std::string h = "", std::string p = "", time_t h_p_ts = 0, std::string cc = "", time_t cc_ts = 0) : Extensible(EXTENSIBLE_ACCOUNT), name(nameref), ts(ourTS), hash_password_ts(h_p_ts), hash(h), password(p)
{
}
virtual CullResult cull() = 0;
virtual ~AccountDBEntry() {}
};
typedef std::map<irc::string, AccountDBEntry*> AccountDB;
class AccountDBModifiedEvent : public Event
{
public:
const irc::string name;
const AccountDBEntry* const entry;
/**
* Create an event indicating that an account in the database has been modified
* The AccountDBProvider Send functions will automatically send this event
* @param me A pointer to the module creating the event
* @param acctname The name of the account that was modified
* @param ent A pointer to the account that was modified, or NULL if the account was deleted
*/
AccountDBModifiedEvent(Module* me, const irc::string& acctname, const AccountDBEntry* ent)
: Event(me, "accountdb_modified"), name(acctname), entry(ent)
{
}
};
class GetAccountByAliasEvent : public Event
{
public:
const irc::string account;
AccountDBEntry* entry;
time_t alias_ts;
void (*RemoveAliasImpl)(const irc::string&);
GetAccountByAliasEvent(Module* me, const irc::string& name)
: Event(me, "get_account_by_alias"), account(name), entry(NULL), alias_ts(0), RemoveAliasImpl(NULL)
{
Send();
}
inline void RemoveAlias()
{
RemoveAliasImpl(account);
}
};
class AccountDBProvider : public DataProvider
{
public:
AccountDBProvider(Module* mod) : DataProvider(mod, "accountdb") {}
/**
* Create an account and add it to the database
* @param send Whether or not to send the account immediately after adding it, if adding was successful
* @param nameref The name of the account to add
* @param ourTS The creation TS of the account to add
* @param h The hash type of the account to add
* @param p The password of the account to add
* @param h_p_ts The hash/password TS of the account to add
* @param cc The connect class of the account to add
* @param cc_ts The TS of the connect class of the account to add
* @return A pointer to the new account if it was successfully added, NULL if an account with the same name already existed
*/
virtual AccountDBEntry* AddAccount(bool send, const irc::string& nameref, time_t ourTS, std::string h = "", std::string p = "", time_t h_p_ts = 0, std::string cc = "", time_t cc_ts = 0) = 0;
/**
* Get an account from the database
* @param name The name of the account
* @param alias Whether or not to check the given name as an alias
* @return A pointer to the account, or NULL if no account by the given name exists
*/
virtual AccountDBEntry* GetAccount(irc::string name, bool alias) const = 0;
/**
* Remove an account from the database and delete it
* This frees the memory associated with the account and invalidates any pointers to it
* @param send Whether or not to send the removal immediately after removing it
* @param entry A pointer to the account to remove
*/
virtual void RemoveAccount(bool send, AccountDBEntry* entry) = 0;
/**
* Get the internal database used to store accounts
* @return A const reference to the database
*/
virtual const AccountDB& GetDB() const = 0;
/**
* Send an entire account
* @param entry A pointer to the account to send
*/
virtual void SendAccount(const AccountDBEntry* entry) const = 0;
/** Send an update to an account
* @param entry A pointer to the account to send an update for
* @param field The name of the field to send an update for
*/
virtual void SendUpdate(const AccountDBEntry* entry, std::string field) const = 0;
/** Send a removal for an account
* @param name The name of the account to remove
* @param ts The creation TS of the account to remove
*/
virtual void SendRemoval(irc::string name, time_t ts) const = 0;
};
// Some generic extension items for use with accounts
class TSExtItem : public SimpleExtItem<time_t>
{
public:
TSExtItem(const std::string& Key, Module* parent) : SimpleExtItem<time_t>(EXTENSIBLE_ACCOUNT, Key, parent) {}
std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
{
time_t* ts = static_cast<time_t*>(item);
if(format == FORMAT_USER)
{
if(!ts || !*ts)
return "never";
return ServerInstance->TimeString(*ts);
}
if(!ts)
return "";
return ConvToStr(*ts);
}
void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
{
time_t* ours = get(container);
time_t theirs = atol(value.c_str());
if(!ours || theirs > *ours)
set(container, theirs);
}
};
template<typename T>
class TSGenericExtItem : public ExtensionItem
{
protected:
const T* const default_value;
/**
* Serialize the value of the extension item
* @param format The format to serialize the value in
* @param value The value to serialize. It it the caller's responsibility to make sure that it is not null.
* @return The serialized value
*/
virtual std::string value_serialize(SerializeFormat format, const T* value) const = 0;
/**
* Unserialize the value of an extension item
* This function needs to allocate memory for the unserialized value.
* Its lifetime will be handled by the caller.
* @param format The format to unserialize the value from
* @param value The serialized value
* @return A pointer to the unserialized value. This should not be null.
*/
virtual T* value_unserialize(SerializeFormat format, const std::string& value) = 0;
/**
* Resolve a conflict when timestamps are identical
* @param value The existing value, which will be modified if necessary to resolve the conflict. It is the caller's responsibility to make sure that it is not null.
* @param newvalue The new value. It is the caller's responsibility to make sure that it is not null. The contents of the object that this pointer points to are undefined after calling this function.
*/
virtual void value_resolve_conflict(T* value, T* newvalue) = 0;
public:
typedef std::pair<time_t, T* const> value_pair;
TSGenericExtItem(const std::string& Key, const T& default_val, Module* parent) : ExtensionItem(EXTENSIBLE_ACCOUNT, Key, parent), default_value(new T(default_val))
{
}
TSGenericExtItem(const std::string& Key, T* default_val, Module* parent) : ExtensionItem(EXTENSIBLE_ACCOUNT, Key, parent), default_value(default_val)
{
}
virtual ~TSGenericExtItem()
{
delete default_value;
}
inline value_pair* get(const Extensible* container) const
{
return static_cast<value_pair*>(get_raw(container));
}
inline T* get_value(const Extensible* container) const
{
value_pair* ptr = get(container);
return ptr ? ptr->second : NULL;
}
inline void set(Extensible* container, time_t ts, const T& value)
{
value_pair* old = static_cast<value_pair*>(set_raw(container, new value_pair(ts, new T(value))));
if(old)
{
delete old->second;
delete old;
}
}
inline void set(Extensible* container, time_t ts, T* value)
{
value_pair* old = static_cast<value_pair*>(set_raw(container, new value_pair(ts, value)));
if(old)
{
delete old->second;
delete old;
}
}
inline void set(Extensible* container, const T& value)
{
set(container, ServerInstance->Time(), value);
}
inline void set(Extensible* container, T* value)
{
set(container, ServerInstance->Time(), value);
}
// The way account extension items are synchronized between servers, it would always be a bug to unset one, so the lack of an unset function is deliberate.
virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
{
value_pair* p = static_cast<value_pair*>(item);
if(!p)
{
if(format == FORMAT_USER)
return value_serialize(FORMAT_USER, default_value);
return "";
}
if(format == FORMAT_USER)
return value_serialize(FORMAT_USER, p->second);
return ConvToStr(p->first) + (format == FORMAT_NETWORK ? " :" : " ") + value_serialize(format, p->second);
}
virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
{
time_t ts;
T* ptr;
std::string::size_type delim = value.find_first_of(' ');
ts = atol(value.substr(0, delim).c_str());
if(delim == std::string::npos)
ptr = new T(*default_value);
else
ptr = value_unserialize(format, value.substr(delim + 1));
value_pair* p = get(container);
if(!p || ts > p->first)
set(container, ts, ptr);
else
{
if(ts == p->first)
value_resolve_conflict(p->second, ptr);
delete ptr;
}
}
virtual void free(void* item)
{
value_pair* old = static_cast<value_pair*>(item);
if(old)
{
delete old->second;
delete old;
}
}
};
class TSBoolExtItem : public TSGenericExtItem<bool>
{
const bool conflict_value;
protected:
virtual std::string value_serialize(SerializeFormat format, const bool* value) const
{
if(format == FORMAT_USER)
return *value ? "true" : "false";
return *value ? "1" : "0";
}
virtual bool* value_unserialize(SerializeFormat format, const std::string& value)
{
if(value.empty())
return new bool(default_value);
if(value[0] == '0')
return new bool(false);
return new bool(true);
}
virtual void value_resolve_conflict(bool* value, bool* newvalue)
{
if(*value != *newvalue)
*value = conflict_value;
}
public:
TSBoolExtItem(const std::string& Key, bool default_val, bool conflict_val, Module* parent) : TSGenericExtItem<bool>(Key, default_val, parent), conflict_value(conflict_val)
{
}
};
class TSStringExtItem : public TSGenericExtItem<std::string>
{
protected:
virtual std::string value_serialize(SerializeFormat format, const std::string* value) const
{
return *value;
}
virtual std::string* value_unserialize(SerializeFormat format, const std::string& value)
{
return new std::string(value);
}
virtual void value_resolve_conflict(std::string* value, std::string* newvalue)
{
if(*value < *newvalue)
value->swap(*newvalue);
}
public:
TSStringExtItem(const std::string& Key, const std::string& default_val, Module* parent) : TSGenericExtItem<std::string>(Key, default_val, parent)
{
}
TSStringExtItem(const std::string& Key, std::string* default_val, Module* parent) : TSGenericExtItem<std::string>(Key, default_val, parent)
{
}
};
class TSStringVectorExtItem : public TSGenericExtItem<std::vector<std::string> >
{
const char delimeter;
protected:
virtual std::string value_serialize(SerializeFormat format, const std::vector<std::string>* value) const
{
std::ostringstream str;
for(std::vector<std::string>::const_iterator i = value->begin(); i != value->end(); ++i)
str << *i << delimeter;
std::string retval = str.str();
retval.erase(retval.length() - 1);
return retval;
}
virtual std::vector<std::string>* value_unserialize(SerializeFormat format, const std::string& value)
{
irc::sepstream sep(value, delimeter, false);
std::string token;
std::vector<std::string>* retval = new std::vector<std::string>;
while(sep.GetToken(token))
retval->push_back(token);
return retval;
}
virtual void value_resolve_conflict(std::vector<std::string>* value, std::vector<std::string>* newvalue)
{
if(*value < *newvalue)
value->swap(*newvalue);
}
public:
TSStringVectorExtItem(const std::string& Key, const std::vector<std::string>& default_val, const char delim, Module* parent) : TSGenericExtItem<std::vector<std::string> >(Key, default_val, parent), delimeter(delim)
{
}
TSStringVectorExtItem(const std::string& Key, std::vector<std::string>* default_val, const char delim, Module* parent) : TSGenericExtItem<std::vector<std::string> >(Key, default_val, parent), delimeter(delim)
{
}
};
#endif