private void AddAgentToSession(UUID AgentID, UUID GroupID, GridInstantMessage msg)
        {
            if (m_log.IsDebugEnabled)
            {
                m_log.DebugFormat("[GroupsMessagingModule]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
            // Agent not in session and hasn't dropped from session
            // Add them to the session for now, and Invite them
            AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID);

            IClientAPI activeClient = GetActiveClient(AgentID);

            if (activeClient != null)
            {
                GroupRecord groupInfo = m_groupData.GetGroupRecord(UUID.Zero.ToString(), GroupID, null);
                if (groupInfo != null)
                {
                    if (m_log.IsDebugEnabled)
                    {
                        m_log.DebugFormat("[GroupsMessagingModule]: Sending chatterbox invite instant message");
                    }

                    // Force? open the group session dialog???
                    // and simultanously deliver the message, so we don't need to do a seperate client.SendInstantMessage(msg);
                    IEventQueue eq = activeClient.Scene.RequestModuleInterface <IEventQueue>();
                    eq.ChatterboxInvitation(
                        GroupID
                        , groupInfo.GroupName
                        , new UUID(msg.fromAgentID)
                        , msg.message
                        , AgentID
                        , msg.fromAgentName
                        , msg.dialog
                        , msg.timestamp
                        , msg.offline == 1
                        , (int)msg.ParentEstateID
                        , msg.Position
                        , 1
                        , new UUID(msg.imSessionID)
                        , msg.fromGroup
                        , OpenMetaverse.Utils.StringToBytes(groupInfo.GroupName)
                        );

                    eq.ChatterBoxSessionAgentListUpdates(
                        new UUID(GroupID)
                        , AgentID
                        , new UUID(msg.toAgentID)
                        , false //canVoiceChat
                        , false //isModerator
                        , false //text mute
                        );
                }
            }
        }
        private void AddAgentToSession(UUID AgentID, UUID GroupID, GridInstantMessage msg)
        {
            // Agent not in session and hasn't dropped from session
            // Add them to the session for now, and Invite them
            AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID);

            IClientAPI activeClient = GetActiveClient(AgentID);

            if (activeClient != null)
            {
                GroupRecord groupInfo = m_groupData.GetGroupRecord(UUID.Zero.ToString(), GroupID, null);
                if (groupInfo != null)
                {
                    if (m_debugEnabled)
                    {
                        m_log.DebugFormat("[Groups.Messaging]: Sending chatterbox invite instant message");
                    }

                    UUID fromAgent = new UUID(msg.fromAgentID);
                    // Force? open the group session dialog???
                    // and simultanously deliver the message, so we don't need to do a seperate client.SendInstantMessage(msg);
                    IEventQueue eq = activeClient.Scene.RequestModuleInterface <IEventQueue>();
                    if (eq != null)
                    {
                        eq.ChatterboxInvitation(
                            GroupID
                            , groupInfo.GroupName
                            , fromAgent
                            , msg.message
                            , AgentID
                            , msg.fromAgentName
                            , msg.dialog
                            , msg.timestamp
                            , msg.offline == 1
                            , (int)msg.ParentEstateID
                            , msg.Position
                            , 1
                            , new UUID(msg.imSessionID)
                            , msg.fromGroup
                            , OpenMetaverse.Utils.StringToBytes(groupInfo.GroupName)
                            );

                        var update  = new GroupChatListAgentUpdateData(AgentID);
                        var updates = new List <GroupChatListAgentUpdateData> {
                            update
                        };
                        eq.ChatterBoxSessionAgentListUpdates(GroupID, new UUID(msg.toAgentID), updates);
                    }
                }
            }
        }
        private void ProcessMessageFromGroupSession(GridInstantMessage msg)
        {
            if (m_debugEnabled)
            {
                m_log.DebugFormat("[GROUPS-MESSAGING]: Session message from {0} going to agent {1}", msg.fromAgentName, msg.toAgentID);
            }

            switch (msg.dialog)
            {
            case (byte)InstantMessageDialog.SessionAdd:
                AddAgentToGroupSession(msg.fromAgentID, msg.imSessionID);
                break;

            case (byte)InstantMessageDialog.SessionDrop:
                RemoveAgentFromGroupSession(msg.fromAgentID, msg.imSessionID);
                break;

            case (byte)InstantMessageDialog.SessionSend:
                if (!m_agentsInGroupSession.ContainsKey(msg.toAgentID) &&
                    !m_agentsDroppedSession.ContainsKey(msg.toAgentID))
                {
                    // Agent not in session and hasn't dropped from session
                    // Add them to the session for now, and Invite them
                    AddAgentToGroupSession(msg.toAgentID, msg.imSessionID);

                    UUID       toAgentID    = new UUID(msg.toAgentID);
                    IClientAPI activeClient = GetActiveClient(toAgentID);
                    if (activeClient != null)
                    {
                        UUID groupID = new UUID(msg.fromAgentID);

                        GroupRecord groupInfo = m_groupsModule.GetGroupRecord(groupID);
                        if (groupInfo != null)
                        {
                            if (m_debugEnabled)
                            {
                                m_log.DebugFormat("[GROUPS-MESSAGING]: Sending chatterbox invite instant message");
                            }

                            // Force? open the group session dialog???
                            IEventQueue eq = activeClient.Scene.RequestModuleInterface <IEventQueue>();
                            eq.ChatterboxInvitation(
                                groupID
                                , groupInfo.GroupName
                                , new UUID(msg.fromAgentID)
                                , msg.message, new UUID(msg.toAgentID)
                                , msg.fromAgentName
                                , msg.dialog
                                , msg.timestamp
                                , msg.offline == 1
                                , (int)msg.ParentEstateID
                                , msg.Position
                                , 1
                                , new UUID(msg.imSessionID)
                                , msg.fromGroup
                                , Utils.StringToBytes(groupInfo.GroupName)
                                );

                            eq.ChatterBoxSessionAgentListUpdates(
                                new UUID(groupID)
                                , new UUID(msg.fromAgentID)
                                , new UUID(msg.toAgentID)
                                , false     //canVoiceChat
                                , false     //isModerator
                                , false     //text mute
                                );
                        }
                    }
                }
                else if (!m_agentsDroppedSession.ContainsKey(msg.toAgentID))
                {
                    // User hasn't dropped, so they're in the session,
                    // maybe we should deliver it.
                    IClientAPI client = GetActiveClient(new UUID(msg.toAgentID));
                    if (client != null)
                    {
                        // Deliver locally, directly
                        if (m_debugEnabled)
                        {
                            m_log.DebugFormat("[GROUPS-MESSAGING]: Delivering to {0} locally", client.Name);
                        }
                        client.SendInstantMessage(msg);
                    }
                    else
                    {
                        m_log.WarnFormat("[GROUPS-MESSAGING]: Received a message over the grid for a client that isn't here: {0}", msg.toAgentID);
                    }
                }
                break;

            default:
                m_log.WarnFormat("[GROUPS-MESSAGING]: I don't know how to proccess a {0} message.", ((InstantMessageDialog)msg.dialog).ToString());
                break;
            }
        }
        private void ProcessMessageFromGroupSession(GridInstantMessage msg, IClientAPI client)
        {
            if (m_debugEnabled)
            {
                m_log.DebugFormat(
                    "[GROUPS-MESSAGING]: Session message from {0} going to agent {1}, sessionID {2}, type {3}",
                    msg.fromAgentName, msg.toAgentID, msg.imSessionID, (InstantMessageDialog)msg.dialog);
            }

            UUID        fromAgentID = new UUID(msg.fromAgentID);
            UUID        GroupID     = new UUID(msg.imSessionID);
            IEventQueue eq          = client.Scene.RequestModuleInterface <IEventQueue>();

            switch (msg.dialog)
            {
            case (byte)InstantMessageDialog.SessionAdd:
                m_groupData.AgentInvitedToGroupChatSession(fromAgentID, GroupID);
                if (eq != null)
                {
                    eq.ChatterBoxSessionAgentListUpdates(
                        GroupID
                        , fromAgentID
                        , client.AgentId
                        , false    //canVoiceChat
                        , false    //isModerator
                        , false    //text mute
                        , true     // enter
                        );
                }
                break;

            case (byte)InstantMessageDialog.SessionDrop:
                m_groupData.AgentDroppedFromGroupChatSession(fromAgentID, GroupID);
                if (eq != null)
                {
                    eq.ChatterBoxSessionAgentListUpdates(
                        GroupID
                        , fromAgentID
                        , client.AgentId
                        , false         //canVoiceChat
                        , false         //isModerator
                        , false         //text mute
                        , false         // leave
                        );
                }
                break;

            case (byte)InstantMessageDialog.SessionSend:
                if (!m_groupData.hasAgentDroppedGroupChatSession(client.AgentId, GroupID))
                {
                    if (!m_groupData.hasAgentBeenInvitedToGroupChatSession(client.AgentId, GroupID))
                    {
                        GroupRecord groupInfo = m_groupData.GetGroupRecord(UUID.Zero, GroupID, null);
                        if (groupInfo != null)
                        {
                            if (m_debugEnabled)
                            {
                                m_log.DebugFormat("[GROUPS-MESSAGING]: Sending chatterbox invite instant message");
                            }

                            if (eq != null)
                            {
                                eq.ChatterboxInvitation(
                                    GroupID
                                    , groupInfo.GroupName
                                    , fromAgentID
                                    , msg.message
                                    , client.AgentId
                                    , msg.fromAgentName
                                    , msg.dialog
                                    , msg.timestamp
                                    , msg.offline == 1
                                    , (int)msg.ParentEstateID
                                    , msg.Position
                                    , 1
                                    , new UUID(msg.imSessionID)
                                    , msg.fromGroup
                                    , Utils.StringToBytes(groupInfo.GroupName)
                                    );
                            }
                        }
                    }
                    else
                    {
                        client.SendInstantMessage(msg);
                    }

//                        if (!m_groupData.hasAgentBeenInvitedToGroupChatSession(fromAgentID, GroupID))
                    {
                        m_groupData.AgentInvitedToGroupChatSession(fromAgentID, GroupID);
                        eq.ChatterBoxSessionAgentListUpdates(
                            GroupID
                            , fromAgentID
                            , client.AgentId
                            , false        //canVoiceChat
                            , false        //isModerator
                            , false        //text mute
                            , true         // enter
                            );
                    }
                }
                break;

            default:
                client.SendInstantMessage(msg);
                break;;
            }
        }
        private void ProcessMessageFromGroupSession(GridInstantMessage msg)
        {
            if (m_debugEnabled)
            {
                m_log.DebugFormat("[GROUPS-MESSAGING]: Session message from {0} going to agent {1}", msg.fromAgentName, msg.toAgentID);
            }

            switch (msg.dialog)
            {
            case (byte)InstantMessageDialog.SessionAdd:
                AddAgentToGroupSession(msg.fromAgentID, msg.imSessionID);
                NotifyNewSessionUserOfExisting(msg.imSessionID, msg.fromAgentID);
                NotifySessionUserTransition(msg.imSessionID, msg.fromAgentID, msg.dialog);
                break;

            case (byte)InstantMessageDialog.SessionDrop:
                RemoveAgentFromGroupSession(msg.fromAgentID, msg.imSessionID);
                NotifySessionUserTransition(msg.imSessionID, msg.fromAgentID, msg.dialog);
                break;

            case (byte)InstantMessageDialog.SessionSend:
                bool needsSessionAdd     = false;
                bool agentDroppedSession = false;

                lock (m_sessionLock)
                {
                    needsSessionAdd     = (!HasAgentBeenInvited(msg.toAgentID, msg.imSessionID)) && (!HasAgentDroppedSession(msg.toAgentID, msg.imSessionID));
                    agentDroppedSession = HasAgentDroppedSession(msg.toAgentID, msg.imSessionID);
                }

                if (needsSessionAdd)
                {
                    // Agent not in session and hasn't dropped from session
                    // Add them to the session for now, and Invite them
                    AddAgentToGroupSession(msg.toAgentID, msg.imSessionID);

                    UUID       toAgentID    = new UUID(msg.toAgentID);
                    IClientAPI activeClient = GetActiveClient(toAgentID);
                    if (activeClient != null)
                    {
                        UUID groupID = new UUID(msg.imSessionID);       // caller passes the group ID here

                        GroupRecord groupInfo = m_groupsModule.GetGroupRecord(groupID);
                        if (groupInfo != null)
                        {
                            if (m_debugEnabled)
                            {
                                m_log.DebugFormat("[GROUPS-MESSAGING]: Sending chatterbox invite instant message");
                            }

                            // UUID sessionID = new UUID(groupInfo.GroupID);
                            // msg.imSessionID = sessionID.Guid;
                            UUID sessionID = new UUID(msg.imSessionID);

                            // Force? open the group session dialog???
                            IEventQueue eq = activeClient.Scene.RequestModuleInterface <IEventQueue>();
                            eq.ChatterboxInvitation(
                                sessionID                                  // session ID
                                , groupInfo.GroupName                      // session name
                                , new UUID(msg.fromAgentID)                // from agent ID
                                , msg.message                              // message text
                                , new UUID(msg.toAgentID)                  // to agent ID
                                , msg.fromAgentName                        // from agent name
                                , msg.dialog                               // IM dialog type
                                , msg.timestamp                            // time stamp
                                , msg.offline == 1                         // offline
                                , 0                                        // parent estate ID
                                , Vector3.Zero                             // agent position
                                , 1                                        // TTL
                                , groupID                                  // transaction ID (caller passes the group ID here)
                                , msg.fromGroup                            // from group boolean (true)
                                , Utils.StringToBytes(groupInfo.GroupName) // binary bucket
                                );

                            NotifyNewSessionUserOfExisting(msg.imSessionID, msg.fromAgentID);
                            NotifySessionUserTransition(msg.imSessionID, msg.fromAgentID, (byte)InstantMessageDialog.SessionAdd);

                            ((Scene)activeClient.Scene).EventManager.TriggerOnChatToClient(msg.message,
                                                                                           UUID.Parse(msg.fromAgentID.ToString()), UUID.Parse(msg.toAgentID.ToString()),
                                                                                           activeClient.Scene.RegionInfo.RegionID, msg.timestamp,
                                                                                           ChatToClientType.GroupMessage);
                        }
                    }
                }
                else if (!agentDroppedSession)
                {
                    // User hasn't dropped, so they're in the session,
                    // maybe we should deliver it.
                    IClientAPI client = GetActiveClient(new UUID(msg.toAgentID));
                    if (client != null)
                    {
                        // Deliver locally, directly
                        if (m_debugEnabled)
                        {
                            m_log.DebugFormat("[GROUPS-MESSAGING]: Delivering to {0} locally", client.Name);
                        }
                        client.SendInstantMessage(msg);
                    }
                    else
                    {
                        m_log.WarnFormat("[GROUPS-MESSAGING]: Received a message over the grid for a client that isn't here: {0}", msg.toAgentID);
                    }
                }
                break;

            default:
                m_log.WarnFormat("[GROUPS-MESSAGING]: I don't know how to proccess a {0} message.", ((InstantMessageDialog)msg.dialog).ToString());
                break;
            }
        }
        private void ProcessMessageFromGroupSession(GridInstantMessage msg, IClientAPI client)
        {
            if (m_debugEnabled)
            {
                m_log.DebugFormat(
                    "[GROUPS-MESSAGING]: Session message from {0} going to agent {1}, sessionID {2}, type {3}",
                    msg.fromAgentName, msg.toAgentID, msg.imSessionID, (InstantMessageDialog)msg.dialog);
            }

            UUID AgentID = new UUID(msg.fromAgentID);
            UUID GroupID = new UUID(msg.imSessionID);

            switch (msg.dialog)
            {
            case (byte)InstantMessageDialog.SessionAdd:
                m_groupData.AgentInvitedToGroupChatSession(AgentID, GroupID);
                break;

            case (byte)InstantMessageDialog.SessionDrop:
                m_groupData.AgentDroppedFromGroupChatSession(AgentID, GroupID);
                break;

            case (byte)InstantMessageDialog.SessionSend:
                if (!m_groupData.hasAgentDroppedGroupChatSession(AgentID, GroupID) &&
                    !m_groupData.hasAgentBeenInvitedToGroupChatSession(AgentID, GroupID)
                    )
                {
                    // Agent not in session and hasn't dropped from session
                    // Add them to the session for now, and Invite them
                    m_groupData.AgentInvitedToGroupChatSession(AgentID, GroupID);

                    GroupRecord groupInfo = m_groupData.GetGroupRecord(UUID.Zero, GroupID, null);
                    if (groupInfo != null)
                    {
                        if (m_debugEnabled)
                        {
                            m_log.DebugFormat("[GROUPS-MESSAGING]: Sending chatterbox invite instant message");
                        }

                        // Force? open the group session dialog???
                        // and simultanously deliver the message, so we don't need to do a seperate client.SendInstantMessage(msg);
                        IEventQueue eq = client.Scene.RequestModuleInterface <IEventQueue>();
                        eq.ChatterboxInvitation(
                            GroupID
                            , groupInfo.GroupName
                            , new UUID(msg.fromAgentID)
                            , msg.message
                            , new UUID(msg.toAgentID)
                            , msg.fromAgentName
                            , msg.dialog
                            , msg.timestamp
                            , msg.offline == 1
                            , (int)msg.ParentEstateID
                            , msg.Position
                            , 1
                            , new UUID(msg.imSessionID)
                            , msg.fromGroup
                            , Utils.StringToBytes(groupInfo.GroupName)
                            );

                        eq.ChatterBoxSessionAgentListUpdates(
                            new UUID(GroupID)
                            , new UUID(msg.fromAgentID)
                            , new UUID(msg.toAgentID)
                            , false     //canVoiceChat
                            , false     //isModerator
                            , false     //text mute
                            );
                    }

                    break;
                }
                else if (!m_groupData.hasAgentDroppedGroupChatSession(AgentID, GroupID))
                {
                    // User hasn't dropped, so they're in the session,
                    // maybe we should deliver it.
                    client.SendInstantMessage(msg);
                }

                break;

            default:
                client.SendInstantMessage(msg);

                break;;
            }
        }