def __init__(self, address, socket, factory=None): _BaseConnection.__init__(self, address=address, socket=socket, factory=factory) self.me.host = self.socket_address[0] self.server.nick = ClientConnection.DEFAULT_SERVERNAME evt = Event() evt.bind(ClientConnection.authentication_event, filter=match_source(self)) self.authentication_event = evt self._password = None
def __init__(self, address, socket=None, factory=None): """ Base class for IRC connections. address: A tuple containing the remote host/IP and port of the connection socket: An existing socket for the connection, or None if a new connection is to be established. factory: The factory that was used to create this object. """ self.socket_address = address self.socket = socket self.factory = factory evt = Event() evt.bind(self.__class__.connection_closed_event, filter=match_source(self)) self.connection_closed_event = evt evt = Event() evt.bind(self.__class__.registration_event, filter=match_source(self)) self.registration_event = evt evt = Event() evt.bind(self.__class__.command_received_event, filter=match_source(self)) self.command_received_event = evt self.me = Nick(self) self.server = Nick(self) self.registered = False self.away = False self.realname = None self.usermodes = '' self.nicks = WeakValueDictionary() self.channels = {} self.isupport = { 'CHANMODES': 'bIe,k,l', 'CHANTYPES': '#&+', 'PREFIX': '(ov)@+', 'NAMESX': '' } self.motd = [] self.owner = None self._registration_timeout = None
class Proxy(Service): package = 'info.shroudbnc.services.proxy' version = 'wip-1' def __init__(self): self.irc_factory = ConnectionFactory(IRCConnection) self.client_factory = ConnectionFactory(irc.ClientConnection) def start(self, config_root_node): self._config_root = config_root_node self._config = self.get_plugin_config(Proxy) ClientConnection.authentication_event.add_listener(self._client_authentication_handler, Event.Handler, ConnectionFactory.match_factory(self.client_factory)) ClientConnection.registration_event.add_listener(ProxyUser._client_registration_handler, Event.PreObserver, ConnectionFactory.match_factory(self.client_factory)) ClientConnection.registration_event.add_listener(ProxyUser._client_post_registration_handler, Event.PostObserver, ConnectionFactory.match_factory(self.client_factory)) ClientConnection.command_received_event.add_listener(ProxyUser._client_command_handler, Event.Handler, ConnectionFactory.match_factory(self.client_factory), last=True) ClientConnection.connection_closed_event.add_listener(ProxyUser._client_closed_handler, Event.PostObserver, ConnectionFactory.match_factory(self.client_factory)) IRCConnection.command_received_event.add_listener(ProxyUser._irc_command_handler, Event.Handler, ConnectionFactory.match_factory(self.irc_factory), last=True) IRCConnection.connection_closed_event.add_listener(ProxyUser._irc_closed_handler, Event.PreObserver, ConnectionFactory.match_factory(self.irc_factory)) IRCConnection.registration_event.add_listener(ProxyUser._irc_registration_handler, Event.PreObserver, ConnectionFactory.match_factory(self.irc_factory)) self.users = {} for user_config in self._config['users']: self.users[user_config.name] = ProxyUser(self, user_config) self._last_reconnect = None Timer(10, self._reconnect_timer).start() # high-level helper events, to make things easier for plugins self.new_client_event = Event() self.client_registration_event = Event() self.client_registration_event.bind(irc.ClientConnection.registration_event, filter=ConnectionFactory.match_factory(self.client_factory)) self.irc_registration_event = Event() self.irc_registration_event.bind(IRCConnection.registration_event, filter=ConnectionFactory.match_factory(self.irc_factory)) self.client_command_received_event = Event() self.client_command_received_event.bind(irc.ClientConnection.command_received_event, filter=ConnectionFactory.match_factory(self.client_factory)) self.irc_command_received_event = Event() self.irc_command_received_event.bind(IRCConnection.command_received_event, filter=ConnectionFactory.match_factory(self.irc_factory)) self.client_connection_closed_event = Event() self.client_connection_closed_event.bind(ClientConnection.connection_closed_event, filter=ConnectionFactory.match_factory(self.irc_factory)) self.irc_connection_closed_event = Event() self.irc_connection_closed_event.bind(IRCConnection.connection_closed_event, filter=ConnectionFactory.match_factory(self.irc_factory)) def _client_authentication_handler(self, evt, clientobj, username, password): if not username in self.users: return Event.Continue userobj = self.users[clientobj.me.user] if not userobj.check_password(password): return Event.Continue clientobj.owner = userobj return Event.Handled def create_user(self, name): if name in self.users: raise ValueError('User already exists.') user_config = self._config['users'][name] user = ProxyUser(self, user_config) self.users[name] = user # TODO: event return user def remove_user(self, name): # TODO: event del self.users[name] del self.config['users'][name] def _reconnect_timer(self): if self._last_reconnect != None and \ self._last_reconnect > time() - 60: return True for userobj in self.users.values(): if userobj.irc_connection != None or (userobj.last_reconnect != None and \ userobj.last_reconnect > time() - 120): continue self._last_reconnect = time() userobj.reconnect_to_irc() break return True def get_plugin_config(self, plugin_cls): return self._config_root[plugin_cls.package]
def __init__(self, cls): evt = Event() evt.bind(ConnectionFactory.new_connection_event, filter=match_source(self)) self.new_connection_event = evt self._cls = cls