+
+ def bind_tcp(self, host, port):
+ return self._hooksocket(socket.AF_INET, socket.SOCK_STREAM, (host, port))
+ def bind_udp(self, host, port):
+ return self._hooksocket(socket.AF_INET, socket.SOCK_DGRAM, (host, port))
+ def bind_unix(self, path):
+ return self._hooksocket(socket.AF_UNIX, socket.SOCK_STREAM, path)
+ def _hooksocket(self, af, ty, address):
+ def realhook(cls):
+ if not (hasattr(cls, 'getdata') and callable(cls.getdata)):
+ # Check early that the object implements getdata.
+ # If getdata ever returns a non-empty list, then a parse method must also exist, but we don't check that.
+ raise Exception('Attempted to hook a socket without a class to process data')
+ self.sockhooks.append((af, ty, address, cls))
+ if self.parent is not None:
+ self._create_socket(af, ty, address, cls)
+ return cls
+ return realhook
+ def _create_socket(self, af, ty, address, cls):
+ ty = ty | socket.SOCK_NONBLOCK
+ sock = socket.socket(af, ty)
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ sock.bind(address)
+ obj = _ListenSocket(self, sock, cls)
+ self.sockets.append((sock,obj))
+ sock.listen(5)
+ self.parent.newfd(obj, sock.fileno())
+ self.parent.log(repr(obj), '?', 'Socket ready to accept new connections')
+ def _destroy_socket(self, sock, obj):
+ obj.close()
+
+ def mod(self, modname):
+ if self.parent is not None:
+ return self.parent.module(modname)
+ else:
+ return error('unknown parent')
+
+ def argsEQ(self, num):
+ def realhook(func):
+ @wraps(func)
+ def checkargs(bot, user, chan, realtarget, *args):
+ if len(args) == num:
+ return func(bot, user, chan, realtarget, *args)
+ else:
+ bot.msg(user, self.WRONGARGS)
+ return checkargs
+ return realhook
+
+ def argsGE(self, num):
+ def realhook(func):
+ @wraps(func)
+ def checkargs(bot, user, chan, realtarget, *args):
+ if len(args) >= num:
+ return func(bot, user, chan, realtarget, *args)
+ else:
+ bot.msg(user, self.WRONGARGS)
+ return checkargs
+ return realhook
+
+ def help(self, *args, **kwargs):
+ """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
+ Example:
+ help("<user> <pass>", "login")
+ ^ Help will only be one line. Command name determined based on function name.
+ help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
+ ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
+ help(None, "do stuff", "This command is really complicated.")
+ ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
+ Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
+ """
+ def realhook(func):
+ if self.parent is not None:
+ try:
+ self.mod('help').reghelp(func, *args, **kwargs)
+ except:
+ pass
+ self.helps.append((func, args, kwargs))
+ return func
+ return realhook
+
+class _ListenSocket(object):
+ def __init__(self, lib, sock, cls):
+ self.clients = []
+ self.lib = lib
+ self.sock = sock
+ self.cls = cls
+
+ def _make_closer(self, obj, client):
+ def close():
+ print(repr(self), repr(obj))
+ self.lib.parent.log(repr(self), '?', 'Closing child socket %d' % (client.fileno()))
+ try:
+ obj.closing()
+ except AttributeError:
+ pass
+ self.lib.parent.delfd(client.fileno())
+ client.shutdown(socket.SHUT_RDWR)
+ client.close()
+ self.clients.remove((client,obj))
+ return close
+
+ def getdata(self):
+ client, addr = self.sock.accept()
+ obj = self.cls(client)
+ obj.close = self._make_closer(obj, client)
+ self.lib.parent.log(repr(self), '?', 'New connection %d from %s' % (client.fileno(), addr))
+ self.clients.append((client,obj))
+ self.lib.parent.newfd(obj, client.fileno())
+ return []
+
+ def close(self):
+ self.lib.parent.log(repr(self), '?', 'Socket closing')
+ if self.sock.fileno() != -1:
+ self.lib.parent.delfd(self.sock.fileno())
+ self.sock.shutdown(socket.SHUT_RDWR)
+ self.sock.close()
+ for client, obj in self.clients:
+ obj.close()
+
+ def __repr__(self): return '<_ListenSocket #%d>' % (self.sock.fileno())