From d78ca949afe0f2c414443924d89bd4a009924c1e Mon Sep 17 00:00:00 2001 From: Les De Ridder Date: Fri, 7 Apr 2017 08:08:24 +0200 Subject: [PATCH] Implement MOTD --- .gitignore | 1 + source/ircd/connection.d | 14 ++++++++++++++ source/ircd/server.d | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/.gitignore b/.gitignore index 3a6c85b..87cbe7f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ __dummy.html __test__*__ /salty-ircd source/ircd/packageVersion.d +motd diff --git a/source/ircd/connection.d b/source/ircd/connection.d index 054fdf9..dd6c8a9 100644 --- a/source/ircd/connection.d +++ b/source/ircd/connection.d @@ -173,6 +173,10 @@ class Connection if(!registered) sendErrNotRegistered(); else onTime(message); break; + case "MOTD": + if(!registered) sendErrNotRegistered(); + else onMotd(message); + break; default: writeln("unknown command '", message.command, "'"); send(Message(_server.name, "421", [nick, message.command, "Unknown command"])); @@ -597,6 +601,16 @@ class Connection _server.sendTime(this); } + void onMotd(Message message) + { + if(message.parameters.length > 0) + { + notImplemented("querying the motd of another server"); + return; + } + _server.sendMotd(this); + } + void sendWhoReply(string channel, Connection user, uint hopCount) { auto flags = user.modes.canFind('a') ? "G" : "H"; diff --git a/source/ircd/server.d b/source/ircd/server.d index 1dd736c..2469f81 100644 --- a/source/ircd/server.d +++ b/source/ircd/server.d @@ -7,6 +7,7 @@ import std.conv; import std.socket; import core.time; import std.datetime; +import std.string; import vibe.core.core; @@ -26,15 +27,28 @@ class Server string name; + string motd; + Channel[] channels; this() { name = Socket.hostName; + readMotd(); + runTask(&pingLoop); } + private void readMotd() + { + import std.file : exists, readText; + if(exists("motd")) + { + motd = readText("motd"); + } + } + private void pingLoop() { while(true) @@ -269,6 +283,24 @@ class Server user.send(Message(inviter.mask, "INVITE", [user.nick, channelName])); } + void sendMotd(Connection connection) + { + if(motd !is null) + { + connection.send(Message(name, "375", [connection.nick, ":- " ~ name ~ " Message of the day - "], true)); + foreach(line; motd.splitLines) + { + //TODO: Implement line wrapping + connection.send(Message(name, "372", [connection.nick, ":- " ~ line], true)); + } + connection.send(Message(name, "376", [connection.nick, "End of MOTD command"], true)); + } + else + { + connection.send(Message(name, "422", [connection.nick, "MOTD File is missing"], true)); + } + } + void listen(ushort port = 6667) { listenTCP(port, &acceptConnection);