private string SendServerQueryCommand(Command cmd, Models.ServerQueryUser user) { Lyralei.Core.ServerQueryConnection.Models.Subscribers subscriberUserCredentials = new Lyralei.Core.ServerQueryConnection.Models.Subscribers() { AdminPassword = user.ServerQueryPassword, AdminUsername = user.ServerQueryUsername, ServerIp = Subscriber.ServerIp, ServerPort = Subscriber.ServerPort, SubscriberId = Subscriber.SubscriberId, SubscriberUniqueId = Subscriber.SubscriberUniqueId, VirtualServerId = Subscriber.VirtualServerId, }; using (Core.ServerQueryConnection.ServerQueryConnection ServerQueryConnection = new Core.ServerQueryConnection.ServerQueryConnection(subscriberUserCredentials)) { Thread thread = new Thread((ThreadStart) new SynchronizationCallback(ServerQueryConnection.InitializeQuiet)); thread.Start(); thread.Join(); var result = ServerQueryConnection.QueryRunner.SendCommand(cmd); return(result); } }
public void ServerQueryUserRegistrationCommand(BotCommandEventArgs e) { using (var db = new CoreContext()) { var user = UserManager.QueryUser(Subscriber.SubscriberId, Subscriber.SubscriberUniqueId, e.MessageInfo.InvokerUniqueId); Models.ServerQueryUser sqUser = new Models.ServerQueryUser() { UserId = user.UserId, Users = user, //SubscriberId = user.SubscriberId, ServerQueryUsername = e.CommandInfo["username"].Value, ServerQueryPassword = e.CommandInfo["password"].Value, }; Lyralei.Core.ServerQueryConnection.Models.Subscribers subscriberUserCredentials = new Lyralei.Core.ServerQueryConnection.Models.Subscribers() { AdminPassword = sqUser.ServerQueryPassword, AdminUsername = sqUser.ServerQueryUsername, ServerIp = Subscriber.ServerIp, ServerPort = Subscriber.ServerPort, SubscriberId = Subscriber.SubscriberId, SubscriberUniqueId = Subscriber.SubscriberUniqueId, VirtualServerId = Subscriber.VirtualServerId, }; Core.ServerQueryConnection.ServerQueryConnection ServerQueryConnection = new Core.ServerQueryConnection.ServerQueryConnection(subscriberUserCredentials); Thread thread = new Thread((ThreadStart) new SynchronizationCallback(ServerQueryConnection.InitializeQuiet)); thread.Start(); thread.Join(); try { if (ServerQueryConnection.AsyncTcpDispatcher.IsConnected) { var test = ServerQueryConnection.whoAmI; if (test == null) { throw new Exception("Login failure"); } if (test.IsErroneous) { throw new Exception(test.ResponseText); } else { db.ServerQueryUser.Add(sqUser); db.SaveChanges(); ServerQueryConnection.Logout(); ServerQueryConnection.Disconnect(); //User successfully registered ServerQueryConnection.TextReply(e.MessageInfo, "Successfully registered! You can now execute serverquery commands directly to me based on your user permissions."); } } } catch (Exception ex) { logger.Debug(ex, "User failed to register"); ServerQueryConnection.TextReply(e.MessageInfo, "Whoops! Did you put in the right details?"); } } }
private void onBotCommand(object sender, CommandParameterGroup cmdPG, MessageReceivedEventArgs e) { //Command cmd = new Command("help", new string[] { "serverinfo", "additionaltest" }); Command cmd = new Command(cmdPG); //userManager.GetUserInformation if (cmd.Name.ToLower() == "serverquery") { try { if (cmdPG.Exists(cmdP => cmdP.Name.ToLower() == "action" && cmdP.Value.ToLower() == "register")) { CommandParameter Username = null; CommandParameter Password = null; if (cmdPG.Exists(cmdP => (Username = cmdP).Name.ToLower() == "username") && cmdPG.Exists(cmdP => (Password = cmdP).Name.ToLower() == "password")) { using (var db = new CoreContext()) { var user = UserManager.QueryUser(Subscriber.SubscriberId, Subscriber.SubscriberUniqueId, e.InvokerUniqueId); Models.ServerQueryUser sqUser = new Models.ServerQueryUser() { UserId = user.UserId, Users = user, //SubscriberId = user.SubscriberId, ServerQueryUsername = Username.Value, ServerQueryPassword = Password.Value, }; Lyralei.Core.ServerQueryConnection.Models.Subscribers subscriberUserCredentials = new Lyralei.Core.ServerQueryConnection.Models.Subscribers() { AdminPassword = sqUser.ServerQueryPassword, AdminUsername = sqUser.ServerQueryUsername, ServerIp = Subscriber.ServerIp, ServerPort = Subscriber.ServerPort, SubscriberId = Subscriber.SubscriberId, SubscriberUniqueId = Subscriber.SubscriberUniqueId, VirtualServerId = Subscriber.VirtualServerId, }; Core.ServerQueryConnection.ServerQueryConnection ServerQueryConnection = new Core.ServerQueryConnection.ServerQueryConnection(subscriberUserCredentials); Thread thread = new Thread((ThreadStart) new SynchronizationCallback(ServerQueryConnection.InitializeQuiet)); thread.Start(); thread.Join(); //do //{ // Thread.Sleep(5); //} while (t.ThreadState == ThreadState.Running); try { if (ServerQueryConnection.AsyncTcpDispatcher.IsConnected) { var test = ServerQueryConnection.whoAmI; if (test == null) { throw new Exception("Login failed"); } if (test.IsErroneous) { throw new Exception(test.ResponseText); } else { db.ServerQueryUser.Add(sqUser); db.SaveChanges(); ServerQueryConnection.QueryRunner.Logout(); ServerQueryConnection.AsyncTcpDispatcher.Disconnect(); //User successfully registered ServerQueryConnection.TextReply(e, "Successfully registered! You can now execute serverquery commands directly to me based on your user permissions."); } } } catch (Exception ex) { throw ex; } } } } } catch (Exception ex) { logger.Debug(ex, "User sent invalid command: {0}"); ServerQueryConnection.TextReply(e, "Error, please check your command and try again."); } } foreach (CommandName cmdName in Enum.GetValues(typeof(CommandName))) { if (cmdName.ToString().ToLower() == cmd.Name.ToLower()) { Models.ServerQueryUser serverQueryUser = null; try { using (var db = new CoreContext()) { //user = userManager.GetUser(e.InvokerUniqueId); var User = db.Users.Single(usr => usr.UserTeamSpeakClientUniqueId == e.InvokerUniqueId && usr.SubscriberUniqueId == Subscriber.SubscriberUniqueId); serverQueryUser = db.ServerQueryUser.Single(sqUser => sqUser.Users.UserId == User.UserId); } } catch (Exception ex) when(ex.Message == "Sequence contains no elements") { ServerQueryConnection.TextReply(e, "You do not have access to this command."); return; } catch (Exception) { ServerQueryConnection.TextReply(e, "There was an error performing this command."); return; } try { string result = SendServerQueryCommand(cmd, serverQueryUser); //string result = SendServerQueryCommand(cmd, serverQueryUser); this.ServerQueryConnection.QueryRunner.SendTextMessage(MessageTarget.Client, e.InvokerClientId, result); } catch (Exception) { ServerQueryConnection.TextReply(e, "There was an error performing this command, or you lacked permissions."); return; } break; } } }
static void Main(string[] args) { CommandParameterGroupList cmds; cmds = CommandParameterGroupList.Parse(String.Join(" ", args)); SynchronizationContext ctx = new SynchronizationContext(); SynchronizationContext.SetSynchronizationContext(ctx); List <BotConnection> botConnections = new List <BotConnection>(); ////DB connectionstring-adding, not a good option because it won't let you use Add-Migration / Update-Database //var cmd_connectionstring = cmds.Where(cmdPG => cmdPG.Exists(cmdP => cmdP.Name == "connectionstring")); //foreach (var cmd in cmd_connectionstring) //{ // CoreContext.connectionString = cmd.SingleOrDefault(x => x.Name.ToLower() == "connectionstring").Value; // string test2 = cmd.SingleOrDefault(x => x.Name.ToLower() == "connectionstring").EncodedValue; //} using (var db = new CoreContext()) { //Database deletion switch var cmds_deldb = cmds.Where(cmdPG => cmdPG.Exists(cmdP => cmdP.Name == "deletedb")); if (cmds_deldb.Count() > 0) { db.Database.EnsureDeleted(); logger.Info("Database deleted!"); } } using (var db = new CoreContext()) { //Subscribers reset switch var cmds_rs = cmds.Where(cmdPG => cmdPG.Exists(cmdP => cmdP.Name == "resetsubscribers")); if (cmds_rs.Count() > 0) { db.Subscribers.Clear(); db.SaveChanges(); logger.Info("Subscribers removed!"); } //Subscriber-adding var cmds_dc = cmds.Where(cmdPG => cmdPG.Exists(cmdP => cmdP.Name == "subscriber")); foreach (var cmd in cmds_dc) { try { Core.ServerQueryConnection.Models.Subscribers sub = new Core.ServerQueryConnection.Models.Subscribers() { ServerIp = cmd.SingleOrDefault(x => x.Name.ToLower() == "serverip").Value, AdminPassword = cmd.SingleOrDefault(x => x.Name.ToLower() == "adminpassword").Value, AdminUsername = cmd.SingleOrDefault(x => x.Name.ToLower() == "adminusername").Value, ServerPort = Convert.ToInt16(cmd.SingleOrDefault(x => x.Name.ToLower() == "serverport").Value), VirtualServerId = Convert.ToInt32(cmd.SingleOrDefault(x => x.Name.ToLower() == "virtualserverid").Value), SubscriberUniqueId = cmd.SingleOrDefault(x => x.Name.ToLower() == "uniqueid").Value, BotNickName = cmd.SingleOrDefault(x => x.Name.ToLower() == "botnickname").Value }; var search = db.Subscribers.SingleOrDefault(x => x.SubscriberUniqueId == sub.SubscriberUniqueId && x.VirtualServerId == sub.VirtualServerId); if (search == null) { db.Subscribers.Add(sub); } else { search.ServerIp = sub.ServerIp; search.AdminPassword = sub.AdminPassword; search.AdminUsername = sub.AdminUsername; search.ServerPort = Convert.ToInt16(sub.ServerPort); search.VirtualServerId = Convert.ToInt32(sub.VirtualServerId); search.SubscriberUniqueId = sub.SubscriberUniqueId; search.BotNickName = sub.BotNickName; } var count = db.SaveChanges(); logger.Info("Subscriber {0} saved to database", sub.ToString(true)); } catch (Exception ex) { logger.Warn(ex, "Failed to add a subscriber from command line"); } } foreach (var subscriber in db.Subscribers) { logger.Info("Setting up subscriber {0}..", subscriber.ToString(true)); botConnections.Add(new BotConnection(subscriber)); } } while (true) { Thread.Sleep(1000); } }