Пример #1
0
        protected override void OnMessage(MessageEventArgs e)
        {
            if (!e.IsText) {
                return;
            }

            Env.Log.Write(LogSeverity.Info, e.Data);

            string[] data = Shared.Unpack(e.Data);
            ClientMethod method = (ClientMethod)int.Parse(data[0]);

            switch (method) {
                case ClientMethod.Ping:
                    if (User != null && User.Id == int.Parse(data[1])) {
                        Send(Shared.Pack(ServerMethod.Ping, "pong"));
                    }
                    break;

                case ClientMethod.Auth:
                    // check if there's already an active context here first
                    // alternatively attempt to implement multiple sessions

                    AuthResult authResult = AuthClient.Attempt(new AuthAttempt {
                        Method = AuthMethod.Session,
                        UserId = int.Parse(data[1]),
                        SessionId = data[2]
                    });

                    if (!authResult.Success) {
                        Send(Shared.Pack(
                            ServerMethod.Join,
                            "n",
                            AuthFail[0]
                        ));
                        return;
                    }

                    if (ServerContext.Users.Exists(authResult.UserId)) {
                        User = ServerContext.Users.Get(authResult.UserId);
                    } else {
                        User = new User {
                            Id = authResult.UserId,
                            Username = authResult.Username,
                            Colour = authResult.Colour == null ? Colour.None : Colour.FromHex(authResult.Colour),
                            Hierarchy = authResult.Hierarchy,
                            CanBan = authResult.CanBan,
                            CanSilence = authResult.CanSilence,
                            ChangeUsername = authResult.ChangeUsername,
                            CreateChannel = authResult.CreateChannel,
                            PermanentChannel = authResult.PermanentChannel
                        };

                        ServerContext.Users.Add(User);
                    }

                    Channel firstChan = ServerContext.Channels.First();

                    Send(Shared.Pack(
                        ServerMethod.Join,
                        "y",
                        User.Id,
                        User.Username,
                        User.Colour,
                        Shared.GetPermissions(User),
                        firstChan.Name
                    ));

                    SendChannels();
                    SwitchChannel(firstChan.Name);
                    break;

                case ClientMethod.Message:
                    string msgText = data[2] ?? null;

                    if (msgText == null || msgText.Length < 1) {
                        return;
                    }

                    if (msgText.Substring(0, 1) == "/") {
                        string[] msgSplit = msgText.Split(' ');

                        switch (msgSplit[0].Substring(1)) {
                            case "kick":
                                return;

                            case "unban":
                                return;

                            case "bans":
                                return;

                            case "silence":
                                return;

                            case "unsilence":
                                return;

                            case "ip":
                                return;

                            case "afk":
                                return;

                            case "join":
                                SwitchChannel(msgSplit[1]);
                                return;

                            case "create":
                                return;

                            case "msg":
                                User pmTarget = ServerContext.Users.Get(msgSplit[1]);

                                if (pmTarget == null) {
                                    //
                                } else {
                                    //
                                }
                                return;
                        }
                    }

                    Message addMsg = new Message {
                        Id = ServerContext.Messages.Increments,
                        User = ServerContext.Users.Get(int.Parse(data[1])),
                        Text = msgText,
                        Date = DateTime.UtcNow,
                        Channel = Channel,
                    };

                    ServerContext.Messages.Add(addMsg);
                    break;
            }
        }
