-
Notifications
You must be signed in to change notification settings - Fork 4
/
mod_hvl_muc.lua
293 lines (232 loc) · 8.18 KB
/
mod_hvl_muc.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
-- Prosody IM
-- Copyright (C) 2017 Atlassian
--
function get_log()
local log = { _version = "0.1.0" }
log.usecolor = true
log.outfile = nil
log.level = "trace"
local modes = {
{ name = "trace", color = "\27[34m", },
{ name = "debug", color = "\27[36m", },
{ name = "info", color = "\27[32m", },
{ name = "warn", color = "\27[33m", },
{ name = "error", color = "\27[31m", },
{ name = "fatal", color = "\27[35m", },
}
local levels = {}
for i, v in ipairs(modes) do
levels[v.name] = i
end
local round = function(x, increment)
increment = increment or 1
x = x / increment
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
end
local _tostring = tostring
local tostring = function(...)
local t = {}
for i = 1, select('#', ...) do
local x = select(i, ...)
if type(x) == "number" then
x = round(x, .01)
end
t[#t + 1] = _tostring(x)
end
return table.concat(t, " ")
end
for i, x in ipairs(modes) do
local nameupper = x.name:upper()
log[x.name] = function(...)
-- Return early if we're below the log level
if i < levels[log.level] then
return
end
local msg = tostring(...)
local info = debug.getinfo(2, "Sl")
local lineinfo = info.short_src .. ":" .. info.currentline
-- Output to console
print(string.format("%s[%-6s%s]%s %s: %s",
log.usecolor and x.color or "",
nameupper,
os.date("%H:%M:%S"),
log.usecolor and "\27[0m" or "",
lineinfo,
msg))
-- Output to log file
if log.outfile then
local fp = io.open(log.outfile, "a")
local str = string.format("[%-6s%s] %s\n",
nameupper, os.date("%m/%d/%Y %H:%M:%S"), msg)
fp:write(str)
fp:close()
end
end
end
return log
end
local log = get_log();
log.outfile = "hvl_muc.log";
local debug_log = get_log();
debug_log.outfile = "hvl_muc.debug.log";
local tostring = tostring;
local function starts_with(str, start)
return str:sub(1, #start) == start
end
local driver = require "luasql.sqlite3"
env = driver.sqlite3()
con = env:connect("logs.db")
res = con:execute[[
CREATE TABLE IF NOT EXISTS rooms(
jid varchar(255) NOT NULL PRIMARY KEY,
name varchar(255),
password varchar(255),
created_at varchar(255)
)
]]
res = con:execute[[
CREATE TABLE IF NOT EXISTS room_occupants(
jid varchar(255) NOT NULL,
room_jid varchar(255) NOT NULL,
email varchar(255),
display_name varchar(255),
created_at varchar(255),
PRIMARY KEY (room_jid, display_name)
)
]]
function room_created(event)
debug_log.info("room_created ok");
local room = event.room;
if starts_with(room:get_name(), "org.jitsi.jicofo.health.health") then
debug_log.info("room org.jitsi.jicofo.health.health ignored")
return
end
log.info(string.format("room created: room=%s, room_jid=%s", room:get_name(), room.jid));
res = con:execute(string.format([[
INSERT INTO rooms
VALUES ('%s', '%s', '%s', '%s')]],
room.jid,
room:get_name(),
room:get_password() or "",
tostring(room.created_timestamp or os.time(os.date("!*t")) * 1000))
)
res = con:execute(string.format([[DELETE FROM room_occupants WHERE room_jid='%s']],
room.jid))
debug_log.info(string.format([[room added INSERT INTO rooms VALUES ('%s', '%s', '%s', '%s')]],
room.jid,
room:get_name(),
room:get_password() or "",
tostring(room.created_timestamp or os.time(os.date("!*t")) * 1000)));
end
function room_destroyed(event)
debug_log.info("room_destroyed ok");
local room = event.room;
if starts_with(room:get_name(), "org.jitsi.jicofo.health.health") then
debug_log.info("room org.jitsi.jicofo.health.health ignored")
return
end
log.info(string.format("room destroyed: room=%s, room_jid=%s", room:get_name(), room.jid));
end
function occupant_joined(event)
debug_log.info("occupant_joined ok");
local room = event.room;
if starts_with(room:get_name(), "org.jitsi.jicofo.health.health") then
debug_log.info("room org.jitsi.jicofo.health.health ignored")
return
end
local occupant = event.occupant;
if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
for _, pr in occupant:each_session() do
local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "";
if nick~="" then
local email = pr:get_child_text("email") or "";
cur = con:execute(string.format("SELECT COUNT(*) as count FROM room_occupants WHERE room_jid='%s' AND jid='%s'", room.jid, tostring(occupant.nick)));
if tonumber(cur:fetch({}, "a").count) > 0 then
cur = con:execute(string.format("SELECT * FROM room_occupants WHERE room_jid='%s' AND jid='%s'", room.jid, tostring(occupant.nick)));
old_room = cur:fetch({}, "a");
debug_log.info(string.format("occupant changed old name %s new name %s", old_room.display_name, tostring(nick)))
if old_room.display_name~=tostring(nick) then
debug_log.info("occupant changed if check ok")
log.info(string.format("occupant changed username: room=%s, room_jid=%s, user_jid=%s, nick=%s, old_nick=%s", room:get_name(), room.jid, tostring(occupant.nick), tostring(nick), old_room.display_name));
end
res = assert(con:execute(string.format([[
UPDATE room_occupants
SET email='%s', display_name='%s' WHERE room_jid='%s' AND jid='%s']],
tostring(email),
tostring(nick),
room.jid,
tostring(occupant.nick)
)))
debug_log.info(string.format([[occupant changed UPDATE room_occupants SET email='%s', display_name='%s' WHERE room_jid='%s' AND jid='%s')]],
tostring(email),
tostring(nick),
room.jid,
tostring(occupant.nick)));
else
res = con:execute(string.format([[
INSERT INTO room_occupants
VALUES ('%s', '%s', '%s', '%s', '%s')]],
tostring(occupant.nick),
room.jid,
tostring(email),
tostring(nick),
tostring(os.time(os.date("!*t")) * 1000) or ""
))
debug_log.info(string.format([[occupant added INSERT INTO room_occupants VALUES ('%s', '%s', '%s', '%s')]],
tostring(occupant.nick),
room.jid,
tostring(email),
tostring(nick)));
end
end
end
end
end
function occupant_joined_log(event)
debug_log.info("occupant_joined_log ok");
local room = event.room;
if starts_with(room:get_name(), "org.jitsi.jicofo.health.health") then
debug_log.info("room org.jitsi.jicofo.health.health ignored")
return
end
local occupant = event.occupant;
if occupant then
if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
for _, pr in occupant:each_session() do
local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "no_name";
log.info(string.format("occupant joined: room=%s, room_jid=%s, user_jid=%s, nick=%s", room:get_name(), room.jid, tostring(occupant.nick), tostring(nick)));
end
end
end
end
function occupant_left_log(event)
debug_log.info("occupant_left_log ok");
local room = event.room;
if starts_with(room:get_name(), "org.jitsi.jicofo.health.health") then
debug_log.info("room org.jitsi.jicofo.health.health ignored")
return
end
local occupant = event.occupant;
if string.sub(occupant.nick,-string.len("/focus"))~="/focus" then
res = con:execute(string.format([[DELETE FROM room_occupants WHERE room_jid = '%s' AND jid = '%s']],
room.jid,
tostring(occupant.nick)))
debug_log.info(string.format([[occupant left DELETE FROM room_occupants WHERE room_jid = '%s' AND jid = '%s')]],
room.jid,
tostring(occupant.nick)));
for _, pr in occupant:each_session() do
local nick = pr:get_child_text("nick", "http://jabber.org/protocol/nick") or "no_name";
log.info(string.format("occupant left: room=%s, room_jid=%s, user_jid=%s, nick=%s", room:get_name(), room.jid, tostring(occupant.nick), tostring(nick)));
end
end
end
function module.load()
module:hook("muc-room-created", room_created, -1);
module:hook("muc-room-created", occupant_joined_log, -1);
module:hook("muc-room-destroyed", room_destroyed, -1);
module:hook("muc-occupant-joined", occupant_joined, -1);
module:hook("muc-occupant-joined", occupant_joined_log, -1);
module:hook("muc-occupant-pre-leave", occupant_left_log, -1);
module:hook("muc-broadcast-presence", occupant_joined, -1);
debug_log.info("hooks ok ",module.host);
end