예제 #1
0
  void sendOrErrorAuth(boolean err, OTRCallbacks callback) {

    if (!err) {
      String msg = this.auth.lastauthmsg;
      if (msg != null) {
        this.fragmentAndSend(msg, Policy.FRAGMENT_SEND_ALL, callback);
        this.lastSent = System.currentTimeMillis();
      }
    } else {
      callback.handleMsgEvent(OTRCallbacks.OTRL_MSGEVENT_SETUP_ERROR, this, null);
    }
  }
예제 #2
0
  /**
   * Handle a message about to be sent to the network. It is safe to pass all messages about to be
   * sent to this routine.
   *
   * <p>tlvs is an array of OTRTLVs to append to the private message. It is usually correct to just
   * pass null here.
   */
  public String messageSending(String message, OTRTLV[] tlvs, int fragPolicy, OTRCallbacks callback)
      throws OTRException {

    int policy = Policy.DEFAULT;

    if (message == null) throw new OTRException("MessageSending: Null argument");

    // Check the policy
    policy = callback.getOtrPolicy(this);

    // Should we go on at all?
    if ((policy & Policy.VERSION_MASK) == 0) {
      throw new OTRException("Invalid protocol");
    }

    // If this is an OTR Query message, don't encrypt it.
    OTRMessage otrmessage = OTRMessage.parse(message);
    if (otrmessage.getType() == OTRMessage.MSG_QUERY) {
      // Replace the "?OTR?" with a custom message
      QueryMessage bettermsg = defaultQueryMessage(accountName, policy);
      String ret = new String(bettermsg.getContent());
      if (fragPolicy == Policy.FRAGMENT_SEND_SKIP) return ret;
      return this.fragmentAndSend(ret, fragPolicy, callback);
    }

    switch (msgState.getCurState()) {
      case MsgState.ST_UNENCRYPTED:
        if ((policy & Policy.REQUIRE_ENCRYPTION) != 0) {
          /*
           * We're trying to send an unencrypted message with a policy
           * that disallows that. Don't do that, but try to start up OTR
           * instead.
           */
          callback.handleMsgEvent(OTRCallbacks.OTRL_MSGEVENT_ENCRYPTION_REQUIRED, this, null);
          QueryMessage bettermsg = defaultQueryMessage(accountName, policy);
          lastMessage = new String(bettermsg.getContent());
          lastSent = System.currentTimeMillis() % 1000;
          mayRetransmit = 2;
          String ret = new String(bettermsg.getContent());
          if (fragPolicy == Policy.FRAGMENT_SEND_SKIP) return ret;
          return this.fragmentAndSend(ret, fragPolicy, callback);
        } else {
          if ((policy & Policy.SEND_WHITESPACE_TAG) != 0 && otr_offer != OFFER_REJECTED) {
            /*
             * See if this user can speak OTR. Append the
             * OTR_MESSAGE_TAG to the plaintext message, and see if he
             * responds.
             */
            String taggedmsg = message + OTRL_MESSAGE_TAG_BASE + OTRL_MESSAGE_TAG_V2;
            otr_offer = OFFER_SENT;
            if (fragPolicy == Policy.FRAGMENT_SEND_SKIP) return taggedmsg;
            return this.fragmentAndSend(taggedmsg, fragPolicy, callback);
          }
        }
        break;
      case MsgState.ST_ENCRYPTED:
        // Create the new, encrypted message
        try {
          DataMessage dm = Proto.createData(this, message.getBytes(), (byte) 0, tlvs);
          lastMessage = new String(dm.getContent());
          lastSent = System.currentTimeMillis() % 1000;
          String ret = new String(dm.getContent());
          if (fragPolicy == Policy.FRAGMENT_SEND_SKIP) return ret;
          return this.fragmentAndSend(ret, fragPolicy, callback);
        } catch (OTRException e) {
          /* Uh, oh.  Whatever we do, *don't* send the message in the
           * clear. */
          callback.handleMsgEvent(OTRCallbacks.OTRL_ERRCODE_ENCRYPTION_ERROR, this, null);
          callback.errorMessage(this, OTRCallbacks.OTRL_ERRCODE_ENCRYPTION_ERROR);
          return null;
        }

      case MsgState.ST_FINISHED:
        callback.handleMsgEvent(OTRCallbacks.OTRL_MSGEVENT_CONNECTION_ENDED, this, null);
        String ret = new String(new ErrorMessage("").getContent());
        if (fragPolicy == Policy.FRAGMENT_SEND_SKIP) return ret;
        return this.fragmentAndSend(ret, fragPolicy, callback);
    }
    return null;
  }