summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2013-09-27 20:07:07 -0400
committerAdam <Adam@anope.org>2013-09-27 20:07:07 -0400
commit4b059beb247a12726d9a471433a27a39190d583b (patch)
treef6daaf13fb81e5c591f02f6cac90687ec592e2c9
parentb319fb089c144312ab9141d6f6469d50d9d7a1ea (diff)
Replace this silly random code generation code with something more sane looking
-rw-r--r--include/anope.h5
-rw-r--r--modules/commands/ns_register.cpp11
-rw-r--r--modules/commands/ns_resetpass.cpp17
-rw-r--r--modules/commands/ns_set.cpp12
-rw-r--r--src/misc.cpp15
5 files changed, 24 insertions, 36 deletions
diff --git a/include/anope.h b/include/anope.h
index e4a654b78..6c31c879f 100644
--- a/include/anope.h
+++ b/include/anope.h
@@ -535,6 +535,11 @@ namespace Anope
* @return the IP if it was found, else the host
*/
extern Anope::string Resolve(const Anope::string &host, int type);
+
+ /** Generate a string of random letters and numbers
+ * @param len The length of the string returned
+ */
+ extern Anope::string Random(size_t len);
}
/** sepstream allows for splitting token seperated lists.
diff --git a/modules/commands/ns_register.cpp b/modules/commands/ns_register.cpp
index 73e72803b..7fd19b9d9 100644
--- a/modules/commands/ns_register.cpp
+++ b/modules/commands/ns_register.cpp
@@ -391,16 +391,7 @@ static bool SendRegmail(User *u, const NickAlias *na, BotInfo *bi)
if (code == NULL)
{
code = na->nc->Extend<Anope::string>("passcode");
- 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;
- for (idx = 0; idx < 9; ++idx)
- *code += chars[1 + static_cast<int>((static_cast<float>(max - min)) * static_cast<uint16_t>(rand()) / 65536.0) + min];
+ *code = Anope::Random(9);
}
Anope::string subject = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("registration_subject").c_str()),
diff --git a/modules/commands/ns_resetpass.cpp b/modules/commands/ns_resetpass.cpp
index 4134155c4..fd0d582fb 100644
--- a/modules/commands/ns_resetpass.cpp
+++ b/modules/commands/ns_resetpass.cpp
@@ -120,22 +120,9 @@ class NSResetPass : public Module
static bool SendResetEmail(User *u, const NickAlias *na, BotInfo *bi)
{
- int min = 1, max = 62;
- 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',
- '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'
- };
-
- Anope::string passcode;
- int idx;
- for (idx = 0; idx < 20; ++idx)
- passcode += chars[1 + static_cast<int>((static_cast<float>(max - min)) * static_cast<uint16_t>(rand()) / 65536.0) + min];
-
Anope::string subject = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("reset_subject").c_str()),
- message = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("reset_message").c_str());
+ message = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("reset_message").c_str()),
+ passcode = Anope::Random(20);
subject = subject.replace_all_cs("%n", na->nick);
subject = subject.replace_all_cs("%N", Config->GetBlock("networkinfo")->Get<const Anope::string>("networkname"));
diff --git a/modules/commands/ns_set.cpp b/modules/commands/ns_set.cpp
index 11b6fd9d1..e561a1524 100644
--- a/modules/commands/ns_set.cpp
+++ b/modules/commands/ns_set.cpp
@@ -387,17 +387,7 @@ class CommandNSSetEmail : public Command
{
static bool SendConfirmMail(User *u, BotInfo *bi, const Anope::string &new_email)
{
- 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)) * static_cast<uint16_t>(rand()) / 65536.0) + min];
+ Anope::string code = Anope::Random(9);
std::pair<Anope::string, Anope::string> *n = u->Account()->Extend<std::pair<Anope::string, Anope::string> >("ns_set_email");
n->first = new_email;
diff --git a/src/misc.cpp b/src/misc.cpp
index 76f666d28..33ac56bcd 100644
--- a/src/misc.cpp
+++ b/src/misc.cpp
@@ -749,3 +749,18 @@ Anope::string Anope::Resolve(const Anope::string &host, int type)
return result;
}
+Anope::string Anope::Random(size_t len)
+{
+ char 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'
+ };
+ Anope::string buf;
+ for (size_t i = 0; i < len; ++i)
+ buf.append(chars[rand() % sizeof(chars)]);
+ return buf;
+}
+