Ejemplo n.º 1
0
  public void sendMessage(String text) {
    final Message message = new Message();

    if (threadID == null) {
      threadID = StringUtils.randomString(6);
    }
    message.setThread(threadID);

    // Set the body of the message using typedMessage
    message.setBody(text);

    // IF there is no body, just return and do nothing
    if (!ModelUtil.hasLength(text)) {
      return;
    }

    // Fire Message Filters
    SparkManager.getChatManager().filterOutgoingMessage(this, message);

    // Fire Global Filters
    SparkManager.getChatManager().fireGlobalMessageSentListeners(this, message);

    sendMessage(message);

    sendNotification = true;
  }
Ejemplo n.º 2
0
 /**
  * Sends a message to the other chat participant. The thread ID, recipient, and message type of
  * the message will automatically set to those of this chat.
  *
  * @param message the message to send.
  * @throws XMPPException if an error occurs sending the message.
  */
 public void sendMessage(Message message) throws XMPPException {
   // Force the recipient, message type, and thread ID since the user elected
   // to send the message through this chat object.
   message.setTo(participant);
   message.setType(Message.Type.chat);
   message.setThread(threadID);
   chatManager.sendMessage(this, message);
 }
Ejemplo n.º 3
0
 private void sendFeature(String account, String user, String session, Feature feature) {
   Message message = new Message(user, Message.Type.normal);
   message.setThread(session);
   message.addExtension(feature);
   try {
     ConnectionManager.getInstance().sendStanza(account, message);
   } catch (NetworkException e) {
   }
 }
Ejemplo n.º 4
0
  /**
   * Sends a reply to a previously received packet that was sent to multiple recipients. Before
   * attempting to send the reply message some checkings are performed. If any of those checkings
   * fail then an XMPPException is going to be thrown with the specific error detail.
   *
   * @param connection the connection to use to send the reply.
   * @param original the previously received packet that was sent to multiple recipients.
   * @param reply the new message to send as a reply.
   * @throws XMPPException if the original message was not sent to multiple recipients, or the
   *     original message cannot be replied or reply should be sent to a room.
   */
  public static void reply(Connection connection, Message original, Message reply)
      throws XMPPException {
    MultipleRecipientInfo info = getMultipleRecipientInfo(original);
    if (info == null) {
      throw new XMPPException("Original message does not contain multiple recipient info");
    }
    if (info.shouldNotReply()) {
      throw new XMPPException("Original message should not be replied");
    }
    if (info.getReplyRoom() != null) {
      throw new XMPPException("Reply should be sent through a room");
    }
    // Any <thread/> element from the initial message MUST be copied into the reply.
    if (original.getThread() != null) {
      reply.setThread(original.getThread());
    }
    MultipleAddresses.Address replyAddress = info.getReplyAddress();
    if (replyAddress != null && replyAddress.getJid() != null) {
      // Send reply to the reply_to address
      reply.setTo(replyAddress.getJid());
      connection.sendPacket(reply);
    } else {
      // Send reply to multiple recipients
      List<String> to = new ArrayList<String>();
      List<String> cc = new ArrayList<String>();
      for (Iterator<MultipleAddresses.Address> it = info.getTOAddresses().iterator();
          it.hasNext(); ) {
        String jid = it.next().getJid();
        to.add(jid);
      }
      for (Iterator<MultipleAddresses.Address> it = info.getCCAddresses().iterator();
          it.hasNext(); ) {
        String jid = it.next().getJid();
        cc.add(jid);
      }
      // Add original sender as a 'to' address (if not already present)
      if (!to.contains(original.getFrom()) && !cc.contains(original.getFrom())) {
        to.add(original.getFrom());
      }
      // Remove the sender from the TO/CC list (try with bare JID too)
      String from = connection.getUser();
      if (!to.remove(from) && !cc.remove(from)) {
        String bareJID = StringUtils.parseBareAddress(from);
        to.remove(bareJID);
        cc.remove(bareJID);
      }

      String serviceAddress = getMultipleRecipienServiceAddress(connection);
      if (serviceAddress != null) {
        // Send packet to target users using multiple recipient service provided by the server
        sendThroughService(connection, reply, to, cc, null, null, null, false, serviceAddress);
      } else {
        // Server does not support JEP-33 so try to send the packet to each recipient
        sendToIndividualRecipients(connection, reply, to, cc, null);
      }
    }
  }
