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
|
/* NickServ core functions
*
* (C) 2003-2013 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 "nickserv.h"
class NickServCollide;
typedef std::map<Anope::string, NickServCollide *> nickservcollides_map;
static nickservcollides_map NickServCollides;
/** Timer for colliding nicks to force people off of nicknames
*/
class NickServCollide : public Timer
{
Reference<User> u;
Anope::string nick;
public:
/** Constructor
* @param nick The nick we're colliding
* @param delay How long to delay before kicking the user off the nick
*/
NickServCollide(User *user, time_t delay) : Timer(delay), u(user), nick(u->nick)
{
/* Erase the current collide and use the new one */
nickservcollides_map::iterator nit = NickServCollides.find(user->nick);
if (nit != NickServCollides.end())
delete nit->second;
NickServCollides.insert(std::make_pair(nick, this));
}
virtual ~NickServCollide()
{
NickServCollides.erase(this->nick);
}
/** Called when the delay is up
* @param t The current time
*/
void Tick(time_t t) anope_override
{
if (!u)
return;
/* If they identified or don't exist anymore, don't kill them. */
NickAlias *na = NickAlias::Find(u->nick);
if (!na || u->Account() == na->nc || u->timestamp > this->GetSetTime())
return;
u->Collide(na);
}
};
class MyNickServService : public NickServService
{
public:
MyNickServService(Module *m) : NickServService(m) { }
void Validate(User *u) anope_override
{
NickAlias *na = NickAlias::Find(u->nick);
if (!na)
return;
if (na->nc->HasExt("SUSPENDED"))
{
u->SendMessage(NickServ, NICK_X_SUSPENDED, u->nick.c_str());
u->Collide(na);
return;
}
if (!(u->Account() == na->nc) && !u->fingerprint.empty() && na->nc->FindCert(u->fingerprint))
{
u->SendMessage(NickServ, _("SSL Fingerprint accepted, you are now identified."));
Log(u) << "automatically identified for account " << na->nc->display << " using a valid SSL fingerprint.";
u->Identify(na);
return;
}
if (!na->nc->HasExt("SECURE") && u->IsRecognized())
{
na->last_seen = Anope::CurTime;
Anope::string last_usermask = u->GetIdent() + "@" + u->GetDisplayedHost();
na->last_usermask = last_usermask;
na->last_realname = u->realname;
return;
}
if (Config->NoNicknameOwnership)
return;
if (u->IsRecognized(false) || !na->nc->HasExt("KILL_IMMED"))
{
if (na->nc->HasExt("SECURE"))
u->SendMessage(NickServ, NICK_IS_SECURE, Config->UseStrictPrivMsgString.c_str(), Config->NickServ.c_str());
else
u->SendMessage(NickServ, NICK_IS_REGISTERED, Config->UseStrictPrivMsgString.c_str(), Config->NickServ.c_str());
}
if (na->nc->HasExt("KILLPROTECT") && !u->IsRecognized(false))
{
if (na->nc->HasExt("KILL_IMMED"))
{
u->SendMessage(NickServ, FORCENICKCHANGE_NOW);
u->Collide(na);
}
else if (na->nc->HasExt("KILL_QUICK"))
{
u->SendMessage(NickServ, _("If you do not change within %s, I will change your nick."), Anope::Duration(Config->NSKillQuick, u->Account()).c_str());
new NickServCollide(u, Config->NSKillQuick);
}
else
{
u->SendMessage(NickServ, _("If you do not change within %s, I will change your nick."), Anope::Duration(Config->NSKill, u->Account()).c_str());
new NickServCollide(u, Config->NSKill);
}
}
}
void Login(User *user, NickAlias *na) anope_override
{
const NickAlias *u_na = NickAlias::Find(user->nick);
user->Login(na->nc);
if (u_na && *u_na->nc == *na->nc && !Config->NoNicknameOwnership && na->nc->HasExt("UNCONFIRMED") == false)
user->SetMode(NickServ, "REGISTERED");
}
};
class ExpireCallback : public CallBack
{
public:
ExpireCallback(Module *owner) : CallBack(owner, Config->ExpireTimeout, Anope::CurTime, true) { }
void Tick(time_t) anope_override
{
if (Anope::NoExpire || Anope::ReadOnly)
return;
for (nickalias_map::const_iterator it = NickAliasList->begin(), it_end = NickAliasList->end(); it != it_end; )
{
NickAlias *na = it->second;
++it;
User *u = User::Find(na->nick);
if (u && (na->nc->HasExt("SECURE") ? u->IsIdentified(true) : u->IsRecognized()))
na->last_seen = Anope::CurTime;
bool expire = false;
if (na->nc->HasExt("UNCONFIRMED"))
if (Config->NSUnconfirmedExpire && Anope::CurTime - na->time_registered >= Config->NSUnconfirmedExpire)
expire = true;
if (Config->NSExpire && Anope::CurTime - na->last_seen >= Config->NSExpire)
expire = true;
if (na->HasExt("NO_EXPIRE"))
expire = false;
FOREACH_MOD(I_OnPreNickExpire, OnPreNickExpire(na, expire));
if (expire)
{
Anope::string extra;
if (na->nc->HasExt("SUSPENDED"))
extra = "suspended ";
Log(LOG_NORMAL, "expire") << "Expiring " << extra << "nickname " << na->nick << " (group: " << na->nc->display << ") (e-mail: " << (na->nc->email.empty() ? "none" : na->nc->email) << ")";
FOREACH_MOD(I_OnNickExpire, OnNickExpire(na));
delete na;
}
}
}
};
class NickServCore : public Module
{
MyNickServService mynickserv;
ExpireCallback expires;
public:
NickServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE), mynickserv(this), expires(this)
{
this->SetAuthor("Anope");
NickServ = BotInfo::Find(Config->NickServ);
if (!NickServ)
throw ModuleException("No bot named " + Config->NickServ);
Implementation i[] = { I_OnBotDelete, I_OnDelNick, I_OnDelCore, I_OnChangeCoreDisplay, I_OnNickIdentify, I_OnNickGroup,
I_OnNickUpdate, I_OnUserConnect, I_OnPostUserLogoff, I_OnServerSync, I_OnUserNickChange, I_OnPreHelp, I_OnPostHelp };
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
}
~NickServCore()
{
NickServ = NULL;
}
void OnBotDelete(BotInfo *bi) anope_override
{
if (bi == NickServ)
NickServ = NULL;
}
void OnDelNick(NickAlias *na) anope_override
{
User *u = User::Find(na->nick);
if (u && u->Account() == na->nc)
{
IRCD->SendLogout(u);
u->RemoveMode(NickServ, "REGISTERED");
u->Logout();
}
}
void OnDelCore(NickCore *nc) anope_override
{
Log(NickServ, "nick") << "deleting nickname group " << nc->display;
/* Clean up this nick core from any users online */
for (std::list<User *>::iterator it = nc->users.begin(); it != nc->users.end();)
{
User *user = *it++;
IRCD->SendLogout(user);
user->RemoveMode(NickServ, "REGISTERED");
user->Logout();
FOREACH_MOD(I_OnNickLogout, OnNickLogout(user));
}
nc->users.clear();
}
void OnChangeCoreDisplay(NickCore *nc, const Anope::string &newdisplay) anope_override
{
Log(LOG_NORMAL, "nick", NickServ) << "Changing " << nc->display << " nickname group display to " << newdisplay;
}
void OnNickIdentify(User *u) anope_override
{
if (!Config->NoNicknameOwnership)
{
const NickAlias *this_na = NickAlias::Find(u->nick);
if (this_na && this_na->nc == u->Account() && u->Account()->HasExt("UNCONFIRMED") == false)
u->SetMode(NickServ, "REGISTERED");
}
if (Config->NSModeOnID)
for (User::ChanUserList::iterator it = u->chans.begin(), it_end = u->chans.end(); it != it_end; ++it)
{
ChanUserContainer *cc = *it;
Channel *c = cc->chan;
if (c)
c->SetCorrectModes(u, true, true);
}
if (!Config->NSModesOnID.empty())
u->SetModes(NickServ, "%s", Config->NSModesOnID.c_str());
if (Config->NSForceEmail && u->Account()->email.empty())
{
u->SendMessage(NickServ, _("You must now supply an e-mail for your nick.\n"
"This e-mail will allow you to retrieve your password in\n"
"case you forget it."));
u->SendMessage(NickServ, _("Type \002%s%s SET EMAIL \037e-mail\037\002 in order to set your e-mail.\n"
"Your privacy is respected; this e-mail won't be given to\n"
"any third-party person."), Config->UseStrictPrivMsgString.c_str(), Config->NickServ.c_str());
}
if (u->Account()->HasExt("UNCONFIRMED"))
{
u->SendMessage(NickServ, _("Your email address is not confirmed. To confirm it, follow the instructions that were emailed to you when you registered."));
const NickAlias *this_na = NickAlias::Find(u->Account()->display);
time_t time_registered = Anope::CurTime - this_na->time_registered;
if (Config->NSUnconfirmedExpire > time_registered)
u->SendMessage(NickServ, _("Your account will expire, if not confirmed, in %s"), Anope::Duration(Config->NSUnconfirmedExpire - time_registered).c_str());
}
}
void OnNickGroup(User *u, NickAlias *target) anope_override
{
if (target->nc->HasExt("UNCONFIRMED") == false)
u->SetMode(NickServ, "REGISTERED");
}
void OnNickUpdate(User *u) anope_override
{
for (User::ChanUserList::iterator it = u->chans.begin(), it_end = u->chans.end(); it != it_end; ++it)
{
ChanUserContainer *cc = *it;
Channel *c = cc->chan;
if (c)
c->SetCorrectModes(u, true, true);
}
}
void OnUserConnect(User *u, bool &exempt) anope_override
{
if (u->Quitting() || !u->server->IsSynced())
return;
const NickAlias *na = NickAlias::Find(u->nick);
if (!Config->NoNicknameOwnership && !Config->NSUnregisteredNotice.empty() && !na)
u->SendMessage(NickServ, Config->NSUnregisteredNotice);
else if (na)
this->mynickserv.Validate(u);
}
void OnPostUserLogoff(User *u) anope_override
{
NickAlias *na = NickAlias::Find(u->nick);
if (na)
na->OnCancel(u);
}
void OnServerSync(Server *s) anope_override
{
for (user_map::const_iterator it = UserListByNick.begin(); it != UserListByNick.end(); ++it)
{
User *u = it->second;
if (u->server == s && !u->IsIdentified())
this->mynickserv.Validate(u);
}
}
void OnUserNickChange(User *u, const Anope::string &oldnick) anope_override
{
NickAlias *old_na = NickAlias::Find(oldnick), *na = NickAlias::Find(u->nick);
/* If the new nick isnt registerd or its registerd and not yours */
if (!na || na->nc != u->Account())
{
/* Remove +r, but keep an account associated with the user */
u->RemoveMode(NickServ, "REGISTERED");
this->mynickserv.Validate(u);
}
else
{
/* Reset +r and re-send account (even though it really should be set at this point) */
IRCD->SendLogin(u);
if (!Config->NoNicknameOwnership && na->nc == u->Account() && na->nc->HasExt("UNCONFIRMED") == false)
u->SetMode(NickServ, "REGISTERED");
Log(NickServ) << u->GetMask() << " automatically identified for group " << u->Account()->display;
}
if (!u->nick.equals_ci(oldnick) && old_na)
old_na->OnCancel(u);
}
void OnUserModeSet(User *u, const Anope::string &mname) anope_override
{
if (mname == "REGISTERED" && !u->IsIdentified())
u->RemoveMode(NickServ, mname);
}
EventReturn OnPreHelp(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
if (!params.empty() || source.c || source.service->nick != Config->NickServ)
return EVENT_CONTINUE;
if (!Config->NoNicknameOwnership)
source.Reply(_("\002%s\002 allows you to register a nickname and\n"
"prevent others from using it. The following\n"
"commands allow for registration and maintenance of\n"
"nicknames; to use them, type \002%s%s \037command\037\002.\n"
"For more information on a specific command, type\n"
"\002%s%s %s \037command\037\002.\n "), Config->NickServ.c_str(), Config->UseStrictPrivMsgString.c_str(), Config->NickServ.c_str(), Config->UseStrictPrivMsgString.c_str(), Config->NickServ.c_str(), source.command.c_str());
else
source.Reply(_("\002%s\002 allows you to register an account.\n"
"The following commands allow for registration and maintenance of\n"
"accounts; to use them, type \002%s%s \037command\037\002.\n"
"For more information on a specific command, type\n"
"\002%s%s %s \037command\037\002.\n "), Config->NickServ.c_str(), Config->UseStrictPrivMsgString.c_str(), Config->NickServ.c_str(), Config->UseStrictPrivMsgString.c_str(), Config->NickServ.c_str(), source.command.c_str());
return EVENT_CONTINUE;
}
void OnPostHelp(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
if (!params.empty() || source.c || source.service->nick != Config->NickServ)
return;
if (source.IsServicesOper())
source.Reply(_(" \n"
"Services Operators can also drop any nickname without needing\n"
"to identify for the nick, and may view the access list for\n"
"any nickname."));
if (Config->NSExpire >= 86400)
source.Reply(_(" \n"
"Accounts that are not used anymore are subject to \n"
"the automatic expiration, i.e. they will be deleted\n"
"after %d days if not used."), Config->NSExpire / 86400);
source.Reply(_(" \n"
"\002NOTICE:\002 This service is intended to provide a way for\n"
"IRC users to ensure their identity is not compromised.\n"
"It is \002NOT\002 intended to facilitate \"stealing\" of\n"
"nicknames or other malicious actions. Abuse of %s\n"
"will result in, at minimum, loss of the abused\n"
"nickname(s)."), Config->NickServ.c_str());
}
};
MODULE_INIT(NickServCore)
|