Enhance znccreate.py for better config and error handling
This commit is contained in:
parent
1018f98986
commit
cec665c4c8
56
znccreate.py
56
znccreate.py
|
@ -2,40 +2,74 @@
|
||||||
# Script created/contributed by ~jmjl
|
# Script created/contributed by ~jmjl
|
||||||
|
|
||||||
import socket, ssl, json, time, sys
|
import socket, ssl, json, time, sys
|
||||||
# Takes the first argument as a username and the second as the password.
|
|
||||||
def loadconf(cfgfile):
|
def loadconf(cfgfile):
|
||||||
with open(cfgfile, 'r') as f:
|
with open(cfgfile, 'r') as f:
|
||||||
cfg = json.load(f)
|
return json.load(f)
|
||||||
return cfg
|
|
||||||
def send(msg):
|
def send(msg):
|
||||||
s.send(f"{msg}\n".encode('utf-8'))
|
s.send(f"{msg}\n".encode('utf-8'))
|
||||||
|
|
||||||
cfg = loadconf("/root/.znc-conf/znc-config.json")
|
cfg = loadconf("/root/.znc-conf/znc-config.json")
|
||||||
|
|
||||||
readbuffer=""
|
readbuffer = ""
|
||||||
s = socket.socket()
|
s = socket.socket()
|
||||||
if cfg['tls'] == 'yes':
|
if cfg.get('tls') == 'yes':
|
||||||
ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
|
ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
|
||||||
s = ctx.wrap_socket(s)
|
s = ctx.wrap_socket(s)
|
||||||
|
|
||||||
s.connect((cfg['srv'], int(cfg['port'])))
|
s.connect((cfg['srv'], int(cfg['port'])))
|
||||||
send("NICK bot")
|
send("NICK bot")
|
||||||
send("USER bot 0 * :A bot to make users")
|
send("USER bot 0 * :A bot to make users")
|
||||||
|
|
||||||
|
# Parse optional key=value settings after username/password.
|
||||||
|
# Example: MaxNetworks=3 MaxClients=5
|
||||||
|
cli_settings = {}
|
||||||
|
for arg in sys.argv[3:]:
|
||||||
|
if '=' in arg:
|
||||||
|
k, v = arg.split('=', 1)
|
||||||
|
k = k.strip()
|
||||||
|
v = v.strip()
|
||||||
|
if k:
|
||||||
|
cli_settings[k] = v
|
||||||
|
|
||||||
|
# Also allow defaults from config, but CLI wins.
|
||||||
|
# In /root/.znc-conf/znc-config.json you may add:
|
||||||
|
# { ..., "default_user_settings": { "MaxNetworks": "3" } }
|
||||||
|
default_settings = cfg.get("default_user_settings", {}) or {}
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
readbuffer = readbuffer + s.recv(2048).decode('utf-8')
|
readbuffer += s.recv(2048).decode('utf-8', errors='ignore')
|
||||||
temp = str.split(readbuffer, "\n")
|
temp = str.split(readbuffer, "\n")
|
||||||
readbuffer = temp.pop()
|
readbuffer = temp.pop()
|
||||||
|
|
||||||
for line in temp:
|
for line in temp:
|
||||||
line = str.rstrip(line)
|
line = line.rstrip("\r")
|
||||||
line = str.split(line)
|
parts = line.split()
|
||||||
|
|
||||||
#print(' '.join(line))
|
if len(parts) < 2:
|
||||||
if line[1] == '464':
|
continue
|
||||||
|
|
||||||
|
# Authenticate when ZNC asks for PASS (ERR_PASSWDMISMATCH 464).
|
||||||
|
if parts[1] == '464':
|
||||||
send(f"PASS {cfg['user']}:{cfg['password']}")
|
send(f"PASS {cfg['user']}:{cfg['password']}")
|
||||||
if line[0][1:] == 'irc.znc.in' and line[1] == '001':
|
|
||||||
|
# On welcome (001), create user and apply settings.
|
||||||
|
# (Preserves your original hostname check.)
|
||||||
|
if parts[0][1:] == 'irc.znc.in' and parts[1] == '001':
|
||||||
user = sys.argv[1]
|
user = sys.argv[1]
|
||||||
pswd = sys.argv[2]
|
pswd = sys.argv[2]
|
||||||
|
|
||||||
|
# Create user (unchanged)
|
||||||
send(f"PRIVMSG *controlpanel :AddUser {user} {pswd}")
|
send(f"PRIVMSG *controlpanel :AddUser {user} {pswd}")
|
||||||
|
|
||||||
|
# Merge defaults + CLI, CLI overrides
|
||||||
|
effective = dict(default_settings)
|
||||||
|
effective.update(cli_settings)
|
||||||
|
|
||||||
|
# Apply settings like: set MaxNetworks <user> <val>
|
||||||
|
for key, val in effective.items():
|
||||||
|
send(f"PRIVMSG *controlpanel :set {key} {user} {val}")
|
||||||
|
|
||||||
print(f"Maken znc user {user}")
|
print(f"Maken znc user {user}")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
Loading…
Reference in New Issue