isengard-bot/commands.py

148 lines
3.7 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
2022-02-08 20:25:51 +01:00
from data import HostData
2022-02-08 15:46:17 +01:00
# Logging
log = logging.getLogger(__name__)
log.addHandler(JournalHandler())
2022-02-05 00:46:57 +01:00
# Isengard commands
2022-02-08 20:25:51 +01:00
def cmdping(owners, nick, text, store):
2022-02-05 00:46:57 +01:00
"""
Ping command.
"""
return "pong !"
2022-02-08 20:25:51 +01:00
def cmdhelp(owners, nick, text, store):
2022-02-07 18:39:34 +01:00
"""
Ping command.
"""
global commandtable
2022-02-08 20:25:51 +01:00
msg = "je suis Isengard, le bot de supervision de ce salon.\n"
2022-02-07 18:39:34 +01:00
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-08 20:25:51 +01:00
def cmdmainteneur(owners, nick, text, store):
2022-02-06 01:29:59 +01:00
"""
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]
2022-02-14 17:41:42 +01:00
for realhost in store.knownHosts:
if host in realhost:
return "le responsable de cette machine est " \
+ store.knownHosts[realhost].maintainer
else:
return "machine inconnue (tout le monde est son mainteneur ou bien elle n'existe pas)"
2022-02-06 01:29:59 +01:00
if len(splittedtext) == 3:
host = splittedtext[1]
maintainer = splittedtext[2]
2022-02-08 20:25:51 +01:00
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)"
2022-02-06 01:29:59 +01:00
2022-02-09 10:01:08 +01:00
# except Exception as e:
# log.error(repr(e))
# return "erreur à l'exécution"
2022-02-06 01:29:59 +01:00
return "Syntaxe invalide"
2022-02-05 00:46:57 +01:00
2022-02-08 20:25:51 +01:00
def cmdsave(owners, nick, text, store):
2022-02-06 01:54:17 +01:00
"""
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-08 20:25:51 +01:00
pickle.dump(store.knownHosts, 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:
2022-02-08 20:25:51 +01:00
log.error(repr(e))
return "erreur à l'exécution"
2022-02-05 00:46:57 +01:00
2022-02-08 20:25:51 +01:00
def cmdload(owners, nick, text, store):
2022-02-06 01:54:17 +01:00
"""
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-08 20:25:51 +01:00
store.knownHosts = 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:
2022-02-08 20:25:51 +01:00
log.error(repr(e))
return "erreur à l'exécution"
2022-02-05 00:46:57 +01:00
2022-02-08 20:25:51 +01:00
def cmdhist(owners, nick, text, store):
2022-02-07 18:39:34 +01:00
"""
Check history for a maintainer
"""
2022-02-08 20:25:51 +01:00
if not nick in store.knownMaintainers or len(store.knownMaintainers[nick]) == 0:
2022-02-07 18:39:34 +01:00
return "pas d'historique disponible pour vous."
2022-02-08 20:25:51 +01:00
msg = "voici les cinq derniers évènements pour chaque hôte notifié:\n"
for host in store.knownMaintainers[nick]:
2022-02-17 11:27:35 +01:00
msg += "\nHôte %s:\n" % host
2022-02-07 18:39:34 +01:00
2022-02-08 20:25:51 +01:00
count = 0
for serviceline in store.knownMaintainers[nick][host]:
2022-02-07 18:39:34 +01:00
msg += "- %s" % serviceline + "\n"
2022-02-08 20:25:51 +01:00
count += 1
if count >= 5:
break
2022-02-07 18:39:34 +01:00
2022-02-08 20:25:51 +01:00
del store.knownMaintainers[nick]
store.knownMaintainers[nick] = {}
2022-02-07 18:39:34 +01:00
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
}