package com.vayunmathur.communicate.data.rcs /** * RCS Universal Profile session + messaging primitives. * * Covers the TestRcsApp gap analysis: * - TestRcsApp HAS: INVITE/MSRP 1:1 sessions (SimpleChatSession), CPIM * envelopes (CpimUtils), FT-over-HTTP upload (FileUploadController), * provisioning flow (ProvisioningController), UCE (UceActivity). * - TestRcsApp LACKS (built here from the UP spec): IMDN delivery/display * receipts (RFC 5438), is-typing notifications (RFC 3994), group * conference INVITE semantics, FT-over-HTTP receiver side. * * All SIP construction uses the `SipDelegateConfiguration` captured by * [RcsSipTransport] (public user identity, home domain, local address). * Pager-mode CPIM remains the v1 send path; sessions upgrade a conversation * to INVITE/MSRP when both ends support it. */ /** A live CPM session dialog (INVITE/200/ACK completed, MSRP bound). */ data class RcsSession( /** SIP dialog id (Call-ID + tags); routes in-dialog traffic. */ val dialogId: String, val callId: String, val localTag: String, val remoteTag: String, val remoteUri: String, val conversationId: String, val isGroup: Boolean = false, /** True for the pseudo-session tracking a conference we host (focus role). */ val isFocus: Boolean = false, /** MSRP path negotiated via SDP (`a=path`). Null until bound. */ val msrpLocalPath: String? = null, val msrpRemotePath: String? = null, /** * Peer's MSRP setup role from the SDP answer. ACTIVE-only: we always * connect out, so a peer answering `active` keeps `msrpRemotePath` * nulled (pager-mode fallback). Null = absent, treated as passive. */ val msrpSetup: MsrpSetup? = null, /** * True when this session's MSRP media runs over TLS (`msrps://`, * RFC 4976). Negotiated from the SDP (`TCP/TLS/MSRP` or `a=fingerprint` * on either side); plaintext otherwise. Both socket directions honor it. */ val msrpSecure: Boolean = false, /** * Next CSeq number for in-dialog requests (RFC 3261 §12.2: each new * request in the dialog increments; ACK reuses the INVITE's CSeq). * Starts at 2 — CSeq 1 is the initial INVITE. */ val nextCseq: Long = 2L, /** Peer's Contact URI from the 200 OK (in-dialog Request-URI). */ val remoteContact: String? = null, /** Recorded route set from the 200 OK (`Record-Route`, reversed). */ val routeSet: List = emptyList(), /** True once the dialog is confirmed (ACK sent for our INVITE's 2xx, or * ACK received for an incoming INVITE we answered). Unconfirmed dialogs * exist for in-dialog MESSAGE but not media. */ val confirmed: Boolean = false, /** CSeq number of our initial INVITE (echoed by the ACK, RFC 3261 §13). */ val inviteCseq: Long = 1L, val createdAt: Long = System.currentTimeMillis(), ) /** MSRP connection roles (`a=setup:`). */ enum class MsrpSetup { ACTIVE, PASSIVE, ACTPASS, } /** IMDN disposition (RFC 5438) for delivery/display receipts. */ enum class ImdnDisposition(val token: String) { Delivered("positive-delivery"), Displayed("display"), Failed("failed"), } /** * Build an IMDN `message/imdn+xml` body reporting [disposition] for * [originalMessageId] (the `imdn.Message-ID` from the received CPIM). */ fun buildImdnBody(originalMessageId: String, disposition: ImdnDisposition): String = buildString { append("\n") append("\n") append(" $originalMessageId\n") append(" ${java.time.Instant.now()}\n") append(" <${disposition.token}/>\n") append("") } /** Parse an IMDN report body into (message-id, disposition), or null. */ fun parseImdnBody(body: String): Pair? { if (!body.contains("([^<]+)").find(body)?.groupValues?.getOrNull(1) ?: return null val disposition = ImdnDisposition.entries.firstOrNull { body.contains("<${it.token}", ignoreCase = true) } ?: return null return id.trim() to disposition } /** Extract the `imdn.Message-ID` from a CPIM envelope, or null. */ fun extractImdnMessageId(cpim: String): String? = Regex("imdn\\.Message-ID:\\s*(\\S+)", RegexOption.IGNORE_CASE).find(cpim) ?.groupValues?.getOrNull(1)?.trim() /** * Build an `application/im-iscomposing+xml` body (RFC 3994): [active] true * while the user is typing, false + `lastactive` on idle. */ fun buildIsComposingBody(active: Boolean): String = buildString { append("\n") append("\n") append(" ${if (active) "active" else "idle"}\n") if (!active) append(" ${java.time.Instant.now()}\n") append("") } /** True when [body] is an is-composing notification; returns active flag or null. */ fun parseIsComposingBody(body: String): Boolean? { if (!body.contains("im-composing", ignoreCase = true)) return null return when { body.contains("active", ignoreCase = true) -> true body.contains("idle", ignoreCase = true) -> false else -> null } }