Anope IRC Services  Version 2.0
nickcore.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 "modules.h"
15 #include "account.h"
16 #include "config.h"
17 
19 
20 NickCore::NickCore(const Anope::string &coredisplay) : Serializable("NickCore"), chanaccess("ChannelInfo"), aliases("NickAlias")
21 {
22  if (coredisplay.empty())
23  throw CoreException("Empty display passed to NickCore constructor");
24 
25  this->o = NULL;
26  this->channelcount = 0;
27  this->lastmail = 0;
28 
29  this->display = coredisplay;
30 
31  size_t old = NickCoreList->size();
32  (*NickCoreList)[this->display] = this;
33  if (old == NickCoreList->size())
34  Log(LOG_DEBUG) << "Duplicate account " << coredisplay << " in nickcore table?";
35 
36  FOREACH_MOD(OnNickCoreCreate, (this));
37 }
38 
40 {
41  FOREACH_MOD(OnDelCore, (this));
42 
43  if (!this->chanaccess->empty())
44  Log(LOG_DEBUG) << "Non-empty chanaccess list in destructor!";
45 
46  for (std::list<User *>::iterator it = this->users.begin(); it != this->users.end();)
47  {
48  User *user = *it++;
49  user->Logout();
50  }
51  this->users.clear();
52 
53  NickCoreList->erase(this->display);
54 
55  this->ClearAccess();
56 
57  if (!this->memos.memos->empty())
58  {
59  for (unsigned i = 0, end = this->memos.memos->size(); i < end; ++i)
60  delete this->memos.GetMemo(i);
61  this->memos.memos->clear();
62  }
63 }
64 
66 {
67  data["display"] << this->display;
68  data["pass"] << this->pass;
69  data["email"] << this->email;
70  data["language"] << this->language;
71  for (unsigned i = 0; i < this->access.size(); ++i)
72  data["access"] << this->access[i] << " ";
73  data["memomax"] << this->memos.memomax;
74  for (unsigned i = 0; i < this->memos.ignores.size(); ++i)
75  data["memoignores"] << this->memos.ignores[i] << " ";
76  Extensible::ExtensibleSerialize(this, this, data);
77 }
78 
80 {
81  NickCore *nc;
82 
83  Anope::string sdisplay;
84 
85  data["display"] >> sdisplay;
86 
87  if (obj)
89  else
90  nc = new NickCore(sdisplay);
91 
92  data["pass"] >> nc->pass;
93  data["email"] >> nc->email;
94  data["language"] >> nc->language;
95  {
96  Anope::string buf;
97  data["access"] >> buf;
98  spacesepstream sep(buf);
99  nc->access.clear();
100  while (sep.GetToken(buf))
101  nc->access.push_back(buf);
102  }
103  data["memomax"] >> nc->memos.memomax;
104  {
105  Anope::string buf;
106  data["memoignores"] >> buf;
107  spacesepstream sep(buf);
108  nc->memos.ignores.clear();
109  while (sep.GetToken(buf))
110  nc->memos.ignores.push_back(buf);
111  }
112 
113  Extensible::ExtensibleUnserialize(nc, nc, data);
114 
115  /* compat */
116  bool b;
117  b = false;
118  data["extensible:SECURE"] >> b;
119  if (b)
120  nc->Extend<bool>("NS_SECURE");
121  b = false;
122  data["extensible:PRIVATE"] >> b;
123  if (b)
124  nc->Extend<bool>("NS_PRIVATE");
125  b = false;
126  data["extensible:AUTOOP"] >> b;
127  if (b)
128  nc->Extend<bool>("AUTOOP");
129  b = false;
130  data["extensible:HIDE_EMAIL"] >> b;
131  if (b)
132  nc->Extend<bool>("HIDE_EMAIL");
133  b = false;
134  data["extensible:HIDE_QUIT"] >> b;
135  if (b)
136  nc->Extend<bool>("HIDE_QUIT");
137  b = false;
138  data["extensible:MEMO_RECEIVE"] >> b;
139  if (b)
140  nc->Extend<bool>("MEMO_RECEIVE");
141  b = false;
142  data["extensible:MEMO_SIGNON"] >> b;
143  if (b)
144  nc->Extend<bool>("MEMO_SIGNON");
145  b = false;
146  data["extensible:KILLPROTECT"] >> b;
147  if (b)
148  nc->Extend<bool>("KILLPROTECT");
149  /* end compat */
150 
151  return nc;
152 }
153 
155 {
156  if (na->nc != this || na->nick == this->display)
157  return;
158 
159  FOREACH_MOD(OnChangeCoreDisplay, (this, na->nick));
160 
161  /* this affects the serialized aliases */
162  for (unsigned i = 0; i < aliases->size(); ++i)
163  aliases->at(i)->QueueUpdate();
164 
165  /* Remove the core from the list */
166  NickCoreList->erase(this->display);
167 
168  this->display = na->nick;
169 
170  (*NickCoreList)[this->display] = this;
171 }
172 
174 {
175  return this->o != NULL;
176 }
177 
179 {
180  this->access.push_back(entry);
181  FOREACH_MOD(OnNickAddAccess, (this, entry));
182 }
183 
184 Anope::string NickCore::GetAccess(unsigned entry) const
185 {
186  if (this->access.empty() || entry >= this->access.size())
187  return "";
188  return this->access[entry];
189 }
190 
191 unsigned NickCore::GetAccessCount() const
192 {
193  return this->access.size();
194 }
195 
197 {
198  for (unsigned i = 0, end = this->access.size(); i < end; ++i)
199  if (this->access[i] == entry)
200  return true;
201 
202  return false;
203 }
204 
206 {
207  for (unsigned i = 0, end = this->access.size(); i < end; ++i)
208  if (this->access[i] == entry)
209  {
210  FOREACH_MOD(OnNickEraseAccess, (this, entry));
211  this->access.erase(this->access.begin() + i);
212  break;
213  }
214 }
215 
217 {
218  FOREACH_MOD(OnNickClearAccess, (this));
219  this->access.clear();
220 }
221 
222 bool NickCore::IsOnAccess(const User *u) const
223 {
224  Anope::string buf = u->GetIdent() + "@" + u->host, buf2, buf3;
225  if (!u->vhost.empty())
226  buf2 = u->GetIdent() + "@" + u->vhost;
227  if (!u->GetCloakedHost().empty())
228  buf3 = u->GetIdent() + "@" + u->GetCloakedHost();
229 
230  for (unsigned i = 0, end = this->access.size(); i < end; ++i)
231  {
232  Anope::string a = this->GetAccess(i);
233  if (Anope::Match(buf, a) || (!buf2.empty() && Anope::Match(buf2, a)) || (!buf3.empty() && Anope::Match(buf3, a)))
234  return true;
235  }
236  return false;
237 }
238 
240 {
241  ++(*this->chanaccess)[ci];
242 }
243 
245 {
246  int& i = (*this->chanaccess)[ci];
247  if (--i <= 0)
248  this->chanaccess->erase(ci);
249 }
250 
251 void NickCore::GetChannelReferences(std::deque<ChannelInfo *> &queue)
252 {
253  queue.clear();
254  for (std::map<ChannelInfo *, int>::iterator it = this->chanaccess->begin(), it_end = this->chanaccess->end(); it != it_end; ++it)
255  queue.push_back(it->first);
256 }
257 
259 {
260  nickcore_map::const_iterator it = NickCoreList->find(nick);
261  if (it != NickCoreList->end())
262  {
263  it->second->QueueUpdate();
264  return it->second;
265  }
266 
267  return NULL;
268 }
269 
Serialize::Reference< NickCore > nc
Definition: account.h:47
void SetDisplay(const NickAlias *na)
Definition: nickcore.cpp:154
~NickCore()
Definition: nickcore.cpp:39
const Anope::string & GetIdent() const
Definition: users.cpp:264
Definition: hashcomp.h:84
Serialize::Checker< std::vector< NickAlias * > > aliases
Definition: account.h:128
void Serialize(Serialize::Data &data) const anope_override
Definition: nickcore.cpp:65
void GetChannelReferences(std::deque< ChannelInfo * > &queue)
Definition: nickcore.cpp:251
void EraseAccess(const Anope::string &entry)
Definition: nickcore.cpp:205
Anope::string nick
Definition: account.h:37
Serialize::Checker< nickcore_map > NickCoreList("NickCore")
Definition: users.h:34
std::vector< Anope::string > access
Definition: account.h:121
Anope::string GetAccess(unsigned entry) const
Definition: nickcore.cpp:184
bool FindAccess(const Anope::string &entry)
Definition: nickcore.cpp:196
uint16_t channelcount
Definition: account.h:136
NickCore(const Anope::string &nickdisplay)
Definition: nickcore.cpp:20
Anope::string pass
Definition: account.h:115
#define FOREACH_MOD(ename, args)
Definition: modules.h:62
MemoInfo memos
Definition: account.h:122
CoreExport bool Match(const string &str, const string &mask, bool case_sensitive=false, bool use_regex=false)
Definition: misc.cpp:407
std::vector< Anope::string > ignores
Definition: memo.h:44
void ClearAccess()
Definition: nickcore.cpp:216
unsigned GetAccessCount() const
Definition: nickcore.cpp:191
Anope::string display
Definition: account.h:113
static void ExtensibleUnserialize(Extensible *, Serializable *, Serialize::Data &data)
Definition: extensible.cpp:50
Oper * o
Definition: account.h:131
Memo * GetMemo(unsigned index) const
Definition: memos.cpp:82
bool empty() const
Definition: anope.h:126
void RemoveChannelReference(ChannelInfo *ci)
Definition: nickcore.cpp:244
void AddChannelReference(ChannelInfo *ci)
Definition: nickcore.cpp:239
Anope::string host
Definition: users.h:65
void Logout()
Definition: users.cpp:408
Serialize::Checker< std::map< ChannelInfo *, int > > chanaccess
Definition: account.h:110
static Serializable * Unserialize(Serializable *obj, Serialize::Data &)
Definition: nickcore.cpp:79
int16_t memomax
Definition: memo.h:42
const Anope::string & GetCloakedHost() const
Definition: users.cpp:225
Serialize::Checker< std::vector< Memo * > > memos
Definition: memo.h:43
void AddAccess(const Anope::string &entry)
Definition: nickcore.cpp:178
time_t lastmail
Definition: account.h:138
bool GetToken(Anope::string &token)
Definition: hashcomp.cpp:99
T anope_dynamic_static_cast(O ptr)
Definition: anope.h:774
bool IsOnAccess(const User *u) const
Definition: nickcore.cpp:222
Definition: logger.h:53
virtual bool IsServicesOper() const
Definition: nickcore.cpp:173
std::list< User * > users
Definition: account.h:140
static NickCore * Find(const Anope::string &nick)
Definition: nickcore.cpp:258
Anope::string email
Definition: account.h:116
static void ExtensibleSerialize(const Extensible *, const Serializable *, Serialize::Data &data)
Definition: extensible.cpp:41
Anope::string vhost
Definition: users.h:67
Anope::string language
Definition: account.h:118