-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIrcBridge.lua
164 lines (144 loc) · 5.82 KB
/
IrcBridge.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
-- IrcBridge.lua -*-lua-*-
-- "THE BEER-WARE LICENCE" (Revision 42):
-- <[email protected]> wrote this file. As long as you retain
-- this notice you can do whatever you want with this stuff. If we meet
-- some day, and you think this stuff is worth it, you can buy me a beer
-- in return. Michael Fitzmayer
require("color")
local irc = require("irc")
local goTES3MPConfig = require("custom.goTES3MP.config")
local goTES3MPSync = require("custom.goTES3MP.sync")
local goTES3MPModules = nil
local IrcBridge = {}
IrcBridge.version = "v0.4.1-goTES3MP"
IrcBridge.scriptName = "IrcBridge"
IrcBridge.debugMode = false
IrcBridge.maxMessageLength = 2048
local config = goTES3MPConfig.GetConfig()
if (config.IRCBridge.nick == "" or config.IRCBridge.systemchannel == "" or config.IRCBridge.systemchannel == "#") then
tes3mp.LogMessage(
enumerations.log.ERROR,
"IrcBridge has not been configured correctly."
)
tes3mp.StopServer(0)
end
IRCTimerId = nil
local s = irc.new {nick = config.IRCBridge.nick}
if config.IRCBridge.password ~= "" then
s:connect(
{
host = config.IRCBridge.server,
port = config.IRCBridge.port,
password = config.IRCBridge.password,
timeout = 120,
secure = false
}
)
else
s:connect(config.IRCBridge.server, config.IRCBridge.port)
end
local nspasswd = "identify " .. config.IRCBridge.nspasswd
local systemchannel = config.IRCBridge.systemchannel
s:sendChat("NickServ", nspasswd)
s:join(systemchannel)
local lastMessage = ""
IrcBridge.RecvMessage = function()
s:hook(
"OnChat",
function(user, systemchannel, message)
if message ~= lastMessage then
if IrcBridge.debugMode then
print("IRCDebug: " .. message)
end
local response = goTES3MPModules.utils.isJsonValidDecode(message)
if response ~= nil then
IrcBridge.switch(response.method) {
["Sync"] = function()
goTES3MPSync.gotSync(response.ServerID, response.SyncID)
end,
["Command"] = function()
local command = response.data.command
local commandArgs = goTES3MPModules.utils.isJsonValidDecode(response.data.commandArgs)
tes3mp.LogMessage(enumerations.log.INFO, "[GoTES3MP:Command] Executing command \"" .. command .. "\" with args {" .. tableHelper.getSimplePrintableTable(commandArgs).."}")
commandArgs["discordInteractiveToken"] = response.data["discordInteractiveToken"]
goTES3MPModules.commands.processCommand(command, commandArgs)
end,
["DiscordChat"] = function()
IrcBridge.chatMessage(response)
end,
["VPNCheck"] = function()
goTES3MPModules.vpnChecker.kickPlayer(response.data["playerpid"], response.data["kickPlayer"])
end,
default = function()
print("Error: "..tableHelper.getSimplePrintableTable(response))
print("Unknown method (" .. response.method .. ") was received.")
end,
}
end
end
lastMessage = message
end
)
tes3mp.RestartTimer(IRCTimerId, time.seconds(1))
end
IrcBridge.chatMessage = function(response)
local wherefrom = color.Default .. "[" .. response.source .. "]" .. color.Default
local finalMessage = ""
if response.method == "DiscordChat" then
wherefrom = config.IRCBridge.discordColor .. "[" .. response.source .. "]" .. color.Default
end
if response.data["RoleColor"] ~= "" and response.data["RoleColor"] ~= "" then
local staffRole = "#" .. response.data["RoleColor"] .. "[" .. response.data["RoleName"] .. "]" .. color.Default
finalMessage = wherefrom .. " " .. staffRole .. " " .. response.data["User"] .. ": " .. response.data["Message"] .. "\n"
else
finalMessage = wherefrom .. " " .. response.data["User"] .. ": " .. response.data["Message"] .. "\n"
end
for pid, player in pairs(Players) do
if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then
tes3mp.SendMessage(pid, finalMessage, false)
end
end
end
IrcBridge.SendSystemMessage = function(message)
if string.len(message) > IrcBridge.maxMessageLength then
tes3mp.LogMessage(enumerations.log.INFO, "[goTES3MP:IRCBridge] SendSystemMessage was skipped due to message length exceeding limit.")
return
end
if message ~= lastMessage then
s:sendChat(systemchannel, message)
lastMessage = message
end
end
function OnIRCUpdate()
IrcBridge.RecvMessage()
s:think()
end
-- Yes, i swapped out the chain of if statements for a switch statement, the performance loss is minimal
-- however it severely increases code readability
IrcBridge.switch = function(value)
return function(cases)
local case = cases[value] or cases.default
if case then
return case(value)
else
error(string.format("Unhandled case (%s)", value), 2)
end
end
end
customEventHooks.registerValidator(
"OnServerInit",
function()
IRCTimerId = tes3mp.CreateTimer("OnIRCUpdate", time.seconds(1))
tes3mp.LogMessage(enumerations.log.INFO, "[goTES3MP:IRCBridge] ".. IrcBridge.version.. " Loaded")
goTES3MPModules = goTES3MP.GetModules()
tes3mp.StartTimer(IRCTimerId)
end
)
customEventHooks.registerValidator(
"OnServerExit",
function()
tes3mp.StopTimer(IRCTimerId)
s:shutdown()
end
)
return IrcBridge