BotLogMauve/log.py

112 lines
4.2 KiB
Python

#!/usr/bin/env python3
import logging
import os
from datetime import datetime
from getpass import getpass
from argparse import ArgumentParser
import slixmpp
class MUCBot(slixmpp.ClientXMPP):
def __init__(self, jid, password, rooms, nick):
slixmpp.ClientXMPP.__init__(self, jid, password)
self.rooms = rooms
self.nick = nick
self.add_event_handler("session_start", self.start)
self.add_event_handler("groupchat_message", self.muc_message)
for room in self.rooms:
self.add_event_handler("muc::%s::got_online" % room, self.muc_online)
self.add_event_handler("muc::%s::got_offline" % room, self.muc_offline)
def start(self, event):
for room in self.rooms:
self.plugin['xep_0045'].join_muc(room,
self.nick,
# If a room password is needed, use:
# password=the_room_password,
wait=True)
def muc_message(self, message):
room = message['from'].bare
nick = message['from'].resource
self.log(room, 'R', '<%s> %s' % (message['from'].resource, message['body']))
def muc_online(self, presence):
room = presence['muc']['room']
nick = presence['muc']['nick']
status = ' (%s)' % presence['status'] if presence['status'] else ''
self.log(room, 'I', '---> %s joined the room%s' % (nick, status))
def muc_offline(self, presence):
room = presence['muc']['room']
nick = presence['muc']['nick']
status = ' (%s)' % presence['status'] if presence['status'] else ''
self.log(room, 'I', '<--- %s left the room%s' % (nick, status))
def log(self, room, typ, message):
now = datetime.utcnow()
day = now.strftime('%Y-%m-%d')
filename = '%s/%s.log' % (room, day)
os.makedirs(room, exist_ok=True)
with open(filename, 'a') as out:
timestamp = now.isoformat() + 'Z'
split_message = message.split('\n')
nb_lines = len(split_message) - 1
final_message = '\n '.join(split_message)
lines = 'M%s %s %03d %s\n' % (typ, timestamp, nb_lines, final_message)
out.write(lines)
if __name__ == '__main__':
# Setup the command line arguments.
parser = ArgumentParser()
# Output verbosity options.
parser.add_argument("-q", "--quiet", help="set logging to ERROR",
action="store_const", dest="loglevel",
const=logging.ERROR, default=logging.INFO)
parser.add_argument("-d", "--debug", help="set logging to DEBUG",
action="store_const", dest="loglevel",
const=logging.DEBUG, default=logging.INFO)
# JID and password options.
parser.add_argument("-j", "--jid", dest="jid",
help="JID to use")
parser.add_argument("-p", "--password", dest="password",
help="password to use")
parser.add_argument("-r", "--rooms", dest="rooms", nargs='+',
help="MUC rooms to join")
parser.add_argument("-n", "--nick", dest="nick",
help="MUC nickname")
args = parser.parse_args()
# Setup logging.
logging.basicConfig(level=args.loglevel,
format='%(levelname)-8s %(message)s')
if args.jid is None:
args.jid = input("Username: ")
if args.password is None:
args.password = getpass("Password: ")
if args.rooms is None:
args.rooms = input("MUC rooms: ")
if args.nick is None:
args.nick = input("MUC nickname: ")
# Setup the MUCBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
xmpp = MUCBot(args.jid, args.password, args.rooms, args.nick)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
xmpp.process()