regex.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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... " "nothing with something?"
  32. )
  33. try:
  34. if infinite_checker(repl):
  35. return await message.reply_text("Nice try -_-")
  36. if "i" in flags and "g" in flags:
  37. text = re.sub(repl, repl_with, to_fix, flags=re.I).strip()
  38. elif "i" in flags:
  39. text = re.sub(
  40. repl, repl_with, to_fix, count=1, flags=re.I
  41. ).strip()
  42. elif "g" in flags:
  43. text = re.sub(repl, repl_with, to_fix).strip()
  44. else:
  45. text = re.sub(repl, repl_with, to_fix, count=1).strip()
  46. except sre_constants.error:
  47. return
  48. # empty string errors -_-
  49. if len(text) >= 4096:
  50. await message.reply_text(
  51. "The result of the sed command was too long for \
  52. telegram!"
  53. )
  54. elif text:
  55. await message.reply_to_message.reply_text(text)
  56. def infinite_checker(repl):
  57. regex = [
  58. r"\((.{1,}[\+\*]){1,}\)[\+\*].",
  59. r"[\(\[].{1,}\{\d(,)?\}[\)\]]\{\d(,)?\}",
  60. r"\(.{1,}\)\{.{1,}(,)?\}\(.*\)(\+|\* |\{.*\})",
  61. ]
  62. for match in regex:
  63. status = re.search(match, repl)
  64. return bool(status)
  65. def separate_sed(sed_string):
  66. if (
  67. len(sed_string) >= 3
  68. and sed_string[1] in DELIMITERS
  69. and sed_string.count(sed_string[1]) >= 2
  70. ):
  71. delim = sed_string[1]
  72. start = counter = 2
  73. while counter < len(sed_string):
  74. if sed_string[counter] == "\\":
  75. counter += 1
  76. elif sed_string[counter] == delim:
  77. replace = sed_string[start:counter]
  78. counter += 1
  79. start = counter
  80. break
  81. counter += 1
  82. else:
  83. return None
  84. while counter < len(sed_string):
  85. if (
  86. sed_string[counter] == "\\"
  87. and counter + 1 < len(sed_string)
  88. and sed_string[counter + 1] == delim
  89. ):
  90. sed_string = sed_string[:counter] + sed_string[counter + 1 :]
  91. elif sed_string[counter] == delim:
  92. replace_with = sed_string[start:counter]
  93. counter += 1
  94. break
  95. counter += 1
  96. else:
  97. return replace, sed_string[start:], ""
  98. flags = ""
  99. if counter < len(sed_string):
  100. flags = sed_string[counter:]
  101. return replace, replace_with, flags.lower()