Exemplo n.º 1
0
        public void TestAddNickname()
        {
            var message = new Message();
            var room = new Room();

            message.Body = "nick add bob";

            handler.Receive(message, room);

            Mock<INicknameService>()
                .Verify(call => call.Add("bob"));
        }
Exemplo n.º 2
0
        public void TestAddNickname()
        {
            var message = new Message();
            var room = new Room();

            message.Body = "nick remove tracy";

            handler.Receive(message, room);

            Mock<INicknameService>()
                .Verify(call => call.Remove("tracy"));
        }
Exemplo n.º 3
0
        public void TestHandleMessageWhenNotAddressedToBot()
        {
            var room = new Room();
            var message = new Message { Body = "two" };

            Mock<INicknameService>()
                .Setup(call => call.IsAddressedToMe(message))
                .Returns(false);

            service.Handle(message, room);

            Assert.IsFalse(((FakeHandler)service.Handlers[0]).Fired);
            Assert.IsFalse(((FakeHandler)service.Handlers[1]).Fired);
            Assert.IsFalse(((FakeHandler)service.Handlers[2]).Fired);
        }
Exemplo n.º 4
0
        public void TestHandleMessageWhenAliasedButBypassed()
        {
            var room = new Room();
            var message = new Message { Body = "!two" };

            Mock<INicknameService>()
                .Setup(call => call.IsAddressedToMe(message))
                .Returns(true);

            Mock<IAliasService>()
                .Setup(call => call.IsAlias("!two"))
                .Returns(false);

            service.Handle(message, room);

            Assert.IsFalse(((FakeHandler)service.Handlers[0]).Fired);
            Assert.IsTrue(((FakeHandler)service.Handlers[1]).Fired);
            Assert.IsFalse(((FakeHandler)service.Handlers[2]).Fired);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handles the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="room"></param>
        public void Handle(Message message, Room room)
        {
            // Check bot can handle this message
            if (!CanHandle(message, room))
            {
                return;
            }

            // Check for aliases
            if (AliasService.IsAlias(message.Body))
            {
                message.Body = AliasService.GetAlias(message.Body);
            }

            // Remove Alias bypass
            if (message.Body.StartsWith("!"))
            {
                message.Body = message.Body.Substring(1);
            }

            var handled = false;

            // Check each handler
            foreach (var handler in Handlers)
            {
                if (!handler.CanHandle(message)) continue;

                handler.Receive(message, room);

                handled = true;

                break;
            }

            if (!handled)
            {
                HipChatService.Say(room, "I didn't understand: {0}", message.Body);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets all the rooms.
        /// </summary>
        /// <returns></returns>
        public IList<Room> List()
        {
            var rooms = new List<Room>();

            var credentials = CredentialService.GetCredentials();

            var url = "https://api.hipchat.com/v1/rooms/list?format=xml&auth_token=" + credentials.ApiToken;

            var response = HttpService.Get(url);

            if (response.Success)
            {
                var xml = response.ToXml();

                foreach (var node in xml.GetMatches("//room"))
                {
                    var room = new Room();

                    room.Id = node.GetInnerXml("//room_id").AsInt32();
                    room.Name = node.GetInnerXml("//name");
                    room.Topic = node.GetInnerXml("//topic");
                    room.LastActive = node.GetInnerXml("//last_actice").ToDouble().FromUnixTimestamp();
                    room.Created = node.GetInnerXml("//created").ToDouble().FromUnixTimestamp(); ;
                    room.OwnerUserId = node.GetInnerXml("//owner_user_id").AsInt32();
                    room.IsArchived = node.GetInnerXml("//is_archived") == "1";
                    room.IsPrivate = node.GetInnerXml("//is_private") == "1";
                    room.JabberId = node.GetInnerXml("//xmpp_jid");

                    // Strip domain part
                    room.JabberId = room.JabberId.SubstringBeforeChar("@");

                    rooms.Add(room);
                }
            }

            _roomCache = rooms;
            return rooms;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Determines whether this instance can handle the specified message in the room.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="room">The room.</param>
        /// <returns>
        ///   <c>true</c> if this instance can handle the specified room; otherwise, <c>false</c>.
        /// </returns>
        private bool CanHandle(Message message, Room room)
        {
            var canHandle = message.Body != null;

            // Check message is not one-on-one message
            if (!room.IsChat)
            {
                // Check message is addressed to this bot
                if (!NicknameService.IsAddressedToMe(message))
                {
                    canHandle = false;
                }

                // Strip Bot name from message
                if (message.Body == null)
                    return false;

                var index = message.Body.IndexOf(" ");
                message.Body = message.Body.Substring(index + 1);
            }

            return canHandle;
        }
Exemplo n.º 8
0
 public void Receive(Message message, Room room)
 {
     Fired = true;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Occurs when an incoming message is received
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="msg">The MSG.</param>
        void ConnectionOnMessage(object sender, Msg msg)
        {
            // Prevent loop-back
            if (msg.From.Resource == current.Name) return;

            // Ignore composing messages
            if (msg.Chatstate == Chatstate.composing) return;

            // Construct message
            var message = new Message
            {
                From = msg.From.Resource,
                Room = msg.From.User,
                Body = msg.Body,
                To = msg.To.Resource,
                Received = DateTime.Now
            };

            var room = new Room
            {
                JabberId = msg.From.User,
                IsChat = msg.Type == MessageType.chat
            };

            if (room.IsChat)
            {
                room.JabberId = msg.From.User;
            }

            // Construct args
            var args = new MessageEventArgs
            {
                Message = message,
                Room = room
            };

            // Fire
            OnMessage(this, args);
        }
Exemplo n.º 10
0
        private void Say(Room room, string message, bool html)
        {
            Msg msg;

            if (room.IsChat)
            {
                var to = new Jid(room.JabberId, "chat.hipchat.com", string.Empty);

                msg = new Msg(to, current.JabberId, MessageType.chat, message);
            }
            else
            {
                Out.WriteLine("{0:HH:mm:ss} [{1}] {2}", DateTime.Now, current.Name, message);

                var to = new Jid(room.JabberId, "conf.hipchat.com", string.Empty);

                msg = new Msg(to, current.JabberId, MessageType.groupchat, message);
            }

            if (html)
            {
                msg.Html = new Html();
                msg.Html.Body = new Body();
                msg.Html.Body.Value = message;
            }
            
            Out.WriteLine(msg.ToString());

            connection.Send(msg);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Says the message in the specified room.
        /// </summary>
        /// <param name="room">The room.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The args.</param>
        public void SayHtml(Room room, string message, params object[] args)
        {
            //Say(room, string.Format(message, args), true);

            var credentials = CredentialService.GetCredentials();

            var url = "https://api.hipchat.com/v1/rooms/message?format=xml&auth_token=" + credentials.ApiToken;
            var body = new Dictionary<string, string>();
            body.Add("room_id", room.Id.ToString());
            body.Add("from", current.Name);
            body.Add("message", string.Format(message, args));

            var client = new HttpClient();
            var postTask = client.PostAsync(url, new FormUrlEncodedContent(body));

            postTask.ContinueWith(x=>
            {
                if (x.Result.IsSuccessStatusCode)
                {
                    Out.WriteLine("Html Message Sent");
                }
                else
                {
                    Out.WriteLine("Html Message Failed");
                }
            });
        }
Exemplo n.º 12
0
 /// <summary>
 /// Says the message in the specified room.
 /// </summary>
 /// <param name="room">The room.</param>
 /// <param name="message">The message.</param>
 public void SayHtml(Room room, string message)
 {
     SayHtml(room, message, true);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Says the message in the specified room.
 /// </summary>
 /// <param name="room">The room.</param>
 /// <param name="message">The message.</param>
 /// <param name="args">The args.</param>
 public void Say(Room room, string message, params object[] args)
 {
     Say(room, string.Format(message, args), false);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Says the message in the specified room.
 /// </summary>
 /// <param name="room">The room.</param>
 /// <param name="message">The message.</param>
 public void Say(Room room, string message)
 {
     Say(room, message, false);
 }
Exemplo n.º 15
0
        /// <summary>
        /// Leaves the specified room.
        /// </summary>
        /// <param name="room">The room.</param>
        public bool Leave(Room room)
        {
            var manager = new MucManager(connection);

            var jid = new Jid(room.JabberId, "conf.hipchat.com", string.Empty);

            manager.LeaveRoom(jid, current.Name);

            Out.WriteLine("Left room: {0}.", room.Name);

            return true;
        }
Exemplo n.º 16
-1
        /// <summary>
        /// Joins the specified room.
        /// </summary>
        /// <param name="room">The room.</param>
        /// <returns></returns>
        public bool Join(Room room)
        {
            var manager = new MucManager(connection);

            var jid = new Jid(room.JabberId, "conf.hipchat.com", string.Empty);

            manager.JoinRoom(jid, current.Name);

            Out.WriteLine("{1:HH:mm:ss} Joined room: {0}.", room.Name, DateTime.Now);

            return true;
        }