Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- """
- znc-autoop.py - ZNC Autoop with Sopel
- https://wiki.znc.in/Autoop
- No Copyright 02018
- Not licensed under anything
- https://sopel.chat
- """
- from __future__ import unicode_literals
- import sopel.module
- import string
- import random
- import time
- import hashlib
- import fnmatch
- # Needs good data structure to avoid collisions
- # To be improved upon
- global CHANNEL
- USERS = {
- "user1": [
- "password1",
- ["*!*user1@10.10.0.10", "*!~user1@arpa.net"],
- ["#sopel", "#znc"],
- ""
- ],
- "user2": [
- "password2",
- ["*!*user2@2600::", "*!*user2@bing.com"],
- ["#chat", "#idle"],
- ""
- ]
- }
- # Check whether the joining user is on the auto-op list
- @sopel.module.rule('.*')
- @sopel.module.event('JOIN')
- @sopel.module.priority('low')
- @sopel.module.unblockable
- def start_autoop(bot, trigger):
- # Random sleep time
- time.sleep(random.randint(2, 8))
- # Get hostmask and channel from the join event
- event = trigger.raw.split(" JOIN ")
- hostmask = event[0][1:]
- channel = event[1][1:]
- for user in USERS:
- # Proceed if at least one hostmask and one channel matches
- if ([h for h in USERS[user][1] if fnmatch.fnmatch(hostmask, h)] and any(channel.lower() == c.lower() for c in USERS[user][2])):
- challenge = generate_challenge()
- challenge_string = USERS[user][0] + "::" + challenge
- global CHANNEL
- USERS[user][3] = md5_your_life(challenge_string)
- CHANNEL = channel
- bot.notice("!ZNCAO CHALLENGE " + challenge, trigger.nick)
- # Verify response and give operator status if successful
- @sopel.module.rule(r'^\!ZNCAO RESPONSE ([a-f0-9]{32})$')
- @sopel.module.event('NOTICE')
- @sopel.module.priority('low')
- @sopel.module.unblockable
- def continue_autoop(bot, trigger):
- for user in USERS:
- a_user = USERS[user]
- if [h for h in a_user[1] if fnmatch.fnmatch(trigger.hostmask, h)]:
- if trigger.group(1) == a_user[3]:
- bot.write(['MODE', CHANNEL, "+o", trigger.sender])
- # Respond to auto-op request
- # Verification needed
- @sopel.module.rule(r'^\!ZNCAO CHALLENGE (.{32})$')
- @sopel.module.event('NOTICE')
- @sopel.module.priority('low')
- @sopel.module.unblockable
- def accept_autoop(bot, trigger):
- for user in USERS:
- a_user = USERS[user]
- if [h for h in a_user[1] if fnmatch.fnmatch(trigger.hostmask, h)]:
- challenge = trigger.group(1)
- challenge_string = a_user[0] + "::" + challenge
- md5 = md5_your_life(challenge_string)
- bot.notice("!ZNCAO RESPONSE " + md5, trigger.sender)
- # Generate and return 32 random characters
- def generate_challenge(size=32, chars=string.ascii_letters + string.digits):
- return ''.join(random.choice(chars) for _ in range(size))
- # Calculate and return MD5 hash
- def md5_your_life(challenge):
- return hashlib.md5(challenge.encode('utf-8')).hexdigest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement