isengard-bot/commands.py

144 lines
3.4 KiB
Python

import pickle
import logging
from systemd.journal import JournalHandler
from data import HostData
# Logging
log = logging.getLogger(__name__)
log.addHandler(JournalHandler())
# Isengard commands
def cmdping(owners, nick, text, store):
"""
Ping command.
"""
return "pong !"
def cmdhelp(owners, nick, text, store):
"""
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
def cmdmainteneur(owners, nick, text, store):
"""
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 " \
+ store.knownHosts[host]["maintainer"]
if len(splittedtext) == 3:
host = splittedtext[1]
maintainer = splittedtext[2]
if not host in store.knownHosts:
store.knownHosts[host] = HostData(host)
store.knownHosts[host].maintainer = maintainer
return "le responsable est à présent " + maintainer
except KeyError as e:
log.error(repr(e))
return "machine inconnue (tout le monde est son mainteneur)"
except Exception as e:
log.error(repr(e))
return "erreur à l'exécution"
return "Syntaxe invalide"
def cmdsave(owners, nick, text, store):
"""
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:
pickle.dump(store.knownHosts, current_buffer_file)
return "OK"
except Exception as e:
log.error(repr(e))
return "erreur à l'exécution"
def cmdload(owners, nick, text, store):
"""
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:
store.knownHosts = pickle.load(current_buffer_file)
return "OK"
except Exception as e:
log.error(repr(e))
return "erreur à l'exécution"
def cmdhist(owners, nick, text, store):
"""
Check history for a maintainer
"""
if not nick in store.knownMaintainers or len(store.knownMaintainers[nick]) == 0:
return "pas d'historique disponible pour vous."
msg = "voici les cinq derniers évènements pour chaque hôte notifié:\n"
for host in store.knownMaintainers[nick]:
msg += "\nHôte %s:" % host
count = 0
for serviceline in store.knownMaintainers[nick][host]:
msg += "- %s" % serviceline + "\n"
count += 1
if count >= 5:
break
del store.knownMaintainers[nick]
store.knownMaintainers[nick] = {}
return msg
# Commands
commandtable = {
"help" : cmdhelp,
"hist" : cmdhist,
"load" : cmdload,
"mainteneur" : cmdmainteneur,
"ping" : cmdping,
"save" : cmdsave,
}