muc.py 16.5 KB
Newer Older
Philipp Hörist's avatar
Philipp Hörist committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim.  If not, see <http://www.gnu.org/licenses/>.

# XEP-0045: Multi-User Chat
# XEP-0249: Direct MUC Invitations

18
import time
Philipp Hörist's avatar
Philipp Hörist committed
19 20 21 22
import logging

import nbxmpp

23
from gajim.common import i18n
24
from gajim.common.modules import dataforms
Philipp Hörist's avatar
Philipp Hörist committed
25 26
from gajim.common import app
from gajim.common import helpers
27
from gajim.common.caps_cache import muc_caps_cache
Philipp Hörist's avatar
Philipp Hörist committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
from gajim.common.nec import NetworkIncomingEvent

log = logging.getLogger('gajim.c.m.muc')


class MUC:
    def __init__(self, con):
        self._con = con
        self._account = con.name

        self.handlers = [
            ('message', self._mediated_invite, '', nbxmpp.NS_MUC_USER),
            ('message', self._direct_invite, '', nbxmpp.NS_CONFERENCE),
        ]

43
    def pass_disco(self, from_, identities, features, _data, _node):
44 45 46 47 48 49 50 51 52 53 54
        for identity in identities:
            if identity.get('category') != 'conference':
                continue
            if identity.get('type') != 'text':
                continue
            if nbxmpp.NS_MUC in features:
                log.info('Discovered MUC: %s', from_)
                # TODO: make this nicer
                self._con.muc_jid['jabber'] = from_
                raise nbxmpp.NodeProcessed

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    def send_muc_join_presence(self, *args, room_jid=None, password=None,
                               rejoin=False, **kwargs):
        if not app.account_is_connected(self._account):
            return
        presence = self._con.get_module('Presence').get_presence(
            *args, **kwargs)

        if room_jid is not None:
            self._add_history_query(presence, room_jid, rejoin)

        if password is not None:
            presence.setTagData('password', password)

        log.debug('Send MUC join presence:\n%s', presence)

        self._con.connection.send(presence)

    def _add_history_query(self, presence, room_jid, rejoin):
        last_date = app.logger.get_room_last_message_time(
            self._account, room_jid)
        if not last_date:
            last_date = 0

        history = presence.setTag(nbxmpp.NS_MUC + ' x')
        if muc_caps_cache.has_mam(room_jid):
            # The room is MAM capable dont get MUC History
            history.setTag('history', {'maxchars': '0'})
        else:
            # Request MUC History (not MAM)
            tags = {}
            timeout = app.config.get_per('rooms', room_jid,
                                         'muc_restore_timeout')
            if timeout is None or timeout == -2:
                timeout = app.config.get('muc_restore_timeout')
            if last_date == 0 and timeout >= 0:
                last_date = time.time() - timeout * 60
            elif not rejoin and timeout >= 0:
                last_date = max(last_date, time.time() - timeout * 60)
            last_date = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(
                last_date))
            tags['since'] = last_date

            nb = app.config.get_per('rooms', room_jid, 'muc_restore_lines')
            if nb is None or nb == -2:
                nb = app.config.get('muc_restore_lines')
            if nb >= 0:
                tags['maxstanzas'] = nb
            if tags:
                history.setTag('history', tags)

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    def set_subject(self, room_jid, subject):
        if not app.account_is_connected(self._account):
            return
        message = nbxmpp.Message(room_jid, typ='groupchat', subject=subject)
        log.info('Set subject for %s', room_jid)
        self._con.connection.send(message)

    def request_config(self, room_jid):
        if not app.account_is_connected(self._account):
            return
        iq = nbxmpp.Iq(typ='get',
                       queryNS=nbxmpp.NS_MUC_OWNER,
                       to=room_jid)
        iq.setAttr('xml:lang', i18n.LANG)
        log.info('Request config for %s', room_jid)
        self._con.connection.SendAndCallForResponse(
            iq, self._config_received)

    def _config_received(self, stanza):
        if not nbxmpp.isResultNode(stanza):
            log.info('Error: %s', stanza.getError())
            return

        room_jid = stanza.getFrom().getStripped()
        payload = stanza.getQueryPayload()

        for form in payload:
            if form.getNamespace() == nbxmpp.NS_DATA:
Philipp Hörist's avatar
Philipp Hörist committed
133
                dataform = dataforms.extend_form(node=form)
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
                log.info('Config form received for %s', room_jid)
                app.nec.push_incoming_event(MucOwnerReceivedEvent(
                    None,
                    conn=self._con,
                    form_node=form,
                    dataform=dataform,
                    jid=room_jid))
                break

    def cancel_config(self, room_jid):
        if not app.account_is_connected(self._account):
            return
        cancel = nbxmpp.Node(tag='x', attrs={'xmlns': nbxmpp.NS_DATA,
                                             'type': 'cancel'})
        iq = nbxmpp.Iq(typ='set',
                       queryNS=nbxmpp.NS_MUC_OWNER,
                       payload=cancel,
                       to=room_jid)
        log.info('Cancel config for %s', room_jid)
        self._con.connection.SendAndCallForResponse(
            iq, self._default_response, {})

    def destroy(self, room_jid, reason='', jid=''):
        if not app.account_is_connected(self._account):
            return
        iq = nbxmpp.Iq(typ='set',
                       queryNS=nbxmpp.NS_MUC_OWNER,
                       to=room_jid)
        destroy = iq.setQuery().setTag('destroy')
        if reason:
            destroy.setTagData('reason', reason)
        if jid:
            destroy.setAttr('jid', jid)
        log.info('Destroy room: %s, reason: %s, alternate: %s',
                 room_jid, reason, jid)
        self._con.connection.SendAndCallForResponse(
            iq, self._default_response, {})

    def set_config(self, room_jid, form):
        if not app.account_is_connected(self._account):
            return
        iq = nbxmpp.Iq(typ='set', to=room_jid, queryNS=nbxmpp.NS_MUC_OWNER)
        query = iq.setQuery()
        form.setAttr('type', 'submit')
        query.addChild(node=form)
        log.info('Set config for %s', room_jid)
        self._con.connection.SendAndCallForResponse(
            iq, self._default_response, {})

    def set_affiliation(self, room_jid, users_dict):
        if not app.account_is_connected(self._account):
            return
        iq = nbxmpp.Iq(typ='set', to=room_jid, queryNS=nbxmpp.NS_MUC_ADMIN)
        item = iq.setQuery()
        for jid in users_dict:
            affiliation = users_dict[jid].get('affiliation')
            reason = users_dict[jid].get('reason') or None
            item_tag = item.addChild('item', {'jid': jid,
                                              'affiliation': affiliation})
            if reason is not None:
                item_tag.setTagData('reason', reason)
        log.info('Set affiliation for %s: %s', room_jid, users_dict)
        self._con.connection.SendAndCallForResponse(
            iq, self._default_response, {})

    def get_affiliation(self, room_jid, affiliation):
        if not app.account_is_connected(self._account):
            return
        iq = nbxmpp.Iq(typ='get', to=room_jid, queryNS=nbxmpp.NS_MUC_ADMIN)
        item = iq.setQuery().setTag('item')
        item.setAttr('affiliation', affiliation)
        log.info('Get affiliation %s for %s', affiliation, room_jid)
        self._con.connection.SendAndCallForResponse(
            iq, self._affiliation_received)

    def _affiliation_received(self, stanza):
        if not nbxmpp.isResultNode(stanza):
            log.info('Error: %s', stanza.getError())
            return

        room_jid = stanza.getFrom().getStripped()
        query = stanza.getTag('query', namespace=nbxmpp.NS_MUC_ADMIN)
        items = query.getTags('item')
        users_dict = {}
        for item in items:
            try:
                jid = helpers.parse_jid(item.getAttr('jid'))
            except helpers.InvalidFormat:
                log.warning('Invalid JID: %s, ignoring it',
                            item.getAttr('jid'))
                continue
            affiliation = item.getAttr('affiliation')
            users_dict[jid] = {'affiliation': affiliation}
            if item.has_attr('nick'):
                users_dict[jid]['nick'] = item.getAttr('nick')
            if item.has_attr('role'):
                users_dict[jid]['role'] = item.getAttr('role')
            reason = item.getTagData('reason')
            if reason:
                users_dict[jid]['reason'] = reason
        log.info('Affiliations received from %s: %s', room_jid, users_dict)
        app.nec.push_incoming_event(MucAdminReceivedEvent(
            None, conn=self._con, room_jid=room_jid, users_dict=users_dict))

    def set_role(self, room_jid, nick, role, reason=''):
        if not app.account_is_connected(self._account):
            return
        iq = nbxmpp.Iq(typ='set', to=room_jid, queryNS=nbxmpp.NS_MUC_ADMIN)
        item = iq.setQuery().setTag('item')
        item.setAttr('nick', nick)
        item.setAttr('role', role)
        if reason:
            item.addChild(name='reason', payload=reason)
        log.info('Set role for %s: %s %s %s', room_jid, nick, role, reason)
        self._con.connection.SendAndCallForResponse(
            iq, self._default_response, {})

