Anope IRC Services  Version 2.0
ns_maxemail.cpp
Go to the documentation of this file.
1 /* ns_maxemail.cpp - Limit the amount of times an email address
2  * can be used for a NickServ account.
3  *
4  * (C) 2003-2014 Anope Team
5  * Contact us at team@anope.org
6  *
7  * Included in the Anope module pack since Anope 1.7.9
8  * Anope Coder: GeniusDex <geniusdex@anope.org>
9  *
10  * Please read COPYING and README for further details.
11  */
12 
13 #include "module.h"
14 
15 class NSMaxEmail : public Module
16 {
17  bool CheckLimitReached(CommandSource &source, const Anope::string &email)
18  {
19  int NSEmailMax = Config->GetModule(this)->Get<int>("maxemails");
20 
21  if (NSEmailMax < 1 || email.empty())
22  return false;
23 
24  if (this->CountEmail(email, source.nc) < NSEmailMax)
25  return false;
26 
27  if (NSEmailMax == 1)
28  source.Reply(_("The email address \002%s\002 has reached its usage limit of 1 user."), email.c_str());
29  else
30  source.Reply(_("The email address \002%s\002 has reached its usage limit of %d users."), email.c_str(), NSEmailMax);
31 
32  return true;
33  }
34 
35  int CountEmail(const Anope::string &email, NickCore *unc)
36  {
37  int count = 0;
38 
39  if (email.empty())
40  return 0;
41 
42  for (nickcore_map::const_iterator it = NickCoreList->begin(), it_end = NickCoreList->end(); it != it_end; ++it)
43  {
44  const NickCore *nc = it->second;
45 
46  if (unc != nc && !nc->email.empty() && nc->email.equals_ci(email))
47  ++count;
48  }
49 
50  return count;
51  }
52 
53  public:
54  NSMaxEmail(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
55  {
56  }
57 
58  EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> &params) anope_override
59  {
60  if (source.IsOper())
61  return EVENT_CONTINUE;
62 
63  if (command->name == "nickserv/register")
64  {
65  if (this->CheckLimitReached(source, params.size() > 1 ? params[1] : ""))
66  return EVENT_STOP;
67  }
68  else if (command->name == "nickserv/set/email")
69  {
70  if (this->CheckLimitReached(source, params.size() > 0 ? params[0] : ""))
71  return EVENT_STOP;
72  }
73  else if (command->name == "nickserv/ungroup" && source.GetAccount())
74  {
75  if (this->CheckLimitReached(source, source.GetAccount()->email))
76  return EVENT_STOP;
77  }
78 
79  return EVENT_CONTINUE;
80  }
81 };
82 
CoreExport Serialize::Checker< nickcore_map > NickCoreList
int CountEmail(const Anope::string &email, NickCore *unc)
Definition: ns_maxemail.cpp:35
bool equals_ci(const char *_str) const
Definition: anope.h:78
EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector< Anope::string > &params) anope_override
Definition: ns_maxemail.cpp:58
Definition: Config.cs:26
void Reply(const char *message,...)
Definition: command.cpp:96
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
bool CheckLimitReached(CommandSource &source, const Anope::string &email)
Definition: ns_maxemail.cpp:17
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
NSMaxEmail(const Anope::string &modname, const Anope::string &creator)
Definition: ns_maxemail.cpp:54
Reference< NickCore > nc
Definition: commands.h:61
const char * c_str() const
Definition: anope.h:117
Anope::string email
Definition: account.h:116
#define _(x)
Definition: services.h:50