You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_muc_call.lua 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. local ext_events = module:require "ext_events"
  2. local jid = require "util.jid"
  3. -- Options and configuration
  4. local poltergeist_component = module:get_option_string(
  5. "poltergeist_component",
  6. module.host
  7. );
  8. local muc_domain_base = module:get_option_string("muc_mapper_domain_base");
  9. if not muc_domain_base then
  10. module:log(
  11. "warn",
  12. "No 'muc_domain_base' option set, unable to send call events."
  13. );
  14. return
  15. end
  16. -- Status strings that trigger call events.
  17. local calling_status = "calling"
  18. local busy_status = "busy"
  19. local rejected_status = "rejected"
  20. local connected_status = "connected"
  21. local expired_status = "expired"
  22. -- url_from_room_jid will determine the url for a conference
  23. -- provided a room jid. It is required that muc domain mapping
  24. -- is enabled and configured. There are two url formats that are supported.
  25. -- The following urls are examples of the supported formats.
  26. -- https://meet.jit.si/jitsi/ProductiveMeeting
  27. -- https://meet.jit.si/MoreProductiveMeeting
  28. -- The urls are derived from portions of the room jid.
  29. local function url_from_room_jid(room_jid)
  30. local node, _, _ = jid.split(room_jid)
  31. if not node then return nil end
  32. local target_subdomain, target_node = node:match("^%[([^%]]+)%](.+)$")
  33. if not(target_node or target_subdomain) then
  34. return "https://"..muc_domain_base.."/"..node
  35. else
  36. return "https://"..muc_domain_base.."/"..target_subdomain.."/"..target_node
  37. end
  38. end
  39. -- Listening for all muc presences stanza events. If a presence stanza is from
  40. -- a poltergeist then it will be further processed to determine if a call
  41. -- event should be triggered. Call events are triggered by status strings
  42. -- the status strings supported are:
  43. -- -------------------------
  44. -- Status | Event Type
  45. -- _________________________
  46. -- "calling" | INVITE
  47. -- "busy" | CANCEL
  48. -- "rejected" | CANCEL
  49. -- "connected" | CANCEL
  50. module:hook(
  51. "muc-broadcast-presence",
  52. function (event)
  53. -- Detect if the presence is for a poltergeist or not.
  54. if not (jid.bare(event.occupant.jid) == poltergeist_component) then
  55. return
  56. end
  57. -- A presence stanza is needed in order to trigger any calls.
  58. if not event.stanza then
  59. return
  60. end
  61. local call_id = event.stanza:get_child_text("call_id")
  62. if not call_id then
  63. module:log("info", "A call id was not provided in the status.")
  64. return
  65. end
  66. local invite = function()
  67. local url = assert(url_from_room_jid(event.stanza.attr.from))
  68. ext_events.invite(event.stanza, url, call_id)
  69. end
  70. local cancel = function()
  71. local url = assert(url_from_room_jid(event.stanza.attr.from))
  72. local status = event.stanza:get_child_text("status")
  73. ext_events.cancel(event.stanza, url, string.lower(status), call_id)
  74. end
  75. -- If for any reason call_cancel is set to true then a cancel
  76. -- is sent regardless of the rest of the presence info.
  77. local should_cancel = event.stanza:get_child_text("call_cancel")
  78. if should_cancel == "true" then
  79. cancel()
  80. return
  81. end
  82. local missed = function()
  83. cancel()
  84. ext_events.missed(event.stanza, call_id)
  85. end
  86. -- All other call flow actions will require a status.
  87. if event.stanza:get_child_text("status") == nil then
  88. return
  89. end
  90. local switch = function(status)
  91. case = {
  92. [calling_status] = function() invite() end,
  93. [busy_status] = function() cancel() end,
  94. [rejected_status] = function() missed() end,
  95. [expired_status] = function() missed() end,
  96. [connected_status] = function() cancel() end
  97. }
  98. if case[status] then case[status]() end
  99. end
  100. switch(event.stanza:get_child_text("status"))
  101. end,
  102. -101
  103. );