'Update the roster after receiving a roster stanza.
:param iq: The roster stanza.'

def _handle_roster(self, iq):

    if (iq[u'type'] == u'set'):
        if (iq[u'from'].bare and (iq[u'from'].bare != self.boundjid.bare)):
            raise XMPPError(condition=u'service-unavailable')
    roster = self.client_roster
    if iq[u'roster'][u'ver']:
        roster.version = iq[u'roster'][u'ver']
    items = iq[u'roster'][u'items']
    valid_subscriptions = (u'to', u'from', u'both', u'none', u'remove')
    for (jid, item) in items.items():
        if (item[u'subscription'] in valid_subscriptions):
            roster[jid][u'name'] = item[u'name']
            roster[jid][u'groups'] = item[u'groups']
            roster[jid][u'from'] = (item[u'subscription'] in (u'from', u'both'))
            roster[jid][u'to'] = (item[u'subscription'] in (u'to', u'both'))
            roster[jid][u'pending_out'] = (item[u'ask'] == u'subscribe')
            roster[jid].save(remove=(item[u'subscription'] == u'remove'))
    if (iq[u'type'] == u'set'):
        resp = self.Iq(stype=u'result', sto=iq[u'from'], sid=iq[u'id'])
        resp.enable(u'roster')
        resp.send()
