Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
 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()
Exemple #6
0
 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)
Exemple #7
0
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)
Exemple #8
0
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)
Exemple #9
0
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)
Exemple #10
0
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)
Exemple #11
0
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)
Exemple #12
0
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)
Exemple #13
0
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)
Exemple #14
0
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)
Exemple #15
0
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))
Exemple #16
0
def handleBroadcast(server, user, packet):
    s = packet.getString(DATA_STRING, "")
    broadcast = HLPacket(HTLS_HDR_BROADCAST)
    broadcast.addString(DATA_STRING, s)
    server.sendPacket(broadcast)
    server.sendPacket(packet.response(), user)
Exemple #17
0
def handle(server, user, arg, ref):
    if len(arg) > 0 and user.hasPriv(PRIV_BROADCAST):
        broadcast = HLPacket(HTLS_HDR_BROADCAST)
        broadcast.addString(DATA_STRING, arg)
        server.sendPacket(broadcast)
Exemple #18
0
 def sendChat(self, chat):
     if chat:
         p = HLPacket(HTLC_HDR_CHAT)
         p.addString(DATA_STRING, chat)
         self.sendPacket(p)
Exemple #19
0
 def sendChange(self, nick):
     p = HLPacket(HTLC_HDR_USER_CHANGE)
     p.addString(DATA_NICK, nick)
     self.sendPacket(p)
Exemple #20
0
 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)
Exemple #21
0
 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()