Fixed URI completion bugs and improved URI related checks
This commit is contained in:
@@ -39,7 +39,7 @@ class AccountsActivity : AppCompatActivity() {
|
|||||||
val newAorView = binding.newAor
|
val newAorView = binding.newAor
|
||||||
addAccountButton.setOnClickListener {
|
addAccountButton.setOnClickListener {
|
||||||
val aor = newAorView.text.toString().trim()
|
val aor = newAorView.text.toString().trim()
|
||||||
if (!Utils.checkAor(aor)) {
|
if (!Utils.checkAor("sip:$aor")) {
|
||||||
Log.d("Baresip", "Invalid Address of Record $aor")
|
Log.d("Baresip", "Invalid Address of Record $aor")
|
||||||
Utils.alertView(this, getString(R.string.notice),
|
Utils.alertView(this, getString(R.string.notice),
|
||||||
String.format(getString(R.string.invalid_aor), aor))
|
String.format(getString(R.string.invalid_aor), aor))
|
||||||
|
|||||||
@@ -263,14 +263,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
if (Call.calls().isEmpty()) {
|
if (Call.calls().isEmpty()) {
|
||||||
val uriText = callUri.text.toString().trim()
|
val uriText = callUri.text.toString().trim()
|
||||||
if (uriText.isNotEmpty()) {
|
if (uriText.isNotEmpty()) {
|
||||||
var uri = ContactsActivity.findContactURI(uriText)
|
val uri = Utils.uriComplete(ContactsActivity.findContactURI(uriText),
|
||||||
if (!uri.startsWith("sip:")) {
|
Utils.aorDomain(aor))
|
||||||
uri = "sip:$uri"
|
|
||||||
if (!uri.contains("@")) {
|
|
||||||
val host = aor.substring(aor.indexOf("@") + 1)
|
|
||||||
uri = "$uri@$host"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!Utils.checkSipUri(uri)) {
|
if (!Utils.checkSipUri(uri)) {
|
||||||
Utils.alertView(this, getString(R.string.notice),
|
Utils.alertView(this, getString(R.string.notice),
|
||||||
String.format(getString(R.string.invalid_sip_uri), uri))
|
String.format(getString(R.string.invalid_sip_uri), uri))
|
||||||
@@ -1237,15 +1231,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
val uriText = transferUri.text.toString().trim()
|
val uriText = transferUri.text.toString().trim()
|
||||||
if (uriText.isNotEmpty()) {
|
if (uriText.isNotEmpty()) {
|
||||||
var uri = ContactsActivity.findContactURI(uriText)
|
val uri = Utils.uriComplete(ContactsActivity.findContactURI(uriText),
|
||||||
if (!uri.startsWith("sip:")) {
|
Utils.aorDomain(ua.account.aor))
|
||||||
uri = "sip:$uri"
|
|
||||||
if (!uri.contains("@")) {
|
|
||||||
val aor = ua.account.aor
|
|
||||||
val host = aor.substring(aor.indexOf("@") + 1)
|
|
||||||
uri = "$uri@$host"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!Utils.checkSipUri(uri))
|
if (!Utils.checkSipUri(uri))
|
||||||
Utils.alertView(this@MainActivity, getString(R.string.notice),
|
Utils.alertView(this@MainActivity, getString(R.string.notice),
|
||||||
String.format(getString(R.string.invalid_sip_uri), uri))
|
String.format(getString(R.string.invalid_sip_uri), uri))
|
||||||
|
|||||||
@@ -70,14 +70,32 @@ object Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun uriHostPart(uri: String): String {
|
fun uriHostPart(uri: String): String {
|
||||||
return uri.substringAfter("@")
|
return if (uri.contains("@")) {
|
||||||
|
uri.substringAfter("@")
|
||||||
.substringBefore(":")
|
.substringBefore(":")
|
||||||
.substringBefore(";")
|
.substringBefore(";")
|
||||||
.substringBefore(">")
|
.substringBefore(">")
|
||||||
|
} else {
|
||||||
|
val parts = uri.split(":")
|
||||||
|
when (parts.size) {
|
||||||
|
2 -> parts[1].substringBefore(";")
|
||||||
|
.substringBefore(">")
|
||||||
|
3 -> parts[1]
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun uriUserPart(uri: String): String {
|
fun uriUserPart(uri: String): String {
|
||||||
return uri.substringAfter(":").substringBefore("@")
|
return if (uri.contains("@"))
|
||||||
|
uri.substringAfter(":").substringBefore("@")
|
||||||
|
else
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun uriParams(uri: String): List<String> {
|
||||||
|
val params = uri.split(";")
|
||||||
|
return if (params.size == 1) listOf<String>() else params.subList(1, params.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun friendlyUri(uri: String, domain: String): String {
|
fun friendlyUri(uri: String, domain: String): String {
|
||||||
@@ -96,32 +114,25 @@ object Utils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun aorUser(aor: String): String {
|
fun uriComplete(uri: String, domain: String): String {
|
||||||
val user = aor.substringBefore("@")
|
val res = if (!uri.startsWith("sip:")) "sip:$uri" else uri
|
||||||
return if (user == aor) "" else user
|
return if (checkUriUser(uri)) "$res@$domain" else res
|
||||||
}
|
}
|
||||||
|
|
||||||
fun aorDomain(aor: String): String {
|
fun aorDomain(aor: String): String {
|
||||||
val domain = aor.substringAfter("@")
|
return uriHostPart(aor)
|
||||||
return if (domain == aor) "" else domain
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun plainAor(aor: String): String {
|
fun plainAor(aor: String): String {
|
||||||
return aor.substringAfter(":").substringBefore("@") + "@" +
|
return uriUserPart(aor) + "@" + uriHostPart(aor)
|
||||||
aor.substringAfter("@").substringBefore(";")
|
|
||||||
.substringBefore(":")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkAor(aor: String): Boolean {
|
fun checkAor(aor: String): Boolean {
|
||||||
val p = aor.split(":")
|
if (!checkSipUri(aor)) return false
|
||||||
if (p.size == 2)
|
val params = uriParams(aor)
|
||||||
return checkUriUser(aorUser(p[0])) && checkDomain(aorDomain(p[0])) &&
|
return params.isEmpty() ||
|
||||||
checkPortTransport(p[1])
|
((params.size == 1) &&
|
||||||
val t = aor.split(";transport=")
|
params[0] in arrayOf("transport=udp", "transport=tcp","transport=tls"))
|
||||||
if (t.size == 2)
|
|
||||||
return checkUriUser(aorUser(t[0])) && checkDomain(aorDomain(t[0])) &&
|
|
||||||
t[1] in arrayOf("udp", "tcp", "tls")
|
|
||||||
return checkUriUser(aorUser(aor)) && checkDomain(aorDomain(aor))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkStunUri(uri: String): Boolean {
|
fun checkStunUri(uri: String): Boolean {
|
||||||
@@ -130,14 +141,6 @@ object Utils {
|
|||||||
return checkHostPort(uri.substringAfter(":"))
|
return checkHostPort(uri.substringAfter(":"))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkPortTransport(portTransport: String): Boolean {
|
|
||||||
val pt = portTransport.split(";transport=")
|
|
||||||
if (pt.count() == 1)
|
|
||||||
return checkPort(pt[0])
|
|
||||||
else
|
|
||||||
return checkPort(pt[0]) && pt[1] in arrayOf("udp", "tcp", "tls")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isE164Number(no: String): Boolean {
|
fun isE164Number(no: String): Boolean {
|
||||||
return Regex("^[+][1-9][0-9]{0,14}\$").matches(no)
|
return Regex("^[+][1-9][0-9]{0,14}\$").matches(no)
|
||||||
}
|
}
|
||||||
@@ -189,7 +192,7 @@ object Utils {
|
|||||||
checkPort(domainPort.substringAfterLast(":"))
|
checkPort(domainPort.substringAfterLast(":"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkHostPort(hostPort: String): Boolean {
|
private fun checkHostPort(hostPort: String): Boolean {
|
||||||
return checkIpV4(hostPort) || checkIpv6InBrackets(hostPort) || checkDomain(hostPort) ||
|
return checkIpV4(hostPort) || checkIpv6InBrackets(hostPort) || checkDomain(hostPort) ||
|
||||||
checkIpPort(hostPort) || checkDomainPort(hostPort)
|
checkIpPort(hostPort) || checkDomainPort(hostPort)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user