Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / rcs / RcsSipResponse.kt
RcsSipResponse.kt77 lines · 3.3 KiBRaw
| 1 | package com.vayunmathur.communicate.data.rcs |
| 2 | |
| 3 | import android.telephony.ims.SipMessage |
| 4 | |
| 5 | /** |
| 6 | * Pure SIP response parser (RFC 3261 §7.2): `SIP/2.0 <code> <reason>` start |
| 7 | * line + the dialog identifiers a response routes on. |
| 8 | * |
| 9 | * Split from `RcsSipTransport` for testability — no transport, no session |
| 10 | * map. [parse] returns null when [message] is a request, not a response. |
| 11 | */ |
| 12 | object RcsSipResponse { |
| 13 | /** A parsed SIP response with its routing keys. */ |
| 14 | data class Parsed( |
| 15 | val statusCode: Int, |
| 16 | val reason: String, |
| 17 | /** Via branch of the top Via (== our transaction id). */ |
| 18 | val branch: String, |
| 19 | val callId: String, |
| 20 | /** To-tag (dialog id for 2xx to INVITE). */ |
| 21 | val toTag: String?, |
| 22 | /** From-tag (echo of our tag). */ |
| 23 | val fromTag: String?, |
| 24 | /** CSeq method token (INVITE/MESSAGE/BYE/REFER/…). */ |
| 25 | val cseqMethod: String, |
| 26 | val cseqNumber: Long?, |
| 27 | /** Raw header section (for Contact/Record-Route/privacy parsing). */ |
| 28 | val headers: String, |
| 29 | /** Body text (SDP answers). */ |
| 30 | val body: String, |
| 31 | ) |
| 32 | |
| 33 | fun parse(message: SipMessage): Parsed? { |
| 34 | return runCatching { |
| 35 | val startLine = message.getStartLine().trim() |
| 36 | if (!startLine.startsWith("SIP/2.0", ignoreCase = true)) return null |
| 37 | val statusCode = startLine.substringAfter("SIP/2.0").trim() |
| 38 | .substringBefore(" ").trim().toIntOrNull() ?: return null |
| 39 | val reason = startLine.substringAfter("SIP/2.0").trim() |
| 40 | .substringAfter(" ").trim() |
| 41 | val headers = message.getHeaderSection() |
| 42 | val branch = headers.lineSequence() |
| 43 | .firstOrNull { it.trim().startsWith("Via:", ignoreCase = true) } |
| 44 | ?.substringAfter("branch=")?.substringBefore(";")?.trim() |
| 45 | ?.takeIf { it.isNotEmpty() } ?: return null |
| 46 | val callId = headers.lineSequence() |
| 47 | .firstOrNull { it.trim().startsWith("Call-ID:", ignoreCase = true) } |
| 48 | ?.substringAfter(":")?.trim()?.takeIf { it.isNotEmpty() } |
| 49 | ?: return null |
| 50 | fun tagOf(name: String): String? = headers.lineSequence() |
| 51 | .firstOrNull { it.trim().startsWith(name, ignoreCase = true) } |
| 52 | ?.substringAfter("tag=", "")?.substringBefore(";")?.trim() |
| 53 | ?.takeIf { it.isNotEmpty() } |
| 54 | val cseqLine = headers.lineSequence() |
| 55 | .firstOrNull { it.trim().startsWith("CSeq:", ignoreCase = true) } |
| 56 | ?.substringAfter(":")?.trim().orEmpty() |
| 57 | val cseqNumber = cseqLine.substringBefore(" ").trim().toLongOrNull() |
| 58 | val cseqMethod = cseqLine.substringAfter(" ").trim() |
| 59 | Parsed( |
| 60 | statusCode = statusCode, |
| 61 | reason = reason, |
| 62 | branch = branch, |
| 63 | callId = callId, |
| 64 | toTag = tagOf("To:"), |
| 65 | fromTag = tagOf("From:"), |
| 66 | cseqMethod = cseqMethod, |
| 67 | cseqNumber = cseqNumber, |
| 68 | headers = headers, |
| 69 | body = message.getContent().toString(Charsets.UTF_8), |
| 70 | ) |
| 71 | }.getOrNull() |
| 72 | } |
| 73 | |
| 74 | /** True when [startLine] is a SIP response rather than a request. */ |
| 75 | fun isResponse(startLine: String): Boolean = |
| 76 | startLine.trim().startsWith("SIP/2.0", ignoreCase = true) |
| 77 | } |