From 035c934d99d0445f63cf79110e880c43b4172ff2 Mon Sep 17 00:00:00 2001 From: Nyo Date: Thu, 19 May 2016 22:53:09 +0200 Subject: [PATCH] .HIDE. .BANCHO. Add public (discord) and private (file) chat logging --- .gitignore | 1 + events/sendPrivateMessageEvent.py | 5 +++ events/sendPublicMessageEvent.py | 8 +++-- helpers/configHelper.py | 7 +++++ helpers/discordBotHelper.py | 51 +++++++++++++++++++++++++++++++ pep.py | 13 ++++++++ 6 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 helpers/discordBotHelper.py diff --git a/.gitignore b/.gitignore index c95ea97..c3b3ef9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ **/__pycache__ config.ini +.data diff --git a/events/sendPrivateMessageEvent.py b/events/sendPrivateMessageEvent.py index 58082cf..7af1967 100644 --- a/events/sendPrivateMessageEvent.py +++ b/events/sendPrivateMessageEvent.py @@ -6,6 +6,7 @@ from objects import glob from objects import fokabot from constants import exceptions from constants import messageTemplates +from time import gmtime, strftime def handle(userToken, packetData): """ @@ -47,6 +48,10 @@ def handle(userToken, packetData): # Console output consoleHelper.printColored("> {}>{}: {}".format(username, packetData["to"], packetData["message"]), bcolors.PINK) + + # Log to file + with open(".data/chatlog_private.txt", "a") as f: + f.write("[{date}] {fro} -> {to}: {message}\n".format(date=strftime("%Y-%m-%d %H:%M:%S", gmtime()), fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1])) except exceptions.tokenNotFoundException: # Token not found, user disconnected consoleHelper.printColored("[!] {} tried to send a message to {}, but their token couldn't be found".format(username, packetData["to"]), bcolors.RED) diff --git a/events/sendPublicMessageEvent.py b/events/sendPublicMessageEvent.py index 6938c23..a619c2e 100644 --- a/events/sendPublicMessageEvent.py +++ b/events/sendPublicMessageEvent.py @@ -5,6 +5,7 @@ from objects import fokabot from helpers import consoleHelper from constants import bcolors from constants import serverPackets +from helpers import discordBotHelper def handle(userToken, packetData): """ @@ -15,7 +16,7 @@ def handle(userToken, packetData): """ try: - # Get uesrToken data + # Get userToken data userID = userToken.userID username = userToken.username userRank = userToken.rank @@ -99,7 +100,10 @@ def handle(userToken, packetData): consoleHelper.printColored("> FokaBot@{}: {}".format(packetData["to"], str(fokaMessage.encode("UTF-8"))), bcolors.PINK) # Console output - consoleHelper.printColored("> {}@{}: {}".format(username, packetData["to"], str(packetData["message"].encode("UTF-8"))), bcolors.PINK) + consoleHelper.printColored("> {}@{}: {}".format(username, packetData["to"], str(packetData["message"].encode("utf-8"))), bcolors.PINK) + + # Discord/file log + discordBotHelper.sendChatlog("**{fro}{type}{to}:** {message}".format(fro=username, type="@" if packetData["to"].startswith("#") else ">", to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1])) except exceptions.channelModeratedException: consoleHelper.printColored("[!] {} tried to send a message to a channel that is in moderated mode ({})".format(username, packetData["to"]), bcolors.RED) except exceptions.channelUnknownException: diff --git a/helpers/configHelper.py b/helpers/configHelper.py index b48ef44..e355704 100644 --- a/helpers/configHelper.py +++ b/helpers/configHelper.py @@ -63,6 +63,9 @@ class config: self.config.get("flask","debug") self.config.get("flask","logger") + self.config.get("discord","enable") + self.config.get("discord","boturl") + self.config.get("ci","key") return True except: @@ -102,6 +105,10 @@ class config: self.config.add_section("ci") self.config.set("ci", "key", "changeme") + self.config.add_section("discord") + self.config.set("discord", "enable", "False") + self.config.set("discord", "boturl", "") + # Write ini to file and close self.config.write(f) f.close() diff --git a/helpers/discordBotHelper.py b/helpers/discordBotHelper.py new file mode 100644 index 0000000..33ecd0a --- /dev/null +++ b/helpers/discordBotHelper.py @@ -0,0 +1,51 @@ +import requests +from objects import glob +from helpers import generalFunctions +from urllib.parse import urlencode + +def sendDiscordMessage(channel, message): + """ + Send a message to a discord server. + This is used with ripple's schiavobot. + + channel -- bunk, staff or general + message -- message to send + """ + if generalFunctions.stringToBool(glob.conf.config["discord"]["enable"]) == True: + requests.get("{}/{}?{}".format(glob.conf.config["discord"]["boturl"], channel, urlencode({ "message": message }))) + + +def sendConfidential(message): + """ + Send a message to #bunker + + message -- message to send + """ + sendDiscordMessage("bunk", message) + + +def sendChatlog(message): + """ + Send a message to #chatlog + + message -- message to send + """ + sendDiscordMessage("chatlog", message) + + +def sendStaff(message): + """ + Send a message to #staff + + message -- message to send + """ + sendDiscordMessage("staff", message) + + +def sendGeneral(message): + """ + Send a message to #general + + message -- message to send + """ + sendDiscordMessage("general", message) diff --git a/pep.py b/pep.py index ee5f14b..cc75889 100644 --- a/pep.py +++ b/pep.py @@ -3,6 +3,7 @@ import logging import sys import flask import datetime +import os # Tornado server from tornado.wsgi import WSGIContainer @@ -14,6 +15,7 @@ from constants import bcolors from constants import packetIDs from constants import serverPackets from helpers import configHelper +from helpers import discordBotHelper from constants import exceptions from objects import glob from objects import fokabot @@ -289,6 +291,14 @@ if __name__ == "__main__": consoleHelper.printColored("[!] Error while loading bancho_settings. Please make sure the table in DB has all the required rows", bcolors.RED) raise + # Create data folder if needed + consoleHelper.printNoNl("> Checking folders... ") + paths = [".data"] + for i in paths: + if not os.path.exists(i): + os.makedirs(i, 0o770) + consoleHelper.printDone() + # Initialize chat channels consoleHelper.printNoNl("> Initializing chat channels... ") glob.channels.loadChannels() @@ -321,6 +331,9 @@ if __name__ == "__main__": serverOutputPackets = generalFunctions.stringToBool(glob.conf.config["server"]["outputpackets"]) serverOutputRequestTime = generalFunctions.stringToBool(glob.conf.config["server"]["outputrequesttime"]) + # Send server start message + discordBotHelper.sendConfidential("w00t p00t! (pep.py started)") + # Run server sanic way if serverName == "tornado": # Tornado server