]>
gitweb.pimeys.fr Git - bots/basile.git/blob - nk.py
6 """ Module de Basile pour dialoguer avec la NoteKfet2015 """
15 class NKError(Exception):
16 def __init__(self
,msg
):
17 Exception.__init
__(self
)
21 def __unicode__(self
):
22 return unicode(self
.msg
)
24 class NKRefused(NKError
):
27 class NKHelloFailed(NKError
):
30 class NKUnknownError(NKError
):
34 sock
= socket
.socket()
36 # On établit la connexion sur port 4242
37 sock
.connect((config
.nk_server
, config
.nk_port
))
39 sock
= ssl
.wrap_socket(sock
, ca_certs
='../keys/ca_.crt')
41 sock
.write(json
.dumps(["hello", "Basile"]))
42 # On récupère la réponse du hello
45 except Exception as exc
:
46 # Si on a foiré quelque part, c'est que le serveur est down
47 raise NKRefused(str(exc
))
48 if out
["retcode"] == 0:
50 elif out
["retcode"] == 11:
51 raise NKHelloFailed(out
["errmsg"])
53 raise NKUnknownError(out
["errmsg"])
56 def login(username
, password
, typ
="bdd"):
58 if typ
== "special": # ça c'est pour Basile lui-même
61 masque
= [[], [], True]
63 # Basile a un compte special user
64 commande
= ["login", [username
, password
, typ
, masque
]]
65 sock
.write(json
.dumps(commande
))
67 except Exception as exc
:
68 # Si on a foiré quelque part, c'est que le serveur est down
69 raise NKRefused(str(exc
))
70 return json
.loads(out
), sock