isValidGroupJid static method

bool isValidGroupJid(
  1. String groupJid
)

Validates a group JID (Jabber ID) for a group.

This method checks if the provided groupJid is a valid group JID. A valid group JID must:

  • Contain the substring "@mix".
  • Contain exactly one '@' character.
  • Have a non-empty local part (the part before the '@').
  • Have a non-empty domain part (the part after the '@') that contains "mix".

Parameters:

  • groupJid: The group JID to be validated. This parameter is required.

Returns:

  • true if the groupJid is valid.
  • false if the groupJid is invalid.

The method logs debug messages using LogMessage.d to indicate the reason for invalidity.

Example usage:

bool isValid = isValidGroupJid('group@mix.example.com');
if (isValid) {
  print('The group JID is valid.');
} else {
  print('The group JID is invalid.');
}

Implementation

static bool isValidGroupJid(String groupJid) {
  // Check if the JID contains "@mix" and follows basic JID validation
  if (groupJid.isNotEmpty && groupJid.contains("@mix")) {
    int atIndex = groupJid.indexOf('@');
    if (atIndex == -1) {
      LogMessage.d("isValidGroupJid",
          "Invalid Group JID: '$groupJid' does not contain a '@' character.");
      return false;
    } else if (groupJid.indexOf('@', atIndex + 1) != -1) {
      LogMessage.d("isValidGroupJid",
          "Invalid Group JID: '$groupJid' contains multiple '@' characters.");
      return false;
    }

    String localPart = groupJid.split('@')[0];
    if (localPart.isEmpty) {
      LogMessage.d("isValidGroupJid",
          "Invalid Group JID: '$groupJid' has an empty localPart.");
      return false;
    }

    String domainPart = groupJid.split('@')[1];
    if (domainPart.isEmpty || !domainPart.contains("mix")) {
      LogMessage.d("isValidGroupJid",
          "Invalid Group JID: '$groupJid' has an invalid domain part (does not contain 'mix').");
      return false;
    }

    return true;
  }

  LogMessage.d("isValidGroupJid",
      "Invalid Group JID: '$groupJid' is empty or does not contain '@mix'.");
  return false;
}