def on_join(self, channel, user): if user == self.nickname: self._check_flags() for elec in Election.select().where(Election.status == 0): if elec.close < datetime.utcnow(): # Already closed!! self._closevote(elec.id) else: closes_in = elec.close - datetime.utcnow() self.eventloop.schedule_in(closes_in, self._closevote, elec.id) for elec in Effective.select(): if elec.close < datetime.utcnow(): # Already closed!! self._expire(elec.id) else: closes_in = elec.close - datetime.utcnow() self.eventloop.schedule_in(closes_in, self._expire, elec.id) self.eventloop.schedule_periodically(600, self.set_mode, config.CHANNEL, 'b') self.eventloop.schedule_periodically(3600, self._check_flags) else: self.whois(user)
def start_vote(self, by, args): account = self.users[by]['account'].lower() # 1 - Check if user has voice if by not in self.channels[config.CHANNEL]['modes'].get('v', []): if by not in self.channels[config.CHANNEL]['modes'].get('o', []): return self.notice(by, _('Failed: You are not enfranchised.')) # 2 - get vote class vote = VOTE_NAMES[args[0]](self) if not vote.get_target(args): return self.notice( by, _('Failed: Target user not found or not identified.')) # 3 - check if vote already exists opener = User.get(User.name == account) try: vote = Election.select() \ .where(Election.vote_type == args[0], Election.status == 0, Election.vote_target == vote.get_target(args)).get() # !!! vote already exists return self.vote(vote, opener, by) except Election.DoesNotExist: pass # 5 - Custom vote type checks if vote.vote_check(args, by) is not True: print("Vote creation rejected by custom rule") return # 6 - Create the vote elec = Election(vote_type=args[0], opened=datetime.utcnow(), close=datetime.utcnow() + timedelta(seconds=vote.openfor), status=0, opened_by=opener, vote_target=vote.get_target(args)) elec.save() if not (vote.is_target_user and opener.name == vote.get_target(args)): # 7 - Emit self vote svote = Suffrage(election=elec, yea=True, emitted_by=opener) svote.save() # 8 - Schedule self.eventloop.schedule_in(timedelta(seconds=vote.openfor), self._closevote, elec.id) # 9 - announce dt = display_time(vote.openfor) self.msg( _("Vote \002#{0}\002: \002{1}\002: \037{2}\037. You have " "\002{3}\002 to vote; \002{4}\002 votes are required for a " "quorum! Type or PM \002\00303!vote y {0}\003\002 or " "\002\00304!vote n {0}\003\002").format(elec.id, args[0], vote.get_target(args), dt, vote.quorum))
def vote_check(self, args, by): if self.is_target_user: try: account = self.irc.users[args[1]]['account'].lower() except KeyError: return self.irc.notice(by, 'Can\'t start vote: User not found ' 'or not identified.') user = self.irc.usermap.get(account) if not user or not user.get('lines'): return self.irc.notice(by, 'Can\'t start vote: User has never ' 'interacted with the channel.') try: x = Effective.select().where((Effective.vote_type == self.name) & (Effective.vote_target == self.get_target(args))) \ .get() try: elecid = x.election.id except Election.DoesNotExist: elecid = "Unknown election" return self.irc.notice(by, 'Can\'t start vote: There\'s an identical motion already active (\002{0}\002).'.format(elecid)) except Effective.DoesNotExist: pass try: x = Election.select().where((Election.vote_type == self.name) & (Election.vote_target == self.get_target(args)) & (Election.status == 3) & (Election.close > (datetime.utcnow() - timedelta(seconds=self.cooldown)))).get() return self.irc.notice(by, 'Can\'t start vote: There was a similar vote that failed not too long ago (\002{0}\002).'.format(x.id)) except Election.DoesNotExist: pass if self.required_time != 0: reqtime = datetime.utcnow() - timedelta(seconds=self.required_time) if user['first_seen'] > reqtime: return self.irc.notice(by, "Can't start vote: User at issue " "has not been present long enough for " "consideration.") if user['last_seen'] < reqtime: return self.irc.notice(by, "Can't start vote: User at issue " "has not been active recently.") if user['lines'] < self.required_lines: return self.irc.notice(by, "Can't start vote: User at issue " "has {0} of {1} required lines" .format(user['lines'], self.required_lines)) return True # True = check passed
def on_message(self, target, by, message): try: account = self.users[by]['account'] except KeyError: print("{0}: Not identified/not found".format(by)) return if not account: return # Unregistered users don't exist account = account.lower() if target == config.CHANNEL: self.count_line(account) message = message.strip().lower() if not message.startswith('!'): return command = message[1:].split()[0] args = message.split()[1:] if command == 'vote': if not args: return args[0] = args[0].strip('#') if args[0] in list(VOTE_NAMES): # creating a vote! self.start_vote(by, args) elif args[0] == "list" and target == config.CHANNEL: print('list ', by) if by not in self.channels[config.CHANNEL]['modes'].get( 'v', []): if by not in self.channels[config.CHANNEL]['modes'].get( 'o', []): return self.notice( by, 'Failed: You are not enfranchised.') vpar, unk = votelistParser.parse_known_args(args[1:]) if not vpar.type: votes = Election.select().where(Election.status == 0) \ .order_by(Election.id.desc()).limit(5) else: if vpar.type not in list(VOTE_NAMES): return self.notice(by, 'Failed: Unknown vote type') votes = Election.select() \ .where(Election.vote_type == vpar.type) \ .order_by(Election.id.desc()).limit(10) if not votes: return self.notice(by, 'No matching results.') user = User.get(User.name == account) for vote in votes: posit = Suffrage.select() \ .where((Suffrage.election == vote) & (Suffrage.yea == True)).count() negat = Suffrage.select() \ .where((Suffrage.election == vote) & (Suffrage.yea == False)).count() try: yv = Suffrage.get((Suffrage.election == vote) & (Suffrage.emitted_by == user)) if yv.yea: you = '\00300,03YEA\003' else: you = '\00300,04NAY\003' except Suffrage.DoesNotExist: you = '\00300,01---\003' stat = self._resolve_status(vote.status) if vote.status == 0: tdel = vote.close - datetime.utcnow() ostr = self._resolve_time(tdel, _('left')) else: tdel = datetime.utcnow() - vote.close ostr = self._resolve_time(tdel, _('ago')) self.msg( _('\002#{0} YEA: \00303{1}\003 NAY: \00305{2}\003 ' 'YOU: {3} {4} {5}\002 \037{6}\037 - {7}').format( vote.id, posit, negat, you, stat, vote.vote_type, vote.vote_target, ostr)) elif args[0].isdigit() or args[0] in ['y', 'yes', 'n', 'no']: if by not in self.channels[config.CHANNEL]['modes'].get( 'v', []): if by not in self.channels[config.CHANNEL]['modes'].get( 'o', []): return self.notice( by, 'Failed: You are not enfranchised.') user = User.get(User.name == account) if args[0].isdigit(): if len(args) == 1: return self.vote_info(by, args[0]) voteid = args[0] positive = True if args[1] in ['y', 'yes'] else False else: positive = True if args[0] in ['y', 'yes'] else False if len(args) == 1: xe = Election.select().where(Election.status == 1) if xe.count() == 1: voteid = xe.get().id else: return self.notice( by, 'Failed: Usage: !vote y/n ' '<vote id>') else: voteid = args[1].strip('#') if not voteid.isdigit(): self.notice(by, 'Usage: !vote y/n <vote id>') return try: elec = Election.get(Election.id == voteid) except Election.DoesNotExist: return self.notice(by, 'Failed: Vote not found') if elec.status != 0: return self.notice(by, 'Failed: This vote already ' 'ended') return self.vote(elec, user, by, positive, (target != config.CHANNEL))