From e8fcd048fe805a9caec4eacefd8631dcad9e6638 Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Fri, 10 Feb 2012 15:02:19 +0100 Subject: [PATCH] Feature log on/off --- settings-sample.py | 4 +++- snurr.py | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/settings-sample.py b/settings-sample.py index 99ede79..863c91c 100644 --- a/settings-sample.py +++ b/settings-sample.py @@ -1,4 +1,6 @@ -# snurrbot database configuration +# snurrbot configuration +DISABLE_LOG = False +# DATABASE DB_API_ADAPTER = "MySQLdb" # DB-API 2.0 adapter DB_HOST = "localhost" # hostname DB_USER = "snurr" # username diff --git a/snurr.py b/snurr.py index f342ec7..a06084c 100755 --- a/snurr.py +++ b/snurr.py @@ -3,9 +3,9 @@ import optparse import subprocess import time -import MySQLdb from datetime import datetime + from twisted.words.protocols import irc from twisted.internet import protocol, reactor, ssl from twisted.enterprise import adbapi @@ -13,6 +13,9 @@ from twisted.python import log import settings +if not settings.DISABLE_LOG: + import MySQLdb + class SnurrBot(irc.IRCClient): def __init__(self): self.actions = IRCActions(self) @@ -95,7 +98,8 @@ class UDPListener(protocol.DatagramProtocol): class IRCActions(): def __init__(self, bot): self.bot = bot - self.dbpool = self._get_dbpool() + if not settings.DISABLE_LOG: + self.dbpool = self._get_dbpool() def _get_dbpool(self): # Setup an async db connection @@ -124,10 +128,11 @@ class IRCActions(): text = "" text += "Command: !help\n" text += " This help message\n" - text += "Command: !log DESCRIPTION\n" - text += " Add new entry in log\n" - text += "Command: !lastlog\n" - text += " Last 3 log entries\n" + if not settings.DISABLE_LOG: + text += "Command: !log DESCRIPTION\n" + text += " Add new entry in log\n" + text += "Command: !lastlog\n" + text += " Last 3 log entries\n" text += "Command: !ping HOST\n" text += " Ping target host" return text @@ -145,11 +150,11 @@ class IRCActions(): self.bot.msgReply(nick, channel, self.ping(parts[1])) elif parts[0] == "help" and len(parts) == 1: self.bot.msgReply(nick, channel, self.help()) - elif parts[0] == "log" and len(parts) >= 2: + elif parts[0] == "log" and len(parts) >= 2 and not settings.DISABLE_LOG: # set_log_entry should create a deferred and # the callback should fire when the db returns. self.set_log_entry(nick, parts[1:]).addCallback(self.msg_log_entry, channel, nick) - elif parts[0] == "lastlog" and len(parts) == 1: + elif parts[0] == "lastlog" and len(parts) == 1 and not settings.DISABLE_LOG: # ...same as above self.get_lastlog().addCallback(self.msg_lastlog, channel, nick) else: