2018-07-05 20:20:22 +02:00
|
|
|
# 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
|
2019-01-30 20:23:16 +01:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-07-05 20:20:22 +02:00
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2019-01-30 20:23:16 +01:00
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2018-07-05 20:20:22 +02:00
|
|
|
|
|
|
|
# XEP-0172: User Nickname
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from typing import Any
|
|
|
|
from typing import Tuple
|
|
|
|
|
2018-07-05 20:20:22 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import nbxmpp
|
|
|
|
|
|
|
|
from gajim.common import app
|
2019-01-30 20:23:16 +01:00
|
|
|
from gajim.common.nec import NetworkEvent
|
|
|
|
from gajim.common.modules.base import BaseModule
|
|
|
|
from gajim.common.modules.util import event_node
|
2018-07-05 20:20:22 +02:00
|
|
|
|
|
|
|
log = logging.getLogger('gajim.c.m.user_nickname')
|
|
|
|
|
|
|
|
|
2019-01-30 20:23:16 +01:00
|
|
|
class UserNickname(BaseModule):
|
2018-07-05 20:20:22 +02:00
|
|
|
|
2019-01-30 20:23:16 +01:00
|
|
|
_nbxmpp_extends = 'Nickname'
|
|
|
|
_nbxmpp_methods = [
|
|
|
|
'set_nickname',
|
|
|
|
]
|
2018-07-05 20:20:22 +02:00
|
|
|
|
2019-01-30 20:23:16 +01:00
|
|
|
def __init__(self, con):
|
|
|
|
BaseModule.__init__(self, con)
|
|
|
|
self._register_pubsub_handler(self._nickname_received)
|
2018-07-05 20:20:22 +02:00
|
|
|
|
2019-01-30 20:23:16 +01:00
|
|
|
@event_node(nbxmpp.NS_NICK)
|
|
|
|
def _nickname_received(self, _con, _stanza, properties):
|
|
|
|
nick = properties.pubsub_event.data
|
|
|
|
if properties.self_message:
|
|
|
|
if nick is None:
|
|
|
|
nick = app.config.get_per('accounts', self._account, 'name')
|
|
|
|
app.nicks[self._account] = nick
|
2018-07-05 20:20:22 +02:00
|
|
|
|
2019-01-30 20:23:16 +01:00
|
|
|
for contact in app.contacts.get_contacts(self._account,
|
|
|
|
str(properties.jid)):
|
|
|
|
contact.contact_name = nick
|
2018-07-05 20:20:22 +02:00
|
|
|
|
2019-01-30 20:23:16 +01:00
|
|
|
app.nec.push_incoming_event(
|
|
|
|
NetworkEvent('nickname-received',
|
|
|
|
account=self._account,
|
|
|
|
jid=properties.jid.getBare(),
|
|
|
|
nickname=nick))
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def parse_nickname(stanza: nbxmpp.Node) -> str:
|
2018-07-19 21:26:45 +02:00
|
|
|
nick = stanza.getTag('nick', namespace=nbxmpp.NS_NICK)
|
|
|
|
if nick is None:
|
|
|
|
return ''
|
|
|
|
return nick.getData()
|
|
|
|
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def get_instance(*args: Any, **kwargs: Any) -> Tuple[UserNickname, str]:
|
2018-07-07 13:52:44 +02:00
|
|
|
return UserNickname(*args, **kwargs), 'UserNickname'
|