isengard-bot/commands.py

140 lines
3.5 KiB
Python
Raw Normal View History

2022-02-06 01:54:17 +01:00
import pickle
2022-02-08 15:46:17 +01:00
import logging
from systemd.journal import JournalHandler
# Logging
log = logging.getLogger(__name__)
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
2022-02-05 00:46:57 +01:00
# Isengard commands
2022-02-06 01:29:59 +01:00
def cmdping(owners, nick, text, sbuf):
2022-02-05 00:46:57 +01:00
"""
Ping command.
"""
return "pong !"
2022-02-07 18:39:34 +01:00
def cmdhelp(owners, nick, text, sbuf):
"""
Ping command.
"""
global commandtable
msg = "Je suis Isengard, le bot de supervision de ce salon.\n"
msg += "\n"
msg += "Voici la liste de mes commandes disponibles : \n"
for cmd in commandtable:
msg += "- " + cmd + "\n"
return msg
2022-02-05 00:46:57 +01:00
2022-02-06 01:29:59 +01:00
def cmdmainteneur(owners, nick, text, sbuf):
"""
Change maintainer for an host
"""
if not nick in owners:
return "désolé mais vous n'êtes pas autorisé à utiliser cette commande."
try:
splittedtext = text.split(" ")
# print maintainer
if len(splittedtext) == 2:
host = splittedtext[1]
return "le responsable de cette machine est " \
2022-02-07 18:39:34 +01:00
+ sbuf.serviceData[host]["maintainer"]
2022-02-06 01:29:59 +01:00
if len(splittedtext) == 3:
host = splittedtext[1]
maintainer = splittedtext[2]
2022-02-07 18:39:34 +01:00
sbuf.serviceData[host] = {}
sbuf.serviceData[host]["destmuc"] = None
sbuf.serviceData[host]["status_state"] = None
sbuf.serviceData[host]["status_type"] = None
sbuf.serviceData[host]["text"] = None
sbuf.serviceData[host]["raw"] = None
sbuf.serviceData[host]["needUpdate"] = False
sbuf.serviceData[host]["maintainer"] = maintainer
2022-02-06 01:29:59 +01:00
return "le responsable est à présent " \
2022-02-07 18:39:34 +01:00
+ sbuf.serviceData[host]["maintainer"]
2022-02-06 01:29:59 +01:00
except Exception as e:
print(repr(e))
return "Erreur à l'exécution"
return "Syntaxe invalide"
2022-02-05 00:46:57 +01:00
2022-02-06 01:54:17 +01:00
def cmdsave(owners, nick, text, sbuf):
"""
Save
"""
if not nick in owners:
return "désolé mais vous n'êtes pas autorisé à utiliser cette commande."
try:
with open('current_buffer', 'wb') as current_buffer_file:
2022-02-07 18:39:34 +01:00
pickle.dump(sbuf.serviceData, current_buffer_file)
2022-02-06 01:54:17 +01:00
return "OK"
2022-02-05 00:46:57 +01:00
2022-02-06 01:54:17 +01:00
except Exception as e:
print(repr(e))
return "Erreur à l'exécution"
2022-02-05 00:46:57 +01:00
2022-02-06 01:54:17 +01:00
def cmdload(owners, nick, text, sbuf):
"""
Save
"""
if not nick in owners:
return "désolé mais vous n'êtes pas autorisé à utiliser cette commande."
try:
with open('current_buffer', 'rb') as current_buffer_file:
2022-02-07 18:39:34 +01:00
sbuf.serviceData = pickle.load(current_buffer_file)
2022-02-06 01:54:17 +01:00
return "OK"
2022-02-05 00:46:57 +01:00
2022-02-06 01:54:17 +01:00
except Exception as e:
print(repr(e))
return "Erreur à l'exécution"
2022-02-05 00:46:57 +01:00
2022-02-07 18:39:34 +01:00
def cmdhist(owners, nick, text, sbuf):
"""
Check history for a maintainer
"""
if not nick in sbuf.maintainerData or len(sbuf.maintainerData[nick]) == 0:
return "pas d'historique disponible pour vous."
msg = "voici historique pour le mainteneur %s (vous) :\n" % nick
for host in sbuf.maintainerData[nick]:
msg += "\nHôte %s:" % host
for serviceline in sbuf.maintainerData[nick][host]:
msg += "- %s" % serviceline + "\n"
sbuf.maintainerData[nick] = {}
return msg
2022-02-05 00:46:57 +01:00
# Commands
commandtable = {
2022-02-07 18:39:34 +01:00
"help" : cmdhelp,
"hist" : cmdhist,
"load" : cmdload,
2022-02-06 01:29:59 +01:00
"mainteneur" : cmdmainteneur,
2022-02-07 18:39:34 +01:00
"ping" : cmdping,
2022-02-06 01:54:17 +01:00
"save" : cmdsave,
2022-02-05 00:46:57 +01:00
}