--- a/rollbot.py Fri Jun 24 07:36:35 2016 +0800
+++ b/rollbot.py Mon Jul 04 22:56:48 2016 +0800
setdefaultencoding('utf8')
+def simple_markup(string):
+ tagre = re.compile(r'\{(\w+)\}(.*?)\{/\1\}')
+ plain = tagre.sub(r'\2', string)
+ def get_replacement(match):
+ 'i': 'font-style: italic;',
+ 't': 'font-family: monospace;'
+ style = codes.get(code, '')
+ return '<span style="{}">{}</span>'.format(style, text)
+ html = tagre.sub(get_replacement, cgi.escape(string))
regex = re.compile('^(?P<times>\d+)?d(?P<sides>\d+)$', re.IGNORECASE)
- return '%s <X>D<Y> means roll Y-sided die X times, if X is 1 it can be omitted' % self.name
+ '{t}%s <X>D<Y>{/t} to roll {t}Y{/t}-sided die {t}X{/t} times,'
+ ' if {t}X{/t} is 1 it can be omitted' % self.name)
def respond(self, words, message):
def format(self, rolls, total):
+ return '{G}%d{/G}' % total
- return '(%s) = %d' % (' + '.join('%d' % r for r in rolls), total)
+ sequence = ' + '.join('{B}%d{/B}' % r for r in rolls)
+ return '(%s) = {G}%d{/G}' % (sequence, total)
- return '%s or %s <1st-option>[ or] <2nd-option>' % (self.name, self.name)
+ return '{t}%s{/t} {i}or{/i} {t}%s <1st-option>[ or] <2nd-option>{/t}' % (self.name, self.name)
def respond(self, words, message):
return random.choice(options)
def format(self, option):
+ return '{G}%s{/G}' % option
- return '%s or %s <command>' % (self.name, self.name)
+ return '{t}%s{/t} {i}or{/i} {t}%s <command>{/t}' % (self.name, self.name)
def respond(self, words, message):
mbody = mbody[len(self.prefix):]
mbody = mbody.lstrip(' ')
- rbody = self.respond(mbody)
+ rbody, rxbody = self.respond(mbody)
+ msg['html']['body'] = rxbody
def muc_message(self, msg):
mbody = mbody.lstrip(':, ')
- rbody = self.respond(mbody)
+ rbody, rxbody = self.respond(mbody)
rbody = '%s: %s' % (msg['mucnick'], rbody)
+ rxbody = '%s: %s' % (cgi.escape(msg['mucnick']), rxbody)
+ msg['html']['body'] = rxbody
def muc_invite(self, invite):
self.plugin['xep_0045'].joinMUC(room, self.nick, password=password, wait=True)
def respond(self, message, prefix=None):
cmdname = words[0].lower()
if cmdname in self.commands:
command = self.commands[cmdname]
response = command.respond(words, message)
+ plain, html = simple_markup(response)
def lookup(key, args, config, default=None):