Ejemplo n.º 5
0
  /**
   * Delivers a message directly to this chat, which will add the message to the collector and
   * deliver it to all listeners registered with the Chat. This is used by the XMPPConnection class
   * to deliver messages without a thread ID.
   *
   * @param message the message.
   */
  void deliver(Message message) {
    // Because the collector and listeners are expecting a thread ID with
    // a specific value, set the thread ID on the message even though it
    // probably never had one.
    message.setThread(threadID);

    for (MessageListener listener : listeners) {
      listener.processMessage(this, message);
    }
  }
 private void processAndSendResponse(Element element, Message message) {
   Element response = new Element(RESPONSE, Transport.NAMESPACE);
   XmlResponseProvider provider = XmlResponseProvider.getProvider(element, getBroadcaster());
   if (provider.processAndFillResponse(
       response, element, JabberTransport.this, getFrom(message))) {
     Message responseMessage = new Message(getFrom(message));
     responseMessage.addExtension(new JDOMExtension(response));
     responseMessage.setThread(message.getThread());
     myFacade.getConnection().sendPacket(responseMessage);
   }
 }
  private Message doSendMessage(XmlMessage xmlMessage, User user, String threadId) {
    Element element = new Element(xmlMessage.getTagName(), xmlMessage.getTagNamespace());
    xmlMessage.fillRequest(element);

    Message message = createBaseMessage(user, element.getText());
    message.setThread(threadId);
    message.addExtension(new JDOMExtension(element));
    myFacade.getConnection().sendPacket(message);

    return message;
  }
Ejemplo n.º 8
0
 /**
  * Sends the specified text as a message to the other chat participant. This is a convenience
  * method for:
  *
  * <pre>
  *     Message message = chat.createMessage();
  *     message.setBody(messageText);
  *     chat.sendMessage(message);
  * </pre>
  *
  * @param text the text to send.
  * @throws XMPPException if sending the message fails.
  */
 public void sendMessage(String text) throws XMPPException {
   Message message = new Message(participant, Message.Type.chat);
   message.setThread(threadID);
   message.setBody(text);
   chatManager.sendMessage(this, message);
 }
  /**
   * Helper function used to send a message to a contact, with the given extensions attached.
   *
   * @param to The contact to send the message to.
   * @param toResource The resource to send the message to or null if no resource has been specified
   * @param message The message to send.
   * @param extensions The XMPP extensions that should be attached to the message before sending.
   * @return The MessageDeliveryEvent that resulted after attempting to send this message, so the
   *     calling function can modify it if needed.
   */
  private MessageDeliveredEvent sendMessage(
      Contact to, ContactResource toResource, Message message, PacketExtension[] extensions) {
    if (!(to instanceof ContactJabberImpl))
      throw new IllegalArgumentException("The specified contact is not a Jabber contact." + to);

    assertConnected();

    org.jivesoftware.smack.packet.Message msg = new org.jivesoftware.smack.packet.Message();

    String toJID = null;

    if (toResource != null) {
      if (toResource.equals(ContactResource.BASE_RESOURCE)) {
        toJID = to.getAddress();
      } else toJID = ((ContactResourceJabberImpl) toResource).getFullJid();
    }

    if (toJID == null) {
      toJID = to.getAddress();
    }

    msg.setPacketID(message.getMessageUID());
    msg.setTo(toJID);

    for (PacketExtension ext : extensions) {
      msg.addExtension(ext);
    }

    if (logger.isTraceEnabled())
      logger.trace("Will send a message to:" + toJID + " chat.jid=" + toJID);

    MessageDeliveredEvent msgDeliveryPendingEvt =
        new MessageDeliveredEvent(message, to, toResource);

    MessageDeliveredEvent[] transformedEvents =
        messageDeliveryPendingTransform(msgDeliveryPendingEvt);

    if (transformedEvents == null || transformedEvents.length == 0) return null;

    for (MessageDeliveredEvent event : transformedEvents) {
      String content = event.getSourceMessage().getContent();

      if (message.getContentType().equals(HTML_MIME_TYPE)) {
        msg.setBody(Html2Text.extractText(content));

        // Check if the other user supports XHTML messages
        // make sure we use our discovery manager as it caches calls
        if (jabberProvider.isFeatureListSupported(toJID, HTML_NAMESPACE)) {
          // Add the XHTML text to the message
          XHTMLManager.addBody(msg, OPEN_BODY_TAG + content + CLOSE_BODY_TAG);
        }
      } else {
        // this is plain text so keep it as it is.
        msg.setBody(content);
      }

      // msg.addExtension(new Version());

      if (event.isMessageEncrypted() && isCarbonEnabled) {
        msg.addExtension(new CarbonPacketExtension.PrivateExtension());
      }

      MessageEventManager.addNotificationsRequests(msg, true, false, false, true);

      String threadID = getThreadIDForAddress(toJID);
      if (threadID == null) threadID = nextThreadID();

      msg.setThread(threadID);
      msg.setType(org.jivesoftware.smack.packet.Message.Type.chat);
      msg.setFrom(jabberProvider.getConnection().getUser());

      jabberProvider.getConnection().sendPacket(msg);

      putJidForAddress(toJID, threadID);
    }

    return new MessageDeliveredEvent(message, to, toResource);
  }
