# Path: txircd/modbase.py
# class Command(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def onUse(self, user, data):
#         pass
#     def processParams(self, user, params):
#         return {
#             "user": user,
#             "params": params
#         }
#     def updateActivity(self, user):
#         user.lastactivity = now()
# 
# Path: txircd/server.py
# class RemoteUser(object):
#     class RemoteSocket(object):
#         class RemoteTransport(object):
# class RemoteServer(object):
# class HandshakeAlreadyComplete(Exception):
# class HandshakeNotYetComplete(Exception):
# class ServerAlreadyConnected(Exception):
# class ServerMismatchedIP(Exception):
# class ServerPasswordIncorrect(Exception):
# class ServerNoLink(Exception):
# class ModuleMismatch(Exception):
# class ServerNotConnected(Exception):
# class UserAlreadyConnected(Exception):
# class NoSuchTarget(Exception):
# class NoSuchUser(Exception):
# class NoSuchServer(Exception):
# class NoSuchChannel(Exception):
# class IntroduceServer(Command):
# class AddNewServer(Command):
# class DisconnectServer(Command):
# class ConnectUser(Command):
# class RegisterUser(Command):
# class RemoveUser(Command):
# class SetIdent(Command):
# class SetHost(Command):
# class SetName(Command):
# class RequestJoinChannel(Command):
# class JoinChannel(Command):
# class LeaveChannel(Command):
# class RequestSetMode(Command):
# class SetMode(Command):
# class SetTopic(Command):
# class RequestNick(Command):
# class ChangeNick(Command):
# class SetMetadata(Command):
# class SendAnnouncement(Command):
# class ChannelMessage(Command):
# class ModuleMessage(Command):
# class PingServer(Command):
# class ServerProtocol(AMP):
# class ServerFactory(Factory):
#             def loseConnection(self):
#         def __init__(self, secure):
#     def __init__(self, ircd, uuid, nick, ident, host, realhost, gecos, ip, password, server, secure, signonTime, nickTime):
#     def callConnectHooks(self):
#     def disconnect(self, reason, sourceServer = None):
#     def sendMessage(self, command, *parameter_list, **kw):
#     def handleCommand(self, command, prefix, params):
#     def commandPermission(self, command, data):
#     def setMetadata(self, namespace, key, value, sourceServer = None):
#     def delMetadata(self, namespace, key, sourceServer = None):
#     def prefix(self):
#     def hasAccess(self, channel, level):
#     def setUsername(self, newUsername, sourceServer = None):
#     def setHostname(self, newHostname, sourceServer = None):
#     def setRealname(self, newRealname, sourceServer = None):
#     def setMode(self, user, modes, params, displayPrefix = None):
#     def modeString(self, user):
#     def join(self, channel):
#     def leave(self, channel, sourceServer = None):
#     def nick(self, newNick):
#     def __init__(self, ircd, name, desc, nearestServer, hopCount):
#     def callRemote(self, command, *args, **kw):
#     def __init__(self, ircd):
#     def connectionMade(self):
#     def serverHandshake(self, name, password, description, version, commonmodules):
#     def ping(self):
#     def handlePong(self, data):
#     def handlePing(self, data):
#     def sendBurstData(self):
#     def newServer(self, name, description, hopcount, nearhop):
#     def splitServer(self, name):
#     def connectionLost(self, reason):
#     def basicConnectUser(self, uuid, ip, server, secure, signon):
#     def addUser(self, uuid, nick, ident, host, realhost, gecos, ip, password, server, secure, signon, nickts):
#     def removeUser(self, user, reason):
#     def setIdent(self, user, ident):
#     def setHost(self, user, host):
#     def setName(self, user, gecos):
#     def requestJoin(self, channel, user):
#     def joinChannel(self, channel, user, chants):
#     def leaveChannel(self, channel, user):
#     def requestMode(self, user, source, modestring, params):
#     def setMode(self, target, targetts, source, modestring, params):
#     def setTopic(self, channel, chants, topic, topicsetter, topicts):
#     def requestNick(self, user, newnick):
#     def changeNick(self, user, newnick):
#     def setMetadata(self, target, targetts, namespace, key, value):
#     def announce(self, user, type, args, prefix, to):
#     def chanMessage(self, channel, type, args, prefix, to, skip):
#     def modMessage(self, destserver, type, args):
#     def __init__(self, ircd):
#     def buildProtocol(self, addr):


from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.server import IntroduceServer, ModuleMessage, ServerProtocol, protocol_version

class ConnectCommand(Command):
    def onUse(self, user, data):
        if "sourceserver" not in data or "sourceserver" == self.ircd.name:
            try:
                self.ircd.connect_server(data["destserver"])
                user.sendMessage("NOTICE", ":*** Connecting to {}".format(data["destserver"]))
            except RuntimeError as ex:
                user.sendMessage("NOTICE", ":*** Connection to {} failed: {}".format(data["destserver"], ex))
        else:
            server = self.ircd.servers[data["sourceserver"]]
            server.callRemote(ModuleMessage, destserver=server.name, type="ServerConnect", args=[user.uuid, data["destserver"]])
    
    def processParams(self, user, params):
        if user.registered > 0:
            user.sendMessage(irc.ERR_NOTREGISTERED, "CONNECT", ":You have not registered")
            return {}
        if "o" not in user.mode:
            user.sendMessage(irc.ERR_NOPRIVILEGES, ":Permission denied - You do not have the correct operator privileges")
            return {}
        if not params:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "CONNECT", ":Not enough parameters")
            return {}
        if len(params) >= 2:

