Exemple #1
0
        ///////////////////////////////////////////////////////////////////////////////
        public void SetPresence(XmppPresenceStatus newStatus, string customMessage, string photoHash)
        {
            if (newStatus == XmppPresenceStatus.Unavailable)
            {
                sendStream("<presence type=\"unavailable\"></presence>");
            }
            else
            {
                StringBuilder stream = new StringBuilder();
                if (!string.IsNullOrEmpty(customMessage))
                {
                    customMessage = string.Format("<status>{0}</status>", customMessage);
                }

                stream.Append("<presence><priority>1</priority>");
                stream.Append(Caps.ToStream());
                stream.Append(customMessage);
                stream.AppendFormat("<show>{0}</show>", XmppIq.PresenceToStr(newStatus));
                stream.AppendFormat("<x xmlns=\"vcard-temp:x:update\">");
                if (!string.IsNullOrWhiteSpace(photoHash))
                {
                    stream.AppendFormat("<photo>{0}</photo>", photoHash);
                }
                stream.AppendFormat("</x>");

                stream.Append("</presence>");
                sendStream(stream.ToString());
            }

            statePresence = newStatus;
        }
Exemple #2
0
        private void Signal(XmppIQ xStream)
        {
            if (IqSignal != null)
            {
                string typeStr = xStream.GetAttribute("type");
                string idStr   = xStream.GetAttribute("id");

                int id = -1;
                if (Int32.TryParse(idStr, out id) && id >= 0)
                {
                    IqSignal(this, new XmppIqSignalEventArgs(id, XmppIq.StrToType(typeStr), xStream));
                }
            }
        }
