Skip to content

Commit

Permalink
Add server side ncurses terminal
Browse files Browse the repository at this point in the history
This adds a chat console the server owner can use for administration
or to talk with players.
It runs in its own thread, which makes the user interface immune to
the server's lag, behaving just like a client, except timeout.
As it uses the same console code as the f10 console, things like nick
completion or a scroll buffer basically come for free.
The terminal itself is written in a general way so that adding a
client version later on is just about implementing an interface.

Fatal errors are printed after the console exists and the ncurses
terminal buffer gets cleaned up with endwin(), so that the error still
remains visible.

The server owner can chose their username their entered text will
have in chat and where players can send PMs to.
Once the username is secured with a password to prevent anybody to
take over the server, the owner can execute admin tasks over the
console.

This change includes a contribution by @kahrl who has improved ncurses
library detection.
  • Loading branch information
est31 committed Nov 6, 2015
1 parent 1384108 commit 5e507c9
Show file tree
Hide file tree
Showing 22 changed files with 1,214 additions and 91 deletions.
9 changes: 9 additions & 0 deletions builtin/init.lua
Expand Up @@ -7,6 +7,15 @@

-- Initialize some very basic things
function core.debug(...) core.log(table.concat({...}, "\t")) end
if core.print then

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja Nov 9, 2015

Member

It seems that print is always included -- except for in the async env, which doesn't make sense since it shouldn't have any threading issues.

This comment has been minimized.

Copy link
@est31

est31 Nov 9, 2015

Author Contributor

I think this also gets executed when the mainmenu initializes, no?

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja Nov 9, 2015

Member

Yes.

This comment has been minimized.

Copy link
@est31

est31 Nov 9, 2015

Author Contributor

Well either way, we do need the check, otherwise it would try to call nil(some_string) the next time the mainmenu or the async env call print()

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja Nov 11, 2015

Member

When? The main menu uses ModApiUtil too IIRC.
It would happen in the async env, but you just have to add ASYNC_API_FCT to fix that.

local core_print = core.print
-- Override native print and use
-- terminal if that's turned on
function print(...)
core_print(table.concat({...}, "\t"))
end
core.print = nil -- don't pollute our namespace
end
math.randomseed(os.time())
os.setlocale("C", "numeric")
minetest = core
Expand Down
189 changes: 189 additions & 0 deletions cmake/Modules/FindNcursesw.cmake
@@ -0,0 +1,189 @@
#.rst:
# FindNcursesw
# ------------
#
# Find the ncursesw (wide ncurses) include file and library.
#
# Based on FindCurses.cmake which comes with CMake.
#
# Checks for ncursesw first. If not found, it then executes the
# regular old FindCurses.cmake to look for for ncurses (or curses).
#
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# This module defines the following variables:
#
# ``CURSES_FOUND``
# True if curses is found.
# ``NCURSESW_FOUND``
# True if ncursesw is found.
# ``CURSES_INCLUDE_DIRS``
# The include directories needed to use Curses.
# ``CURSES_LIBRARIES``
# The libraries needed to use Curses.
# ``CURSES_HAVE_CURSES_H``
# True if curses.h is available.
# ``CURSES_HAVE_NCURSES_H``
# True if ncurses.h is available.
# ``CURSES_HAVE_NCURSES_NCURSES_H``
# True if ``ncurses/ncurses.h`` is available.
# ``CURSES_HAVE_NCURSES_CURSES_H``
# True if ``ncurses/curses.h`` is available.
# ``CURSES_HAVE_NCURSESW_NCURSES_H``
# True if ``ncursesw/ncurses.h`` is available.
# ``CURSES_HAVE_NCURSESW_CURSES_H``
# True if ``ncursesw/curses.h`` is available.
#
# Set ``CURSES_NEED_NCURSES`` to ``TRUE`` before the
# ``find_package(Ncursesw)`` call if NCurses functionality is required.
#
#=============================================================================
# Copyright 2001-2014 Kitware, Inc.
# modifications: Copyright 2015 kahrl <kahrl@gmx.net>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the names of Kitware, Inc., the Insight Software Consortium,
# nor the names of their contributors may be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ------------------------------------------------------------------------------
#
# The above copyright and license notice applies to distributions of
# CMake in source and binary form. Some source files contain additional
# notices of original copyright by their contributors; see each source
# for details. Third-party software packages supplied with CMake under
# compatible licenses provide their own copyright notices documented in
# corresponding subdirectories.
#
# ------------------------------------------------------------------------------
#
# CMake was initially developed by Kitware with the following sponsorship:
#
# * National Library of Medicine at the National Institutes of Health
# as part of the Insight Segmentation and Registration Toolkit (ITK).
#
# * US National Labs (Los Alamos, Livermore, Sandia) ASC Parallel
# Visualization Initiative.
#
# * National Alliance for Medical Image Computing (NAMIC) is funded by the
# National Institutes of Health through the NIH Roadmap for Medical Research,
# Grant U54 EB005149.
#
# * Kitware, Inc.
#=============================================================================

