2015-04-25 07:37:07 +02:00
|
|
|
import string
|
2015-06-17 05:46:01 +02:00
|
|
|
import re
|
2015-04-25 07:37:07 +02:00
|
|
|
|
2015-05-31 21:20:09 +02:00
|
|
|
global bot_commands
|
|
|
|
# This should be a mapping of command names to functions
|
|
|
|
bot_commands = {}
|
|
|
|
|
2015-04-25 07:37:07 +02:00
|
|
|
# From http://www.inspircd.org/wiki/Modules/spanningtree/UUIDs.html
|
2015-06-17 06:02:38 +02:00
|
|
|
chars = string.ascii_uppercase + string.digits
|
2015-04-25 07:37:07 +02:00
|
|
|
iters = [iter(chars) for _ in range(6)]
|
2015-06-17 06:02:38 +02:00
|
|
|
uidchars = [next(char) for char in iters]
|
2015-04-25 07:37:07 +02:00
|
|
|
|
|
|
|
def next_uid(sid, level=-1):
|
|
|
|
try:
|
2015-06-17 06:02:38 +02:00
|
|
|
uidchars[level] = next(iters[level])
|
|
|
|
return sid + ''.join(uidchars)
|
2015-04-25 07:37:07 +02:00
|
|
|
except StopIteration:
|
2015-06-17 06:02:38 +02:00
|
|
|
return next_uid(sid, level-1)
|
2015-05-31 08:00:39 +02:00
|
|
|
|
2015-05-31 21:20:09 +02:00
|
|
|
def msg(irc, target, text, notice=False):
|
2015-05-31 08:00:39 +02:00
|
|
|
command = 'NOTICE' if notice else 'PRIVMSG'
|
2015-06-17 05:05:41 +02:00
|
|
|
irc.proto._sendFromUser(irc, irc.pseudoclient.uid, '%s %s :%s' % (command, target, text))
|
2015-05-31 21:20:09 +02:00
|
|
|
|
|
|
|
def add_cmd(func, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = func.__name__
|
|
|
|
name = name.lower()
|
|
|
|
bot_commands[name] = func
|
2015-06-08 04:31:56 +02:00
|
|
|
|
2015-06-08 04:36:21 +02:00
|
|
|
def nickToUid(irc, nick):
|
2015-06-08 04:31:56 +02:00
|
|
|
for k, v in irc.users.items():
|
|
|
|
if v.nick == nick:
|
|
|
|
return k
|
2015-06-17 05:46:01 +02:00
|
|
|
|
|
|
|
# A+ regex
|
|
|
|
_nickregex = r'^[A-Za-z\|\\_\[\]\{\}\^\`][A-Z0-9a-z\-\|\\_\[\]\{\}\^\`]*$'
|
|
|
|
def isNick(s, nicklen=None):
|
|
|
|
if nicklen and len(s) > nicklen:
|
|
|
|
return False
|
|
|
|
return bool(re.match(_nickregex, s))
|
|
|
|
|
|
|
|
def isChannel(s):
|
|
|
|
return bool(s.startswith('#'))
|