Browse Source

Add uptime cmd & changes in cmd system.

- Add bot behavior on unknown command.
- Modified muc got_online presence handling.
- Refactoring.
- Code cleaning.
- Comments.
master
Bogdan Cordier 3 years ago
parent
commit
6c9e3279a1
1 changed files with 119 additions and 39 deletions
  1. +119
    -39
      kaabot.py

+ 119
- 39
kaabot.py View File

@@ -8,10 +8,12 @@ import locale
import dataset
import argparse
import getpass
import random
from sleekxmpp.util.misc_ops import setdefaultencoding

setdefaultencoding('utf8')
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
logger = logging.getLogger(__name__)


class KaaBot(sleekxmpp.ClientXMPP):
@@ -20,20 +22,20 @@ class KaaBot(sleekxmpp.ClientXMPP):

self.room = room
self.nick = nick
self.online_timestamp = None
self.db = dataset.connect('sqlite:///{db}'.format(db=database),
engine_kwargs={'connect_args': {'check_same_thread': False}})
engine_kwargs={'connect_args': {
'check_same_thread': False}})

self.users = self.db['user']
self.muc_log = self.db['muc_log']

self.add_event_handler("session_start", self.session_start)
self.add_event_handler("message", self.message)
# self.add_event_handler("groupchat_message", self.muc_message)
self.add_event_handler("muc::%s::got_online" % self.room, self.muc_online)
self.add_event_handler("muc::%s::got_offline" % self.room, self.muc_offline)

for user in self.users.all():
self.users.update(dict(nick=user['nick'], conn_time=None, last_seen=None), ['nick'])
self.add_event_handler("muc::%s::got_online" % self.room,
self.muc_online)
self.add_event_handler("muc::%s::got_offline" % self.room,
self.muc_offline)

def session_start(self, event):
self.send_presence()
@@ -51,22 +53,32 @@ class KaaBot(sleekxmpp.ClientXMPP):

# Public (MUC) message
elif msg['type'] in ('groupchat'):

if msg['mucnick'] != self.nick and msg['body'] == self.nick:
self.send_help(msg['from'])

# TODO: Gestion fine des commandes pour le bot
elif msg['mucnick'] != self.nick and self.nick in msg['body'] and msg['body'].endswith('/log'):
self.send_log(msg['mucnick'], msg['from'])

# Enregistre les messages dans la bdd exceptés ceux qui viennent du bot.
elif msg['mucnick'] != self.nick and self.nick in msg['body']:
if msg['body'].split()[1] in ['log', 'histo']:
self.send_log(msg['mucnick'], msg['from'])
elif msg['body'].split()[1] in ['help', 'aide']:
self.send_help(msg['from'])
elif msg['body'].split()[1] in ['uptime']:
self.send_uptime(msg['from'])
else:
self.send_insults(msg['from'].bare)

# Insert message in database with timestamp except for
# bot messages or commands.
elif msg['mucnick'] != self.nick:
self.muc_log.insert(dict(datetime=datetime.datetime.now(), msg=msg['body'], user=msg['mucnick']))
self.muc_log.insert(dict(datetime=datetime.datetime.now(),
msg=msg['body'], user=msg['mucnick']))

def send_help(self, dest):
"""Sends help messages to 'dest'
"""
mbody = "Il a besoin d'aide le boulet ?\n- /log : voir les messages postés durant ton absence."
intro = ["Il a besoin d'aide le boulet ?"]
cmd = [
'([back]log|histo[rique]) : Historique des messages postés durant ton absence.',
'(uptime) : Depuis combien de temps je suis debout ? ']
mbody = '\n '.join(intro + cmd)
self.send_message(mto=dest,
mbody=mbody,
mtype='chat')
@@ -74,58 +86,126 @@ class KaaBot(sleekxmpp.ClientXMPP):
def send_log(self, nick, dest):
"""Look up backlog for 'nick' and send it to 'dest'.
"""
last_seen = self.users.find_one(nick=nick)['last_seen']
conn_time = self.users.find_one(nick=nick)['conn_time']

# Get offline timestamp from database.
offline_timestamp = self.users.find_one(nick=nick)['offline_timestamp']
# Get online timestamp from database.
online_timestamp = self.users.find_one(nick=nick)['online_timestamp']

