コード例 #1
0
ファイル: channel.py プロジェクト: zorun/hookbox
    def publish(self, user, payload, needs_auth=True, conn=None, **kwargs):
        try:
            encoded_payload = json.loads(payload)
        except:
            raise ExpectedException("Invalid json for payload")
        payload = encoded_payload
        if needs_auth and (self.moderated or self.moderated_publish):
            form = { 'channel_name': self.name, 'payload': json.dumps(encoded_payload) }
            success, options = self.server.http_request('publish', user.get_cookie(conn), form, conn=conn)
            self.server.maybe_auto_subscribe(user, options, conn=conn)
            if not success:
                raise ExpectedException(options.get('error', 'Unauthorized'))
            payload = options.get('override_payload', payload)

        frame = {"channel_name": self.name, "payload":payload, "datetime": get_now()}

        if not self.anonymous:
            if 'originator' in kwargs:
                frame['user'] = kwargs['originator']
            else:
                frame['user'] = user.get_name()

        omit = None
        if not self.reflective:
            omit = conn
        for subscriber in self.subscribers:
            subscriber.send_frame('PUBLISH', frame, omit=omit)
        self.server.admin.channel_event('publish', self.name, frame)
        if self.history_size:
            del frame['channel_name']
            self.history.append(('PUBLISH', frame))
            self.prune_history()
コード例 #2
0
ファイル: channel.py プロジェクト: zorun/hookbox
 def unsubscribe(self, user, conn=None, needs_auth=True, force_auth=False):
     if user not in self.subscribers:
         return
     if needs_auth and (self.moderated or self.moderated_unsubscribe):
         form = { 'channel_name': self.name, "user": user.get_name() }
         try:
             success, options = self.server.http_request('unsubscribe', user.get_cookie(conn), form, conn=conn)
         except ExpectedException:
             if not force_auth:
                 raise
             success, options = False, {}
         if not (success or force_auth):
             raise ExpectedException(options.get('error', 'Unauthorized'))
         self.server.maybe_auto_subscribe(user, options, conn=conn)
     frame = {"channel_name": self.name, "user": user.get_name(), 'datetime': get_now()}
     self.server.admin.channel_event('unsubscribe', self.name, frame)
     if self.presenceful:
         for subscriber in self.subscribers:
             if subscriber == user: continue
             subscriber.send_frame('UNSUBSCRIBE', frame)
     user.send_frame('UNSUBSCRIBE', frame)
     self.subscribers.remove(user)
     user.channel_unsubscribed(self)
     if self.history_size:
         del frame['channel_name']
         self.history.append(('UNSUBSCRIBE', frame))
         self.prune_history()
     
     if not self.subscribers:
         self.server.destroy_channel(self.name)
コード例 #3
0
ファイル: user.py プロジェクト: zorun/hookbox
    def send_message(self,
                     recipient_name,
                     payload,
                     conn=None,
                     needs_auth=True):
        try:
            encoded_payload = json.loads(payload)
        except:
            raise ExpectedException("Invalid json for payload")
        payload = encoded_payload
        if needs_auth and self.moderated_message:
            form = {
                'sender': self.get_name(),
                'recipient': recipient_name,
                'recipient_exists': self.server.exists_user(recipient_name),
                'payload': json.dumps(payload)
            }
            success, options = self.server.http_request('message',
                                                        self.get_cookie(conn),
                                                        form,
                                                        conn=conn)
            self.server.maybe_auto_subscribe(self, options, conn=conn)
            if not success:
                raise ExpectedException(options.get('error', 'Unauthorized'))
            payload = options.get('override_payload', payload)
            recipient_name = options.get('override_recipient_name',
                                         recipient_name)
        elif not self.server.exists_user(recipient_name):
            raise ExpectedException('Invalid user name')

        recipient = self.server.get_user(
            recipient_name) if self.server.exists_user(
                recipient_name) else None

        frame = {
            "sender": self.get_name(),
            "recipient": recipient.get_name() if recipient else "null",
            "payload": payload,
            "datetime": get_now()
        }
        if recipient:
            recipient.send_frame('MESSAGE', frame)
        if self.reflective and (not recipient or recipient.name != self.name):
            self.send_frame('MESSAGE', frame)
コード例 #4
0
    def frame_CONNECT(self, fid, fargs):
        if self.state != 'initial':
            return self.send_error(fid, "Already logged in")
        if 'cookie_string' not in fargs:
            raise ExpectedException("Missing cookie_string")

        self.cookie_string = fargs['cookie_string']
        self.cookies = parse_cookies(fargs['cookie_string'])
        self.cookie_id = self.cookies.get(self.cookie_identifier, None)
        self.server.connect(self, fargs.get('payload', 'null'))
        self.state = 'connected'
コード例 #5
0
ファイル: channel.py プロジェクト: ceshine/hookbox
    def subscribe(self, user, conn=None, needs_auth=True):
        if user in self.subscribers:
            user.channel_subscribed(self, conn=conn)
            return

        has_initial_data = False
        initial_data = None

        if needs_auth and (self.moderated or self.moderated_subscribe):
            form = {'channel_name': self.name, "user": user.get_name()}
            success, options = self.server.http_request('subscribe',
                                                        user.get_cookie(conn),
                                                        form,
                                                        conn=conn)
            if not success:
                raise ExpectedException(options.get('error', 'Unauthorized'))
            if 'initial_data' in options:
                has_initial_data = True
                initial_data = options['initial_data']
            self.server.maybe_auto_subscribe(user, options, conn=conn)

        if has_initial_data or self.history:
            frame = dict(channel_name=self.name,
                         history=self.history,
                         initial_data=initial_data)
            user.send_frame('CHANNEL_INIT', frame, channel=self)

        self.subscribers.append(user)
        user.channel_subscribed(self, conn=conn)
        _now = get_now()
        frame = {
            "channel_name": self.name,
            "user": user.get_name(),
            "datetime": _now
        }
        self.server.admin.channel_event('subscribe', self.name, frame)
        if self.presenceful:
            for subscriber in self.subscribers:
                if subscriber == user: continue
                subscriber.send_frame('SUBSCRIBE', frame, channel=self)

        frame = self._build_subscribe_frame(user, initial_data)

        user.send_frame('SUBSCRIBE', frame, channel=self)

        if self.history_size:
            self.history.append(('SUBSCRIBE', {
                "user": user.get_name(),
                "datetime": _now
            }))
            self.prune_history()