SecondBot/Core/Commands/Core.cs
Madpeter d5853dd4de Multiple improvements
Merged IM and Chat command groups.
Changed return hints for failures to ReturnHintsFailure so the wiki make can mark errors.
Added missing arg hints for Notecard commands.
Added new NotecardDirectSend command for speed runners.
Improved About info for GroupAddRole command.
Corrected About info for GroupInvite command.
GoHome command moved to self workgroup.
2021-11-13 13:41:50 +00:00

104 lines
3.8 KiB
C#

using BetterSecondBotShared.Static;
using BetterSecondBot.bottypes;
using EmbedIO;
using EmbedIO.Routing;
using EmbedIO.WebApi;
using System;
using System.Threading;
namespace BetterSecondBot.HttpService
{
public class HTTP_Core : WebApiControllerWithTokens
{
public HTTP_Core(SecondBot mainbot, TokenStorage setuptokens) : base(mainbot, setuptokens) { }
[About("Requests a new token (Vaild for 10 mins) <br/>to use with all other requests")]
[NeedsToken(false)]
[ReturnHints("A new token with full system scope")]
[ReturnHintsFailure("Authcode not accepted")]
[ArgHints("authcode", "text", "the first 10 chars of SHA1(unixtime+WebUIkey)<br/>unixtime can be +- 30 of the bots time.")]
[ArgHints("unixtimegiven", "number", "the unixtime you made this request")]
[Route(HttpVerbs.Post, "/GetToken")]
public Object GetToken([FormField] string authcode, [FormField] string unixtimegiven)
{
long now = helpers.UnixTimeNow();
long dif = 0;
if(long.TryParse(unixtimegiven,out long given) == true)
{
dif = now - given;
now = given;
}
if((dif < 30) && (dif > -30))
{
var raw = now.ToString() + bot.getMyConfig.Security_WebUIKey;
string hash = helpers.GetSHA1(raw).Substring(0, 10);
if (hash == authcode)
{
string newtoken = tokens.CreateToken(handleGetClientIP());
if(newtoken != null) return BasicReply(newtoken, "GetToken", new [] { authcode, unixtimegiven });
}
}
return Failure("Authcode not accepted", "GetToken", new [] { authcode, unixtimegiven });
}
[About("Used to check HTTP connections")]
[ReturnHints("world")]
[NeedsToken(false)]
[Route(HttpVerbs.Get, "/Hello")]
public object Hello()
{
return BasicReply("world", "Hello");
}
[About("Delays a thead by X ms<br/>Mostly pointless but good if your doing custom commands")]
[ReturnHintsFailure("Invaild amount")]
[ReturnHints("ok")]
[Route(HttpVerbs.Get, "/Delay/{amount}/{token}")]
public object Delay(string amount,string token)
{
if (tokens.Allow(token, "core", "Delay", handleGetClientIP()) == false)
{
return Failure("Token not accepted", "Delay", new [] { amount });
}
if(int.TryParse(amount,out int amountvalue) == false)
{
return Failure("Invaild amount", "Delay", new [] { amount });
}
Thread.Sleep(amountvalue);
return BasicReply("Ok", "Delay", new [] { amount });
}
[About("Removes the given token from the accepted token pool")]
[ReturnHints("ok")]
[Route(HttpVerbs.Get, "/LogoutUI/{token}")]
public object LogoutUI(string token)
{
if (tokens.Allow(token, "core", "LogoutUI", handleGetClientIP()) == false)
{
return Failure("Token not accepted", "LogoutUI");
}
SuccessNoReturn("LogoutUI");
return tokens.Expire(token);
}
}
public class CommandLibCall
{
public string Command { get; set; }
public string[] Args { get; set; }
public string AuthCode { get; set; }
}
public class NearMeDetails
{
public string id { get; set; }
public string name { get; set; }
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
public int range { get; set; }
}
}