aboutsummaryrefslogtreecommitdiff
path: root/src/guiPasswordChange.cpp
blob: e4b77b611193ccbe2429c9ce2bc1991f500f1201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Part of Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "guiPasswordChange.h"
#include "debug.h"
#include "serialization.h"
#include <string>
#include <IGUICheckBox.h>
#include <IGUIEditBox.h>
#include <IGUIButton.h>
#include <IGUIStaticText.h>
#include <IGUIFont.h>

#include "gettext.h"

const int ID_oldPassword = 256;
const int ID_newPassword1 = 257;
const int ID_newPassword2 = 258;
const int ID_change = 259;
const int ID_message = 260;

GUIPasswordChange::GUIPasswordChange(gui::IGUIEnvironment* env,
		gui::IGUIElement* parent, s32 id,
		IMenuManager *menumgr,
		Client* client
):
	GUIModalMenu(env, parent, id, menumgr),
	m_client(client)
{
}

GUIPasswordChange::~GUIPasswordChange()
{
	removeChildren();
}

void GUIPasswordChange::removeChildren()
{
	{
		gui::IGUIElement *e = getElementFromId(ID_oldPassword);
		if(e != NULL)
			e->remove();
	}
	{
		gui::IGUIElement *e = getElementFromId(ID_newPassword1);
		if(e != NULL)
			e->remove();
	}
	{
		gui::IGUIElement *e = getElementFromId(ID_newPassword2);
		if(e != NULL)
			e->remove();
	}
	{
		gui::IGUIElement *e = getElementFromId(ID_change);
		if(e != NULL)
			e->remove();
	}
}

void GUIPasswordChange::regenerateGui(v2u32 screensize)
{
	/*
		Remove stuff
	*/
	removeChildren();
	
	/*
		Calculate new sizes and positions
	*/
	core::rect<s32> rect(
			screensize.X/2 - 580/2,
			screensize.Y/2 - 300/2,
			screensize.X/2 + 580/2,
			screensize.Y/2 + 300/2
	);
	
	DesiredRect = rect;
	recalculateAbsolutePosition(false);

	v2s32 size = rect.getSize();
	v2s32 topleft_client(40, 0);
	v2s32 size_client = size - v2s32(40, 0);

	/*
		Add stuff
	*/
	s32 ypos = 50;
	changeCtype("");
	{
		core::rect<s32> rect(0, 0, 110, 20);
		rect += topleft_client + v2s32(35, ypos+6);
		Environment->addStaticText(wgettext("Old Password"),
			rect, false, true, this, -1);
	}
	changeCtype("C");
	{
		core::rect<s32> rect(0, 0, 230, 30);
		rect += topleft_client + v2s32(160, ypos);
		gui::IGUIEditBox *e = 
		Environment->addEditBox(L"", rect, true, this, ID_oldPassword);
		Environment->setFocus(e);
		e->setPasswordBox(true);
	}
	ypos += 50;
	changeCtype("");
	{
		core::rect<s32> rect(0, 0, 110, 20);
		rect += topleft_client + v2s32(35, ypos+6);
		Environment->addStaticText(wgettext("New Password"),
			rect, false, true, this, -1);
	}
	changeCtype("C");
	{
		core::rect<s32> rect(0, 0, 230, 30);
		rect += topleft_client + v2s32(160, ypos);
		gui::IGUIEditBox *e = 
		Environment->addEditBox(L"", rect, true, this, ID_newPassword1);
		e->setPasswordBox(true);
	}
	ypos += 50;
	changeCtype("");
	{
		core::rect<s32> rect(0, 0, 110, 20);
		rect += topleft_client + v2s32(35, ypos+6);
		Environment->addStaticText(wgettext("Confirm Password"),
			rect, false, true, this, -1);
	}
	changeCtype("C");
	{
		core::rect<s32> rect(0, 0, 230, 30);
		rect += topleft_client + v2s32(160, ypos);
		gui::IGUIEditBox *e = 
		Environment->addEditBox(L"", rect, true, this, ID_newPassword2);
		e->setPasswordBox(true);
	}

	ypos += 50;
	changeCtype("");
	{
		core::rect<s32> rect(0, 0, 140, 30);
		rect = rect + v2s32(size.X/2-140/2, ypos);
		Environment->addButton(rect, this, ID_change, wgettext("Change"));
	}

	ypos += 50;
	{
		core::rect<s32> rect(0, 0, 300, 20);
		rect += topleft_client + v2s32(35, ypos);
		IGUIElement *e = 
		Environment->addStaticText(
			wgettext("Passwords do not match!"),
			rect, false, true, this, ID_message);
		e->setVisible(false);
	}
	changeCtype("C");

}