include(CheckLibraryExists)

find_library(CURSES_NCURSESW_LIBRARY NAMES ncursesw
DOC "Path to libncursesw.so or .lib or .a")

set(CURSES_USE_NCURSES FALSE)
set(CURSES_USE_NCURSESW FALSE)

if(CURSES_NCURSESW_LIBRARY)
set(CURSES_USE_NCURSES TRUE)
set(CURSES_USE_NCURSESW TRUE)
endif()

if(CURSES_USE_NCURSESW)
get_filename_component(_cursesLibDir "${CURSES_NCURSESW_LIBRARY}" PATH)
get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)

find_path(CURSES_INCLUDE_PATH
NAMES ncursesw/ncurses.h ncursesw/curses.h
HINTS "${_cursesParentDir}/include"
)

# Previous versions of FindCurses provided these values.
if(NOT DEFINED CURSES_LIBRARY)
set(CURSES_LIBRARY "${CURSES_NCURSESW_LIBRARY}")
endif()

CHECK_LIBRARY_EXISTS("${CURSES_NCURSESW_LIBRARY}"
cbreak "" CURSES_NCURSESW_HAS_CBREAK)
if(NOT CURSES_NCURSESW_HAS_CBREAK)
find_library(CURSES_EXTRA_LIBRARY tinfo HINTS "${_cursesLibDir}"
DOC "Path to libtinfo.so or .lib or .a")
find_library(CURSES_EXTRA_LIBRARY tinfo )
endif()

# Report whether each possible header name exists in the include directory.
if(NOT DEFINED CURSES_HAVE_NCURSESW_NCURSES_H)
if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
set(CURSES_HAVE_NCURSESW_NCURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
else()
set(CURSES_HAVE_NCURSESW_NCURSES_H "CURSES_HAVE_NCURSESW_NCURSES_H-NOTFOUND")
endif()
endif()
if(NOT DEFINED CURSES_HAVE_NCURSESW_CURSES_H)
if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
set(CURSES_HAVE_NCURSESW_CURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
else()
set(CURSES_HAVE_NCURSESW_CURSES_H "CURSES_HAVE_NCURSESW_CURSES_H-NOTFOUND")
endif()
endif()

find_library(CURSES_FORM_LIBRARY form HINTS "${_cursesLibDir}"
DOC "Path to libform.so or .lib or .a")
find_library(CURSES_FORM_LIBRARY form )

# Need to provide the *_LIBRARIES
set(CURSES_LIBRARIES ${CURSES_LIBRARY})

if(CURSES_EXTRA_LIBRARY)
set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_EXTRA_LIBRARY})
endif()

if(CURSES_FORM_LIBRARY)
set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_FORM_LIBRARY})
endif()

# Provide the *_INCLUDE_DIRS result.
set(CURSES_INCLUDE_DIRS ${CURSES_INCLUDE_PATH})
set(CURSES_INCLUDE_DIR ${CURSES_INCLUDE_PATH}) # compatibility

# handle the QUIETLY and REQUIRED arguments and set CURSES_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Ncursesw DEFAULT_MSG
CURSES_LIBRARY CURSES_INCLUDE_PATH)
set(CURSES_FOUND ${NCURSESW_FOUND})

else()
find_package(Curses)
set(NCURSESW_FOUND FALSE)
endif()

mark_as_advanced(
CURSES_INCLUDE_PATH
CURSES_CURSES_LIBRARY
CURSES_NCURSES_LIBRARY
CURSES_NCURSESW_LIBRARY
CURSES_EXTRA_LIBRARY
CURSES_FORM_LIBRARY
)
3 changes: 3 additions & 0 deletions doc/minetest.6
Expand Up @@ -89,6 +89,9 @@ Run speed tests
.B \-\-migrate <value>
Migrate from current map backend to another. Possible values are sqlite3,
leveldb, redis, and dummy.
.TP
.B \-\-terminal
Display an interactive terminal over ncurses during execution.

.SH ENVIRONMENT
.TP
Expand Down
23 changes: 22 additions & 1 deletion src/CMakeLists.txt
Expand Up @@ -160,6 +160,20 @@ find_package(Lua REQUIRED)

find_package(GMP REQUIRED)

option(ENABLE_CURSES "Enable ncurses console" TRUE)
set(USE_CURSES FALSE)

