浏览代码

Adds prosody plugin that query existing rooms for information.

Queries room for their size or room particiapnt's information. Depends on luarocks net-url module.
master
damencho 8 年前
父节点
当前提交
589f77ef0e
共有 1 个文件被更改,包括 127 次插入0 次删除
  1. 127
    0
      resources/prosody-plugins/mod_muc_size.lua

+ 127
- 0
resources/prosody-plugins/mod_muc_size.lua 查看文件

@@ -0,0 +1,127 @@
1
+-- Prosody IM
2
+-- Copyright (C) 2017 Atlassian
3
+--
4
+-- This project is MIT/X11 licensed. Please see the
5
+-- COPYING file in the source package for more information.
6
+--
7
+-- This module requires net-url module
8
+-- Install it using #luarocks install net-url
9
+
10
+module:set_global(); -- Global module
11
+
12
+local split_jid = require "util.jid".split;
13
+local st = require "util.stanza";
14
+local it = require "util.iterators";
15
+local json = require "util.json";
16
+local iterators = require "util.iterators";
17
+local array = require"util.array";
18
+
19
+local tostring = tostring;
20
+local neturl = require "net.url";
21
+local parse = neturl.parseQuery;
22
+
23
+function get_room_from_jid(jid)
24
+	local node, host = split_jid(jid);
25
+	local component = hosts[host];
26
+	if component then
27
+		local muc = component.modules.muc
28
+		if muc and rawget(muc,"rooms") then
29
+			-- We're running 0.9.x or 0.10 (old MUC API)
30
+			return muc.rooms[jid];
31
+		elseif muc and rawget(muc,"get_room_from_jid") then
32
+			-- We're running >0.10 (new MUC API)
33
+			return muc.get_room_from_jid(jid);
34
+		else
35
+			return
36
+		end
37
+	end
38
+end
39
+
40
+function handle_get_room_size(event)
41
+	local params = parse(event.request.url.query);
42
+	local room_name = params["room"];
43
+	local room = get_room_from_jid(room_name);
44
+	local participant_count = 0;
45
+
46
+	log("debug", "Querying room %s", tostring(room_name));
47
+
48
+	if room then
49
+		local occupants = room._occupants;
50
+		if occupants then
51
+			participant_count = iterators.count(room:each_occupant());
52
+		end
53
+		log("debug", "there are %s occupants in room", tostring(participant_count));
54
+	else
55
+		log("debug", "no such room exists");
56
+	end
57
+
58
+	if participant_count > 1 then
59
+		participant_count = participant_count - 1;
60
+	end
61
+
62
+	local GET_response = {
63
+		headers = {
64
+			content_type = "application/json";
65
+		};
66
+		body = [[{"participants":]]..participant_count..[[}]];
67
+	};
68
+	return GET_response;
69
+end
70
+
71
+function handle_get_room (event)
72
+	local params = parse(event.request.url.query);
73
+	local room_name = params["room"];
74
+	local room = get_room_from_jid(room_name);
75
+	local participant_count = 0;
76
+	local occupants_json = array();
77
+
78
+	log("debug", "Querying room %s", tostring(room_name));
79
+
80
+	if room then
81
+		local occupants = room._occupants;
82
+		if occupants then
83
+			participant_count = iterators.count(room:each_occupant());
84
+			for _, occupant in room:each_occupant() do
85
+			    -- filter focus as we keep it as hidden participant
86
+			    if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
87
+				    for _, pr in occupant:each_session() do
88
+					local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "";
89
+					local email = pr:get_child_text("email") or "";
90
+					occupants_json:push({
91
+					    jid = tostring(occupant.nick),
92
+					    email = tostring(email),
93
+					    display_name = tostring(nick)});
94
+				    end
95
+			    end
96
+			end
97
+		end
98
+		log("debug", "there are %s occupants in room", tostring(participant_count));
99
+	else
100
+		log("debug", "no such room exists");
101
+	end
102
+
103
+	if participant_count > 1 then
104
+		participant_count = participant_count - 1;
105
+	end
106
+
107
+	local GET_response = {
108
+		headers = {
109
+			content_type = "application/json";
110
+		};
111
+		body = json.encode(occupants_json);
112
+	};
113
+	return GET_response;
114
+end;
115
+
116
+function module.add_host(module)
117
+	module:depends("http");
118
+	module:provides("http", {
119
+		default_path = "/";
120
+		route = {
121
+			["GET room-size"] = handle_get_room_size;
122
+			["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
123
+			["GET room"] = handle_get_room;
124
+		};
125
+	});
126
+end
127
+

正在加载...
取消
保存