whois: now able to use a CRI API dump
This commit is contained in:
parent
aa81aa4e96
commit
89772ebce0
@ -1,5 +1,6 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from nemubot import context
|
from nemubot import context
|
||||||
@ -13,13 +14,26 @@ from more import Response
|
|||||||
from networking.page import headers
|
from networking.page import headers
|
||||||
|
|
||||||
PASSWD_FILE = None
|
PASSWD_FILE = None
|
||||||
|
# You can get one with: curl -b "sessionid=YOURSESSIONID" 'https://accounts.cri.epita.net/api/users/' > users.json
|
||||||
|
APIEXTRACT_FILE = None
|
||||||
|
|
||||||
def load(context):
|
def load(context):
|
||||||
global PASSWD_FILE
|
global PASSWD_FILE
|
||||||
if not context.config or "passwd" not in context.config:
|
if not context.config or "passwd" not in context.config:
|
||||||
print("No passwd file given")
|
print("No passwd file given")
|
||||||
|
else:
|
||||||
|
PASSWD_FILE = context.config["passwd"]
|
||||||
|
print("passwd file loaded:", PASSWD_FILE)
|
||||||
|
|
||||||
|
global APIEXTRACT_FILE
|
||||||
|
if not context.config or "apiextract" not in context.config:
|
||||||
|
print("No passwd file given")
|
||||||
|
else:
|
||||||
|
APIEXTRACT_FILE = context.config["apiextract"]
|
||||||
|
print("JSON users file loaded:", APIEXTRACT_FILE)
|
||||||
|
|
||||||
|
if PASSWD_FILE is None and APIEXTRACT_FILE is None:
|
||||||
return None
|
return None
|
||||||
PASSWD_FILE = context.config["passwd"]
|
|
||||||
|
|
||||||
if not context.data.hasNode("aliases"):
|
if not context.data.hasNode("aliases"):
|
||||||
context.data.addChild(ModuleState("aliases"))
|
context.data.addChild(ModuleState("aliases"))
|
||||||
@ -35,16 +49,26 @@ def load(context):
|
|||||||
|
|
||||||
class Login:
|
class Login:
|
||||||
|
|
||||||
def __init__(self, line):
|
def __init__(self, line=None, login=None, uidNumber=None, cn=None, promo=None, **kwargs):
|
||||||
s = line.split(":")
|
if line is not None:
|
||||||
self.login = s[0]
|
s = line.split(":")
|
||||||
self.uid = s[2]
|
self.login = s[0]
|
||||||
self.gid = s[3]
|
self.uid = s[2]
|
||||||
self.cn = s[4]
|
self.gid = s[3]
|
||||||
self.home = s[5]
|
self.cn = s[4]
|
||||||
|
self.home = s[5]
|
||||||
|
else:
|
||||||
|
self.login = login
|
||||||
|
self.uid = uidNumber
|
||||||
|
self.promo = promo
|
||||||
|
self.cn = cn
|
||||||
|
self.gid = "epita" + promo
|
||||||
|
|
||||||
def get_promo(self):
|
def get_promo(self):
|
||||||
return self.home.split("/")[2].replace("_", " ")
|
if hasattr(self, "promo"):
|
||||||
|
return self.promo
|
||||||
|
if hasattr(self, "home"):
|
||||||
|
return self.home.split("/")[2].replace("_", " ")
|
||||||
|
|
||||||
def get_photo(self):
|
def get_photo(self):
|
||||||
if self.login in context.data.getNode("pics").index:
|
if self.login in context.data.getNode("pics").index:
|
||||||
@ -60,17 +84,25 @@ class Login:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def found_login(login, search=False):
|
def login_lookup(login, search=False):
|
||||||
if login in context.data.getNode("aliases").index:
|
if login in context.data.getNode("aliases").index:
|
||||||
login = context.data.getNode("aliases").index[login]["to"]
|
login = context.data.getNode("aliases").index[login]["to"]
|
||||||
|
|
||||||
|
if APIEXTRACT_FILE:
|
||||||
|
with open(APIEXTRACT_FILE, encoding="utf-8") as f:
|
||||||
|
api = json.load(f)
|
||||||
|
for l in api:
|
||||||
|
if (not search and l["login"] == login) or (search and (("login" in l and l["login"].find(login) != -1) or ("cn" in l and l["cn"].find(login) != -1) or ("uid" in l and str(l["uid"]) == login))):
|
||||||
|
yield Login(**l)
|
||||||
|
|
||||||
login_ = login + (":" if not search else "")
|
login_ = login + (":" if not search else "")
|
||||||
lsize = len(login_)
|
lsize = len(login_)
|
||||||
|
|
||||||
with open(PASSWD_FILE, encoding="iso-8859-15") as f:
|
if PASSWD_FILE:
|
||||||
for l in f.readlines():
|
with open(PASSWD_FILE, encoding="iso-8859-15") as f:
|
||||||
if l[:lsize] == login_:
|
for l in f.readlines():
|
||||||
yield Login(l.strip())
|
if l[:lsize] == login_:
|
||||||
|
yield Login(l.strip())
|
||||||
|
|
||||||
def cmd_whois(msg):
|
def cmd_whois(msg):
|
||||||
if len(msg.args) < 1:
|
if len(msg.args) < 1:
|
||||||
@ -87,7 +119,7 @@ def cmd_whois(msg):
|
|||||||
res = Response(channel=msg.channel, count=" (%d more logins)", line_treat=format_response)
|
res = Response(channel=msg.channel, count=" (%d more logins)", line_treat=format_response)
|
||||||
for srch in msg.args:
|
for srch in msg.args:
|
||||||
found = False
|
found = False
|
||||||
for l in found_login(srch, "lookup" in msg.kwargs):
|
for l in login_lookup(srch, "lookup" in msg.kwargs):
|
||||||
found = True
|
found = True
|
||||||
res.append_message((srch, l))
|
res.append_message((srch, l))
|
||||||
if not found:
|
if not found:
|
||||||
@ -98,7 +130,7 @@ def cmd_whois(msg):
|
|||||||
def cmd_nicks(msg):
|
def cmd_nicks(msg):
|
||||||
if len(msg.args) < 1:
|
if len(msg.args) < 1:
|
||||||
raise IMException("Provide a login")
|
raise IMException("Provide a login")
|
||||||
nick = found_login(msg.args[0])
|
nick = login_lookup(msg.args[0])
|
||||||
if nick is None:
|
if nick is None:
|
||||||
nick = msg.args[0]
|
nick = msg.args[0]
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user