Skip to content

Commit

Permalink
Refactor around translatePassword
Browse files Browse the repository at this point in the history
Change types for passed password from wstring to string, which removes converting
back and forth in most cases. Move the narrow_to_wide conversion, where its neccessary,
closer to irrlicht. Remove trailing spaces in guiPasswordChange.cpp. Make parameters for
translatePassword passed as const reference.
  • Loading branch information
est31 committed Apr 17, 2015
1 parent 821d002 commit f0a1379
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/client.cpp
Expand Up @@ -1062,8 +1062,8 @@ void Client::sendChatMessage(const std::wstring &message)
Send(&pkt);
}

void Client::sendChangePassword(const std::wstring &oldpassword,
const std::wstring &newpassword)
void Client::sendChangePassword(const std::string &oldpassword,
const std::string &newpassword)
{
Player *player = m_env.getLocalPlayer();
if(player == NULL)
Expand Down
4 changes: 2 additions & 2 deletions src/client.h
Expand Up @@ -407,8 +407,8 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
const std::map<std::string, std::string> &fields);
void sendInventoryAction(InventoryAction *a);
void sendChatMessage(const std::wstring &message);
void sendChangePassword(const std::wstring &oldpassword,
const std::wstring &newpassword);
void sendChangePassword(const std::string &oldpassword,
const std::string &newpassword);
void sendDamage(u8 damage);
void sendBreath(u16 breath);
void sendRespawn();
Expand Down
2 changes: 1 addition & 1 deletion src/client/clientlauncher.cpp
Expand Up @@ -392,7 +392,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
else
playername = menudata.name;

password = translatePassword(playername, narrow_to_wide(menudata.password));
password = translatePassword(playername, menudata.password);

g_settings->set("name", playername);

Expand Down
17 changes: 9 additions & 8 deletions src/guiPasswordChange.cpp
Expand Up @@ -79,7 +79,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
Remove stuff
*/
removeChildren();

/*
Calculate new sizes and positions
*/
Expand All @@ -89,7 +89,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
screensize.X/2 + 580/2,
screensize.Y/2 + 300/2
);

DesiredRect = rect;
recalculateAbsolutePosition(false);

Expand All @@ -112,7 +112,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
{
core::rect<s32> rect(0, 0, 230, 30);
rect += topleft_client + v2s32(160, ypos);
gui::IGUIEditBox *e =
gui::IGUIEditBox *e =
Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
Environment->setFocus(e);
e->setPasswordBox(true);
Expand All @@ -128,7 +128,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
{
core::rect<s32> rect(0, 0, 230, 30);
rect += topleft_client + v2s32(160, ypos);
gui::IGUIEditBox *e =
gui::IGUIEditBox *e =
Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
e->setPasswordBox(true);
}
Expand All @@ -143,7 +143,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
{
core::rect<s32> rect(0, 0, 230, 30);
rect += topleft_client + v2s32(160, ypos);
gui::IGUIEditBox *e =
gui::IGUIEditBox *e =
Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
e->setPasswordBox(true);
}
Expand All @@ -162,7 +162,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
core::rect<s32> rect(0, 0, 300, 20);
rect += topleft_client + v2s32(35, ypos);
text = wgettext("Passwords do not match!");
IGUIElement *e =
IGUIElement *e =
Environment->addStaticText(
text,
rect, false, true, this, ID_message);
Expand All @@ -177,7 +177,7 @@ void GUIPasswordChange::drawMenu()
if (!skin)
return;
video::IVideoDriver* driver = Environment->getVideoDriver();

video::SColor bgcolor(140,0,0,0);
driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);

Expand All @@ -203,7 +203,8 @@ bool GUIPasswordChange::acceptInput()
e->setVisible(true);
return false;
}
m_client->sendChangePassword(oldpass, newpass);
m_client->sendChangePassword(wide_to_narrow(oldpass),
wide_to_narrow(newpass));
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions src/network/serverpackethandler.cpp
Expand Up @@ -267,8 +267,8 @@ void Server::handleCommand_Auth(NetworkPacket* pkt)
DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_EMPTY_PASSWORD);
return;
}
std::wstring raw_default_password =
narrow_to_wide(g_settings->get("default_password"));
std::string raw_default_password =
g_settings->get("default_password");
std::string initial_password =
translatePassword(playername, raw_default_password);

Expand Down Expand Up @@ -571,8 +571,8 @@ void Server::handleCommand_Init_Legacy(NetworkPacket* pkt)
L"disallowed. Set a password and try again.");
return;
}
std::wstring raw_default_password =
narrow_to_wide(g_settings->get("default_password"));
std::string raw_default_password =
g_settings->get("default_password");
std::string initial_password =
translatePassword(playername, raw_default_password);

Expand Down
3 changes: 1 addition & 2 deletions src/script/lua_api/l_util.cpp
Expand Up @@ -255,8 +255,7 @@ int ModApiUtil::l_get_password_hash(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1);
std::string raw_password = luaL_checkstring(L, 2);
std::string hash = translatePassword(name,
narrow_to_wide(raw_password));
std::string hash = translatePassword(name, raw_password);
lua_pushstring(L, hash.c_str());
return 1;
}
Expand Down
5 changes: 3 additions & 2 deletions src/util/string.cpp
Expand Up @@ -181,12 +181,13 @@ std::string wide_to_narrow(const std::wstring &wcs)
// their password. (Exception : if the password field is
// blank, we send a blank password - this is for backwards
// compatibility with password-less players).
std::string translatePassword(std::string playername, std::wstring password)
std::string translatePassword(const std::string &playername,
const std::string &password)
{
if (password.length() == 0)
return "";

std::string slt = playername + wide_to_narrow(password);
std::string slt = playername + password;
SHA1 sha1;
sha1.addBytes(slt.c_str(), slt.length());
unsigned char *digest = sha1.getDigest();
Expand Down
3 changes: 2 additions & 1 deletion src/util/string.h
Expand Up @@ -43,7 +43,8 @@ wchar_t *narrow_to_wide_c(const char *str);

std::wstring narrow_to_wide(const std::string &mbs);
std::string wide_to_narrow(const std::wstring &wcs);
std::string translatePassword(std::string playername, std::wstring password);
std::string translatePassword(const std::string &playername,
const std::string &password);
std::string urlencode(std::string str);
std::string urldecode(std::string str);
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
Expand Down

0 comments on commit f0a1379

Please sign in to comment.