'For the given user and rooms, return any new events for them. If
there are no new events wait for up to `timeout` milliseconds for any
new events to happen before returning.
If `only_keys` is not None, events from keys will be sent down.
If explicit_room_id is not set, the user\'s joined rooms will be polled
for events.
If explicit_room_id is set, that room will be polled for events only if
it is world readable or the user has joined the room.'

@defer.inlineCallbacks
def get_events_for(self, user, pagination_config, timeout, only_keys=None, is_guest=False, explicit_room_id=None):

    from_token = pagination_config.from_token
    if (not from_token):
        from_token = (yield self.event_sources.get_current_token())
    limit = pagination_config.limit
    (room_ids, is_joined) = (yield self._get_room_ids(user, explicit_room_id))
    is_peeking = (not is_joined)
    @defer.inlineCallbacks
    def check_for_updates(before_token, after_token):
        if (not after_token.is_after(before_token)):
            defer.returnValue(EventStreamResult([], (from_token, from_token)))
        events = []
        end_token = from_token
        for (name, source) in self.event_sources.sources.items():
            keyname = ('%s_key' % name)
            before_id = getattr(before_token, keyname)
            after_id = getattr(after_token, keyname)
            if (before_id == after_id):
                continue
            if (only_keys and (name not in only_keys)):
                continue
            (new_events, new_key) = (yield source.get_new_events(user=user, from_key=getattr(from_token, keyname), limit=limit, is_guest=is_peeking, room_ids=room_ids, explicit_room_id=explicit_room_id))
            if (name == 'room'):
                new_events = (yield filter_events_for_client(self.store, user.to_string(), new_events, is_peeking=is_peeking))
            elif (name == 'presence'):
                now = self.clock.time_msec()
                new_events[:] = [{'type': 'm.presence', 'content': format_user_presence_state(event, now)} for event in new_events]
            events.extend(new_events)
            end_token = end_token.copy_and_replace(keyname, new_key)
        defer.returnValue(EventStreamResult(events, (from_token, end_token)))
    user_id_for_stream = user.to_string()
    if is_peeking:
        user_id_for_stream = ('_PEEKING_%s_%s' % (explicit_room_id, user_id_for_stream))
    result = (yield self.wait_for_events(user_id_for_stream, timeout, check_for_updates, room_ids=room_ids, from_token=from_token))
    defer.returnValue(result)
