예제 #1
0
 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
 public void Test_Create()
 {
     Message msg = new Message(doc);
     msg.Html = "foo";
     Assert.AreEqual("<message id=\"" + msg.ID + "\"><html xmlns=\"http://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.jabber.org\">o</a>o";
     Assert.AreEqual("<message id=\"" + msg.ID + "\"><html xmlns=\"http://jabber.org/protocol/xhtml-im\"><body xmlns=\"http://www.w3.org/1999/xhtml\">f<a href=\"http://www.jabber.org\">o</a>o</body></html><body>foo</body></message>", msg.ToString());
     Assert.AreEqual("f<a href=\"http://www.jabber.org\">o</a>o", msg.Html);
 }
예제 #3
0
        /// <summary>
        /// Create an x:data form from the given message stanza.
        /// </summary>
        /// <param name="parent">Original stanza</param>
        public XDataForm(Jabber.Protocol.Client.Message parent)
            : this(FindData(parent) as Jabber.Protocol.X.Data)
        {
            m_stanza = (Packet)parent.CloneNode(true);
            Data d = FindData(m_stanza);

            Debug.Assert(d != null);
            m_parent = (Element)d.ParentNode;
            m_parent.RemoveChild(d);
        }
예제 #4
0
 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"));
 }
예제 #5
0
 private void jc_OnMessage(object sender, Jabber.Protocol.Client.Message msg)
 {
     Jabber.Protocol.X.Data x = msg["x", URI.XDATA] as Jabber.Protocol.X.Data;
     if (x != null)
     {
         Muzzle.Forms.XDataForm f = new Muzzle.Forms.XDataForm(msg);
         f.ShowDialog(this);
         jc.Write(f.GetResponse());
     }
     else
     {
         MessageBox.Show(this, msg.Body, msg.From, MessageBoxButtons.OK);
     }
 }
예제 #6
0
 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");
     }
 }
예제 #7
0
        /// <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);
        }
예제 #8
0
        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;
                    }

                    Presence oldPresence = (m_participants[from] != null) ? ((RoomParticipant)m_participants[from]).Presence : null;

                    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);
                                else
                                    OnPresenceChange(this, party, oldPresence);
                                break;
                        }
                    }
                    else
                    {
                        switch (mod)
                        {
                            case ParticipantCollection.Modification.NONE:
                                if (OnParticipantPresenceChange != null)
                                    OnParticipantPresenceChange(this, party, oldPresence);
                                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":
                    if (OnIQ != null)
                        OnIQ(this, (IQ)rp);
                    break;
            }
        }
예제 #9
0
        /// <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);
        }
예제 #10
0
        /// <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);
        }
예제 #11
0
        private void muc_OnInvite(object sender, Jabber.Protocol.Client.Message msg)
        {
            Room r = sender as Room;

            r.Join();
        }
예제 #12
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.");
     }
 }
예제 #13
0
        static void Main(string[] args)
        {
            //newThread = new Thread(new ThreadStart(() =>
            //{
            jc = new JabberClient();
            jc.OnReadText += new Bedrock.TextHandler((sender, text) => Console.WriteLine(text));
            jc.OnWriteText += new Bedrock.TextHandler((sender, text) => Console.WriteLine(text));
            jc.OnError += new Bedrock.ExceptionHandler((sender, ex) => Console.WriteLine(ex.Message));
            jc.OnStreamError += new Jabber.Protocol.ProtocolHandler((sender, ex) => Console.WriteLine(ex.ToString()));
            jc.AutoReconnect = 3f;
            jc.OnInvalidCertificate += jc_OnInvalidCertificate;
            jc.User = Console.ReadLine();
            jc.Server = "jabber.devsmm.com";
            jc.NetworkHost = "192.168.1.46";
            jc.Port = 5222;
            jc.Resource = "Jabber.Net Console Client";
            jc.Password = "******";
            jc.AutoStartTLS = true;
            jc.AutoPresence = true;
            jc.Connection = ConnectionType.Socket;
            jc["USE_WINDOWS_CREDS"] = false;
            jc["plaintext"] = false;
            jc["to"] = "jabber.devsmm.com";
            jc["network_host"] = "192.168.1.46"; //"jabber.devsmm.com";
            jc["ssl"] = false;
            jc["port"] = 5222;
            jc["poll.url"] = "";
            jc.OnMessage += jc_OnMessage;
            jc.OnAuthError += jc_OnAuthError;
            jc.OnAuthenticate += jc_OnAuthenticate;
            jc.OnConnect += jc_OnConnect;

            Console.WriteLine("Connecting");
            jc.Connect();
            Console.WriteLine("Connected");
            Console.ReadLine();
            Console.WriteLine("Subject:");
            var subject = Console.ReadLine();
            Console.WriteLine("Subject:" + subject);
            Console.WriteLine("Body:");
            var body = Console.ReadLine();
            Message msg = new Message(jc.Document);
            msg.To = "admin@" + jc.Server;
            //msg.From = new JID("kill@" + jc.Server);
            msg.Subject = subject;
            msg.Body = body;
            jc.Write(msg);

            Console.WriteLine("sent");
            Console.WriteLine("Closing");
            //}));
            //newThread.Start();

            //handle = new ManualResetEvent(false);
            //handle.WaitOne(Timeout.Infinite);
            Console.WriteLine("Closing");
            Console.ReadLine();
        }
예제 #14
0
 static void jc_OnMessage(object sender, Message msg)
 {
     Console.WriteLine(msg.From);
     Console.WriteLine("Subject:" + msg.Subject);
     Console.WriteLine("Body:" + msg.Body);
 }