SHOW:
|
|
- or go back to the newest paste.
1 | - | fs = require("filesystem") |
1 | + | fs = require("filesystem") |
2 | - | component = require("component") |
2 | + | component = require("component") |
3 | - | jenisis = require("jenisis") |
3 | + | jenisis = require("jenisis") |
4 | - | modem = component.getPrimary("modem") |
4 | + | modem = component.getPrimary("modem") |
5 | - | textutils = require("serialization") |
5 | + | textutils = require("serialization") |
6 | - | event = require("event") |
6 | + | event = require("event") |
7 | - | term = require("term") |
7 | + | term = require("term") |
8 | - | |
8 | + | |
9 | - | function broadcast(packet) |
9 | + | function broadcast(packet) |
10 | - | packet_ = textutils.serialize(packet) |
10 | + | packet_ = textutils.serialize(packet) |
11 | - | |
11 | + | |
12 | - | modem.broadcast(1024, packet_) |
12 | + | modem.broadcast(1024, packet_) |
13 | end | |
14 | - | |
14 | + | |
15 | - | function receive() |
15 | + | function receive() |
16 | - | event_ = {event.pull("modem_message")} |
16 | + | event_ = {event.pull("modem_message")} |
17 | - | |
17 | + | |
18 | - | return textutils.unserialize(event_[6]) |
18 | + | return textutils.unserialize(event_[6]) |
19 | end | |
20 | - | |
20 | + | |
21 | - | function check(accName) |
21 | + | function check(accName) |
22 | - | if fs.exists("/acc/"..accName) then |
22 | + | if fs.exists("/acc/"..accName) then |
23 | - | print("Checked for "..accName..", found") |
23 | + | print("Checked for "..accName..", found") |
24 | - | return true |
24 | + | return true |
25 | - | else |
25 | + | else |
26 | - | print("Checked for "..accName..", not found") |
26 | + | print("Checked for "..accName..", not found") |
27 | - | return false |
27 | + | return false |
28 | - | end |
28 | + | end |
29 | end | |
30 | - | |
30 | + | |
31 | - | function pullInfo(accName) |
31 | + | function pullInfo(accName) |
32 | - | if not check(accName) then |
32 | + | if not check(accName) then |
33 | - | return false |
33 | + | return false |
34 | - | end |
34 | + | end |
35 | - | |
35 | + | |
36 | - | file = io.open("/acc/"..accName) |
36 | + | file = io.open("/acc/"..accName) |
37 | - | acctable = textutils.unserialize(file:read("*a")) |
37 | + | acctable = textutils.unserialize(file:read("*a")) |
38 | - | file:close() |
38 | + | file:close() |
39 | - | |
39 | + | |
40 | - | print("Pulled info for "..accName) |
40 | + | print("Pulled info for "..accName) |
41 | - | return acctable |
41 | + | return acctable |
42 | end | |
43 | - | |
43 | + | |
44 | - | function writeInfo(acctable) |
44 | + | function writeInfo(acctable) |
45 | - | if not check(acctable.accountName) then |
45 | + | if not check(acctable.accountName) then |
46 | - | return false |
46 | + | return false |
47 | - | end |
47 | + | end |
48 | - | |
48 | + | |
49 | - | file = io.open("/acc/"..acctable.accountName, "w") |
49 | + | file = io.open("/acc/"..acctable.accountName, "w") |
50 | - | file:write(textutils.serialize(acctable)) |
50 | + | file:write(textutils.serialize(acctable)) |
51 | - | file:close() |
51 | + | file:close() |
52 | - | |
52 | + | |
53 | - | print("Wrote info for "..acctable.accountName) |
53 | + | print("Wrote info for "..acctable.accountName) |
54 | - | return true |
54 | + | return true |
55 | end | |
56 | - | |
56 | + | |
57 | - | function flag(accountname) |
57 | + | function flag(accountname) |
58 | - | if not check(accountname) then |
58 | + | if not check(accountname) then |
59 | - | return false |
59 | + | return false |
60 | - | end |
60 | + | end |
61 | - | |
61 | + | |
62 | - | accTable = pullInfo(accountname) |
62 | + | accTable = pullInfo(accountname) |
63 | - | |
63 | + | |
64 | - | if accTable.isFlagged then |
64 | + | if accTable.isFlagged then |
65 | - | accTable.isFlagged = false |
65 | + | accTable.isFlagged = false |
66 | - | else |
66 | + | else |
67 | - | accTable.isFlagged = true |
67 | + | accTable.isFlagged = true |
68 | - | end |
68 | + | end |
69 | - | |
69 | + | |
70 | - | writeInfo(accTable) |
70 | + | writeInfo(accTable) |
71 | - | |
71 | + | |
72 | - | print("Modified flag state for "..accountname..", now "..tostring(accTable.isFlagged)) |
72 | + | print("Modified flag state for "..accountname..", now "..tostring(accTable.isFlagged)) |
73 | - | return true |
73 | + | return true |
74 | end | |
75 | - | |
75 | + | |
76 | - | function recordTrans(flagee, addendum) |
76 | + | function recordTrans(flagee, addendum) |
77 | - | if not fs.exists("/history/"..flagee) then |
77 | + | if not fs.exists("/history/"..flagee) then |
78 | - | mode = "w" |
78 | + | mode = "w" |
79 | - | else |
79 | + | else |
80 | - | mode = "a" |
80 | + | mode = "a" |
81 | - | end |
81 | + | end |
82 | - | |
82 | + | |
83 | - | file = io.open("/history/"..flagee, mode) |
83 | + | file = io.open("/history/"..flagee, mode) |
84 | - | file:write(flagee..addendum) |
84 | + | file:write(flagee..addendum) |
85 | - | file:close() |
85 | + | file:close() |
86 | - | |
86 | + | |
87 | - | print("Recorded transaction") |
87 | + | print("Recorded transaction") |
88 | - | return true |
88 | + | return true |
89 | end | |
90 | - | |
90 | + | |
91 | - | function transfer(accSendName, accTakeName, amount) |
91 | + | function transfer(accSendName, accTakeName, amount) |
92 | - | if not check(accSendName.accountName) or not check(accTakeName) then |
92 | + | if not check(accSendName.accountName) or not check(accTakeName) then |
93 | - | print("Rejected transfer for nonexistent account") |
93 | + | print("Rejected transfer for nonexistent account") |
94 | - | return false |
94 | + | return false |
95 | - | end |
95 | + | end |
96 | - | |
96 | + | |
97 | - | accSend = pullInfo(accSendName.accountName) |
97 | + | accSend = pullInfo(accSendName.accountName) |
98 | - | accTake = pullInfo(accTakeName) |
98 | + | accTake = pullInfo(accTakeName) |
99 | - | govAcc = pullInfo("government") |
99 | + | govAcc = pullInfo("government") |
100 | - | |
100 | + | |
101 | - | if accSend.balance < amount then |
101 | + | if accSend.balance < amount then |
102 | - | print("Transfer rejected for too low balance") |
102 | + | print("Transfer rejected for too low balance") |
103 | - | return false |
103 | + | return false |
104 | - | elseif amount < 0 then |
104 | + | elseif amount < 0 then |
105 | - | print("Transfer rejected for negative transaction") |
105 | + | print("Transfer rejected for negative transaction") |
106 | - | return false |
106 | + | return false |
107 | - | elseif accSend.accountKey ~= accSendName.accountKey then |
107 | + | elseif accSend.accountKey ~= accSendName.accountKey then |
108 | - | print("Transfer rejected for failed authentications") |
108 | + | print("Transfer rejected for failed authentications") |
109 | - | return false |
109 | + | return false |
110 | - | end |
110 | + | end |
111 | - | |
111 | + | |
112 | - | if accSend.isFlagged then |
112 | + | if accSend.isFlagged then |
113 | - | recordTrans(accSend.accountName, " ("..amount..") -> "..accTake.accountName) |
113 | + | recordTrans(accSend.accountName, " ("..amount..") -> "..accTake.accountName) |
114 | - | elseif accTake.isFlagged then |
114 | + | elseif accTake.isFlagged then |
115 | - | recordTrans(accTake.accountName, " ("..amount..") <- "..accSend.accountName) |
115 | + | recordTrans(accTake.accountName, " ("..amount..") <- "..accSend.accountName) |
116 | - | end |
116 | + | end |
117 | - | |
117 | + | |
118 | - | if not accTake.isFront then |
118 | + | if not accTake.isFront then |
119 | - | accSend.balance = accSend.balance - amount |
119 | + | accSend.balance = accSend.balance - amount |
120 | - | accTake.balance = accTake.balance + (amount * 0.95) |
120 | + | accTake.balance = accTake.balance + (amount * 0.95) |
121 | - | govAcc.balance = govAcc.balance + (amount * 0.05) |
121 | + | govAcc.balance = govAcc.balance + (amount * 0.05) |
122 | - | |
122 | + | |
123 | - | writeInfo(accSend) |
123 | + | writeInfo(accSend) |
124 | - | writeInfo(accTake) |
124 | + | writeInfo(accTake) |
125 | - | writeInfo(govAcc) |
125 | + | writeInfo(govAcc) |
126 | - | |
126 | + | |
127 | - | print("Did succesful transfer") |
127 | + | print("Did succesful transfer") |
128 | - | return true |
128 | + | return true |
129 | - | else |
129 | + | else |
130 | - | owners = accTake.owners |
130 | + | owners = accTake.owners |
131 | - | |
131 | + | |
132 | - | for k, v in pairs(owners) do |
132 | + | for k, v in pairs(owners) do |
133 | - | transfer(accSend, k, (amount * v)) |
133 | + | transfer(accSend, k, (amount * v)) |
134 | - | end |
134 | + | end |
135 | - | |
135 | + | |
136 | - | return true |
136 | + | return true |
137 | - | end |
137 | + | end |
138 | end | |
139 | - | |
139 | + | |
140 | - | function newAccount(accTable) |
140 | + | function newAccount(accTable) |
141 | - | for k, v in pairs(accTable) do |
141 | + | for k, v in pairs(accTable) do |
142 | - | if v == nil or v == "" then |
142 | + | if v == nil or v == "" then |
143 | - | return false |
143 | + | return false |
144 | - | end |
144 | + | end |
145 | - | end |
145 | + | end |
146 | - | |
146 | + | |
147 | - | if check(accTable.accountName) then |
147 | + | if check(accTable.accountName) then |
148 | - | print("Failed creation for "..accTable.accountName..", already exists") |
148 | + | print("Failed creation for "..accTable.accountName..", already exists") |
149 | - | return false |
149 | + | return false |
150 | - | else |
150 | + | else |
151 | - | file = io.open("/acc/"..accTable.accountName, "w") |
151 | + | file = io.open("/acc/"..accTable.accountName, "w") |
152 | - | file:write(textutils.serialize(accTable)) |
152 | + | file:write(textutils.serialize(accTable)) |
153 | - | file:close() |
153 | + | file:close() |
154 | - | |
154 | + | |
155 | - | print("Made new account titled "..accTable.accountName) |
155 | + | print("Made new account titled "..accTable.accountName) |
156 | - | return true |
156 | + | return true |
157 | - | end |
157 | + | end |
158 | end | |
159 | - | |
159 | + | |
160 | - | function getMoney(accTable) |
160 | + | function getMoney(accTable) |
161 | - | if not check(accTable.accountName) then |
161 | + | if not check(accTable.accountName) then |
162 | - | return false |
162 | + | return false |
163 | - | end |
163 | + | end |
164 | - | |
164 | + | |
165 | - | account = pullInfo(accTable.accountName) |
165 | + | account = pullInfo(accTable.accountName) |
166 | - | if account.accountKey ~= accTable.accountKey then |
166 | + | if account.accountKey ~= accTable.accountKey then |
167 | - | return false |
167 | + | return false |
168 | - | else |
168 | + | else |
169 | - | print("Pulled account balance for "..accTable.accountName) |
169 | + | print("Pulled account balance for "..accTable.accountName) |
170 | - | return account.balance |
170 | + | return account.balance |
171 | - | end |
171 | + | end |
172 | end | |
173 | - | |
173 | + | |
174 | - | function doFront(accTable) |
174 | + | function doFront(accTable) |
175 | - | if not check(accTable.accountName) then |
175 | + | if not check(accTable.accountName) then |
176 | - | return false |
176 | + | return false |
177 | - | end |
177 | + | end |
178 | - | |
178 | + | |
179 | - | acc = pullInfo(accTable.accountName) |
179 | + | acc = pullInfo(accTable.accountName) |
180 | - | |
180 | + | |
181 | - | if acc.accountKey ~= accTable.accountKey then |
181 | + | if acc.accountKey ~= accTable.accountKey then |
182 | - | return false |
182 | + | return false |
183 | - | else |
183 | + | else |
184 | - | acc.owners = accTable.owners |
184 | + | acc.owners = accTable.owners |
185 | - | end |
185 | + | end |
186 | - | |
186 | + | |
187 | - | total = 0 |
187 | + | total = 0 |
188 | - | for k, v in pairs(acc.owners) do |
188 | + | for k, v in pairs(acc.owners) do |
189 | - | if not check(k) then |
189 | + | if not check(k) then |
190 | - | return false |
190 | + | return false |
191 | - | end |
191 | + | end |
192 | - | |
192 | + | |
193 | - | total = total + v |
193 | + | total = total + v |
194 | - | end |
194 | + | end |
195 | - | |
195 | + | |
196 | - | if total ~= 1 then |
196 | + | if total ~= 1 then |
197 | - | print("Failed to set up front for "..acc.accountName..", not equal to 1") |
197 | + | print("Failed to set up front for "..acc.accountName..", not equal to 1") |
198 | - | return false |
198 | + | return false |
199 | - | else |
199 | + | else |
200 | - | acc.isFront = true |
200 | + | acc.isFront = true |
201 | - | writeInfo(acc) |
201 | + | writeInfo(acc) |
202 | - | print("Set up front for "..acc.accountName) |
202 | + | print("Set up front for "..acc.accountName) |
203 | - | return true |
203 | + | return true |
204 | - | end |
204 | + | end |
205 | end | |
206 | - | |
206 | + | |
207 | - | function getInfo(acctable) |
207 | + | function getInfo(acctable) |
208 | - | if not check(acctable.accountName) then |
208 | + | if not check(acctable.accountName) then |
209 | - | return false |
209 | + | return false |
210 | - | end |
210 | + | end |
211 | - | |
211 | + | |
212 | - | newacc = pullInfo(acctable.accountName) |
212 | + | newacc = pullInfo(acctable.accountName) |
213 | - | |
213 | + | |
214 | - | if acctable.accountKey == newacc.accountKey then |
214 | + | if acctable.accountKey == newacc.accountKey then |
215 | - | print("Grabbed info for "..acctable.accountName) |
215 | + | print("Grabbed info for "..acctable.accountName) |
216 | - | return newacc |
216 | + | return newacc |
217 | - | else |
217 | + | else |
218 | - | print("Attempted info grab for "..acctable.accountName..", authentication failed.") |
218 | + | print("Attempted info grab for "..acctable.accountName..", authentication failed.") |
219 | - | return false |
219 | + | return false |
220 | - | end |
220 | + | end |
221 | end | |
222 | - | |
222 | + | |
223 | - | function doOrder(account, amount, adminpass) |
223 | + | function doOrder(account, amount, adminpass) |
224 | - | if not check(account) then |
224 | + | if not check(account) then |
225 | - | return false |
225 | + | return false |
226 | - | end |
226 | + | end |
227 | - | |
227 | + | |
228 | - | if adminpass ~= "th1s1smy@dm1np@ssw0rd" then |
228 | + | if adminpass ~= "th1s1smy@dm1np@ssw0rd" then |
229 | - | return false |
229 | + | return false |
230 | - | end |
230 | + | end |
231 | - | |
231 | + | |
232 | - | accTable = pullInfo(account) |
232 | + | accTable = pullInfo(account) |
233 | - | accTable.balance = accTable.balance + amount |
233 | + | accTable.balance = accTable.balance + amount |
234 | - | writeInfo(accTable) |
234 | + | writeInfo(accTable) |
235 | - | |
235 | + | |
236 | - | print("Did order to "..account) |
236 | + | print("Did order to "..account) |
237 | - | return true |
237 | + | return true |
238 | end | |
239 | - | |
239 | + | |
240 | - | function auth(accTable) |
240 | + | function auth(accTable) |
241 | - | if not check(accTable.accountName) then |
241 | + | if not check(accTable.accountName) then |
242 | - | return false |
242 | + | return false |
243 | - | end |
243 | + | end |
244 | - | |
244 | + | |
245 | - | acctable = pullInfo(accTable.accountName) |
245 | + | acctable = pullInfo(accTable.accountName) |
246 | - | |
246 | + | |
247 | - | if acctable.accountKey == accTable.accountKey then |
247 | + | if acctable.accountKey == accTable.accountKey then |
248 | - | print("Verified authentication for "..acctable.accountName) |
248 | + | print("Verified authentication for "..acctable.accountName) |
249 | - | return true |
249 | + | return true |
250 | - | else |
250 | + | else |
251 | - | print("Failed authentication for "..accTable.accountName) |
251 | + | print("Failed authentication for "..accTable.accountName) |
252 | - | return false |
252 | + | return false |
253 | - | end |
253 | + | end |
254 | end | |
255 | - | |
255 | + | |
256 | - | term.clear() |
256 | + | term.clear() |
257 | - | print("Starting banking server. STOP LOOKING AT ME >:[") |
257 | + | print("Starting banking server. STOP LOOKING AT ME >:[") |
258 | - | |
258 | + | |
259 | - | while true do |
259 | + | while true do |
260 | - | |
260 | + | |
261 | - | message = receive() |
261 | + | message = receive() |
262 | - | |
262 | + | |
263 | - | print("Message received: ") |
263 | + | print("Message received: ") |
264 | - | for k, v in ipairs(message) do |
264 | + | for k, v in ipairs(message) do |
265 | - | print(k, v) |
265 | + | print(k, v) |
266 | - | |
266 | + | |
267 | - | if type(v) == "table" then |
267 | + | if type(v) == "table" then |
268 | - | for j, c in pairs(v) do |
268 | + | for j, c in pairs(v) do |
269 | - | print(j, c) |
269 | + | print(j, c) |
270 | - | end |
270 | + | end |
271 | - | end |
271 | + | end |
272 | - | end |
272 | + | end |
273 | - | |
273 | + | |
274 | - | if type(message) == "table" then |
274 | + | if type(message) == "table" then |
275 | - | if message.type == "newaccount" then |
275 | + | if message.type == "newaccount" then |
276 | - | result = newAccount(message.account) |
276 | + | result = newAccount(message.account) |
277 | - | |
277 | + | |
278 | - | broadcast(result) |
278 | + | broadcast(result) |
279 | - | elseif message.type == "getfunds" then |
279 | + | elseif message.type == "getfunds" then |
280 | - | temp = pullInfo(message.target) |
280 | + | temp = pullInfo(message.target) |
281 | - | |
281 | + | |
282 | - | broadcast(temp.balance) |
282 | + | broadcast(temp.balance) |
283 | - | elseif message.type == "doorder" then |
283 | + | elseif message.type == "doorder" then |
284 | - | result = doOrder(message.target, message.amount, message.masterpass) |
284 | + | result = doOrder(message.target, message.amount, message.masterpass) |
285 | - | |
285 | + | |
286 | - | broadcast(result) |
286 | + | broadcast(result) |
287 | - | elseif message.type == "transfer" then |
287 | + | elseif message.type == "transfer" then |
288 | - | result = transfer(message.account, message.target, message.amount) |
288 | + | result = transfer(message.account, message.target, message.amount) |
289 | - | |
289 | + | |
290 | - | broadcast(result) |
290 | + | broadcast(result) |
291 | - | elseif message.type == "getinfo" then |
291 | + | elseif message.type == "getinfo" then |
292 | - | result = getInfo(message.account) |
292 | + | result = getInfo(message.account) |
293 | - | |
293 | + | |
294 | - | broadcast(result) |
294 | + | broadcast(result) |
295 | - | elseif message.type == "dofront" then |
295 | + | elseif message.type == "dofront" then |
296 | - | result = doFront(message.account) |
296 | + | result = doFront(message.account) |
297 | - | |
297 | + | |
298 | - | broadcast(result) |
298 | + | broadcast(result) |
299 | - | elseif message.type == "flag" then |
299 | + | elseif message.type == "flag" then |
300 | - | result = flag(message.target) |
300 | + | result = flag(message.target) |
301 | - | |
301 | + | |
302 | - | broadcast(result) |
302 | + | broadcast(result) |
303 | - | elseif message.type == "auth" then |
303 | + | elseif message.type == "auth" then |
304 | - | result = auth(message.account) |
304 | + | result = auth(message.account) |
305 | - | |
305 | + | |
306 | - | broadcast(result) |
306 | + | broadcast(result) |
307 | - | else |
307 | + | else |
308 | - | broadcast("error") |
308 | + | broadcast("error") |
309 | - | end |
309 | + | end |
310 | - | end |
310 | + | end |
311 | end |