2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
import re, time
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import EventManager, ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
KARMA_DELAY_SECONDS = 3
|
|
|
|
|
2019-12-19 15:06:15 +00:00
|
|
|
REGEX_WORD = re.compile(r"^([^(\s,:]+)(?:[:,])?\s*(\+\+|--)\s*$")
|
2019-12-10 10:51:37 +00:00
|
|
|
REGEX_WORD_START = re.compile(r"^(\+\+|--)(?:\s*)([^(\s,:]+)\s*$")
|
2019-10-08 13:34:14 +00:00
|
|
|
REGEX_PARENS = re.compile(r"\(([^)]+)\)(\+\+|--)")
|
2019-06-26 13:37:26 +00:00
|
|
|
|
2019-09-12 11:09:16 +00:00
|
|
|
@utils.export("channelset", utils.BoolSetting("karma-pattern",
|
|
|
|
"Enable/disable parsing ++/-- karma format"))
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2021-01-11 17:04:57 +00:00
|
|
|
def listify(self, items):
|
|
|
|
if type(items) != list:
|
|
|
|
items = list(items)
|
2021-02-06 00:38:27 +00:00
|
|
|
listified = ""
|
|
|
|
if len(items) > 2:
|
|
|
|
listified = ', '.join(items[:-1]) + ', and ' + items[-1]
|
|
|
|
elif len(items) > 1:
|
|
|
|
listified = items[0] + ' and ' + items[1]
|
|
|
|
elif items:
|
|
|
|
listified = items[0]
|
|
|
|
return listified
|
2021-01-11 17:04:57 +00:00
|
|
|
|
2019-01-11 17:33:43 +00:00
|
|
|
def _karma_str(self, karma):
|
|
|
|
karma_str = str(karma)
|
|
|
|
if karma < 0:
|
|
|
|
return utils.irc.color(str(karma), utils.consts.RED)
|
|
|
|
elif karma > 0:
|
2019-01-11 21:00:09 +00:00
|
|
|
return utils.irc.color(str(karma), utils.consts.LIGHTGREEN)
|
2019-10-12 06:23:58 +00:00
|
|
|
else:
|
|
|
|
return utils.irc.color(str(karma), utils.consts.YELLOW)
|
2019-01-11 17:33:43 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("new.user")
|
2016-03-29 11:56:58 +00:00
|
|
|
def new_user(self, event):
|
2019-08-12 12:38:14 +00:00
|
|
|
event["user"]._last_positive_karma = None
|
|
|
|
event["user"]._last_negative_karma = None
|
|
|
|
|
|
|
|
def _check_throttle(self, user, positive):
|
|
|
|
timestamp = None
|
|
|
|
if positive:
|
|
|
|
timestamp = user._last_positive_karma
|
|
|
|
else:
|
|
|
|
timestamp = user._last_negative_karma
|
|
|
|
return timestamp == None or (time.time()-timestamp
|
|
|
|
) >= KARMA_DELAY_SECONDS
|
|
|
|
def _set_throttle(self, user, positive):
|
|
|
|
if positive:
|
|
|
|
user._last_positive_karma = time.time()
|
|
|
|
else:
|
|
|
|
user._last_negative_karma = time.time()
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-12-01 08:49:59 +00:00
|
|
|
def _get_target(self, server, target):
|
|
|
|
target = target.strip()
|
|
|
|
if not " " in target and server.has_user(target):
|
|
|
|
return server.get_user_nickname(server.get_user(target).get_id())
|
|
|
|
return target.lower()
|
|
|
|
|
2019-12-01 07:06:30 +00:00
|
|
|
def _change_karma(self, server, sender, target, positive):
|
|
|
|
if not self._check_throttle(sender, positive):
|
|
|
|
return False, "Try again in a couple of seconds"
|
2019-04-27 13:51:26 +00:00
|
|
|
|
2019-12-01 10:32:40 +00:00
|
|
|
target = self._get_target(server, target)
|
2019-05-13 19:57:47 +00:00
|
|
|
|
2019-12-01 07:06:30 +00:00
|
|
|
setting = "karma-%s" % target
|
|
|
|
karma = sender.get_setting(setting, 0)
|
|
|
|
karma += 1 if positive else -1
|
2018-10-02 13:29:32 +00:00
|
|
|
|
2019-12-01 07:06:30 +00:00
|
|
|
if karma == 0:
|
|
|
|
sender.del_setting(setting)
|
2019-08-15 12:38:04 +00:00
|
|
|
else:
|
2019-12-01 07:06:30 +00:00
|
|
|
sender.set_setting(setting, karma)
|
|
|
|
|
|
|
|
self._set_throttle(sender, positive)
|
|
|
|
karma_str = self._karma_str(karma)
|
2019-12-01 10:46:07 +00:00
|
|
|
|
2020-09-29 15:06:37 +00:00
|
|
|
karma_total = sum(self._get_karma(server, target).values())
|
|
|
|
karma_total = self._karma_str(karma_total)
|
2019-12-05 17:20:37 +00:00
|
|
|
|
2020-01-02 22:28:39 +00:00
|
|
|
return True, "%s now has %s karma (%s from %s)" % (
|
|
|
|
target, karma_total, karma_str, sender.nickname)
|
2019-08-15 12:38:04 +00:00
|
|
|
|
2019-10-03 12:20:03 +00:00
|
|
|
@utils.hook("command.regex", pattern=REGEX_WORD)
|
|
|
|
@utils.hook("command.regex", pattern=REGEX_PARENS)
|
2019-08-15 12:38:04 +00:00
|
|
|
@utils.kwarg("command", "karma")
|
2020-01-17 11:18:00 +00:00
|
|
|
@utils.kwarg("priority", EventManager.PRIORITY_HIGH)
|
|
|
|
# high priority to make `++asd++` count as `++` on `++help`
|
2019-10-03 12:20:03 +00:00
|
|
|
def regex_word(self, event):
|
|
|
|
if event["target"].get_setting("karma-pattern", False):
|
2020-01-17 11:18:00 +00:00
|
|
|
event.eat()
|
|
|
|
|
2019-10-03 12:20:03 +00:00
|
|
|
target = event["match"].group(1)
|
|
|
|
positive = event["match"].group(2)=="++"
|
2019-12-01 07:06:30 +00:00
|
|
|
success, message = self._change_karma(
|
|
|
|
event["server"], event["user"], target, positive)
|
2019-09-26 10:50:10 +00:00
|
|
|
event["stdout" if success else "stderr"].write(message)
|
2020-01-17 11:18:00 +00:00
|
|
|
|
2019-12-10 10:49:12 +00:00
|
|
|
@utils.hook("command.regex", pattern=REGEX_WORD_START)
|
|
|
|
@utils.kwarg("command", "karma")
|
|
|
|
def regex_word_start(self, event):
|
|
|
|
if event["target"].get_setting("karma-pattern", False):
|
|
|
|
target = event["match"].group(2)
|
|
|
|
positive = event["match"].group(1)=="++"
|
|
|
|
success, message = self._change_karma(
|
|
|
|
event["server"], event["user"], target, positive)
|
|
|
|
event["stdout" if success else "stderr"].write(message)
|
2019-10-03 12:20:03 +00:00
|
|
|
|
2019-08-15 12:38:04 +00:00
|
|
|
@utils.hook("received.command.addpoint")
|
|
|
|
@utils.hook("received.command.rmpoint")
|
|
|
|
@utils.kwarg("min_args", 1)
|
2020-01-26 02:59:16 +00:00
|
|
|
@utils.spec("!<target>string")
|
2019-08-15 12:38:04 +00:00
|
|
|
def changepoint(self, event):
|
2019-09-10 11:59:52 +00:00
|
|
|
positive = event["command"] == "addpoint"
|
2019-12-01 07:06:30 +00:00
|
|
|
success, message = self._change_karma(
|
2020-01-26 02:59:16 +00:00
|
|
|
event["server"], event["user"], event["spec"][0], positive)
|
2019-08-15 12:38:04 +00:00
|
|
|
event["stdout" if success else "stderr"].write(message)
|
2018-09-09 16:34:44 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.karma")
|
2016-03-29 11:56:58 +00:00
|
|
|
def karma(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get your or someone else's karma
|
|
|
|
:usage: [target]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["args"]:
|
|
|
|
target = event["args"]
|
|
|
|
else:
|
|
|
|
target = event["user"].nickname
|
2019-12-05 17:20:37 +00:00
|
|
|
|
2019-12-01 08:49:59 +00:00
|
|
|
target = self._get_target(event["server"], target)
|
2020-09-29 15:06:37 +00:00
|
|
|
karma = sum(self._get_karma(event["server"], target).values())
|
|
|
|
karma = self._karma_str(karma)
|
2018-10-02 13:29:32 +00:00
|
|
|
|
2019-12-05 17:20:37 +00:00
|
|
|
event["stdout"].write("%s has %s karma" % (target, karma))
|
2018-08-18 17:28:27 +00:00
|
|
|
|
2020-09-29 15:06:37 +00:00
|
|
|
@utils.hook("received.command.karmawho")
|
|
|
|
@utils.spec("!<target>string")
|
|
|
|
def karmawho(self, event):
|
2021-01-11 17:12:46 +00:00
|
|
|
target = event["server"].irc_lower(event["spec"][0])
|
2020-09-29 15:06:37 +00:00
|
|
|
karma = self._get_karma(event["server"], target, True)
|
2020-09-29 15:36:43 +00:00
|
|
|
karma = sorted(list(karma.items()),
|
|
|
|
key=lambda k: abs(k[1]),
|
|
|
|
reverse=True)
|
2020-09-29 15:06:37 +00:00
|
|
|
|
|
|
|
parts = ["%s (%d)" % (n, v) for n, v in karma]
|
2021-01-11 17:04:57 +00:00
|
|
|
if len(parts) == 0:
|
|
|
|
event["stdout"].write("%s has no karma." % target)
|
|
|
|
return
|
2020-09-29 15:06:37 +00:00
|
|
|
event["stdout"].write("%s has karma from: %s" %
|
2021-01-11 17:04:57 +00:00
|
|
|
(target, self.listify(parts)))
|
2020-09-29 15:06:37 +00:00
|
|
|
|
|
|
|
def _get_karma(self, server, target, own=False):
|
2019-12-05 17:20:37 +00:00
|
|
|
settings = dict(server.get_all_user_settings("karma-%s" % target))
|
|
|
|
|
|
|
|
target_lower = server.irc_lower(target)
|
2020-09-29 15:06:37 +00:00
|
|
|
if target_lower in settings and not own:
|
2019-12-01 07:06:30 +00:00
|
|
|
del settings[target_lower]
|
|
|
|
|
2020-09-29 15:06:37 +00:00
|
|
|
return settings
|
2019-12-01 07:06:30 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.resetkarma")
|
|
|
|
@utils.kwarg("min_args", 2)
|
|
|
|
@utils.kwarg("help", "Reset a specific karma to 0")
|
|
|
|
@utils.kwarg("permission", "resetkarma")
|
2020-01-26 02:59:16 +00:00
|
|
|
@utils.spec("!'by !<nickname>ouser")
|
|
|
|
@utils.spec("!'for !<target>string")
|
2018-08-18 17:28:27 +00:00
|
|
|
def reset_karma(self, event):
|
2020-01-26 02:59:16 +00:00
|
|
|
subcommand = event["spec"][0]
|
2019-12-01 07:06:30 +00:00
|
|
|
|
|
|
|
if subcommand == "by":
|
2020-01-26 02:59:16 +00:00
|
|
|
target_user = event["spec"][1]
|
2019-12-01 07:06:30 +00:00
|
|
|
karma = target_user.find_setting(prefix="karma-")
|
|
|
|
for setting, _ in karma:
|
|
|
|
target_user.del_setting(setting)
|
|
|
|
|
|
|
|
if karma:
|
|
|
|
event["stdout"].write("Cleared karma by %s" %
|
|
|
|
target_user.nickname)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No karma to clear by %s" %
|
|
|
|
target_user.nickname)
|
|
|
|
elif subcommand == "for":
|
2020-01-26 02:59:16 +00:00
|
|
|
setting = "karma-%s" % event["spec"][1]
|
2019-12-01 07:06:30 +00:00
|
|
|
karma = event["server"].get_all_user_settings(setting)
|
|
|
|
for nickname, value in karma:
|
|
|
|
user = event["server"].get_user(nickname)
|
|
|
|
user.del_setting(setting)
|
|
|
|
|
|
|
|
if karma:
|
2020-01-26 02:59:16 +00:00
|
|
|
event["stdout"].write("Cleared karma for %s" % event["spec"][1])
|
2019-12-01 07:06:30 +00:00
|
|
|
else:
|
2020-01-26 02:59:16 +00:00
|
|
|
event["stderr"].write("No karma to clear for %s"
|
|
|
|
% event["spec"][1])
|
2018-08-18 17:28:27 +00:00
|
|
|
else:
|
2019-12-01 07:06:30 +00:00
|
|
|
raise utils.EventError("Unknown subcommand '%s'" % subcommand)
|