def join(self, sock, source, name): user = self.users[sock] if name not in self.channels: channel = self.channels[name] = Channel(name) else: channel = self.channels[name] if user in channel.users: return user.channels.append(name) channel.users.append(user) self._notify(channel.users, Message("JOIN", name, prefix=user.prefix)) if channel.topic: self.fire(reply(sock, RPL_TOPIC(channel.topic))) else: self.fire(reply(sock, RPL_NOTOPIC(channel.name))) self.fire( reply( sock, RPL_NAMEREPLY(channel.name, [x.prefix for x in channel.users]))) self.fire(reply(sock, RPL_ENDOFNAMES(channel.name)))
def kick(self, sock, source, name, nick, reason=None): user = models.User.objects.filter(sock=sock).first() channel = models.Channel.objects.filter(name=name).first() if channel is None: return ERR_NOSUCHCHANNEL(name) if not user.oper and user not in channel.operators: return ERR_CHANOPRIVSNEEDED(channel.name) if nick not in imap(attrgetter("nick"), channel.users): return ERR_USERNOTINCHANNEL(nick, channel.name) nick = models.User.objects.filter(nick=nick).first() self.notify( channel.users[:], Message(u"KICK", channel.name, nick.nick, reason or nick.nick, prefix=user.prefix)) nick.channels.remove(channel) nick.save() channel.users.remove(nick) if user in channel.operators: channel.operators.remove(user) if user in channel.voiced: channel.voiced.remove(user) channel.save() if not channel.users: channel.delete()
def part(self, sock, source, name, reason=u"Leaving"): user = models.User.objects.filter(sock=sock).first() channel = models.Channel.objects.filter(name=name).first() if channel is None: return if user not in channel.users: return self.notify(channel.users[:], Message(u"PART", name, reason, prefix=user.prefix)) user.channels.remove(channel) user.save() channel.users.remove(user) if user in channel.operators: channel.operators.remove(user) if user in channel.voiced: channel.voiced.remove(user) channel.save() if not channel.users: channel.delete()
def nick(self, sock, source, nick): user = User.objects.filter(sock=sock).first() if not VALID_NICK_REGEX.match(nick): return ERR_ERRONEUSNICKNAME(nick) if len(nick) > self.parent.nicklen: return ERR_ERRONEUSNICKNAME(nick) if any(x for x in User.objects.all() if x.nick and x.nick.lower() == nick.lower()): return ERR_NICKNAMEINUSE(nick) prefix = user.prefix or joinprefix(*source) user.nick = nick user.save() if user.userinfo and user.userinfo.user is not None: user.registered = True user.save() return signon(sock, user.source) users = chain(*map(attrgetter("users"), user.channels)) self.notify(users, Message(u("NICK"), nick, prefix=prefix))
def privmsg(self, sock, source, target, message): user = self.users[sock] if target.startswith("#"): if target not in self.channels: return self.fire(reply(sock, ERR_NOSUCHCHANNEL(target))) channel = self.channels[target] self._notify( channel.users, Message("PRIVMSG", target, message, prefix=user.prefix), user) else: if target not in self.nicks: return self.fire(reply(sock, ERR_NOSUCHNICK(target))) self.fire( reply(self.nicks[target].sock, Message("PRIVMSG", target, message, prefix=user.prefix)))
def signal(self, signo, stack): if signo == SIGHUP: self.config.reload_config() elif signo in (SIGINT, SIGTERM): Timer(5, terminate()).register(self) self.fire( broadcast( User.objects, Message("NOTICE", "Received SIGTERM, terminating...") ), self.server ) return True
def connect(self, sock, *args): host, port = args[:2] self.pending[sock] = True self.fire( reply(sock, Message(u"NOTICE", u"*", u"*** Looking up your hostname..."))) e = task(check_host, sock) e.complete = True e.complete_channels = ("server", ) self.fire(e, "threadpool")
def part(self, sock, source, name, reason="Leaving"): user = self.users[sock] channel = self.channels[name] self._notify(channel.users, Message("PART", name, reason, prefix=user.prefix)) user.channels.remove(name) channel.users.remove(user) if not channel.users: del self.channels[name]
def quit(self, sock, source, reason="Leaving"): user = self.users[sock] channels = [self.channels[channel] for channel in user.channels] for channel in channels: channel.users.remove(user) if not channel.users: del self.channels[channel.name] users = chain(*map(attrgetter("users"), channels)) self.fire(close(sock)) self._notify(users, Message("QUIT", reason, prefix=user.prefix), user)
def quit(self, sock, source, reason=u"Leaving", **kwargs): user = User.objects.filter(sock=sock).first() for channel in user.channels: channel.users.remove(user) if not channel.users: channel.delete() users = chain(*map(attrgetter("users"), user.channels)) if kwargs.get("disconnect", True): self.disconnect(user) self.notify(users, Message(u"QUIT", reason, prefix=user.prefix), user)
def task_complete(self, e, value): _, sock = e.args del self.pending[sock] self.fire( reply(sock, Message(u"NOTICE", u"*", u"*** Found your hostname"))) user = User.objects.filter(sock=sock).first() if user.userinfo is None: userinfo = UserInfo() userinfo.save() user.userinfo = userinfo user.save() user.userinfo.host = value user.userinfo.save() if user.registered: return signon(sock, user.source)
def cmdQUOTE(self, message): self.fire(request(Message(message)))
def ping(self, sock, source, server): return Message(u"PONG", server)
def ping(self, event, sock, source, server): event.stop() self.fire(reply(sock, Message("PONG", server)))
def ping(self, sock, source, *args): return Message(u("PONG"), u(" ").join(args))
def hello(self, sock, source): return Message(u"NOTICE", u"*", u"Hello!")
def hello(self, sock, source): return Message(u("NOTICE"), u("*"), u("Hello!"))