protected void SearchGitHub(IrcMessageData data, string term)
        {
            var projects = _gitHubService.Search(term)
                .Take(5);

            foreach(var project in projects)
                Client.SendMessage(SendType.Message, data.Channel, string.Format("{0} - {1}", project.Name, project.Url));
        }
Exemple #2
0
        protected void AddQuote(IrcMessageData data, string text)
        {
            UnitOfWork.Start(() => _quoteRepository.CreateQuote(new Quote
                                                                        {
                                                                            Text = text,
                                                                            Timestamp = SystemTime.Now()
                                                                        }));

            Client.SendMessage(SendType.Message, data.Channel, "Quote has been added.");
        }
Exemple #3
0
        protected void SearchQuotes(IrcMessageData data, string term)
        {
            UnitOfWork.Start(() =>
                                 {
                                     var comments = _quoteRepository.SearchQuotes(term, 3);

                                     foreach (var comment in comments)
                                         Client.SendMessage(SendType.Message, data.Channel, string.Format("{0} on {1}", comment.Text, comment.Timestamp));
                                 });
        }
        protected void ListLinks(IrcMessageData data)
        {
            UnitOfWork.Start(() =>
                                 {
                                     var urls = _urlRepository.GetAllUrls();

                                     foreach (var url in urls)
                                         Client.SendMessage(SendType.Message, data.Nickname, url.SafeLink);
                                 });
        }
Exemple #5
0
        public void Process(IrcMessageData data)
        {
            _handlers.AsParallel()
                .Where(handler => handler.Type == data.Type)
                .ForAll(handler =>
                            {
                                var matches = handler.CommandRegex.Matches(data.Message);

                                foreach (Match match in matches)
                                    ExecuteHandlerMethod(data, handler, match);
                            });
        }
        public void Does_Not_Add_Empty_Quote()
        {
            // Arrange
            var data = new IrcMessageData() { Message = "!quotes add" };

            // Act
            _module.Process(data);

            // Assert
            _repository.AssertWasNotCalled(repository => repository.CreateQuote(null), opt => opt.IgnoreArguments());
            _client.AssertWasNotCalled(client => client.SendMessage(SendType.Notice, null, null), opt => opt.IgnoreArguments());
        }
        public void Can_Display_Date()
        {
            // Arrange
            var data = new IrcMessageData {Channel = "#test", Type = ReceiveType.ChannelMessage, Message = "!date"};

            SystemTime.Now = () => new DateTime(2009, 1, 1, 10, 22, 34);

            // Act
            _module.Process(data);

            // Assert
            string expected = SystemTime.Now().ToString("yyyy-MM-dd hh:mm:sstt");

            _client.AssertWasCalled(client => client.SendMessage(SendType.Message, data.Channel, expected));
        }
        public void BotRunner_Calls_Process_On_All_Modules_For_New_Line()
        {
            // Arrange
            var message = new IrcMessageData() {Message = "!test"};

            _client.Stub(client => client.MessageParser("message"))
                .Return(message);

            // Act
            _runner.Start();
            _client.OnReadLine("message");

            // Assert
            _module1.AssertWasCalled(module => module.Process(message));
            _module2.AssertWasCalled(module => module.Process(message));
        }
        public void Does_Not_Add_Same_Link_Twice()
        {
            // Arrange
            const string URL = "http://xx.com";

            var data = new IrcMessageData {Type = ReceiveType.ChannelMessage, Message = URL};

            _urlRepository.Stub(repository => repository.UrlExists(URL))
                .Return(true);

            // Act
            _module.Process(data);

            // Assert
            _urlRepository.AssertWasNotCalled(repository => repository.SaveUrl(null), opt => opt.IgnoreArguments());
            _client.AssertWasNotCalled(client => client.SendMessage(SendType.Notice, null, null), opt => opt.IgnoreArguments());
        }
        public void Can_Shorten_Url()
        {
            // Arrange
            const string URL = "www.google.com";
            const string SHORTENED_URL = "a";

            var data = new IrcMessageData { Message = "!tinyurl " + URL, Channel = "#test", Type = ReceiveType.ChannelMessage };

            _tinyUrlService.Stub(service => service.ShortenUrl(URL))
                .Return(SHORTENED_URL);

            // Act
            _module.Process(data);

            // Assert
            _client.AssertWasCalled(client => client.SendMessage(SendType.Message, data.Channel, "TinyUrl: " + SHORTENED_URL));
        }
        protected void AddLink(IrcMessageData data, string link)
        {
            bool nsfw = Regex.IsMatch(data.Message, "nsfw", RegexOptions.IgnoreCase);

            UnitOfWork.Start(() =>
                                 {
                                     if (_urlRepository.UrlExists(link))
                                         return;

                                     _urlRepository.SaveUrl(new Url
                                                                {
                                                                    Link = link,
                                                                    Timestamp = SystemTime.Now(),
                                                                    Nsfw = nsfw
                                                                });
                                 });
        }
        public void Can_Add_Links_From_Message()
        {
            // Arrange
            const string URL = "http://bob.com";

            var data = new IrcMessageData { Type = ReceiveType.ChannelMessage, Message = "cool site " + URL, Channel = "#test" };

            SystemTime.Now = () => new DateTime(2009, 1, 1);

            // Act
            _module.Process(data);

            // Assert
            _urlRepository.AssertWasCalled(repository => repository.SaveUrl(Arg<Url>.Matches(x =>
                                                                                             x.Link == URL
                                                                                             && x.Timestamp == SystemTime.Now())));
        }
        public void Can_Handle_Multiple_Links()
        {
            // Arrange
            const string URL1 = "http://bob.com";
            const string URL2 = "http://foo.com";

            var data = new IrcMessageData { Type = ReceiveType.ChannelMessage, Message = "bleh " + URL1 + " awesome pic " + URL2, Channel = "#test" };

            SystemTime.Now = () => new DateTime(2009, 1, 1);

            // Act
            _module.Process(data);

            // Assert
            _urlRepository.AssertWasCalled(repository => repository.SaveUrl(null), opt =>
                                                                                       {
                                                                                           opt.IgnoreArguments();
                                                                                           opt.Repeat.Times(2);
                                                                                       });
        }
        public void Can_Add_Quote()
        {
            // Arrange
            const string QUOTE = "testing 123";

            var data = new IrcMessageData() { Message = "!quotes add " + QUOTE, Type = ReceiveType.ChannelMessage };

            SystemTime.Now = () => new DateTime(2009, 1, 1);

            // Act
            _module.Process(data);

            // Assert
            _client.AssertWasCalled(client => client.SendMessage(SendType.Message, data.Channel, "Quote has been added."));
            _repository.AssertWasCalled(
                repository => repository.CreateQuote(Arg<Quote>.Matches(x =>
                                                                          x.Text == QUOTE
                                                                          &&
                                                                          x.Timestamp == SystemTime.Now())));
        }