251
    def _mediated_invite(self, _con, stanza):
Philipp Hörist's avatar
Philipp Hörist committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        muc_user = stanza.getTag('x', namespace=nbxmpp.NS_MUC_USER)
        if muc_user is None:
            return

        decline = muc_user.getTag('decline')
        if decline is not None:

            room_jid = stanza.getFrom().getStripped()
            from_ = self._get_from(room_jid, decline)

            reason = decline.getTagData('reason')
            log.info('Invite declined: %s, %s', reason, from_)
            app.nec.push_incoming_event(
                GcDeclineReceived(None,
                                  account=self._account,
                                  from_=from_,
                                  room_jid=room_jid,
                                  reason=reason))
            raise nbxmpp.NodeProcessed

        invite = muc_user.getTag('invite')
        if invite is not None:

            room_jid = stanza.getFrom().getStripped()
            from_ = self._get_from(room_jid, invite)

            reason = invite.getTagData('reason')
            password = muc_user.getTagData('password')
            is_continued = invite.getTag('continue') is not None
            log.info('Mediated invite: continued: %s, reason: %s, from: %s',
                     is_continued, reason, from_)
            if room_jid in app.gc_connected[self._account] and \
                    app.gc_connected[self._account][room_jid]:
                # We are already in groupchat. Ignore invitation
                log.info('We are already in this room')
                raise nbxmpp.NodeProcessed

            app.nec.push_incoming_event(
                GcInvitationReceived(None,
                                     account=self._account,
                                     from_=from_,
                                     room_jid=room_jid,
                                     reason=reason,
                                     password=password,
                                     is_continued=is_continued))
            raise nbxmpp.NodeProcessed

    def _get_from(self, room_jid, stanza):
        try:
            from_ = nbxmpp.JID(helpers.parse_jid(stanza.getAttr('from')))
        except helpers.InvalidFormat:
            log.warning('Invalid JID on invite: %s, ignoring it',
                        stanza.getAttr('from'))
            raise nbxmpp.NodeProcessed

        known_contact = app.contacts.get_contacts(self._account, room_jid)
        ignore = app.config.get_per(
            'accounts', self._account, 'ignore_unknown_contacts')
        if ignore and not known_contact:
            log.info('Ignore invite from unknown contact %s', from_)
            raise nbxmpp.NodeProcessed

        return from_

316
    def _direct_invite(self, _con, stanza):
