示例#1
0
 [Test] public void Test_NullBody()
 {
     Message msg = new Message(doc);
     Assert.AreEqual(null, msg.Body);
     msg.Body = "foo";
     Assert.AreEqual("foo", msg.Body);
     msg.Body = null;
     Assert.AreEqual(null, msg.Body);
 }
示例#2
0
 [Test] public void Test_Create()
 {
     Message msg = new Message(doc);
     msg.Html = "foo";
     Assert.AreEqual("<message id=\""+msg.ID+"\"><html xmlns=\"http://kixeye.jabber.org/protocol/xhtml-im\"><body xmlns=\"http://www.w3.org/1999/xhtml\">foo</body></html><body>foo</body></message>", msg.ToString());
     // TODO: deal with the namespace problem here
     msg.Html = "f<a href=\"http://www.kixeye.jabber.org\">o</a>o";
     Assert.AreEqual("<message id=\""+msg.ID+"\"><html xmlns=\"http://kixeye.jabber.org/protocol/xhtml-im\"><body xmlns=\"http://www.w3.org/1999/xhtml\">f<a href=\"http://www.kixeye.jabber.org\">o</a>o</body></html><body>foo</body></message>", msg.ToString());
     Assert.AreEqual("f<a href=\"http://www.kixeye.jabber.org\">o</a>o", msg.Html);
 }
示例#3
0
 [Test] public void Test_Normal()
 {
     Message msg = new Message(doc);
     Assert.AreEqual(MessageType.normal, msg.Type);
     Assert.AreEqual("", msg.GetAttribute("type"));
     msg.Type = MessageType.chat;
     Assert.AreEqual(MessageType.chat, msg.Type);
     Assert.AreEqual("chat", msg.GetAttribute("type"));
     msg.Type = MessageType.normal;
     Assert.AreEqual(MessageType.normal, msg.Type);
     Assert.AreEqual("", msg.GetAttribute("type"));
 }
