Ejemplo n.º 1
0
 /// <summary>
 /// Se produit lorsqu'un message est disponible
 /// </summary>
 /// <param name="sender">Objet parent</param>
 /// <param name="message">Message en question</param>
 private void xmppOnMessage(object sender, agsXMPP.protocol.client.Message message)
 {
     if (message.From != null)
     {
         string bare = message.From.Bare;
         if (message.From.Resource != null && message.Nickname != null)
         {
             if (contacts.ContainsKey(bare) && contacts[bare].ContainsKey(message.From.Resource) && message.Nickname.ToString().Trim() != string.Empty)
             {
                 contacts[bare][message.From.Resource].identity.nickname = message.Nickname.ToString().Trim();
             }
         }
         if (message.HasTag("event"))
         {
             agsXMPP.Xml.Dom.Element evt = message.SelectSingleElement("event");
             if (evt.Namespace == "http://jabber.org/protocol/pubsub#event" && evt.HasChildElements)
             {
                 #region PubSub Events
                 // HACK: Bug serveur. Suivant la XEP c'est Items, ejabberd2.0 retourne parfois Item sans S
                 if (evt.HasTag("items") || evt.HasTag("item"))
                 {
                     agsXMPP.Xml.Dom.Element items = evt.SelectSingleElement("items");
                     if (items == null)
                     {
                         items = evt.SelectSingleElement("item");
                     }
                     if (items.HasTag("item"))
                     {
                         agsXMPP.Xml.Dom.Element item = items.SelectSingleElement("item");
                         // TODO: g�rer le User mood dans les messages chat
                         #region User mood
                         if (items.HasAttribute("node") && (items.Attributes["node"] as string) == "http://jabber.org/protocol/mood" && (item.HasTag("mood") || item.HasTag("retract")))
                         {
                             agsXMPP.Xml.Dom.Element mood = item.SelectSingleElement("mood");
                             if (mood != null)
                             {
                                 // HACK: mood.Namespace == "http://jabber.org/protocol/mood"
                                 if (mood.HasChildElements)
                                 {
                                     Enumerations.MoodType moodType = Enumerations.MoodType.none;
                                     foreach (Enumerations.MoodType mt in Enum.GetValues(typeof(Enumerations.MoodType)))
                                     {
                                         string mtn = Enum.GetName(typeof(Enumerations.MoodType), mt);
                                         if (mood.HasTag(mtn))
                                         {
                                             moodType = mt;
                                             break;
                                         }
                                     }
                                     string moodString = string.Empty;
                                     if (mood.HasTag("text") && mood.SelectSingleElement("text").Value != null)
                                     {
                                         moodString = mood.SelectSingleElement("text").Value.Trim();
                                     }
                                     if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                     {
                                         foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                         {
                                             Mood md = new Mood();
                                             md.type = moodType;
                                             md.text = moodString;
                                             r.Value.mood = md;
                                             OnMoodUpdated(r.Value, r.Value.mood);
                                         }
                                     }
                                     else
                                     {
                                         Mood md = new Mood();
                                         md.type = moodType;
                                         md.text = moodString;
                                         UserPEP up = new UserPEP();
                                         up.mood = md;
                                         if (!initialUserPEP.ContainsKey(bare))
                                         {
                                             initialUserPEP.Add(bare, up);
                                         }
                                         else
                                         {
                                             up.tune = initialUserPEP[bare].tune;
                                             up.activity = initialUserPEP[bare].activity;
                                             up.location = initialUserPEP[bare].location;
                                             initialUserPEP[bare] = up;
                                         }
                                     }
                                 }
                             }
                             else if (item.SelectSingleElement("retract") != null)
                             {
                                 if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                 {
                                     foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                     {
                                         Mood md = new Mood();
                                         md.type = Enumerations.MoodType.none;
                                         md.text = string.Empty;
                                         r.Value.mood = md;
                                         OnMoodUpdated(r.Value, r.Value.mood);
                                     }
                                 }
                                 else
                                 {
                                     Mood md = new Mood();
                                     md.type = Enumerations.MoodType.none;
                                     md.text = string.Empty;
                                     UserPEP up = new UserPEP();
                                     up.mood = md;
                                     if (!initialUserPEP.ContainsKey(bare))
                                     {
                                         initialUserPEP.Add(bare, up);
                                     }
                                     else
                                     {
                                         up.tune = initialUserPEP[bare].tune;
                                         up.activity = initialUserPEP[bare].activity;
                                         up.location = initialUserPEP[bare].location;
                                         initialUserPEP[bare] = up;
                                     }
                                 }
                             }
                         }
                         #endregion
                         #region User activity
                         if (items.HasAttribute("node") && (items.Attributes["node"] as string) == "http://jabber.org/protocol/activity" && (item.HasTag("activity") || item.HasTag("retract")))
                         {
                             agsXMPP.Xml.Dom.Element activity = item.SelectSingleElement("activity");
                             if (activity != null)
                             {
                                 // HACK: activity.Namespace == "http://jabber.org/protocol/activity"
                                 if (activity.HasChildElements)
                                 {
                                     Enumerations.ActivityType activityType = Enumerations.ActivityType.none;
                                     List<string> activityTypes = recurseActivityTags(activity, new List<string>());
                                     activityTypes.Remove("activity");
                                     if (activityTypes.Count > 0)
                                     {
                                         object o = Enum.Parse(typeof(Enumerations.ActivityType), activityTypes[0], true);
                                         if (o != null)
                                         {
                                             activityType = (Enumerations.ActivityType)o;
                                         }
                                     }
                                     string activityString = string.Empty;
                                     if (activity.HasTag("text") && activity.SelectSingleElement("text").Value != null)
                                     {
                                         activityString = activity.SelectSingleElement("text").Value.Trim();
                                     }
                                     if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                     {
                                         foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                         {
                                             Activity a = new Activity();
                                             a.type = activityType;
                                             a.text = activityString;
                                             r.Value.activity = a;
                                             OnActivityUpdated(r.Value, r.Value.activity);
                                         }
                                     }
                                     else
                                     {
                                         Activity a = new Activity();
                                         a.type = activityType;
                                         a.text = activityString;
                                         UserPEP up = new UserPEP();
                                         up.activity = a;
                                         if (!initialUserPEP.ContainsKey(bare))
                                         {
                                             initialUserPEP.Add(bare, up);
                                         }
                                         else
                                         {
                                             up.tune = initialUserPEP[bare].tune;
                                             up.mood = initialUserPEP[bare].mood;
                                             up.location = initialUserPEP[bare].location;
                                             initialUserPEP[bare] = up;
                                         }
                                     }
                                 }
                             }
                             else if (item.SelectSingleElement("retract") != null)
                             {
                                 if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                 {
                                     foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                     {
                                         Activity a = new Activity();
                                         a.type = Enumerations.ActivityType.none;
                                         a.text = string.Empty;
                                         r.Value.activity = a;
                                         OnActivityUpdated(r.Value, r.Value.activity);
                                     }
                                 }
                                 else
                                 {
                                     Activity a = new Activity();
                                     a.type = Enumerations.ActivityType.none;
                                     a.text = string.Empty;
                                     UserPEP up = new UserPEP();
                                     up.activity = a;
                                     if (!initialUserPEP.ContainsKey(bare))
                                     {
                                         initialUserPEP.Add(bare, up);
                                     }
                                     else
                                     {
                                         up.tune = initialUserPEP[bare].tune;
                                         up.mood = initialUserPEP[bare].mood;
                                         up.location = initialUserPEP[bare].location;
                                         initialUserPEP[bare] = up;
                                     }
                                 }
                             }
                         }
                         #endregion
                         #region User location
                         if (items.HasAttribute("node") && (items.Attributes["node"] as string) == "http://jabber.org/protocol/geoloc" && (item.HasTag("geoloc") || item.HasTag("retract")))
                         {
                             agsXMPP.Xml.Dom.Element geoloc = item.SelectSingleElement("geoloc");
                             if (geoloc != null)
                             {
                                 // HACK: geoloc.Namespace == "http://jabber.org/protocol/geoloc"
                                 if (geoloc.HasChildElements)
                                 {
                                     Location l = new Location();
                                     l.altitude = (geoloc.HasTag("alt") && geoloc.SelectSingleElement("alt").Value != null) ? Convert.ToDouble(geoloc.SelectSingleElement("alt").Value, System.Globalization.CultureInfo.InvariantCulture) : 0.0d;
                                     l.latitude = (geoloc.HasTag("lat") && geoloc.SelectSingleElement("lat").Value != null) ? Convert.ToDouble(geoloc.SelectSingleElement("lat").Value, System.Globalization.CultureInfo.InvariantCulture) : 0.0d;
                                     l.longitude = (geoloc.HasTag("lon") && geoloc.SelectSingleElement("lon").Value != null) ? Convert.ToDouble(geoloc.SelectSingleElement("lon").Value, System.Globalization.CultureInfo.InvariantCulture) : 0.0d;
                                     l.bearing = (geoloc.HasTag("bearing") && geoloc.SelectSingleElement("bearing").Value != null) ? Convert.ToDouble(geoloc.SelectSingleElement("bearing").Value, System.Globalization.CultureInfo.InvariantCulture) : 0.0d;
                                     l.error = (geoloc.HasTag("error") && geoloc.SelectSingleElement("error").Value != null) ? Convert.ToDouble(geoloc.SelectSingleElement("error").Value, System.Globalization.CultureInfo.InvariantCulture) : 0.0d;
                                     l.speed = (geoloc.HasTag("speed") && geoloc.SelectSingleElement("speed").Value != null) ? Convert.ToDouble(geoloc.SelectSingleElement("speed").Value, System.Globalization.CultureInfo.InvariantCulture) : 0.0d;
                                     l.area = (geoloc.HasTag("area") && geoloc.SelectSingleElement("area").Value != null) ? geoloc.SelectSingleElement("area").Value.Trim() : string.Empty;
                                     l.building = (geoloc.HasTag("building") && geoloc.SelectSingleElement("building").Value != null) ? geoloc.SelectSingleElement("building").Value.Trim() : string.Empty;
                                     l.country = (geoloc.HasTag("country") && geoloc.SelectSingleElement("country").Value != null) ? geoloc.SelectSingleElement("country").Value.Trim() : string.Empty;
                                     l.datum = (geoloc.HasTag("datum") && geoloc.SelectSingleElement("datum").Value != null) ? geoloc.SelectSingleElement("datum").Value.Trim() : string.Empty;
                                     l.description = (geoloc.HasTag("description") && geoloc.SelectSingleElement("description").Value != null) ? geoloc.SelectSingleElement("description").Value.Trim() : string.Empty;
                                     l.floor = (geoloc.HasTag("floor") && geoloc.SelectSingleElement("floor").Value != null) ? geoloc.SelectSingleElement("floor").Value.Trim() : string.Empty;
                                     l.locality = (geoloc.HasTag("locality") && geoloc.SelectSingleElement("locality").Value != null) ? geoloc.SelectSingleElement("locality").Value.Trim() : string.Empty;
                                     l.postalcode = (geoloc.HasTag("postalcode") && geoloc.SelectSingleElement("postalcode").Value != null) ? geoloc.SelectSingleElement("postalcode").Value.Trim() : string.Empty;
                                     l.region = (geoloc.HasTag("region") && geoloc.SelectSingleElement("region").Value != null) ? geoloc.SelectSingleElement("region").Value.Trim() : string.Empty;
                                     l.room = (geoloc.HasTag("room") && geoloc.SelectSingleElement("room").Value != null) ? geoloc.SelectSingleElement("room").Value.Trim() : string.Empty;
                                     l.street = (geoloc.HasTag("street") && geoloc.SelectSingleElement("street").Value != null) ? geoloc.SelectSingleElement("street").Value.Trim() : string.Empty; l.area = (geoloc.HasTag("area") && geoloc.SelectSingleElement("area").Value != null) ? geoloc.SelectSingleElement("area").Value.Trim() : string.Empty;
                                     l.text = (geoloc.HasTag("text") && geoloc.SelectSingleElement("text").Value != null) ? geoloc.SelectSingleElement("text").Value.Trim() : string.Empty;
                                     l.timestamp = (geoloc.HasTag("timestamp") && geoloc.SelectSingleElement("timestamp").Value != null) ? DateTime.Parse(geoloc.SelectSingleElement("timestamp").Value) : new DateTime();
                                     l.uri = (geoloc.HasTag("uri") && geoloc.SelectSingleElement("uri").Value != null) ? geoloc.SelectSingleElement("uri").Value.Trim() : string.Empty;
                                     if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                     {
                                         foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                         {
                                             r.Value.location = l;
                                             OnLocationUpdated(r.Value, r.Value.location);
                                         }
                                     }
                                     else
                                     {
                                         UserPEP up = new UserPEP();
                                         up.location = l;
                                         if (!initialUserPEP.ContainsKey(bare))
                                         {
                                             initialUserPEP.Add(bare, up);
                                         }
                                         else
                                         {
                                             up.tune = initialUserPEP[bare].tune;
                                             up.mood = initialUserPEP[bare].mood;
                                             up.activity = initialUserPEP[bare].activity;
                                             initialUserPEP[bare] = up;
                                         }
                                     }
                                 }
                             }
                             else if (item.SelectSingleElement("retract") != null)
                             {
                                 Location l = new Location();
                                 l.altitude = 0;
                                 l.latitude = 0;
                                 l.longitude = 0;
                                 l.bearing = 0;
                                 l.error = 0;
                                 l.speed = 0;
                                 l.area = string.Empty;
                                 l.building = string.Empty;
                                 l.country = string.Empty;
                                 l.datum = string.Empty;
                                 l.description = string.Empty;
                                 l.floor = string.Empty;
                                 l.locality = string.Empty;
                                 l.postalcode = string.Empty;
                                 l.region = string.Empty;
                                 l.room = string.Empty;
                                 l.street = string.Empty;
                                 l.text = string.Empty;
                                 l.timestamp = new DateTime();
                                 l.uri = string.Empty;
                                 if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                 {
                                     foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                     {
                                         r.Value.location = l;
                                         OnLocationUpdated(r.Value, r.Value.location);
                                     }
                                 }
                                 else
                                 {
                                     UserPEP up = new UserPEP();
                                     up.location = l;
                                     if (!initialUserPEP.ContainsKey(bare))
                                     {
                                         initialUserPEP.Add(bare, up);
                                     }
                                     else
                                     {
                                         up.tune = initialUserPEP[bare].tune;
                                         up.mood = initialUserPEP[bare].mood;
                                         up.activity = initialUserPEP[bare].activity;
                                         initialUserPEP[bare] = up;
                                     }
                                 }
                             }
                         }
                         #endregion
                         #region User tune
                         if (items.HasAttribute("node") && (items.Attributes["node"] as string) == "http://jabber.org/protocol/tune" && (item.HasTag("tune") || item.HasTag("retract")))
                         {
                             agsXMPP.Xml.Dom.Element tune = item.SelectSingleElement("tune");
                             if (tune != null)
                             {
                                 // HACK: tune.Namespace == "http://jabber.org/protocol/tune"
                                 if (tune.HasChildElements)
                                 {
                                     Tune t = new Tune();
                                     t.length = (tune.HasTag("length") && tune.SelectSingleElement("length").Value != null) ? Convert.ToInt32(tune.SelectSingleElement("length").Value, System.Globalization.CultureInfo.InvariantCulture) : 0;
                                     t.rating = (tune.HasTag("rating") && tune.SelectSingleElement("rating").Value != null) ? Convert.ToInt32(tune.SelectSingleElement("rating").Value, System.Globalization.CultureInfo.InvariantCulture) : 0;
                                     t.track = (tune.HasTag("track") && tune.SelectSingleElement("track").Value != null) ? Convert.ToInt32(tune.SelectSingleElement("track").Value, System.Globalization.CultureInfo.InvariantCulture) : 0;
                                     t.artist = (tune.HasTag("artist") && tune.SelectSingleElement("artist").Value != null) ? tune.SelectSingleElement("artist").Value.Trim() : string.Empty;
                                     t.source = (tune.HasTag("source") && tune.SelectSingleElement("source").Value != null) ? tune.SelectSingleElement("source").Value.Trim() : string.Empty;
                                     t.title = (tune.HasTag("title") && tune.SelectSingleElement("title").Value != null) ? tune.SelectSingleElement("title").Value.Trim() : string.Empty;
                                     t.uri = (tune.HasTag("uri") && tune.SelectSingleElement("uri").Value != null) ? tune.SelectSingleElement("uri").Value.Trim() : string.Empty;
                                     if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                     {
                                         foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                         {
                                             r.Value.tune = t;
                                             OnTuneUpdated(r.Value, r.Value.tune);
                                         }
                                     }
                                     else
                                     {
                                         UserPEP up = new UserPEP();
                                         up.tune = t;
                                         if (!initialUserPEP.ContainsKey(bare))
                                         {
                                             initialUserPEP.Add(bare, up);
                                         }
                                         else
                                         {
                                             up.mood = initialUserPEP[bare].mood;
                                             up.activity = initialUserPEP[bare].activity;
                                             up.location = initialUserPEP[bare].location;
                                             initialUserPEP[bare] = up;
                                         }
                                     }
                                 }
                             }
                             else if (item.SelectSingleElement("retract") != null)
                             {
                                 Tune t = new Tune();
                                 t.artist = string.Empty;
                                 t.length = 0;
                                 t.rating = 1;
                                 t.source = string.Empty;
                                 t.title = string.Empty;
                                 t.track = 0;
                                 t.uri = string.Empty;
                                 if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                 {
                                     foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                     {
                                         r.Value.tune = t;
                                         OnTuneUpdated(r.Value, r.Value.tune);
                                     }
                                 }
                                 else
                                 {
                                     UserPEP up = new UserPEP();
                                     up.tune = t;
                                     if (!initialUserPEP.ContainsKey(bare))
                                     {
                                         initialUserPEP.Add(bare, up);
                                     }
                                     else
                                     {
                                         up.mood = initialUserPEP[bare].mood;
                                         up.activity = initialUserPEP[bare].activity;
                                         up.location = initialUserPEP[bare].location;
                                         initialUserPEP[bare] = up;
                                     }
                                 }
                             }
                         }
                         #endregion
                         #region User nickname
                         if (items.HasAttribute("node") && (items.Attributes["node"] as string) == "http://jabber.org/protocol/nick" && item.HasTag("nick"))
                         {
                             agsXMPP.Xml.Dom.Element nick = item.SelectSingleElement("nick");
                             if (nick != null)
                             {
                                 // HACK: nick.Namespace == "http://jabber.org/protocol/nick"
                                 if (nick.HasChildElements)
                                 {
                                     string nm = (nick.Value != null) ? nick.Value.Trim() : string.Empty;
                                     if (nm != string.Empty)
                                     {
                                         if (contacts.ContainsKey(bare) && contacts[bare].Values.Count > 0)
                                         {
                                             foreach (KeyValuePair<string, Contact> r in contacts[bare])
                                             {
                                                 r.Value.identity.nickname = nm;
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         #endregion
                     }
                 }
                 #endregion
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Change la Presence d'une resource
        /// </summary>
        /// <param name="presence">Contenu de la pr�sence</param>
        private void SetPresence(agsXMPP.protocol.client.Presence presence)
        {
            if (presence.From != null && presence.To.Bare == Jabber.xmpp.MyJID.Bare)
            {
                string bare = presence.From.Bare;
                string resource = (presence.From.Resource != null) ? presence.From.Resource.Trim() : string.Empty;
                if (contacts.ContainsKey(bare))
                {
                    if (resource != string.Empty)
                    {
                        if (!contacts[bare].ContainsKey(resource))
                        {
                            contacts[bare].Add(resource, new Contact(presence.From, string.Empty, null));
                            OnResourceAdded(contacts[bare][resource]);
                        }
                        contacts[bare][resource].priority = presence.Priority;
                        Status st = new Status();
                        st.message = (presence.Status != null) ? presence.Status.Trim() : string.Empty;
                        st.type = Enumerations.PresenceTypeConverter(presence.Type);
                        if (st.type == Enumerations.StatusType.Normal)
                        {
                            if (contacts[bare][resource].status.type != Enumerations.StatusType.Normal)
                            {
                                if (initialUserPEP.ContainsKey(bare))
                                {
                                    if (initialUserPEP[bare].activity.text != null)
                                    {
                                        contacts[bare][resource].activity = initialUserPEP[bare].activity;
                                        OnActivityUpdated(contacts[bare][resource], contacts[bare][resource].activity);
                                    }
                                    if (initialUserPEP[bare].mood.text != null)
                                    {
                                        contacts[bare][resource].mood = initialUserPEP[bare].mood;
                                        OnMoodUpdated(contacts[bare][resource], contacts[bare][resource].mood);
                                    }
                                    if (initialUserPEP[bare].location.text != null)
                                    {
                                        contacts[bare][resource].location = initialUserPEP[bare].location;
                                        OnLocationUpdated(contacts[bare][resource], contacts[bare][resource].location);
                                    }
                                    if (initialUserPEP[bare].tune.title != null)
                                    {
                                        contacts[bare][resource].tune = initialUserPEP[bare].tune;
                                        OnTuneUpdated(contacts[bare][resource], contacts[bare][resource].tune);
                                    }
                                    initialUserPEP.Remove(bare);
                                }
                            }
                            st.type = Enumerations.StatusTypeConverter(presence.Show);
                        }
                        if (st.type == Enumerations.StatusType.Unvailable)
                        {
                            if (contacts[bare][resource].status.type != Enumerations.StatusType.Unvailable)
                            {

                                if (initialUserPEP.ContainsKey(bare))
                                {
                                    initialUserPEP.Remove(bare);
                                }
                                UserPEP up = new UserPEP();
                                up.tune = contacts[bare][resource].tune;
                                up.activity = contacts[bare][resource].activity;
                                up.mood = contacts[bare][resource].mood;
                                up.location = contacts[bare][resource].location;
                                initialUserPEP.Add(bare, up);
                            }
                        }
                        contacts[bare][resource].status = st;
                        if (presence.XDelay != null)
                        {
                            contacts[bare][resource].timeInterval = (DateTime.Now - presence.XDelay.Stamp);
                        }
                        if (presence.Nickname != null)
                        {
                            contacts[bare][resource].identity.nickname = presence.Nickname.ToString().Trim();
                        }
                        contacts[bare][resource].lastUpdated = DateTime.Now;
                        OnPresenceUpdated(contacts[bare][resource]);
                    }
                    privacyListUpdated(Jabber._privacy);
                }
            }
        }