void GUIPasswordChange::drawMenu()
{
	gui::IGUISkin* skin = Environment->getSkin();
	if (!skin)
		return;
	video::IVideoDriver* driver = Environment->getVideoDriver();
	
	video::SColor bgcolor(140,0,0,0);
	driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);

	gui::IGUIElement::draw();
}

bool GUIPasswordChange::acceptInput()
{
		std::wstring oldpass;
		std::wstring newpass;
		gui::IGUIElement *e;
		e = getElementFromId(ID_oldPassword);
		if(e != NULL)
			oldpass = e->getText();
		e = getElementFromId(ID_newPassword1);
		if(e != NULL)
			newpass = e->getText();
		e = getElementFromId(ID_newPassword2);
		if(e != NULL && newpass != e->getText())
		{
			e = getElementFromId(ID_message);
			if(e != NULL)
				e->setVisible(true);
			return false;
		}
		m_client->sendChangePassword(oldpass, newpass);
		return true;
}

bool GUIPasswordChange::OnEvent(const SEvent& event)
{
	if(event.EventType==EET_KEY_INPUT_EVENT)
	{
		if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
		{
			quitMenu();
			return true;
		}
		if(event.KeyInput.Key==KEY_RETURN && event.KeyInput.PressedDown)
		{
			if(acceptInput())
				quitMenu();
			return true;
		}
	}
	if(event.EventType==EET_GUI_EVENT)
	{
		if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
				&& isVisible())
		{
			if(!canTakeFocus(event.GUIEvent.Element))
			{
				dstream<<"GUIPasswordChange: Not allowing focus change."
						<<std::endl;
				// Returning true disables focus change
				return true;
			}
		}
		if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
		{
			switch(event.GUIEvent.Caller->getID())
			{
			case ID_change:
				if(acceptInput())
					quitMenu();
				return true;
			}
		}
		if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
		{
			switch(event.GUIEvent.Caller->getID())
			{
			case ID_oldPassword:
			case ID_newPassword1:
			case ID_newPassword2:
				if(acceptInput())
					quitMenu();
				return true;
			}
		}
	}

	return Parent ? Parent->OnEvent(event) : false;
}

