regex.py 3.6 KB

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