Пример #2
0
        private void OnMessage(object sender, MessageEventArgs ev)
        {
            if (!ev.IsText) {
                Log.Write(LogSeverity.Info, "Received non-text data, ingoring.");
                return;
            }

            string[] data = Shared.Unpack(ev.Data);
            ServerMethod method = (ServerMethod)int.Parse(data[0]);

            switch (method)
            {
                case ServerMethod.Join:
                    if (UserId == 0)
                    {
                        if (data[1] == "y")
                        {
                            UserId = int.Parse(data[2]);

                            if (Channel == null)
                            {
                                Channel = data[6];
                            }

                            StartKeepAlive();
                            StartMessageQueue();
                        } else
                        {
                            switch (data[2])
                            {
                                case "joinfail":
                                    if (data[2] == "-1")
                                    {
                                        Log.Write(LogSeverity.Severe, "You are permanently banned.");
                                    }
                                    else
                                    {
                                        DateTime banDate = EpochFrom(long.Parse(data[3]));

                                        Log.Write(
                                            LogSeverity.Severe,
                                            "You are banned until {0} {1}.",
                                            banDate.ToLongDateString(),
                                            banDate.ToLongTimeString()
                                        );
                                    }
                                    break;

                                case "authfail":
                                    Log.Write(LogSeverity.Severe, "Authentication failed.");
                                    break;

                                default:
                                    Log.Write(LogSeverity.Severe, "Connection failed.");
                                    break;
                            }

                            Socket.Close();
                            return;
                        }
                    }

                    User joinUser = new User {
                        Id = int.Parse(data[2]),
                        Username = data[3],
                        Colour = data[4] == "inherit" ? Colour.None : Colour.FromHex(data[4]),
                        ActiveUser = UserId == int.Parse(data[2])
                    };

                    joinUser = Shared.SetPermissions(joinUser, data[5]);
                    Users.Add(joinUser);

                    if (joinUser.ActiveUser)
                    {
                        break;
                    }

                    Log.Write(LogSeverity.Info, "{0} joined.", joinUser.Username);
                    break;

                case ServerMethod.Message:
                    User msgUser = Users.Get(int.Parse(data[2]));
                    Message msg = new Message();
                    string msgText = data[3];
                    string[] msgTextParts = msgText.Split('\f');
                    bool msgIsPM = data[5][4] == '1';
                    bool msgIsBot = msgUser == null;
                    bool msgIsError = msgIsBot && msgTextParts[0] == "1";

                    if (msgIsBot)
                    {
                        msgUser = ServerUser;
                        // parse server messages?
                    }

                    msg.Id = int.Parse(data[4]);
                    msg.Date = EpochFrom(long.Parse(data[1]));
                    msg.User = msgUser;
                    msg.Text = msgText;
                    //msg.Channel = Channel;
                    //msg.IsPrivate = msgIsPM;

                    Messages.Add(msg);

                    Log.Write(
                        LogSeverity.Info,
                        "{0}: {1}",
                        msg.User.Username,
                        msg.Text
                    );
                    break;

                case ServerMethod.Leave:
                    User leaveUser = Users.Get(int.Parse(data[1]));

                    Log.Write(LogSeverity.Info, "{0} left.", leaveUser.Username);

                    Users.Remove(leaveUser);
                    break;

                case ServerMethod.Channel:
                    Log.Write(LogSeverity.Info, "Channel update.");
                    break;

                case ServerMethod.Move:
                    Log.Write(LogSeverity.Info, "User moved channels.");
                    break;

                case ServerMethod.Delete:
                    Log.Write(LogSeverity.Info, "Message was deleted.");
                    break;

                case ServerMethod.Populate:
                    int populateMode = int.Parse(data[1]);

                    switch (populateMode)
                    {
                        case 0:
                            int amount = int.Parse(data[2]);
                            int start = 3;

                            for (int i = 0; i < amount; i++)
                            {
                                User populateUser = new User {
                                    Id = int.Parse(data[start]),
                                    Username = data[start + 1],
                                    Colour = data[start + 2] == "inherit" ? Colour.None : Colour.FromHex(data[start + 2])
                                };

                                populateUser = Shared.SetPermissions(populateUser, data[start + 3]);

                                Log.Write(LogSeverity.Info, "{0} is logged in.", populateUser.Username);

                                Users.Add(populateUser);

                                start = start + 5;
                            }
                            break;

                        case 1:
                            // we don't need this
                            break;

                        case 2:
                            // i don't think this'll be needed either i dunno
                            break;
                    }
                    break;

                case ServerMethod.Clear:
                    User clearKeep = Users.Get(UserId);

                    switch (int.Parse(data[1]))
                    {
                        case 0:
                            Messages.Clear();
                            break;

                        case 1:
                            Users.Clear();
                            Users.Add(clearKeep);
                            break;

                        case 2:
                            // remove all channels
                            break;

                        case 3:
                            Messages.Clear();
                            Users.Clear();
                            Users.Add(clearKeep);
                            break;

                        case 4:
                            Messages.Clear();
                            Users.Clear();
                            Users.Add(clearKeep);
                            // channels delete
                            break;
                    }
                    break;

                case ServerMethod.Ban:
                    if ((data[1] == "ban" || data[1] == "1"))
                    {
                        if (data[2] == "-1")
                        {
                            Log.Write(LogSeverity.Severe, "You are permanently banned.");
                        } else
                        {
                            DateTime banDate = EpochFrom(long.Parse(data[2]));

                            Log.Write(
                                LogSeverity.Severe,
                                "You are banned until {0} {1}.",
                                banDate.ToLongDateString(),
                                banDate.ToLongTimeString()
                            );
                        }
                    }
                    break;

                case ServerMethod.User:
                    User updateUser = Users.Get(int.Parse(data[1]));
                    string oldUsername = updateUser.Username;

                    updateUser.Id = int.Parse(data[1]);
                    updateUser.Username = data[2];
                    updateUser.Colour = data[3] == "inherit" ? Colour.None : Colour.FromHex(data[3]);

                    updateUser = Shared.SetPermissions(updateUser, data[4]);

                    Log.Write(
                        LogSeverity.Info,
                        "{0} is now {1}.",
                        oldUsername,
                        updateUser.Username
                    );

                    Users.Update(updateUser.Id, updateUser);
                    break;
            }
        }
Пример #3
0
 private void SendMessage(Message msg)
 {
     Send(Shared.Pack(
         ServerMethod.Message,
         (new DateTimeOffset(msg.Date)).ToUnixTimeSeconds(),
         msg.User.Id,
         msg.Text,
         msg.Id,
         ""
     ));
 }
Пример #4
0
        private void HandleMessageUpdate(MessageManager manager, Message prev, Message msg)
        {
            string data = Shared.Pack(
                ServerMethod.Message,
                UnixNow,
                msg.User.Id,
                msg.Text,
                msg.Id,
                "0 0 0 0 0"
            );

            foreach (Service session in Sessions) {
                if (session.Channel != msg.Channel) {
                    continue;
                }

                session.Context.WebSocket.Send(data);
            }
        }
Пример #5
0
        private void HandleMessageRemove(MessageManager manager, Message msg)
        {
            string data = Shared.Pack(ServerMethod.Delete, msg.Id);

            foreach (Service session in Sessions) {
                if (session.Channel != msg.Channel) {
                    continue;
                }

                session.Context.WebSocket.Send(data);
            }
        }
Пример #6
0
 private void HandleMessageUpdate(MessageManager manager, Message prev, Message msg)
 {
 }
Пример #7
0
 private void HandleMessageRemove(MessageManager manager, Message msg)
 {
 }
Пример #8
0
        private void HandleMessageAdd(MessageManager manager, Message msg)
        {
            string send = $":{msg.User.Username} PRIVMSG {msg.Channel.Name} :{msg.Text}";

            foreach (Session session in Sessions.Values) {
                if (!session.IsAuthenticated) {// || session.User == null) {
                    continue;
                }

                // todo: check if we're in channel

                session.Send(send);
            }
        }