using System.Net.Sockets;
using System.Text;
using System.Web;

namespace Shoko;

[Protocol("finger")]
class FingerProtoHandler : ProtoHandler
{
    public FingerProtoHandler(Uri url)
    {
        URL = url;
    }

    public override async Task Load()
    {
        var file = URL.PathAndQuery;

        if(file.StartsWith("/")) file = file.Remove(0,1);

        var uri = Encoding.UTF8.GetBytes(HttpUtility.UrlDecode(file)+"\r\n");

        var tcp = new TcpClient(URL.Host, URL.Port < 0 ? 79 : URL.Port);

        var stream = tcp.GetStream();

        await stream.WriteAsync(uri);

        Content = await Download(stream);
        MediaType = "text/plain";
        OnLoaded();
    }
    public override void Render()
    {
    }
}