From fc1ad7795762c50f2eeadfcb14bd3058b3bfe8c2 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Sat, 1 Jun 2013 07:35:04 +0200 Subject: [PATCH] Passage de la gestion du solde dans nk.py --- basile.py | 70 +++++++++++++++++-------------------------------------- nk.py | 22 +++++++++++++++++ 2 files changed, 44 insertions(+), 48 deletions(-) diff --git a/basile.py b/basile.py index 797717a..75c6341 100755 --- a/basile.py +++ b/basile.py @@ -210,31 +210,31 @@ class Basile(ircbot.SingleServerIRCBot): ou bien (``False``, None)""" try: login_result, sock = nk.login(username, password, typ) - droits, retcode, errmsg = login_result["msg"], login_result["retcode"], login_result["errmsg"] + info, retcode, errmsg = login_result["msg"], login_result["retcode"], login_result["errmsg"] except nk.NKRefused as exc: for report in self.report_bugs_to: serv.privmsg(report, "Le Serveur NK2015 est down.") - return (False, None) + return (False, None, None) except nk.NKHelloFailed as exc: for report in self.report_bugs_to: serv.privmsg(report, "La version du protocole utilisée n'est pas supportée par le serveur NK2015.") - return (False, None) + return (False, None, None) except nk.NKUnknownError as exc: erreurs = ["Une fucking erreur inconnue s'est produite"] erreurs += str(exc).split("\n") for report in self.report_bugs_to: for err in erreurs: serv.privmsg(report, err) - return (False, None) + return (False, None, None) except Exception as exc: # Exception qui ne vient pas de la communication avec le serveur NK2015 log(self.serveur, "Erreur dans new_connection_NK\n" + str(exc)) - return (False, None) + return (False, None, None) if retcode == 0: - return (True, sock) + return (True, info, sock) else: - return (False, None) + return (False, None, None) def give_me_my_pseudo(self, serv): """Récupère le pseudo auprès de NickServ.""" @@ -255,7 +255,7 @@ class Basile(ircbot.SingleServerIRCBot): log(self.serveur, "JOIN %s" % (c)) serv.join(c) # on ouvre la connexion note de Basile, special user - self.nk = self.new_connection_NK(serv, config.note_pseudo, config.note_password, "special")[1] + self.nk = self.new_connection_NK(serv, config.note_pseudo, config.note_password, "special")[2] if self.nk == None: for report in self.report_bugs_to: serv.privmsg(report, "Connection to NK2015 failed, invalid password ?") @@ -321,16 +321,16 @@ class Basile(ircbot.SingleServerIRCBot): if len(message) == 1: if self.identities.has_key(auteur): serv.privmsg(auteur, "Je vous connais sous le pseudo note %s." % ( - self.identities[auteur].encode("utf8"))) + self.identities[auteur]["pseudo"].encode("utf8"))) else: serv.privmsg(auteur, "Je ne connais pas votre pseudo note.") elif len(message) >= 3: username, password = message[1], " ".join(message[2:]) - success, _ = self.new_connection_NK(serv, username, password) + success, info, _ = self.new_connection_NK(serv, username, password) if success: log(self.serveur, "priv", auteur, " ".join(message) + "[successful]") serv.privmsg(auteur, "Identité enregistrée.") - self.identities[auteur] = username + self.identities[auteur] = info json.dump(self.identities, open(config.identities_file,"w")) else: log(self.serveur, "priv", auteur, " ".join(message) + "[failed]") @@ -341,7 +341,7 @@ class Basile(ircbot.SingleServerIRCBot): if len(message) > 1: if self.identities.has_key(auteur): password = " ".join(message[1:]) - success, _ = self.new_connection_NK(serv, self.identities[auteur], password) + success, _, _ = self.new_connection_NK(serv, self.identities[auteur], password) if success: del self.identities[auteur] log(self.serveur, "priv", auteur, " ".join(message) + "[successful]") @@ -430,7 +430,7 @@ class Basile(ircbot.SingleServerIRCBot): if auteur in self.ops: try: self.nk = self.new_connection_NK(serv, config.note_pseudo, - config.note_password, "special")[1] + config.note_password, "special")[2] except Exception as exc: self.nk = None log(self.serveur, 'Erreur dans on_pubmsg/"cmd in ["reconnect"]\n' + str(exc)) @@ -505,31 +505,12 @@ class Basile(ircbot.SingleServerIRCBot): elif cmd == u"solde": if len(message) == 1: if self.identities.has_key(auteur): - try: - self.nk.write('["search", ["x",["pseudo"],%s]]' % (json.dumps(self.identities[auteur]))) - ret = json.loads(self.nk.read()) - solde = ret["msg"][0]["solde"] - pseudo = ret["msg"][0]["pseudo"] - except Exception as exc: - print exc - serv.privmsg(auteur, "failed") - log(self.serveur, "priv", auteur, " ".join(message) + "[failed]") - return - serv.privmsg(auteur, "%s (%s)" % (float(solde)/100, pseudo.encode("utf8"))) - log(self.serveur, "priv", auteur, " ".join(message) + "[successful]") + success, solde, pseudo = nk.get_solde(self.nk, self.identities[auteur]["idbde"], serv, auteur) + if success: + serv.privmsg(auteur, "%s (%s)" % (float(solde)/100, pseudo.encode("utf8"))) + log(self.serveur, "priv", auteur, " ".join(message) + "[successful]" if success else "[failed]") else: serv.privmsg(canal, "Je ne connais pas ton pseudo note.") - elif auteur in self.ops: - try: - self.nk.write('["search", ["x",["pseudo"],%s]]' % (json.dumps(message[1]))) - ret = json.loads(self.nk.read()) - solde = ret["msg"][0]["solde"] - pseudo = ret["msg"][0]["pseudo"] - except Exception as exc: - serv.privmsg(auteur, "failed") - log(self.serveur, "priv", auteur, " ".join(message) + "[failed]") - return - serv.privmsg(auteur, "%s (%s)" % (float(solde)/100, pseudo.encode("utf8"))) elif cmd == u"ops": if auteur in self.overops: serv.privmsg(auteur, " ".join(self.ops)) @@ -592,7 +573,7 @@ class Basile(ircbot.SingleServerIRCBot): if auteur in self.ops: try: self.nk = self.new_connection_NK(serv, config.note_pseudo, - config.note_password, "special")[1] + config.note_password, "special")[2] except Exception as exc: self.nk = None log(self.serveur, 'Erreur dans on_pubmsg/"cmd in ["reconnect"]\n' + str(exc)) @@ -621,18 +602,11 @@ class Basile(ircbot.SingleServerIRCBot): elif cmd in [u"solde", u"!solde"]: if self.identities.has_key(auteur): - pseudo = self.identities[auteur] - try: - self.nk.write(json.dumps(["search", ["x", ["pseudo"], pseudo]])) - ret = json.loads(self.nk.read()) - solde = ret["msg"][0]["solde"] - pseudo = ret["msg"][0]["pseudo"] - except Exception as exc: - serv.privmsg(canal, "%s: failed"%(auteur)) - log(self.serveur, canal, auteur, message + "[failed]") - else: + idbde = self.identities[auteur]["idbde"] + success, solde, pseudo = nk.get_solde(self.nk, self.identities[auteur]["idbde"], serv, canal) + if success: serv.privmsg(canal, "%s: %s (%s)" % (auteur, float(solde)/100, pseudo.encode("utf8"))) - log(self.serveur, canal, auteur, message + "[successful]") + log(self.serveur, canal, auteur, message + "[successful]" if success else "[failed]") else: serv.privmsg(canal, "%s: Je ne connais pas votre pseudo note." % (auteur)) log(self.serveur, canal, auteur, message + "[unknown]") diff --git a/nk.py b/nk.py index ecad88f..78b6594 100644 --- a/nk.py +++ b/nk.py @@ -8,6 +8,7 @@ import socket import json import ssl +import traceback #: Config de basile import config @@ -69,3 +70,24 @@ def login(username, password, typ="bdd"): raise NKRefused(str(exc)) return json.loads(out), sock +def get_solde(sock, idbde, serv, canal): + """Récupère le (solde, pseudo, success) de l'utilisateur NK n°``idbde``""" + try: + sock.write(json.dumps(["compte", idbde])) + ret = json.loads(sock.read()) + retcode = ret["retcode"] + if retcode == 0: + solde = ret["msg"]["solde"] + pseudo = ret["msg"]["pseudo"] + return (True, solde, pseudo) + else: + serv.privmsg(canal, ret["errmsg"]) + return (False, None, None) + except Exception as exc: + trace = traceback.format_exc() + msg = "failed\n%s" % trace + for l in msg.split("\n"): + serv.privmsg(canal, l) + #log(self.serveur, "priv", auteur, " ".join(message) + "[failed]") + return (False, None, None) + -- 2.39.2