def push(self, request):
        command = request.args.get("c")
        if command is None:
            raise AJAXException, "No command specified."
        self.__total_hit()

        seq_no = request.args.get("n")
        try:
            if seq_no is not None:
                seq_no = int(seq_no[0])
                if seq_no < 0 or seq_no > MAX_SEQNO:
                    raise ValueError
        except ValueError:
            raise AJAXEngine("Bad sequence number %r" % seq_no)

        session = self.getSession(request)
        try:
            session.push(ircclient.irc_decode(command[0]), seq_no)
        except AttributeError:  # occurs when we haven't noticed an error
            session.disconnect()
            raise AJAXException, "Connection closed by server; try reconnecting by reloading the page."
        except Exception, e:  # catch all
            session.disconnect()
            traceback.print_exc(file=sys.stderr)
            raise AJAXException, "Unknown error."
        def onMessage(self, msg, isBinary):
            # we don't bother checking the Origin header, as if you can auth then you've been able to pass the browser's
            # normal origin handling (POSTed the new connection request and managed to get the session id)
            state = self.__state
            message_type, message = msg[:1], msg[1:]
            if state == self.AWAITING_AUTH:
                if message_type == "s":  # subscribe
                    tokens = message.split(",", 1)
                    if len(tokens) != 2:
                        self.close("Bad tokens")
                        return

                    seq_no, message = tokens[0], tokens[1]
                    try:
                        seq_no = int(seq_no)
                        if seq_no < 0 or seq_no > MAX_SEQNO:
                            raise ValueError
                    except ValueError:
                        self.close("Bad value")

                    session = Sessions.get(message)
                    if not session:
                        self.close(BAD_SESSION_MESSAGE)
                        return

                    self.__cancelTimeout()
                    self.__session = session
                    self.send("s", "True")
                    self.__state = self.AUTHED
                    self.__channel = WebSocketChannel(self)
                    session.subscribe(self.__channel, seq_no)
                    return
            elif state == self.AUTHED:
                if message_type == "p":  # push
                    tokens = message.split(",", 1)
                    if len(tokens) != 2:
                        self.close("Bad tokens")
                        return

                    seq_no, message = tokens[0], tokens[1]
                    try:
                        seq_no = int(seq_no)
                        if seq_no < 0 or seq_no > MAX_SEQNO:
                            raise ValueError
                    except ValueError:
                        self.close("Bad value")
                    self.__session.push(ircclient.irc_decode(message))
                    return

            self.close("Bad message type")
Exemple #3
0
  def push(self, request):
    command = request.args.get("c")
    if command is None:
      raise AJAXException, "No command specified."
    self.__total_hit()
    
    decoded = ircclient.irc_decode(command[0])
    
    session = self.getSession(request)

    if len(decoded) > config.MAXLINELEN:
      session.disconnect()
      raise AJAXException, "Line too long."

    try:
      session.push(decoded)
    except AttributeError: # occurs when we haven't noticed an error
      session.disconnect()
      raise AJAXException, "Connection closed by server; try reconnecting by reloading the page."
    except Exception, e: # catch all
      session.disconnect()        
      traceback.print_exc(file=sys.stderr)
      raise AJAXException, "Unknown error."
Exemple #4
0
    def push(self, request):
        command = request.args.get("c")
        if command is None:
            raise AJAXException, "No command specified."
        self.__total_hit()

        decoded = ircclient.irc_decode(command[0])

        session = self.getSession(request)

        if len(decoded) > config.MAXLINELEN:
            session.disconnect()
            raise AJAXException, "Line too long."

        try:
            session.push(decoded)
        except AttributeError:  # occurs when we haven't noticed an error
            session.disconnect()
            raise AJAXException, "Connection closed by server; try reconnecting by reloading the page."
        except Exception, e:  # catch all
            session.disconnect()
            traceback.print_exc(file=sys.stderr)
            raise AJAXException, "Unknown error."
    def newConnection(self, request):
        ticket = login_optional(request)

        ip = request.getClientIP()

        nick = request.args.get("nick")
        if not nick:
            raise AJAXException, "Nickname not supplied."
        nick = ircclient.irc_decode(nick[0])

        password = request.args.get("password")
        if password is not None:
            password = ircclient.irc_decode(password[0])

        for i in range(10):
            id = get_session_id()
            if not Sessions.get(id):
                break
        else:
            raise IDGenerationException()

        session = IRCSession(id)

        qticket = getSessionData(request).get("qticket")
        if qticket is None:
            perform = None
        else:
            service_mask = config.AUTH_SERVICE
            msg_mask = service_mask.split("!")[0] + "@" + service_mask.split(
                "@", 1)[1]
            perform = ["PRIVMSG %s :TICKETAUTH %s" % (msg_mask, qticket)]

        ident, realname = config.IDENT, config.REALNAME
        if ident is config_options.IDENT_HEX or ident is None:  # latter is legacy
            ident = socket.inet_aton(ip).encode("hex")
        elif ident is config_options.IDENT_NICKNAME:
            ident = nick

        self.__connect_hit()

        def proceed(hostname):
            kwargs = dict(nick=nick,
                          ident=ident,
                          ip=ip,
                          realname=realname,
                          perform=perform,
                          hostname=hostname)
            if password is not None:
                kwargs["password"] = password

            client = ircclient.createIRC(session, **kwargs)
            session.client = client

        if not hasattr(config, "WEBIRC_MODE") or config.WEBIRC_MODE == "hmac":
            proceed(None)
        elif config.WEBIRC_MODE != "hmac":
            notice = lambda x: session.event(connect_notice(x))
            notice("Looking up your hostname...")

            def callback(hostname):
                notice("Found your hostname.")
                proceed(hostname)

            def errback(failure):
                notice("Couldn't look up your hostname!")
                proceed(ip)

            qdns.lookupAndVerifyPTR(ip,
                                    timeout=[config.DNS_TIMEOUT]).addCallbacks(
                                        callback, errback)

        Sessions[id] = session

        return json.dumps((True, id, TRANSPORTS))
