/**
   * 修改会议室 当前该函数只能修改会议室名称和成员
   *
   * @param roomName
   * @param memberList
   */
  public static boolean modifyRoom(
      ImConnection talkConnection, String roomName, String newRoomName, List<String> memberList) {
    try {
      MultiUserChat muc = getMultiUserChat(talkConnection, roomName);
      if (muc != null) {
        muc.join(talkConnection.getXMPPConnection().getUser());
        // 获得聊天室的配置表单
        Form form = muc.getConfigurationForm();
        // 根据原始表单创建一个要提交的新表单。
        Form submitForm = form.createAnswerForm();
        submitForm.setAnswer("muc#roomconfig_roomname", newRoomName); // 设置房间的新名称
        // form.setAnswer();
        // 先删除所以的群成员
        List<Affiliate> delMembers = (List<Affiliate>) muc.getMembers();
        if (delMembers != null && delMembers.size() > 0) {
          for (Affiliate affiliate : delMembers) {
            muc.revokeMembership(affiliate.getJid());
          }
        }

        if (memberList != null && memberList.size() > 0) {
          for (String member : memberList) {
            muc.grantMembership(member);
          }
        }

        muc.sendConfigurationForm(submitForm);
      }
    } catch (XMPPException e) {
      e.printStackTrace();
    }
    return true;
  }
 public static MultiUserChat createConferenceRoom(
     ImConnection talkConnection, String roomName, List<String> memberList) throws XMPPException {
   try {
     MultiUserChat muc = getMultiUserChat(talkConnection, roomName);
     if (muc != null) {
       // 创建聊天室
       muc.join(talkConnection.getXMPPConnection().getUser());
       //                muc.create(roomName); // roomName房间的名字
       // 获得聊天室的配置表单
       Form form = muc.getConfigurationForm();
       // 根据原始表单创建一个要提交的新表单。
       Form submitForm = form.createAnswerForm();
       // 向要提交的表单添加默认答复
       for (Iterator<FormField> fields = form.getFields(); fields.hasNext(); ) {
         FormField field = (FormField) fields.next();
         if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
           // 设置默认值作为答复
           submitForm.setDefaultAnswer(field.getVariable());
         }
       }
       // 设置聊天室的新拥有者
       List<String> owners = new ArrayList<String>();
       owners.add(talkConnection.getXMPPConnection().getUser()); // 用户JID
       submitForm.setAnswer("muc#roomconfig_roomowners", owners);
       // 设置聊天室是持久聊天室,即将要被保存下来
       submitForm.setAnswer("muc#roomconfig_persistentroom", true);
       // 房间仅对成员开放
       submitForm.setAnswer("muc#roomconfig_membersonly", false);
       // 允许占有者邀请其他人
       submitForm.setAnswer("muc#roomconfig_allowinvites", true);
       // 登录房间对话
       submitForm.setAnswer("muc#roomconfig_enablelogging", true);
       // 仅允许注册的昵称登录
       submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
       // 允许使用者修改昵称
       submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
       // 允许用户注册房间
       submitForm.setAnswer("x-muc#roomconfig_registration", false);
       // 发送已完成的表单(有默认值)到服务器来配置聊天室
       submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
       // 添加群成员
       for (String tempMember : memberList) {
         muc.grantMembership(tempMember);
       }
       // 发送已完成的表单(有默认值)到服务器来配置聊天室
       muc.sendConfigurationForm(submitForm);
     }
     return muc;
   } catch (XMPPException e) {
     e.printStackTrace();
     throw new XMPPException("");
   }
 }
示例#3
0
  public static void createMucChannel(
      String channelname, String channeltopic, int userid, int channelid) {
    try {
      if (!Application.mucchannels.containsKey(channelid)) {
        // create MUC
        MultiUserChat muc =
            new MultiUserChat(Application.conn, channelname + "@conference.webchat");
        System.out.println("Channelname: " + channelname);
        muc.create(channelname);

        Application.mucchannels.put(channelid, muc);

        Form form = muc.getConfigurationForm();
        Form submitForm = form.createAnswerForm();

        for (Iterator<FormField> fields = form.getFields(); fields.hasNext(); ) {
          FormField field = (FormField) fields.next();
          if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
            submitForm.setDefaultAnswer(field.getVariable());
          }
        }

        List<String> owners = new ArrayList<String>();
        owners.add("webchat@webchat");
        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        muc.sendConfigurationForm(submitForm);
        muc.invite(
            models.User.find.byId(userid).username + "@webchat/Smack",
            "Einladung in Channel " + channelname);
        final int chanid = channelid;
        muc.sendMessage("Willkommen: Topic lautet " + channeltopic + "!");
        muc.addMessageListener(
            new PacketListener() {
              @Override
              public void processPacket(Packet packet) {
                if (packet instanceof Message) {
                  String[] temp;
                  temp = ((Message) packet).getFrom().split("/");
                  if (!temp[1].equals(models.Channel.find.byId(chanid).name)) {
                    System.out.println("Received user: "******"@webchat/Smack",
            "Einladung in Channel " + channelname);
        muc.sendMessage("Willkommen: Topic lautet" + channeltopic + "!");
      }
    } catch (XMPPException exp) {
      exp.printStackTrace();
    }
  }
示例#4
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;
  }