regex.py 3.4 KB

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