Philipp Hörist's avatar
Philipp Hörist committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        direct = stanza.getTag('x', namespace=nbxmpp.NS_CONFERENCE)
        if direct is None:
            return

        from_ = stanza.getFrom()

        try:
            room_jid = helpers.parse_jid(direct.getAttr('jid'))
        except helpers.InvalidFormat:
            log.warning('Invalid JID on invite: %s, ignoring it',
                        direct.getAttr('jid'))
            raise nbxmpp.NodeProcessed

        reason = direct.getAttr('reason')
        password = direct.getAttr('password')
        is_continued = direct.getAttr('continue') == 'true'

        log.info('Direct invite: continued: %s, reason: %s, from: %s',
                 is_continued, reason, from_)

        app.nec.push_incoming_event(
            GcInvitationReceived(None,
                                 account=self._account,
                                 from_=from_,
                                 room_jid=room_jid,
                                 reason=reason,
                                 password=password,
                                 is_continued=is_continued))
        raise nbxmpp.NodeProcessed

    def invite(self, room, to, reason=None, continue_=False):
        if not app.account_is_connected(self._account):
            return
        contact = app.contacts.get_contact_from_full_jid(self._account, to)
        if contact and contact.supports(nbxmpp.NS_CONFERENCE):
            invite = self._build_direct_invite(room, to, reason, continue_)
        else:
            invite = self._build_mediated_invite(room, to, reason, continue_)
        self._con.connection.send(invite)

357 358
    @staticmethod
    def _build_direct_invite(room, to, reason, continue_):
Philipp Hörist's avatar
Philipp Hörist committed
359 360 361 362 363 364 365 366 367
        message = nbxmpp.Message(to=to)
        attrs = {'jid': room}
        if reason:
            attrs['reason'] = reason
        if continue_:
            attrs['continue'] = 'true'
        password = app.gc_passwords.get(room, None)
        if password:
            attrs['password'] = password
Philipp Hörist's avatar
Philipp Hörist committed
368 369
        message.addChild(name='x', attrs=attrs,
                         namespace=nbxmpp.NS_CONFERENCE)
Philipp Hörist's avatar
Philipp Hörist committed
370 371
        return message

372 373
    @staticmethod
    def _build_mediated_invite(room, to, reason, continue_):
Philipp Hörist's avatar
Philipp Hörist committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        message = nbxmpp.Message(to=room)
        muc_user = message.addChild('x', namespace=nbxmpp.NS_MUC_USER)
        invite = muc_user.addChild('invite', attrs={'to': to})
        if continue_:
            invite.addChild(name='continue')
        if reason:
            invite.setTagData('reason', reason)
        password = app.gc_passwords.get(room, None)
        if password:
            muc_user.setTagData('password', password)
        return message

    def decline(self, room, to, reason=None):
        if not app.account_is_connected(self._account):
            return
        message = nbxmpp.Message(to=room)
        muc_user = message.addChild('x', namespace=nbxmpp.NS_MUC_USER)
        decline = muc_user.addChild('decline', attrs={'to': to})
        if reason:
            decline.setTagData('reason', reason)
        self._con.connection.send(message)

    def request_voice(self, room):
        if not app.account_is_connected(self._account):
            return
        message = nbxmpp.Message(to=room)
400 401 402 403 404 405 406
        xdata = nbxmpp.DataForm(typ='submit')
        xdata.addChild(node=nbxmpp.DataField(name='FORM_TYPE',
                                             value=nbxmpp.NS_MUC + '#request'))
        xdata.addChild(node=nbxmpp.DataField(name='muc#role',
                                             value='participant',
                                             typ='text-single'))
        message.addChild(node=xdata)
Philipp Hörist's avatar
Philipp Hörist committed
407 408
        self._con.connection.send(message)

409 410
    @staticmethod
    def _default_response(_con, stanza, **kwargs):
411 412 413
        if not nbxmpp.isResultNode(stanza):
            log.info('Error: %s', stanza.getError())

Philipp Hörist's avatar
Philipp Hörist committed
414 415 416 417 418 419 420 421 422

class GcInvitationReceived(NetworkIncomingEvent):
    name = 'gc-invitation-received'


class GcDeclineReceived(NetworkIncomingEvent):
    name = 'gc-decline-received'


423 424 425 426 427 428 429 430
class MucAdminReceivedEvent(NetworkIncomingEvent):
    name = 'muc-admin-received'


class MucOwnerReceivedEvent(NetworkIncomingEvent):
    name = 'muc-owner-received'


Philipp Hörist's avatar
Philipp Hörist committed
431 432
def get_instance(*args, **kwargs):
    return MUC(*args, **kwargs), 'MUC'