Ejemplo n.º 10
0
  /**
   * Parses a message packet.
   *
   * @param parser the XML parser, positioned at the start of a message packet.
   * @return a Message packet.
   * @throws Exception if an exception occurs while parsing the packet.
   */
  public static Packet parseMessage(XmlPullParser parser) throws Exception {
    Message message = new Message();
    String id = parser.getAttributeValue("", "id");
    message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
    message.setTo(parser.getAttributeValue("", "to"));
    message.setFrom(parser.getAttributeValue("", "from"));
    message.setType(Message.Type.fromString(parser.getAttributeValue("", "type")));
    String language = getLanguageAttribute(parser);

    // determine message's default language
    String defaultLanguage = null;
    if (language != null && !"".equals(language.trim())) {
      message.setLanguage(language);
      defaultLanguage = language;
    } else {
      defaultLanguage = Packet.getDefaultLanguage();
    }

    // Parse sub-elements. We include extra logic to make sure the values
    // are only read once. This is because it's possible for the names to appear
    // in arbitrary sub-elements.
    boolean done = false;
    String thread = null;
    Map<String, Object> properties = null;
    while (!done) {
      int eventType = parser.next();
      if (eventType == XmlPullParser.START_TAG) {
        String elementName = parser.getName();
        String namespace = parser.getNamespace();
        if (elementName.equals("subject")) {
          String xmlLang = getLanguageAttribute(parser);
          if (xmlLang == null) {
            xmlLang = defaultLanguage;
          }

          String subject = parseContent(parser);

          if (message.getSubject(xmlLang) == null) {
            message.addSubject(xmlLang, subject);
          }
        } else if (elementName.equals("body")) {
          String xmlLang = getLanguageAttribute(parser);
          if (xmlLang == null) {
            xmlLang = defaultLanguage;
          }

          String body = parseContent(parser);

          if (message.getBody(xmlLang) == null) {
            message.addBody(xmlLang, body);
          }
        } else if (elementName.equals("thread")) {
          if (thread == null) {
            thread = parser.nextText();
          }
        } else if (elementName.equals("error")) {
          message.setError(parseError(parser));
        } else if (elementName.equals("properties") && namespace.equals(PROPERTIES_NAMESPACE)) {
          properties = parseProperties(parser);
        }
        // Otherwise, it must be a packet extension.
        else {
          message.addExtension(
              PacketParserUtils.parsePacketExtension(elementName, namespace, parser));
        }
      } else if (eventType == XmlPullParser.END_TAG) {
        if (parser.getName().equals("message")) {
          done = true;
        }
      }
    }

    message.setThread(thread);
    // Set packet properties.
    if (properties != null) {
      for (String name : properties.keySet()) {
        message.setProperty(name, properties.get(name));
      }
    }
    return message;
  }
  boolean sendMessage(OutMessage message, boolean sendChatState) {
    // check for correct receipt status and reset it
    KonMessage.Status status = message.getStatus();
    assert status == KonMessage.Status.PENDING || status == KonMessage.Status.ERROR;
    message.setStatus(KonMessage.Status.PENDING);

    if (!mClient.isConnected()) {
      LOGGER.info("not sending message(s), not connected");
      return false;
    }

    MessageContent content = message.getContent();
    MessageContent.Attachment att = content.getAttachment().orElse(null);
    if (att != null && !att.hasURL()) {
      LOGGER.warning("attachment not uploaded");
      message.setStatus(KonMessage.Status.ERROR);
      return false;
    }

    boolean encrypted =
        message.getCoderStatus().getEncryption() != Coder.Encryption.NOT
            || message.getCoderStatus().getSigning() != Coder.Signing.NOT;

    Chat chat = message.getChat();

    Message protoMessage = encrypted ? new Message() : rawMessage(content, chat, false);

    protoMessage.setType(Message.Type.chat);
    protoMessage.setStanzaId(message.getXMPPID());
    String threadID = chat.getXMPPID();
    if (!threadID.isEmpty()) protoMessage.setThread(threadID);

    // extensions

    // TODO with group chat? (for muc "NOT RECOMMENDED")
    if (!chat.isGroupChat()) protoMessage.addExtension(new DeliveryReceiptRequest());

    if (sendChatState) protoMessage.addExtension(new ChatStateExtension(ChatState.active));

    if (encrypted) {
      byte[] encryptedData =
          content.isComplex() || chat.isGroupChat()
              ? Coder.encryptStanza(message, rawMessage(content, chat, true).toXML().toString())
                  .orElse(null)
              : Coder.encryptMessage(message).orElse(null);
      // check also for security errors just to be sure
      if (encryptedData == null || !message.getCoderStatus().getErrors().isEmpty()) {
        LOGGER.warning("encryption failed");
        message.setStatus(KonMessage.Status.ERROR);
        mControl.handleSecurityErrors(message);
        return false;
      }
      protoMessage.addExtension(new E2EEncryption(encryptedData));
    }

    // transmission specific
    Transmission[] transmissions = message.getTransmissions();
    ArrayList<Message> sendMessages = new ArrayList<>(transmissions.length);
    for (Transmission transmission : message.getTransmissions()) {
      Message sendMessage = protoMessage.clone();
      JID to = transmission.getJID();
      if (!to.isValid()) {
        LOGGER.warning("invalid JID: " + to);
        return false;
      }
      sendMessage.setTo(to.string());
      sendMessages.add(sendMessage);
    }

    return mClient.sendPackets(sendMessages.toArray(new Message[0]));
  }