Example #1
0
        public void NSCSMOnline()
        {
            packetCommandSub = ez.Read1();
            byte packetCommandSubSub = ez.Read1();

            if (packetCommandSub == 0) { // Login
                ez.Read1(); // Reserved byte

                string smoUsername = ez.ReadNT();
                string smoPassword = ez.ReadNT();

                if (!new Regex("^([A-F0-9]{32})$").Match(smoPassword).Success) {
                    ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                    ez.Write2(1);
                    ez.WriteNT("Login failed! Invalid password.");
                    ez.SendPack();

                    MainClass.AddLog("Invalid password hash given!", true);
                    return;
                }

                Hashtable[] smoLoginCheck = Sql.Query("SELECT * FROM \"users\" WHERE \"Username\"='" + Sql.AddSlashes(smoUsername) + "'");
                if (smoLoginCheck.Length == 1 && smoLoginCheck[0]["Password"].ToString() == smoPassword) {
                    MainClass.AddLog(smoUsername + " logged in.");

                    User_Table = smoLoginCheck[0];
                    User_ID = (int)User_Table["ID"];
                    User_Name = (string)User_Table["Username"];
                    User_Rank = (UserRank)User_Table["Rank"];

                    ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                    ez.Write2(0);
                    ez.WriteNT("Login success!");
                    ez.SendPack();

                    SendChatMessage(mainClass.ServerConfig.Get("Server_MOTD"));
                    SendRoomList();

                    User[] users = GetUsersInRoom();
                    foreach (User user in users)
                        user.SendRoomPlayers();

                    return;
                } else if (smoLoginCheck.Length == 0) {
                    if (bool.Parse(mainClass.ServerConfig.Get("Allow_Registration"))) {
                        Sql.Query("INSERT INTO main.users (\"Username\",\"Password\",\"Email\",\"Rank\",\"XP\") VALUES(\"" + Sql.AddSlashes(smoUsername) + "\",\"" + Sql.AddSlashes(smoPassword) + "\",\"\",0,0)");
                        MainClass.AddLog(smoUsername + " is now registered with hash " + smoPassword);

                        User_Table = Sql.Query("SELECT * FROM \"users\" WHERE \"Username\"='" + Sql.AddSlashes(smoUsername) + "' AND \"Password\"='" + Sql.AddSlashes(smoPassword) + "'")[0];
                        User_ID = (int)User_Table["ID"];
                        User_Name = (string)User_Table["Username"];
                        User_Rank = (UserRank)User_Table["Rank"];

                        ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                        ez.Write2(0);
                        ez.WriteNT("Login success!");
                        ez.SendPack();

                        SendChatMessage(mainClass.ServerConfig.Get("Server_MOTD"));
                        SendRoomList();

                        User[] users = GetUsersInRoom();
                        foreach (User user in users)
                            user.SendRoomPlayers();

                        return;
                    }
                }

                MainClass.AddLog(smoUsername + " tried logging in with hash " + smoPassword + " but failed");

                ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                ez.Write2(1);
                ez.WriteNT("Login failed! Invalid password.");
                ez.SendPack();
            } else if (packetCommandSub == 01) { // Join room
                if (!RequiresAuthentication()) return;

                if (ez.LastPacketSize == 0)
                    return;

                string joinRoomName = ez.ReadNT();
                string joinRoomPass = "";

                if (ez.LastPacketSize > 0)
                    joinRoomPass = ez.ReadNT();

                foreach (Room room in mainClass.Rooms) {
                    if (room.Name == joinRoomName && (room.Password == joinRoomPass || IsModerator())) {
                        CurrentRoom = room;
                        SendToRoom();
                        break;
                    }
                }
            } else if (packetCommandSub == 02) { // New room
                if (!RequiresAuthentication()) return;

                string newRoomName = ez.ReadNT();
                string newRoomDesc = ez.ReadNT();
                string newRoomPass = "";

                if (ez.LastPacketSize > 0)
                    newRoomPass = ez.ReadNT();

                MainClass.AddLog(User_Name + " made a new room '" + newRoomName + "'");

                Room newRoom = new Room(mainClass, this);

                newRoom.Name = newRoomName;
                newRoom.Description = newRoomDesc;
                newRoom.Password = newRoomPass;

                mainClass.Rooms.Add(newRoom);

                User[] users = GetUsersInRoom();
                foreach (User user in users)
                    user.SendRoomList();

                CurrentRoom = newRoom;
                CurrentRoomRights = RoomRights.Owner;
                SendToRoom();

                SendChatMessage("Welcome to your room! Type /help for a list of commands.");
            } else {
                // This is probably only for command sub 3, which is information you get when you hover over a room in the lobby.
                // TODO: Make NSCSMOnline sub packet 3 (room info on hover)
                //MainClass.AddLog( "Discarded unknown sub-packet " + packetCommandSub.ToString() + " for NSCSMOnline" );
                ez.Discard();
            }
        }
