IRCat/modules/pawserv.py

102 lines
6.3 KiB
Python
Raw Normal View History

2025-01-21 19:16:24 -08:00
# Replacement for services bots.
2025-01-21 20:43:56 -08:00
import traceback, smtplib, uuid, ssl
2025-01-21 19:16:24 -08:00
__ircat_type__ = "command"
2025-01-22 19:35:21 -08:00
__ircat_requires__ = ["name", "smtp_host", "smtp_port", "smtp_starttls", "smtp_username", "smtp_password", "host"]
2025-01-21 19:16:24 -08:00
__ircat_giveme__ = ["sql"] # Only command and allsocket have these.
__ircat_fakeusers__ = {
"NickServ": {
"host": "PawServ",
"username": "Meow",
"realname": "PawServ plugin - Identification bot",
2025-01-23 11:32:10 -08:00
"modes": "iw",
"away": False,
"identified": False,
"ssl": False
2025-01-21 19:16:24 -08:00
},
"ChanServ": {
"host": "PawServ",
"username": "Meow",
"realname": "PawServ plugin - Channel management bot",
"modes": "iw",
2025-01-23 11:32:10 -08:00
"away": False,
"identified": False,
"ssl": False
2025-01-21 19:16:24 -08:00
}
}
2025-01-21 19:20:15 -08:00
class IRCatModule:
2025-01-22 19:35:21 -08:00
def __init__(self, sql, smtp_host, smtp_port, smtp_starttls, smtp_username, smtp_password, name, host):
2025-01-21 19:16:24 -08:00
self.sql = sql
2025-01-21 20:44:26 -08:00
self.smtp_server = smtp_host
2025-01-21 20:43:22 -08:00
self.smtp_port = smtp_port
self.smtp_starttls = smtp_starttls
self.smtp_username = smtp_username
self.smtp_password = smtp_password
self.net_name = name
2025-01-22 19:35:21 -08:00
self.hostname = host
2025-01-21 20:43:22 -08:00
self.memory = {} # {nick: [authtoken, password, email]}
2025-01-21 19:16:24 -08:00
print("PawServ loaded!")
def command(self, command, args, ip, nick, connection, user):
try:
if command == "NICKSERV" or (command == "PRIVMSG" and args[0].lower() == "nickserv"):
if command == "PRIVMSG":
args = args[1:]
2025-01-21 19:19:37 -08:00
args[0] = args[0][1:] if args[0][0] == ":" else args[0]
2025-01-21 20:43:22 -08:00
if len(args) > 0 and args[0].lower() == "verify":
if len(args) == 3:
2025-01-22 01:08:56 -08:00
if args[1].lower() in self.memory:
if args[2] == self.memory[args[1].lower()][0]:
2025-01-24 18:33:45 -08:00
self.sql.nickserv_register(nick=args[1].lower(), password=self.memory[args[1].lower()][1], email=self.memory[args[1].lower()][2])
2025-01-22 01:08:56 -08:00
nck = args[1].lower()
2025-01-21 20:51:44 -08:00
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Done, you may now identify as {nck}.\r\n", "UTF-8"))
2025-01-22 01:08:56 -08:00
del self.memory[args[1].lower()]
2025-01-22 19:35:21 -08:00
else:
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Invalid verification.\r\n", "UTF-8"))
2025-01-21 20:43:22 -08:00
else:
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Nickname doesn't exist, try registering again?\r\n", "UTF-8"))
else:
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Invalid verification.\r\n", "UTF-8"))
2025-01-21 20:49:43 -08:00
elif len(args) > 0 and args[0].lower() == "register":
2025-01-21 20:43:22 -08:00
if len(args) == 3:
2025-01-22 21:09:09 -08:00
if not self.sql.nickserv_isexist(nick.lower()):
if not nick in self.memory:
context = ssl.create_default_context()
token = str(uuid.uuid4())
message = f"Subject: {self.net_name} Account Verification\n\nHi,\nIt appears you have tried to register an account ({nick}) with this email on {self.net_name},\nIf you did not register an account, feel free to delete this email.\nIf you did, use this command:\n/nickserv verify {nick} {token}"
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
2025-01-22 19:35:21 -08:00
server.ehlo()
2025-01-22 21:09:09 -08:00
if self.smtp_starttls:
server.starttls(context=context)
server.ehlo()
server.login(self.smtp_username, self.smtp_password)
server.sendmail(self.smtp_username, args[2], message)
self.memory[nick.lower()] = [token, args[1], args[2]]
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Email sent, please verify.\r\n", "UTF-8"))
else:
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :A verification is already pending.\r\n", "UTF-8"))
2025-01-21 20:48:35 -08:00
else:
2025-01-22 21:09:09 -08:00
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :The user {nick} already exists.\r\n", "UTF-8"))
2025-01-21 20:43:22 -08:00
else:
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Needs 3 arguments, nickname, password, and email.\r\n", "UTF-8"))
2025-01-21 20:49:43 -08:00
elif len(args) > 0 and args[0].lower() == "identify":
2025-01-21 21:00:41 -08:00
nck = nick if len(args) == 2 else args[2]
2025-01-22 01:08:56 -08:00
temp = self.sql.nickserv_identify(nick=nck.lower(), password=args[1])
2025-01-21 21:01:31 -08:00
if temp != False:
2025-01-22 19:35:21 -08:00
hostmask = user["host"]
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :You are now identified as {nck}.\r\n", "UTF-8"))
connection.sendall(bytes(f":{self.hostname} 900 {nick} {hostmask} {nck} :You are now logged in as {nck}.\r\n", "UTF-8"))
return {"success": True, "identify": temp}
2025-01-21 19:56:00 -08:00
else:
2025-01-22 01:08:56 -08:00
if nick.lower() in self.memory:
2025-01-21 20:43:22 -08:00
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Your account isn't verified, please verify now.\r\n", "UTF-8"))
else:
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :Invalid username/password.\r\n", "UTF-8"))
2025-01-21 19:19:52 -08:00
else:
2025-01-21 19:16:24 -08:00
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :NickServ Usage:\r\n","UTF-8"))
2025-01-21 19:56:00 -08:00
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :IDENTIFY pass <nick> - Identifies your nickname\r\n","UTF-8"))
2025-01-21 20:43:22 -08:00
connection.sendall(bytes(f":NickServ!Meow@PawServ NOTICE {nick} :REGISTER pass email - Register your nickname\r\n","UTF-8"))
2025-01-22 19:35:21 -08:00
return {"success": True}
2025-01-21 19:16:24 -08:00
else:
2025-01-22 19:35:21 -08:00
return {"success": False}
2025-01-21 19:16:24 -08:00
except:
2025-01-21 19:17:07 -08:00
print(traceback.format_exc())
2025-01-22 19:35:21 -08:00
return {"success": False}