-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathweb_chat.lua
370 lines (276 loc) · 11.5 KB
/
web_chat.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
local CHAT_HISTORY = 50
local LastMessageID = 0
local JavaScript = [[
<script type="text/javascript">
function createXHR()
{
var request = false;
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (err3) {
try {
request = new XMLHttpRequest();
}
catch (err1) {
request = false;
}
}
}
return request;
}
function OpenPage( url, postParams, callback )
{
var xhr = createXHR();
xhr.onreadystatechange=function()
{
if (xhr.readyState == 4)
{
callback( xhr );
}
}
xhr.open( (postParams!=null)?"POST":"GET", url , true);
if( postParams != null )
{
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhr.send(postParams);
}
function LoadPageInto( url, postParams, storage )
{
OpenPage( url, postParams, function( xhr )
{
var ScrollBottom = storage.scrollTop + storage.offsetHeight;
var bAutoScroll = (ScrollBottom >= storage.scrollHeight); // Detect whether we scrolled to the bottom of the div
results = xhr.responseText.split("<<divider>>");
if( results[2] != LastMessageID ) return; // Check if this message was meant for us
LastMessageID = results[1];
if( results[0] != "" )
{
storage.innerHTML += results[0];
if( bAutoScroll == true )
{
storage.scrollTop = storage.scrollHeight;
}
}
} );
return false;
}
function SendChatMessage()
{
var MessageContainer = document.getElementById('ChatMessage');
if( MessageContainer.value == "" ) return;
var postParams = "ChatMessage=" + MessageContainer.value;
OpenPage( "/~webadmin/Core/chat/", postParams, function( xhr )
{
RefreshChat();
} );
MessageContainer.value = "";
}
function RefreshChat()
{
var postParams = "JustChat=true&LastMessageID=" + LastMessageID;
LoadPageInto("/~webadmin/Core/chat/", postParams, document.getElementById('ChatDiv'));
}
setInterval(RefreshChat, 1000);
window.onload = RefreshChat;
var LastMessageID = 0;
</script>
]]
-- Array of {PluginName, FunctionName} tables
local OnWebChatCallbacks = {}
local ChatLogMessages = {}
local WebCommands = {}
local ltNormal = 1
local ltInfo = 2
local ltWarning = 3
local ltError = 4
-- Adds Webchat callback, plugins can return true to
-- prevent message from appearing / being processed
-- by further callbacks
-- OnWebChat(Username, Message)
function AddWebChatCallback(PluginName, FunctionName)
for k, v in pairs(OnWebChatCallbacks) do
if v[1] == PluginName and v[2] == FunctionName then
return false
end
end
table.insert(OnWebChatCallbacks, {PluginName, FunctionName})
return true
end
-- Removes webchat callback
function RemoveWebChatCallback(PluginName, FunctionName)
for i = #OnWebChatCallbacks, 0, -1 do
if OnWebChatCallbacks[i][1] == PluginName and OnWebChatCallbacks[i][2] == FunctionName then
table.remove(OnWebChatCallbacks, i)
return true
end
end
return false
end
-- Checks the chatlogs to see if the size gets too big.
function TrimWebChatIfNeeded()
while( #ChatLogMessages > CHAT_HISTORY ) do
table.remove( ChatLogMessages, 1 )
end
end
-- Adds a plain message to the chat log
-- The a_WebUserName parameter can be either a string(name) to send it to a certain webuser or nil to send it to every webuser.
function WEBLOG(a_Message, a_WebUserName)
LastMessageID = LastMessageID + 1
table.insert(ChatLogMessages, {timestamp = os.date("[%Y-%m-%d %H:%M:%S]", os.time()), webuser = a_WebUserName, message = a_Message, id = LastMessageID, logtype = ltNormal})
TrimWebChatIfNeeded()
end
-- Adds a yellow-ish message to the chat log.
-- The a_WebUserName parameter can be either a string(name) to send it to a certain webuser or nil to send it to every webuser.
function WEBLOGINFO(a_Message, a_WebUserName)
LastMessageID = LastMessageID + 1
table.insert(ChatLogMessages, {timestamp = os.date("[%Y-%m-%d %H:%M:%S]", os.time()), webuser = a_WebUserName, message = a_Message, id = LastMessageID, logtype = ltInfo})
TrimWebChatIfNeeded()
end
-- Adds a red message to the chat log
-- The a_WebUserName parameter can be either a string(name) to send it to a certain webuser or nil to send it to every webuser.
function WEBLOGWARN(a_Message, a_WebUserName)
LastMessageID = LastMessageID + 1
table.insert(ChatLogMessages, {timestamp = os.date("[%Y-%m-%d %H:%M:%S]", os.time()), webuser = a_WebUserName, message = a_Message, id = LastMessageID, logtype = ltWarning})
TrimWebChatIfNeeded()
end
-- Adds a message with a red background to the chat log
-- The a_WebUserName parameter can be either a string(name) to send it to a certain webuser or nil to send it to every webuser.
function WEBLOGERROR(a_Message, a_WebUserName)
LastMessageID = LastMessageID + 1
table.insert(ChatLogMessages, {timestamp = os.date("[%Y-%m-%d %H:%M:%S]", os.time()), webuser = a_WebUserName, message = a_Message, id = LastMessageID, logtype = ltError})
TrimWebChatIfNeeded()
end
-- This function allows other plugins to add new commands to the webadmin.
-- a_CommandString is the the way you call the command ("/help")
-- a_HelpString is the message that tells you what the command does ("Shows a list of all the possible commands")
-- a_PluginName is the name of the plugin binding the command ("Core")
-- a_CallbackName is the name if the function that will be called when the command is executed ("HandleWebHelpCommand")
function BindWebCommand(a_CommandString, a_HelpString, a_PluginName, a_CallbackName)
assert(type(a_CommandString) == 'string')
assert(type(a_PluginName) == 'string')
assert(type(a_CallbackName) == 'string')
-- Check if the command is already bound. Return false with an error message if.
for Idx, CommandInfo in ipairs(WebCommands) do
if (CommandInfo.Command == a_CommandString) then
return false, "That command is already bound to a plugin called \"" .. CommandInfo.PluginName .. "\"."
end
end
-- Insert the command into the array and return true
table.insert(WebCommands, {CommandString = a_CommandString, HelpString = a_HelpString, PluginName = a_PluginName, CallbackName = a_CallbackName})
return true
end
-- Used by the webadmin to use /help
function HandleWebHelpCommand(a_User, a_Message)
local Content = "Available Commands:"
for Idx, CommandInfo in ipairs(WebCommands) do
if (CommandInfo.HelpString ~= "") then
Content = Content .. '<br />' .. CommandInfo.CommandString .. '  -  ' .. CommandInfo.HelpString
end
end
WEBLOG(Content, a_User)
return true
end
-- Used by the webadmin to reload the server
function HandleWebReloadCommand(a_User, a_Message)
cPluginManager:Get():ReloadPlugins()
WEBLOG("Reloading Plugins", a_User)
return true
end
-- Register some basic commands
BindWebCommand("/help", "Shows a list of all the possible commands", "Core", "HandleWebHelpCommand")
BindWebCommand("/reload", "Reloads all the plugins", "Core", "HandleWebReloadCommand")
-- Add a chatmessage from a player to the chatlog
function OnChat(a_Player, a_Message)
WEBLOG("[" .. a_Player:GetName() .. "]: " .. a_Message)
end
function OnPlayerJoined_WebChat(Player)
WEBLOGINFO(Player:GetName() .. " has joined the game")
end
function OnDisconnect(a_Player)
WEBLOGINFO(a_Player:GetName() .. " has left the game")
end
--- Removes html tags
--- Creates a tag when http(s) links are send.
--- It does this by selecting all the characters between "http(s)://" and a space, and puts an anker tag around it.
local function ParseMessage(a_Message)
local function PlaceString(a_Url)
return '<a href="' .. a_Url .. '" target="_blank">' .. a_Url .. '</a>'
end
a_Message = a_Message:gsub("<", "<"):gsub(">", ">"):gsub("=", "="):gsub('"', """):gsub("'", "'"):gsub("&", "&")
a_Message = a_Message:gsub('http://[^%s]+', PlaceString):gsub('https://[^%s]+', PlaceString)
return a_Message
end
function HandleRequest_Chat( Request )
-- The webadmin asks if there are new messages.
if( Request.PostParams["JustChat"] ~= nil ) then
local LastIdx = tonumber(Request.PostParams["LastMessageID"] or 0) or 0
local Content = ""
-- Go through each message to see if they are older then the last message, and add them to the content
for key, MessageInfo in pairs(ChatLogMessages) do
if( MessageInfo.id > LastIdx ) then
if (not MessageInfo.webuser or (MessageInfo.webuser == Request.Username)) then
local Message = MessageInfo.timestamp .. ' ' .. ParseMessage(MessageInfo.message)
if (MessageInfo.logtype == ltNormal) then
Content = Content .. Message .. "<br />"
elseif (MessageInfo.logtype == ltInfo) then
Content = Content .. '<span style="color: #FE9A2E;">' .. Message .. '</span><br />'
elseif (MessageInfo.logtype == ltWarning) then
Content = Content .. '<span style="color: red;">' .. Message .. '</span><br />'
elseif (MessageInfo.logtype == ltError) then
Content = Content .. '<span style="background-color: red; color: black;">' .. Message .. '</span><br />'
end
end
end
end
Content = Content .. "<<divider>>" .. LastMessageID .. "<<divider>>" .. LastIdx
return Content
end
-- Check if the webuser send a chat message.
if( Request.PostParams["ChatMessage"] ~= nil ) then
local Split = StringSplit(Request.PostParams["ChatMessage"])
local CommandExecuted = false
-- Check if the message was actualy a command
for Idx, CommandInfo in ipairs(WebCommands) do
if (CommandInfo.CommandString == Split[1]) then
-- cPluginManager:CallPlugin doesn't support calling yourself, so we have to check if the command is from the Core.
if (CommandInfo.PluginName == "Core") then
if (not _G[CommandInfo.CallbackName](Request.Username, Request.PostParams["ChatMessage"])) then
WEBLOG("Something went wrong while calling \"" .. CommandInfo.CallbackName .. "\" From \"" .. CommandInfo.PluginName .. "\".", Request.Username)
end
else
if (not cPluginManager:CallPlugin(CommandInfo.PluginName, CommandInfo.CallbackName, Request.Username, Request.PostParams["ChatMessage"])) then
WEBLOG("Something went wrong while calling \"" .. CommandInfo.CallbackName .. "\" From \"" .. CommandInfo.PluginName .. "\".", Request.Username)
end
end
return ""
end
end
-- If the message starts with a '/' then the message is a command, but since it wasn't executed a few lines above the command didn't exist
if (Request.PostParams["ChatMessage"]:sub(1, 1) == "/") then
WEBLOG('Unknown Command "' .. Request.PostParams["ChatMessage"] .. '"', nil)
return ""
end
-- Broadcast the message to the server
for k, v in pairs(OnWebChatCallbacks) do
if cPluginManager:CallPlugin(v[1], v[2], Request.Username, Request.PostParams["ChatMessage"]) then
return ""
end
end
cRoot:Get():BroadcastChat(cCompositeChat("[WEB] <" .. Request.Username .. "> " .. Request.PostParams["ChatMessage"]):UnderlineUrls())
-- Add the message to the chatlog
WEBLOG("[WEB] [" .. Request.Username .. "]: " .. Request.PostParams["ChatMessage"])
return ""
end
local Content = JavaScript
Content = Content .. [[
<div style="font-family: Courier; border: 1px solid #DDD; padding: 10px; width: 97%; height: 400px; overflow: scroll;" id="ChatDiv"></div>
<input type="text" id="ChatMessage" onKeyPress="if (event.keyCode == 13) { SendChatMessage(); }"><input type="submit" value="Submit" onClick="SendChatMessage();">
]]
return Content
end