regex.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # https://github.com/PaulSonOfLars/tgbot/blob/master/tg_bot/modules/sed.py
  2. import re
  3. import sre_constants
  4. from pyrogram import filters
  5. from wbb import app
  6. from wbb.utils.filter_groups import regex_group
  7. __MODULE__ = "Sed"
  8. __HELP__ = "**Usage:**\ns/foo/bar"
  9. DELIMITERS = ("/", ":", "|", "_")
  10. @app.on_message(
  11. filters.regex(r"s([{}]).*?\1.*".format("".join(DELIMITERS))),
  12. group=regex_group,
  13. )
  14. async def sed(_, message):
  15. if not message.text:
  16. return
  17. sed_result = separate_sed(message.text)
  18. if message.reply_to_message:
  19. if message.reply_to_message.text:
  20. to_fix = message.reply_to_message.text
  21. elif message.reply_to_message.caption:
  22. to_fix = message.reply_to_message.caption
  23. else:
  24. return
  25. try:
  26. repl, repl_with, flags = sed_result
  27. except Exception:
  28. return
  29. if not repl:
  30. return await message.reply_text(
  31. "You're trying to replace... "
  32. "nothing with something?"
  33. )
  34. try:
  35. if infinite_checker(repl):
  36. return await message.reply_text("Nice try -_-")
  37. if "i" in flags and "g" in flags:
  38. text = re.sub(
  39. repl, repl_with, to_fix, flags=re.I
  40. ).strip()
  41. elif "i" in flags:
  42. text = re.sub(
  43. repl, repl_with, to_fix, count=1, flags=re.I
  44. ).strip()
  45. elif "g" in flags:
  46. text = re.sub(repl, repl_with, to_fix).strip()
  47. else:
  48. text = re.sub(
  49. repl, repl_with, to_fix, count=1
  50. ).strip()
  51. except sre_constants.error:
  52. return
  53. # empty string errors -_-
  54. if len(text) >= 4096:
  55. await message.reply_text(
  56. "The result of the sed command was too long for \
  57. telegram!"
  58. )
  59. elif text:
  60. await message.reply_to_message.reply_text(text)
  61. def infinite_checker(repl):
  62. regex = [
  63. r"\((.{1,}[\+\*]){1,}\)[\+\*].",
  64. r"[\(\[].{1,}\{\d(,)?\}[\)\]]\{\d(,)?\}",
  65. r"\(.{1,}\)\{.{1,}(,)?\}\(.*\)(\+|\* |\{.*\})",
  66. ]
  67. for match in regex:
  68. status = re.search(match, repl)
  69. return bool(status)
  70. def separate_sed(sed_string):
  71. if (
  72. len(sed_string) >= 3
  73. and sed_string[1] in DELIMITERS
  74. and sed_string.count(sed_string[1]) >= 2
  75. ):
  76. delim = sed_string[1]
  77. start = counter = 2
  78. while counter < len(sed_string):
  79. if sed_string[counter] == "\\":
  80. counter += 1
  81. elif sed_string[counter] == delim:
  82. replace = sed_string[start:counter]
  83. counter += 1
  84. start = counter
  85. break
  86. counter += 1
  87. else:
  88. return None
  89. while counter < len(sed_string):
  90. if (
  91. sed_string[counter] == "\\"
  92. and counter + 1 < len(sed_string)
  93. and sed_string[counter + 1] == delim
  94. ):
  95. sed_string = (
  96. sed_string[:counter] + sed_string[counter + 1 :]
  97. )
  98. elif sed_string[counter] == delim:
  99. replace_with = sed_string[start:counter]
  100. counter += 1
  101. break
  102. counter += 1
  103. else:
  104. return replace, sed_string[start:], ""
  105. flags = ""
  106. if counter < len(sed_string):
  107. flags = sed_string[counter:]
  108. return replace, replace_with, flags.lower()