- added a simple command-line interface (you can now supply host and password on the command-line)

Mon, 15 Dec 2014 02:15:09 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 15 Dec 2014 02:15:09 +0200
changeset 30
21fba5183768
parent 29
e534f2f78196
child 31
b5b5a6a96d91

- added a simple command-line interface (you can now supply host and password on the command-line)
- the password is now masked with asterisks when being input

sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/main.cpp file | annotate | diff | comparison | revisions
--- a/sources/interface.cpp	Mon Dec 15 02:03:08 2014 +0200
+++ b/sources/interface.cpp	Mon Dec 15 02:15:09 2014 +0200
@@ -124,6 +124,7 @@
 	::noecho();
 	::refresh();
 	::timeout (0);
+	::use_default_colors();
 	g_title = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string());
 
 	for (int i = 0; i < NUM_COLORS; ++i)
@@ -174,8 +175,9 @@
 static FUNCTION
 safe_disconnect (Function<void()> afterwards) -> void
 {
-	if (RCONSession::get_session() != nullptr
-		and RCONSession::get_session()->state() != RCON_DISCONNECTED)
+	RCONSession* session = RCONSession::get_session();
+
+	if (session and session->state() != RCON_DISCONNECTED)
 	{
 		g_disconnectConfirmFunction = afterwards;
 		set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION);
@@ -247,9 +249,18 @@
 	int end = min<int> (g_input.length(), start + displaylength);
 	assert (g_cursor >= start and g_cursor <= end);
 
+	String displayinput = g_input;
+
+	// If we're inputting a password, replace it with asterisks
+	if (g_inputState == INPUTSTATE_PASSWORD)
+	{
+		for (char& ch : displayinput)
+			ch = '*';
+	}
+
 	// Render the input string
 	mvhline (LINES - 2, 0, ' ', COLS);
-	mvprintw (y, prompt.length() + 1, "%s", g_input.chars());
+	mvprintw (y, prompt.length() + 1, "%s", displayinput.chars());
 
 	// Render the prompt
 	attron (promptColor);
@@ -258,7 +269,7 @@
 
 	// Store in memory where the cursor is now (so that we can re-draw it to position the terminal
 	// cursor).
-	g_cursorChar.ch = g_cursor != 0 ? g_input[g_cursor - 1] : '\0';
+	g_cursorChar.ch = g_cursor != 0 ? displayinput[g_cursor - 1] : '\0';
 	g_cursorChar.x = prompt.length() + (g_cursor - g_pan);
 	g_needRefresh = true;
 	g_needInputRender = false;
@@ -366,9 +377,13 @@
 	if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
 	{
 		if (ch == 'y' or ch == 'Y')
+		{
+			RCONSession::get_session()->disconnect();
 			g_disconnectConfirmFunction();
+		}
 		else if (ch == 'n' or ch == 'N')
 			set_input_state (INPUTSTATE_NORMAL);
+
 		return;
 	}
 
@@ -553,3 +568,26 @@
 
 	g_needOutputRender = true;
 }
+
+// -------------------------------------------------------------------------------------------------
+//
+FUNCTION
+Interface::connect (String address, String password) -> void
+{
+	try
+	{
+		g_address = IPAddress::from_string (address);
+	}
+	catch (std::exception& e)
+	{
+		print (e.what());
+		return;
+	}
+
+	if (g_address.port == 0)
+		g_address.port = 10666;
+
+	RCONSession* session = RCONSession::new_session();
+	session->set_password (password);
+	session->connect (g_address);
+}
--- a/sources/interface.h	Mon Dec 15 02:03:08 2014 +0200
+++ b/sources/interface.h	Mon Dec 15 02:15:09 2014 +0200
@@ -39,4 +39,5 @@
 	FUNCTION render_full() -> void;
 	FUNCTION set_title (const String& message) -> void;
 	FUNCTION update_statusbar() -> void;
+	FUNCTION connect (String address, String password) -> void;
 };
--- a/sources/main.cpp	Mon Dec 15 02:03:08 2014 +0200
+++ b/sources/main.cpp	Mon Dec 15 02:15:09 2014 +0200
@@ -43,8 +43,18 @@
 main (int argc, char* argv[]) -> int
 {
 	HUFFMAN_Construct();
+
+	if (argc != 1 and argc != 3)
+	{
+		fprintf (stderr, "usage: %s\nusage: %s <address> <password>\n", argv[0], argv[0]);
+		return EXIT_FAILURE;
+	}
+
 	Interface::initialize();
 
+	if (argc == 3)
+		Interface::connect (argv[1], argv[2]);
+
 	try
 	{
 		for (;;)

mercurial