diff options
Diffstat (limited to 'modules/core/ns_set_email.cpp')
-rw-r--r-- | modules/core/ns_set_email.cpp | 80 |
1 files changed, 75 insertions, 5 deletions
diff --git a/modules/core/ns_set_email.cpp b/modules/core/ns_set_email.cpp index 6d6d97795..f6374f240 100644 --- a/modules/core/ns_set_email.cpp +++ b/modules/core/ns_set_email.cpp @@ -13,6 +13,39 @@ #include "module.h" +static bool SendConfirmMail(User *u) +{ + int chars[] = { + ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', + 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', + 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', + 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', + 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' + }; + int idx, min = 1, max = 62; + Anope::string code; + for (idx = 0; idx < 9; ++idx) + code += chars[1 + static_cast<int>((static_cast<float>(max - min)) * getrandom16() / 65536.0) + min]; + u->Account()->Extend("ns_set_email_passcode", new ExtensibleItemRegular<Anope::string>(code)); + + Anope::string subject = _("Email confirmation"); + Anope::string message = Anope::printf(_("Hi,\n" + " \n" + "You have requested to change your email address to %s.\n" + "Please type \" %R%s confirm %s \" to confirm this change.\n" + " \n" + "If you don't know why this mail was sent to you, please ignore it silently.\n" + " \n" + "%s administrators."), u->Account()->email.c_str(), Config->s_NickServ.c_str(), code.c_str(), Config->NetworkName.c_str()); + + if (Config->UseStrictPrivMsg) + message = message.replace_all_cs("%R", "/"); + else + message = message.replace_all_cs("%R", "/msg "); + + return Mail(u, u->Account(), NickServ, subject, message); +} + class CommandNSSetEmail : public Command { public: @@ -47,15 +80,27 @@ class CommandNSSetEmail : public Command return MOD_CONT; } - if (!param.empty()) + if (!param.empty() && Config->NSConfirmEmailChanges && !u->Account()->IsServicesOper()) { - nc->email = param; - source.Reply(_("E-mail address for \002%s\002 changed to \002%s\002."), nc->display.c_str(), param.c_str()); + u->Account()->Extend("ns_set_email", new ExtensibleItemRegular<Anope::string>(param)); + Anope::string old = u->Account()->email; + u->Account()->email = param; + if (SendConfirmMail(u)) + source.Reply(_("A confirmation email has been sent to \002%s\002. Follow the instructions in it to change your email address."), param.c_str()); + u->Account()->email = old; } else { - nc->email.clear(); - source.Reply(_("E-mail address for \002%s\002 unset."), nc->display.c_str()); + if (!param.empty()) + { + nc->email = param; + source.Reply(_("E-mail address for \002%s\002 changed to \002%s\002."), nc->display.c_str(), param.c_str()); + } + else + { + nc->email.clear(); + source.Reply(_("E-mail address for \002%s\002 unset."), nc->display.c_str()); + } } return MOD_CONT; @@ -99,6 +144,8 @@ class NSSetEmail : public Module this->SetAuthor("Anope"); this->SetType(CORE); + ModuleManager::Attach(I_OnPreCommand, this); + Command *c = FindCommand(NickServ, "SET"); if (c) c->AddSubcommand(this, &commandnssetemail); @@ -118,6 +165,29 @@ class NSSetEmail : public Module if (c) c->DelSubcommand(&commandnssasetemail); } + + EventReturn OnPreCommand(CommandSource &source, Command *command, const std::vector<Anope::string> ¶ms) + { + User *u = source.u; + if (command->service == NickServ && command->name.equals_ci("CONFIRM") && !params.empty() && u->IsIdentified()) + { + Anope::string new_email, passcode; + if (u->Account()->GetExtRegular("ns_set_email", new_email) && u->Account()->GetExtRegular("ns_set_email_passcode", passcode)) + { + if (params[0] == passcode) + { + u->Account()->email = new_email; + Log(LOG_COMMAND, u, command) << "to confirm their email address change to " << u->Account()->email; + source.Reply(_("Your email address has been changed to \002%s\002."), u->Account()->email.c_str()); + u->Account()->Shrink("ns_set_email"); + u->Account()->Shrink("ns_set_email_passcode"); + return EVENT_STOP; + } + } + } + + return EVENT_CONTINUE; + } }; MODULE_INIT(NSSetEmail) |