regex.py 3.4 KB

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