ircWebRegistration/irc_register.py

59 lines
2.4 KiB
Python
Raw Normal View History

2021-07-16 13:09:58 +02:00
import irctokens
import socket
import os
2021-07-16 09:58:22 +02:00
2021-07-16 13:09:58 +02:00
webircpass = os.getenv("WEBIRC_PASS")
def ircregister(userip, username, password, email="*"):
d = irctokens.StatefulDecoder()
e = irctokens.StatefulEncoder()
s = socket.socket()
2021-07-16 13:09:58 +02:00
# Here we assume using this only on localhost i.e. loopback
s.connect(("127.0.0.1", 6667))
def _send(line):
print(f"> {line.format()}")
e.push(line)
while e.pending():
e.pop(s.send(e.pending()))
_send(irctokens.build("WEBIRC", [ webircpass, "WebregGateway", userip, userip, "secure"]))
lines = d.push(s.recv(1024))
if lines == None:
print("!disconnected")
return "disconnected"
elif lines.command == "ERROR" and lines.params == "Invalid WebIRC password":
return "WebIRC bad password"
_send(irctokens.build("CAP", ["LS", "302"]))
while True:
lines = d.push(s.recv(1024))
if lines == None:
print("! disconnected")
break
2021-07-16 13:09:58 +02:00
for line in lines:
print(f"< {line.format()}")
# REGISTER can be attempted before-connect if server supports
# but if the server responds with the corresponding FAIL we
# need to try again. We can also handle email-required using
# the same keys. How to access these key-value pairs?
# reference: https://ircv3.net/specs/extensions/account-registration.html
# do we handle if email-req but not before-connect
# also how about when `custom-account-name` may be a value
if 'draft/account-registration=before-connect' in line.params:
_send(irctokens.build("CAP", ["REQ", "draft/account-registration"]))
if line.command == "CAP" and ("NAK" in line.params):
2022-04-16 15:48:20 +02:00
return "CAP_REFUSED"
elif line.command == "CAP" and ("ACK" in line.params):
_send(irctokens.build("CAP", ["END"]))
_send(irctokens.build("USER", ["u", "0", "*", username]))
_send(irctokens.build("NICK", [username]))
if line.command == "432":
return "ERR_ERRONEUSNICKNAME"
if line.command == "433":
return "ERR_NICKNAMEINUSE"
_send(irctokens.build("REGISTER", [username, "*", password]))
2021-07-16 13:09:58 +02:00
if line.command == "REGISTER" and ("SUCCESS" in line.params):
_send(irctokens.build("QUIT"))
return "SUCCESS"