2016-03-29 11:56:58 +00:00
|
|
|
import re, time
|
2018-09-27 11:08:07 +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,})$")
|
2016-03-29 11:56:58 +00:00
|
|
|
KARMA_DELAY_SECONDS = 3
|
|
|
|
|
2018-09-27 11:08:07 +00:00
|
|
|
@Utils.export("channelset", {"setting": "karma-verbose",
|
2018-10-02 13:29:32 +00:00
|
|
|
"help": "Enable/disable automatically responding to karma changes",
|
|
|
|
"validate": Utils.bool_or_none})
|
|
|
|
@Utils.export("serverset", {"setting": "karma-nickname-only",
|
|
|
|
"help": "Enable/disable karma being for nicknames only",
|
2018-09-27 11:08:07 +00:00
|
|
|
"validate": Utils.bool_or_none})
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("new.user")
|
2016-03-29 11:56:58 +00:00
|
|
|
def new_user(self, event):
|
|
|
|
event["user"].last_karma = None
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.message.channel",
|
|
|
|
priority=EventManager.PRIORITY_MONITOR)
|
2016-03-29 11:56:58 +00:00
|
|
|
def channel_message(self, event):
|
2016-04-20 23:09:30 +00:00
|
|
|
match = re.match(REGEX_KARMA, event["message"].strip())
|
2016-06-24 12:10:36 +00:00
|
|
|
if match and not event["action"]:
|
2016-05-01 21:47:59 +00:00
|
|
|
verbose = event["channel"].get_setting("karma-verbose", False)
|
2018-10-02 13:29:32 +00:00
|
|
|
nickname_only = event["server"].get_setting("karma-nickname-only",
|
|
|
|
False)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
if not event["user"].last_karma or (time.time()-event["user"
|
|
|
|
].last_karma) >= KARMA_DELAY_SECONDS:
|
2018-10-02 13:29:32 +00:00
|
|
|
target = match.group(1).strip()
|
2018-10-02 14:14:06 +00:00
|
|
|
if Utils.irc_lower(event["server"], target
|
|
|
|
) == event["user"].name:
|
2016-04-06 11:02:44 +00:00
|
|
|
if verbose:
|
2018-09-09 16:34:44 +00:00
|
|
|
self.events.on("send.stderr").call(
|
|
|
|
module_name="Karma", target=event["channel"],
|
|
|
|
message="You cannot change your own karma")
|
2018-10-02 13:29:32 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
setting = "karma-%s" % target
|
|
|
|
setting_target = event["server"]
|
|
|
|
if nickname_only:
|
|
|
|
user = event["server"].get_user(target)
|
2018-10-02 14:14:34 +00:00
|
|
|
setting = "karma"
|
2018-10-02 13:29:32 +00:00
|
|
|
setting_target = user
|
|
|
|
if not event["channel"].has_user(user):
|
|
|
|
return
|
|
|
|
|
|
|
|
positive = match.group(2)[0] == "+"
|
2018-10-02 14:14:34 +00:00
|
|
|
karma = setting_target.get_setting(setting, 0)
|
2018-10-02 13:29:32 +00:00
|
|
|
karma += 1 if positive else -1
|
|
|
|
|
|
|
|
if karma:
|
2018-10-02 14:14:34 +00:00
|
|
|
setting_target.set_setting(setting, karma)
|
2018-10-02 13:29:32 +00:00
|
|
|
else:
|
2018-10-02 14:14:34 +00:00
|
|
|
setting_target.del_setting(setting)
|
2018-10-02 13:29:32 +00:00
|
|
|
|
|
|
|
if verbose:
|
|
|
|
self.events.on("send.stdout").call(
|
|
|
|
module_name="Karma", target=event["channel"],
|
|
|
|
message="%s now has %d karma" % (target, karma))
|
|
|
|
event["user"].last_karma = time.time()
|
2016-04-20 23:09:30 +00:00
|
|
|
elif verbose:
|
2018-09-09 16:34:44 +00:00
|
|
|
self.events.on("send.stderr").call(module_name="Karma",
|
|
|
|
target=event["channel"],
|
|
|
|
message="Try again in a couple of seconds")
|
|
|
|
|
2018-09-30 16:29:09 +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 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)
|
2016-03-29 11:56:58 +00:00
|
|
|
event["stdout"].write("%s has %s karma" % (target, karma))
|
2018-08-18 17:28:27 +00:00
|
|
|
|
2018-09-30 16:29:09 +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])
|