Exemple #3
0
        private void doParseStream(string stream)
        {
            XmppIQ xStream;

            try {
                xStream = XmppIQ.Parse(stream);
            }       catch (Exception) {
                //MessageBox.Show("Invalid stream :(\n\n" + stream);
                return;
            }

            switch (xStream.Name)
            {
                #region PRESENCE
            case "presence": {
                XmppPresence presence = new XmppPresence();
                string       type     = xStream.GetAttribute("type");
                presence.From = new XmppJid(xStream.GetAttribute("from"));
                presence.To   = new XmppJid(xStream.GetAttribute("to"));

                // # type == ""
                if (string.IsNullOrEmpty(type))
                {
                    XmppIQ xStatus = xStream.FindDescendant("status");
                    if (xStatus != null)
                    {
                        presence.MessageStatus = xStatus.Text;
                    }

                    XmppIQ xShow = xStream.FindDescendant("show");
                    presence.Status = xShow != null?XmppIq.StrToPresence(xShow.Text) : XmppPresenceStatus.Online;

                    XmppIQ xCard = xStream.FindDescendant("x", "vcard-temp:x:update");
                    if (xCard != null)
                    {
                        XmppIQ xPhoto = xCard.FindDescendant("photo", "vcard-temp:x:update");
                        if (xPhoto != null)
                        {
                            presence.PhotoHash = xPhoto.Text;
                        }
                    }

                    if (Presence != null)
                    {
                        Presence(this, new XmppPresenceEventArgs(presence));
                    }
                }
                // # type == "unavailable"
                else if (type == "unavailable")
                {
                    presence.Status = XmppPresenceStatus.Unavailable;
                    if (Presence != null)
                    {
                        Presence(this, new XmppPresenceEventArgs(presence));
                    }
                }
                // # type == "probe"	// probe request from server
                else if (type == "probe")
                {
                    SetPresence(statePresence, string.Empty, null);
                }
                // # type == "subscribe"	// new subscription request
                else if (type == "subscribe")
                {
                    if (SubscribeRequest != null)
                    {
                        SubscribeRequest(this, new XmppSubscribeRequestEventArgs(presence.From));
                    }
                }
                // # type == "error"	// presence stanza error
                else if (type == "error")
                {
                    // @@@
                }
            }
            break;

                #endregion
                #region MESSAGE
            case "message": {
                XmppMessage message = new XmppMessage();
                message.Type = xStream.GetAttribute("type");
                message.From = new XmppJid(xStream.GetAttribute("from"));
                message.To   = new XmppJid(xStream.GetAttribute("to"));
                Int32.TryParse(xStream.GetAttribute("id"), out message.ID);

                if (message.Type != "error")
                {
                    // # user composing new message
                    if (xStream.FindDescendant("composing", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.NowComposing));
                        }
                    }
                    // # user stop composing
                    else if (xStream.FindDescendant("paused", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.StopComposing));
                        }
                    }
                    // # user is inactive
                    else if (xStream.FindDescendant("inactive", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.Inactive));
                        }
                    }
                    // # user has left conversation
                    else if (xStream.FindDescendant("gone", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.Gone));
                        }
                    }
                    // # user is active ;)
                    else if (xStream.FindDescendant("active", "http://jabber.org/protocol/chatstates") != null)
                    {
                        if (ChatNotify != null)
                        {
                            ChatNotify(this, new XmppChatNotifyEventArgs(message.From, string.Empty, XmppChatStatus.Active));
                        }
                    }

                    // # check for new message
                    XmppIQ xBody = xStream.FindDescendant("body");
                    if (xBody != null)
                    {
                        message.Body = xBody.Text;

                        XmppIQ xThread = xStream.FindDescendant("thread");
                        if (xThread != null)
                        {
                            message.Thread = xThread.Text;
                        }

                        XmppIQ xSubject = xStream.FindDescendant("subject");
                        if (xSubject != null)
                        {
                            message.Subject = xSubject.Text;
                        }

                        if (Message != null)
                        {
                            Message(this, new XmppMessageEventArgs(message));
                        }
                    }
                }
            }
            break;

                #endregion
                #region IQ
            case "iq": {
                XmppIQ xQuery = null;
                string type   = xStream.GetAttribute("type");

                // Roster
                xQuery = xStream.FindDescendant("query", "jabber:iq:roster");
                if (xQuery != null)
                {
                    List <XmppRosterItem> roster = new List <XmppRosterItem>();
                    foreach (XmppIQ xItem in xQuery.Children())
                    {
                        if (xItem.Name != "item")
                        {
                            continue;
                        }

                        string jid   = xItem.GetAttribute("jid");
                        string sub   = xItem.GetAttribute("subscription");
                        string ask   = xItem.GetAttribute("ask");
                        string name  = xItem.GetAttribute("name");
                        string group = xItem.GetAttribute("group");
                        roster.Add(new XmppRosterItem(jid, sub, name, group, ask));
                    }

                    if (Roster != null)
                    {
                        Roster(this, new XmppRosterEventArgs(roster, XmppIq.StrToType(type)));
                    }
                }

                // Disco
                xQuery = xStream.FindDescendant("query", "http://jabber.org/protocol/disco#info");
                if (xQuery != null)
                {
                    if (type == "get")
                    {
                        XmppJid from = new XmppJid(xStream.GetAttribute("from"));
                        string  id   = xStream.GetAttribute("id");
                        sendStream(new XmppIq().ToStream(Jid, from, XmppIqType.Result, id, Disco.ToStreamResult()));                                // @@@@@ ottimizza SendIq(...)
                        //SendIq(from, XmppIqType.Result, Disco.ToStreamResult());// @@@@@ ottimizza SendIq(...)
                    }
                }

                // vCard
                xQuery = xStream.FindDescendant("vCard", "vcard-temp");
                if (xQuery != null)
                {
                    if (type == "result")
                    {
                        XmppJid   from  = new XmppJid(xStream.GetAttribute("from"));
                        XmppVCard vCard = new XmppVCard(from, xQuery);
                        if (VCard != null)
                        {
                            VCard(this, new XmppVCartEventArgs(vCard));
                        }
                    }
                }

                // Ping
                xQuery = xStream.FindDescendant("ping", "urn:xmpp:ping");
                if (xQuery != null)
                {
                    if (type == "get")
                    {
                        XmppJid from = new XmppJid(xStream.GetAttribute("from"));
                        string  id   = xStream.GetAttribute("id");
                        sendStream(new XmppIq().ToStream(Jid, from, XmppIqType.Result, id, string.Empty));                                // @@@@@ ottimizza SendIq(...)
                    }
                }
            }

            break;
                #endregion
            }

            Signal(xStream);
        }