Anope IRC Services  Version 2.0
m_sql_authentication.cpp
Go to the documentation of this file.
1 #include "module.h"
2 #include "modules/sql.h"
3 
4 static Module *me;
5 
7 {
10 
11  public:
12  SQLAuthenticationResult(User *u, IdentifyRequest *r) : SQL::Interface(me), user(u), req(r)
13  {
14  req->Hold(me);
15  }
16 
18  {
19  req->Release(me);
20  }
21 
23  {
24  if (r.Rows() == 0)
25  {
26  Log(LOG_DEBUG) << "m_sql_authentication: Unsuccessful authentication for " << req->GetAccount();
27  delete this;
28  return;
29  }
30 
31  Log(LOG_DEBUG) << "m_sql_authentication: Successful authentication for " << req->GetAccount();
32 
33  Anope::string email;
34  try
35  {
36  email = r.Get(0, "email");
37  }
38  catch (const SQL::Exception &) { }
39 
40  NickAlias *na = NickAlias::Find(req->GetAccount());
41  BotInfo *NickServ = Config->GetClient("NickServ");
42  if (na == NULL)
43  {
44  na = new NickAlias(req->GetAccount(), new NickCore(req->GetAccount()));
45  FOREACH_MOD(OnNickRegister, (user, na, ""));
46  if (user && NickServ)
47  user->SendMessage(NickServ, _("Your account \002%s\002 has been successfully created."), na->nick.c_str());
48  }
49 
50  if (!email.empty() && email != na->nc->email)
51  {
52  na->nc->email = email;
53  if (user && NickServ)
54  user->SendMessage(NickServ, _("Your email has been updated to \002%s\002."), email.c_str());
55  }
56 
57  req->Success(me);
58  delete this;
59  }
60 
62  {
63  Log(this->owner) << "m_sql_authentication: Error executing query " << r.GetQuery().query << ": " << r.GetError();
64  delete this;
65  }
66 };
67 
69 {
73 
75 
76  public:
77  ModuleSQLAuthentication(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR)
78  {
79  me = this;
80 
81  }
82 
84  {
85  Configuration::Block *config = conf->GetModule(this);
86  this->engine = config->Get<const Anope::string>("engine");
87  this->query = config->Get<const Anope::string>("query");
88  this->disable_reason = config->Get<const Anope::string>("disable_reason");
89  this->disable_email_reason = config->Get<Anope::string>("disable_email_reason");
90 
91  this->SQL = ServiceReference<SQL::Provider>("SQL::Provider", this->engine);
92  }
93 
94  EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> &params) anope_override
95  {
96  if (!this->disable_reason.empty() && (command->name == "nickserv/register" || command->name == "nickserv/group"))
97  {
98  source.Reply(this->disable_reason);
99  return EVENT_STOP;
100  }
101 
102  if (!this->disable_email_reason.empty() && command->name == "nickserv/set/email")
103  {
104  source.Reply(this->disable_email_reason);
105  return EVENT_STOP;
106  }
107 
108  return EVENT_CONTINUE;
109  }
110 
112  {
113  if (!this->SQL)
114  {
115  Log(this) << "Unable to find SQL engine";
116  return;
117  }
118 
119  SQL::Query q(this->query);
120  q.SetValue("a", req->GetAccount());
121  q.SetValue("p", req->GetPassword());
122  if (u)
123  {
124  q.SetValue("n", u->nick);
125  q.SetValue("i", u->ip.addr());
126  }
127  else
128  {
129  q.SetValue("n", "");
130  q.SetValue("i", "");
131  }
132 
133 
134  this->SQL->Run(new SQLAuthenticationResult(u, req), q);
135 
136  Log(LOG_DEBUG) << "m_sql_authentication: Checking authentication for " << req->GetAccount();
137  }
138 };
139 
Serialize::Reference< NickCore > nc
Definition: account.h:47
void Release(Module *m)
Definition: account.cpp:37
Definition: bots.h:24
Module * owner
Definition: sql.h:186
ServiceReference< SQL::Provider > SQL
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
void Hold(Module *m)
Definition: account.cpp:32
void OnResult(const SQL::Result &r) anope_override
Anope::string nick
Definition: account.h:37
Definition: users.h:34
void SetValue(const Anope::string &key, const T &value, bool escape=true)
Definition: sql.h:121
#define FOREACH_MOD(ename, args)
Definition: modules.h:62
ModuleSQLAuthentication(const Anope::string &modname, const Anope::string &creator)
Interface(Module *m)
Definition: sql.h:188
Definition: sql.h:96
Definition: Config.cs:26
static Module * me
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
SQLAuthenticationResult(User *u, IdentifyRequest *r)
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
Definition: sql.h:8
void OnError(const SQL::Result &r) anope_override
void Success(Module *m)
Definition: account.cpp:48
void OnReload(Configuration::Conf *conf) anope_override
EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector< Anope::string > &params) anope_override
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
T Get(const Anope::string &tag)
Definition: config.h:44
void SendMessage(BotInfo *source, const char *fmt,...)
Definition: users.cpp:320
#define _(x)
Definition: services.h:50
Definition: modules.h:163
const Anope::string & GetAccount() const
Definition: account.h:250
void OnCheckAuthentication(User *u, IdentifyRequest *req) anope_override