public string HandleCommand(string input) { if (string.IsNullOrEmpty(input)) { Logging.LogNullError(nameof(input)); return(null); } if (Program.GlobalConfig.SteamOwnerID == 0) { return("Refusing to handle request because SteamOwnerID is not set!"); } Bot bot = Bot.Bots.Values.FirstOrDefault(); if (bot == null) { return("ERROR: No bots are enabled!"); } string command = "!" + input; string output = bot.Response(Program.GlobalConfig.SteamOwnerID, command).Result; // TODO: This should be asynchronous Logging.LogGenericInfo("Answered to command: " + input + " with: " + output); return(output); }
public string HandleCommand(string input) { if (string.IsNullOrEmpty(input)) { ASF.ArchiLogger.LogNullError(nameof(input)); return(null); } if (Program.GlobalConfig.SteamOwnerID == 0) { return(Strings.ErrorWCFAccessDenied); } Bot bot = Bot.Bots.Values.FirstOrDefault(); if (bot == null) { return(Strings.ErrorNoBotsDefined); } if (input[0] != '!') { input = "!" + input; } // TODO: This should be asynchronous, but for some reason Mono doesn't return any WCF output if it is // We must keep it synchronous until either Mono gets fixed, or culprit for freeze located (and corrected) string output = bot.Response(Program.GlobalConfig.SteamOwnerID, input).Result; ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.WCFAnswered, input, output)); return(output); }
public string HandleCommand(string input) { if (string.IsNullOrEmpty(input)) { ASF.ArchiLogger.LogNullError(nameof(input)); return(null); } if (Program.GlobalConfig.SteamOwnerID == 0) { return("Refusing to handle request because SteamOwnerID is not set!"); } Bot bot = Bot.Bots.Values.FirstOrDefault(); if (bot == null) { return("ERROR: No bots are enabled!"); } string command = "!" + input; // TODO: This should be asynchronous, but for some reason Mono doesn't return any WCF output if it is // We must keep it synchronous until either Mono gets fixed, or culprit for freeze located (and corrected) string output = bot.Response(Program.GlobalConfig.SteamOwnerID, command).Result; ASF.ArchiLogger.LogGenericInfo("Answered to WCF command: " + input + " with: " + output); return(output); }
private static async Task <bool> HandleApiCommandGeneric(HttpListenerRequest request, HttpListenerResponse response, string[] arguments, byte argumentsIndex) { if ((request == null) || (response == null) || (arguments == null) || (argumentsIndex == 0)) { ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(arguments) + " || " + nameof(argumentsIndex)); return(false); } if (arguments.Length <= argumentsIndex) { return(false); } string command = WebUtility.UrlDecode(arguments[argumentsIndex]); if (string.IsNullOrEmpty(command)) { await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(command))), HttpStatusCode.BadRequest).ConfigureAwait(false); return(true); } Bot targetBot = Bot.Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value).FirstOrDefault(); if (targetBot == null) { await ResponseJsonObject(request, response, new GenericResponse(false, Strings.ErrorNoBotsDefined), HttpStatusCode.BadRequest).ConfigureAwait(false); return(true); } if (command[0] != '!') { command = "!" + command; } string content = await targetBot.Response(Program.GlobalConfig.SteamOwnerID, command).ConfigureAwait(false); await ResponseJsonObject(request, response, new GenericResponse(true, "OK", content)).ConfigureAwait(false); return(true); }
public async Task <bool> ExecuteCommandObsolete(WebServer server, HttpListenerContext context) { if ((server == null) || (context == null)) { ASF.ArchiLogger.LogNullError(nameof(server) + " || " + nameof(context)); return(false); } string command = context.QueryString("command"); if (string.IsNullOrEmpty(command)) { await context.PlainTextResponse(string.Format(Strings.ErrorIsEmpty, nameof(command)), HttpStatusCode.BadRequest).ConfigureAwait(false); return(true); } Bot targetBot = Bot.Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value).FirstOrDefault(); if (targetBot == null) { await context.PlainTextResponse(Strings.ErrorNoBotsDefined, HttpStatusCode.BadRequest).ConfigureAwait(false); return(true); } if (command[0] != '!') { command = "!" + command; } string content = await targetBot.Response(Program.GlobalConfig.SteamOwnerID, command).ConfigureAwait(false); ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.IPCAnswered, command, content)); await context.PlainTextResponse(content).ConfigureAwait(false); return(true); }
private static async Task ExecuteCommand(HttpListenerContext context) { if (context == null) { ASF.ArchiLogger.LogNullError(nameof(context)); return; } string command = context.Request.GetQueryStringValue("command"); if (string.IsNullOrEmpty(command)) { await context.StringResponse(string.Format(Strings.ErrorIsEmpty, nameof(command)), statusCode : HttpStatusCode.BadRequest).ConfigureAwait(false); return; } Bot targetBot = Bot.Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value).FirstOrDefault(); if (targetBot == null) { await context.StringResponse(Strings.ErrorNoBotsDefined, statusCode : HttpStatusCode.BadRequest).ConfigureAwait(false); return; } if (command[0] != '!') { command = "!" + command; } string content = await targetBot.Response(Program.GlobalConfig.SteamOwnerID, command).ConfigureAwait(false); ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.IPCAnswered, command, content)); await context.StringResponse(content).ConfigureAwait(false); }
private static async Task HandleRequest(HttpListenerContext context) { if (context == null) { ASF.ArchiLogger.LogNullError(nameof(context)); return; } try { if (Program.GlobalConfig.SteamOwnerID == 0) { ASF.ArchiLogger.LogGenericWarning(Strings.ErrorIPCAccessDenied); await context.Response.WriteAsync(HttpStatusCode.Forbidden, Strings.ErrorIPCAccessDenied).ConfigureAwait(false); return; } if (!context.Request.RawUrl.StartsWith("/" + nameof(IPC), StringComparison.Ordinal)) { await context.Response.WriteAsync(HttpStatusCode.BadRequest, nameof(HttpStatusCode.BadRequest)).ConfigureAwait(false); return; } switch (context.Request.HttpMethod) { case WebRequestMethods.Http.Get: for (int i = 0; i < context.Request.QueryString.Count; i++) { string key = context.Request.QueryString.GetKey(i); switch (key) { case "command": string command = context.Request.QueryString.Get(i); if (string.IsNullOrWhiteSpace(command)) { break; } Bot bot = Bot.Bots.Values.FirstOrDefault(); if (bot == null) { await context.Response.WriteAsync(HttpStatusCode.NotAcceptable, Strings.ErrorNoBotsDefined).ConfigureAwait(false); return; } if (command[0] != '!') { command = "!" + command; } string response = await bot.Response(Program.GlobalConfig.SteamOwnerID, command).ConfigureAwait(false); ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.IPCAnswered, command, response)); await context.Response.WriteAsync(HttpStatusCode.OK, response).ConfigureAwait(false); break; } } break; default: await context.Response.WriteAsync(HttpStatusCode.BadRequest, nameof(HttpStatusCode.BadRequest)).ConfigureAwait(false); return; } if (context.Response.ContentLength64 == 0) { await context.Response.WriteAsync(HttpStatusCode.MethodNotAllowed, nameof(HttpStatusCode.MethodNotAllowed)).ConfigureAwait(false); } } finally { context.Response.Close(); } }