Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into 5.0
Browse files Browse the repository at this point in the history
Conflicts:
	willie/db.py
	willie/modules/adminchannel.py
	willie/modules/clock.py
	willie/modules/rss.py
	willie/tools.py
  • Loading branch information
embolalia committed Dec 4, 2014
2 parents 1f87499 + fd5bb92 commit 6faaa95
Show file tree
Hide file tree
Showing 23 changed files with 147 additions and 111 deletions.
35 changes: 22 additions & 13 deletions adminchannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@
from __future__ import unicode_literals

import re
from willie import formatting
from willie.module import commands, priority, OP, HALFOP
from willie.tools import Nick


def default_mask(trigger):
welcome = formatting.color('Welcome to:', formatting.colors.PURPLE)
chan = formatting.color(trigger.sender, formatting.colors.TEAL)
topic_ = formatting.bold('Topic:')
topic_ = formatting.color('| ' + topic_, formatting.colors.PURPLE)
arg = formatting.color('{}', formatting.colors.GREEN)
return '{} {} {} {}'.format(welcome, chan, topic_, arg)


@commands('op')
def op(bot, trigger):
"""
Expand Down Expand Up @@ -293,36 +303,35 @@ def topic(bot, trigger):
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
text = trigger.group(2)
if text == '':
if not trigger.group(2):
return
channel = trigger.sender.lower()

narg = 1
mask = None
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'
mask = mask or default_mask(trigger)
mask = mask.replace('%s', '{}')
narg = len(re.findall('{}', mask))

top = trigger.group(2)
text = tuple()
args = []
if top:
text = tuple(unicode.split(top, '~', narg))
args = top.split('~', narg)

if len(text) != narg:
message = "Not enough arguments. You gave " + str(len(text)) + ', it requires ' + str(narg) + '.'
if len(args) != narg:
message = "Not enough arguments. You gave {}, it requires {}.".format(
len(args), narg)
return bot.say(message)
topic = mask % text
topic = mask.format(*args)

bot.write(('TOPIC', channel + ' :' + topic))


@commands('tmask')
def set_mask(bot, trigger):
"""
Set the mask to use for .topic in the current channel. %s is used to allow
Set the mask to use for .topic in the current channel. {} is used to allow
substituting in chunks of text.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
Expand All @@ -337,5 +346,5 @@ def show_mask(bot, trigger):
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
mask = bot.db.get_channel_value(trigger.sender, 'topic_mask')
mask = mask or "%s"
mask = mask or default_mask(trigger)
bot.say(mask)
1 change: 0 additions & 1 deletion announce.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ def announce(bot, trigger):
return
for channel in bot.channels:
bot.msg(channel, '[ANNOUNCEMENT] %s' % trigger.group(2))

9 changes: 5 additions & 4 deletions bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

regex = None


def configure(config):
"""
Expand All @@ -42,8 +43,8 @@ def setup(bot):

domains = '|'.join(bot.config.bugzilla.get_list('domains'))
regex = re.compile((r'https?://(%s)'
'(/show_bug.cgi\?\S*?)'
'(id=\d+)')
'(/show_bug.cgi\?\S*?)'
'(id=\d+)')
% domains)
bot.memory['url_callbacks'][regex] = show_bug

Expand All @@ -53,8 +54,8 @@ def shutdown(bot):


@rule(r'.*https?://(\S+?)'
'(/show_bug.cgi\?\S*?)'
'(id=\d+).*')
'(/show_bug.cgi\?\S*?)'
'(id=\d+).*')
def show_bug(bot, trigger, match=None):
"""Show information about a Bugzilla bug."""
match = match or trigger
Expand Down
3 changes: 1 addition & 2 deletions calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def c(bot, trigger):
except ZeroDivisionError:
result = "Division by zero is not supported in this universe."
except Exception as e:
result = "{error}: {msg}".format(
error=type(e), msg=e)
result = "{error}: {msg}".format(error=type(e), msg=e)
bot.reply(result)


Expand Down
47 changes: 21 additions & 26 deletions clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ def update_user(bot, trigger):
tz = trigger.group(2)
if not tz:
bot.reply("What timezone do you want to set? Try one from "
"http://dft.ba/-tz")
"http://dft.ba/-tz")
return
if tz not in pytz.all_timezones:
bot.reply("I don't know that time zone. Try one from "
"http://dft.ba/-tz")
"http://dft.ba/-tz")
return

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 "
"you use DST.")
bot.say("Okay, {}, but you should use one from http://dft.ba/-tz "
"if you use DST.".format(trigger.nick))
else:
bot.reply('I now have you in the %s time zone.' % tz)

Expand All @@ -81,10 +80,9 @@ def update_user_format(bot, trigger):
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.")
" http://strftime.net to make one.")

tz = get_timezone(bot.db, bot.config, None, None,
trigger.sender)
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')
Expand All @@ -93,16 +91,16 @@ def update_user_format(bot, trigger):
bot.db.set_nick_value(trigger.nick, 'time_format', tformat)

try:
timef = format_time(db = bot.db, zone=tz, nick=trigger.nick)
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.")
" 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)
"timezone is wrong, you might try the settz command)"
% timef)


@commands('channeltz')
Expand All @@ -119,21 +117,20 @@ def update_channel(bot, trigger):
tz = trigger.group(2)
if not tz:
bot.reply("What timezone do you want to set? Try one from "
"http://dft.ba/-tz")
"http://dft.ba/-tz")
return
if tz not in pytz.all_timezones:
bot.reply("I don't know that time zone. Try one from "
"http://dft.ba/-tz")
"http://dft.ba/-tz")
return

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 "
"you use DST.")
bot.say("Okay, {}, but you should use one from http://dft.ba/-tz "
"if you use DST.".format(trigger.nick))
else:
bot.reply(
'I now have {} in the {} time zone.'.format(trigger.sender,tz))
'I now have {} in the {} time zone.'.format(trigger.sender, tz))


@commands('setchanneltimeformat', 'setctf')
Expand All @@ -149,19 +146,17 @@ def update_channel_format(bot, trigger):
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.")
" http://strftime.net to make one.")

tz = get_timezone(bot.db, bot.config, None, None,
trigger.sender)
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.")
" 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)

"unless a user has their own format set. (If the timezone"
" is wrong, you might try the settz and channeltz "
"commands)" % timef)
9 changes: 5 additions & 4 deletions currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_rate(code):
def exchange(bot, trigger):
"""Show the exchange rate between two currencies"""
if not trigger.group(2):
return bot.reply("No search term. An example: .cur 20 EUR in USD")
return bot.reply("No search term. An example: .cur 20 EUR in USD")
match = regex.match(trigger.group(2))
if not match:
# It's apologetic, because it's using Canadian data.
Expand All @@ -68,17 +68,18 @@ def exchange(bot, trigger):
bot.reply("Sorry, I didn't understand the input.")
display(bot, amount, of, to)


def display(bot, amount, of, to):
if not amount:
bot.reply("Zero is zero, no matter what country you're in.")
try:
of_rate, of_name = get_rate(of)
if not of_name:
bot.reply("Unkown currency: %s" % of)
bot.reply("Unknown currency: %s" % of)
return
to_rate, to_name = get_rate(to)
if not to_name:
bot.reply("Unkown currency: %s" % to)
bot.reply("Unknown currency: %s" % to)
return
except Exception as e:
raise
Expand All @@ -87,7 +88,7 @@ def display(bot, amount, of, to):

result = amount / of_rate * to_rate
bot.say("{} {} ({}) = {} {} ({})".format(amount, of, of_name,
result, to, to_name))
result, to, to_name))


@commands('btc', 'bitcoin')
Expand Down
19 changes: 15 additions & 4 deletions dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_number_of_faces(self):
return len(self.dice) + len(self.dropped)


def _roll_dice(dice_expression):
def _roll_dice(bot, dice_expression):
result = re.search(
r"""
(?P<dice_num>\d*)
Expand All @@ -131,11 +131,17 @@ def _roll_dice(dice_expression):
dice_num = int(result.group('dice_num') or 1)
dice_type = int(result.group('dice_type'))

# Dice can't have zero or a negative number of sides.
if dice_type <= 0:
bot.reply("I don't have any dice with %d sides. =(" % dice_type)
return None # Signal there was a problem

# Upper limit for dice should be at most a million. Creating a dict with
# more than a million elements already takes a noticeable amount of time
# on a fast computer and ~55kB of memory.
if dice_num > 1000:
return None
bot.reply('I only have 1000 dice. =(')
return None # Signal there was a problem

dice = DicePouch(dice_num, dice_type, 0)

Expand Down Expand Up @@ -171,13 +177,18 @@ def roll(bot, trigger):
# Get a list of all dice expressions, evaluate them and then replace the
# expressions in the original string with the results. Replacing is done
# using string formatting, so %-characters must be escaped.
if not trigger.group(2):
return bot.reply("No dice to roll.")
arg_str = trigger.group(2)
dice_expressions = re.findall(dice_regexp, arg_str)
arg_str = arg_str.replace("%", "%%")
arg_str = re.sub(dice_regexp, "%s", arg_str)
dice = list(map(_roll_dice, dice_expressions))

f = lambda dice_expr: _roll_dice(bot, dice_expr)
dice = list(map(f, dice_expressions))

if None in dice:
bot.reply("I only have 1000 dice. =(")
# Stop computing roll if there was a problem rolling dice.
return

def _get_eval_str(dice):
Expand Down
15 changes: 10 additions & 5 deletions github.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import re

issueURL = (r'https?://(?:www\.)?github.com/'
'([A-z0-9\-]+/[A-z0-9\-]+)/'
'(?:issues|pull)/'
'([\d]+)')
'([A-z0-9\-]+/[A-z0-9\-]+)/'
'(?:issues|pull)/'
'([\d]+)')
regex = re.compile(issueURL)


def checkConfig(bot):
if not bot.config.has_option('github', 'oauth_token') or not bot.config.has_option('github', 'repo'):
return False
Expand Down Expand Up @@ -96,7 +97,11 @@ def add_traceback(bot, trigger):
# Make sure the API is set up
gitAPI = checkConfig(bot)
if not gitAPI:
return bot.say('Git module not configured, make sure github.oauth_token and github.repo are defined')
return bot.say('GitHub module not configured, make sure github.oauth_token and github.repo are defined')

if not trigger.group(2):
bot.say('Please give both the issue number and the error message.')
return

# Make sure the input is valid
args = trigger.group(2).split(None, 1)
Expand Down Expand Up @@ -195,6 +200,7 @@ def findIssue(bot, trigger):
bot.reply('[#%s]\x02title:\x02 %s \x02|\x02 %s' % (data['number'], data['title'], body))
bot.say(data['html_url'])


@rule('.*%s.*' % issueURL)
def issue_info(bot, trigger, match=None):
match = match or trigger
Expand All @@ -215,4 +221,3 @@ def issue_info(bot, trigger, match=None):
bot.say('The API says this is an invalid issue. Please report this if you know it\'s a correct link!')
return NOLIMIT
bot.say('[#%s]\x02title:\x02 %s \x02|\x02 %s' % (data['number'], data['title'], body))

Loading

0 comments on commit 6faaa95

Please sign in to comment.