Ejemplo n.º 1
0
        private static string TrimHtml(string ok, string mark)
        {
            StringFinder sf = new StringFinder(ok);

            sf.FindNext($"<{mark}");
            sf.FindNext(">", false);
            sf.FindNext($"</{mark}>");
            return(sf.Cut());
        }
Ejemplo n.º 2
0
        private void SetRows(TableElement tableElement, string ori, string key)
        {
            StringFinder sf = new StringFinder(ori);

            while (sf.FindNext($"<{key}") != -1)
            {
                sf.FindNext($"</{key}>", false);
                string oriRow = sf.Cut();

                var list = GetColList(oriRow, "th", "td");

                tableElement.Rows.AddRow(list);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 将消息中出现的图片类型CQ码转换为可读形式。
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private static string DecodeBFaceToText(string source)
        {
            StringBuilder sb   = new StringBuilder(source);
            StringFinder  sf   = new StringFinder(source);
            const string  str1 = "[CQ:bface,";

            while (sf.FindNext(str1) != -1)
            {
                sf.FindNext("]", false);
                sb.Remove(sf.StartIndex, sf.Length);
                sb.Insert(sf.StartIndex, "[大表情]");
                sf = new StringFinder(sb.ToString());
            }

            return(sb.ToString());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 将消息中出现的图片类型CQ码转换为可读形式。
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private static string DecodeAtToText(string source)
        {
            StringBuilder sb   = new StringBuilder(source);
            StringFinder  sf   = new StringFinder(source);
            const string  str1 = "[CQ:at,";

            while (sf.FindNext(str1) != -1)
            {
                sf.FindNext("]", false);
                string qq = GetAt(sf.Cut())[0];
                sb.Remove(sf.StartIndex, sf.Length);
                sb.Insert(sf.StartIndex, $"@{qq} ");
                sf = new StringFinder(sb.ToString());
            }

            return(sb.ToString());
        }
Ejemplo n.º 5
0
        private static IEnumerable <string> GetColList(string ori, params string[] mark)
        {
            List <string> param = new List <string>();

            foreach (var item in mark)
            {
                StringFinder sf = new StringFinder(ori);
                while (sf.FindNext($"<{item}") != -1)
                {
                    sf.FindNext($"</{item}>", false);
                    string ok     = sf.Cut();
                    string trimed = TrimHtml(ok, item);
                    param.Add(trimed);
                }
            }

            return(param);
        }
Ejemplo n.º 6
0
        public HtmlTable(string htmlStr)
        {
            _htmlStr = htmlStr.Replace("\r", "").Replace("\n", "");
            StringFinder sf = new StringFinder(_htmlStr);

            if (sf.FindNext("<thead") != -1)
            {
                TableHead = new TableHead();
                sf.FindNext("</thead>", false);
                string ori = sf.Cut();
                SetRows(TableHead, ori, "tr");
            }

            if (sf.FindNext("<tbody") != -1)
            {
                TableBody = new TableBody();
                sf.FindNext("</tbody>");
                string ori = sf.Cut();
                SetRows(TableBody, ori, "tr");
            }

            if (sf.FindNext("<tfoot") != -1)
            {
                TableFoot = new TableFoot();
                sf.FindNext("</tfoot>");
                string ori = sf.Cut();
                SetRows(TableFoot, ori, "tr");
            }
        }
Ejemplo n.º 7
0
        public static void Query()
        {
            while (MessageQueue.Count != 0)
            {
                if (!MessageQueue.TryDequeue(out var routeMsg))
                    continue;
                var cmd = routeMsg.CommandName;

                const long cabbageId = 1335734629;
                string uname;
                if (cmd == "statme" || cmd == "bpme" || cmd == "mybp" || cmd == "costme" || cmd == "mycost")
                {
                    BllUserRole bllUserRole = new BllUserRole();
                    List<TableUserRole> userInfo = bllUserRole.GetUserRoleByQq(long.Parse(routeMsg.UserId));
                    if (userInfo.Count == 0)
                        DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(routeMsg.ToSource(DefaultReply.IdNotBound, true));

                    uname = userInfo[0].CurrentUname;
                }
                else
                    uname = routeMsg.ArgString;

                using (Session session = new Session(25000, new CoolQIdentity(cabbageId, MessageType.Private), cabbageId))
                {
                    DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(
                        new CoolQRouteMessage($"!{cmd.Replace("my", "").Replace("me", "")} {uname}",
                            new CoolQIdentity(cabbageId, MessageType.Private)));
                    try
                    {
                        CoolQRouteMessage result = (CoolQRouteMessage)session.GetMessage();
                        session.Timeout = 600;
                        CoolQRouteMessage result2 = null;
                        try
                        {
                            result2 = (CoolQRouteMessage)session.GetMessage();
                        }
                        catch
                        {
                            // ignored
                        }

                        ImageInfo[] imgList =
                            CoolQCode.GetImageInfo(result.RawMessage) ?? CoolQCode.GetImageInfo(result2?.RawMessage);

                        if (imgList == null)
                        {
                            DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(routeMsg.ToSource(result.RawMessage));
                            if (result2 != null)
                                DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(routeMsg.ToSource(result2.RawMessage));
                            continue;
                        }
                        //throw new IndexOutOfRangeException("查询失败:" + result.Message);
                        var message = CoolQCode.DecodeToString(result.RawMessage);
                        foreach (var item in imgList)
                        {
                            var str = new FileImage(new Uri(item.Url));
                            StringFinder sf = new StringFinder(message);
                            sf.FindNext("[图片]");
                            string str1 = sf.Cut();
                            if (sf.FindNext("[图片]", false) > message.Length - 1)
                            {
                                message = str1 + str;
                                continue;
                            }

                            sf.FindToLast();
                            string str2 = sf.Cut();
                            message = str1 + str + str2;
                        }

                        DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(
                            routeMsg.ToSource(message + "\r\n(查询由白菜支持)"));
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        string msg = e.Message;
                        DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(routeMsg.ToSource(msg, true));
                    }
                    catch (TimeoutException)
                    {
                        string msg = "查询失败,白菜没有搭理人家..";
                        DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(routeMsg.ToSource(msg, true));
                    }
                    catch (Exception ex)
                    {
                        string msg = "查询失败,未知错误。";
                        Logger.Exception(ex);
                        DaylilyCore.Current.MessageDispatcher?.SendMessageAsync(routeMsg.ToSource(msg, true));
                    } // catch
                } // using
            } // while
        }
Ejemplo n.º 8
0
        public override CommonMessageResponse Message_Received(CommonMessage messageObj)
        {
            string userName, userId;

            if (OsuId == null)
            {
                BllUserRole        bllUserRole = new BllUserRole();
                List <TblUserRole> userInfo    = bllUserRole.GetUserRoleByQq(long.Parse(messageObj.UserId));
                if (userInfo.Count == 0)
                {
                    return(new CommonMessageResponse(LoliReply.IdNotBound, messageObj, true));
                }
                userId   = userInfo[0].UserId.ToString();
                userName = userInfo[0].CurrentUname;
            }
            else
            {
                int userNum = OldSiteApi.GetUser(OsuId, out var userObj);
                if (userNum == 0)
                {
                    return(new CommonMessageResponse(LoliReply.IdNotFound, messageObj, true));
                }
                if (userNum > 1)
                {
                    // ignored
                }

                userId   = userObj.user_id;
                userName = userObj.username;
            }

            var jsonString = HttpClientUtil.HttpGet("https://syrin.me/pp+/u/" + userId);

            if (jsonString == null)
            {
                return(new CommonMessageResponse("PP+获取超时,请重试…", messageObj, true));
            }

            StringFinder sf = new StringFinder(jsonString);

            sf.FindNext("<div class=\"performance-table\">", false);
            sf.FindNext("</div>");

            string    innerText = sf.Cut().Trim('\n').Trim('\r');
            HtmlTable htmlTable = new HtmlTable(innerText);

            string[,] array = htmlTable.GetArray();

            Dictionary <string, int> dValue = new Dictionary <string, int>();

            for (var i = 0; i < array.GetLength(0); i++)
            {
                for (var j = 0; j < array.GetLength(1); j += 2)
                {
                    string key   = array[i, j].Trim(':').Replace("Aim (", "").Replace(")", "");
                    int    value = int.Parse(array[i, j + 1].Replace(",", "").Trim('p'));
                    dValue.Add(key, value);
                }
            }

            var cqImg = new FileImage(Draw(userName, dValue)).ToString();

            return(new CommonMessageResponse(cqImg, messageObj));
        }
Ejemplo n.º 9
0
        public static void Query()
        {
            while (MessageQueue.Count != 0)
            {
                if (!MessageQueue.TryDequeue(out var messageObj))
                {
                    continue;
                }
                var cmd = messageObj.Command;

                const long cabbageId = 1020640876;
                string     uname;
                if (cmd == "statme" || cmd == "bpme" || cmd == "mybp" || cmd == "costme" || cmd == "mycost")
                {
                    BllUserRole        bllUserRole = new BllUserRole();
                    List <TblUserRole> userInfo    = bllUserRole.GetUserRoleByQq(long.Parse(messageObj.UserId));
                    if (userInfo.Count == 0)
                    {
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(LoliReply.IdNotBound, messageObj, true));
                    }

                    uname = userInfo[0].CurrentUname;
                }
                else
                {
                    uname = messageObj.ArgString;
                }

                using (Session session = new Session(25000, new Identity(cabbageId, MessageType.Private), cabbageId))
                {
                    CoolQDispatcher.SendMessage(
                        new CommonMessageResponse($"!{cmd.Replace("my", "").Replace("me", "")} {uname}",
                                                  new Identity(cabbageId, MessageType.Private)));
                    try
                    {
                        CommonMessage result = session.GetMessage();
                        session.Timeout = 600;
                        CommonMessage result2 = null;
                        try
                        {
                            result2 = session.GetMessage();
                        }
                        catch
                        {
                            // ignored
                        }

                        ImageInfo[] imgList =
                            CqCode.GetImageInfo(result.Message) ?? CqCode.GetImageInfo(result2?.Message);

                        if (imgList == null)
                        {
                            CoolQDispatcher.SendMessage(new CommonMessageResponse(result.Message, messageObj));
                            if (result2 != null)
                            {
                                CoolQDispatcher.SendMessage(new CommonMessageResponse(result2.Message, messageObj));
                            }
                            continue;
                        }
                        //throw new IndexOutOfRangeException("查询失败:" + result.Message);
                        var message = CqCode.DecodeToString(result.Message);
                        foreach (var item in imgList)
                        {
                            var          str = new FileImage(new Uri(item.Url));
                            StringFinder sf  = new StringFinder(message);
                            sf.FindNext("[图片]");
                            string str1 = sf.Cut();
                            if (sf.FindNext("[图片]", false) > message.Length - 1)
                            {
                                message = str1 + str;
                                continue;
                            }

                            sf.FindToLast();
                            string str2 = sf.Cut();
                            message = str1 + str + str2;
                        }

                        CoolQDispatcher.SendMessage(
                            new CommonMessageResponse(message + "\r\n(查询由白菜支持)", messageObj));
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        string msg = e.Message;
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(msg, messageObj, true));
                    }
                    catch (TimeoutException)
                    {
                        string msg = "查询失败,白菜没有搭理人家..";
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(msg, messageObj, true));
                    }
                    catch (Exception ex)
                    {
                        string msg = "查询失败,未知错误。";
                        Logger.Exception(ex);
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(msg, messageObj, true));
                    } // catch
                }     // using
            }         // while
        }             //void