Example #2
0
 public void SendChatAll(string Message, Room room, User exception)
 {
     foreach (User user in Users) {
     if (user.CurrentRoom == room && user != exception)
       user.SendChatMessage(Message);
       }
 }
Example #3
0
        public MainClass(string[] args)
        {
            Instance = this;
              StartTime = DateTime.Now;

              string argConfigFile = "Config.ini";

              try {
            for (int i = 0; i < args.Length; i++) {
              switch (args[i]) {
            case "--help":
            case "-h":
            case "-?":
            default:
              this.ShowHelp();
              return;

            case "--version":
            case "-v":
              Console.WriteLine("OpenSMO build " + Build);
              return;

            case "--config":
            case "-c":
              argConfigFile = args[++i];
              break;
              }
            }
              } catch {
            this.ShowHelp();
              }

              ServerConfig = new Config(argConfigFile);

              Console.Title = ServerConfig.Get("Server_Name");

              AddLog("Server starting at " + StartTime);

              if (bool.Parse(ServerConfig.Get("Server_HigherPriority"))) {
            AddLog("Setting priority to above normal");
            Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
              }

              FPS = int.Parse(ServerConfig.Get("Server_FPS"));

              // Get optional advanced settings
              if (ServerConfig.Contains("Server_Offset")) ServerOffset = (byte)int.Parse(ServerConfig.Get("Server_Offset"));
              if (ServerConfig.Contains("Server_Version")) ServerVersion = (byte)int.Parse(ServerConfig.Get("Server_Version"));
              if (ServerConfig.Contains("Server_MaxPlayers")) ServerMaxPlayers = (byte)int.Parse(ServerConfig.Get("Server_MaxPlayers"));

              MySql.Host = ServerConfig.Get("MySql_Host");
              MySql.User = ServerConfig.Get("MySql_User");
              MySql.Password = ServerConfig.Get("MySql_Password");
              MySql.Database = ServerConfig.Get("MySql_Database");

              Hashtable[] fixedRooms = MySql.Query("SELECT * FROM fixedrooms;");

              if (fixedRooms == null) {
            AddLog("It appears there's no \"fixedrooms\" table, creating one now.");
            MySql.Query(@"CREATE TABLE ""main"".""fixedrooms"" (
              ""ID""  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
              ""Name""  TEXT(255) NOT NULL,
              ""Description""  TEXT(255),
              ""Password""  TEXT(255),
              ""Free""  INTEGER,
              ""MOTD""  TEXT(255),
              ""Operators""  TEXT(255));");
              } else {
            foreach (Hashtable room in fixedRooms) {
              Room newRoom = new Room(this, null);
              newRoom.Fixed = true;
              newRoom.Name = room["Name"].ToString();
              newRoom.Description = room["Description"].ToString();
              newRoom.Password = room["Password"].ToString();
              newRoom.Free = room["Free"].ToString() == "1";
              newRoom.FixedMotd = room["MOTD"].ToString();

              string[] strOps = room["Operators"].ToString().Split(',');
              List<int> ops = new List<int>();
              foreach (string op in strOps) {
            if (op == "") {
              continue;
            }

            int opID = 0;
            if (int.TryParse(op, out opID)) {
              ops.Add(opID);
            } else {
              AddLog("Invalid op ID '" + op + "'");
            }
              }
              newRoom.FixedOperators = ops.ToArray();
              Rooms.Add(newRoom);

              AddLog("Added fixed room '" + newRoom.Name + "'");
            }
              }

              ReloadScripts();

              tcpListener = new TcpListener(IPAddress.Parse(ServerConfig.Get("Server_IP")), int.Parse(ServerConfig.Get("Server_Port")));
              tcpListener.Start();

              AddLog("Server started on port " + ServerConfig.Get("Server_Port"));

              new Thread(new ThreadStart(UserThread)).Start();

              if (bool.Parse(ServerConfig.Get("RTS_Enabled"))) {
            tcpListenerRTS = new TcpListener(IPAddress.Parse(ServerConfig.Get("RTS_IP")), int.Parse(ServerConfig.Get("RTS_Port")));
            tcpListenerRTS.Start();

            AddLog("RTS server started on port " + ServerConfig.Get("RTS_Port"));

            new Thread(new ThreadStart(RTSThread)).Start();
              }

              AddLog("Server running.");

              while (true) {
            TcpClient newTcpClient = tcpListener.AcceptTcpClient();

            string IP = newTcpClient.Client.RemoteEndPoint.ToString().Split(':')[0];
            if (Data.IsBanned(IP)) {
              if (bool.Parse(ServerConfig.Get("Game_ShadowBan"))) {
            AddLog("Shadowbanned client connected: " + IP, true);

            User newUser = new User(this, newTcpClient);
            newUser.ShadowBanned = true;
            Users.Add(newUser);
              } else {
            AddLog("Banned client kicked: " + IP, true);
            newTcpClient.Close();
              }
            } else {
              AddLog("Client connected: " + IP);

              User newUser = new User(this, newTcpClient);
              Users.Add(newUser);
            }
              }
        }
