Esempio n. 1
0
        public void Handler(Event CurrentEvent)
        {
            if (CurrentEvent.GetCode() == Events.Code_PacketRequestEvent)
            {
                PacketRequestEvent ev = (PacketRequestEvent)CurrentEvent;

                if (ev.GetPacket().GetPacketID() == Network.INVENTORY_PACKET)
                {
                    Player player = ((InventoryPacketRequest)ev.GetPacket()).Player;

                    if (ev.GetPacket().GetInt("ox") != -1 && player != null)
                    {
                        ushort ox = ev.GetPacket().GetUShort("ox");
                        ushort oy = ev.GetPacket().GetUShort("oy");
                        ushort x  = ev.GetPacket().GetUShort("x");
                        ushort y  = ev.GetPacket().GetUShort("y");
                        int    id = ev.GetPacket().GetInt("id");

                        player.Click(ox, oy, x, y, id);
                    }
                }

                if (ev.GetPacket().GetPacketID() == Network.CHAT_PACKET && !ev.Cancelled)
                {
                    ChatPacketRequest packet = (ChatPacketRequest)ev.GetPacket();

                    if (packet.Message.Length > 0)
                    {
                        packet.Player.Chat(packet.Message);
                    }
                }
            }

            if (CurrentEvent.GetCode() == Events.Code_PacketResponseEvent)
            {
                PacketResponseEvent ev = (PacketResponseEvent)CurrentEvent;

                switch (ev.GetPacket().GetPacketID())
                {
                case Network.AUTH_PACKET:
                    if (ev.GetPacket().GetStatus() == Packet.RESPONSE_STATUS_OK)
                    {
                        AuthPacketResponse packet = (AuthPacketResponse)ev.GetPacket();

                        if (player.control.Ban.IsBanned(packet.Login))
                        {
                            packet.SetError(Errors.PlayerBanned);

                            Data.SendToLog("Name banned! Closed: " + packet.Login, Data.Log_Warning);

                            return;
                        }

                        Regex r = new Regex(@"^[a-zA-Z][a-zA-Z0-9-_]{1,20}$", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                        if (!r.Match(packet.Login).Success)
                        {
                            packet.SetError(Errors.InvalidName);

                            Data.SendToLog("Name invalid! Closed: " + packet.Login, Data.Log_Warning);

                            return;
                        }

                        if (Array.IndexOf(Server.GetOnlinePlayersStr(), packet.Login) < 0)
                        {
                            Server.JoinPlayer(new Player(packet.Token, packet.Login,
                                                         Server.GetLevel(Server.Properties.GetProperty("default-level-name")), packet.Address));
                        }
                        else
                        {
                            packet.SetError(Errors.PlayerAlreadyExists);
                        }
                    }
                    break;

                case Network.GAMESTATUS_PACKET:
                    if (ev.GetPacket().GetStatus() == Packet.RESPONSE_STATUS_OK)
                    {
                        GamestatusPacketResponse packet = (GamestatusPacketResponse)ev.GetPacket();
                        if (packet.Player != null)
                        {
                            packet.Player.Connection.NewStamp(DateTime.Now);
                        }
                    }
                    break;

                case Network.SETTINGS_PACKET:
                    if (ev.GetPacket().GetStatus() == Packet.RESPONSE_STATUS_OK)
                    {
                        SettingsPacketResponse packet = (SettingsPacketResponse)ev.GetPacket();

                        Events.CallEvent(new SettingsActivatedEvent(packet.Player, packet.ActivatedElement));
                    }
                    break;
                }
            }
        }
Esempio n. 2
0
        public static string HandleCommand(string[] Args, string ownerName = "Console")
        {
            events.ConsoleCommandEvent ev = new events.ConsoleCommandEvent(Args);
            events.Events.CallEvent(ev);
            if (ev.Cancelled)
            {
                return(ev.Metadata);
            }

            switch (Args[0])
            {
            case "?":
            case "help": ShowHelpText(); return(string.Join(", ", HelpCommandLines));

            case "exit": Server.Exit(); return(Strings.From("cmd.closing"));

            case "stop": Server.ServerStop(); return(Strings.From("cmd.closing"));

            case "resume": Server.ServerResume(); return(Strings.From("cmd.resumed"));

            case "online":
                Data.SendToLog("Online: " + Server.GetOnlinePlayers().Length.ToString());
                foreach (player.Player p in Server.GetOnlinePlayers())
                {
                    Data.SendToLog("- " + p.ToString());
                }
                return(string.Join(", ", Server.GetOnlinePlayersStr()));

            case "ban":
                if (Args.Length < 2)
                {
                    return(Strings.From("cmd.badargument"));
                }

                string ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9‌​]{2}|2[0-4][0-9]|25[0-5])$";
                Regex  r = new Regex(ValidIpAddressRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);

                if (r.Match(Args[1]).Success)
                {
                    player.control.Ban.BanIP(Args[1], ownerName);
                }
                else
                {
                    player.control.Ban.BanByName(Args[1], ownerName);
                }
                return("ok");

            case "pardon":
                if (Args.Length < 2)
                {
                    return(Strings.From("cmd.badargument"));
                }

                ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9‌​]{2}|2[0-4][0-9]|25[0-5])$";
                r = new Regex(ValidIpAddressRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline);

                if (r.Match(Args[1]).Success)
                {
                    player.control.Ban.PardonIP(Args[1], ownerName);
                }
                else
                {
                    player.control.Ban.PardonByName(Args[1], ownerName);
                }
                return("ok");

            case "ip":
                string ip = "(server) " + Server.GetFullAddress();
                if (Args.Length > 1 && Server.GetPlayer(Args[1]) != null)
                {
                    ip = Server.GetPlayer(Args[1]).Connection.Address;
                }
                Data.SendToLog("IP = " + ip);
                return("IP = " + ip);

            default: Data.SendToLog(Strings.From("command.unknown"), Data.Log_Warning); return(Strings.From("command.unknown"));
            }
        }