SHOW:
|
|
- or go back to the newest paste.
| 1 | --os.pullEvent = os.pullEventRaw CC bug makes this break | |
| 2 | ||
| 3 | local modem = peripheral.find("modem")
| |
| 4 | local event, side, sChannel, id, data, dist | |
| 5 | local sessions = {}
| |
| 6 | ||
| 7 | modem.open(os.getComputerID()) | |
| 8 | ||
| 9 | local function send(to, msg) | |
| 10 | modem.transmit(to, os.getComputerID(), msg); | |
| 11 | end | |
| 12 | ||
| 13 | local function listen() | |
| 14 | event, side, sChannel, id, data, dist = os.pullEvent("modem_message")
| |
| 15 | end | |
| 16 | ||
| 17 | local function receive() | |
| 18 | modem.close(os.getComputerID()) | |
| 19 | local rand = math.random(60000, 65000) | |
| 20 | modem.transmit(id, rand) | |
| 21 | modem.open(rand) | |
| 22 | listen() | |
| 23 | modem.close(rand) | |
| 24 | modem.open(os.getComputerID()) | |
| 25 | end | |
| 26 | ||
| 27 | local function getSID() | |
| 28 | local rand = math.random(0, 10000) | |
| 29 | for index, tmp in pairs(sessions) do | |
| 30 | if tmp == rand then | |
| 31 | return getSID() | |
| 32 | end | |
| 33 | end | |
| 34 | return rand | |
| 35 | end | |
| 36 | ||
| 37 | local function indexOf(table, val) | |
| 38 | for index, tmp in pairs(table) do | |
| 39 | if tmp == val then | |
| 40 | return index | |
| 41 | end | |
| 42 | end | |
| 43 | return nil | |
| 44 | end | |
| 45 | ||
| 46 | local function login() | |
| 47 | receive() | |
| 48 | if not data:find(":") then
| |
| 49 | send(id, "MessageFormatException") | |
| 50 | return | |
| 51 | end | |
| 52 | local c = data:find(":")
| |
| 53 | local user = data:sub(0, c - 1) | |
| 54 | local pass = data:sub(c + 1) | |
| 55 | local file = fs.open("user/" .. user, "r")
| |
| 56 | if file ~= nil then | |
| 57 | if pass == file.readLine() then | |
| 58 | local sid = getSID() | |
| 59 | sessions[user] = sid; | |
| 60 | if fs.exists("admin/" .. user) then
| |
| 61 | send(id, "Login Successful:" .. sid .. ":") | |
| 62 | else | |
| 63 | send(id, "Login Successful:" .. sid) | |
| 64 | end | |
| 65 | else | |
| 66 | send(id, "Password Incorrect") | |
| 67 | end | |
| 68 | file.close() | |
| 69 | else | |
| 70 | send(id, "Unknown User") | |
| 71 | end | |
| 72 | end | |
| 73 | ||
| 74 | local function balance() | |
| 75 | receive() | |
| 76 | local user = indexOf(sessions, tonumber(data)) | |
| 77 | if user == nil then | |
| 78 | return | |
| 79 | end | |
| 80 | local file = fs.open("$/" .. user, "r")
| |
| 81 | send(id, file.readLine()) | |
| 82 | file.close() | |
| 83 | end | |
| 84 | ||
| 85 | local function transfer() | |
| 86 | receive() | |
| 87 | if not data:find(":") then
| |
| 88 | send(id, "MessageFormatException") | |
| 89 | return | |
| 90 | end | |
| 91 | local c1 = data:find(":")
| |
| 92 | local c2 = data:find(":", c1 + 1)
| |
| 93 | local user1 = indexOf(sessions, tonumber(data:sub(0, c1 - 1))) | |
| 94 | if user1 == nil then | |
| 95 | return | |
| 96 | end | |
| 97 | local user2 = data:sub(c1 + 1, c2 - 1) | |
| 98 | local amt = tonumber(data:sub(c2 + 1)) | |
| 99 | local f1 = fs.open("$/" .. user1, "r")
| |
| 100 | local f2 = fs.open("$/" .. user2, "r")
| |
| 101 | if f1 ~= nil and f2 ~= nil then | |
| 102 | local bal1 = tonumber(f1.readLine()); | |
| 103 | local bal2 = tonumber(f2.readLine()); | |
| 104 | if bal1 >= amt and amt > 0 then | |
| 105 | f1.close() | |
| 106 | f2.close() | |
| 107 | f1 = fs.open("$/" .. user1, "w")
| |
| 108 | f2 = fs.open("$/" .. user2, "w")
| |
| 109 | f1.write(bal1 - amt) | |
| 110 | f2.write(bal2 + amt) | |
| 111 | f1.close() | |
| 112 | f2.close() | |
| 113 | send(id, "Funds Transfered") | |
| 114 | else | |
| 115 | send(id, "Not enough funds or invalid amount") | |
| 116 | end | |
| 117 | else | |
| 118 | send(id, "User(s) Not Found") | |
| 119 | end | |
| 120 | end | |
| 121 | ||
| 122 | local function create() | |
| 123 | receive() | |
| 124 | if not data:find(":") then
| |
| 125 | send(id, "MessageFormatException") | |
| 126 | return | |
| 127 | end | |
| 128 | local c = data:find(":")
| |
| 129 | local user = data:sub(0, c - 1) | |
| 130 | local pass = data:sub(c + 1) | |
| 131 | if fs.exists("user/" .. user) then
| |
| 132 | send(id, "User Exists") | |
| 133 | else | |
| 134 | local file = fs.open("user/" .. user, "w")
| |
| 135 | file.write(pass) | |
| 136 | file.close() | |
| 137 | file = fs.open("$/" .. user, "w")
| |
| 138 | file.write("0")
| |
| 139 | file.close() | |
| 140 | send(id, "Account Created") | |
| 141 | end | |
| 142 | end | |
| 143 | ||
| 144 | local function add() | |
| 145 | receive() | |
| 146 | if not data:find(":") then
| |
| 147 | send(id, "MessageFormatException") | |
| 148 | return | |
| 149 | end | |
| 150 | local c1 = data:find(":")
| |
| 151 | local c2 = data:find(":", c1 + 1)
| |
| 152 | local user1 = indexOf(sessions, tonumber(data:sub(0, c1 - 1))) | |
| 153 | if user1 == nil then | |
| 154 | return | |
| 155 | end | |
| 156 | local user2 = data:sub(c1 + 1, c2 - 1) | |
| 157 | local amt = tonumber(data:sub(c2 + 1)) | |
| 158 | if fs.exists("admin/" .. user1) then
| |
| 159 | if fs.exists("user/".. user2) then
| |
| 160 | local file = fs.open("$/" .. user2, "r")
| |
| 161 | local bal = tonumber(file.readLine()) | |
| 162 | file.close() | |
| 163 | bal = bal + amt | |
| 164 | file = fs.open("$/" .. user2, "w")
| |
| 165 | file.write(bal) | |
| 166 | file.close() | |
| 167 | send(id, "$" .. amt .. " added to " .. user2) | |
| 168 | else | |
| 169 | send(id, "No Such User") | |
| 170 | end | |
| 171 | else | |
| 172 | send(id, "You are not an admin") | |
| 173 | end | |
| 174 | end | |
| 175 | ||
| 176 | local function delete() | |
| 177 | receive() | |
| 178 | if not data:find(":") then
| |
| 179 | send(id, "MessageFormatException") | |
| 180 | return | |
| 181 | end | |
| 182 | local c = data:find(":")
| |
| 183 | local user1 = indexOf(sessions, tonumber(data:sub(0, c - 1))) | |
| 184 | if user1 == nil then | |
| 185 | return | |
| 186 | end | |
| 187 | if fs.exists("admin/" .. user1) then
| |
| 188 | local user2 = data:sub(c + 1) | |
| 189 | fs.delete("user/" .. user2)
| |
| 190 | fs.delete("$/" .. user2)
| |
| 191 | fs.delete("admin/" .. user2)
| |
| 192 | send(id, "User deleted") | |
| 193 | else | |
| 194 | send(id, "You are not an admin") | |
| 195 | end | |
| 196 | end | |
| 197 | ||
| 198 | local function chmod() | |
| 199 | receive() | |
| 200 | if not data:find(":") then
| |
| 201 | send(id, "MessageFormatException") | |
| 202 | return | |
| 203 | end | |
| 204 | local c1 = data:find(":")
| |
| 205 | local c2 = data:find(":", c1 + 1)
| |
| 206 | local user1 = indexOf(sessions, tonumber(data:sub(0, c1 - 1))) | |
| 207 | if user1 == nil then | |
| 208 | return | |
| 209 | end | |
| 210 | if fs.exists("admin/" .. user1) then
| |
| 211 | local user2 = data:sub(c1 + 1, c2 - 1) | |
| 212 | local status = data:sub(c2 + 1) | |
| 213 | if fs.exists("user/" .. user2) then
| |
| 214 | if status == "true" then | |
| 215 | fs.open("admin/" .. user2, "w").close()
| |
| 216 | else | |
| 217 | fs.delete("admin/" .. user2)
| |
| 218 | end | |
| 219 | send(id, "User Status Successfully Edited") | |
| 220 | else | |
| 221 | send(id, "No such user") | |
| 222 | end | |
| 223 | else | |
| 224 | send(id, "You are not an admin") | |
| 225 | end | |
| 226 | ||
| 227 | end | |
| 228 | ||
| 229 | local function logout() | |
| 230 | receive() | |
| 231 | local user = indexOf(sessions, tonumber(data)) | |
| 232 | if user == nil then | |
| 233 | return | |
| 234 | end | |
| 235 | sessions[user] = nil | |
| 236 | send(id, "You have been logged out!") | |
| 237 | end | |
| 238 | ||
| 239 | local function drawStart() | |
| 240 | term.clear() | |
| 241 | term.setCursorPos(1, 1) | |
| 242 | print("BlueBank Server 3.0 Status: Online")
| |
| 243 | print("--------------------------------------")
| |
| 244 | while true do | |
| 245 | listen() | |
| 246 | if data == "login" then | |
| 247 | login() | |
| 248 | elseif data == "balance" then | |
| 249 | balance() | |
| 250 | elseif data == "transfer" then | |
| 251 | transfer() | |
| 252 | elseif data == "create" then | |
| 253 | create() | |
| 254 | elseif data == "delete" then | |
| 255 | delete() | |
| 256 | elseif data == "rename" then | |
| 257 | rename() | |
| 258 | elseif data == "passcode" then | |
| 259 | passcode() | |
| 260 | elseif data == "add" then | |
| 261 | add() | |
| 262 | elseif data == "auth" then | |
| 263 | auth() | |
| 264 | elseif data == "logout" then | |
| 265 | logout() | |
| 266 | elseif data == "chmod" then | |
| 267 | chmod() | |
| 268 | else | |
| 269 | send(id, "Command Not Found") | |
| 270 | end | |
| 271 | end | |
| 272 | end | |
| 273 | ||
| 274 | drawStart() |