1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 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 133 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
|
########################################################################
# File name: p2p.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import asyncio
import aioxmpp.service
from .conversation import (
AbstractConversationMember,
AbstractConversation,
AbstractConversationService,
ConversationFeature,
)
from .dispatcher import IMDispatcher, MessageSource
from .service import ConversationService
class Member(AbstractConversationMember):
"""
Member of a one-on-one conversation.
.. autoattribute:: direct_jid
"""
def __init__(self, peer_jid, is_self):
super().__init__(peer_jid, is_self)
@property
def direct_jid(self):
"""
The JID of the peer.
"""
return self._conversation_jid
@property
def uid(self) -> bytes:
return b"xmpp:" + str(self._conversation_jid.bare()).encode("utf-8")
class Conversation(AbstractConversation):
"""
Implementation of :class:`~.im.conversation.AbstractConversation` for
one-on-one conversations.
.. seealso::
:class:`.im.conversation.AbstractConversation`
for documentation on the interface implemented by this class.
"""
def __init__(self, service, peer_jid, parent=None):
super().__init__(service, parent=parent)
self.__peer_jid = peer_jid
self.__members = (
Member(self._client.local_jid, True),
Member(peer_jid, False),
)
@property
def features(self):
return (
frozenset([ConversationFeature.SEND_MESSAGE,
ConversationFeature.LEAVE]) |
super().features
)
def _handle_message(self, msg, peer, sent, source):
if sent:
member = self.__members[0]
else:
member = self.__members[1]
self._service.logger.debug("emitting on_message for %s",
self.__peer_jid)
self.on_message(msg, member, source)
@property
def jid(self):
return self.__peer_jid
@property
def members(self):
return self.__members
@property
def me(self):
return self.__members[0]
def send_message(self, msg):
msg.autoset_id()
msg.to = self.__peer_jid
self.on_message(msg, self.me, MessageSource.STREAM)
return self._client.enqueue(msg)
async def send_message_tracked(self, msg):
raise self._not_implemented_error("message tracking")
async def leave(self):
self._service._conversation_left(self)
class Service(AbstractConversationService, aioxmpp.service.Service):
"""
Manage one-to-one conversations.
.. seealso::
:class:`~.AbstractConversationService`
for useful common signals
This service manages one-to-one conversations, including private
conversations running in the framework of a multi-user chat. In those
cases, the respective multi-user chat conversation service requests a
conversation from this service to use.
For each bare JID, there can either be a single conversation for the bare
JID or zero or more conversations for full JIDs. Mixing conversations to
bare and full JIDs of the same bare JID is not allowed, because it is
ambiguous.
This service creates conversations if it detects them as one-on-one
conversations. Subscribe to
:meth:`aioxmpp.im.ConversationService.on_conversation_added` to be notified
about new conversations being auto-created.
.. automethod:: get_conversation
"""
ORDER_AFTER = [
ConversationService,
IMDispatcher,
]
def __init__(self, client, **kwargs):
super().__init__(client, **kwargs)
self._conversationmap = {}
self.on_conversation_new.connect(
self.dependencies[ConversationService]._add_conversation
)
def _make_conversation(self, peer_jid, spontaneous):
self.logger.debug("creating new conversation for %s (spontaneous=%s)",
peer_jid, spontaneous)
result = Conversation(self, peer_jid, parent=None)
self._conversationmap[peer_jid] = result
if spontaneous:
self.on_spontaneous_conversation(result)
self.on_conversation_new(result)
result.on_enter()
self.logger.debug("new conversation for %s set up and events emitted",
peer_jid)
return result
@aioxmpp.service.depfilter(IMDispatcher, "message_filter")
def _filter_message(self, msg, peer, sent, source):
try:
existing = self._conversationmap[peer]
except KeyError:
try:
existing = self._conversationmap[peer.bare()]
except KeyError:
existing = None
if (existing is None and
(msg.type_ == aioxmpp.MessageType.CHAT or
msg.type_ == aioxmpp.MessageType.NORMAL) and
msg.body):
conversation_jid = peer.bare()
if msg.xep0045_muc_user is not None:
conversation_jid = peer
existing = self._make_conversation(conversation_jid, True)
if existing is not None:
existing._handle_message(msg, peer, sent, source)
return None
return msg
def get_conversation(self, peer_jid, *, current_jid=None):
"""
Get or create a new one-to-one conversation with a peer.
:param peer_jid: The JID of the peer to converse with.
:type peer_jid: :class:`aioxmpp.JID`
:param current_jid: The current JID to lock the conversation to (see
:rfc:`6121`).
:type current_jid: :class:`aioxmpp.JID`
:rtype: :class:`Conversation`
:return: The new or existing conversation with the peer.
`peer_jid` must be a full or bare JID. See the :class:`Service`
documentation for details.
.. versionchanged:: 0.10
In 0.9, this was a coroutine. Sorry.
"""
try:
return self._conversationmap[peer_jid]
except KeyError:
pass
return self._make_conversation(peer_jid, False)
def _conversation_left(self, conv):
del self._conversationmap[conv.jid]
|