Represents the configuration information necessary to connect to a chat server.
Ejemplo n.º 1
0
        public SlackAdapter(ChatServer chatServerConfig)
        {
            serverConfig = chatServerConfig;
            client = new SlackSocketClient(serverConfig.Password);

            Messages = Observable.FromEvent<Action<SlackAPI.WebSocketMessages.NewMessage>, ChatMessage>(handler =>
            {
                Action<SlackAPI.WebSocketMessages.NewMessage> converter = slackMessage =>
                {
                    try
                    {
                        if (client == null || string.IsNullOrWhiteSpace(slackMessage.user))
                        {
                            return;
                        }

                        User user = client.UserLookup[slackMessage.user];

                        // Don't relay our own messages
                        if (user.name.Equals(serverConfig.UserName, StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }

                        if (slackMessage.channel.StartsWith("D"))
                        {
                            Console.WriteLine("({0})DirectMessage {1}: {2}", serverConfig.ServerId, user.name, slackMessage.text);
                            return;
                        }

                        Channel channel = slackMessage.channel.StartsWith("C")
                            ? client.ChannelLookup[slackMessage.channel]
                            : client.GroupLookup[slackMessage.channel];

                        var chatMessage = new ChatMessage
                        {
                            ServerId = serverConfig.ServerId,
                            Room = channel.name,
                            User = user.name,
                            Text = FormatIncomingMessage(slackMessage.text),
                            TimeStamp = slackMessage.ts
                        };

                        handler(chatMessage);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{serverConfig.ServerId}|EXCEPTION: {ex}");
                    }
                };

                return converter;
            },
                converter => client.OnMessageReceived += converter,
                converter => client.OnMessageReceived -= converter
            );

            connectionStatusTimer = new Timer(15000) { AutoReset = true };
            connectionStatusTimer.Elapsed += ConnectionStatusTimer_Elapsed;
        }
Ejemplo n.º 2
0
        public JabbrAdapter(ChatServer chatServerConfig)
        {
            serverConfig = chatServerConfig;
            client = new JabbRClient(serverConfig.ServerAddress)
            {
                AutoReconnect = true
            };

            Messages = Observable.FromEvent<Action<Message, string>, ChatMessage>(handler =>
            {
                Action<Message, string> converter = (jabbrMessage, room) =>
                {
                    try
                    {
                        if (client == null)
                        {
                            return;
                        }

                        // Don't relay our own messages
                        if (jabbrMessage.User.Name.Equals(serverConfig.UserName, StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }

                        var chatMessage = new ChatMessage
                        {
                            ServerId = serverConfig.ServerId,
                            Room = room,
                            User = jabbrMessage.User.Name,
                            Text = jabbrMessage.Content,
                            TimeStamp = jabbrMessage.When
                        };

                        handler(chatMessage);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{serverConfig.ServerId}|EXCEPTION: {ex}");
                    }
                };

                return converter;
            },
                converter => client.MessageReceived += converter,
                converter => client.MessageReceived -= converter
            );
        }
Ejemplo n.º 3
0
        public IrcAdapter(ChatServer chatServerConfig)
        {
            serverConfig = chatServerConfig;
            client = new StandardIrcClient { FloodPreventer = new IrcStandardFloodPreventer(5, 2000) };

            Messages = Observable.FromEvent<EventHandler<IrcRawMessageEventArgs>, ChatMessage>(handler =>
            {
                EventHandler<IrcRawMessageEventArgs> converter = (sender, ircMessageArgs) =>
                {
                    try
                    {
                        if (client == null)
                        {
                            return;
                        }

                        // If it's not an actual user message, ignore it
                        if (!ircMessageArgs.Message.Command.Equals("PRIVMSG", StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }

                        string messageSender = ircMessageArgs.Message.Source.Name;
                        string message = ircMessageArgs.Message.Parameters[1];
                        string roomName = ircMessageArgs.Message.Parameters[0];
                        if (roomName != null && roomName.StartsWith("#"))
                        {
                            roomName = roomName.Substring(1);
                        }

                        // Don't relay our own messages
                        if (messageSender.Equals(serverConfig.UserName, StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }

                        // Check for emotes, ignore for now
                        if (message.StartsWith("\u0001" + "ACTION", StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }

                        var chatMessage = new ChatMessage
                        {
                            ServerId = serverConfig.ServerId,
                            Room = roomName,
                            User = messageSender,
                            Text = message,
                            TimeStamp = DateTimeOffset.Now // IRC Doesn't hand us the timestamp
                        };

                        handler(chatMessage);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{serverConfig.ServerId}|EXCEPTION: {ex}");
                    }
                };

                return converter;
            },
                converter => client.RawMessageReceived += converter,
                converter => client.RawMessageReceived -= converter
            );

            client.Connected += Client_Connected;
            client.Disconnected += Client_Disconnected;

            // Debugging
            client.ConnectFailed += Client_ConnectFailed;
            client.ErrorMessageReceived += Client_ErrorMessageReceived;
        }