maple/storage/bot/plugins/auth_sasl_plugin.py
2023-03-26 15:46:52 -05:00

118 lines
4.9 KiB
Python

# -*- coding: utf-8 -*- ############################################################### SOF
###########################################################################################
???AUTHSASL???
###########################################################################################
###########################################################################################
#####################
if SERVICES_AUTHSASL:
#####################
import irc3,os,sys,base64
from tool_colors_plugin import colorform as print
#######################################################################################
#######################################################################################
def getenv(s):
try:
s = os.environ[s]
return s
except:
error_type="environmental variable error"
error_reason=f"exported {s} not found"
print(f"{error_type}: {error_reason}")
sys.exit(1)
#######################################################################################
#######################################################################################
BOT_SASL_USERNAME=getenv('BOT_SASL_USERNAME')
BOT_SASL_PASSWORD=getenv('BOT_SASL_PASSWORD')
#######################################################################################
#######################################################################################
@irc3.plugin
class DR1PSASL:
###################################################################################
###################################################################################
def __init__(self, bot):
print('[ loaded ]')
self.bot=bot
self.auth=(f'{BOT_SASL_USERNAME}\0{BOT_SASL_USERNAME}\0{BOT_SASL_PASSWORD}')
self.auth=base64.encodebytes(self.auth.encode('utf8'))
self.auth=self.auth.decode('utf8').rstrip('\n')
self.events = [
irc3.event(r'^:\S+ CAP \S+ LS :(?P<data>.*)', self.cap_ls),
irc3.event(r'^:\S+ CAP \S+ ACK sasl', self.cap_ack),
irc3.event(r'AUTHENTICATE +', self.authenticate),
irc3.event(r'^:\S+ 903 \S+ :Authentication successful',self.cap_end),
]
###################################################################################
###################################################################################
@classmethod
def reload(cls, old):
return cls(old.bot)
###################################################################################
###################################################################################
def before_reload(self):
pass
###################################################################################
###################################################################################
def after_reload(self):
pass
###################################################################################
###################################################################################
def connection_ready(self, *args, **kwargs):
print('[ CAP LS ]')
self.bot.send('CAP LS\r\n')
self.bot.attach_events(*self.events)
###################################################################################
###################################################################################
def cap_ls(self, data=None, **kwargs):
print('[ CAP REQ :sasl ]')
if 'sasl' in data.lower():
self.bot.send_line('CAP REQ :sasl')
else:
self.cap_end()
###################################################################################
###################################################################################
def cap_ack(self, **kwargs):
print('[ AUTHENTICATE PLAIN ]')
self.bot.send_line('AUTHENTICATE PLAIN')
###################################################################################
###################################################################################
def authenticate(self, **kwargs):
print(f'[ AUTHENTICATE <CENSORED> ]')
self.bot.send_line(f'AUTHENTICATE {self.auth}\n')
###################################################################################
###################################################################################
def cap_end(self, **kwargs):
print('[ CAP END ]')
self.bot.send_line('CAP END\r\n')
self.bot.detach_events(*self.events)
###########################################################################################
####################################################################################### EOF