Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

poltergeist.lib.lua 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. local inspect = require("inspect")
  2. local jid = require("util.jid")
  3. local stanza = require("util.stanza")
  4. local timer = require("util.timer")
  5. local update_presence_identity = module:require("util").update_presence_identity
  6. local uuid = require("util.uuid")
  7. local component = module:get_option_string(
  8. "poltergeist_component",
  9. module.host
  10. )
  11. local expiration_timeout = module:get_option_string(
  12. "poltergeist_leave_timeout",
  13. 30 -- defaults to 30 seconds
  14. )
  15. local MUC_NS = "http://jabber.org/protocol/muc"
  16. --------------------------------------------------------------------------------
  17. -- Utility functions for commonly used poltergeist codes.
  18. --------------------------------------------------------------------------------
  19. -- Creates a nick for a poltergeist.
  20. -- @param username is the unique username of the poltergeist
  21. -- @return a nick to use for xmpp
  22. local function create_nick(username)
  23. return string.sub(username, 0,8)
  24. end
  25. -- Returns the last presence of the occupant.
  26. -- @param room the room instance where to check for occupant
  27. -- @param nick the nick of the occupant
  28. -- @return presence stanza of the occupant
  29. function get_presence(room, nick)
  30. local occupant_jid = room:get_occupant_jid(component.."/"..nick)
  31. if occupant_jid then
  32. return room:get_occupant_by_nick(occupant_jid):get_presence();
  33. end
  34. return nil;
  35. end
  36. -- Checks for existance of a poltergeist occupant in a room.
  37. -- @param room the room instance where to check for the occupant
  38. -- @param nick the nick of the occupant
  39. -- @return true if occupant is found, false otherwise
  40. function occupies(room, nick)
  41. -- Find out if we have a poltergeist occupant in the room for this JID
  42. return not not room:get_occupant_jid(component.."/"..nick);
  43. end
  44. --------------------------------------------------------------------------------
  45. -- Username storage for poltergeist.
  46. --
  47. -- Every poltergeist will have a username stored in a table underneath
  48. -- the room name that they are currently active in. The username can
  49. -- be retrieved given a room and a user_id. The username is removed from
  50. -- a room by providing the room and the nick.
  51. --
  52. -- A table with a single entry looks like:
  53. -- {
  54. -- ["[hug]hostilewerewolvesthinkslightly"] = {
  55. -- ["655363:52148a3e-b5fb-4cfc-8fbd-f55e793cf657"] = "ed7757d6-d88d-4e6a-8e24-aca2adc31348",
  56. -- ed7757d6 = "655363:52148a3e-b5fb-4cfc-8fbd-f55e793cf657"
  57. -- }
  58. -- }
  59. --------------------------------------------------------------------------------
  60. -- username is the table where all poltergeist are stored
  61. local usernames = {}
  62. -- Adds a poltergeist to the store.
  63. -- @param room is the room the poltergeist is being added to
  64. -- @param user_id is the user_id of the user the poltergeist represents
  65. -- @param username is the unique id of the poltergeist itself
  66. local function store_username(room, user_id, username)
  67. local room_name = jid.node(room.jid)
  68. if not usernames[room_name] then
  69. usernames[room_name] = {}
  70. end
  71. usernames[room_name][user_id] = username
  72. usernames[room_name][create_nick(username)] = user_id
  73. end
  74. -- Retrieves a poltergeist username from the store if one exists.
  75. -- @param room is the room to check for the poltergeist in the store
  76. -- @param user_id is the user id of the user the poltergeist represents
  77. local function get_username(room, user_id)
  78. local room_name = jid.node(room.jid)
  79. if not usernames[room_name] then
  80. return nil
  81. end
  82. return usernames[room_name][user_id]
  83. end
  84. local function get_username_from_nick(room_name, nick)
  85. if not usernames[room_name] then
  86. return nil
  87. end
  88. local user_id = usernames[room_name][nick]
  89. return usernames[room_name][user_id]
  90. end
  91. -- Removes the username from the store.
  92. -- @param room is the room the poltergeist is being removed from
  93. -- @param nick is the nick of the muc occupant
  94. local function remove_username(room, nick)
  95. local room_name = jid.node(room.jid);
  96. if not usernames[room_name] then
  97. return
  98. end
  99. local user_id = usernames[room_name][nick]
  100. usernames[room_name][user_id] = nil
  101. usernames[room_name][nick] = nil
  102. end
  103. -- Removes all poltergeists in the store for the provided room.
  104. -- @param room is the room all poltergiest will be removed from
  105. local function remove_room(room)
  106. local room_name = jid.node(room.jid)
  107. if usernames[room_name] then
  108. usernames[room_name] = nil
  109. end
  110. end
  111. --------------------------------------------------------------------------------
  112. -- State for toggling the tagging of presence stanzas with ignored tag.
  113. --
  114. -- A poltergeist with it's full room/nick set to ignore will have a jitsi ignore
  115. -- tag applied to all presence stanza's broadcasted. The following funcitons
  116. -- assisst in managing this state.
  117. --------------------------------------------------------------------------------
  118. local presence_ignored = {}
  119. -- Sets the nick to ignored state.
  120. -- @param room_nick full room/nick jid
  121. local function set_ignored(room_nick)
  122. presence_ignored[room_nick] = true
  123. end
  124. -- Resets the nick out of ignored state.
  125. -- @param room_nick full room/nick jid
  126. local function reset_ignored(room_nick)
  127. presence_ignored[room_nick] = nil
  128. end
  129. -- Determines whether or not the leave presence should be tagged with ignored.
  130. -- @param room_nick full room/nick jid
  131. local function should_ignore(room_nick)
  132. if presence_ignored[room_nick] == nil then
  133. return false
  134. end
  135. return presence_ignored[room_nick]
  136. end
  137. --------------------------------------------------------------------------------
  138. -- Poltergeist control functions for adding, updating and removing poltergeist.
  139. --------------------------------------------------------------------------------
  140. -- Updates the status tags and call flow tags of an existing poltergeist
  141. -- presence.
  142. -- @param presence_stanza is the actual presence stanza for a poltergeist.
  143. -- @param status is the new status to be updated in the stanza.
  144. -- @param call_details is a table of call flow signal information.
  145. function update_presence_tags(presence_stanza, status, call_details)
  146. local call_cancel = false
  147. local call_id = nil
  148. -- Extract optional call flow signal information.
  149. if call_details then
  150. call_id = call_details["id"]
  151. if call_details["cancel"] then
  152. call_cancel = call_details["cancel"]
  153. end
  154. end
  155. presence_stanza:maptags(function (tag)
  156. if tag.name == "status" then
  157. if call_cancel then
  158. -- If call cancel is set then the status should not be changed.
  159. return tag
  160. end
  161. return stanza.stanza("status"):text(status)
  162. elseif tag.name == "call_id" then
  163. if call_id then
  164. return stanza.stanza("call_id"):text(call_id)
  165. else
  166. -- If no call id is provided the re-use the existing id.
  167. return tag
  168. end
  169. elseif tag.name == "call_cancel" then
  170. if call_cancel then
  171. return stanza.stanza("call_cancel"):text("true")
  172. else
  173. return stanza.stanza("call_cancel"):text("false")
  174. end
  175. end
  176. return tag
  177. end)
  178. return presence_stanza
  179. end
  180. -- Updates the presence status of a poltergeist.
  181. -- @param room is the room the poltergeist has occupied
  182. -- @param nick is the xmpp nick of the poltergeist occupant
  183. -- @param status is the status string to set in the presence
  184. -- @param call_details is a table of call flow control details
  185. local function update(room, nick, status, call_details)
  186. local original_presence = get_presence(room, nick)
  187. if not original_presence then
  188. module:log("info", "update issued for a non-existing poltergeist")
  189. return
  190. end
  191. -- update occupant presence with appropriate to and from
  192. -- so we can send it again
  193. update_presence = stanza.clone(original_presence)
  194. update_presence.attr.to = room.jid.."/"..nick
  195. update_presence.attr.from = component.."/"..nick
  196. update_presence = update_presence_tags(update_presence, status, call_details)
  197. module:log("info", "updating poltergeist: %s/%s - %s", room, nick, status)
  198. room:handle_normal_presence(
  199. prosody.hosts[component],
  200. update_presence
  201. )
  202. end
  203. -- Removes the poltergeist from the room.
  204. -- @param room is the room the poltergeist has occupied
  205. -- @param nick is the xmpp nick of the poltergeist occupant
  206. -- @param ignore toggles if the leave subsequent leave presence should be tagged
  207. local function remove(room, nick, ignore)
  208. local original_presence = get_presence(room, nick);
  209. if not original_presence then
  210. module:log("info", "attempted to remove a poltergeist with no presence")
  211. return
  212. end
  213. local leave_presence = stanza.clone(original_presence)
  214. leave_presence.attr.to = room.jid.."/"..nick
  215. leave_presence.attr.from = component.."/"..nick
  216. leave_presence.attr.type = "unavailable"
  217. if (ignore) then
  218. set_ignored(room.jid.."/"..nick)
  219. end
  220. remove_username(room, nick)
  221. module:log("info", "removing poltergeist: %s/%s", room, nick)
  222. room:handle_normal_presence(
  223. prosody.hosts[component],
  224. leave_presence
  225. )
  226. end
  227. -- Adds a poltergeist to a muc/room.
  228. -- @param room is the room the poltergeist will occupy
  229. -- @param is the id of the user the poltergeist represents
  230. -- @param display_name is the display name to use for the poltergeist
  231. -- @param avatar is the avatar link used for the poltergeist display
  232. -- @param context is the session context of the user making the request
  233. -- @param status is the presence status string to use
  234. local function add_to_muc(room, user_id, display_name, avatar, context, status)
  235. local username = uuid.generate()
  236. local presence_stanza = original_presence(
  237. room,
  238. username,
  239. display_name,
  240. avatar,
  241. context,
  242. status
  243. )
  244. store_username(room, user_id, username)
  245. module:log("info", "adding poltergeist: %s/%s", room, create_nick(username))
  246. room:handle_first_presence(
  247. prosody.hosts[component],
  248. presence_stanza
  249. )
  250. local remove_delay = 5
  251. local expiration = expiration_timeout - remove_delay;
  252. local nick = create_nick(username)
  253. timer.add_task(
  254. expiration,
  255. function ()
  256. update(room, nick, "expired")
  257. timer.add_task(
  258. remove_delay,
  259. function ()
  260. if occupies(room, nick) then
  261. remove(room, nick, false)
  262. end
  263. end
  264. )
  265. end
  266. )
  267. end
  268. -- Generates an original presence for a new poltergeist
  269. -- @param room is the room the poltergeist will occupy
  270. -- @param username is the unique name for the poltergeist
  271. -- @param display_name is the display name to use for the poltergeist
  272. -- @param avatar is the avatar link used for the poltergeist display
  273. -- @param context is the session context of the user making the request
  274. -- @param status is the presence status string to use
  275. -- @return a presence stanza that can be used to add the poltergeist to the muc
  276. function original_presence(room, username, display_name, avatar, context, status)
  277. local nick = create_nick(username)
  278. local p = stanza.presence({
  279. to = room.jid.."/"..nick,
  280. from = component.."/"..nick,
  281. }):tag("x", { xmlns = MUC_NS }):up();
  282. p:tag("bot", { type = "poltergeist" }):up();
  283. p:tag("call_cancel"):text(nil):up();
  284. p:tag("call_id"):text(username):up();
  285. if status then
  286. p:tag("status"):text(status):up();
  287. else
  288. p:tag("status"):text(nil):up();
  289. end
  290. if display_name then
  291. p:tag(
  292. "nick",
  293. { xmlns = "http://jabber.org/protocol/nick" }):text(display_name):up();
  294. end
  295. if avatar then
  296. p:tag("avatar-url"):text(avatar):up();
  297. end
  298. -- If the room has a password set, let the poltergeist enter using it
  299. local room_password = room:get_password();
  300. if room_password then
  301. local join = p:get_child("x", MUC_NS);
  302. join:tag("password", { xmlns = MUC_NS }):text(room_password);
  303. end
  304. update_presence_identity(
  305. p,
  306. context.user,
  307. context.group,
  308. context.creator_user,
  309. context.creator_group
  310. )
  311. return p
  312. end
  313. return {
  314. get_username = get_username,
  315. get_username_from_nick = get_username_from_nick,
  316. occupies = occupies,
  317. remove_room = remove_room,
  318. reset_ignored = reset_ignored,
  319. should_ignore = should_ignore,
  320. create_nick = create_nick,
  321. add_to_muc = add_to_muc,
  322. update = update,
  323. remove = remove
  324. }