コード例 #1
0
  @Override
  public void processPacket(Stanza packet) {
    LOGGER.info("got public key: " + StringUtils.abbreviate(packet.toXML().toString(), 300));

    PublicKeyPublish publicKeyPacket = (PublicKeyPublish) packet;

    if (publicKeyPacket.getType() == IQ.Type.set) {
      LOGGER.warning("ignoring public key packet with type 'set'");
      return;
    }

    if (publicKeyPacket.getType() == IQ.Type.result) {
      byte[] keyData = publicKeyPacket.getPublicKey();
      if (keyData == null) {
        LOGGER.warning("got public key packet without public key");
        return;
      }
      mControl.setPGPKey(publicKeyPacket.getFrom(), keyData);
    }
  }
コード例 #2
0
  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]));
  }