Skip to content

Commit

Permalink
Get MOTD template directly from config table during login (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
slook authored Sep 3, 2024
1 parent fadab8b commit d94d8c7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ class User
if (server.is_admin(username)) writeln(username, " is an admin.");
server.add_user(this);

auto motd = server.get_motd(username);
auto motd = server.get_motd(this);
auto supporter = privileges > 0;

send_message(new SLogin(true, motd, address, password, supporter));
Expand Down
2 changes: 1 addition & 1 deletion src/db.d
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Sdb

init_config_option("port", port);
init_config_option("max_users", max_users);
init_config_option("motd", "Soulfind %version%");
init_config_option("motd", "Soulfind %sversion%");
}

void init_config_option(string option, string value)
Expand Down
20 changes: 9 additions & 11 deletions src/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class Server

private ushort port;
private uint max_users;
private string motd;

private MonoTime started_at; // for server uptime

Expand Down Expand Up @@ -336,7 +335,7 @@ class Server
~ "message <message>\n\tSend global message"
~ " <message>\n\n"
~ "uptime\n\tShow server uptime\n\n"
~ "reload\n\tReload settings(Admins, MOTD, etc)"
~ "reload\n\tReload settings(Admins, etc)"
);
break;

Expand Down Expand Up @@ -570,23 +569,22 @@ class Server
db.user_update_field(username, "banned", 0);
}

string get_motd(string username)
string get_motd(User user)
{
auto user = get_user(username);
string motd;
auto motd_template = db.get_config_value("motd");
auto client_version = "%d.%d".format(
user.major_version, user.minor_version);

string ret;
ret = replace(motd, "%version%", VERSION);
ret = replace(ret, "%nbusers%", user_list.length.to!string);
ret = replace(ret, "%username%", username);
ret = replace(ret, "%userversion%", client_version);
return ret;
motd = replace(motd_template, "%sversion%", VERSION);
motd = replace(motd, "%users%", user_list.length.to!string);
motd = replace(motd, "%username%", user.username);
motd = replace(motd, "%version%", client_version);
return motd;
}

private void config(bool reload = false)
{
motd = db.get_config_value("motd");
if (!reload) {
port = db.get_config_value("port").to!ushort;
max_users = db.get_config_value("max_users").to!uint;
Expand Down
22 changes: 11 additions & 11 deletions src/soulsetup.d
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void set_max_users()
void motd()
{
auto menu = new Menu(
format("Current message of the day :\n--\n%s\n--\n",
format("Current message of the day :\n--\n%s\n--",
sdb.get_config_value("motd"))
);
menu.add("1", "Change MOTD", &set_motd);
Expand All @@ -183,26 +183,26 @@ void motd()
void set_motd()
{
writeln(
"You can use the following variables :\n"
~ "%version% : server version(", VERSION, ")\n"
~ "%nbusers% : number of users already connected\n"
~ "%username% : name of the connecting user\n"
~ "%userversion% : version of the user's client software\n"
~ "New MOTD(end with a dot on a single line) :"
"\nYou can use the following variables :"
~ "\n\t%sversion% : server version (", VERSION, ")"
~ "\n\t%users% : number of connected users"
~ "\n\t%username% : name of the connecting user"
~ "\n\t%version% : version of the user's client software"
~ "\n\nNew MOTD (end with a dot on a single line) :\n--"
);

string MOTD;
string motd_template;

do {
auto line = input.chomp;
if (line.strip == ".")
break;
if (MOTD.length > 0) MOTD ~= "\n";
MOTD ~= line;
if (motd_template.length > 0) motd_template ~= "\n";
motd_template ~= line;
}
while(true);

sdb.set_config_value("motd", MOTD);
sdb.set_config_value("motd", motd_template);
motd();
}

Expand Down

0 comments on commit d94d8c7

Please sign in to comment.