예제 #1
0
  /**
   * Joins the workgroup queue to wait to be routed to an agent. After joining the queue, queue
   * status events will be sent to indicate the user's position and estimated time left in the
   * queue. Once joining the queue, there are three ways the user can leave the queue:
   *
   * <ul>
   *   <p>
   *   <li>The user is routed to an agent, which triggers a GroupChat invitation.
   *   <li>The user asks to leave the queue by calling the {@link #departQueue} method.
   *   <li>A server error occurs, or an administrator explicitly removes the user from the queue.
   * </ul>
   *
   * <p>A user cannot request to join the queue again if already in the queue. Therefore, this
   * method will throw an IllegalStateException if the user is already in the queue.
   *
   * <p>
   *
   * <p>Some servers may be configured to require certain meta-data in order to join the queue.
   *
   * <p>
   *
   * <p>The server tracks the conversations that a user has with agents over time. By default, that
   * tracking is done using the user's JID. However, this is not always possible. For example, when
   * the user is logged in anonymously using a web client. In that case the user ID might be a
   * randomly generated value put into a persistent cookie or a username obtained via the session.
   * When specified, that userID will be used instead of the user's JID to track conversations. The
   * server will ignore a manually specified userID if the user's connection to the server is not
   * anonymous.
   *
   * @param metadata metadata to create a dataform from.
   * @param userID String that represents the ID of the user when using anonymous sessions or
   *     <tt>null</tt> if a userID should not be used.
   * @throws XMPPException if an error occured joining the queue. An error may indicate that a
   *     connection failure occured or that the server explicitly rejected the request to join the
   *     queue.
   */
  public void joinQueue(Map<String, Object> metadata, String userID) throws XMPPException {
    // If already in the queue ignore the join request.
    if (inQueue) {
      throw new IllegalStateException("Already in queue " + workgroupJID);
    }

    // Build dataform from metadata
    Form form = new Form(Form.TYPE_SUBMIT);
    Iterator<String> iter = metadata.keySet().iterator();
    while (iter.hasNext()) {
      String name = iter.next();
      String value = metadata.get(name).toString();

      String escapedName = StringUtils.escapeForXML(name);
      String escapedValue = StringUtils.escapeForXML(value);

      FormField field = new FormField(escapedName);
      field.setType(FormField.TYPE_TEXT_SINGLE);
      form.addField(field);
      form.setAnswer(escapedName, escapedValue);
    }
    joinQueue(form, userID);
  }
예제 #2
0
  /**
   * Creates a new MUC AND invites the user room name will be extended with an random number for
   * security purposes
   *
   * @param number
   * @param name - the name of the contact to chat via SMS with
   * @return
   * @throws XMPPException
   */
  private MultiUserChat createRoom(String number, String name, int mode) throws Exception {
    MultiUserChat multiUserChat;
    Integer randomInt;

    // With "@conference.jabber.org" messages are sent several times...
    // Jwchat seems to work fine and is the default
    final String roomJID;
    final String subjectInviteStr;

    do {
      randomInt = mRndGen.nextInt();
    } while (mRoomNumbers.contains(randomInt));

    String normalizedName = name.replaceAll(" ", "_").replaceAll("[\\W]|�", "");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
      normalizedName =
          Normalizer.normalize(normalizedName, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
    }
    String cleanLogin = mSettings.getLogin().replaceAll("@", "_");
    String roomUID = normalizedName + "_" + ROOM_START_TAG + randomInt + "_" + cleanLogin;

    switch (mode) {
      case MODE_SMS:
        roomJID = roomUID + "_SMS_" + "@" + getMUCServer();
        subjectInviteStr = mCtx.getString(R.string.xmpp_muc_sms) + name;
        break;

      case MODE_SHELL:
        roomJID = roomUID + "_Shell_" + number + "@" + getMUCServer();
        subjectInviteStr = mCtx.getString(R.string.xmpp_muc_shell) + name + " " + number;
        name = "Shell " + number;
        break;

      default:
        roomJID = null;
        subjectInviteStr = null;
        break;
    }
    Log.i("Creating room " + roomJID + " " + getRoomInt(roomJID));

    // See issue 136
    try {
      multiUserChat = new MultiUserChat(mConnection, roomJID);
    } catch (Exception e) {
      Log.e("MUC creation failed: ", e);
      throw new Exception("MUC creation failed for " + roomJID + ": " + e.getLocalizedMessage(), e);
    }

    try {
      multiUserChat.createOrJoin(name);
    } catch (Exception e) {
      Log.e("MUC creation failed: ", e);
      throw new Exception("MUC creation failed for " + name + ": " + e.getLocalizedMessage(), e);
    }

    try {
      // Since this is a private room, make the room not public and set user as owner of the room.
      Form submitForm = multiUserChat.getConfigurationForm().createAnswerForm();
      submitForm.setAnswer("muc#roomconfig_publicroom", false);
      submitForm.setAnswer("muc#roomconfig_roomname", name);
      try {
        submitForm.setAnswer("muc#roomconfig_roomdesc", name);
      } catch (Exception ex) {
        Log.w("Unable to configure room description to " + name, ex);
      }

      try {
        submitForm.setAnswer("muc#roomconfig_whois", "anyone");
      } catch (Exception ex) {
        Log.w("Unable to configure setting whois");
      }

      try {
        List<String> owners = new ArrayList<String>();
        if (mConnection.getUser() != null) {
          owners.add(mConnection.getUser());
        } else {
          owners.add(mSettings.getLogin());
        }
        Collections.addAll(owners, mSettings.getNotifiedAddresses().getAll());
        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        submitForm.setAnswer("muc#roomconfig_membersonly", true);
      } catch (Exception ex) {
        // Password protected MUC fallback code begins here
        Log.w(
            "Unable to configure room owners on Server "
                + getMUCServer()
                + ". Falling back to room passwords",
            ex);
        // See http://xmpp.org/registrar/formtypes.html#http:--jabber.org-protocol-mucroomconfig
        try {
          if (submitForm.getField("muc#roomconfig_passwordprotectedroom") != null) {
            submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
          }
          submitForm.setAnswer("muc#roomconfig_roomsecret", mSettings.roomPassword);
        } catch (IllegalArgumentException iae) {
          // If a server doesn't provide even password protected MUC, the setAnswer
          // call will result in an IllegalArgumentException, which we wrap into an XMPPException
          // See also Issue 247 http://code.google.com/p/gtalksms/issues/detail?id=247
          throw iae;
        }
      }

      Log.d(submitForm.getDataFormToSend().toXML().toString());
      multiUserChat.sendConfigurationForm(submitForm);
      multiUserChat.changeSubject(subjectInviteStr);
    } catch (XMPPException e1) {
      Log.w("Unable to send conference room configuration form.", e1);
      send(mCtx.getString(R.string.chat_sms_muc_conf_error, e1.getMessage()));
      // then we also should not send an invite as the room will be locked
      throw e1;
    }

    for (String notifiedAddress : mSettings.getNotifiedAddresses().getAll()) {
      multiUserChat.invite(notifiedAddress, subjectInviteStr);
    }

    registerRoom(multiUserChat, number, name, randomInt, mode);
    return multiUserChat;
  }