2019-06-19 21:14:13 +00:00
|
|
|
#--depends-on check_mode
|
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2019-06-17 21:31:27 +00:00
|
|
|
import binascii, enum, os, uuid
|
2019-06-17 15:29:14 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
STR_NOVOTE = "Unknown vote '%s'"
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 21:31:27 +00:00
|
|
|
class VoteCastResult(enum.Enum):
|
|
|
|
Cast = 1
|
|
|
|
Changed = 2
|
2019-06-25 19:00:53 +00:00
|
|
|
@utils.export("channelset", {"setting": "votes-start-restricted",
|
2019-06-20 11:08:35 +00:00
|
|
|
"help": "Whether starting a vote should be restricted to ops",
|
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2019-06-17 15:29:14 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-06-17 16:51:42 +00:00
|
|
|
def _get_vote(self, channel, vote_id):
|
|
|
|
return channel.get_setting("vote-%s" % vote_id, None)
|
|
|
|
def _set_vote(self, channel, vote_id, vote):
|
|
|
|
channel.set_setting("vote-%s" % vote_id, vote)
|
2019-06-17 15:36:48 +00:00
|
|
|
|
2019-06-17 16:54:41 +00:00
|
|
|
def _random_id(self, channel):
|
|
|
|
while True:
|
2019-06-17 20:39:43 +00:00
|
|
|
vote_id = binascii.hexlify(os.urandom(3)).decode("ascii")
|
2019-06-17 16:54:41 +00:00
|
|
|
if self._get_vote(channel, vote_id) == None:
|
|
|
|
return vote_id
|
2019-06-17 16:51:42 +00:00
|
|
|
|
|
|
|
def _close_vote(self, channel, vote_id):
|
|
|
|
vote = self._get_vote(channel, vote_id)
|
|
|
|
if vote:
|
|
|
|
vote["open"] = False
|
|
|
|
self._set_vote(channel, vote_id, vote)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _start_vote(self, channel, description):
|
2019-06-17 16:54:41 +00:00
|
|
|
vote_id = self._random_id(channel)
|
2019-06-17 16:51:42 +00:00
|
|
|
vote = {"description": description, "options": {"yes": [], "no": []},
|
|
|
|
"electorate": [], "open": True, "id": vote_id}
|
|
|
|
self._set_vote(channel, vote_id, vote)
|
|
|
|
return vote
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 15:36:48 +00:00
|
|
|
def _format_vote(self, vote):
|
2019-06-17 16:51:42 +00:00
|
|
|
options = ["%d %s" % (len(v), k) for k, v in vote["options"].items()]
|
|
|
|
return "%s (%s)" % (vote["description"], ", ".join(options))
|
|
|
|
def _format_options(self, vote):
|
|
|
|
return ", ".join("'%s'" % o for o in vote["options"])
|
2019-06-17 15:36:48 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
def _cast_vote(self, channel, vote_id, user, option):
|
|
|
|
vote = self._get_vote(channel, vote_id)
|
|
|
|
option = vote["options"][option]
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 21:31:27 +00:00
|
|
|
cast_type = VoteCastResult.Cast
|
|
|
|
|
|
|
|
for nicks in vote["options"].values():
|
|
|
|
if user.name in nicks:
|
|
|
|
nicks.remove(user.name)
|
|
|
|
cast_type = VoteCastResult.Changed
|
|
|
|
break
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
option.append(user.name)
|
|
|
|
self._set_vote(channel, vote_id, vote)
|
2019-06-17 21:31:27 +00:00
|
|
|
return cast_type
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
def _open_votes(self, channel):
|
|
|
|
open = []
|
|
|
|
for setting, vote in channel.find_settings_prefix("vote-"):
|
|
|
|
if vote["open"]:
|
|
|
|
open.append(vote)
|
|
|
|
return open
|
|
|
|
|
2019-06-17 15:29:14 +00:00
|
|
|
@utils.hook("received.command.startvote", channel_only=True, min_args=1)
|
|
|
|
def start_vote(self, event):
|
|
|
|
"""
|
2019-06-17 17:08:02 +00:00
|
|
|
:help: Start a vote
|
2019-06-17 15:29:14 +00:00
|
|
|
:usage: <description>
|
|
|
|
"""
|
2019-06-20 11:08:35 +00:00
|
|
|
|
2019-06-25 19:00:53 +00:00
|
|
|
if event["target"].get_setting("votes-start-restricted", True):
|
2019-06-20 11:08:35 +00:00
|
|
|
event["check_assert"](utils.Check("channel-mode", "o")|
|
2019-06-20 11:13:55 +00:00
|
|
|
utils.Check("permission", "vote")|
|
|
|
|
utils.Check("channel-access", "vote"))
|
2019-06-20 11:08:35 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
vote = self._start_vote(event["target"], event["args"])
|
2019-06-17 15:29:14 +00:00
|
|
|
event["stdout"].write(
|
2019-06-17 16:51:42 +00:00
|
|
|
"Vote %s started. use '%svote <option>' to vote (options: %s)" %
|
|
|
|
(vote["id"], event["command_prefix"], self._format_options(vote)))
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
@utils.hook("received.command.endvote", channel_only=True, min_args=1)
|
2019-06-17 15:29:14 +00:00
|
|
|
def end_vote(self, event):
|
|
|
|
"""
|
2019-06-17 17:08:02 +00:00
|
|
|
:help: End a vote
|
2019-06-17 16:51:42 +00:00
|
|
|
:usage: <id>
|
2019-06-17 15:29:14 +00:00
|
|
|
:require_mode: o
|
2019-06-20 11:13:55 +00:00
|
|
|
:require_access: vote
|
2019-06-17 15:30:23 +00:00
|
|
|
:permission: vote
|
2019-06-17 15:29:14 +00:00
|
|
|
"""
|
2019-06-17 16:51:42 +00:00
|
|
|
vote_id = event["args_split"][0]
|
|
|
|
|
|
|
|
if self._close_vote(event["target"], vote_id):
|
|
|
|
vote = self._get_vote(event["target"], vote_id)
|
2019-06-17 15:36:48 +00:00
|
|
|
event["stdout"].write("Vote %s ended: %s" %
|
|
|
|
(vote_id, self._format_vote(vote)))
|
2019-06-17 16:51:42 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write(STR_NOVOTE % vote_id)
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
@utils.hook("received.command.vote", channel_only=True, min_args=1)
|
2019-06-17 15:29:14 +00:00
|
|
|
def vote(self, event):
|
|
|
|
"""
|
|
|
|
:help: Vote in the channel's current vote
|
2019-06-17 17:08:02 +00:00
|
|
|
:usage: <id> [choice]
|
2019-06-17 15:29:14 +00:00
|
|
|
"""
|
2019-06-17 16:51:42 +00:00
|
|
|
vote_id = event["args_split"][0]
|
|
|
|
vote = self._get_vote(event["target"], vote_id)
|
2019-06-17 15:29:14 +00:00
|
|
|
if vote == None:
|
2019-06-17 16:51:42 +00:00
|
|
|
raise utils.EventError(STR_NOVOTE % vote_id)
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
if not len(event["args_split"]) > 1:
|
|
|
|
closed = "" if vote["open"] else " (closed)"
|
|
|
|
event["stdout"].write("Vote %s%s: %s" % (
|
|
|
|
vote_id, closed, self._format_vote(vote)))
|
2019-06-17 15:29:14 +00:00
|
|
|
else:
|
2019-06-19 20:24:56 +00:00
|
|
|
if not vote["open"]:
|
|
|
|
raise utils.EventError("%s: vote %s is closed" % (
|
|
|
|
event["user"].nickname, vote_id))
|
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
choice = event["args_split"][1].lower()
|
|
|
|
if not choice in vote["options"]:
|
|
|
|
raise utils.EventError("Vote options: %s" %
|
|
|
|
self._format_options(vote))
|
2019-06-17 15:29:14 +00:00
|
|
|
|
2019-06-17 21:31:27 +00:00
|
|
|
cast_result = self._cast_vote(event["target"], vote_id,
|
|
|
|
event["user"], choice)
|
|
|
|
|
|
|
|
cast_desc = "cast"
|
|
|
|
if cast_result == VoteCastResult.Changed:
|
|
|
|
cast_desc = "changed"
|
|
|
|
|
|
|
|
event["stdout"].write("%s: your vote has been %s." %
|
|
|
|
(event["user"].nickname, cast_desc))
|
2019-06-17 15:36:48 +00:00
|
|
|
|
2019-06-17 16:51:42 +00:00
|
|
|
@utils.hook("received.command.votes", channel_only=True)
|
|
|
|
def votes(self, event):
|
2019-06-17 15:36:48 +00:00
|
|
|
"""
|
2019-06-17 16:51:42 +00:00
|
|
|
:help: List open votes in the current channel
|
2019-06-17 15:36:48 +00:00
|
|
|
"""
|
2019-06-17 16:51:42 +00:00
|
|
|
open_votes = self._open_votes(event["target"])
|
|
|
|
if open_votes:
|
|
|
|
open_votes_str = [
|
|
|
|
"%s (%s)" % (v["description"], v["id"]) for v in open_votes]
|
|
|
|
event["stdout"].write("Open votes: %s" % ", ".join(open_votes_str))
|
2019-06-17 15:36:48 +00:00
|
|
|
else:
|
2019-06-17 16:51:42 +00:00
|
|
|
event["stderr"].write("There are no open votes")
|