Advertisement
Guest User

Untitled

a guest
Jan 4th, 2019
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. # coding=utf-8
  2. """
  3. znc-autoop.py - ZNC Autoop with Sopel
  4. https://wiki.znc.in/Autoop
  5.  
  6. No Copyright 02018
  7. Not licensed under anything
  8.  
  9. https://sopel.chat
  10. """
  11. from __future__ import unicode_literals
  12. import sopel.module
  13. import string
  14. import random
  15. import time
  16. import hashlib
  17. import fnmatch
  18.  
  19. # Needs good data structure to avoid collisions
  20. # To be improved upon
  21. global CHANNEL
  22. USERS = {
  23.     "user1": [
  24.         "password1",
  25.         ["*!*user1@10.10.0.10", "*!~user1@arpa.net"],
  26.         ["#sopel", "#znc"],
  27.         ""
  28.     ],
  29.     "user2": [
  30.         "password2",
  31.         ["*!*user2@2600::", "*!*user2@bing.com"],
  32.         ["#chat", "#idle"],
  33.         ""
  34.     ]
  35. }
  36.  
  37.  
  38. # Check whether the joining user is on the auto-op list
  39. @sopel.module.rule('.*')
  40. @sopel.module.event('JOIN')
  41. @sopel.module.priority('low')
  42. @sopel.module.unblockable
  43. def start_autoop(bot, trigger):
  44.     # Random sleep time
  45.     time.sleep(random.randint(2, 8))
  46.     # Get hostmask and channel from the join event
  47.     event = trigger.raw.split(" JOIN ")
  48.     hostmask = event[0][1:]
  49.     channel = event[1][1:]
  50.     for user in USERS:
  51.         # Proceed if at least one hostmask and one channel matches
  52.         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])):
  53.             challenge = generate_challenge()
  54.             challenge_string = USERS[user][0] + "::" + challenge
  55.             global CHANNEL
  56.             USERS[user][3] = md5_your_life(challenge_string)
  57.             CHANNEL = channel
  58.             bot.notice("!ZNCAO CHALLENGE " + challenge, trigger.nick)
  59.  
  60.  
  61. # Verify response and give operator status if successful
  62. @sopel.module.rule(r'^\!ZNCAO RESPONSE ([a-f0-9]{32})$')
  63. @sopel.module.event('NOTICE')
  64. @sopel.module.priority('low')
  65. @sopel.module.unblockable
  66. def continue_autoop(bot, trigger):
  67.     for user in USERS:
  68.         a_user = USERS[user]
  69.         if [h for h in a_user[1] if fnmatch.fnmatch(trigger.hostmask, h)]:
  70.             if trigger.group(1) == a_user[3]:
  71.                 bot.write(['MODE', CHANNEL, "+o", trigger.sender])
  72.  
  73.  
  74. # Respond to auto-op request
  75. # Verification needed
  76. @sopel.module.rule(r'^\!ZNCAO CHALLENGE (.{32})$')
  77. @sopel.module.event('NOTICE')
  78. @sopel.module.priority('low')
  79. @sopel.module.unblockable
  80. def accept_autoop(bot, trigger):
  81.     for user in USERS:
  82.         a_user = USERS[user]
  83.         if [h for h in a_user[1] if fnmatch.fnmatch(trigger.hostmask, h)]:
  84.             challenge = trigger.group(1)
  85.             challenge_string = a_user[0] + "::" + challenge
  86.             md5 = md5_your_life(challenge_string)
  87.             bot.notice("!ZNCAO RESPONSE " + md5, trigger.sender)
  88.  
  89. # Generate and return 32 random characters
  90. def generate_challenge(size=32, chars=string.ascii_letters + string.digits):
  91.     return ''.join(random.choice(chars) for _ in range(size))
  92.  
  93. # Calculate and return MD5 hash
  94. def md5_your_life(challenge):
  95.     return hashlib.md5(challenge.encode('utf-8')).hexdigest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement