diff --git a/adminchannel.py b/adminchannel.py index 2cda4b5ffb..80e6e9a7d8 100644 --- a/adminchannel.py +++ b/adminchannel.py @@ -15,12 +15,6 @@ from willie.tools import Nick -def setup(bot): - #Having a db means pref's exists. Later, we can just use `if bot.db`. - if bot.db and not bot.db.preferences.has_columns('topic_mask'): - bot.db.preferences.add_columns(['topic_mask']) - - @commands('op') def op(bot, trigger): """ @@ -228,9 +222,9 @@ def quiet(bot, trigger): @commands('unquiet') def unquiet(bot, trigger): """ - This gives admins the ability to unquiet a user. - The bot must be a Channel Operator for this command to work. - """ + This gives admins the ability to unquiet a user. + The bot must be a Channel Operator for this command to work. + """ if bot.privileges[trigger.sender][trigger.nick] < OP: return if bot.privileges[trigger.sender][bot.nick] < OP: @@ -257,10 +251,10 @@ def unquiet(bot, trigger): @priority('high') def kickban(bot, trigger): """ - This gives admins the ability to kickban a user. - The bot must be a Channel Operator for this command to work. - .kickban [#chan] user1 user!*@* get out of here - """ + This gives admins the ability to kickban a user. + The bot must be a Channel Operator for this command to work. + .kickban [#chan] user1 user!*@* get out of here + """ if bot.privileges[trigger.sender][trigger.nick] < OP: return if bot.privileges[trigger.sender][bot.nick] < HALFOP: @@ -306,9 +300,8 @@ def topic(bot, trigger): narg = 1 mask = None - if bot.db and channel in bot.db.preferences: - mask = bot.db.preferences.get(channel, 'topic_mask') - narg = len(re.findall('%s', mask)) + mask = bot.db.get_channel_value(channel, 'topic_mask') + narg = len(re.findall('%s', mask)) if not mask or mask == '': mask = purple + 'Welcome to: ' + green + channel + purple \ + ' | ' + bold + 'Topic: ' + bold + green + '%s' @@ -334,11 +327,8 @@ def set_mask(bot, trigger): """ if bot.privileges[trigger.sender][trigger.nick] < OP: return - if not bot.db: - bot.say("I'm afraid I can't do that.") - else: - bot.db.preferences.update(trigger.sender.lower(), {'topic_mask': trigger.group(2)}) - bot.say("Gotcha, " + trigger.nick) + bot.db.set_channel_value(trigger.sender, 'topic_mask', trigger.group(2)) + bot.say("Gotcha, " + trigger.nick) @commands('showmask') @@ -346,9 +336,6 @@ def show_mask(bot, trigger): """Show the topic mask for the current channel.""" if bot.privileges[trigger.sender][trigger.nick] < OP: return - if not bot.db: - bot.say("I'm afraid I can't do that.") - elif trigger.sender.lower() in bot.db.preferences: - bot.say(bot.db.preferences.get(trigger.sender.lower(), 'topic_mask')) - else: - bot.say("%s") + mask = bot.db.get_channel_value(trigger.sender, 'topic_mask') + mask = mask or "%s" + bot.say(mask) diff --git a/clock.py b/clock.py index 9e86b5b634..0145045d0d 100644 --- a/clock.py +++ b/clock.py @@ -26,14 +26,6 @@ def configure(config): 'Preferred time format (http://strftime.net)', '%F - %T%Z') -def setup(bot): - #Having a db means pref's exists. Later, we can just use `if bot.db`. - if bot.db and not bot.db.preferences.has_columns('tz'): - bot.db.preferences.add_columns(['tz']) - if bot.db and not bot.db.preferences.has_columns('time_format'): - bot.db.preferences.add_columns(['time_format']) - - @commands('t', 'time') @example('.t America/New_York') def f_time(bot, trigger): @@ -59,8 +51,6 @@ def update_user(bot, trigger): """ if not pytz: bot.reply("Sorry, I don't have timezone support installed.") - elif not bot.db: - bot.reply("I can't remember that; I don't have a database.") else: tz = trigger.group(2) if not tz: @@ -72,7 +62,7 @@ def update_user(bot, trigger): "http://dft.ba/-tz") return - bot.db.preferences.update(trigger.nick, {'tz': tz}) + bot.db.set_nick_value(trigger.nick, 'timezone', tz) if len(tz) < 7: bot.say("Okay, " + trigger.nick + ", but you should use one from http://dft.ba/-tz if " @@ -88,34 +78,31 @@ def update_user_format(bot, trigger): Sets your preferred format for time. Uses the standard strftime format. You can use http://strftime.net or your favorite search engine to learn more. """ - if bot.db: - tformat = trigger.group(2) - if not tformat: - bot.reply("What format do you want me to use? Try using" - " http://strftime.net to make one.") - - tz = get_timezone(bot.db, bot.config, None, None, - trigger.sender) - - # Get old format as back-up - old_format = bot.db.preferences.get(trigger.nick, 'time_format') - - # Save the new format in the database so we can test it. - bot.db.preferences.update(trigger.nick, {'time_format': tformat}) - - try: - timef = format_time(db = bot.db, zone=tz, nick=trigger.nick) - except: - bot.reply("That format doesn't work. Try using" - " http://strftime.net to make one.") - # New format doesn't work. Revert save in database. - bot.db.preferences.update(trigger.nick, {'time_format': old_format}) - return - bot.reply("Got it. Your time will now appear as %s. (If the " - "timezone is wrong, you might try the settz command)" - % timef) - else: - bot.reply("I can't remember that; I don't have a database.") + tformat = trigger.group(2) + if not tformat: + bot.reply("What format do you want me to use? Try using" + " http://strftime.net to make one.") + + tz = get_timezone(bot.db, bot.config, None, None, + trigger.sender) + + # Get old format as back-up + old_format = bot.db.get_nick_value(trigger.nick, 'time_format') + + # Save the new format in the database so we can test it. + bot.db.set_nick_value(trigger.nick, 'time_format', tformat) + + try: + timef = format_time(db = bot.db, zone=tz, nick=trigger.nick) + except: + bot.reply("That format doesn't work. Try using" + " http://strftime.net to make one.") + # New format doesn't work. Revert save in database. + bot.db.set_nick_value(trigger.nick, 'time_format', old_format) + return + bot.reply("Got it. Your time will now appear as %s. (If the " + "timezone is wrong, you might try the settz command)" + % timef) @commands('channeltz') @@ -128,8 +115,6 @@ def update_channel(bot, trigger): return elif not pytz: bot.reply("Sorry, I don't have timezone support installed.") - elif not bot.db: - bot.reply("I can't remember that; I don't have a database.") else: tz = trigger.group(2) if not tz: @@ -141,7 +126,7 @@ def update_channel(bot, trigger): "http://dft.ba/-tz") return - bot.db.preferences.update(trigger.sender, {'tz': tz}) + bot.db.set_channel_value(trigger.sender, 'timezone', tz) if len(tz) < 7: bot.say("Okay, " + trigger.nick + ", but you should use one from http://dft.ba/-tz if " @@ -160,25 +145,23 @@ def update_channel_format(bot, trigger): """ if bot.privileges[trigger.sender][trigger.nick] < OP: return - elif not bot.db: - bot.reply("I can't remember that; I don't have a database.") - else: - tformat = trigger.group(2) - if not tformat: - bot.reply("What format do you want me to use? Try using" - " http://strftime.net to make one.") - - tz = get_timezone(bot.db, bot.config, None, None, - trigger.sender) - try: - timef = format_time(zone=tz) - except: - bot.reply("That format doesn't work. Try using" - " http://strftime.net to make one.") - return - bot.db.preferences.update(trigger.sender, {'time_format': tformat}) - bot.reply("Got it. Times in this channel will now appear as %s " - "unless a user has their own format set. (If the timezone" - " is wrong, you might try the settz and channeltz " - "commands)" % timef) + + tformat = trigger.group(2) + if not tformat: + bot.reply("What format do you want me to use? Try using" + " http://strftime.net to make one.") + + tz = get_timezone(bot.db, bot.config, None, None, + trigger.sender) + try: + timef = format_time(zone=tz) + except: + bot.reply("That format doesn't work. Try using" + " http://strftime.net to make one.") + return + bot.db.set_channel_value(trigger.sender, 'time_format', tformat) + bot.reply("Got it. Times in this channel will now appear as %s " + "unless a user has their own format set. (If the timezone" + " is wrong, you might try the settz and channeltz " + "commands)" % timef) diff --git a/rss.py b/rss.py index e772b30581..18d366fafa 100644 --- a/rss.py +++ b/rss.py @@ -27,8 +27,6 @@ def setup(bot): bot.memory['rss_manager'] = RSSManager(bot) - if not bot.db: - raise ConfigurationError("Database not set up, or unavailable.") conn = bot.db.connect() c = conn.cursor() @@ -38,31 +36,14 @@ def setup(bot): c.execute('SELECT * FROM rss_feeds') except StandardError: create_table(bot, c) - migrate_from_old_tables(bot, c) - - # These tables are no longer used, but lets not delete them right away. - # c.execute('DROP TABLE IF EXISTS rss') - # c.execute('DROP TABLE IF EXISTS recent') - - conn.commit() - - # The modified column was added on 2013-07-21. - try: - c.execute('SELECT modified FROM rss_feeds') - except StandardError: - c.execute('ALTER TABLE rss_feeds ADD modified TEXT') conn.commit() - conn.close() def create_table(bot, c): # MySQL needs to only compare on the first n characters of a TEXT field # but SQLite won't accept the syntax needed to make it do it. - if bot.db.type == 'mysql': - primary_key = '(channel(254), feed_name(254))' - else: - primary_key = '(channel, feed_name)' + primary_key = '(channel, feed_name)' c.execute('''CREATE TABLE IF NOT EXISTS rss_feeds ( channel TEXT, @@ -80,41 +61,6 @@ def create_table(bot, c): )'''.format(primary_key)) -def migrate_from_old_tables(bot, c): - sub = bot.db.substitution - - try: - c.execute('SELECT * FROM rss') - oldfeeds = c.fetchall() - except StandardError: - oldfeeds = [] - - for feed in oldfeeds: - channel, site_name, site_url, fg, bg = feed - - # get recent article if possible - try: - c.execute(''' - SELECT article_title, article_url FROM recent - WHERE channel = {0} AND site_name = {0} - '''.format(sub), (channel, site_name)) - article_title, article_url = c.fetchone() - except (StandardError, TypeError): - article_title = article_url = None - - # add feed to new table - if article_url: - c.execute(''' - INSERT INTO rss_feeds (channel, feed_name, feed_url, fg, bg, article_title, article_url) - VALUES ({0}, {0}, {0}, {0}, {0}, {0}, {0}) - '''.format(sub), (channel, site_name, site_url, fg, bg, article_title, article_url)) - else: - c.execute(''' - INSERT INTO rss_feeds (channel, feed_name, feed_url, fg, bg) - VALUES ({0}, {0}, {0}, {0}, {0}) - '''.format(sub), (channel, site_name, site_url, fg, bg)) - - def colour_text(text, fg, bg=''): """Given some text and fore/back colours, return a coloured text string.""" if fg == '': @@ -133,7 +79,6 @@ def manage_rss(bot, trigger): class RSSManager: def __init__(self, bot): self.running = True - self.sub = bot.db.substitution # get a list of all methods in this class that start with _rss_ self.actions = sorted(method[5:] for method in dir(self) if method[:5] == '_rss_') @@ -202,19 +147,19 @@ def _rss_add(self, bot, trigger, c): bg = int(match.group(5)) % 16 if match.group(5) else '' c.execute(''' - SELECT * FROM rss_feeds WHERE channel = {0} AND feed_name = {0} - '''.format(self.sub), (channel, feed_name)) + SELECT * FROM rss_feeds WHERE channel = ? AND feed_name = ? + ''', (channel, feed_name)) if not c.fetchone(): c.execute(''' INSERT INTO rss_feeds (channel, feed_name, feed_url, fg, bg) - VALUES ({0}, {0}, {0}, {0}, {0}) - '''.format(self.sub), (channel, feed_name, feed_url, fg, bg)) + VALUES (?, ?, ?, ?, ?) + ''', (channel, feed_name, feed_url, fg, bg)) bot.reply("Successfully added the feed to the channel.") else: c.execute(''' - UPDATE rss_feeds SET feed_url = {0}, fg = {0}, bg = {0} - WHERE channel = {0} AND feed_name = {0} - '''.format(self.sub), (feed_url, fg, bg, channel, feed_name)) + UPDATE rss_feeds SET feed_url = ?, fg = ?, bg = ? + WHERE channel = ? AND feed_name = ? + ''', (feed_url, fg, bg, channel, feed_name)) bot.reply("Successfully modified the feed.") return True @@ -238,9 +183,9 @@ def _rss_del(self, bot, trigger, c): args = [arg for arg in (channel, feed_name) if arg] c.execute(('DELETE FROM rss_feeds WHERE ' - + ('channel = {0} AND ' if channel else '') - + ('feed_name = {0}' if feed_name else '') - ).rstrip(' AND ').format(self.sub), args) + + ('channel = ? AND ' if channel else '') + + ('feed_name = ?' if feed_name else '') + ).rstrip(' AND '), args) if c.rowcount: noun = 'feeds' if c.rowcount != 1 else 'feed' @@ -278,10 +223,10 @@ def _toggle(self, bot, trigger, c): feed_name = match.group(3).strip('"') if match.group(3) else None args = [arg for arg in (enabled, channel, feed_name) if arg is not None] - c.execute(('UPDATE rss_feeds SET enabled = {0} WHERE ' - + ('channel = {0} AND ' if channel else '') - + ('feed_name = {0}' if feed_name else '') - ).rstrip(' AND ').format(self.sub), args) + c.execute(('UPDATE rss_feeds SET enabled = ? WHERE ' + + ('channel = ? AND ' if channel else '') + + ('feed_name = ?' if feed_name else '') + ).rstrip(' AND '), args) if c.rowcount: noun = 'feeds' if c.rowcount != 1 else 'feed' @@ -373,7 +318,6 @@ def read_feeds(bot, force=False): if not bot.memory['rss_manager'].running and not force: return - sub = bot.db.substitution conn = bot.db.connect() c = conn.cursor() c.execute('SELECT * FROM rss_feeds') @@ -388,9 +332,9 @@ def read_feeds(bot, force=False): def disable_feed(): c.execute(''' - UPDATE rss_feeds SET enabled = {0} - WHERE channel = {0} AND feed_name = {0} - '''.format(sub), (0, feed.channel, feed.name)) + UPDATE rss_feeds SET enabled = ? + WHERE channel = ? AND feed_name = ? + ''', (0, feed.channel, feed.name)) conn.commit() try: @@ -414,9 +358,9 @@ def disable_feed(): "Got HTTP 301 (Moved Permanently) on {0}, updating URI to {1}".format( feed.name, fp.href), 'warning') c.execute(''' - UPDATE rss_feeds SET feed_url = {0} - WHERE channel = {0} AND feed_name = {0} - '''.format(sub), (fp.href, feed.channel, feed.name)) + UPDATE rss_feeds SET feed_url = ? + WHERE channel = ? AND feed_name = ? + ''', (fp.href, feed.channel, feed.name)) conn.commit() elif status == 410: # GONE @@ -447,9 +391,9 @@ def disable_feed(): # save article title, url, and modified date c.execute(''' UPDATE rss_feeds - SET article_title = {0}, article_url = {0}, published = {0}, etag = {0}, modified = {0} - WHERE channel = {0} AND feed_name = {0} - '''.format(sub), (entry.title, entry.link, entry_dt, feed_etag, feed_modified, + SET article_title = ?, article_url = ?, published = ?, etag = ?, modified = ? + WHERE channel = ? AND feed_name = ? + ''', (entry.title, entry.link, entry_dt, feed_etag, feed_modified, feed.channel, feed.name)) conn.commit() @@ -472,9 +416,7 @@ def disable_feed(): timestamp = entry_update_dt or entry_dt if timestamp: # attempt to get time format from preferences - tformat = '' - if feed.channel in bot.db.preferences: - tformat = bot.db.preferences.get(feed.channel, 'time_format') or tformat + tformat = bot.db.get_channel_value(feed.channel, 'time_format') if not tformat and bot.config.has_option('clock', 'time_format'): tformat = bot.config.clock.time_format diff --git a/safety.py b/safety.py index 66fc43ac7d..51502fa315 100644 --- a/safety.py +++ b/safety.py @@ -50,8 +50,6 @@ def configure(config): def setup(bot): if not bot.config.has_section('safety'): raise ConfigurationError("Safety module not configured") - if bot.db and not bot.db.preferences.has_columns('safety'): - bot.db.preferences.add_columns(['safety']) bot.memory['safety_cache'] = willie.tools.WillieMemory() for item in bot.config.safety.get_list('known_good'): known_good.append(re.compile(item, re.I)) @@ -90,8 +88,8 @@ def url_handler(bot, trigger): else: check = bool(check) # DB overrides config: - if bot.db and trigger.sender.lower() in bot.db.preferences: - setting = bot.db.preferences.get(trigger.sender.lower(), 'safety') + setting = bot.db.get_channel_value(trigger.sender, 'safety') + if setting is not None: if setting == 'off': return # Not checking elif setting in ['on', 'strict', 'local', 'local strict']: @@ -161,14 +159,11 @@ def toggle_safety(bot, trigger): options = ' / '.join(allowed_states) bot.reply('Available options: %s' % options) return - if not bot.db: - bot.reply('No database configured, can\'t modify settings') - return if not trigger.isop and not trigger.admin: bot.reply('Only channel operators can change safety settings') channel = trigger.sender.lower() - bot.db.preferences.update(channel, {'safety': trigger.group(2).lower()}) + bot.db.set_channel_value(channel, 'safety', trigger.group(2).lower()) bot.reply('Safety is now set to %s in this channel' % trigger.group(2)) diff --git a/weather.py b/weather.py index 3929ea5f75..4ffd51ce7a 100644 --- a/weather.py +++ b/weather.py @@ -16,12 +16,6 @@ from lxml import etree -def setup(bot): - # Having a db means pref's exists. Later, we can just use `if bot.db`. - if bot.db and not bot.db.preferences.has_columns('woeid'): - bot.db.preferences.add_columns(['woeid']) - - def woeid_search(query): """ Find the first Where On Earth ID for the given query. Result is the etree @@ -134,16 +128,14 @@ def weather(bot, trigger): location = trigger.group(2) woeid = '' if not location: - if bot.db and trigger.nick in bot.db.preferences: - woeid = bot.db.preferences.get(trigger.nick, 'woeid') + woeid = bot.db.get_nick_value(trigger.nick, 'woeid') if not woeid: return bot.msg(trigger.sender, "I don't know where you live. " + 'Give me a location, like .weather London, or tell me where you live by saying .setlocation London, for example.') else: location = location.strip() - if bot.db and location in bot.db.preferences: - woeid = bot.db.preferences.get(location, 'woeid') - else: + woeid = bot.db.get_channel_value(location, 'woeid') + if woeid is None: first_result = woeid_search(location) if first_result is not None: woeid = first_result.find('woeid').text @@ -171,23 +163,20 @@ def update_woeid(bot, trigger): bot.reply('Give me a location, like "Washington, DC" or "London".') return NOLIMIT - if bot.db: - first_result = woeid_search(trigger.group(2)) - if first_result is None: - return bot.reply("I don't know where that is.") + first_result = woeid_search(trigger.group(2)) + if first_result is None: + return bot.reply("I don't know where that is.") - woeid = first_result.find('woeid').text + woeid = first_result.find('woeid').text - bot.db.preferences.update(trigger.nick, {'woeid': woeid}) + bot.db.set_nick_value(trigger.nick, 'woeid', woeid) - neighborhood = first_result.find('neighborhood').text or '' - if neighborhood: - neighborhood += ',' - city = first_result.find('city').text or '' - state = first_result.find('state').text or '' - country = first_result.find('country').text or '' - uzip = first_result.find('uzip').text or '' - bot.reply('I now have you at WOEID %s (%s %s, %s, %s %s.)' % - (woeid, neighborhood, city, state, country, uzip)) - else: - bot.reply("I can't remember that; I don't have a database.") + neighborhood = first_result.find('neighborhood').text or '' + if neighborhood: + neighborhood += ',' + city = first_result.find('city').text or '' + state = first_result.find('state').text or '' + country = first_result.find('country').text or '' + uzip = first_result.find('uzip').text or '' + bot.reply('I now have you at WOEID %s (%s %s, %s, %s %s.)' % + (woeid, neighborhood, city, state, country, uzip))