regex.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. text_content = str(message.text)
  18. sed_result = separate_sed(text_content)
  19. if message.reply_to_message:
  20. if message.reply_to_message.text:
  21. to_fix = message.reply_to_message.text
  22. elif message.reply_to_message.caption:
  23. to_fix = message.reply_to_message.caption
  24. else:
  25. return
  26. try:
  27. repl, repl_with, flags = sed_result
  28. except Exception:
  29. return
  30. if not repl:
  31. return await message.reply_text(
  32. "You're trying to replace... " "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(repl, repl_with, to_fix, flags=re.I).strip()
  39. elif "i" in flags:
  40. text = re.sub(
  41. repl, repl_with, to_fix, count=1, flags=re.I
  42. ).strip()
  43. elif "g" in flags:
  44. text = re.sub(repl, repl_with, to_fix).strip()
  45. else:
  46. text = re.sub(repl, repl_with, to_fix, count=1).strip()
  47. except sre_constants.error:
  48. return
  49. # empty string errors -_-
  50. if len(text) >= 4096:
  51. await message.reply_text(
  52. "The result of the sed command was too long for \
  53. telegram!"
  54. )
  55. elif text:
  56. await message.reply_to_message.reply_text(text)
  57. def infinite_checker(repl):
  58. regex = [
  59. r"\((.{1,}[\+\*]){1,}\)[\+\*].",
  60. r"[\(\[].{1,}\{\d(,)?\}[\)\]]\{\d(,)?\}",
  61. r"\(.{1,}\)\{.{1,}(,)?\}\(.*\)(\+|\* |\{.*\})",
  62. ]
  63. for match in regex:
  64. status = re.search(match, repl)
  65. return bool(status)
  66. def separate_sed(sed_string):
  67. if not isinstance(sed_string, str):
  68. sed_string = str(sed_string)
  69. if (
  70. len(sed_string) >= 3
  71. and sed_string[1] in DELIMITERS
  72. and sed_string.count(sed_string[1]) >= 2
  73. ):
  74. delim = sed_string[1]
  75. start = counter = 2
  76. while counter < len(sed_string):
  77. if sed_string[counter] == "\\":
  78. counter += 1
  79. elif sed_string[counter] == delim:
  80. replace = sed_string[start:counter]
  81. counter += 1
  82. start = counter
  83. break
  84. counter += 1
  85. else:
  86. return None
  87. while counter < len(sed_string):
  88. if (
  89. sed_string[counter] == "\\"
  90. and counter + 1 < len(sed_string)
  91. and sed_string[counter + 1] == delim
  92. ):
  93. sed_string = sed_string[:counter] + sed_string[counter + 1 :]
  94. elif sed_string[counter] == delim:
  95. replace_with = sed_string[start:counter]
  96. counter += 1
  97. break
  98. counter += 1
  99. else:
  100. return replace, sed_string[start:], ""
  101. flags = ""
  102. if counter < len(sed_string):
  103. flags = sed_string[counter:]
  104. return replace, replace_with, flags.lower()