Anope IRC Services  Version 2.0
ns_resetpass.cpp
Go to the documentation of this file.
1 /* NickServ core functions
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 #include "module.h"
13 
14 static bool SendResetEmail(User *u, const NickAlias *na, BotInfo *bi);
15 
17 {
18  public:
19  CommandNSResetPass(Module *creator) : Command(creator, "nickserv/resetpass", 2, 2)
20  {
21  this->SetDesc(_("Helps you reset lost passwords"));
22  this->SetSyntax(_("\037nickname\037 \037email\037"));
23  this->AllowUnregistered(true);
24  }
25 
26  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
27  {
28  const NickAlias *na;
29 
30  if (!(na = NickAlias::Find(params[0])))
31  source.Reply(NICK_X_NOT_REGISTERED, params[0].c_str());
32  else if (!na->nc->email.equals_ci(params[1]))
33  source.Reply(_("Incorrect email address."));
34  else
35  {
36  if (SendResetEmail(source.GetUser(), na, source.service))
37  {
38  Log(LOG_COMMAND, source, this) << "for " << na->nick << " (group: " << na->nc->display << ")";
39  source.Reply(_("Password reset email for \002%s\002 has been sent."), na->nick.c_str());
40  }
41  }
42 
43  return;
44  }
45 
46  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
47  {
48  this->SendSyntax(source);
49  source.Reply(" ");
50  source.Reply(_("Sends a passcode to the nickname with instructions on how to\n"
51  "reset their password. Email must be the email address associated\n"
52  "to the nickname."));
53  return true;
54  }
55 };
56 
57 struct ResetInfo
58 {
60  time_t time;
61 };
62 
63 class NSResetPass : public Module
64 {
67 
68  public:
69  NSResetPass(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
70  commandnsresetpass(this), reset(this, "reset")
71  {
72  if (!Config->GetBlock("mail")->Get<bool>("usemail"))
73  throw ModuleException("Not using mail.");
74  }
75 
76  EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> &params) anope_override
77  {
78  if (command->name == "nickserv/confirm" && params.size() > 1)
79  {
80  if (Anope::ReadOnly)
81  {
82  source.Reply(READ_ONLY_MODE);
83  return EVENT_STOP;
84  }
85 
86  NickAlias *na = NickAlias::Find(params[0]);
87 
88  ResetInfo *ri = na ? reset.Get(na->nc) : NULL;
89  if (na && ri)
90  {
91  NickCore *nc = na->nc;
92  const Anope::string &passcode = params[1];
93  if (ri->time < Anope::CurTime - 3600)
94  {
95  reset.Unset(nc);
96  source.Reply(_("Your password reset request has expired."));
97  }
98  else if (passcode.equals_cs(ri->code))
99  {
100  reset.Unset(nc);
101  nc->Shrink<bool>("UNCONFIRMED");
102 
103  Log(LOG_COMMAND, source, &commandnsresetpass) << "confirmed RESETPASS to forcefully identify as " << na->nick;
104 
105  if (source.GetUser())
106  {
107  source.GetUser()->Identify(na);
108  source.Reply(_("You are now identified for your nick. Change your password now."));
109  }
110  }
111  else
112  return EVENT_CONTINUE;
113 
114  return EVENT_STOP;
115  }
116  }
117 
118  return EVENT_CONTINUE;
119  }
120 };
121 
122 static bool SendResetEmail(User *u, const NickAlias *na, BotInfo *bi)
123 {
124  Anope::string subject = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("reset_subject").c_str()),
125  message = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("reset_message").c_str()),
126  passcode = Anope::Random(20);
127 
128  subject = subject.replace_all_cs("%n", na->nick);
129  subject = subject.replace_all_cs("%N", Config->GetBlock("networkinfo")->Get<const Anope::string>("networkname"));
130  subject = subject.replace_all_cs("%c", passcode);
131 
132  message = message.replace_all_cs("%n", na->nick);
133  message = message.replace_all_cs("%N", Config->GetBlock("networkinfo")->Get<const Anope::string>("networkname"));
134  message = message.replace_all_cs("%c", passcode);
135 
136  ResetInfo *ri = na->nc->Extend<ResetInfo>("reset");
137  ri->code = passcode;
138  ri->time = Anope::CurTime;
139 
140  return Mail::Send(u, na->nc, bi, subject, message);
141 }
142 
Serialize::Reference< NickCore > nc
Definition: account.h:47
Definition: bots.h:24
PrimitiveExtensibleItem< ResetInfo > reset
CoreExport bool ReadOnly
Definition: main.cpp:28
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
bool equals_cs(const char *_str) const
Definition: anope.h:74
NSResetPass(const Anope::string &modname, const Anope::string &creator)
CommandNSResetPass commandnsresetpass
Anope::string nick
Definition: account.h:37
time_t time
Definition: users.h:34
#define NICK_X_NOT_REGISTERED
Definition: language.h:79
#define READ_ONLY_MODE
Definition: language.h:71
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
void Unset(Extensible *obj) anope_override
Definition: extensible.h:95
CoreExport time_t CurTime
Definition: main.cpp:41
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: Config.cs:26
void Shrink(const Anope::string &name)
Definition: extensible.h:253
static bool SendResetEmail(User *u, const NickAlias *na, BotInfo *bi)
CommandNSResetPass(Module *creator)
string replace_all_cs(const string &_orig, const string &_repl) const
Definition: anope.h:229
CoreExport bool Send(User *from, NickCore *to, BotInfo *service, const Anope::string &subject, const Anope::string &message)
Definition: mail.cpp:54
#define anope_override
Definition: services.h:56
Anope::string code
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
CoreExport Anope::string Random(size_t len)
Definition: misc.cpp:754
CoreExport const char * Translate(const char *string)
Definition: language.cpp:59
bool AllowUnregistered() const
Definition: command.cpp:159
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
T * Get(const Extensible *obj) const
Definition: extensible.h:103
void SendSyntax(CommandSource &)
Definition: command.cpp:145
const char * c_str() const
Definition: anope.h:117
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: logger.h:53
EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector< Anope::string > &params) anope_override
#define _(x)
Definition: services.h:50