Anope IRC Services  Version 2.0
nickalias.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #include "services.h"
14 #include "account.h"
15 #include "modules.h"
16 #include "opertype.h"
17 #include "protocol.h"
18 #include "users.h"
19 #include "servers.h"
20 #include "config.h"
21 
23 
24 NickAlias::NickAlias(const Anope::string &nickname, NickCore* nickcore) : Serializable("NickAlias")
25 {
26  if (nickname.empty())
27  throw CoreException("Empty nick passed to NickAlias constructor");
28  else if (!nickcore)
29  throw CoreException("Empty nickcore passed to NickAlias constructor");
30 
32  this->nick = nickname;
33  this->nc = nickcore;
34  nickcore->aliases->push_back(this);
35 
36  size_t old = NickAliasList->size();
37  (*NickAliasList)[this->nick] = this;
38  if (old == NickAliasList->size())
39  Log(LOG_DEBUG) << "Duplicate nick " << nickname << " in nickalias table";
40 
41  if (this->nc->o == NULL)
42  {
43  Oper *o = Oper::Find(this->nick);
44  if (o == NULL)
45  o = Oper::Find(this->nc->display);
46  nickcore->o = o;
47  if (this->nc->o != NULL)
48  Log() << "Tied oper " << this->nc->display << " to type " << this->nc->o->ot->GetName();
49  }
50 }
51 
53 {
54  FOREACH_MOD(OnDelNick, (this));
55 
56  /* Accept nicks that have no core, because of database load functions */
57  if (this->nc)
58  {
59  /* Next: see if our core is still useful. */
60  std::vector<NickAlias *>::iterator it = std::find(this->nc->aliases->begin(), this->nc->aliases->end(), this);
61  if (it != this->nc->aliases->end())
62  this->nc->aliases->erase(it);
63  if (this->nc->aliases->empty())
64  {
65  delete this->nc;
66  this->nc = NULL;
67  }
68  else
69  {
70  /* Display updating stuff */
71  if (this->nick.equals_ci(this->nc->display))
72  this->nc->SetDisplay(this->nc->aliases->front());
73  }
74  }
75 
76  /* Remove us from the aliases list */
77  NickAliasList->erase(this->nick);
78 }
79 
80 void NickAlias::SetVhost(const Anope::string &ident, const Anope::string &host, const Anope::string &creator, time_t created)
81 {
82  this->vhost_ident = ident;
83  this->vhost_host = host;
84  this->vhost_creator = creator;
85  this->vhost_created = created;
86 }
87 
89 {
90  this->vhost_ident.clear();
91  this->vhost_host.clear();
92  this->vhost_creator.clear();
93  this->vhost_created = 0;
94 }
95 
96 bool NickAlias::HasVhost() const
97 {
98  return !this->vhost_host.empty();
99 }
100 
102 {
103  return this->vhost_ident;
104 }
105 
107 {
108  return this->vhost_host;
109 }
110 
112 {
113  return this->vhost_creator;
114 }
115 
117 {
118  return this->vhost_created;
119 }
120 
122 {
123  nickalias_map::const_iterator it = NickAliasList->find(nick);
124  if (it != NickAliasList->end())
125  {
126  it->second->QueueUpdate();
127  return it->second;
128  }
129 
130  return NULL;
131 }
132 
134 {
135  data["nick"] << this->nick;
136  data["last_quit"] << this->last_quit;
137  data["last_realname"] << this->last_realname;
138  data["last_usermask"] << this->last_usermask;
139  data["last_realhost"] << this->last_realhost;
140  data.SetType("time_registered", Serialize::Data::DT_INT); data["time_registered"] << this->time_registered;
141  data.SetType("last_seen", Serialize::Data::DT_INT); data["last_seen"] << this->last_seen;
142  data["nc"] << this->nc->display;
143 
144  if (this->HasVhost())
145  {
146  data["vhost_ident"] << this->GetVhostIdent();
147  data["vhost_host"] << this->GetVhostHost();
148  data["vhost_creator"] << this->GetVhostCreator();
149  data["vhost_time"] << this->GetVhostCreated();
150  }
151 
152  Extensible::ExtensibleSerialize(this, this, data);
153 }
154 
156 {
157  Anope::string snc, snick;
158 
159  data["nc"] >> snc;
160  data["nick"] >> snick;
161 
162  NickCore *core = NickCore::Find(snc);
163  if (core == NULL)
164  return NULL;
165 
166  NickAlias *na;
167  if (obj)
169  else
170  na = new NickAlias(snick, core);
171 
172  if (na->nc != core)
173  {
174  std::vector<NickAlias *>::iterator it = std::find(na->nc->aliases->begin(), na->nc->aliases->end(), na);
175  if (it != na->nc->aliases->end())
176  na->nc->aliases->erase(it);
177 
178  if (na->nc->aliases->empty())
179  delete na->nc;
180  else if (na->nick.equals_ci(na->nc->display))
181  na->nc->SetDisplay(na->nc->aliases->front());
182 
183  na->nc = core;
184  core->aliases->push_back(na);
185  }
186 
187  data["last_quit"] >> na->last_quit;
188  data["last_realname"] >> na->last_realname;
189  data["last_usermask"] >> na->last_usermask;
190  data["last_realhost"] >> na->last_realhost;
191  data["time_registered"] >> na->time_registered;
192  data["last_seen"] >> na->last_seen;
193 
195  time_t vhost_time;
196 
197  data["vhost_ident"] >> vhost_ident;
198  data["vhost_host"] >> vhost_host;
199  data["vhost_creator"] >> vhost_creator;
200  data["vhost_time"] >> vhost_time;
201 
202  na->SetVhost(vhost_ident, vhost_host, vhost_creator, vhost_time);
203 
204  Extensible::ExtensibleUnserialize(na, na, data);
205 
206  /* compat */
207  bool b;
208  b = false;
209  data["extensible:NO_EXPIRE"] >> b;
210  if (b)
211  na->Extend<bool>("NS_NO_EXPIRE");
212  /* end compat */
213 
214  return na;
215 }
216 
Serialize::Reference< NickCore > nc
Definition: account.h:47
Anope::string vhost_ident
Definition: account.h:33
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
time_t vhost_created
Definition: account.h:34
void clear()
Definition: anope.h:187
Serialize::Checker< std::vector< NickAlias * > > aliases
Definition: account.h:128
Definition: opertype.h:18
Anope::string nick
Definition: account.h:37
const Anope::string & GetVhostHost() const
Definition: nickalias.cpp:106
Anope::string last_usermask
Definition: account.h:41
bool HasVhost() const
Definition: nickalias.cpp:96
bool equals_ci(const char *_str) const
Definition: anope.h:78
virtual void SetType(const Anope::string &key, Type t)
Definition: serialize.h:38
CoreExport time_t CurTime
Definition: main.cpp:41
#define FOREACH_MOD(ename, args)
Definition: modules.h:62
time_t time_registered
Definition: account.h:44
NickAlias(const Anope::string &nickname, NickCore *nickcore)
Definition: nickalias.cpp:24
static void ExtensibleUnserialize(Extensible *, Serializable *, Serialize::Data &data)
Definition: extensible.cpp:50
Oper * o
Definition: account.h:131
bool empty() const
Definition: anope.h:126
Anope::string vhost_host
Definition: account.h:33
Serialize::Checker< nickalias_map > NickAliasList("NickAlias")
void Serialize(Serialize::Data &data) const anope_override
Definition: nickalias.cpp:133
void RemoveVhost()
Definition: nickalias.cpp:88
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: nickalias.cpp:155
const Anope::string & GetVhostCreator() const
Definition: nickalias.cpp:111
time_t last_seen
Definition: account.h:45
T anope_dynamic_static_cast(O ptr)
Definition: anope.h:774
Anope::string last_realname
Definition: account.h:39
Definition: logger.h:53
void SetVhost(const Anope::string &ident, const Anope::string &host, const Anope::string &creator, time_t created=Anope::CurTime)
Definition: nickalias.cpp:80
static Oper * Find(const Anope::string &name)
Definition: opertype.cpp:29
Anope::string last_quit
Definition: account.h:38
time_t GetVhostCreated() const
Definition: nickalias.cpp:116
static NickCore * Find(const Anope::string &nick)
Definition: nickcore.cpp:258
Anope::string vhost_creator
Definition: account.h:33
static void ExtensibleSerialize(const Extensible *, const Serializable *, Serialize::Data &data)
Definition: extensible.cpp:41
Anope::string last_realhost
Definition: account.h:43
const Anope::string & GetVhostIdent() const
Definition: nickalias.cpp:101