2015-07-05 21:48:39 +02:00
|
|
|
from collections import defaultdict
|
2015-07-08 03:07:20 +02:00
|
|
|
|
|
|
|
from log import log
|
|
|
|
import main
|
2015-07-07 04:00:20 +02:00
|
|
|
import time
|
2015-07-05 21:48:39 +02:00
|
|
|
|
2015-06-07 07:17:45 +02:00
|
|
|
class IrcUser():
|
|
|
|
def __init__(self, nick, ts, uid, ident='null', host='null',
|
|
|
|
realname='PyLink dummy client', realhost='null',
|
2015-06-21 05:58:25 +02:00
|
|
|
ip='0.0.0.0', modes=set()):
|
2015-06-07 07:17:45 +02:00
|
|
|
self.nick = nick
|
|
|
|
self.ts = ts
|
|
|
|
self.uid = uid
|
|
|
|
self.ident = ident
|
|
|
|
self.host = host
|
|
|
|
self.realhost = realhost
|
|
|
|
self.ip = ip
|
|
|
|
self.realname = realname
|
2015-06-21 05:36:35 +02:00
|
|
|
self.modes = modes
|
2015-06-07 07:17:45 +02:00
|
|
|
|
|
|
|
self.identified = False
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.__dict__)
|
|
|
|
|
|
|
|
class IrcServer():
|
2015-06-22 00:00:33 +02:00
|
|
|
"""PyLink IRC Server class.
|
|
|
|
|
|
|
|
uplink: The SID of this IrcServer instance's uplink. This is set to None
|
|
|
|
for the main PyLink PseudoServer!
|
|
|
|
name: The name of the server.
|
|
|
|
internal: Whether the server is an internal PyLink PseudoServer.
|
|
|
|
"""
|
|
|
|
def __init__(self, uplink, name, internal=False):
|
2015-06-07 07:17:45 +02:00
|
|
|
self.uplink = uplink
|
|
|
|
self.users = []
|
2015-06-22 00:00:33 +02:00
|
|
|
self.internal = internal
|
|
|
|
self.name = name.lower()
|
2015-06-07 07:17:45 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.__dict__)
|
2015-06-07 08:04:11 +02:00
|
|
|
|
|
|
|
class IrcChannel():
|
|
|
|
def __init__(self):
|
2015-06-07 18:43:13 +02:00
|
|
|
self.users = set()
|
2015-07-05 09:20:45 +02:00
|
|
|
self.modes = set()
|
2015-07-07 00:33:23 +02:00
|
|
|
self.topic = ''
|
2015-07-07 04:00:20 +02:00
|
|
|
self.ts = int(time.time())
|
2015-07-05 21:48:39 +02:00
|
|
|
self.prefixmodes = {'ops': set(), 'halfops': set(), 'voices': set(),
|
|
|
|
'owners': set(), 'admins': set()}
|
|
|
|
|
2015-06-07 08:04:11 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.__dict__)
|
2015-07-04 03:07:01 +02:00
|
|
|
|
2015-07-05 21:48:39 +02:00
|
|
|
def removeuser(self, target):
|
|
|
|
for s in self.prefixmodes.values():
|
|
|
|
s.discard(target)
|
|
|
|
self.users.discard(target)
|
|
|
|
|
2015-07-04 03:07:01 +02:00
|
|
|
class ProtocolError(Exception):
|
|
|
|
pass
|
2015-07-08 03:07:20 +02:00
|
|
|
|
|
|
|
global testconf
|
|
|
|
testconf = {'server':
|
|
|
|
{
|
|
|
|
'netname': 'fakeirc',
|
|
|
|
'ip': '0.0.0.0',
|
|
|
|
'port': 7000,
|
|
|
|
'recvpass': "abcd",
|
|
|
|
'sendpass': "abcd",
|
|
|
|
'protocol': "null",
|
|
|
|
'hostname': "pylink.unittest",
|
|
|
|
'sid': "9PY",
|
|
|
|
'channels': ["#pylink"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FakeIRC(main.Irc):
|
|
|
|
def connect(self):
|
|
|
|
self.messages = []
|
|
|
|
self.hookargs = []
|
|
|
|
self.hookmsgs = []
|
|
|
|
self.socket = None
|
|
|
|
|
|
|
|
def run(self, data):
|
|
|
|
"""Queues a message to the fake IRC server."""
|
|
|
|
log.debug('<- ' + data)
|
|
|
|
self.proto.handle_events(self, data)
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
self.messages.append(data)
|
|
|
|
log.debug('-> ' + data)
|
|
|
|
|
|
|
|
def takeMsgs(self):
|
|
|
|
"""Returns a list of messages sent by the protocol module since
|
|
|
|
the last takeMsgs() call, so we can track what has been sent."""
|
|
|
|
msgs = self.messages
|
|
|
|
self.messages = []
|
|
|
|
return msgs
|
|
|
|
|
|
|
|
def takeCommands(self, msgs):
|
|
|
|
"""Returns a list of commands parsed from the output of takeMsgs()."""
|
|
|
|
sidprefix = ':' + self.sid
|
|
|
|
commands = []
|
|
|
|
for m in msgs:
|
|
|
|
args = m.split()
|
|
|
|
if m.startswith(sidprefix):
|
|
|
|
commands.append(args[1])
|
|
|
|
else:
|
|
|
|
commands.append(args[0])
|
|
|
|
return commands
|
|
|
|
|
|
|
|
def takeHooks(self):
|
|
|
|
"""Returns a list of hook arguments sent by the protocol module since
|
|
|
|
the last takeHooks() call."""
|
|
|
|
hookmsgs = self.hookmsgs
|
|
|
|
self.hookmsgs = []
|
|
|
|
return hookmsgs
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def dummyhook(irc, source, command, parsed_args):
|
|
|
|
"""Dummy function to bind to hooks."""
|
|
|
|
irc.hookmsgs.append(parsed_args)
|
|
|
|
|
|
|
|
class FakeProto():
|
|
|
|
"""Dummy protocol module for testing purposes."""
|
|
|
|
@staticmethod
|
|
|
|
def handle_events(irc, data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def connect(irc):
|
|
|
|
pass
|