if(ENABLE_CURSES)
find_package(Ncursesw)
if(CURSES_FOUND)
set(USE_CURSES TRUE)
message(STATUS "ncurses console enabled.")
include_directories(${CURSES_INCLUDE_DIRS})
else()
message(STATUS "ncurses not found!")
endif()
endif(ENABLE_CURSES)

option(ENABLE_LEVELDB "Enable LevelDB backend" TRUE)
set(USE_LEVELDB FALSE)

Expand Down Expand Up @@ -322,6 +336,7 @@ set(common_SRCS
areastore.cpp
ban.cpp
cavegen.cpp
chat.cpp
clientiface.cpp
collision.cpp
content_abm.cpp
Expand Down Expand Up @@ -387,6 +402,7 @@ set(common_SRCS
sound.cpp
staticobject.cpp
subgame.cpp
terminal_chat_console.cpp
tool.cpp
treegen.cpp
version.cpp
Expand Down Expand Up @@ -431,7 +447,6 @@ set(client_SRCS
${sound_SRCS}
${client_network_SRCS}
camera.cpp
chat.cpp
client.cpp
clientmap.cpp
clientmedia.cpp
Expand Down Expand Up @@ -558,6 +573,9 @@ if(BUILD_CLIENT)
${CGUITTFONT_LIBRARY}
)
endif()
if (USE_CURSES)
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
endif()
if (USE_LEVELDB)
target_link_libraries(${PROJECT_NAME} ${LEVELDB_LIBRARY})
endif()
Expand Down Expand Up @@ -585,6 +603,9 @@ if(BUILD_SERVER)
)
set_target_properties(${PROJECT_NAME}server PROPERTIES
COMPILE_DEFINITIONS "SERVER")
if (USE_CURSES)
target_link_libraries(${PROJECT_NAME}server ${CURSES_LIBRARIES})
endif()
if (USE_LEVELDB)
target_link_libraries(${PROJECT_NAME}server ${LEVELDB_LIBRARY})
endif()
Expand Down
6 changes: 3 additions & 3 deletions src/chat.h
Expand Up @@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector>
#include <list>

// Chat console related classes, only used by the client
// Chat console related classes

struct ChatLine
{
Expand Down Expand Up @@ -123,7 +123,7 @@ class ChatBuffer
u32 m_scrollback;
// Array of unformatted chat lines
std::vector<ChatLine> m_unformatted;

// Number of character columns in console
u32 m_cols;
// Number of character rows in console
Expand Down Expand Up @@ -213,7 +213,7 @@ class ChatPrompt
std::wstring m_line;
// History buffer
std::vector<std::wstring> m_history;
// History index (0 <= m_history_index <= m_history.size())
// History index (0 <= m_history_index <= m_history.size())
u32 m_history_index;
// Maximum number of history entries
u32 m_history_limit;
Expand Down
82 changes: 82 additions & 0 deletions src/chat_interface.h
@@ -0,0 +1,82 @@
/*
Minetest
Copyright (C) 2015 est31 <MTest31@outlook.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef CHAT_INTERFACE_H
#define CHAT_INTERFACE_H

#include "util/container.h"
#include <string>
#include <queue>
#include "irrlichttypes.h"

enum ChatEventType {
CET_CHAT,
CET_NICK_ADD,
CET_NICK_REMOVE,
CET_TIME_INFO,
};

class ChatEvent {
protected:
ChatEvent(ChatEventType a_type) { type = a_type; }
public:
ChatEventType type;
};

struct ChatEventTimeInfo : public ChatEvent {
ChatEventTimeInfo(
u64 a_game_time,
u32 a_time) :
ChatEvent(CET_TIME_INFO),
game_time(a_game_time),
time(a_time)
{}

u64 game_time;
u32 time;
};

struct ChatEventNick : public ChatEvent {
ChatEventNick(ChatEventType a_type,
const std::string &a_nick) :
ChatEvent(a_type), // one of CET_NICK_ADD, CET_NICK_REMOVE
nick(a_nick)
{}

std::string nick;
};

struct ChatEventChat : public ChatEvent {
ChatEventChat(const std::string &a_nick,
const std::wstring &an_evt_msg) :
ChatEvent(CET_CHAT),
nick(a_nick),
evt_msg(an_evt_msg)
{}

std::string nick;
std::wstring evt_msg;
};

struct ChatInterface {
MutexedQueue<ChatEvent *> command_queue; // chat backend --> server
MutexedQueue<ChatEvent *> outgoing_queue; // server --> chat backend
};

#endif

0 comments on commit 5e507c9

Please sign in to comment.