regex.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if status:
  70. return True
  71. else:
  72. return False
  73. def separate_sed(sed_string):
  74. if (
  75. len(sed_string) >= 3
  76. and sed_string[1] in DELIMITERS
  77. and sed_string.count(sed_string[1]) >= 2
  78. ):
  79. delim = sed_string[1]
  80. start = counter = 2
  81. while counter < len(sed_string):
  82. if sed_string[counter] == "\\":
  83. counter += 1
  84. elif sed_string[counter] == delim:
  85. replace = sed_string[start:counter]
  86. counter += 1
  87. start = counter
  88. break
  89. counter += 1
  90. else:
  91. return None
  92. while counter < len(sed_string):
  93. if (
  94. sed_string[counter] == "\\"
  95. and counter + 1 < len(sed_string)
  96. and sed_string[counter + 1] == delim
  97. ):
  98. sed_string = (
  99. sed_string[:counter] + sed_string[counter + 1 :]
  100. )
  101. elif sed_string[counter] == delim:
  102. replace_with = sed_string[start:counter]
  103. counter += 1
  104. break
  105. counter += 1
  106. else:
  107. return replace, sed_string[start:], ""
  108. flags = ""
  109. if counter < len(sed_string):
  110. flags = sed_string[counter:]
  111. return replace, replace_with, flags.lower()