Ejemplo n.º 10
0
        public override CoolQRouteMessage OnMessageReceived(CoolQScopeEventArgs scope)
        {
            var              routeMsg = scope.RouteMessage;
            string           userName, userId;
            OldSiteApiClient client = new OldSiteApiClient();

            if (UserName == null)
            {
                BllUserRole          bllUserRole = new BllUserRole();
                List <TableUserRole> userInfo    = bllUserRole.GetUserRoleByQq(long.Parse(routeMsg.UserId));
                if (userInfo.Count == 0)
                {
                    return(routeMsg.ToSource(DefaultReply.IdNotBound, true));
                }
                userId   = userInfo[0].UserId.ToString();
                userName = userInfo[0].CurrentUname;
            }
            else
            {
                int userNum = client.GetUser(UserComponent.FromUserName(UserName), out var userObj);
                if (userNum == 0)
                {
                    return(routeMsg.ToSource(DefaultReply.IdNotFound, true));
                }
                if (userNum > 1)
                {
                    // ignored
                }

                userId   = userObj.UserId.ToString();
                userName = userObj.UserName;
            }

            var jsonString = HttpClient.HttpGet("https://syrin.me/pp+/u/" + userId);

            if (jsonString == null)
            {
                return(routeMsg
                       .ToSource("PP+获取超时,请重试…", true)
                       .ForceToSend());
            }

            StringFinder sf = new StringFinder(jsonString);

            sf.FindNext("<div class=\"performance-table\">", false);
            sf.FindNext("</div>");

            string    innerText = sf.Cut().Trim('\n').Trim('\r');
            HtmlTable htmlTable = new HtmlTable(innerText);

            string[,] array = htmlTable.GetArray();

            Dictionary <string, int> dValue = new Dictionary <string, int>();

            for (var i = 0; i < array.GetLength(0); i++)
            {
                for (var j = 0; j < array.GetLength(1); j += 2)
                {
                    string key   = array[i, j].Trim(':').Replace("Aim (", "").Replace(")", "");
                    int    value = int.Parse(array[i, j + 1].Replace(",", "").Trim('p'));
                    dValue.Add(key, value);
                }
            }

            var cqImg = new FileImage(Draw(userName, dValue)).ToString();

            return(routeMsg
                   .ToSource(cqImg)
                   .ForceToSend());
        }