regex.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(repl, repl_with, to_fix, count=1, flags=re.I).strip()
  40. elif "g" in flags:
  41. text = re.sub(repl, repl_with, to_fix).strip()
  42. else:
  43. text = re.sub(repl, repl_with, to_fix, count=1).strip()
  44. except sre_constants.error:
  45. return
  46. # empty string errors -_-
  47. if len(text) >= 4096:
  48. await message.reply_text(
  49. "The result of the sed command was too long for \
  50. telegram!"
  51. )
  52. elif text:
  53. await message.reply_to_message.reply_text(text)
  54. def infinite_checker(repl):
  55. regex = [
  56. r"\((.{1,}[\+\*]){1,}\)[\+\*].",
  57. r"[\(\[].{1,}\{\d(,)?\}[\)\]]\{\d(,)?\}",
  58. r"\(.{1,}\)\{.{1,}(,)?\}\(.*\)(\+|\* |\{.*\})",
  59. ]
  60. for match in regex:
  61. status = re.search(match, repl)
  62. return bool(status)
  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()