private void Self_OnMessage(string FromAgentName, UUID FromAgentID, UUID ToAgentID,
                                    string Message, UUID IMSessionID, bool GroupIM,
                                    UUID RegionID, Vector3 Position,
                                    InstantMessageDialog Dialog, ChatType Type, EventArgs origin)
        {
            if (Dialog == InstantMessageDialog.GroupNotice)
            {
                GroupIM = true;
            }

            BotPermissions perms = GetSecurityLevel(FromAgentID, FromAgentName);

            // Received an IM from someone that is authenticated
            if (Type == ChatType.OwnerSay)
            {
                perms |= BotPermissions.Owner;
            }

            bool displayedMessage = false;

            if (origin is ChatEventArgs && Message.Length > 0 && Dialog == InstantMessageDialog.MessageFromAgent)
            {
                WriteLine(String.Format("{0} says, \"{1}\".", FromAgentName, Message));
                PosterBoard["/posterboard/onchat"] = Message;
                if (FromAgentName == Self.Name)
                {
                    PosterBoard["/posterboard/onchat-said"] = Message;
                }
                else
                {
                    PosterBoard["/posterboard/onchat-heard"] = Message;
                }
            }

            bool groupIM = GroupIM && GroupMembers != null && GroupMembers.ContainsKey(FromAgentID) ? true : false;


            switch (Dialog)
            {
            case InstantMessageDialog.MessageBox:
                break;

            case InstantMessageDialog.GroupInvitation:
                if ((perms & BotPermissions.AcceptGroupAndFriendRequests) != 0)
                {
                    string groupName = Message;
                    int    found     = groupName.IndexOf("Group:");
                    if (found > 0)
                    {
                        groupName = groupName.Substring(found + 6);
                    }
                    Self.InstantMessage(Self.Name, FromAgentID, string.Empty, IMSessionID,
                                        InstantMessageDialog.GroupInvitationAccept, InstantMessageOnline.Offline,
                                        Self.SimPosition,
                                        UUID.Zero, new byte[0]);
                    found = groupName.IndexOf(":");
                    if (found > 0)
                    {
                        groupName = groupName.Substring(0, found).Trim();
                        ExecuteCommand("joingroup " + groupName, CMDFLAGS.NoResult);
                    }
                }
                break;

            case InstantMessageDialog.InventoryOffered:
                break;

            case InstantMessageDialog.InventoryAccepted:
                break;

            case InstantMessageDialog.InventoryDeclined:
                break;

            case InstantMessageDialog.GroupVote:
                break;

            case InstantMessageDialog.TaskInventoryOffered:
                break;

            case InstantMessageDialog.TaskInventoryAccepted:
                break;

            case InstantMessageDialog.TaskInventoryDeclined:
                break;

            case InstantMessageDialog.NewUserDefault:
                break;

            case InstantMessageDialog.SessionAdd:
                break;

            case InstantMessageDialog.SessionOfflineAdd:
                break;

            case InstantMessageDialog.SessionGroupStart:
                break;

            case InstantMessageDialog.SessionCardlessStart:
                break;

            case InstantMessageDialog.SessionSend:
                break;

            case InstantMessageDialog.SessionDrop:
                break;

            case InstantMessageDialog.BusyAutoResponse:
                break;

            case InstantMessageDialog.ConsoleAndChatHistory:
                break;

            case InstantMessageDialog.Lure911:
            case InstantMessageDialog.RequestTeleport:
                if ((perms & BotPermissions.AcceptTeleport) != 0)
                {
                    TheSimAvatar.StopMoving();
                    if (RegionID != UUID.Zero)
                    {
                        if (!displayedMessage)
                        {
                            DisplayNotificationInChat("TP to Lure from " + FromAgentName);
                            displayedMessage = true;
                        }
                        SimRegion R = SimRegion.GetRegion(RegionID, gridClient);
                        if (R != null)
                        {
                            Self.Teleport(R.RegionHandle, Position);
                            return;
                        }
                    }
                    DisplayNotificationInChat("Accepting TP Lure from " + FromAgentName);
                    displayedMessage = true;
                    Self.TeleportLureRespond(FromAgentID, IMSessionID, true);
                }
                break;

            case InstantMessageDialog.AcceptTeleport:
                break;

            case InstantMessageDialog.DenyTeleport:
                break;

            case InstantMessageDialog.GodLikeRequestTeleport:
                break;

            //  case InstantMessageDialog.CurrentlyUnused:
            //    break;
            case InstantMessageDialog.GotoUrl:
                break;

            case InstantMessageDialog.Session911Start:
                break;

            case InstantMessageDialog.FromTaskAsAlert:
                break;

            case InstantMessageDialog.GroupNotice:
                break;

            case InstantMessageDialog.GroupNoticeInventoryAccepted:
                break;

            case InstantMessageDialog.GroupNoticeInventoryDeclined:
                break;

            case InstantMessageDialog.GroupInvitationAccept:
                break;

            case InstantMessageDialog.GroupInvitationDecline:
                break;

            case InstantMessageDialog.GroupNoticeRequested:
                break;

            case InstantMessageDialog.FriendshipOffered:
                if ((perms & BotPermissions.AcceptGroupAndFriendRequests) != 0)
                {
                    DisplayNotificationInChat("Accepting Friendship from " + FromAgentName);
                    Friends.AcceptFriendship(FromAgentID, IMSessionID);
                    displayedMessage = true;
                }
                break;

            case InstantMessageDialog.FriendshipAccepted:
                break;

            case InstantMessageDialog.FriendshipDeclined:
                break;

            case InstantMessageDialog.StartTyping:
                break;

            case InstantMessageDialog.StopTyping:
                break;

            case InstantMessageDialog.MessageFromObject:
            case InstantMessageDialog.MessageFromAgent:
                // message from self
                if (FromAgentName == GetName())
                {
                    return;
                }
                // message from system
                if (FromAgentName == "System")
                {
                    return;
                }
                // message from others
                CommandInstance ci;
                if (Commands.TryGetValue("im", out ci))
                {
                    var whisper = ci.WithBotClient as Cogbot.Actions.Communication.ImCommand;
                    if (whisper != null)
                    {
                        whisper.currentAvatar  = FromAgentID;
                        whisper.currentSession = IMSessionID;
                    }
                }
                var cea = origin as ChatEventArgs;
                if ((perms & BotPermissions.ExecuteCommands) != 0)
                {
                    OutputDelegate WriteLine;
                    if (origin is InstantMessageEventArgs)
                    {
                        WriteLine = new OutputDelegate(
                            (string text, object[] ps) =>
                        {
                            string reply0 = DLRConsole.SafeFormat(text, ps);
                            InstantMessage(FromAgentID, reply0, IMSessionID);
                        });
                    }
                    else
                    {
                        WriteLine = new OutputDelegate(
                            (string text, object[] ps) =>
                        {
                            string reply0 = DLRConsole.SafeFormat(text, ps);
                            Talk(reply0, 0, Type);
                        });
                    }
                    string   cmd        = Message;
                    CMDFLAGS needResult = CMDFLAGS.Console;
                    if (cmd.StartsWith("cmcmd "))
                    {
                        cmd = cmd.Substring(6);
                        WriteLine("");
                        WriteLine(string.Format("invokecm='{0}'", cmd));
                        ClientManager.DoCommandAll(cmd, FromAgentID, WriteLine);
                    }
                    else if (cmd.StartsWith("cmd "))
                    {
                        cmd = cmd.Substring(4);
                        WriteLine(string.Format("invoke='{0}'", cmd));
                        var res = ExecuteCommand(cmd, FromAgentID, WriteLine, needResult);
                        WriteLine("iresult='" + res + "'");
                    }
                    else if (cmd.StartsWith("/") || cmd.StartsWith("@"))
                    {
                        cmd = cmd.Substring(1);
                        WriteLine("");
                        WriteLine(string.Format("invoke='{0}'", cmd));
                        var res = ExecuteCommand(cmd, FromAgentID, WriteLine, needResult);
                        WriteLine("iresult='" + res + "'");
                    }
                }
                if (cea != null && cea.AudibleLevel == ChatAudibleLevel.Barely)
                {
                    return;
                }
                break;

            default:
                break;
            }
            //if (Dialog != InstantMessageDialog.MessageFromAgent && Dialog != InstantMessageDialog.MessageFromObject)
            {
                string debug = String.Format("{0} {1} {2} {3} {4} {5}: {6}",
                                             groupIM ? "GroupIM" : "IM", Dialog, Type, perms, FromAgentID, FromAgentName,
                                             Helpers.StructToString(origin));
                if (!displayedMessage)
                {
                    DisplayNotificationInChat(debug);
                    displayedMessage = true;
                }
            }
        }