Ejemplo n.º 1
0
        public static void CommandExecutionThread_Start()
        {
            if (CommandExecutionThread.IsAlive)
            {
                return;
            }
            CommandExecutionThread = new Thread(delegate()
            {
                do
                {
                    try
                    {
                        if (RMessagesData.RCmdsListCollect.RCmdsList.Count <= 0 || RMessagesData.RCmdsListCollect.RCmdsList[0] == null)
                        {
                            Thread.Sleep(200);
                            continue;
                        }
                        var rCommandItem = RMessagesData.RCmdsListCollect.RCmdsList[0];
                        RMessagesData.RCmdsListCollect.RCmdsList.Remove(rCommandItem);
                        var accessToken = SiteData.VK.GetAccessToken();
                        int peerId      = rCommandItem.PeerId;
                        SiteUtils.MessagesSetActivity(accessToken, peerId);
                        SiteUtils.MakeAsReadMessage(accessToken, rCommandItem);
                        Action action = null;
                        switch (rCommandItem.MCommand)
                        {
                        case EnumData.MessageCommand.Image:
                            action = () => MessagesCore.VkSendImageMessage(accessToken, rCommandItem);
                            break;

                        case EnumData.MessageCommand.CommandUser:
                            foreach (var key in RMessagesData.UserCommands.Keys)
                            {
                                foreach (var part in key)
                                {
                                    if (!rCommandItem.STRCommand.Equals(part, StringComparison.OrdinalIgnoreCase))
                                    {
                                        continue;
                                    }
                                    rCommandItem.Message = rCommandItem.Message.Replace(part, String.Empty).Trim();
                                    switch (RMessagesData.UserCommands[key])
                                    {
                                    case EnumData.UserCmd.Joke:
                                        action = () => MessagesCore.VkSendJokeMessage(accessToken, rCommandItem);
                                        break;

                                    case EnumData.UserCmd.Weather:
                                        break;

                                    case EnumData.UserCmd.Rsp:
                                        action = () => MessagesCore.VkSendRSPMessage(accessToken, rCommandItem);
                                        break;

                                    case EnumData.UserCmd.Random:
                                        action = () => MessagesCore.VkSendRandomValueMessage(accessToken, rCommandItem);
                                        break;

                                    case EnumData.UserCmd.Awesome:

                                        action = () => MessagesCore.VkSendAwesomeMessage(accessToken, rCommandItem);
                                        break;

                                    case EnumData.UserCmd.Help:
                                        action = () => MessagesCore.VkSendHelpMessage(accessToken, rCommandItem);
                                        break;

                                    case EnumData.UserCmd.AnonymousSendGroup:
                                        action = () => MessagesCore.VkSendAnonymousMessage(accessToken, rCommandItem);
                                        break;

                                    case EnumData.UserCmd.VoiceMessage:
                                        action = () => MessagesCore.VkSendVoiceRecognizedMessage(accessToken, rCommandItem);
                                        break;

                                    case EnumData.UserCmd.MailCheck:
                                        action = () => MessagesCore.VkMailCheck(accessToken, rCommandItem);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException();
                                    }
                                }
                            }
                            break;

                        case EnumData.MessageCommand.Music:
                            action = () => MessagesCore.VkSendMusicMessage(accessToken, rCommandItem);
                            break;

                        case EnumData.MessageCommand.CommandAdmin:
                            foreach (var key in RMessagesData.AdminCommands.Keys)
                            {
                                rCommandItem.Message = rCommandItem.Message.ToLower();
                                if (!rCommandItem.STRCommand.Equals(key, StringComparison.OrdinalIgnoreCase))
                                {
                                    continue;
                                }
                                rCommandItem.Message = rCommandItem.Message.Replace(key, string.Empty).Trim();
                                switch (RMessagesData.AdminCommands[key])
                                {
                                case EnumData.AdminCmd.ImageChange:
                                    action = () => MessagesCore.VkChangeImage(accessToken, rCommandItem);
                                    break;

                                case EnumData.AdminCmd.ImageCacheDelete:
                                    action = () => MessagesCore.VkImageCacheDelete(accessToken, rCommandItem);
                                    break;

                                case EnumData.AdminCmd.TempImageChange:
                                    break;

                                case EnumData.AdminCmd.MailAdd:
                                    action = () => MessagesCore.VkMailAdd(accessToken, rCommandItem);
                                    break;

                                case EnumData.AdminCmd.MailRemove:
                                    action = () => MessagesCore.VkMailRemove(accessToken, rCommandItem);
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException();
                                }
                            }
                            break;

                        case EnumData.MessageCommand.Wiki:
                            if (!rCommandItem.Message.All(i => i == '?' || i == ')' || i == '('))
                            {
                                action = () => MessagesCore.VkSendWikiMessage(accessToken, rCommandItem);
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                        if (action == null)
                        {
                            continue;
                        }
                        new Thread(action.Invoke)
                        {
                            IsBackground = true
                        }.Start();
                    }
                    catch (Exception ex)
                    {
                        ex.Error();
                    }
                } while (!CommandExecutionThreadStop);
                CommandExecutionThreadStop = false;
                CommandExecutionThread.Abort();
            })
            {
                IsBackground = true
            };
            CommandExecutionThread.Start();
        }
Ejemplo n.º 2
0
        public static void VkSendAnonymousMessage(string accessToken, RMessagesData.RCmdsListCollect.RCommandsItem rCommandItem)
        {
            try
            {
                if (rCommandItem.Message.IndexOf(" ", StringComparison.OrdinalIgnoreCase) < 0)
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, "Ошибка в распозновании названия группы!");
                    return;
                }

                Regex r         = new Regex(@"\s+");
                var   groupData = DataStorage.DialogsList.Find(
                    i => r.Replace(i.Title, @" ").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .All(j => r.Replace(rCommandItem.Message, @" ").IndexOf(j, StringComparison.OrdinalIgnoreCase) >= 0));

                if (string.IsNullOrWhiteSpace(groupData?.Title))
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, "Ошибка в распозновании названия группы!");
                    return;
                }
                rCommandItem.Message =
                    Regex.Replace(rCommandItem.Message, groupData.Title, "", RegexOptions.IgnoreCase).Trim();

                if (groupData.ChatId == 0)
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, "Ошибка в распозновании идентификатора группы!");
                    return;
                }

                if (DataStorage.LastAnonymousMessage.ContainsKey(rCommandItem.FromId))
                {
                    if ((DateTime.Now - DataStorage.LastAnonymousMessage[rCommandItem.FromId]).TotalSeconds < 30)
                    {
                        SiteUtils.SendMessage(accessToken, rCommandItem, "Анонимные сообщения можно отправлять ТОЛЬКО один раз в 30 секунд!");
                        //return;
                    }
                }
                else
                {
                    DataStorage.LastAnonymousMessage.Add(rCommandItem.FromId, DateTime.Now);
                }

                var photoId = string.Empty;
                if (rCommandItem?.Attachments != null && rCommandItem.Attachments.Any(i => i.Photo != null))
                {
                    int peerId = rCommandItem.PeerId;
                    ThreadCore.UploadingPhotos = true;
                    var url = SiteUtils.GetMessageAttachmentUrl(accessToken);
                    SiteUtils.MessagesSetActivity(accessToken, peerId);
                    var    attachments = rCommandItem?.Attachments?.Where(i => i.Photo != null);
                    var    attachment  = attachments.First();
                    string imageLink   = attachment.Photo.Sizes.First(i => i.Width * i.Height == attachment.Photo.Sizes.Max(size2 => size2.Height * size2.Width)).Url;
                    var    imagePath   = $"{FileCore.PathImages}\\{imageLink.GetDeterministicHashCode()}.png";
                    using (WebClient wc = new WebClient())
                    {
                        wc.DownloadFile(new Uri(imageLink), imagePath);
                    }
                    var image = Image.Load <Rgba32>(imagePath);
                    if (image.Height > 720)
                    {
                        int height = 720;
                        int width  = (int)(720 / (double)image.Height * image.Width);
                        image.Mutate(i => i.Resize(width, height));
                    }
                    var photos = SiteUtils.UploadPhotoToServerAsync(url, image);
                    photoId = SiteUtils.SaveMessagePhoto(accessToken, photos);

                    SiteUtils.MessagesSetActivity(accessToken, peerId);
                    ThreadCore.UploadingPhotos = false;
                }

                rCommandItem.PeerId = groupData.ChatId;

                var message = $"[АНОНИМНОЕ СООБЩЕНИЕ]"
                              +
                              "\n"
                              +
                              rCommandItem.Message
                              .Substring(rCommandItem.Message.LastIndexOf("*", StringComparison.OrdinalIgnoreCase) +
                                         1).Trim();
                SiteUtils.MakeAsReadMessage(accessToken, rCommandItem);
                rCommandItem.Id = -1;
                DataStorage.LastAnonymousMessage[rCommandItem.FromId] = DateTime.Now;

                if (string.IsNullOrWhiteSpace(photoId))
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, message);
                }
                else
                {
                    SiteUtils.SendMessage(accessToken, rCommandItem, message,
                                          $"photo{photoId}");
                }
                LogCore.Log(rCommandItem.Message, JsonConvert.SerializeObject(rCommandItem), EnumData.LogTypeCommand.CMessage, EnumData.LogSourceCommand.VK);
            }
            catch (Exception ex)
            {
                ex.Error();
            }
        }