# Path: txircd/modbase.py
# class Command(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def onUse(self, user, data):
#         pass
#     def processParams(self, user, params):
#         return {
#             "user": user,
#             "params": params
#         }
#     def updateActivity(self, user):
#         user.lastactivity = now()
# 
# Path: txircd/server.py
# class RemoteUser(object):
#     class RemoteSocket(object):
#         class RemoteTransport(object):
# class RemoteServer(object):
# class HandshakeAlreadyComplete(Exception):
# class HandshakeNotYetComplete(Exception):
# class ServerAlreadyConnected(Exception):
# class ServerMismatchedIP(Exception):
# class ServerPasswordIncorrect(Exception):
# class ServerNoLink(Exception):
# class ModuleMismatch(Exception):
# class ServerNotConnected(Exception):
# class UserAlreadyConnected(Exception):
# class NoSuchTarget(Exception):
# class NoSuchUser(Exception):
# class NoSuchServer(Exception):
# class NoSuchChannel(Exception):
# class IntroduceServer(Command):
# class AddNewServer(Command):
# class DisconnectServer(Command):
# class ConnectUser(Command):
# class RegisterUser(Command):
# class RemoveUser(Command):
# class SetIdent(Command):
# class SetHost(Command):
# class SetName(Command):
# class RequestJoinChannel(Command):
# class JoinChannel(Command):
# class LeaveChannel(Command):
# class RequestSetMode(Command):
# class SetMode(Command):
# class SetTopic(Command):
# class RequestNick(Command):
# class ChangeNick(Command):
# class SetMetadata(Command):
# class SendAnnouncement(Command):
# class ChannelMessage(Command):
# class ModuleMessage(Command):
# class PingServer(Command):
# class ServerProtocol(AMP):
# class ServerFactory(Factory):
#             def loseConnection(self):
#         def __init__(self, secure):
#     def __init__(self, ircd, uuid, nick, ident, host, realhost, gecos, ip, password, server, secure, signonTime, nickTime):
#     def callConnectHooks(self):
#     def disconnect(self, reason, sourceServer = None):
#     def sendMessage(self, command, *parameter_list, **kw):
#     def handleCommand(self, command, prefix, params):
#     def commandPermission(self, command, data):
#     def setMetadata(self, namespace, key, value, sourceServer = None):
#     def delMetadata(self, namespace, key, sourceServer = None):
#     def prefix(self):
#     def hasAccess(self, channel, level):
#     def setUsername(self, newUsername, sourceServer = None):
#     def setHostname(self, newHostname, sourceServer = None):
#     def setRealname(self, newRealname, sourceServer = None):
#     def setMode(self, user, modes, params, displayPrefix = None):
#     def modeString(self, user):
#     def join(self, channel):
#     def leave(self, channel, sourceServer = None):
#     def nick(self, newNick):
#     def __init__(self, ircd, name, desc, nearestServer, hopCount):
#     def callRemote(self, command, *args, **kw):
#     def __init__(self, ircd):
#     def connectionMade(self):
#     def serverHandshake(self, name, password, description, version, commonmodules):
#     def ping(self):
#     def handlePong(self, data):
#     def handlePing(self, data):
#     def sendBurstData(self):
#     def newServer(self, name, description, hopcount, nearhop):
#     def splitServer(self, name):
#     def connectionLost(self, reason):
#     def basicConnectUser(self, uuid, ip, server, secure, signon):
#     def addUser(self, uuid, nick, ident, host, realhost, gecos, ip, password, server, secure, signon, nickts):
#     def removeUser(self, user, reason):
#     def setIdent(self, user, ident):
#     def setHost(self, user, host):
#     def setName(self, user, gecos):
#     def requestJoin(self, channel, user):
#     def joinChannel(self, channel, user, chants):
#     def leaveChannel(self, channel, user):
#     def requestMode(self, user, source, modestring, params):
#     def setMode(self, target, targetts, source, modestring, params):
#     def setTopic(self, channel, chants, topic, topicsetter, topicts):
#     def requestNick(self, user, newnick):
#     def changeNick(self, user, newnick):
#     def setMetadata(self, target, targetts, namespace, key, value):
#     def announce(self, user, type, args, prefix, to):
#     def chanMessage(self, channel, type, args, prefix, to, skip):
#     def modMessage(self, destserver, type, args):
#     def __init__(self, ircd):
#     def buildProtocol(self, addr):


# Path: txircd/modules/cmd_connect.py
from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.server import IntroduceServer, ModuleMessage, ServerProtocol, protocol_version


class ConnectCommand(Command):
    def onUse(self, user, data):
        if "sourceserver" not in data or "sourceserver" == self.ircd.name:
            try:
                self.ircd.connect_server(data["destserver"])
                user.sendMessage("NOTICE", ":*** Connecting to {}".format(data["destserver"]))
            except RuntimeError as ex:
                user.sendMessage("NOTICE", ":*** Connection to {} failed: {}".format(data["destserver"], ex))
        else:
            server = self.ircd.servers[data["sourceserver"]]
            server.callRemote(ModuleMessage, destserver=server.name, type="ServerConnect", args=[user.uuid, data["destserver"]])
    
    def processParams(self, user, params):
        if user.registered > 0:
            user.sendMessage(irc.ERR_NOTREGISTERED, "CONNECT", ":You have not registered")
            return {}
        if "o" not in user.mode:
            user.sendMessage(irc.ERR_NOPRIVILEGES, ":Permission denied - You do not have the correct operator privileges")
            return {}
        if not params:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "CONNECT", ":Not enough parameters")
            return {}
        if len(params) >= 2:

            if params[0] not in self.ircd.servers: