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
|
|
|
|
2018-09-09 17:39:56 +00:00
|
|
|
REGEX_KARMA = re.compile("^(.*[^-+])[-+]*(\+{2,}|\-{2,})$")
|
2019-01-25 23:45:44 +00:00
|
|
|
WORD_STOP = [",", ":"]
|
2016-03-29 11:56:58 +00:00
|
|
|
KARMA_DELAY_SECONDS = 3
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "karma-verbose",
|
2018-10-02 13:29:32 +00:00
|
|
|
"help": "Enable/disable automatically responding to karma changes",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("serverset", {"setting": "karma-nickname-only",
|
2018-10-02 13:29:32 +00:00
|
|
|
"help": "Enable/disable karma being for nicknames only",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
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-01-11 17:33:43 +00:00
|
|
|
return str(karma)
|
|
|
|
|
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):
|
|
|
|
event["user"].last_karma = None
|
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
@utils.hook("command.regex")
|
2016-03-29 11:56:58 +00:00
|
|
|
def channel_message(self, event):
|
2019-05-18 17:35:47 +00:00
|
|
|
"""
|
|
|
|
:command: karma
|
|
|
|
:pattern: ^(.*[^-+])[-+]*(\+{2,}|\-{2,})$
|
|
|
|
"""
|
|
|
|
verbose = event["target"].get_setting("karma-verbose", False)
|
|
|
|
nickname_only = event["server"].get_setting("karma-nickname-only",
|
|
|
|
False)
|
2019-04-27 13:51:26 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
if not event["user"].last_karma or (time.time()-event["user"
|
|
|
|
].last_karma) >= KARMA_DELAY_SECONDS:
|
|
|
|
target = event["match"].group(1).strip().rstrip("".join(WORD_STOP))
|
2019-05-21 15:48:10 +00:00
|
|
|
if not target:
|
|
|
|
return
|
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
if event["server"].irc_lower(target) == event["user"].name:
|
|
|
|
if verbose:
|
2019-05-20 13:14:44 +00:00
|
|
|
event["stderr"].write("You cannot change your own karma")
|
2019-05-13 19:57:47 +00:00
|
|
|
return
|
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
setting = "karma-%s" % target
|
|
|
|
setting_target = event["server"]
|
|
|
|
if nickname_only:
|
|
|
|
user = event["server"].get_user(target)
|
|
|
|
setting = "karma"
|
|
|
|
setting_target = user
|
|
|
|
if not event["target"].has_user(user):
|
2018-10-02 13:29:32 +00:00
|
|
|
return
|
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
positive = event["match"].group(2)[0] == "+"
|
|
|
|
karma = setting_target.get_setting(setting, 0)
|
|
|
|
karma += 1 if positive else -1
|
2018-10-02 13:29:32 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
if not karma == 0:
|
|
|
|
setting_target.set_setting(setting, karma)
|
|
|
|
else:
|
|
|
|
setting_target.del_setting(setting)
|
2018-10-02 13:29:32 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
karma_str = self._karma_str(karma)
|
|
|
|
if verbose:
|
|
|
|
event["stdout"].write(
|
|
|
|
"%s now has %s karma" % (target, karma_str))
|
|
|
|
event["user"].last_karma = time.time()
|
|
|
|
elif verbose:
|
|
|
|
event["stderr"].write("Try again in a couple of seconds")
|
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
|
2018-10-02 14:14:42 +00:00
|
|
|
target = target.strip()
|
2018-10-02 13:29:32 +00:00
|
|
|
|
|
|
|
if event["server"].get_setting("karma-nickname-only", False):
|
|
|
|
karma = event["server"].get_user(target).get_setting("karma", 0)
|
|
|
|
else:
|
|
|
|
karma = event["server"].get_setting("karma-%s" % target, 0)
|
2019-01-11 17:33:43 +00:00
|
|
|
karma_str = self._karma_str(karma)
|
|
|
|
event["stdout"].write("%s has %s karma" % (target, karma_str))
|
2018-08-18 17:28:27 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.resetkarma", min_args=1)
|
2018-08-18 17:28:27 +00:00
|
|
|
def reset_karma(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Reset a specified karma to 0
|
|
|
|
:usage: <target>
|
|
|
|
:permission: resetkarme
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-08-18 17:28:27 +00:00
|
|
|
setting = "karma-%s" % event["args_split"][0]
|
|
|
|
karma = event["server"].get_setting(setting, 0)
|
|
|
|
if karma == 0:
|
|
|
|
event["stderr"].write("%s already has 0 karma" % event[
|
|
|
|
"args_split"][0])
|
|
|
|
else:
|
|
|
|
event["server"].del_setting(setting)
|
|
|
|
event["stdout"].write("Reset karma for %s" % event[
|
|
|
|
"args_split"][0])
|