Exemple #15
0
        // TODO: No error checking at the moment, needs work to stop people doing silly things.
        private void ExecuteHandlerMethod(IrcMessageData data, ModuleMessageHandler handler, Match match)
        {
            var method = GetType().GetMethod(handler.Method, BindingFlags.NonPublic | BindingFlags.Instance);

            if(method == null)
                return;

            var arguments = method.GetParameters()
                .Select(parameter =>
                            {
                                if (parameter.Position == 0)
                                    return data;

                                if (match.Groups[parameter.Name] == null)
                                    return Activator.CreateInstance(parameter.ParameterType);

                                return TypeDescriptor.GetConverter(parameter.ParameterType)
                                    .ConvertFromString(match.Groups[parameter.Name].Value);
                            })
                .ToArray();

            method.Invoke(this, arguments);
        }
        public void Can_Find_Quotes()
        {
            // Arrange
            const string term = "abc";

            var comments = new List<Quote>
                               {
                                   new Quote {Text = "testing1"},
                                   new Quote {Text = "testing2"},
                                   new Quote {Text = "testing3"}
                               };

            var data = new IrcMessageData { Message = "!quotes find " + term, Type = ReceiveType.ChannelMessage };

            _repository.Stub(repository => repository.SearchQuotes(term, 3))
                .Return(comments);

            // Act
            _module.Process(data);

            // Assert
            foreach(var comment in comments)
                _client.AssertWasCalled(client => client.SendMessage(SendType.Message, data.Channel, string.Format("{0} on {1}", comment.Text, comment.Timestamp)));
        }
        public void User_Can_Get_List_Of_Links()
        {
            // Arrange
            var data = new IrcMessageData { Type = ReceiveType.ChannelMessage, Message = "!links", Nickname = "bob"};

            var links = new List<Url>
                            {
                                new Url {Link = "a"},
                                new Url {Link = "b", Nsfw = true}
                            };

            _urlRepository.Stub(repository => repository.GetAllUrls())
                .Return(links);

            // Act
            _module.Process(data);

            // Assert
            _client.AssertWasCalled(client => client.SendMessage(SendType.Message, data.Nickname, links[0].SafeLink));
            _client.AssertWasCalled(client => client.SendMessage(SendType.Message, data.Nickname, links[1].SafeLink));
        }
        protected void ShortenUrl(IrcMessageData data, string url)
        {
            string shortenedUrl = _tinyUrlService.ShortenUrl(url);

            Client.SendMessage(SendType.Message, data.Channel, "TinyUrl: " + shortenedUrl);
        }
Exemple #19
0
        protected void DisplayDate(IrcMessageData data)
        {
            string date = SystemTime.Now().ToString("yyyy-MM-dd hh:mm:sstt");

            Client.SendMessage(SendType.Message, data.Channel, date);
        }
        public void BotRunner_Does_Not_Call_Process_On_Modules_If_Line_Is_From_Myself()
        {
            // Arrange
            var data = new IrcMessageData();

            _client.Stub(client => client.MessageParser("message"))
                .Return(data);

            _client.Stub(client => client.IsMe(data.Nickname))
                .Return(true);

            // Act
            _runner.Start();
            _client.OnReadLine("message");

            // Assert
            _module1.AssertWasNotCalled(module => module.Process(data));
            _module2.AssertWasNotCalled(module => module.Process(data));
        }