Example #4
0
        public void SendChatAll(string Message, Room room)
        {
            if (room != null)
            room.AddChatBuffer(Message);

              for (int i = 0; i < Users.Count; i++) {
            User user = Users[i];
            if (user.CurrentRoom == room)
              user.SendChatMessage(Message);
              }
        }
Example #5
0
        public void NSCSMOnline()
        {
            packetCommandSub = ez.Read1();

            //Client requested more info on room
            if (packetCommandSub == 3)
            {
            string joinRoomName = ez.ReadNT();
            ez.Discard();
            foreach (Room room in mainClass.Rooms)
            {
                if (room.Name == joinRoomName)
                {
                    //MainClass.AddLog("Found room name : " + room.Name);
                    MainClass.AddLog(Utf8Decode(room.CurrentSong.Name) + ", " + Utf8Decode(room.CurrentSong.SubTitle) + ", " + Utf8Decode(room.CurrentSong.Artist));
                    ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                    ez.Write1((byte)3);
                    ez.WriteNT(room.CurrentSong.Name);
                    ez.WriteNT(room.CurrentSong.SubTitle);
                    ez.WriteNT(room.CurrentSong.Artist);
                    ez.Write1((byte)room.Users.Count());
                    ez.Write1((byte)32);
                    foreach (User user in room.Users)
                    {
                        ez.WriteNT(user.User_Name);
                    }
                    return;

                }
            }

            }

            byte packetCommandSubSub = ez.Read1();

            if (packetCommandSub == 0)   // Login
            {
            ez.Read1(); // Reserved byte

            string smoUsername = ez.ReadNT();
            string smoPassword = ez.ReadNT();

            if (!new Regex("^([A-F0-9]{32})$").Match(smoPassword).Success)
            {
                ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                ez.Write2(1);
                ez.WriteNT("Login failed! Invalid password.");
                ez.SendPack();

                MainClass.AddLog("Invalid password hash given!", true);
                return;
            }

            if ( smoUsername.Length > 32 )
            {
                ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                ez.Write2(1);
                ez.WriteNT("Login failed! Name too Long");
                ez.SendPack();

                MainClass.AddLog("Login name too long:" + smoUsername, true);
                return;
            }

            Hashtable[] smoLoginCheck = MySql.Query("SELECT * from users where Username='******'");
            if (smoLoginCheck.Length == 1 && smoLoginCheck[0]["Password"].ToString() == smoPassword)
            {
                MainClass.AddLog(smoUsername + " logged in.");

                User_Table = smoLoginCheck[0];
                User_ID = (int)User_Table["ID"];
                User_Name = (string)User_Table["Username"];
                User_Rank = (UserRank)User_Table["Rank"];
                int User_XP = (int)User_Table["XP"];

                Hashtable[] checkstasrank = MySql.Query("select count(*) as 'levelrank' from users where xp > '" + User_XP.ToString() + "'");
                Rank_Table =  checkstasrank[0];
                User_Level_Rank = (int)Rank_Table["levelrank"] + 1;

                MySql.Query("INSERT INTO connectionlog (userid,username,password,ip,result,clientversion) VALUES('" + User_ID + "','" + MySql.AddSlashes(smoUsername) + "','" + smoPassword + "','" + User_IP + "','suceeded','" + MySql.AddSlashes(User_Game) + "')");

                User[] checkifconnected = GetUsersInServer();
                foreach (User user in  checkifconnected)
                {
                    if (user.User_Name.ToString() == this.User_Name.ToString())
                    {
                        connectioncount++;
                    }
                    if (connectioncount > 1 )
                    {
                        MainClass.AddLog("Kicking user " + this.User_Name.ToString() + " for duplicate login attempt");
                    }
                }
                if (connectioncount > 1)
                {
                    User[] kickconnected = GetUsersInServer();
                    foreach (User user in  kickconnected)
                    {
                        if (user.User_Name.ToString() == this.User_Name.ToString())
                        {
                            user.Kick();
                            break;
                        }
                    }
                }

                connectioncount = 0;
                ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                ez.Write2(0);
                ez.WriteNT("Login success!");
                ez.SendPack();

                SendChatMessage(mainClass.ServerConfig.Get("Server_MOTD"));
                string builddate = mainClass.RetrieveLinkerTimestamp().ToString("MM/dd/yy HH:mm:ss");
                SendChatMessage("Server was last updated on: "+ builddate);

                SendChatMessage("If you have not yet already Please create a room and run /scan");

                Hashtable[] checkforfriends = MySql.Query("select * from friends where friend = " + User_ID.ToString() + "");
                User[] allusers = GetUsersInServer();
                if (checkforfriends.Count() != 0 )
                {
                    for (int i = 0; i < checkforfriends.Count(); i++)
                    {
                        foreach (User user in allusers)
                        {
                            if (user.User_ID == (int)checkforfriends[i]["user"])
                            {
                                string time = DateTime.Now.ToString("HH:mm:ss");
                                user.SendChatMessage(Func.ChatColor("ffff00") + "Your friend: " + Func.ChatColor("ffffff") + "'" +NameFormat() + "'" + Func.ChatColor("ffff00") + " has connected to the server at " + time + "." + Func.ChatColor("ffffff"));
                            }
                        }
                    }
                }

                Hashtable[] checkforusers = MySql.Query("select * from friends where user = "******"");
                User[] allusersfriends = GetUsersInServer();
                if (checkforusers.Count() != 0 )
                {
                    for (int i = 0; i < checkforusers.Count(); i++)
                    {
                        foreach (User user in allusersfriends)
                        {
                            if (user.User_ID == (int)checkforusers[i]["friend"])
                            {
                                SendChatMessage(Func.ChatColor("ffff00") + "Your friend: " + Func.ChatColor("ffffff") + "'" + user.NameFormat() + "'" + Func.ChatColor("ffff00") + " Is on the server." + Func.ChatColor("ffffff"));
                            }
                        }
                    }
                }

                SendRoomList();

                User[] users = GetUsersInRoom();
                foreach (User user in users)
                    user.SendRoomPlayers();

                return;
            }
            else if (smoLoginCheck.Length == 0)
            {
                if (bool.Parse(mainClass.ServerConfig.Get("Allow_Registration")))
                {
                    MySql.Query("INSERT INTO users (Username,Password,Email,Rank,XP) VALUES(\'" + MySql.AddSlashes(smoUsername) + "\',\'" + MySql.AddSlashes(smoPassword) + "\',\'\',0,0)");
                    MainClass.AddLog(smoUsername + " is now registered with hash " + smoPassword);

                    User_Table = MySql.Query("SELECT * FROM users WHERE Username='******' AND Password='******'")[0];
                    User_ID = (int)User_Table["ID"];
                    User_Name = (string)User_Table["Username"];
                    User_Rank = (UserRank)User_Table["Rank"];

                    MySql.Query("INSERT INTO connectionlog (userid,username,password,ip,result,clientversion) VALUES('" + User_ID + "','" + MySql.AddSlashes(User_Name) + "','" + smoPassword + "','" + User_IP + "','suceeded','" + MySql.AddSlashes(User_Game) + "')");

                    ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
                    ez.Write2(0);
                    ez.WriteNT("Login success!");
                    ez.SendPack();

                    SendChatMessage(mainClass.ServerConfig.Get("Server_MOTD"));
                    SendRoomList();

                    User[] users = GetUsersInRoom();
                    foreach (User user in users)
                        user.SendRoomPlayers();

                    return;
                }
            }

            MainClass.AddLog(smoUsername + " tried logging in with hash " + smoPassword + " but failed");
            MySql.Query("INSERT INTO connectionlog (userid,username,password,ip,result,clientversion) VALUES('" + User_ID + "','" + MySql.AddSlashes(smoUsername) + "','" + smoPassword + "','" + User_IP + "','failed','" + MySql.AddSlashes(User_Game) + "')");

            ez.Write1((byte)(mainClass.ServerOffset + NSCommand.NSCSMOnline));
            ez.Write2(1);
            ez.WriteNT("Login failed! Invalid password.");
            ez.SendPack();
            }
            else if (packetCommandSub == 01)     // Join room
            {
            if (!RequiresAuthentication()) return;

            if (ez.LastPacketSize == 0)
                return;

            string joinRoomName = ez.ReadNT();
            string joinRoomPass = "";

            if (ez.LastPacketSize > 0)
                joinRoomPass = ez.ReadNT();

            int roombanned = 0;
            foreach (Room room in mainClass.Rooms)
            {
                if (room.Name == joinRoomName)
                {
                    List<int> banned  = new List<int>(room.banned);
                    foreach (int banned_id in banned)
                    {
                        if (banned_id == User_ID)
                        {
                            roombanned = 1;
                            SendChatMessage("You have been banned from this room.");
                        }
                    }
                }

                if (room.Name == joinRoomName && (room.Password == joinRoomPass || IsModerator() || room.Password == joinpass) && roombanned == 0 )
                {
                    CurrentRoom = room;
                    SendToRoom();
                    break;
                }
            }
            }
            else if (packetCommandSub == 02)     // New room
            {
            if (!RequiresAuthentication()) return;

            string newRoomName = ez.ReadNT();
            string newRoomDesc = ez.ReadNT();
            string newRoomPass = "";

            if (ez.LastPacketSize > 0)
                newRoomPass = ez.ReadNT();

            MainClass.AddLog(User_Name + " made a new room '" + newRoomName + "'");
            Room newRoom = new Room(mainClass, this);

            newRoom.Name = newRoomName;
            newRoom.Description = newRoomDesc;
            newRoom.Password = newRoomPass;

            mainClass.Rooms.Add(newRoom);

            User[] users = GetUsersInRoom();
            foreach (User user in users)
                user.SendRoomList();

            CurrentRoom = newRoom;

            if (this.CurrentRoom != null)
            {
                if ( CurrentRoom.Password != "" )
                {
                    CurrentRoom.Status = RoomStatus.Locked;
                }
                else
                {
                    CurrentRoom.Status = RoomStatus.Ready;
                }
            }
            CurrentRoomRights = RoomRights.Owner;
            SendToRoom();
            UpdateRoomStatus();
            SendChatMessage("Welcome to your room! Type /help for a list of commands.");
            }
            else
            {
            // This is probably only for command sub 3, which is information you get when you hover over a room in the lobby.
            // TODO: Make NSCSMOnline sub packet 3 (room info on hover)
            //MainClass.AddLog( "Discarded unknown sub-packet " + packetCommandSub.ToString() + " for NSCSMOnline" );
            ez.Discard();
            }
        }