Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / rcs / RcsSession.kt
RcsSession.kt128 lines · 5.3 KiBRaw
| 1 | package com.vayunmathur.communicate.data.rcs |
| 2 | |
| 3 | /** |
| 4 | * RCS Universal Profile session + messaging primitives. |
| 5 | * |
| 6 | * Covers the TestRcsApp gap analysis: |
| 7 | * - TestRcsApp HAS: INVITE/MSRP 1:1 sessions (SimpleChatSession), CPIM |
| 8 | * envelopes (CpimUtils), FT-over-HTTP upload (FileUploadController), |
| 9 | * provisioning flow (ProvisioningController), UCE (UceActivity). |
| 10 | * - TestRcsApp LACKS (built here from the UP spec): IMDN delivery/display |
| 11 | * receipts (RFC 5438), is-typing notifications (RFC 3994), group |
| 12 | * conference INVITE semantics, FT-over-HTTP receiver side. |
| 13 | * |
| 14 | * All SIP construction uses the `SipDelegateConfiguration` captured by |
| 15 | * [RcsSipTransport] (public user identity, home domain, local address). |
| 16 | * Pager-mode CPIM remains the v1 send path; sessions upgrade a conversation |
| 17 | * to INVITE/MSRP when both ends support it. |
| 18 | */ |
| 19 | |
| 20 | /** A live CPM session dialog (INVITE/200/ACK completed, MSRP bound). */ |
| 21 | data class RcsSession( |
| 22 | /** SIP dialog id (Call-ID + tags); routes in-dialog traffic. */ |
| 23 | val dialogId: String, |
| 24 | val callId: String, |
| 25 | val localTag: String, |
| 26 | val remoteTag: String, |
| 27 | val remoteUri: String, |
| 28 | val conversationId: String, |
| 29 | val isGroup: Boolean = false, |
| 30 | /** True for the pseudo-session tracking a conference we host (focus role). */ |
| 31 | val isFocus: Boolean = false, |
| 32 | /** MSRP path negotiated via SDP (`a=path`). Null until bound. */ |
| 33 | val msrpLocalPath: String? = null, |
| 34 | val msrpRemotePath: String? = null, |
| 35 | /** |
| 36 | * Peer's MSRP setup role from the SDP answer. ACTIVE-only: we always |
| 37 | * connect out, so a peer answering `active` keeps `msrpRemotePath` |
| 38 | * nulled (pager-mode fallback). Null = absent, treated as passive. |
| 39 | */ |
| 40 | val msrpSetup: MsrpSetup? = null, |
| 41 | /** |
| 42 | * True when this session's MSRP media runs over TLS (`msrps://`, |
| 43 | * RFC 4976). Negotiated from the SDP (`TCP/TLS/MSRP` or `a=fingerprint` |
| 44 | * on either side); plaintext otherwise. Both socket directions honor it. |
| 45 | */ |
| 46 | val msrpSecure: Boolean = false, |
| 47 | /** |
| 48 | * Next CSeq number for in-dialog requests (RFC 3261 §12.2: each new |
| 49 | * request in the dialog increments; ACK reuses the INVITE's CSeq). |
| 50 | * Starts at 2 — CSeq 1 is the initial INVITE. |
| 51 | */ |
| 52 | val nextCseq: Long = 2L, |
| 53 | /** Peer's Contact URI from the 200 OK (in-dialog Request-URI). */ |
| 54 | val remoteContact: String? = null, |
| 55 | /** Recorded route set from the 200 OK (`Record-Route`, reversed). */ |
| 56 | val routeSet: List<String> = emptyList(), |
| 57 | /** True once the dialog is confirmed (ACK sent for our INVITE's 2xx, or |
| 58 | * ACK received for an incoming INVITE we answered). Unconfirmed dialogs |
| 59 | * exist for in-dialog MESSAGE but not media. */ |
| 60 | val confirmed: Boolean = false, |
| 61 | /** CSeq number of our initial INVITE (echoed by the ACK, RFC 3261 §13). */ |
| 62 | val inviteCseq: Long = 1L, |
| 63 | val createdAt: Long = System.currentTimeMillis(), |
| 64 | ) |
| 65 | |
| 66 | /** MSRP connection roles (`a=setup:`). */ |
| 67 | enum class MsrpSetup { |
| 68 | ACTIVE, |
| 69 | PASSIVE, |
| 70 | ACTPASS, |
| 71 | } |
| 72 | |
| 73 | /** IMDN disposition (RFC 5438) for delivery/display receipts. */ |
| 74 | enum class ImdnDisposition(val token: String) { |
| 75 | Delivered("positive-delivery"), |
| 76 | Displayed("display"), |
| 77 | Failed("failed"), |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Build an IMDN `message/imdn+xml` body reporting [disposition] for |
| 82 | * [originalMessageId] (the `imdn.Message-ID` from the received CPIM). |
| 83 | */ |
| 84 | fun buildImdnBody(originalMessageId: String, disposition: ImdnDisposition): String = buildString { |
| 85 | append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") |
| 86 | append("<imdn xmlns=\"urn:ietf:params:xml:ns:imdn\">\n") |
| 87 | append(" <message-id>$originalMessageId</message-id>\n") |
| 88 | append(" <datetime>${java.time.Instant.now()}</datetime>\n") |
| 89 | append(" <status><${disposition.token}/></status>\n") |
| 90 | append("</imdn>") |
| 91 | } |
| 92 | |
| 93 | /** Parse an IMDN report body into (message-id, disposition), or null. */ |
| 94 | fun parseImdnBody(body: String): Pair<String, ImdnDisposition>? { |
| 95 | if (!body.contains("<imdn", ignoreCase = true)) return null |
| 96 | val id = Regex("<message-id>([^<]+)</message-id>").find(body)?.groupValues?.getOrNull(1) |
| 97 | ?: return null |
| 98 | val disposition = ImdnDisposition.entries.firstOrNull { body.contains("<${it.token}", ignoreCase = true) } |
| 99 | ?: return null |
| 100 | return id.trim() to disposition |
| 101 | } |
| 102 | |
| 103 | /** Extract the `imdn.Message-ID` from a CPIM envelope, or null. */ |
| 104 | fun extractImdnMessageId(cpim: String): String? = |
| 105 | Regex("imdn\\.Message-ID:\\s*(\\S+)", RegexOption.IGNORE_CASE).find(cpim) |
| 106 | ?.groupValues?.getOrNull(1)?.trim() |
| 107 | |
| 108 | /** |
| 109 | * Build an `application/im-iscomposing+xml` body (RFC 3994): [active] true |
| 110 | * while the user is typing, false + `lastactive` on idle. |
| 111 | */ |
| 112 | fun buildIsComposingBody(active: Boolean): String = buildString { |
| 113 | append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") |
| 114 | append("<isComposing xmlns=\"urn:ietf:params:xml:ns:im-composing\">\n") |
| 115 | append(" <state>${if (active) "active" else "idle"}</state>\n") |
| 116 | if (!active) append(" <lastactive>${java.time.Instant.now()}</lastactive>\n") |
| 117 | append("</isComposing>") |
| 118 | } |
| 119 | |
| 120 | /** True when [body] is an is-composing notification; returns active flag or null. */ |
| 121 | fun parseIsComposingBody(body: String): Boolean? { |
| 122 | if (!body.contains("im-composing", ignoreCase = true)) return null |
| 123 | return when { |
| 124 | body.contains("<state>active</state>", ignoreCase = true) -> true |
| 125 | body.contains("<state>idle</state>", ignoreCase = true) -> false |
| 126 | else -> null |
| 127 | } |
| 128 | } |