예제 #1
0
        public static UserAccessLevelChanged AddUserAccessLevelChangedEventHandler <T>(
            this RoomWatcher <T> rw,
            Action <Chat.User, Chat.User, UserAccessLevel> callback)
            where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new UserAccessLevelChanged();

            eventProcessor.OnEvent += ualc =>
            {
                Chat.User changedBy  = null;
                Chat.User targetUser = null;

                var tasks = new[]
                {
                    Task.Run(() =>
                    {
                        changedBy = new Chat.User(rw.Host, ualc.ChangedBy, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        targetUser = new Chat.User(rw.Host, ualc.TargetUser, rw.Auth);
                    })
                };

                Task.WaitAll(tasks);

                callback(changedBy, targetUser, ualc.NewLevel);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #2
0
        public static UserMentioned AddUserMentionedEventHandler <T>(
            this RoomWatcher <T> rw,
            Action <Chat.Message, Chat.User> callback)
            where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new UserMentioned();

            eventProcessor.OnEvent += um =>
            {
                Chat.Message msg    = null;
                Chat.User    pinger = null;

                var tasks = new[]
                {
                    Task.Run(() =>
                    {
                        msg = new Chat.Message(rw.Host, um.MessageId, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        pinger = new Chat.User(rw.Host, um.PingerId, rw.Auth);
                    })
                };

                Task.WaitAll(tasks);

                callback(msg, pinger);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #3
0
        public static MessageMovedIn AddMessageMovedInEventHandler <T>(this RoomWatcher <T> rw, Action <Chat.User, Chat.Message> callback) where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new MessageMovedIn();

            eventProcessor.OnEvent += mm =>
            {
                Chat.User    movedBy = null;
                Chat.Message message = null;

                var tasks = new[]
                {
                    Task.Run(() =>
                    {
                        movedBy = new Chat.User(rw.Host, mm.MovedBy, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        message = new Chat.Message(rw.Host, mm.MessageId, rw.Auth);
                    })
                };

                Task.WaitAll(tasks);

                callback(movedBy, message);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #4
0
        public static Task <int> CreatePingAsync(this ActionScheduler actionScheduler, string message, int userId)
        {
            message.ThrowIfNullOrEmpty(nameof(message));

            var userToPing = new Chat.User(actionScheduler.Host, userId);

            return(CreatePingAsync(actionScheduler, message, userToPing.Username));
        }
        public void sessionEnded(string clientIP)
        {
            AllClients.Remove(clientIP);

            Chat.User usr = new Chat.User();
            usr.Name = _clientName;

            Chat.Packet packet = new Chat.Packet();
            packet.ID      = Chat.MSGType.Disconnect;
            packet.Content = usr;

            Broadcast(packet, null);
        }
예제 #6
0
        public static UserLeft AddUserLeftEventHandler <T>(
            this RoomWatcher <T> rw,
            Action <Chat.User> callback)
            where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new UserLeft();

            eventProcessor.OnEvent += id =>
            {
                var user = new Chat.User(rw.Host, id, rw.Auth);

                callback(user);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #7
0
        public static MessageDeleted AddMessageDeletedEventHandler <T>(
            this RoomWatcher <T> rw,
            Action <Chat.User, int> callback)
            where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new MessageDeleted();

            eventProcessor.OnEvent += md =>
            {
                var deletedBy = new Chat.User(rw.Host, md.DeletedBy, rw.Auth);

                callback(deletedBy, md.MessageId);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #8
0
        public static MessageReplyCreated AddMessageReplyCreatedEventHandler <T>(
            this RoomWatcher <T> rw,
            Action <Chat.User, Chat.Message, Chat.Message> callback)
            where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new MessageReplyCreated();

            eventProcessor.OnEvent += mr =>
            {
                Chat.User    author    = null;
                Chat.Message message   = null;
                Chat.Message targetMsg = null;

                var tasks = new[]
                {
                    Task.Run(() =>
                    {
                        author = new Chat.User(rw.Host, mr.AuthorId, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        message = new Chat.Message(rw.Host, mr.MessageId, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        targetMsg = new Chat.Message(rw.Host, mr.TargetMessageId, rw.Auth);
                    })
                };

                Task.WaitAll(tasks);

                callback(author, message, targetMsg);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #9
0
        public static UserInvitedToRoom AddUserInvitedToRoomEventHandler <T>(
            this RoomWatcher <T> rw,
            Action <Chat.User, Chat.User, Chat.Room> callback)
            where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new UserInvitedToRoom();

            eventProcessor.OnEvent += ri =>
            {
                Chat.User inviter = null;
                Chat.User invitee = null;
                Chat.Room room    = null;

                var tasks = new[]
                {
                    Task.Run(() =>
                    {
                        inviter = new Chat.User(rw.Host, ri.Inviter, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        invitee = new Chat.User(rw.Host, ri.Invitee, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        room = new Chat.Room(rw.Host, ri.Room, rw.Auth);
                    })
                };

                Task.WaitAll(tasks);

                callback(inviter, invitee, room);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #10
0
        public static MessageEdited AddMessageEditedEventHandler <T>(
            this RoomWatcher <T> rw,
            Action <Chat.Message,
                    Chat.User> callback)
            where T : IWebSocket
        {
            callback.ThrowIfNull(nameof(callback));

            var eventProcessor = new MessageEdited();

            eventProcessor.OnEvent += me =>
            {
                Chat.Message message = null;
                Chat.User    user    = null;

                var tasks = new[]
                {
                    Task.Run(() =>
                    {
                        message = new Chat.Message(rw.Host, me.Message, rw.Auth);
                    }),
                    Task.Run(() =>
                    {
                        user = new Chat.User(rw.Host, me.EditedBy, rw.Auth);
                    })
                };

                Task.WaitAll(tasks);

                callback(message, user);
            };

            rw.EventRouter.AddProcessor(eventProcessor);

            return(eventProcessor);
        }
예제 #11
0
        public static Task <int> CreatePingAsync(this ActionScheduler actionScheduler, string message, Chat.User userToPing)
        {
            message.ThrowIfNullOrEmpty(nameof(message));
            userToPing.ThrowIfNull(nameof(userToPing));

            return(CreatePingAsync(actionScheduler, message, userToPing.Username));
        }
예제 #12
0
        public static Task <bool> KickMuteUser(this ActionScheduler actionScheduler, Chat.User user)
        {
            user.ThrowIfNull(nameof(user));

            return(KickMuteUserAsync(actionScheduler, user.Id));
        }
예제 #13
0
        public static Task ChangeUserAccessLevelAsync(this ActionScheduler actionScheduler, Chat.User user, UserAccessLevel newLevel)
        {
            user.ThrowIfNull(nameof(user));

            return(ChangeUserAccessLevelAsync(actionScheduler, user.Id, newLevel));
        }
예제 #14
0
        /// <inheritdoc />
        public async Task <string> HandleChatCommand(string commandName, string arguments, Chat.User sender, CancellationToken cancellationToken)
        {
            using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false))
            {
                if (!Running)
                {
                    return("ERROR: Server offline!");
                }

                var commandObject = new ChatCommand
                {
                    Command    = commandName,
                    Parameters = arguments,
                    User       = sender
                };

                var json = JsonConvert.SerializeObject(commandObject, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

                var command = String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", byondTopicSender.SanitizeString(Constants.DMTopicChatCommand), byondTopicSender.SanitizeString(Constants.DMParameterData), byondTopicSender.SanitizeString(json));

                var activeServer = AlphaIsActive ? alphaServer : bravoServer;
                return(await activeServer.SendCommand(command, cancellationToken).ConfigureAwait(false) ?? "ERROR: Bad topic exchange!");
            }
        }
        public void ReceiveMessage(IAsyncResult ar)
        {
            //Read from client
            int bytesRead = 0;

            try
            {
                if (_client.Connected == true)
                {
                    lock (_client.GetStream())
                    {
                        bytesRead = _client.GetStream().EndRead(ar);
                    }

                    //Client disconnected
                    if (bytesRead < 1)
                    {
                        sessionEnded(_clientIP);
                        return;
                    }
                    else
                    {
                        //The message received from the remote client
                        string messageReceived = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);

                        // Deserialize the packet
                        Chat.Packet packet = new Chat.Packet();
                        packet = (Chat.Packet)Chat.Deserialize(messageReceived, packet.GetType());

                        Chat.Users recipients = new Chat.Users();
                        switch (packet.ID)
                        {
                        case Chat.MSGType.Join:


                            Chat.User usr = (Chat.User)packet.Content;
                            _clientName = usr.Name;

                            // Add the user set to receive a full list of user
                            recipients.List.Add(usr);

                            Chat.Users usrs = new Chat.Users();
                            foreach (DictionaryEntry c in AllClients)
                            {
                                Chat.User u = new Chat.User();
                                u.Name = ((Server)c.Value)._clientName;
                                usrs.List.Add(u);
                            }

                            Chat.Packet usersListPacket = new Chat.Packet();
                            usersListPacket.ID      = Chat.MSGType.ClientList;
                            usersListPacket.Content = usrs;


                            //Send the full user list to the user that just signed on
                            Broadcast(usersListPacket, recipients);

                            //Rebroadcast the original message to everyone that this user is online
                            Broadcast(packet, null);

                            break;

                        case Chat.MSGType.Message:
                            Chat.Message message = (Chat.Message)packet.Content;

                            foreach (string recipient in message.Reciptients)
                            {
                                Chat.User u = new Chat.User();
                                u.Name = recipient;
                                recipients.List.Add(u);
                            }

                            //Send the message to the recipients
                            Broadcast(packet, recipients);
                            break;

                        default:

                            break;
                        }
                    }

                    //Continue reading from the client
                    lock (_client.GetStream()) {
                        _client.GetStream().BeginRead(data, 0, Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);
                    }
                }
            } catch (Exception ex) {
                sessionEnded(_clientIP);
            }
        }