fix: reformat using C# rules

This commit is contained in:
ZavaruKitsu
2022-10-08 19:26:34 +03:00
parent a731e2a8d0
commit 5ab15621a0
180 changed files with 13136 additions and 13081 deletions
+25 -29
View File
@@ -3,39 +3,35 @@ using SystemCommandsBot.config;
using SystemCommandsBot.forms;
using TelegramBotBase.Builder;
namespace SystemCommandsBot
namespace SystemCommandsBot;
internal class Program
{
internal class Program
public static Config BotConfig { get; set; }
private static void Main(string[] args)
{
public static Config BotConfig { get; set; }
BotConfig = Config.Load();
private static void Main(string[] args)
if (BotConfig.ApiKey == null || BotConfig.ApiKey.Trim() == "")
{
BotConfig = Config.Load();
if (BotConfig.ApiKey == null || BotConfig.ApiKey.Trim() == "")
{
Console.WriteLine("Config created...");
Console.ReadLine();
return;
}
var bot = BotBaseBuilder.Create()
.QuickStart<StartForm>(BotConfig.ApiKey)
.Build();
bot.Start();
Console.WriteLine("Bot started");
Console.WriteLine("Config created...");
Console.ReadLine();
bot.Stop();
return;
}
var bot = BotBaseBuilder.Create()
.QuickStart<StartForm>(BotConfig.ApiKey)
.Build();
bot.Start();
Console.WriteLine("Bot started");
Console.ReadLine();
bot.Stop();
}
}
}
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="TelegramBotBase" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
<PackageReference Include="TelegramBotBase" Version="5.0.0"/>
</ItemGroup>
</Project>
+12 -16
View File
@@ -1,25 +1,21 @@
namespace SystemCommandsBot.commands
namespace SystemCommandsBot.commands;
public class Commando
{
public class Commando
{
public int Id { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Title { get; set; }
public string ShellCmd { get; set; }
public string ShellCmd { get; set; }
public bool Enabled { get; set; } = true;
public bool Enabled { get; set; } = true;
public string Action { get; set; }
public string Action { get; set; }
public bool UseShell { get; set; } = true;
public bool UseShell { get; set; } = true;
public int? MaxInstances { get; set; }
public int? MaxInstances { get; set; }
public string ProcName
{
get;set;
}
}
}
public string ProcName { get; set; }
}
+69 -69
View File
@@ -4,90 +4,90 @@ using System.IO;
using Newtonsoft.Json;
using SystemCommandsBot.commands;
namespace SystemCommandsBot.config
namespace SystemCommandsBot.config;
public class Config
{
public class Config
public Config()
{
public string Password { get; set; }
Commandos = new List<Commando>();
}
public string ApiKey { get; set; }
public string Password { get; set; }
public List<Commando> Commandos { get; set; }
public string ApiKey { get; set; }
public List<Commando> Commandos { get; set; }
public Config()
public void LoadDefaultValues()
{
ApiKey = "";
Commandos.Add(new Commando
{
Commandos = new List<Commando>();
Id = 0, Enabled = true, Title = "Test Befehl", ShellCmd = "explorer.exe", Action = "start", MaxInstances = 2
});
}
public static Config Load()
{
try
{
return Load(AppContext.BaseDirectory + "config\\default.cfg");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
public void LoadDefaultValues()
return null;
}
public static Config Load(string path)
{
try
{
ApiKey = "";
Commandos.Add(new Commando { Id = 0, Enabled = true, Title = "Test Befehl", ShellCmd = "explorer.exe", Action = "start", MaxInstances = 2 });
var cfg = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
return cfg;
}
catch (DirectoryNotFoundException)
{
var di = new DirectoryInfo(path);
if (!Directory.Exists(di.Parent.FullName))
{
Directory.CreateDirectory(di.Parent.FullName);
}
var cfg = new Config();
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch (FileNotFoundException)
{
var cfg = new Config();
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public static Config Load()
public void Save(string path)
{
try
{
try
{
return Load(AppContext.BaseDirectory + "config\\default.cfg");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
File.WriteAllText(path, JsonConvert.SerializeObject(this));
}
public static Config Load(string path)
catch
{
try
{
var cfg = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
return cfg;
}
catch (DirectoryNotFoundException)
{
var di = new DirectoryInfo(path);
if (!Directory.Exists(di.Parent.FullName))
{
Directory.CreateDirectory(di.Parent.FullName);
}
var cfg = new Config();
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch (FileNotFoundException)
{
var cfg = new Config();
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public void Save(string path)
{
try
{
File.WriteAllText(path, JsonConvert.SerializeObject(this));
}
catch
{
}
}
}
}
}
+145 -161
View File
@@ -6,167 +6,151 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace SystemCommandsBot.forms
namespace SystemCommandsBot.forms;
public class CmdForm : AutoCleanForm
{
public class CmdForm : AutoCleanForm
public DateTime ExpiresAt { get; set; }
public int? MessageId { get; set; }
public override Task Load(MessageResult message)
{
public DateTime ExpiresAt { get; set; }
public int? MessageId { get; set; }
public override Task Load(MessageResult message)
{
return Task.CompletedTask;
}
public override async Task Action(MessageResult message)
{
var btn = message.RawData;
var id = -1;
if (!int.TryParse(btn, out id))
{
return;
}
var cmd = Program.BotConfig.Commandos.Where(a => a.Enabled && a.Id == id).FirstOrDefault();
if (cmd == null)
{
await Device.Send("Cmd nicht verfügbar.");
return;
}
message.Handled = true;
switch (cmd.Action)
{
case "start":
var fi = new FileInfo(cmd.ShellCmd);
if (cmd.MaxInstances != null && cmd.MaxInstances >= 0)
{
if (Process.GetProcessesByName(cmd.ProcName).Count() >= cmd.MaxInstances)
{
await Device.Send("Anwendung läuft bereits.");
await message.ConfirmAction("Anwendung läuft bereits.");
return;
}
}
var psi = new ProcessStartInfo
{
FileName = cmd.ShellCmd,
WorkingDirectory = fi.DirectoryName,
UseShellExecute = cmd.UseShell
};
Process.Start(psi);
await Device.Send(fi.Name + " wurde gestarted.");
await message.ConfirmAction(fi.Name + " wurde gestarted.");
break;
case "kill":
var fi2 = new FileInfo(cmd.ShellCmd);
var pros = fi2.Name.Replace(fi2.Extension, "");
var proc = Process.GetProcessesByName(pros).ToList();
foreach (var p in proc)
{
try
{
p.Kill();
}
catch
{
}
}
await Device.Send(fi2.Name + " wurde beendet.");
await message.ConfirmAction(fi2.Name + " wurde beendet.");
break;
case "restart":
var fi3 = new FileInfo(cmd.ShellCmd);
var pros2 = fi3.Name.Replace(fi3.Extension, "");
var proc2 = Process.GetProcessesByName(pros2).ToList();
foreach (var p in proc2)
{
try
{
p.Kill();
}
catch
{
}
}
var fi4 = new FileInfo(cmd.ShellCmd);
var psi2 = new ProcessStartInfo
{
FileName = cmd.ShellCmd,
WorkingDirectory = fi4.DirectoryName
};
psi2.FileName = cmd.ShellCmd;
psi2.UseShellExecute = cmd.UseShell;
Process.Start(psi2);
await Device.Send(fi3.Name + " wurde neugestarted.");
await message.ConfirmAction(fi3.Name + " wurde neugestarted.");
break;
default:
await message.ConfirmAction();
break;
}
}
public override async Task Render(MessageResult message)
{
if (MessageId == null)
{
var buttons = Program.BotConfig.Commandos.Where(a => a.Enabled).Select(a => new ButtonBase(a.Title, a.Id.ToString()));
var bf = new ButtonForm();
bf.AddSplitted(buttons, 1);
await Device.Send("Deine Optionen", bf);
}
}
return Task.CompletedTask;
}
}
public override async Task Action(MessageResult message)
{
var btn = message.RawData;
var id = -1;
if (!int.TryParse(btn, out id))
{
return;
}
var cmd = Program.BotConfig.Commandos.Where(a => a.Enabled && a.Id == id).FirstOrDefault();
if (cmd == null)
{
await Device.Send("Cmd nicht verfügbar.");
return;
}
message.Handled = true;
switch (cmd.Action)
{
case "start":
var fi = new FileInfo(cmd.ShellCmd);
if (cmd.MaxInstances != null && cmd.MaxInstances >= 0)
{
if (Process.GetProcessesByName(cmd.ProcName).Count() >= cmd.MaxInstances)
{
await Device.Send("Anwendung läuft bereits.");
await message.ConfirmAction("Anwendung läuft bereits.");
return;
}
}
var psi = new ProcessStartInfo
{
FileName = cmd.ShellCmd,
WorkingDirectory = fi.DirectoryName,
UseShellExecute = cmd.UseShell
};
Process.Start(psi);
await Device.Send(fi.Name + " wurde gestarted.");
await message.ConfirmAction(fi.Name + " wurde gestarted.");
break;
case "kill":
var fi2 = new FileInfo(cmd.ShellCmd);
var pros = fi2.Name.Replace(fi2.Extension, "");
var proc = Process.GetProcessesByName(pros).ToList();
foreach (var p in proc)
{
try
{
p.Kill();
}
catch
{
}
}
await Device.Send(fi2.Name + " wurde beendet.");
await message.ConfirmAction(fi2.Name + " wurde beendet.");
break;
case "restart":
var fi3 = new FileInfo(cmd.ShellCmd);
var pros2 = fi3.Name.Replace(fi3.Extension, "");
var proc2 = Process.GetProcessesByName(pros2).ToList();
foreach (var p in proc2)
{
try
{
p.Kill();
}
catch
{
}
}
var fi4 = new FileInfo(cmd.ShellCmd);
var psi2 = new ProcessStartInfo
{
FileName = cmd.ShellCmd,
WorkingDirectory = fi4.DirectoryName
};
psi2.FileName = cmd.ShellCmd;
psi2.UseShellExecute = cmd.UseShell;
Process.Start(psi2);
await Device.Send(fi3.Name + " wurde neugestarted.");
await message.ConfirmAction(fi3.Name + " wurde neugestarted.");
break;
default:
await message.ConfirmAction();
break;
}
}
public override async Task Render(MessageResult message)
{
if (MessageId == null)
{
var buttons = Program.BotConfig.Commandos.Where(a => a.Enabled)
.Select(a => new ButtonBase(a.Title, a.Id.ToString()));
var bf = new ButtonForm();
bf.AddSplitted(buttons, 1);
await Device.Send("Deine Optionen", bf);
}
}
}
+27 -30
View File
@@ -3,39 +3,36 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace SystemCommandsBot.forms
namespace SystemCommandsBot.forms;
public class StartForm : FormBase
{
public class StartForm : FormBase
public string Password { get; set; }
public override Task Load(MessageResult message)
{
public string Password { get; set; }
public override Task Load(MessageResult message)
var inp = message.MessageText;
if (Program.BotConfig.Password == inp)
{
var inp = message.MessageText;
if (Program.BotConfig.Password == inp)
{
Password = inp;
}
return Task.CompletedTask;
}
public override async Task Render(MessageResult message)
{
if (Password == null || Password.Trim() == "")
{
await Device.Send("Bitte gib dein Passwort an.");
return;
}
var cmd = new CmdForm();
cmd.ExpiresAt = DateTime.Now.AddDays(14);
await NavigateTo(cmd);
Password = inp;
}
return Task.CompletedTask;
}
}
public override async Task Render(MessageResult message)
{
if (Password == null || Password.Trim() == "")
{
await Device.Send("Bitte gib dein Passwort an.");
return;
}
var cmd = new CmdForm();
cmd.ExpiresAt = DateTime.Now.AddDays(14);
await NavigateTo(cmd);
}
}