public bool Input(Im.Presence stanza)
        {
            if (MucError.IsError(stanza))
            {
                // Unable to join - No nickname specified / Duplicate nickname exists ... etc
                var error = new MucError(stanza);
                MucErrorResponse?.Raise(this, new GroupErrorEventArgs(error));
                return(true);
            }

            // Things that could happen here:
            // Service Sends Notice of Membership
            // Service Passes Along Changed Presence
            // Service Updates Nick
            XmlElement xElement = stanza.Data ["x"];

            if (xElement != null && xElement.NamespaceURI == MucNs.NsUser)
            {
                Occupant person = null;
                foreach (XmlElement item in xElement.GetElementsByTagName("item"))
                {
                    // There is only ever one item in a message here but,
                    // I don't have a better way of getting the first element as an element, not a node.
                    var itemJid         = item.GetAttribute("jid");
                    var itemAffiliation = item.GetAttribute("affiliation");
                    var itemRole        = item.GetAttribute("role");
                    if (!String.IsNullOrWhiteSpace(itemJid) && !String.IsNullOrWhiteSpace(itemAffiliation) &&
                        !String.IsNullOrWhiteSpace(itemRole))
                    {
                        person = new Occupant(
                            item.GetAttribute("jid"),
                            item.GetAttribute("affiliation"),
                            item.GetAttribute("role"));
                    }
                }


                IList <MucStatusType> statusCodeList = new List <MucStatusType> ();
                foreach (XmlElement item in xElement.GetElementsByTagName("status"))
                {
                    string codeAttribute = item.GetAttribute("code");
                    if (!string.IsNullOrWhiteSpace(codeAttribute))
                    {
                        var code = (MucStatusType)Enum.Parse(typeof(MucStatusType), codeAttribute);
                        statusCodeList.Add(code);
                    }
                }


                if (person != null)
                {
                    PrescenceChanged.Raise(this, new GroupPresenceEventArgs(person, statusCodeList));
                    return(true);
                }
            }

            // Any message with an Availability status can be managed by the Presence extension
            return(false);
        }
Exemple #2
0
 /// <summary>
 /// Constructs a GroupErrorEventArgs.
 /// </summary>
 /// <param name="data">Group Chat Error.</param>
 internal GroupErrorEventArgs(MucError data)
 {
     data.ThrowIfNull("invite");
     Data = data;
 }
        public bool Input(Im.Message stanza)
        {
            if (MucError.IsError(stanza))
            {
                // Unable to send a message... many reasons
                var error = new MucError(stanza);
                MucErrorResponse?.Raise(this, new GroupErrorEventArgs(error));
                return(true);
            }

            if (Invite.IsElement(stanza))
            {
                // Incoming chat room invite
                var invite = new Invite(stanza);
                InviteReceived.Raise(this, new GroupInviteEventArgs(invite));
                return(true);
            }

            if (InviteDeclined.IsElement(stanza))
            {
                // Chat room invite was declined
                var invite = new InviteDeclined(stanza);
                InviteWasDeclined.Raise(this, new GroupInviteDeclinedEventArgs(invite));
                return(true);
            }

            if (stanza.Subject != null)
            {
                // Subject change
                SubjectChanged.Raise(this, new Im.MessageEventArgs(stanza.From, stanza));
                return(true);
            }

            // Things that could happen here:
            // Receive Registration Request
            // Receive Voice Request
            XmlElement xElement = stanza.Data["x"];

            if (xElement != null && xElement.NamespaceURI == MucNs.NsXData)
            {
                switch (xElement.FirstChild.Value)
                {
                default:
                    break;

                case MucNs.NsRequest:
                    // Invoke Voice Request Submission callback/event.
                    // 8.6 Approving Voice Requests
                    if (VoiceRequested != null)
                    {
                        SubmitForm form    = VoiceRequested.Invoke(new RequestForm(xElement));
                        var        message = new Core.Message(stanza.From, im.Jid, form.ToXmlElement());
                        SendMessage(message);
                        return(true);
                    }
                    break;

                case MucNs.NsRegister:
                    // Invoke Registration Request Submission callback/event.
                    // 9.9 Approving Registration Requests
                    // I'm unsure on how to implement this.
                    // return true;
                    break;
                }
            }

            // Any message with a body can be managed by the IM extension
            // Such as Group Chat Message & Group Chat History
            return(false);
        }