start adding FromHostString
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2024-04-23 20:22:12 -04:00
parent 44cf32618f
commit 3f7e13763d
6 changed files with 121 additions and 16 deletions

View File

@ -1,7 +0,0 @@
namespace Robots
{
public class Client
{
}
}

View File

@ -1,6 +1,6 @@
namespace Robots
{
public class Program
{
}
}
using IRCRobots;
var client = new Server();
var b = new Bot();
b.AddServer("libera", new ConnectionParams() {Host = })
await b.Run();

View File

@ -1,7 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\IRCRobots\IRCRobots.csproj" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace IRCRobots
{
@ -19,5 +21,39 @@ namespace IRCRobots
public int Reconnect { get; set; } = 10; // seconds
public IEnumerable<string> AltNicknames { get; set; } = new List<string>();
public IEnumerable<string> Autojoin { get; set; } = new List<string>();
public static ConnectionParams FromHostString(string nickName, string hostString)
{
var result = new ConnectionParams {Nickname = nickName};
var ipv6host = Regex.Matches(hostString, @"\[([a-fA-F0-9:]+)\]").SingleOrDefault();
var portString = "";
if (ipv6host is { Index: 0 })
{
result.Host = ipv6host.Groups[0].Value;
portString = hostString[(ipv6host.Index + ipv6host.Length)..];
}
else
{
var s = hostString.Split(':', 2);
result.Host = s[0];
portString = s[1];
}
if (string.IsNullOrWhiteSpace(portString))
{
portString = "6667";
result.UseTLS = false;
}
else
{
result.UseTLS = portString.StartsWith('+') || portString.StartsWith('~');
result.VerifyTLS = portString.StartsWith('~');
portString = portString[1..];
}
result.Port = int.Parse(portString);
return result;
}
}
}

67
IRCRobots/Glob.cs Normal file
View File

@ -0,0 +1,67 @@
using System.Text;
namespace IRCRobots
{
public class Glob
{
public string Pattern { get; set; }
public static string Collapse(string pattern)
{
var result = new StringBuilder();
var i = 0;
while (i < pattern.Length)
{
var seenAst = false;
while (i < pattern.Length && (pattern[i] == '*' || pattern[i] == '?'))
{
if (pattern[i] == '?') result.Append('?');
else if (pattern[i] == '*') seenAst = true;
i++;
}
if (seenAst) result.Append('*');
if (i < pattern.Length) result.Append(pattern[i++]);
}
return result.ToString();
}
public bool Match(string s)
{
int i = 0, j = 0;
int iBackup = -1, jBackup = -1;
while (j < s.Length)
{
// p = (pattern[i:] or [None])[0]
var p = Pattern[i];
if (p == '*')
{
i++;
iBackup = i;
jBackup = j;
}
else if (p == '?' || p == s[j])
{
i++;
j++;
}
else
{
if (iBackup == -1)
return false;
jBackup++;
j = jBackup;
i = iBackup;
}
}
return i == Pattern.Length;
}
}
}

3
IRCRobots/README.md Normal file
View File

@ -0,0 +1,3 @@
# IRCRobots
WIP port of [jesopo/ircrobots](https://github.com/jesopo/ircrobots)