/// <inheritdoc /> public override void Process(BotData data) { base.Process(data); try { var replacedInput = ReplaceValues(inputString, data); switch (group) { case UtilityGroup.List: var list = data.Variables.GetList(listName); var list2 = data.Variables.GetList(secondListName); var item = ReplaceValues(listItem, data); var index = int.Parse(ReplaceValues(listIndex, data)); CVar variable = null; switch (listAction) { case ListAction.Create: data.Variables.Set(new CVar(variableName, new List <string>(), isCapture)); break; case ListAction.Length: data.Variables.Set(new CVar(variableName, list.Count().ToString(), isCapture)); break; case ListAction.Join: data.Variables.Set(new CVar(variableName, string.Join(separator, list), isCapture)); break; case ListAction.Sort: var sorted = list.Select(e => e).ToList(); // Clone the list so we don't edit the original one if (Numeric) { var nums = sorted.Select(e => double.Parse(e, CultureInfo.InvariantCulture)).ToList(); nums.Sort(); sorted = nums.Select(e => e.ToString()).ToList(); } else { sorted.Sort(); } if (!Ascending) { sorted.Reverse(); } data.Variables.Set(new CVar(variableName, sorted, isCapture)); break; case ListAction.Concat: data.Variables.Set(new CVar(variableName, list.Concat(list2).ToList(), isCapture)); break; case ListAction.Zip: data.Variables.Set(new CVar(variableName, list.Zip(list2, (a, b) => a + b).ToList(), isCapture)); break; case ListAction.Map: data.Variables.Set(new CVar(variableName, list.Zip(list2, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v), isCapture)); break; case ListAction.Add: // TODO: Refactor this variable = data.Variables.Get(listName, CVar.VarType.List); if (variable == null) { variable = data.GlobalVariables.Get(listName, CVar.VarType.List); } if (variable == null) { break; } if (variable.Value.Count == 0) { index = 0; } else if (index < 0) { index += variable.Value.Count; } variable.Value.Insert(index, item); break; case ListAction.Remove: // TODO: Refactor this variable = data.Variables.Get(listName, CVar.VarType.List); if (variable == null) { variable = data.GlobalVariables.Get(listName, CVar.VarType.List); } if (variable == null) { break; } if (variable.Value.Count == 0) { index = 0; } else if (index < 0) { index += variable.Value.Count; } variable.Value.RemoveAt(index); break; case ListAction.RemoveValues: data.Variables.Set(new CVar(variableName, list.Where(l => !Condition.Verify(new KeycheckCondition { Left = ReplaceValues(l, data), Comparer = ListElementComparer, Right = ListComparisonTerm })).ToList(), isCapture)); break; case ListAction.RemoveDuplicates: data.Variables.Set(new CVar(variableName, list.Distinct().ToList(), isCapture)); break; case ListAction.Random: data.Variables.Set(new CVar(variableName, list[data.random.Next(list.Count)], isCapture)); break; case ListAction.Shuffle: // This makes a copy of the original list var listCopy = list.ToArray().ToList(); listCopy.Shuffle(data.random); data.Variables.Set(new CVar(variableName, listCopy, isCapture)); break; default: break; } data.Log(new LogEntry($"Executed action {listAction} on file {listName}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.Variable: string single; switch (varAction) { case VarAction.Split: single = data.Variables.GetSingle(varName); data.Variables.Set(new CVar(variableName, single.Split(new string[] { ReplaceValues(splitSeparator, data) }, StringSplitOptions.None).ToList(), isCapture)); break; } data.Log(new LogEntry($"Executed action {varAction} on variable {varName}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.Conversion: byte[] conversionInputBytes = replacedInput.ConvertFrom(conversionFrom); var conversionResult = conversionInputBytes.ConvertTo(conversionTo); data.Variables.Set(new CVar(variableName, conversionResult, isCapture)); data.Log(new LogEntry($"Executed conversion {conversionFrom} to {conversionTo} on input {replacedInput} with outcome {conversionResult}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.File: var file = ReplaceValues(filePath, data); Files.ThrowIfNotInCWD(file); switch (fileAction) { case FileAction.Exists: data.Variables.Set(new CVar(variableName, File.Exists(file).ToString(), isCapture)); break; case FileAction.Read: lock (FileLocker.GetLock(file)) data.Variables.Set(new CVar(variableName, File.ReadAllText(file), isCapture)); break; case FileAction.ReadLines: lock (FileLocker.GetLock(file)) data.Variables.Set(new CVar(variableName, File.ReadAllLines(file).ToList(), isCapture)); break; case FileAction.Write: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.WriteAllText(file, replacedInput.Unescape()); break; case FileAction.WriteLines: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.WriteAllLines(file, ReplaceValuesRecursive(inputString, data).Select(i => i.Unescape())); break; case FileAction.Append: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.AppendAllText(file, replacedInput.Unescape()); break; case FileAction.AppendLines: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.AppendAllLines(file, ReplaceValuesRecursive(inputString, data).Select(i => i.Unescape())); break; case FileAction.Copy: var fileCopyLocation = ReplaceValues(inputString, data); Files.ThrowIfNotInCWD(fileCopyLocation); Files.CreatePath(fileCopyLocation); lock (FileLocker.GetLock(file)) lock (FileLocker.GetLock(fileCopyLocation)) File.Copy(file, fileCopyLocation); break; case FileAction.Move: var fileMoveLocation = ReplaceValues(inputString, data); Files.ThrowIfNotInCWD(fileMoveLocation); Files.CreatePath(fileMoveLocation); lock (FileLocker.GetLock(file)) lock (FileLocker.GetLock(fileMoveLocation)) File.Move(file, fileMoveLocation); break; } data.Log(new LogEntry($"Executed action {fileAction} on file {file}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.Folder: var folder = ReplaceValues(folderPath, data); Files.ThrowIfNotInCWD(folder); switch (folderAction) { case FolderAction.Exists: data.Variables.Set(new CVar(variableName, Directory.Exists(folder).ToString(), isCapture)); break; case FolderAction.Create: data.Variables.Set(new CVar(variableName, Directory.CreateDirectory(folder).ToString(), isCapture)); break; } data.Log(new LogEntry($"Executed action {folderAction} on folder {folder}", isCapture ? Colors.Tomato : Colors.Yellow)); break; default: break; } } catch (Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); } }
/// <inheritdoc /> public override void Process(BotData data) { base.Process(data); try { var replacedInput = ReplaceValues(inputString, data); switch (group) { case UtilityGroup.List: var list = data.Variables.GetList(listName); var list2 = data.Variables.GetList(secondListName); var item = ReplaceValues(listItem, data); var index = int.Parse(ReplaceValues(listIndex, data)); CVar variable = null; switch (listAction) { case ListAction.Create: data.Variables.Set(new CVar(variableName, new List <string>(), isCapture)); break; case ListAction.Length: data.Variables.Set(new CVar(variableName, list.Count().ToString(), isCapture)); break; case ListAction.Join: data.Variables.Set(new CVar(variableName, string.Join(separator, list), isCapture)); break; case ListAction.Sort: var sorted = list.Select(e => e).ToList(); // Clone the list so we don't edit the original one if (Numeric) { var nums = sorted.Select(e => double.Parse(e, CultureInfo.InvariantCulture)).ToList(); nums.Sort(); sorted = nums.Select(e => e.ToString()).ToList(); } else { sorted.Sort(); } if (!Ascending) { sorted.Reverse(); } data.Variables.Set(new CVar(variableName, sorted, isCapture)); break; case ListAction.Concat: data.Variables.Set(new CVar(variableName, list.Concat(list2).ToList(), isCapture)); break; case ListAction.Intersection: data.Variables.Set(new CVar(variableName, list.Intersect(list2).ToList(), isCapture)); break; case ListAction.Difference: data.Variables.Set(new CVar(variableName, list.Except(list2).ToList(), isCapture)); break; case ListAction.Zip: data.Variables.Set(new CVar(variableName, list.Zip(list2, (a, b) => a + b).ToList(), isCapture)); break; case ListAction.Map: data.Variables.Set(new CVar(variableName, list.Zip(list2, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v), isCapture)); break; case ListAction.Add: // TODO: Refactor this variable = data.Variables.Get(listName, CVar.VarType.List); if (variable == null) { variable = data.GlobalVariables.Get(listName, CVar.VarType.List); } if (variable == null) { break; } if (variable.Value.Count == 0) { index = 0; } else if (index < 0) { index += variable.Value.Count; } variable.Value.Insert(index, item); break; case ListAction.Remove: // TODO: Refactor this variable = data.Variables.Get(listName, CVar.VarType.List); if (variable == null) { variable = data.GlobalVariables.Get(listName, CVar.VarType.List); } if (variable == null) { break; } if (variable.Value.Count == 0) { index = 0; } else if (index < 0) { index += variable.Value.Count; } variable.Value.RemoveAt(index); break; case ListAction.RemoveValues: data.Variables.Set(new CVar(variableName, list.Where(l => !Condition.Verify(new KeycheckCondition { Left = ReplaceValues(l, data), Comparer = ListElementComparer, Right = ListComparisonTerm })).ToList(), isCapture)); break; case ListAction.RemoveDuplicates: data.Variables.Set(new CVar(variableName, list.Distinct().ToList(), isCapture)); break; case ListAction.Random: data.Variables.Set(new CVar(variableName, list[data.random.Next(list.Count)], isCapture)); break; case ListAction.Shuffle: // This makes a copy of the original list var listCopy = list.ToArray().ToList(); listCopy.Shuffle(data.random); data.Variables.Set(new CVar(variableName, listCopy, isCapture)); break; default: break; } data.Log(new LogEntry($"Executed action {listAction} on file {listName}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.Variable: string single; switch (varAction) { case VarAction.Split: single = data.Variables.GetSingle(varName); data.Variables.Set(new CVar(variableName, single.Split(new string[] { ReplaceValues(splitSeparator, data) }, StringSplitOptions.None).ToList(), isCapture)); break; } data.Log(new LogEntry($"Executed action {varAction} on variable {varName}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.Conversion: byte[] conversionInputBytes = replacedInput.ConvertFrom(conversionFrom); var conversionResult = conversionInputBytes.ConvertTo(conversionTo); data.Variables.Set(new CVar(variableName, conversionResult, isCapture)); data.Log(new LogEntry($"Executed conversion {conversionFrom} to {conversionTo} on input {replacedInput} with outcome {conversionResult}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.File: var file = ReplaceValues(filePath, data); Files.ThrowIfNotInCWD(file); switch (fileAction) { case FileAction.Exists: data.Variables.Set(new CVar(variableName, File.Exists(file).ToString(), isCapture)); break; case FileAction.Read: lock (FileLocker.GetLock(file)) data.Variables.Set(new CVar(variableName, File.ReadAllText(file), isCapture)); break; case FileAction.ReadLines: lock (FileLocker.GetLock(file)) data.Variables.Set(new CVar(variableName, File.ReadAllLines(file).ToList(), isCapture)); break; case FileAction.Write: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.WriteAllText(file, replacedInput.Unescape()); break; case FileAction.WriteLines: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.WriteAllLines(file, ReplaceValuesRecursive(inputString, data).Select(i => i.Unescape())); break; case FileAction.Append: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.AppendAllText(file, replacedInput.Unescape()); break; case FileAction.AppendLines: Files.CreatePath(file); lock (FileLocker.GetLock(file)) File.AppendAllLines(file, ReplaceValuesRecursive(inputString, data).Select(i => i.Unescape())); break; case FileAction.Copy: var fileCopyLocation = ReplaceValues(inputString, data); Files.ThrowIfNotInCWD(fileCopyLocation); Files.CreatePath(fileCopyLocation); lock (FileLocker.GetLock(file)) lock (FileLocker.GetLock(fileCopyLocation)) File.Copy(file, fileCopyLocation); break; case FileAction.Move: var fileMoveLocation = ReplaceValues(inputString, data); Files.ThrowIfNotInCWD(fileMoveLocation); Files.CreatePath(fileMoveLocation); lock (FileLocker.GetLock(file)) lock (FileLocker.GetLock(fileMoveLocation)) File.Move(file, fileMoveLocation); break; case FileAction.Delete: // No deletion if the file is in use (DB/OpenBullet.db cannot be deleted but instead DB/OpenBullet-BackupCopy.db) // If another process is just reading the file it will be deleted lock (FileLocker.GetLock(file)) File.Delete(file); break; } data.Log(new LogEntry($"Executed action {fileAction} on file {file}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.Folder: var folder = ReplaceValues(folderPath, data); Files.ThrowIfNotInCWD(folder); switch (folderAction) { case FolderAction.Exists: data.Variables.Set(new CVar(variableName, Directory.Exists(folder).ToString(), isCapture)); break; case FolderAction.Create: data.Variables.Set(new CVar(variableName, Directory.CreateDirectory(folder).ToString(), isCapture)); break; case FolderAction.Delete: // All files in the folder will be deleted expect the ones that are in use // DB/OpenBullet.db cannot be deleted but instead DB/OpenBullet-BackupCopy.db // If another process is just reading a file in the folder it will be deleted Directory.Delete(folder, true); break; } data.Log(new LogEntry($"Executed action {folderAction} on folder {folder}", isCapture ? Colors.Tomato : Colors.Yellow)); break; case UtilityGroup.Telegram: lock (tlgLocker) { var bot = new TelegramBotClient(BotToken); Telegram.Bot.Types.ChatId chatId = null; try { if (ChatId.StartsWith("@")) { chatId = new Telegram.Bot.Types.ChatId(ChatId); } else { chatId = new Telegram.Bot.Types.ChatId(int.Parse(ChatId)); } } catch { data.Log(new LogEntry("Chat Id is invalid", Colors.Red)); } var s = 0; while (s < 20) { var result = bot.SendTextMessageAsync(chatId, ReplaceValues(string.Join("\n", Messages), data), ParseMode).Result; if (result == null) { data.Log(new LogEntry("Message sent not successfully!!", Colors.Red)); data.Log(new LogEntry("Sleep 100ms", Colors.Yellow)); Thread.Sleep(100); s++; continue; } data.Log(new LogEntry("Message sent successfully!", Colors.LimeGreen)); break; } } break; default: break; } } catch (Exception ex) { data.Log(new LogEntry(ex.Message, Colors.Tomato)); if (ex.InnerException != null) { data.Log(new LogEntry(ex.InnerException.Message, Colors.Tomato)); } } }