public.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from common import userstatus, queries
  2. from commands.common import CommandHelpers as CH
  3. bold = "\x02"
  4. italic = "\x1D"
  5. underline = "\x1F"
  6. reverse = "\x16" # swap background and foreground colors ("reverse video")
  7. reset = "\x0F"
  8. blue = "\x0302"
  9. green = "\x0303"
  10. red = "\x0304"
  11. grey = "\x0314"
  12. def do_command(self, connection, event):
  13. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  14. # Do nothing if there is no command.
  15. if not command:
  16. return
  17. try:
  18. command.split()[0]
  19. except:
  20. return
  21. # The actual commands:
  22. if command == "test":
  23. if cmdtype == "help": #Display help text.
  24. if len(command.split()) is not 1:
  25. return
  26. connection.privmsg(replyto, "Strictly for testing purposes only!")
  27. elif cmdtype == "cmd":
  28. connection.privmsg(replyto, str(self.channels[self.homechannel].owners()))
  29. elif command == "cmd" or command == "cmds" or command == "commands":
  30. if cmdtype == "help": #Display help text.
  31. if len(command.split()) is not 1:
  32. return
  33. connection.privmsg(replyto, "Displays a list of commands.")
  34. elif cmdtype == "cmd":
  35. if not event.target == connection.get_nickname() and not self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'"):
  36. connection.privmsg(replyto, grey + "Commands: " + CH.ccc(self, "cmd") + CH.ccc(self, "help")[:-2] + ".")
  37. else:
  38. connection.privmsg(replyto, grey + "Commands: " + CH.ccc(self, "cmd") + CH.ccc(self, "help") + CH.ccc(self, "away") + CH.ccc(self, "stopgreet")[:-2] + ".")
  39. elif command == "help":
  40. if cmdtype == "help": #Display help text.
  41. if len(command.split()) is not 1:
  42. return
  43. connection.privmsg(replyto, "Explains how to get help on any specific command and hints the user to the commandlist.")
  44. elif cmdtype == "cmd":
  45. connection.privmsg(replyto, "Replace the " + italic + "! " + reset + "prefix of any comand with " + italic + self.helpchar + " " + reset + "for help with a specific command. Request the command list with: " + blue + "!cmd")
  46. connection.privmsg(replyto, grey + "Example: " + reset + blue + self.helpchar + "help")
  47. elif command.split()[0] == "stopgreet":
  48. if cmdtype == "help": #Display help text.
  49. if len(command.split()) is not 1:
  50. return
  51. connection.privmsg(replyto, "Stops the bot from greeting you or a specific user. Channel, user and option to resume optional.")
  52. connection.privmsg(replyto, grey + "Usage: " + blue + "!stopgreet " + reset + italic + "resume " + red + "channel user")
  53. elif cmdtype == "cmd":
  54. # Check for resume variation of command.
  55. resume = False
  56. try:
  57. if command.split()[1] == "resume":
  58. resume = True
  59. command = command.split(' ', 1)[1] # Remove the resume argument. Which makes the following logic easier.
  60. except:
  61. pass
  62. if len(command.split()) == 1: # No arguments.
  63. if event.target == connection.get_nickname(): # PM.
  64. connection.privmsg(replyto, "Specify at least a channel. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  65. return
  66. user = event.source.nick
  67. channel = event.target
  68. if len(command.split()) == 2: # One argument.
  69. if command.split()[1] not in self.channels: # Argument is not a channel the bot inhabits.
  70. if event.target == connection.get_nickname(): # PM.
  71. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  72. return
  73. # Channel message
  74. if not userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not userstatus.atleast_halfop(self, event.source.nick, event.target): # Insufficient rights.
  75. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + event.target + reset + ".")
  76. return
  77. # Stopgreet for x user in current channel
  78. user = command.split()[1]
  79. channel = event.target
  80. else: # Bot inhabit channel.
  81. user = event.source.nick
  82. channel = command.split()[1]
  83. if len(command.split()) == 3: # Two arguments.
  84. if command.split()[1] not in self.channels: # Bot does not inhabit channel.
  85. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  86. return
  87. if not userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not userstatus.atleast_halfop(self, event.source.nick, command.split()[1]): # Insufficient rights.
  88. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + command.split()[1] + reset + ".")
  89. return
  90. user = command.split()[2]
  91. channel = command.split()[1]
  92. if len(command.split()) > 3: # Too many arguments.
  93. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  94. return
  95. # Check for database record.
  96. result = self.db.one("SELECT id, stopgreet FROM joins WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "' AND LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  97. if not result: # No record in database.
  98. connection.action(replyto, "has not yet had the pleasure of greeting " + red + user + reset + " in " + red + channel + reset + ".")
  99. return
  100. if resume:
  101. stopgreet = False
  102. message = "has already every intention to greet "
  103. else:
  104. stopgreet = True
  105. message = "has already stopped greeting "
  106. if result[1] == stopgreet:
  107. connection.action(replyto, message + red + user + reset + " in " + red + channel + reset + ".")
  108. return
  109. print(str(stopgreet) + str(user) + str(channel))
  110. try:
  111. self.db.run("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "' AND lower(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  112. except:
  113. connection.privmsg(replyto, "Failed to update database.")
  114. elif command.split()[0] == "away":
  115. if cmdtype == "help": #Display help text.
  116. if len(command.split()) is not 1:
  117. return
  118. connection.privmsg(replyto, "Sets you away, optionally with reason. Affects the !seen command.")
  119. connection.privmsg(replyto, grey + "Usage: " + blue + "!away " + reset + italic + "reason")
  120. elif cmdtype == "cmd":
  121. queries.create_ifnot_onrecord(self, event.source.nick)
  122. if len(trigger.split()) == 1:
  123. self.db.run("UPDATE users SET away=TRUE, away_reason=NULL WHERE name='" + event.source.nick + "' AND network='" + self.network + "'")
  124. else:
  125. self.db.run("UPDATE users SET away=TRUE, away_reason=%s WHERE name='" + event.source.nick + "' AND network='" + self.network + "'", (trigger.split(maxsplit=1)[1], ))