n> #include "log.h" #include "util/quicktune.h" #include "httpfetch.h" #include "gameparams.h" #include "database/database.h" #include "config.h" #include "player.h" #include "porting.h" #include "network/socket.h" #include "mapblock.h" #if USE_CURSES #include "terminal_chat_console.h" #endif #ifndef SERVER #include "gui/guiMainMenu.h" #include "client/clientlauncher.h" #include "gui/guiEngine.h" #include "gui/mainmenumanager.h" #endif #ifdef HAVE_TOUCHSCREENGUI #include "gui/touchscreengui.h" #endif // for version information only extern "C" { #if USE_LUAJIT #include <luajit.h> #else #include <lua.h> #endif } #if !defined(__cpp_rtti) || !defined(__cpp_exceptions) #error Minetest cannot be built without exceptions or RTTI #endif #if defined(__MINGW32__) && !defined(__MINGW64__) && !defined(__clang__) && \ (__GNUC__ < 11 || (__GNUC__ == 11 && __GNUC_MINOR__ < 1)) // see e.g. https://github.com/minetest/minetest/issues/10137 #warning ================================== #warning 32-bit MinGW gcc before 11.1 has known issues with crashes on thread exit, you should upgrade. #warning ================================== #endif #define DEBUGFILE "debug.txt" #define DEFAULT_SERVER_PORT 30000 typedef std::map<std::string, ValueSpec> OptionList; /********************************************************************** * Private functions **********************************************************************/ static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args); static void set_allowed_options(OptionList *allowed_options); static void print_help(const OptionList &allowed_options); static void print_allowed_options(const OptionList &allowed_options); static void print_version(); static void print_worldspecs(const std::vector<WorldSpec> &worldspecs, std::ostream &os, bool print_name = true, bool print_path = true); static void print_modified_quicktune_values(); static void list_game_ids(); static void list_worlds(bool print_name, bool print_path); static bool setup_log_params(const Settings &cmd_args); static bool create_userdata_path(); static bool init_common(const Settings &cmd_args, int argc, char *argv[]); static void uninit_common(); static void startup_message(); static bool read_config_file(const Settings &cmd_args); static void init_log_streams(const Settings &cmd_args); static bool game_configure(GameParams *game_params, const Settings &cmd_args); static void game_configure_port(GameParams *game_params, const Settings &cmd_args); static bool game_configure_world(GameParams *game_params, const Settings &cmd_args); static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args); static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args); static bool auto_select_world(GameParams *game_params); static std::string get_clean_world_path(const std::string &path); static bool game_configure_subgame(GameParams *game_params, const Settings &cmd_args); static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_args); static bool determine_subgame(GameParams *game_params); static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args); static bool migrate_map_database(const GameParams &game_params, const Settings &cmd_args); static bool recompress_map_database(const GameParams &game_params, const Settings &cmd_args, const Address &addr); /**********************************************************************/ FileLogOutput file_log_output; static OptionList allowed_options; int main(int argc, char *argv[]) { int retval; debug_set_exception_handler(); g_logger.registerThread("Main"); g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION); Settings cmd_args; bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args); if (!cmd_args_ok || cmd_args.getFlag("help") || cmd_args.exists("nonopt1")) { porting::attachOrCreateConsole(); print_help(allowed_options); return cmd_args_ok ? 0 : 1; } if (cmd_args.getFlag("console")) porting::attachOrCreateConsole(); if (cmd_args.getFlag("version")) { porting::attachOrCreateConsole(); print_version(); return 0; } if (!setup_log_params(cmd_args)) return 1; porting::signal_handler_init(); #ifdef __ANDROID__ porting::initAndroid(); porting::initializePathsAndroid(); #else porting::initializePaths(); #endif if (!create_userdata_path()) { errorstream << "Cannot create user data directory" << std::endl; return 1; } // Debug handler BEGIN_DEBUG_EXCEPTION_HANDLER // List gameids if requested if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") { list_game_ids(); return 0; } // List worlds, world names, and world paths if requested if (cmd_args.exists("worldlist")) { if (cmd_args.get("worldlist") == "name") { list_worlds(true, false); } else if (cmd_args.get("worldlist") == "path") { list_worlds(false, true); } else if (cmd_args.get("worldlist") == "both") { list_worlds(true, true); } else { errorstream << "Invalid --worldlist value: " << cmd_args.get("worldlist") << std::endl; return 1; } return 0; } if (!init_common(cmd_args, argc, argv)) return 1; if (g_settings->getBool("enable_console")) porting::attachOrCreateConsole(); #ifndef __ANDROID__ // Run unit tests if (cmd_args.getFlag("run-unittests")) { #if BUILD_UNITTESTS return run_tests(); #else errorstream << "Unittest support is not enabled in this binary. " << "If you want to enable it, compile project with BUILD_UNITTESTS=1 flag." << std::endl; return 1; #endif } // Run benchmarks if (cmd_args.getFlag("run-benchmarks")) { #if BUILD_BENCHMARKS return run_benchmarks(); #else errorstream << "Benchmark support is not enabled in this binary. " << "If you want to enable it, compile project with BUILD_BENCHMARKS=1 flag." << std::endl; return 1; #endif } #endif // __ANDROID__ GameStartData game_params; #ifdef SERVER porting::attachOrCreateConsole(); game_params.is_dedicated_server = true; #else const bool isServer = cmd_args.getFlag("server"); if (isServer) porting::attachOrCreateConsole(); game_params.is_dedicated_server = isServer; #endif if (!game_configure(&game_params, cmd_args)) return 1; sanity_check(!game_params.world_path.empty()); if (game_params.is_dedicated_server) return run_dedicated_server(game_params, cmd_args) ? 0 : 1; #ifndef SERVER retval = ClientLauncher().run(game_params, cmd_args) ? 0 : 1; #else retval = 0; #endif // Update configuration file if (!g_settings_path.empty()) g_settings->updateConfigFile(g_settings_path.c_str()); print_modified_quicktune_values(); END_DEBUG_EXCEPTION_HANDLER return retval; } /***************************************************************************** * Startup / Init *****************************************************************************/ static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args) { set_allowed_options(&allowed_options); return cmd_args->parseCommandLine(argc, argv, allowed_options); } static void set_allowed_options(OptionList *allowed_options) { allowed_options->clear(); allowed_options->insert(std::make_pair("help", ValueSpec(VALUETYPE_FLAG, _("Show allowed options")))); allowed_options->insert(std::make_pair("version", ValueSpec(VALUETYPE_FLAG, _("Show version information")))); allowed_options->insert(std::make_pair("config", ValueSpec(VALUETYPE_STRING, _("Load configuration from specified file")))); allowed_options->insert(std::make_pair("port", ValueSpec(VALUETYPE_STRING, _("Set network port (UDP)")))); allowed_options->insert(std::make_pair("run-unittests", ValueSpec(VALUETYPE_FLAG, _("Run the unit tests and exit")))); allowed_options->insert(std::make_pair("run-benchmarks", ValueSpec(VALUETYPE_FLAG, _("Run the benchmarks and exit")))); allowed_options->insert(std::make_pair("map-dir", ValueSpec(VALUETYPE_STRING, _("Same as --world (deprecated)")))); allowed_options->insert(std::make_pair("world", ValueSpec(VALUETYPE_STRING, _("Set world path (implies local game if used with option --go)")))); allowed_options->insert(std::make_pair("worldname", ValueSpec(VALUETYPE_STRING, _("Set world by name (implies local game if used with option --go)")))); allowed_options->insert(std::make_pair("worldlist", ValueSpec(VALUETYPE_STRING, _("Get list of worlds ('path' lists paths, " "'name' lists names, 'both' lists both)")))); allowed_options->insert(std::make_pair("quiet", ValueSpec(VALUETYPE_FLAG, _("Print to console errors only")))); allowed_options->insert(std::make_pair("color", ValueSpec(VALUETYPE_STRING, _("Coloured logs ('always', 'never' or 'auto'), defaults to 'auto'" )))); allowed_options->insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG, _("Print more information to console")))); allowed_options->insert(std::make_pair("verbose", ValueSpec(VALUETYPE_FLAG, _("Print even more information to console")))); allowed_options->insert(std::make_pair("trace", ValueSpec(VALUETYPE_FLAG, _("Print enormous amounts of information to log and console")))); allowed_options->insert(std::make_pair("logfile", ValueSpec(VALUETYPE_STRING, _("Set logfile path ('' = no logging)")))); allowed_options->insert(std::make_pair("gameid", ValueSpec(VALUETYPE_STRING, _("Set gameid (\"--gameid list\" prints available ones)")))); allowed_options->insert(std::make_pair("migrate", ValueSpec(VALUETYPE_STRING, _("Migrate from current map backend to another (Only works when using minetestserver or with --server)")))); allowed_options->insert(std::make_pair("migrate-players", ValueSpec(VALUETYPE_STRING, _("Migrate from current players backend to another (Only works when using minetestserver or with --server)")))); allowed_options->insert(std::make_pair("migrate-auth", ValueSpec(VALUETYPE_STRING, _("Migrate from current auth backend to another (Only works when using minetestserver or with --server)")))); allowed_options->insert(std::make_pair("migrate-mod-storage", ValueSpec(VALUETYPE_STRING, _("Migrate from current mod storage backend to another (Only works when using minetestserver or with --server)")))); allowed_options->insert(std::make_pair("terminal", ValueSpec(VALUETYPE_FLAG, _("Feature an interactive terminal (Only works when using minetestserver or with --server)")))); allowed_options->insert(std::make_pair("recompress", ValueSpec(VALUETYPE_FLAG, _("Recompress the blocks of the given map database.")))); #ifndef SERVER allowed_options->insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG, _("Run speed tests")))); allowed_options->insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING, _("Address to connect to. ('' = local game)")))); allowed_options->insert(std::make_pair("random-input", ValueSpec(VALUETYPE_FLAG, _("Enable random user input, for testing")))); allowed_options->insert(std::make_pair("server", ValueSpec(VALUETYPE_FLAG, _("Run dedicated server")))); allowed_options->insert(std::make_pair("name", ValueSpec(VALUETYPE_STRING, _("Set player name")))); allowed_options->insert(std::make_pair("password", ValueSpec(VALUETYPE_STRING, _("Set password")))); allowed_options->insert(std::make_pair("password-file", ValueSpec(VALUETYPE_STRING, _("Set password from contents of file")))); allowed_options->insert(std::make_pair("go", ValueSpec(VALUETYPE_FLAG, _("Disable main menu")))); allowed_options->insert(std::make_pair("console", ValueSpec(VALUETYPE_FLAG, _("Starts with the console (Windows only)")))); #endif } static void print_help(const OptionList &allowed_options) { std::cout << _("Allowed options:") << std::endl; print_allowed_options(allowed_options); } static void print_allowed_options(const OptionList &allowed_options) { for (const auto &allowed_option : allowed_options) { std::ostringstream os1(std::ios::binary); os1 << " --" << allowed_option.first; if (allowed_option.second.type != VALUETYPE_FLAG) os1 << _(" <value>"); std::cout << padStringRight(os1.str(), 30); if (allowed_option.second.help) std::cout << allowed_option.second.help; std::cout << std::endl; } } static void print_version() { std::cout << PROJECT_NAME_C " " << g_version_hash << " (" << porting::getPlatformName() << ")" << std::endl; #ifndef SERVER std::cout << "Using Irrlicht " IRRLICHT_SDK_VERSION << std::endl; #endif #if USE_LUAJIT std::cout << "Using " << LUAJIT_VERSION << std::endl; #else std::cout << "Using " << LUA_RELEASE << std::endl; #endif std::cout << g_build_info << std::endl; } static void list_game_ids() { std::set<std::string> gameids = getAvailableGameIds(); for (const std::string &gameid : gameids) std::cout << gameid <<std::endl; } static void list_worlds(bool print_name, bool print_path) { std::cout << _("Available worlds:") << std::endl; std::vector<WorldSpec> worldspecs = getAvailableWorlds(); print_worldspecs(worldspecs, std::cout, print_name, print_path); } static void print_worldspecs(const std::vector<WorldSpec> &worldspecs, std::ostream &os, bool print_name, bool print_path) { for (const WorldSpec &worldspec : worldspecs) { std::string name = worldspec.name; std::string path = worldspec.path; if (print_name && print_path) { os << "\t" << name << "\t\t" << path << std::endl; } else if (print_name) { os << "\t" << name << std::endl; } else if (print_path) { os << "\t" << path << std::endl; } } } static void print_modified_quicktune_values() { bool header_printed = false; std::vector<std::string> names = getQuicktuneNames(); for (const std::string &name : names) { QuicktuneValue val = getQuicktuneValue(name); if (!val.modified) continue; if (!header_printed) { dstream << "Modified quicktune values:" << std::endl; header_printed = true; } dstream << name << " = " << val.getString() << std::endl; } } static bool setup_log_params(const Settings &cmd_args) { // Quiet mode, print errors only if (cmd_args.getFlag("quiet")) { g_logger.removeOutput(&stderr_output); g_logger.addOutputMaxLevel(&stderr_output, LL_ERROR); } // Coloured log messages (see log.h) std::string color_mode; if (cmd_args.exists("color")) { color_mode = cmd_args.get("color"); #if !defined(_WIN32) } else { char *color_mode_env = getenv("MT_LOGCOLOR"); if (color_mode_env) color_mode = color_mode_env; #endif } if (color_mode != "") { if (color_mode == "auto") { Logger::color_mode = LOG_COLOR_AUTO; } else if (color_mode == "always") { Logger::color_mode = LOG_COLOR_ALWAYS; } else if (color_mode == "never") { Logger::color_mode = LOG_COLOR_NEVER; } else { errorstream << "Invalid color mode: " << color_mode << std::endl; return false; } } // In certain cases, output info level on stderr if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") || cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests")) g_logger.addOutput(&stderr_output, LL_INFO);