# Since filtered log is a generator we can't know in advance if
# it will be empty. Creating filtered_log_empty allows us to act on
# this event later.
filtered_log_empty = True
filtered_log = (log for log in self.muc_log if log['datetime'] > last_seen and log['datetime'] < conn_time)
filtered_log = (log for log in self.muc_log if
offline_timestamp < log['datetime'] < online_timestamp)
try:
for log in filtered_log:
filtered_log_empty = False
body = log['msg']
user = log['user']
self.send_message(mto=msg['from'],
self.send_message(mto=dest,
mbody=': '.join((user, body)),
mtype='chat')

# Catching TypeError exception if either one of the filtered_log
# variables is None.
except TypeError:
logging.debug('Generator empty')
logging.debug(
'KaaBot : No backlog to send. Filtering messages failed')

#  Send message if filtered_log is still empty.
if filtered_log_empty:
mbody = 'Aucun message depuis ta dernière venue. T\'es content ?'
self.send_message(mto=dest,
mbody='Aucun message depuis ta dernière venue. T\'es content ?',
mbody=mbody,
mtype='chat')

def send_uptime(self, dest):
uptime = str(datetime.datetime.now() - self.online_timestamp)
mbody = 'Je suis debout depuis {uptime}'.format(uptime=uptime)
self.send_message(mto=dest,
mbody=mbody,
mtype='chat')

def send_insults(self, dest):
insults = ["Hé, tu peux apprendre à écrire ?",
"J'y comprends rien à ton charabia",
"T'as perdu l'usage de tes mains ?",
"/me sombre dans une crise existentielle."]

mbody = insults[random.randint(0, len(insults) - 1)]
self.send_message(mto=dest,
mbody=mbody,
mtype='groupchat')

def muc_online(self, presence):
'''Handles MUC online presence.
On bot connection, called for each user in the MUC (bot included).
'''
if presence['muc']['nick'] != self.nick:
print(presence)
# Check if nick in database.
if self.users.find_one(nick=presence['muc']['nick']):
self.users.update(dict(nick=presence['muc']['nick'], conn_time=datetime.datetime.now()), ['nick'])
last_seen = self.users.find_one(nick=presence['muc']['nick'])['last_seen']
msg = 'La dernière fois que j\'ai vu ta pomme c\'était le {date}'
msg_formatted = msg.format(date=datetime.datetime.strftime(last_seen, format="%c"))
self.send_message(mto=presence['from'].bare, mbody=msg_formatted, mtype='groupchat')

# Update nick online timestamp.
self.users.update(dict(nick=presence['muc']['nick'],
online_timestamp=datetime.datetime.now()),
['nick'])

# Check if bot is connecting for the first time.
if self.online_timestamp:
try:
offline_timestamp = \
self.users.find_one(nick=presence['muc']['nick'])[
'offline_timestamp']
msg = "La dernière fois que j'ai vu ta pomme c'était le {date}"
msg_formatted = msg.format(
date=datetime.datetime.strftime(offline_timestamp,
format="%c"))
self.send_message(mto=presence['from'].bare,
mbody=msg_formatted,
mtype='groupchat')
except TypeError:
msg = 'KaaBot : No offline timestamp yet for {nick}'
logging.debug(msg.format(nick=presence['muc']['nick']))
else:
self.users.insert(dict(nick=presence['muc']['nick'], conn_time=datetime.datetime.now()))
self.users.insert(dict(nick=presence['muc']['nick'],
online_timestamp=datetime.datetime.now()))
else:
# Set bot online timestamp.
self.online_timestamp = datetime.datetime.now()
self.send_message(mto=presence['from'].bare,
mbody='/me est dans la place !',
mtype='groupchat')

def muc_offline(self, presence):
'''Handles MUC offline presence.
'''
if presence['muc']['nick'] != self.nick:
if self.users.find_one(nick=presence['muc']['nick']):
self.users.update(dict(nick=presence['muc']['nick'], last_seen=datetime.datetime.now()), ['nick'])
self.users.update(dict(nick=presence['muc']['nick'],
offline_timestamp=datetime.datetime.now()),
['nick'])
else:
self.users.insert(dict(nick=presence['muc']['nick'], last_seen=datetime.datetime.now()))
self.users.insert(dict(nick=presence['muc']['nick'],
offline_timestamp=datetime.datetime.now()))


if __name__ == '__main__':

argp = argparse.ArgumentParser(description="Super Simple Silly Bot for Jabber")
argp.add_argument('-d', '--debug', help='set logging to DEBUG', action='store_const',
dest='loglevel', const=logging.DEBUG, default=logging.INFO)
argp = argparse.ArgumentParser(
description="Super Simple Silly Bot for Jabber")
argp.add_argument('-d', '--debug', help='set logging to DEBUG',
action='store_const',
dest='loglevel', const=logging.DEBUG,
default=logging.INFO)
argp.add_argument("-j", "--jid", dest="jid", help="JID to use")
argp.add_argument("-p", "--password", dest="password", help="password to use")
argp.add_argument("-m", "--muc", dest="muc", help="Multi User Chatroom to join")
argp.add_argument("-p", "--password", dest="password",
help="password to use")
argp.add_argument("-m", "--muc", dest="muc",
help="Multi User Chatroom to join")
argp.add_argument("-n", "--nick", dest="nick", default='KaaBot',
help="Nickname to use in the chatroom (default: KaaBot)")
argp.add_argument("-db", "--database", dest="database", help="database to use", default='muc_log.db')
argp.add_argument("-db", "--database", dest="database",
help="database to use", default="muc_log.db")

args = argp.parse_args()


Loading…
Cancel
Save