gajim-plural/gajim/common/modules/user_mood.py

102 lines
3.0 KiB
Python
Raw Normal View History

2018-07-05 18:52:43 +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
# 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-0107: User Mood
from typing import Any
from typing import Dict
from typing import List # pylint: disable=unused-import
from typing import Optional
from typing import Tuple
2018-07-05 18:52:43 +02:00
import logging
import nbxmpp
from gi.repository import GLib
from gajim.common.const import PEPEventType, MOODS
from gajim.common.exceptions import StanzaMalformed
from gajim.common.modules.pep import AbstractPEPModule, AbstractPEPData
from gajim.common.types import ConnectionT
2018-07-05 18:52:43 +02:00
log = logging.getLogger('gajim.c.m.user_mood')
class UserMoodData(AbstractPEPData):
type_ = PEPEventType.MOOD
def __init__(self, mood: Optional[Dict[str, str]]) -> None:
2018-09-12 00:01:54 +02:00
self.data = mood
2018-07-05 18:52:43 +02:00
def as_markup_text(self) -> str:
if self.data is None:
return ''
2018-09-12 00:01:54 +02:00
mood = self._translate_mood(self.data['mood'])
2018-07-05 18:52:43 +02:00
markuptext = '<b>%s</b>' % GLib.markup_escape_text(mood)
2018-09-12 00:01:54 +02:00
if 'text' in self.data:
text = self.data['text']
2018-07-05 18:52:43 +02:00
markuptext += ' (%s)' % GLib.markup_escape_text(text)
return markuptext
2018-09-11 22:25:55 +02:00
@staticmethod
def _translate_mood(mood: str) -> str:
2018-07-05 18:52:43 +02:00
if mood in MOODS:
return MOODS[mood]
2018-09-11 22:25:55 +02:00
return mood
2018-07-05 18:52:43 +02:00
class UserMood(AbstractPEPModule):
name = 'mood'
namespace = nbxmpp.NS_MOOD
pep_class = UserMoodData
store_publish = True
_log = log
def __init__(self, con: ConnectionT) -> None:
2018-07-05 18:52:43 +02:00
AbstractPEPModule.__init__(self, con, con.name)
self.handlers = [] # type: List[Tuple[Any, ...]]
2018-07-05 18:52:43 +02:00
def _extract_info(self, item: nbxmpp.Node) -> Optional[Dict[str, str]]:
2018-07-05 18:52:43 +02:00
mood_dict = {}
mood_tag = item.getTag('mood', namespace=nbxmpp.NS_MOOD)
if mood_tag is None:
raise StanzaMalformed('No mood node')
for child in mood_tag.getChildren():
name = child.getName().strip()
if name == 'text':
mood_dict['text'] = child.getData()
else:
mood_dict['mood'] = name
return mood_dict or None
def _build_node(self, data: Optional[Tuple[str, str]]) -> nbxmpp.Node:
2018-07-05 18:52:43 +02:00
item = nbxmpp.Node('mood', {'xmlns': nbxmpp.NS_MOOD})
if data is None:
return
mood, text = data
if mood:
item.addChild(mood)
if text:
item.addChild('text', payload=text)
return item
def get_instance(*args: Any, **kwargs: Any) -> Tuple[UserMood, str]:
return UserMood(*args, **kwargs), 'UserMood'