Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / rcs / RcsConferenceEvents.kt

RcsConferenceEvents.kt125 lines · 5.8 KiBRaw
1package com.vayunmathur.communicate.data.rcs
2
3/**
4 * Conference event package (RFC 4575 `conference-info`, RFC 4353 SUBSCRIBE/
5 * NOTIFY for conferences).
6 *
7 * When we join a carrier-hosted focus, SUBSCRIBEing to `Event: conference`
8 * at the focus URI yields the real participant list + join/part transitions
9 * — without it we only know who we invited and who INVITEd us. When we host
10 * (§3.2), we publish minimal NOTIFYs to our subscribers.
11 *
12 * All parsing/building here is pure string work (no transport, no session
13 * map); the dialog flows live in `RcsSessionManager` + `RcsInboundDialog`.
14 */
15object RcsConferenceEvents {
16 /** One participant from a `conference-info` document. */
17 data class Participant(
18 /** SIP URI of the endpoint. */
19 val uri: String,
20 /** `full` / `partial` state wrapper is handled by the caller. */
21 val state: String,
22 /** Display name when present. */
23 val displayName: String?,
24 )
25
26 /** Parsed `conference-info`: entity (focus URI) + current users. */
27 data class ConferenceInfo(
28 val entity: String,
29 val version: Int,
30 val users: List<Participant>,
31 )
32
33 /**
34 * Parse an `application/conference-info+xml` body. Returns null when the
35 * body isn't conference-info. `state="deleted"` users are excluded (the
36 * caller reconciles them out of the member set separately via
37 * [deletedUris]).
38 */
39 fun parseConferenceInfo(body: String): ConferenceInfo? {
40 if (!body.contains("conference-info", ignoreCase = true)) return null
41 val entity = Regex("<conference-info[^>]*\\bentity=\"([^\"]+)\"", RegexOption.IGNORE_CASE)
42 .find(body)?.groupValues?.getOrNull(1)?.trim().orEmpty()
43 val version = Regex("<conference-info[^>]*\\bversion=\"(\\d+)\"", RegexOption.IGNORE_CASE)
44 .find(body)?.groupValues?.getOrNull(1)?.toIntOrNull() ?: 0
45 val users = mutableListOf<Participant>()
46 // Group 1 = the full <user ...> opening tag (may carry state="deleted"
47 // per RFC 4575 §5.4.1), group 2 = entity, group 3 = element body.
48 val userRe = Regex(
49 "(<user[^>]*\\bentity=\"([^\"]+)\"[^>]*>)(.*?)</user>",
50 setOf(RegexOption.IGNORE_CASE, RegexOption.DOT_MATCHES_ALL),
51 )
52 for (m in userRe.findAll(body)) {
53 val tag = m.groupValues.getOrNull(1).orEmpty()
54 val uri = m.groupValues.getOrNull(2)?.trim()
55 val inner = m.groupValues.getOrNull(3).orEmpty()
56 val deleted = tag.contains("state=\"deleted\"", ignoreCase = true) ||
57 inner.contains("state=\"deleted\"", ignoreCase = true) ||
58 inner.contains("<state>deleted</state>", ignoreCase = true)
59 if (uri != null && !deleted) {
60 val state = Regex("<state>([^<]+)</state>", RegexOption.IGNORE_CASE)
61 .find(inner)?.groupValues?.getOrNull(1)?.trim() ?: "full"
62 val display = Regex("<display-text>([^<]*)</display-text>", RegexOption.IGNORE_CASE)
63 .find(inner)?.groupValues?.getOrNull(1)?.trim()?.takeIf { it.isNotEmpty() }
64 users += Participant(uri, state, display)
65 }
66 }
67 return ConferenceInfo(entity, version, users)
68 }
69
70 /** URIs in `state="deleted"` (parted members to reconcile out). */
71 fun deletedUris(body: String): List<String> {
72 if (!body.contains("conference-info", ignoreCase = true)) return emptyList()
73 val out = mutableListOf<String>()
74 val userRe = Regex(
75 "(<user[^>]*\\bentity=\"([^\"]+)\"[^>]*>)(.*?)</user>",
76 setOf(RegexOption.IGNORE_CASE, RegexOption.DOT_MATCHES_ALL),
77 )
78 for (m in userRe.findAll(body)) {
79 val tag = m.groupValues.getOrNull(1).orEmpty()
80 val inner = m.groupValues.getOrNull(3).orEmpty()
81 if (tag.contains("state=\"deleted\"", ignoreCase = true) ||
82 inner.contains("state=\"deleted\"", ignoreCase = true) ||
83 inner.contains("<state>deleted</state>", ignoreCase = true)
84 ) {
85 m.groupValues.getOrNull(2)?.trim()?.takeIf { it.isNotEmpty() }?.let { out += it }
86 }
87 }
88 return out
89 }
90
91 /**
92 * Build a minimal `conference-info` NOTIFY body for a hosted focus:
93 * [focusUri] entity, [version] counter, [members] as full users.
94 */
95 fun buildConferenceInfo(focusUri: String, version: Int, members: Collection<String>): String =
96 buildString {
97 append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
98 append("<conference-info xmlns=\"urn:ietf:params:xml:ns:conference-info\"\n")
99 append(" entity=\"$focusUri\" version=\"$version\" state=\"full\">\n")
100 append(" <users>\n")
101 for (member in members) {
102 append(" <user entity=\"$member\" state=\"full\">\n")
103 append(" <endpoint entity=\"$member\"><state>connected</state></endpoint>\n")
104 append(" </user>\n")
105 }
106 append(" </users>\n")
107 append("</conference-info>")
108 }
109
110 /**
111 * Build a `message/sipfrag` body (RFC 3420) reporting a referral outcome,
112 * e.g. `SIP/2.0 200 OK`. Sent inside NOTIFYs to a REFER referrer (§3.3).
113 */
114 fun buildSipfrag(statusCode: Int, reason: String): String = "SIP/2.0 $statusCode $reason"
115
116 /** Parse a `message/sipfrag` body into (code, reason), or null. */
117 fun parseSipfrag(body: String): Pair<Int, String>? {
118 val trimmed = body.trim()
119 if (!trimmed.startsWith("SIP/2.0")) return null
120 val code = trimmed.substringAfter("SIP/2.0").trim().substringBefore(" ")
121 .trim().toIntOrNull() ?: return null
122 val reason = trimmed.substringAfter("SIP/2.0").trim().substringAfter(" ").trim()
123 return code to reason
124 }
125}