Пример #1
0
        public void Add(User user)
        {
            Actions.BeforeUserAdd.ForEach((Action<UserManager, User> action) => {
                action.Invoke(this, user);
            });

            Users.Add(user.Id, user);

            Actions.AfterUserAdd.ForEach((Action<UserManager, User> action) => {
                action.Invoke(this, user);
            });
        }
Пример #2
0
        public Client(string identifier, Config configManager)
            : base(identifier, configManager)
        {
            // Create the bot user
            ServerUser = new User {
                Id = -1,
                Username = "******"
            };

            // Fetch server address from the config
            string server = Config.Get("Chat.Protocols.SockChat", "Address", "ws://enter_server_address_here");

            // Create the websocket object
            Socket = new WebSocket(server);

            // Assign the event listeners
            Socket.OnOpen += OnOpen;
            Socket.OnClose += OnClose;
            Socket.OnError += OnError;
            Socket.OnMessage += OnMessage;

            // Connect
            Socket.Connect();
        }
Пример #3
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;
            }
        }
Пример #4
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;
            }
        }
Пример #5
0
        private void HandleUserUpdate(UserManager manager, User prev, User user)
        {
            string data = Shared.Pack(
                ServerMethod.User,
                prev.Id,
                user.Username,
                user.Colour,
                Shared.GetPermissions(user)
            );

            foreach (Service session in Sessions) {
                session.Context.WebSocket.Send(data);
            }
        }
Пример #6
0
        private void HandleUserRemove(UserManager manager, User user, UserRemove mode)
        {
            string data = Shared.Pack(
                ServerMethod.Leave,
                user.Id,
                user.Username,
                mode == UserRemove.Forced ? "kick" : "leave",
                UnixNow,
                InternalId
            );

            foreach (Service session in Sessions) {
                if (session.User == user) {
                    continue;
                }

                session.Context.WebSocket.Send(data);
            }
        }
Пример #7
0
        private void HandleUserAdd(UserManager manager, User user)
        {
            string data = Shared.Pack(
                ServerMethod.Join,
                UnixNow,
                user.Id,
                user.Username,
                user.Colour,
                Shared.GetPermissions(user),
                InternalId
            );

            foreach (Service session in Sessions) {
                if (session.User == user) {
                    continue;
                }

                session.Context.WebSocket.Send(data);
            }
        }
Пример #8
0
 private void HandleUserUpdate(UserManager manager, User prev, User user)
 {
 }
Пример #9
0
 private void HandleUserRemove(UserManager manager, User user, UserRemove mode)
 {
 }
Пример #10
0
 private void HandleUserAdd(UserManager manager, User user)
 {
 }
Пример #11
0
        public void Remove(User user, UserRemove mode = UserRemove.Normal)
        {
            Actions.BeforeUserRemove.ForEach((Action<UserManager, User, UserRemove> action) => {
                action.Invoke(this, user, mode);
            });

            Users.Remove(user.Id);

            Actions.AfterUserRemove.ForEach((Action<UserManager, User, UserRemove> action) => {
                action.Invoke(this, user, mode);
            });
        }
Пример #12
0
 public void Update(User old, User user)
 {
     Update(old.Id, user);
 }
Пример #13
0
        public void Update(int id, User user)
        {
            if (Users.ContainsKey(id)) {
                User prev = Users[id];

                Actions.BeforeUserUpdate.ForEach((Action<UserManager, User, User> action) => {
                    action.Invoke(this, prev, user);
                });

                Users[id] = user;

                Actions.AfterUserUpdate.ForEach((Action<UserManager, User, User> action) => {
                    action.Invoke(this, prev, user);
                });
            }
        }