def handleChatInvite(server, user, packet): ref = packet.getNumber(DATA_CHATID, 0) uid = packet.getNumber(DATA_UID, 0) chat = server.getChat(ref) who = server.getUser(uid) if not who: raise HLException("Invalid user.") if not chat: raise HLException("Invalid chat.") if uid == user.uid: # Ignore self invitations. return if chat.hasInvite(who): # Ignore all invitations after the first. return if not chat.hasUser(user): raise HLException("You are not in this chat.") if chat.hasUser(who): # The specified user is already in the chat. return chat.addInvite(who) # Send the invitation to the specified user. invite = HLPacket(HTLS_HDR_CHAT_INVITE) invite.addInt32(DATA_CHATID, chat.id) invite.addNumber(DATA_UID, user.uid) invite.addString(DATA_NICK, user.nick) server.sendPacket(invite, who)
def handleUserDisconnected(server, user): if user.valid: user.valid = False leave = HLPacket(HTLS_HDR_USER_LEAVE) leave.addNumber(DATA_UID, user.uid) server.sendPacket(leave) dispatcher.send(signal=user_leave, sender=server, server=server, user=user)
def handleChatCreate(server, user, packet): uid = packet.getNumber(DATA_UID, 0) who = server.getUser(uid) # First, create the new chat, adding the user. chat = server.createChat() chat.addUser(user) # Send the completed task with user info. reply = packet.response() reply.addInt32(DATA_CHATID, chat.id) reply.addNumber(DATA_UID, user.uid) reply.addString(DATA_NICK, user.nick) reply.addNumber(DATA_ICON, user.icon) reply.addNumber(DATA_STATUS, user.status) if user.color >= 0: reply.addInt32(DATA_COLOR, user.color) server.sendPacket(reply, user) if who and (who.uid != user.uid): # Add the specified user to the invite list. chat.addInvite(who) # Invite the specified user to the newly created chat. invite = HLPacket(HTLS_HDR_CHAT_INVITE) invite.addInt32(DATA_CHATID, chat.id) invite.addNumber(DATA_UID, user.uid) invite.addString(DATA_NICK, user.nick) server.sendPacket(invite, who)
def sendLogin(self, login, passwd): p = HLPacket(HTLC_HDR_LOGIN, self.nextTaskID()) p.addBinary(DATA_LOGIN, HLEncode(login)) p.addBinary(DATA_PASSWORD, HLEncode(passwd)) p.addString(DATA_NICK, self.nickname) self.sendPacket(p, login_received) return self.taskDeferred()
def handleChatJoin(server, user, packet): ref = packet.getNumber(DATA_CHATID, 0) chat = server.getChat(ref) if not chat: raise HLException("Invalid chat.") if not chat.hasInvite(user): raise HLException("You were not invited to this chat.") # Send a join packet to everyone in the chat. join = HLPacket(HTLS_HDR_CHAT_USER_CHANGE) join.addInt32(DATA_CHATID, chat.id) join.addNumber(DATA_UID, user.uid) join.addString(DATA_NICK, user.nick) join.addNumber(DATA_ICON, user.icon) join.addNumber(DATA_STATUS, user.status) if user.color >= 0: join.addInt32(DATA_COLOR, user.color) for u in chat.users: server.sendPacket(join, u) # Add the joiner to the chat. chat.addUser(user) chat.removeInvite(user) # Send the userlist back to the joiner. list = packet.response() for u in chat.users: list.addBinary(DATA_USER, u.flatten()) list.addString(DATA_SUBJECT, chat.subject) server.sendPacket(list, user)
def handle(server, user, arg, ref): if len(arg) > 0: rootDir = user.account.fileRoot if not rootDir: rootDir = conf.FILE_ROOT pre = len(rootDir) matches = [] for (root, dirs, files) in os.walk(rootDir): root = root.decode("utf-8", "replace") for name in dirs: name = name.decode("utf-8", "replace") if arg.upper() in name.upper(): matches.append("+ " + os.path.join(root, name)[pre:]) for name in files: name = name.decode("utf-8", "replace") if arg.upper() in name.upper(): matches.append("- " + os.path.join(root, name)[pre:]) found = "(none)" if len(matches) > 0: found = "\r > ".join(matches) matchStr = "\r > --- search results for '%s' ------------\r > %s" % ( arg, found) chat = HLPacket(HTLS_HDR_CHAT) chat.addString(DATA_STRING, matchStr) if ref > 0: chat.addInt32(DATA_CHATID, ref) server.sendPacket(chat, user)
def handleIconSet(server, user, packet): iconData = packet.getBinary(DATA_GIFICON, b"") if certifyIcon(iconData): user.gif = iconData server.sendPacket(packet.response(), user) change = HLPacket(HTLS_HDR_ICON_CHANGE) change.addNumber(DATA_UID, user.uid) server.sendPacket(change)
def sendUserChange(self, user): change = HLPacket(HTLS_HDR_USER_CHANGE) change.addNumber(DATA_UID, user.uid) change.addString(DATA_NICK, user.nick) change.addNumber(DATA_ICON, user.icon) change.addNumber(DATA_STATUS, user.status) if user.color >= 0: change.addInt32(DATA_COLOR, user.color) self.sendPacket(change, lambda c: c.context.valid)
def handleChatDecline(server, user, packet): ref = packet.getNumber(DATA_CHATID, 0) chat = server.getChat(ref) if chat and chat.hasInvite(user): chat.removeInvite(user) s = "\r< %s has declined the invitation to chat >" % user.nick decline = HLPacket(HTLS_HDR_CHAT) decline.addInt32(DATA_CHATID, chat.id) decline.addString(DATA_STRING, s) for u in chat.users: server.sendPacket(decline, u)
def handle(server, user, args, ref): ids = str(args).strip().split() for id in ids: try: kick = HLPacket(HTLC_HDR_KICK) kick.addNumber(DATA_UID, int(id)) dispatcher.send(signal=(packet_received, kick.type), sender=server, server=server, user=user, packet=kick) except ValueError: pass except HLException: pass
def handleNewsPost(server, user, packet): s = packet.getString(DATA_STRING, "") par = packet.getNumber(DATA_POSTID) if len(s) > 0: post = HLNewsPost(user.nick, user.account.login, s) post.parent_id = par server.database.saveNewsPost(post) notify = HLPacket(HTLS_HDR_NEWS_POST) notify.addString(DATA_STRING, formatPost(post)) server.sendPacket(notify, lambda c: c.context.hasPriv(PRIV_READ_NEWS)) server.sendPacket(packet.response(), user)
def handle(server, user, args, ref): chat = HLPacket(HTLC_HDR_CHAT) chat.addString(DATA_STRING, args) chat.addNumber(DATA_OPTION, 1) if ref > 0: chat.addInt32(DATA_CHATID, ref) dispatcher.send(signal=(packet_received, chat.type), sender=server, server=server, user=user, packet=chat)
def handleChatSubject(server, user, packet): ref = packet.getNumber(DATA_CHATID, 0) sub = packet.getString(DATA_SUBJECT, "") chat = server.getChat(ref) if not chat: return subject = HLPacket(HTLS_HDR_CHAT_SUBJECT) subject.addInt32(DATA_CHATID, ref) subject.addString(DATA_SUBJECT, sub) for u in chat.users: server.sendPacket(subject, u)
def handleMessage(server, user, packet): uid = packet.getNumber(DATA_UID, 0) s = packet.getString(DATA_STRING, "")[:conf.MAX_MSG_LEN] if not server.getUser(uid): raise HLException("Invalid user.") msg = HLPacket(HTLS_HDR_MSG) msg.addNumber(DATA_UID, user.uid) msg.addString(DATA_NICK, user.nick) msg.addString(DATA_STRING, s) server.sendPacket(msg, uid) server.sendPacket(packet.response(), user)
def handle(server, user, arg, ref): if len(arg) > 0 and user.hasPriv(PRIV_MODIFY_USERS): bits = arg.split() cmd = bits[0] mod = "" if len(bits) > 1: mod = bits[1] if cmd == "list": chat = HLPacket(HTLS_HDR_CHAT) chat.addString(DATA_STRING, ", ".join(handlers.__all__)) server.sendPacket(chat, user) elif cmd == "reload": # call next time through the event loop to avoid problems reactor.callLater(0, handlers.reload, "phxd.server.handlers", mod)
def handle(server, user, args, ref): secs = int(time.time() - server.startTime) days = secs / 86400 secs -= (days * 86400) hours = secs / 3600 secs -= (hours * 3600) mins = secs / 60 secs -= (mins * 60) str = "\r > Uptime: %d days, %d hours, %d minutes, and %d seconds." % ( days, hours, mins, secs) chat = HLPacket(HTLS_HDR_CHAT) chat.addString(DATA_STRING, str) if ref > 0: chat.addInt32(DATA_CHATID, ref) server.sendPacket(chat, user)
def handle(server, user, args, ref): if user.hasPriv(PRIV_USER_INFO): str = "" if len(server.fileserver.transfers) == 0: str += "\r > No file transfers in progress." else: str += "\r > File transfers:" for xfer in server.fileserver.transfers: u = server.getUser(xfer.owner) owner = u.nick if u else "<none>" str += "\r > (%s) %s" % (owner, xfer) chat = HLPacket(HTLS_HDR_CHAT) chat.addString(DATA_STRING, str) if ref > 0: chat.addInt32(DATA_CHATID, ref) server.sendPacket(chat, user)
def handleChatLeave(server, user, packet): ref = packet.getNumber(DATA_CHATID, 0) chat = server.getChat(ref) if not chat or not chat.hasUser(user): return chat.removeUser(user) if len(chat.users) > 0: leave = HLPacket(HTLS_HDR_CHAT_USER_LEAVE) leave.addInt32(DATA_CHATID, chat.id) leave.addNumber(DATA_UID, user.uid) for u in chat.users: server.sendPacket(leave, u) else: server.removeChat(chat.id)
def handle(server, user, args, ref): parts = str(args).strip().split() if len(parts) > 1: if user.hasPriv(PRIV_MODIFY_USERS): user = server.getUser(int(parts[0])) arg = parts[1] else: return else: arg = parts[0] try: otherUid = int(arg) otherUser = server.getUser(otherUid) user.gif = otherUser.gif change = HLPacket(HTLS_HDR_ICON_CHANGE) change.addNumber(DATA_UID, user.uid) server.sendPacket(change) except ValueError: getPage(arg).addCallback(gotIcon, user, server)
def parseBuffer(self): """ Parses the current buffer until the buffer is empty or until no more packets can be parsed. """ if self.gotMagic: done = False while not done: size = self.packet.parse(self.buffered) if size > 0: self.buffered = self.buffered[size:] self.factory.notifyPacket(self, self.packet) self.packet = HLPacket() else: done = True else: if len(self.buffered) >= self.expectedMagicLen: magic = self.buffered[0:self.expectedMagicLen] self.buffered = self.buffered[self.expectedMagicLen:] self.gotMagic = True self.factory.notifyMagic(self, magic) if len(self.buffered) > 0: self.parseBuffer()
def handle(server, user, args, ref): parts = str(args).strip().split() try: if len(parts) > 1: if user.hasPriv(PRIV_MODIFY_USERS): user = server.getUser(int(parts[0])) sourceUser = server.getUser(int(parts[1])) else: return else: sourceUser = server.getUser(int(parts[0])) if user is None or sourceUser is None: return user.gif = sourceUser.gif user.color = sourceUser.color changeIcon = HLPacket(HTLS_HDR_ICON_CHANGE) changeIcon.addNumber(DATA_UID, user.uid) server.sendPacket(changeIcon) server.sendUserChange(user) except ValueError: pass
def handleUserDisconnected(server, user): deadChats = [] # go through all the private chats removing this user # keep a list of dead chats to remove them all at once for chat in server.chats.values(): if chat.hasInvite(user): chat.removeInvite(user) if chat.hasUser(user): chat.removeUser(user) if len(chat.users) > 0: # Send a chat leave to everyone left in the chat. leave = HLPacket(HTLS_HDR_CHAT_USER_LEAVE) leave.addInt32(DATA_CHATID, chat.id) leave.addNumber(DATA_UID, user.uid) for u in chat.users: server.sendPacket(leave, u) else: # Otherwise, mark the chat as dead. deadChats.append(chat.id) # Now we can remove all the dead chats without modifying the list we were iterating through. for dead in deadChats: server.removeChat(dead)
def handleChat(server, user, packet): str = packet.getString(DATA_STRING, "") opt = packet.getNumber(DATA_OPTION, 0) ref = packet.getNumber(DATA_CHATID, 0) pchat = server.getChat(ref) if user.hasPriv(PRIV_SEND_CHAT) and (len(str.strip()) > 0): str = str.replace("\n", "\r") lines = str.split("\r") format = conf.CHAT_FORMAT prefix = conf.CHAT_PREFIX_LEN if conf.CHAT_PREFIX_ADD_NICK_LEN: prefix = prefix + len(user.nick) if opt > 0: format = conf.EMOTE_FORMAT prefix = conf.EMOTE_PREFIX_LEN + len(user.nick) for lineStr in lines: line = lineStr[:conf.MAX_CHAT_LEN] if (len(line.strip()) > 0) and (not _dispatchCommand( server, user, line, ref)): f_str = format % (user.nick, line) chat = HLPacket(HTLS_HDR_CHAT) chat.addNumber(DATA_UID, user.uid) chat.addNumber(DATA_OFFSET, prefix) chat.addString(DATA_STRING, f_str) if opt > 0: chat.addNumber(DATA_OPTION, opt) if pchat is not None: # If this is meant for a private chat, add the chat ID # and send it to everyone in the chat. chat.addInt32(DATA_CHATID, pchat.id) for u in pchat.users: server.sendPacket(chat, u) else: # Otherwise, send it to public chat (and log it). server.sendPacket( chat, lambda c: c.context.hasPriv(PRIV_READ_CHAT))
def sendIcon(self, icon): if icon: p = HLPacket(HTLC_HDR_ICON_SET) p.addBinary(DATA_GIFICON, icon) self.sendPacket(p)
def sendChat(self, chat): if chat: p = HLPacket(HTLC_HDR_CHAT) p.addString(DATA_STRING, chat) self.sendPacket(p)
def __init__(self): self.packet = HLPacket() self.gotMagic = False self.expectedMagicLen = 0 self.buffered = b""
def sendMessage(self, msg, to): p = HLPacket(HTLC_HDR_MSG, self.nextTaskID()) p.addString(DATA_STRING, msg) p.addNumber(DATA_UID, to) self.sendPacket(p) return self.taskDeferred()
def gotIcon(data, user, server): if user and data and len(data) > 0 and certifyIcon(data): user.gif = data change = HLPacket(HTLS_HDR_ICON_CHANGE) change.addNumber(DATA_UID, user.uid) server.sendPacket(change)
def _setNick(self, n): self._nickname = n if self.connection is not None: p = HLPacket(HTLC_HDR_USER_CHANGE) p.addString(DATA_NICK, self._nickname) self.sendPacket(p)
def sendChange(self, nick): p = HLPacket(HTLC_HDR_USER_CHANGE) p.addString(DATA_NICK, nick) self.sendPacket(p)