commit 8ec20b716747036f2dbb78993fb75f3c4327d65d Author: Les De Ridder Date: Sat Mar 11 03:44:02 2017 +0100 Implement message parsing and constructing diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad58b69 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.dub +docs.json +__dummy.html +*.o +*.obj +__test__*__ +ircd diff --git a/dub.sdl b/dub.sdl new file mode 100644 index 0000000..17ab569 --- /dev/null +++ b/dub.sdl @@ -0,0 +1,7 @@ +name "ircd" +description "An Internet Relay Chat server in D" +authors "Les De Ridder" +copyright "Copyright © 2017, Les De Ridder" +license "NCSA" +dependency "vibe-d" version="~>0.7.30" +versions "VibeDefaultMain" diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..fbdddc4 --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,11 @@ +{ + "fileVersion": 1, + "versions": { + "diet-ng": "1.2.0", + "libasync": "0.7.9", + "libevent": "2.0.2+2.0.16", + "memutils": "0.4.9", + "openssl": "1.1.5+1.0.1g", + "vibe-d": "0.7.30" + } +} diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..317bb69 --- /dev/null +++ b/source/app.d @@ -0,0 +1,93 @@ +import vibe.d; + +import std.stdio; +import std.functional; +import std.array; +import std.algorithm; +import std.conv; + +shared static this() +{ + listenTCP(6667, toDelegate(&handleConnection), "127.0.0.1"); +} + +struct Message +{ + string prefix; + string command; + string[] parameters; + bool prefixedParameter; + + static Message fromString(string line) + { + string prefix = null; + if(line.startsWith(':')) + { + line = line[1 .. $]; + prefix = line[0 .. line.indexOf(' ')]; + line = line[prefix.length + 1 .. $]; + } + + auto command = line[0 .. line.indexOf(' ')]; + line = line[command.length + 1 .. $]; + string[] params = []; + bool prefixedParam; + while(true) + { + if(line.startsWith(':')) + { + params ~= line[1 .. $]; + prefixedParam = true; + break; + } + else if(line.canFind(' ')) + { + auto param = line[0 .. line.indexOf(' ')]; + line = line[param.length + 1 .. $]; + params ~= param; + } + else + { + params ~= line; + break; + } + } + + return Message(prefix, command, params, prefixedParam); + } + + string toString() + { + auto message = ""; + if(prefix != null) + { + message = ":" ~ prefix ~ " "; + } + + message ~= command ~ " "; + if(parameters.length > 1) + { + message ~= parameters[0 .. $-1].join(' ') ~ " "; + if(parameters[$-1].canFind(' ') || prefixedParameter) + { + message ~= ":"; + } + } + message ~= parameters[$-1]; + + return message; + } +} + +void handleConnection(TCPConnection connection) +{ + writeln("connection opened"); + while(connection.connected) + { + auto message = Message.fromString((cast(string)connection.readLine()).chomp); + writeln("C> " ~ message.toString); + } + writeln("connection closed"); +} + +