2017-03-11 06:14:48 +01:00
|
|
|
module ircd.message;
|
|
|
|
|
|
|
|
import std.string;
|
|
|
|
import std.functional;
|
|
|
|
import std.array;
|
|
|
|
import std.algorithm;
|
|
|
|
import std.conv;
|
|
|
|
|
|
|
|
struct Message
|
|
|
|
{
|
2020-02-11 15:01:08 +01:00
|
|
|
string prefix;
|
|
|
|
string command;
|
|
|
|
string[] parameters;
|
|
|
|
bool prefixedParameter;
|
2017-03-11 06:14:48 +01:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
//NOTE: The RFCs don't state what this is exactly, but common implementations use the byte count of the message parameters
|
|
|
|
ulong bytes;
|
2017-05-19 01:19:23 +02:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
static Message fromString(string line)
|
|
|
|
{
|
|
|
|
string prefix = null;
|
2020-02-12 13:59:41 +01:00
|
|
|
if (line.startsWith(':'))
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
|
|
|
line = line[1 .. $];
|
|
|
|
prefix = line[0 .. line.indexOf(' ')];
|
|
|
|
line = line[prefix.length + 1 .. $];
|
|
|
|
}
|
2017-03-11 06:14:48 +01:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
//stop early when no space character can be found (message without parameters)
|
2020-02-12 13:59:41 +01:00
|
|
|
if (!line.canFind(' '))
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
|
|
|
return Message(prefix, line, [], false);
|
|
|
|
}
|
2017-03-14 17:45:28 +01:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
auto command = line[0 .. line.indexOf(' ')];
|
|
|
|
line = line[command.length + 1 .. $];
|
|
|
|
auto bytes = line.length;
|
|
|
|
string[] params = [];
|
|
|
|
bool prefixedParam;
|
2020-02-12 13:59:41 +01:00
|
|
|
while (true)
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
2020-02-12 13:59:41 +01:00
|
|
|
if (line.startsWith(':'))
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
|
|
|
params ~= line[1 .. $];
|
|
|
|
prefixedParam = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-02-12 13:59:41 +01:00
|
|
|
else if (line.canFind(' '))
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
|
|
|
auto param = line[0 .. line.indexOf(' ')];
|
|
|
|
line = line[param.length + 1 .. $];
|
|
|
|
params ~= param;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
params ~= line;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 06:14:48 +01:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
return Message(prefix, command, params, prefixedParam, bytes);
|
|
|
|
}
|
2017-03-11 06:14:48 +01:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
string toString()
|
|
|
|
{
|
|
|
|
auto message = "";
|
2020-02-12 13:59:41 +01:00
|
|
|
if (prefix != null)
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
|
|
|
message = ":" ~ prefix ~ " ";
|
|
|
|
}
|
2017-03-11 06:14:48 +01:00
|
|
|
|
2020-02-12 13:59:41 +01:00
|
|
|
if (parameters.length == 0)
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
|
|
|
return message ~ command;
|
|
|
|
}
|
2017-03-14 17:45:28 +01:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
message ~= command ~ " ";
|
2020-02-12 13:59:41 +01:00
|
|
|
if (parameters.length > 1)
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
2020-02-12 13:59:41 +01:00
|
|
|
message ~= parameters[0 .. $ - 1].join(' ') ~ " ";
|
2020-02-11 15:01:08 +01:00
|
|
|
}
|
2017-03-11 06:31:36 +01:00
|
|
|
|
2020-02-12 13:59:41 +01:00
|
|
|
if (parameters[$ - 1].canFind(' ') || prefixedParameter)
|
2020-02-11 15:01:08 +01:00
|
|
|
{
|
|
|
|
message ~= ":";
|
|
|
|
}
|
2020-02-12 13:59:41 +01:00
|
|
|
message ~= parameters[$ - 1];
|
2017-03-11 06:14:48 +01:00
|
|
|
|
2020-02-11 15:01:08 +01:00
|
|
|
return message;
|
|
|
|
}
|
2017-03-11 06:14:48 +01:00
|
|
|
}
|