示例#4
0
 [Test] public void Test_Escape()
 {
     Message msg = new Message(doc);
     msg.Body = "&";
     Assert.AreEqual("<message id=\""+msg.ID+"\"><body>&amp;</body></message>", msg.ToString());
     msg.RemoveChild(msg["body"]);
     Assert.AreEqual("<message id=\""+msg.ID+"\"></message>", msg.ToString());
     try
     {
         msg.Html = "&";
         Assert.Fail("should have thrown an exception");
     }
     catch
     {
         Assert.IsTrue(true, "Threw exception, as expected");
     }
 }
        /// <summary>
        /// Invite a user to join the room.
        /// </summary>
        /// <param name="invitee">The JID of the person to invite</param>
        /// <param name="reason">The reason for the invite, or null for none.</param>
        public void Invite(JID invitee, string reason)
        {
            if (m_state != STATE.running)
                throw new InvalidOperationException("Must be in running state to send invite: " + m_state.ToString());

            if (invitee == null)
                throw new ArgumentNullException("invitee");
/*
<message
    from='[email protected]/desktop'
    to='*****@*****.**'>
  <x xmlns='http://jabber.org/protocol/muc#user'>
    <invite to='*****@*****.**'>
      <reason>
        Hey Hecate, this is the place for all good witches!
      </reason>
    </invite>
  </x>
</message>
 */
            Message m = new Message(m_manager.Stream.Document);
            m.To = m_room;
            UserX x = new UserX(m_manager.Stream.Document);
            x.AddInvite(invitee, reason);
            m.AddChild(x);
            m_manager.Write(m);
        }
        /// <summary>
        /// Sends a private message to a single user in the room.
        /// </summary>
        /// <param name="nick">The nickname of the user to send a private message to.</param>
        /// <param name="body">The message body to send.</param>
        public void PrivateMessage(string nick, string body)
        {
            if (m_state != STATE.running)
                throw new InvalidOperationException("Must be in running state to send message: " + m_state.ToString());

/*
<message
    to='[email protected]/firstwitch'
    type='chat'>
  <body>I'll give thee a wind.</body>
</message>
 */
            if (nick == null)
                throw new ArgumentNullException("nick");
            if (body == null)
                throw new ArgumentNullException("body");

            Message m = new Message(m_manager.Stream.Document);
            m.To = new JID(m_room.User, m_room.Server, nick);
            m.Type = MessageType.chat;
            m.Body = body;
            m_manager.Write(m);
        }
        /// <summary>
        /// Sends a message to everyone currently in the room.
        /// </summary>
        /// <param name="body">The message text to send.</param>
        public void PublicMessage(string body)
        {
            if (m_state != STATE.running)
                throw new InvalidOperationException("Must be in running state to send message: " + m_state.ToString());
/*
<message
    to='*****@*****.**'
    type='groupchat'>
  <body>Harpier cries: 'tis time, 'tis time.</body>
</message>
 */
            if (body == null)
                throw new ArgumentNullException("body");
            Message m = new Message(m_manager.Stream.Document);
            m.To = m_room;
            m.Type = MessageType.groupchat;
            m.Body = body;
            m_manager.Write(m);
        }
        private void m_stream_OnProtocol(object sender, System.Xml.XmlElement rp)
        {
            // There isn't always a from address.  iq:roster, for example.
            string af = rp.GetAttribute("from");
            if (af == "")
                return;
            JID from = new JID(af);
            if (from.Bare != (string)m_room)
                return;  // not for this room.

            switch (rp.LocalName)
            {
            case "presence":
                Presence p = (Presence)rp;
                if (p.Error != null)
                {
                    m_state = STATE.error;
                    if (OnPresenceError != null)
                        OnPresenceError(this, p);
                    return;
                }

                ParticipantCollection.Modification mod = ParticipantCollection.Modification.NONE;
                RoomParticipant party = m_participants.Modify(p, out mod);

                // if this is ours
                if (p.From == m_jid)
                {
                    switch (m_state)
                    {
                    case STATE.join:
                        OnJoinPresence(p);
                        break;
                    case STATE.leaving:
                        OnLeavePresence(p);
                        break;
                    case STATE.running:
                        if (p.Type == PresenceType.unavailable)
                            OnLeavePresence(p);
                        break;
                    }
                }
                else
                {
                    switch (mod)
                    {
                    case ParticipantCollection.Modification.NONE:
                        if (OnParticipantPresenceChange != null)
                            OnParticipantPresenceChange(this, party);
                        break;
                    case ParticipantCollection.Modification.JOIN:
                        if (OnParticipantJoin != null)
                            OnParticipantJoin(this, party);
                        break;
                    case ParticipantCollection.Modification.LEAVE:
                        if (OnParticipantLeave != null)
                            OnParticipantLeave(this, party);
                        break;
                    }
                }
                break;
            case "message":
                Message m = (Message)rp;
                if (m.Type == MessageType.groupchat)
                {
                    if (m.Subject != null)
                    {
                        if (OnSubjectChange != null)
                            OnSubjectChange(this, m);
                        m_subject = m;
                    }
                    else if (m.From == m_jid)
                    {
                        if (OnSelfMessage != null)
                            OnSelfMessage(this, m);
                    }
                    else
                    {
                        if (OnRoomMessage != null)
                            OnRoomMessage(this, m);
                    }
                }
                else
                {
                    if (m.From.Resource == null)
                    {
                        // room notification of some kind
                        if (OnAdminMessage != null)
                            OnAdminMessage(this, m);
                    }
                    else
                    {
                        if (OnPrivateMessage != null)
                            OnPrivateMessage(this, m);
                    }
                }
                break;
            case "iq":
                // TODO: IQs the room sends to us.
                break;
            }
        }
示例#9
0
 /// <summary>
 /// Sends a certain type of message packet to another user.
 /// </summary>
 /// <param name="t">The type of message.</param>
 /// <param name="to">The JID to send the message to.</param>
 /// <param name="body">The body of the message.</param>
 public void Message(MessageType t,
     string to,
     string body)
 {
     if (IsAuthenticated)
     {
         Message msg = new Message(Document);
         msg.Type = t;
         msg.To = to;
         msg.Body = body;
         Write(msg);
     }
     else
     {
         throw new InvalidOperationException("Client must be authenticated before sending messages.");
     }
 }