def __get_content(self, message, target_uid=None, target_group=None, parent_uid=None): """ Prepares a message to be sent with relevant headers and content type. :param message: Message object :param target_peer: The target peer, if any :param target_group: The group name, if any :param parent_uid: Parent UID, if any :return: Content of the message to be sent, in JSON format ":rtype: str """ # Convert content to JSON if message.subject in herald.SUBJECTS_RAW: return pelix.utilities.to_str(message.content) # Update headers message.add_header(herald.MESSAGE_HEADER_SENDER_UID, self.__peer.uid) if target_uid is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_PEER, target_uid) if target_group is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_GROUP, target_group) if parent_uid: message.add_header(herald.MESSAGE_HEADER_REPLIES_TO, parent_uid) return utils.to_json(message)
def __send_message(self, msgtype, target, message, parent_uid=None, target_peer=None, target_group=None): """ Prepares and sends a message over XMPP :param msgtype: Kind of message (chat or groupchat) :param target: Target JID or MUC room :param message: Herald message bean :param parent_uid: UID of the message this one replies to (optional) """ # Convert content to JSON if message.subject in herald.SUBJECTS_RAW: content = to_str(message.content) else: # update headers local_peer = self._directory.get_local_peer() message.add_header(herald.MESSAGE_HEADER_SENDER_UID, local_peer.uid) if target_peer is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_PEER, target_peer.uid) if target_group is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_GROUP, target_group) content = utils.to_json(message) # Prepare an XMPP message, based on the Herald message xmpp_msg = self._bot.make_message(mto=target, mbody=content, msubject=message.subject, mtype=msgtype) xmpp_msg['thread'] = message.uid if parent_uid: xmpp_msg['parent_thread'] = parent_uid # Store message content self._probe.store(herald.PROBE_CHANNEL_MSG_CONTENT, { "uid": message.uid, "content": content }) # Send it, using the 1-thread pool, and wait for its execution future = self.__pool.enqueue(xmpp_msg.send) return future.result()
def __prepare_message(self, message, parent_uid=None, target_peer=None, target_group=None): """ Prepares a HTTP request. :param message: The Message bean to send :param parent_uid: UID of the message this one replies to (optional) :return: A (headers, content) tuple """ # Prepare headers """ headers = {'content-type': CONTENT_TYPE_JSON, 'herald-subject': message.subject, 'herald-uid': message.uid, 'herald-sender-uid': self.__peer_uid, 'herald-timestamp': int(time.time() * 1000), 'herald-port': self.__access_port, 'herald-path': self.__access_path} """ headers = {'content-type': CONTENT_TYPE_JSON} message.add_header(herald.MESSAGE_HEADER_SENDER_UID, self.__peer_uid) message.add_header(herald.transports.http.MESSAGE_HEADER_PORT, self.__access_port) message.add_header(herald.transports.http.MESSAGE_HEADER_PATH, self.__access_path) if parent_uid: #headers['herald-reply-to'] = parent_uid message.add_header(herald.MESSAGE_HEADER_REPLIES_TO, parent_uid) # update target peer header if target_peer is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_PEER, target_peer.uid) # update target peer header if target_group is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_GROUP, target_group) if message.subject in herald.SUBJECTS_RAW: content = utils.to_str(message.content) else: # Convert content to JSON content = utils.to_json(message) return headers, content
def __prepare_message(self, message, parent_uid=None, target_peer=None, target_group=None): """ Prepares a HTTP request. :param message: The Message bean to send :param parent_uid: UID of the message this one replies to (optional) :return: A (headers, content) tuple """ # Prepare headers """ headers = {'content-type': CONTENT_TYPE_JSON, 'herald-subject': message.subject, 'herald-uid': message.uid, 'herald-sender-uid': self.__peer_uid, 'herald-timestamp': int(time.time() * 1000), 'herald-port': self.__access_port, 'herald-path': self.__access_path} """ headers = {'content-type': CONTENT_TYPE_JSON} message.add_header(herald.MESSAGE_HEADER_SENDER_UID, self.__peer_uid) message.add_header( herald.transports.http.MESSAGE_HEADER_PORT, self.__access_port) message.add_header( herald.transports.http.MESSAGE_HEADER_PATH, self.__access_path) if parent_uid: #headers['herald-reply-to'] = parent_uid message.add_header(herald.MESSAGE_HEADER_REPLIES_TO, parent_uid) # update target peer header if target_peer is not None: message.add_header( herald.MESSAGE_HEADER_TARGET_PEER, target_peer.uid) # update target peer header if target_group is not None: message.add_header( herald.MESSAGE_HEADER_TARGET_GROUP, target_group) if message.subject in herald.SUBJECTS_RAW: content = utils.to_str(message.content) else: # Convert content to JSON content = utils.to_json(message) return headers, content
def __send_message(self, msgtype, target, message, parent_uid=None, target_peer=None, target_group=None): """ Prepares and sends a message over XMPP :param msgtype: Kind of message (chat or groupchat) :param target: Target JID or MUC room :param message: Herald message bean :param parent_uid: UID of the message this one replies to (optional) """ # Convert content to JSON if message.subject in herald.SUBJECTS_RAW: content = to_str(message.content) else: # update headers local_peer = self._directory.get_local_peer() message.add_header(herald.MESSAGE_HEADER_SENDER_UID, local_peer.uid) if target_peer is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_PEER, target_peer.uid) if target_group is not None: message.add_header(herald.MESSAGE_HEADER_TARGET_GROUP, target_group) content = utils.to_json(message) # Prepare an XMPP message, based on the Herald message xmpp_msg = self._bot.make_message(mto=target, mbody=content, msubject=message.subject, mtype=msgtype) xmpp_msg['thread'] = message.uid if parent_uid: xmpp_msg['parent_thread'] = parent_uid # Store message content self._probe.store( herald.PROBE_CHANNEL_MSG_CONTENT, {"uid": message.uid, "content": content} ) # Send it, using the 1-thread pool, and wait for its execution future = self.__pool.enqueue(xmpp_msg.send) return future.result()