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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 ringing_status = "ringing"
  19. local busy_status = "busy"
  20. local rejected_status = "rejected"
  21. local connected_status = "connected"
  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
  37. "https://"..muc_domain_base.."/"..target_subdomain.."/"..target_node
  38. end
  39. end
  40. -- Listening for all muc presences stanza events. If a presence stanza is from
  41. -- a poltergeist then it will be further processed to determine if a call
  42. -- event should be triggered. Call events are triggered by status strings
  43. -- the status strings supported are:
  44. -- -------------------------
  45. -- Status | Event Type
  46. -- _________________________
  47. -- "calling" | INVITE
  48. -- "ringing" | CANCEL
  49. -- "busy" | CANCEL
  50. -- "rejected" | CANCEL
  51. -- "connected" | CANCEL
  52. module:hook("muc-broadcast-presence", function (event)
  53. -- Detect if the presence is for a poltergeist or not.
  54. if not
  55. (jid.bare(event.occupant.jid) == poltergeist_component)
  56. or
  57. event.stanza == nil
  58. 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. local should_cancel = event.stanza:get_child_text("call_cancel")
  76. if should_cancel == "true" then
  77. cancel()
  78. return
  79. end
  80. local switch = function(status)
  81. case = {
  82. [calling_status] = function() invite() end,
  83. [ringing_status] = function() cancel() end,
  84. [busy_status] = function() cancel() end,
  85. [rejected_status] = function() cancel() end,
  86. [connected_status] = function() cancel() end
  87. }
  88. if case[status] then case[status]() end
  89. end
  90. switch(event.stanza:get_child_text("status"))
  91. end, -101);