-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathircServerNew
More file actions
125 lines (111 loc) · 4.37 KB
/
ircServerNew
File metadata and controls
125 lines (111 loc) · 4.37 KB
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
-- IRC Server
local modem = peripheral.find("modem")
local computerId = os.getComputerID()
local _, h = term.getSize()
modem.open(1)
local serverName = "#" .. "general"
local logsFile = serverName .. ".txt"
local serverDiscoverable = true
-- Load last 20 chat messages to send to clients
local function loadHistory()
local history = {}
if fs.exists(logsFile) then
local file = fs.open(logsFile, "r")
while true do
local line = file.readLine()
if not line then break end
local message = textutils.unserializeJSON(line)
table.insert(history, message)
end
file.close()
else
return nil
end
-- Only return the last 20 messages
local total = #history
if total > 20 then
local recent = {}
for i = total - 19, total do
table.insert(recent, history[i])
end
return recent
else
return history
end
end
-- Main loop
while true do
local _, _, ch, _, data = os.pullEvent("modem_message")
if (ch == 1) then
if (data["protocol"] == "irc") then
if ((data["type"] == "serverSearch") and serverDiscoverable) then
local packet = {
protocol = "irc",
type = "serverResponse",
sender = computerId,
receiver = data["sender"],
channel = serverName,
data = nil
}
print("announcing presence to " .. data["sender"])
modem.transmit(0, 1, packet)
elseif (data["receiver"] == computerId) then
if (data["channel"] == serverName) then
-- Handle connection requests
if (data["type"] == "connectionRequest") then
local packet = {
protocol = "irc",
type = "connectionAccepted",
sender = computerId,
receiver = data["sender"],
channel = serverName,
data = nil
}
print("accepted connection from " .. data["sender"])
modem.transmit(0, 1, packet)
-- Handle requests for chat history
elseif (data["type"] == "historyRequest") then
local packet = {
protocol = "irc",
type = "historyResponse",
sender = computerId,
receiver = data["sender"],
channel = serverName,
data = loadHistory()
}
print("sending history to " .. data["sender"])
modem.transmit(0, 1, packet)
-- Handle new messages on the server
elseif (data["type"] == "messageOutgoing") then
local packet = {
protocol = "irc",
type = "messageAcknowledged",
sender = computerId,
receiver = data["sender"],
channel = serverName,
data = data["data"]
}
print("received message '" .. data["data"]["content"] .. "' from " .. data["sender"])
modem.transmit(0, 1, packet)
local packet = {
protocol = "irc",
type = "messageIncoming",
sender = computerId,
receiver = nil,
channel = serverName,
data = data["data"]
}
print("relayed message")
modem.transmit(0, 1, packet)
-- Save to logs
local message = data["data"]
message.id = data["sender"]
local file = fs.open(logsFile, "a")
file.writeLine(textutils.serializeJSON(message))
file.close()
end
end
end
end
end
end