Exemple #6
0
  def newConnection(self, request):
    ticket = login_optional(request)
    
    ip = request.getClientIP()

    nick = request.args.get("nick")
    if not nick:
      raise AJAXException, "Nickname not supplied."
    nick = ircclient.irc_decode(nick[0])

    password = request.args.get("password")
    if password is not None:
      password = ircclient.irc_decode(password[0])
      
    for i in xrange(10):
      id = get_session_id()
      if not Sessions.get(id):
        break
    else:
      raise IDGenerationException()

    session = IRCSession(id)

    qticket = getSessionData(request).get("qticket")
    if qticket is None:
      perform = None
    else:
      service_mask = config.AUTH_SERVICE
      msg_mask = service_mask.split("!")[0] + "@" + service_mask.split("@", 1)[1]
      perform = ["PRIVMSG %s :TICKETAUTH %s" % (msg_mask, qticket)]

    ident, realname = config.IDENT, config.REALNAME
    if ident is config_options.IDENT_HEX or ident is None: # latter is legacy
      ident = socket.inet_aton(ip).encode("hex")
    elif ident is config_options.IDENT_NICKNAME:
      ident = nick
    elif ident is config_options.IDENT_FORWARDED_USER:
        ident = request.getHeader(config.IDENT_FORWARDED_USER_HEADER)



    self.__connect_hit()

    def proceed(hostname):
      kwargs = dict(nick=nick, ident=ident, ip=ip, realname=realname, perform=perform, hostname=hostname)
      if password is not None:
        kwargs["password"] = password
        
      client = ircclient.createIRC(session, **kwargs)
      session.client = client

    if not hasattr(config, "WEBIRC_MODE") or config.WEBIRC_MODE == "hmac":
      proceed(None)
    elif config.WEBIRC_MODE != "hmac":
      notice = lambda x: session.event(connect_notice(x))
      notice("Looking up your hostname...")
      def callback(hostname):
        notice("Found your hostname.")
        proceed(hostname)
      def errback(failure):
        notice("Couldn't look up your hostname!")
        proceed(ip)
      qdns.lookupAndVerifyPTR(ip, timeout=[config.DNS_TIMEOUT]).addCallbacks(callback, errback)

    Sessions[id] = session
    
    return id
Exemple #7
0
  def newConnection(self, request):
    ip = request.getClientIP()

    nick = request.args.get("nick")
    if not nick:
      raise AJAXException, "Nickname not supplied."
    nick = ircclient.irc_decode(nick[0])

    password = request.args.get("password")
    if password is not None:
      password = ircclient.irc_decode(password[0])

    authUser = request.args.get("authUser")
    if authUser is not None:
      authUser = ircclient.irc_decode(authUser[0])
    authSecret = request.args.get("authSecret")
    if authSecret is not None:
      authSecret = ircclient.irc_decode(authSecret[0])

    for i in xrange(10):
      id = get_session_id()
      if not Sessions.get(id):
        break
    else:
      raise IDGenerationException()

    session = IRCSession(id)
    perform = None

    ident, realname = config.irc["ident_string"], config.irc["realname"]
    if config.irc["ident"] == "hex":
      ident = socket.inet_aton(ip).encode("hex")
    elif config.irc["ident"] == "nick":
      ident = nick

    self.__connect_hit()

    def proceed(hostname):
      kwargs = dict(nick=nick, ident=ident, ip=ip, realname=realname, perform=perform, hostname=hostname)
      if password is not None:
        kwargs["password"] = password
      if ((authUser is not None) and (authSecret is not None)):
        kwargs["authUser"] = authUser
        kwargs["authSecret"] = authSecret
        
        
      client = ircclient.createIRC(session, **kwargs)
      session.client = client

    if config.irc["webirc_mode"] == "":
      proceed(None)
    else:
      notice = lambda x: session.event(connect_notice(x))
      notice("Looking up your hostname...")
      def callback(hostname):
        notice("Found your hostname.")
        proceed(hostname)
      def errback(failure):
        notice("Couldn't look up your hostname!")
        proceed(ip)
      qdns.lookupAndVerifyPTR(ip, timeout=[config.tuneback["dns_timeout"]]).addCallbacks(callback, errback)

    Sessions[id] = session
    
    return id