Example #1
0
 protected void SessionIdReceived(PacketHeader header, Connection connection, string sessionId)
 {
     if (SessionReceived != null)
     {
         SessionReceived.Invoke(sessionId, EventArgs.Empty);
     }
 }
Example #2
0
        protected virtual bool RaiseSessionEvent(RouteMessage message)
        {
            if (SessionReceived != null)
            {
                SessionReceived?.Invoke(null, new SessionReceivedEventArgs
                {
                    RouteMessageObj = message
                });
                return(true);
            }

            return(false);
        }
Example #3
0
        private static void HandleMessageCmd(CommonMessage cm)
        {
            string          fullCmd = cm.FullCommand;
            CommandAnalyzer ca      = new CommandAnalyzer(new ParamDividerV2());

            ca.Analyze(fullCmd, cm);
            CommonMessageResponse replyObj = null;

            SessionReceived?.Invoke(null, new SessionReceivedEventArgs
            {
                MessageObj = cm
            });

            if (!PluginManager.CommandMap.ContainsKey(cm.Command))
            {
                return;
            }

            Type t = PluginManager.CommandMap[cm.Command];

            if (ValidateDisabled(cm, t))
            {
                SendMessage(new CommonMessageResponse("本群已禁用此命令...", cm));
                return;
            }

            CommandPlugin plugin = t == typeof(ExtendPlugin) ? PluginManager.CommandMapStatic[cm.Command] : GetInstance(t);

            if (!CommandHot.Keys.Contains(cm.Command))
            {
                CommandHot.TryAdd(cm.Command, 1);
            }
            else
            {
                CommandHot[cm.Command]++;
            }

            Settings.SaveSettings(CommandHot, "CommandHot");
            Task.Run(() =>
            {
                try
                {
                    if (!TrySetValues(cm, t, plugin))
                    {
                        return;
                    }
                    replyObj = plugin.Message_Received(cm);
                }
                catch (Exception ex)
                {
                    Exception(ex.InnerException ?? ex, fullCmd, plugin?.Name ?? "Unknown plugin");
                }

                if (replyObj == null)
                {
                    return;
                }
                SendMessage(replyObj);
            }
                     );
        }
Example #4
0
        private static void HandleMessage(CommonMessage cm)
        {
            bool cmdFlag   = false;
            long groupId   = Convert.ToInt64(cm.GroupId);
            long userId    = Convert.ToInt64(cm.UserId);
            long discussId = Convert.ToInt64(cm.DiscussId);
            var  type      = cm.MessageType;

            string group, sender, message = cm.Message;

            if (cm.MessageType == MessageType.Private)
            {
                group  = "私聊";
                sender = SessionInfo[cm.Identity].Name;
            }
            else if (cm.MessageType == MessageType.Discuss)
            {
                group  = SessionInfo[cm.Identity].Name;
                sender = cm.UserId;
            }
            else
            {
                var userInfo = SessionInfo[cm.Identity]?.GroupInfo?.Members?.FirstOrDefault(i => i.UserId == userId) ??
                               CqApi.GetGroupMemberInfo(cm.GroupId, cm.UserId).Data;
                group  = SessionInfo?[cm.Identity]?.Name;
                sender = string.IsNullOrEmpty(userInfo.Card)
                    ? userInfo.Nickname
                    : userInfo.Card;
            }

            Message($"({group}) {sender}:\r\n  {CqCode.DecodeToString(message)}");

            if (cm.Message.Substring(0, 1) == CommandFlag)
            {
                if (cm.Message.IndexOf(CommandFlag + "root ", StringComparison.InvariantCulture) == 0)
                {
                    if (cm.UserId != "2241521134")
                    {
                        SendMessage(new CommonMessageResponse(LoliReply.FakeRoot, cm));
                    }
                    else
                    {
                        cm.FullCommand     = cm.Message.Substring(6, cm.Message.Length - 6);
                        cm.PermissionLevel = PermissionLevel.Root;
                        cmdFlag            = true;
                        HandleMessageCmd(cm);
                    }
                }
                else if (message.IndexOf(CommandFlag + "sudo ", StringComparison.InvariantCulture) == 0 &&
                         type == MessageType.Group)
                {
                    if (SessionInfo[cm.Identity].GroupInfo.Admins.Count(q => q.UserId == userId) == 0)
                    {
                        SendMessage(new CommonMessageResponse(LoliReply.FakeAdmin, cm));
                    }
                    else
                    {
                        cm.FullCommand     = message.Substring(6, message.Length - 6);
                        cm.PermissionLevel = PermissionLevel.Admin;
                        cmdFlag            = true;
                        HandleMessageCmd(cm);
                    }
                }
                else
                {
                    // auto
                    if (SessionInfo[cm.Identity].GroupInfo?.Admins.Count(q => q.UserId == userId) != 0)
                    {
                        cm.PermissionLevel = PermissionLevel.Admin;
                    }
                    if (cm.UserId == "2241521134")
                    {
                        cm.PermissionLevel = PermissionLevel.Root;
                    }

                    cm.FullCommand = message.Substring(1, message.Length - 1);
                    cmdFlag        = true;
                    HandleMessageCmd(cm);
                }
            }
            if (!cmdFlag)
            {
                SessionReceived?.Invoke(null, new SessionReceivedEventArgs
                {
                    MessageObj = cm
                });
            }

            HandleMesasgeApp(cm);
            Thread.Sleep(Rnd.Next(MinTime, MaxTime));
        }