Esempio n. 1
0
        private void Chat_PresenceRaw(object sender, Presence e)
        {
            Guid roomId;

            if (!roomsReverse.TryGetValue(e.From.User, out roomId))
            {
                return;
            }

            var jid    = e.MucUser.Item.Jid;
            var list   = users[roomId];
            var friend = new MucFriend(roomId, jid, e.From.Resource);

            bool offline = e.Type == PresenceType.unavailable;

            if (!offline && !list.Contains(jid.User))
            {
                list.Add(jid.User);
                MemberJoin?.Invoke(this, friend);
            }

            if (offline && list.Contains(jid.User))
            {
                list.Remove(jid.User);
                MemberLeave?.Invoke(this, friend);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to add an <see cref="IGroupable"/> to this group.
        /// </summary>
        /// <param name="groupable">The <see cref="IGroupable"/> to try to add to the group.</param>
        /// <returns>
        /// True if the <paramref name="groupable"/> was successfully added to the group;
        /// otherwise false.
        /// This method will always return false is the <paramref name="groupable"/> is already in a group.
        /// </returns>
        public virtual bool TryAddMember(IGroupable groupable)
        {
            // Check the max members value
            if (_members.Count >= _groupSettings.MaxMembersPerGroup)
            {
                if (log.IsInfoEnabled)
                {
                    log.InfoFormat("Failed to add `{0}` to group `{1}` - the group is full.", groupable, this);
                }
                return(false);
            }

            // Ensure not already in a group
            if (groupable.Group != null)
            {
                if (log.IsInfoEnabled)
                {
                    log.InfoFormat("Failed to add `{0}` to group `{1}` - they were already in a group (`{2}`).", groupable, this,
                                   groupable.Group);
                }
                return(false);
            }

            // Check that they can be added
            if (_groupSettings.CanJoinGroupHandler != null && !_groupSettings.CanJoinGroupHandler(groupable, this))
            {
                if (log.IsInfoEnabled)
                {
                    log.InfoFormat("Failed to add `{0}` to group `{1}` - GroupSettings.CanJoinGroupHandler returned false.",
                                   groupable, this);
                }
                return(false);
            }

            // Add the member
            _members.Add(groupable);
            groupable.Group = this;

            if (log.IsInfoEnabled)
            {
                log.InfoFormat("Added `{0}` to group `{1}`.", groupable, this);
            }

            // Raise events
            OnMemberJoin(groupable);

            if (MemberJoin != null)
            {
                MemberJoin.Raise(this, EventArgsHelper.Create(groupable));
            }

            return(true);
        }