SHOW:
|
|
- or go back to the newest paste.
1 | - | local GUI = require("GUI") |
1 | + | local cryptKey = {1, 2, 3, 4, 5} |
2 | - | local system = require("System") |
2 | + | local departments = {"SD","ScD","MD","E&T","O5"} |
3 | - | local cryptKey = {1, 2, 3, 4, 5} |
3 | + | local modemPort = 199 |
4 | - | local departments = {"SD","ScD","MD","E&T","O5"} |
4 | + | |
5 | - | local modemPort = 199 |
5 | + | local component = require("component") |
6 | - | |
6 | + | local gpu = component.gpu |
7 | - | |
7 | + | local gui = require("gui") |
8 | - | local component = require("component") |
8 | + | local event = require("event") |
9 | - | local gpu = component.gpu |
9 | + | local ser = require("serialization") |
10 | - | local event = require("event") |
10 | + | local uuid = require("uuid") |
11 | - | local ser = require("serialization") |
11 | + | writer = component.os_cardwriter |
12 | - | local fs = require("Filesystem") |
12 | + | |
13 | - | writer = component.os_cardwriter |
13 | + | local myGui, cardStatusLabel, userList, userNameText, userLevelLabel, LevelUpButton, LevelDownButton |
14 | - | |
14 | + | local cardBlockedYesButton, cardBlockedNoButton, userNewButton, userDeleteButton, userResetUUIDButton, MTFYesButton, MTFNoButton |
15 | - | ---------- |
15 | + | local GOIYesButton, GOINoButton, SecYesButton, SecNoButton, userArmoryLabel, ArmoryUpButton, ArmoryDownButton |
16 | - | |
16 | + | local userDepLabel, DepUpButton, DepDownButton, IntYesButton, IntNoButton, StaffYesButton, StaffNoButton |
17 | - | local cardStatusLabel, userList, userNameText, userLevelLabel, LevelUpButton, LevelDownButton |
17 | + | |
18 | - | local cardBlockedYesButton, cardBlockedNoButton, userNewButton, userDeleteButton, MTFYesButton, MTFNoButton |
18 | + | local prgName = "SCP Security System" |
19 | - | local GOIYesButton, GOINoButton, SecYesButton, SecNoButton, userArmoryLabel, ArmoryUpButton, ArmoryDownButton |
19 | + | local version = "v2.0a" |
20 | - | local userDepLabel, DepUpButton, DepDownButton, IntYesButton, IntNoButton, StaffYesButton, StaffNoButton |
20 | + | |
21 | - | |
21 | + | local modem = component.modem |
22 | - | ---------- |
22 | + | |
23 | - | |
23 | + | local function convert( chars, dist, inv ) |
24 | - | local prgName = "Security database" |
24 | + | return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 ) |
25 | - | local version = "v0.6" |
25 | + | end |
26 | - | |
26 | + | |
27 | - | local modem = component.modem |
27 | + | |
28 | - | |
28 | + | local function crypt(str,k,inv) |
29 | - | ----------- |
29 | + | local enc= ""; |
30 | - | |
30 | + | for i=1,#str do |
31 | - | local function convert( chars, dist, inv ) |
31 | + | if(#str-k[5] >= i or not inv)then |
32 | - | return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 ) |
32 | + | for inc=0,3 do |
33 | if(i%4 == inc)then | |
34 | - | |
34 | + | enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv); |
35 | - | |
35 | + | break; |
36 | - | local function crypt(str,k,inv) |
36 | + | end |
37 | - | local enc= ""; |
37 | + | end |
38 | - | for i=1,#str do |
38 | + | end |
39 | - | if(#str-k[5] >= i or not inv)then |
39 | + | end |
40 | - | for inc=0,3 do |
40 | + | if(not inv)then |
41 | - | if(i%4 == inc)then |
41 | + | for i=1,k[5] do |
42 | - | enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv); |
42 | + | enc = enc .. string.char(math.random(32,126)); |
43 | - | break; |
43 | + | end |
44 | - | end |
44 | + | end |
45 | - | end |
45 | + | return enc; |
46 | - | end |
46 | + | end |
47 | - | end |
47 | + | |
48 | - | if(not inv)then |
48 | + | --// exportstring( string ) |
49 | - | for i=1,k[5] do |
49 | + | --// returns a "Lua" portable version of the string |
50 | - | enc = enc .. string.char(math.random(32,126)); |
50 | + | local function exportstring( s ) |
51 | - | end |
51 | + | s = string.format( "%q",s ) |
52 | - | end |
52 | + | -- to replace |
53 | - | return enc; |
53 | + | s = string.gsub( s,"\\\n","\\n" ) |
54 | s = string.gsub( s,"\r","\\r" ) | |
55 | - | |
55 | + | s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" ) |
56 | - | --// exportstring( string ) |
56 | + | return s |
57 | - | --// returns a "Lua" portable version of the string |
57 | + | end |
58 | - | local function exportstring( s ) |
58 | + | --// The Save Function |
59 | - | s = string.format( "%q",s ) |
59 | + | function saveTable( tbl,filename ) |
60 | - | -- to replace |
60 | + | local charS,charE = " ","\n" |
61 | - | s = string.gsub( s,"\\\n","\\n" ) |
61 | + | local file,err |
62 | - | s = string.gsub( s,"\r","\\r" ) |
62 | + | -- create a pseudo file that writes to a string and return the string |
63 | - | s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" ) |
63 | + | if not filename then |
64 | - | return s |
64 | + | file = { write = function( self,newstr ) self.str = self.str..newstr end, str = "" } |
65 | charS,charE = "","" | |
66 | - | --// The Save Function |
66 | + | -- write table to tmpfile |
67 | - | function saveTable( tbl,filename ) |
67 | + | elseif filename == true or filename == 1 then |
68 | - | local charS,charE = " ","\n" |
68 | + | charS,charE,file = "","",io.tmpfile() |
69 | - | local file,err |
69 | + | -- write table to file |
70 | - | -- create a pseudo file that writes to a string and return the string |
70 | + | -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error |
71 | - | if not filename then |
71 | + | else |
72 | - | file = { write = function( self,newstr ) self.str = self.str..newstr end, str = "" } |
72 | + | file,err = io.open( filename, "w" ) |
73 | - | charS,charE = "","" |
73 | + | if err then return _,err end |
74 | - | -- write table to tmpfile |
74 | + | end |
75 | - | elseif filename == true or filename == 1 then |
75 | + | -- initiate variables for save procedure |
76 | - | charS,charE,file = "","",io.tmpfile() |
76 | + | local tables,lookup = { tbl },{ [tbl] = 1 } |
77 | - | -- write table to file |
77 | + | file:write( "return {"..charE ) |
78 | - | -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error |
78 | + | for idx,t in ipairs( tables ) do |
79 | - | else |
79 | + | if filename and filename ~= true and filename ~= 1 then |
80 | - | file,err = fs.open(filename, "w") |
80 | + | file:write( "-- Table: {"..idx.."}"..charE ) |
81 | - | --file,err = io.open( filename, "w" ) |
81 | + | end |
82 | - | if err then return _,err end |
82 | + | file:write( "{"..charE ) |
83 | - | end |
83 | + | local thandled = {} |
84 | - | -- initiate variables for save procedure |
84 | + | for i,v in ipairs( t ) do |
85 | - | local tables,lookup = { tbl },{ [tbl] = 1 } |
85 | + | thandled[i] = true |
86 | - | file:write( "return {"..charE ) |
86 | + | -- escape functions and userdata |
87 | - | for idx,t in ipairs( tables ) do |
87 | + | if type( v ) ~= "userdata" then |
88 | - | if filename and filename ~= true and filename ~= 1 then |
88 | + | -- only handle value |
89 | - | file:write( "-- Table: {"..idx.."}"..charE ) |
89 | + | if type( v ) == "table" then |
90 | - | end |
90 | + | if not lookup[v] then |
91 | - | file:write( "{"..charE ) |
91 | + | table.insert( tables, v ) |
92 | - | local thandled = {} |
92 | + | lookup[v] = #tables |
93 | - | for i,v in ipairs( t ) do |
93 | + | end |
94 | - | thandled[i] = true |
94 | + | file:write( charS.."{"..lookup[v].."},"..charE ) |
95 | - | -- escape functions and userdata |
95 | + | elseif type( v ) == "function" then |
96 | - | if type( v ) ~= "userdata" then |
96 | + | file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE ) |
97 | - | -- only handle value |
97 | + | else |
98 | - | if type( v ) == "table" then |
98 | + | local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v ) |
99 | - | if not lookup[v] then |
99 | + | file:write( charS..value..","..charE ) |
100 | - | table.insert( tables, v ) |
100 | + | end |
101 | - | lookup[v] = #tables |
101 | + | end |
102 | - | end |
102 | + | end |
103 | - | file:write( charS.."{"..lookup[v].."},"..charE ) |
103 | + | for i,v in pairs( t ) do |
104 | - | elseif type( v ) == "function" then |
104 | + | -- escape functions and userdata |
105 | - | file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE ) |
105 | + | if (not thandled[i]) and type( v ) ~= "userdata" then |
106 | - | else |
106 | + | -- handle index |
107 | - | local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v ) |
107 | + | if type( i ) == "table" then |
108 | - | file:write( charS..value..","..charE ) |
108 | + | if not lookup[i] then |
109 | - | end |
109 | + | table.insert( tables,i ) |
110 | - | end |
110 | + | lookup[i] = #tables |
111 | - | end |
111 | + | end |
112 | - | for i,v in pairs( t ) do |
112 | + | file:write( charS.."[{"..lookup[i].."}]=" ) |
113 | - | -- escape functions and userdata |
113 | + | else |
114 | - | if (not thandled[i]) and type( v ) ~= "userdata" then |
114 | + | local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i ) |
115 | - | -- handle index |
115 | + | file:write( charS..index.."=" ) |
116 | - | if type( i ) == "table" then |
116 | + | end |
117 | - | if not lookup[i] then |
117 | + | -- handle value |
118 | - | table.insert( tables,i ) |
118 | + | if type( v ) == "table" then |
119 | - | lookup[i] = #tables |
119 | + | if not lookup[v] then |
120 | - | end |
120 | + | table.insert( tables,v ) |
121 | - | file:write( charS.."[{"..lookup[i].."}]=" ) |
121 | + | lookup[v] = #tables |
122 | - | else |
122 | + | end |
123 | - | local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i ) |
123 | + | file:write( "{"..lookup[v].."},"..charE ) |
124 | - | file:write( charS..index.."=" ) |
124 | + | elseif type( v ) == "function" then |
125 | - | end |
125 | + | file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE ) |
126 | - | -- handle value |
126 | + | else |
127 | - | if type( v ) == "table" then |
127 | + | local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v ) |
128 | - | if not lookup[v] then |
128 | + | file:write( value..","..charE ) |
129 | - | table.insert( tables,v ) |
129 | + | end |
130 | - | lookup[v] = #tables |
130 | + | end |
131 | - | end |
131 | + | end |
132 | - | file:write( "{"..lookup[v].."},"..charE ) |
132 | + | file:write( "},"..charE ) |
133 | - | elseif type( v ) == "function" then |
133 | + | end |
134 | - | file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE ) |
134 | + | file:write( "}" ) |
135 | - | else |
135 | + | -- Return Values |
136 | - | local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v ) |
136 | + | -- return stringtable from string |
137 | - | file:write( value..","..charE ) |
137 | + | if not filename then |
138 | - | end |
138 | + | -- set marker for stringtable |
139 | - | end |
139 | + | return file.str.."--|" |
140 | - | end |
140 | + | -- return stringttable from file |
141 | - | file:write( "},"..charE ) |
141 | + | elseif filename == true or filename == 1 then |
142 | - | end |
142 | + | file:seek ( "set" ) |
143 | - | file:write( "}" ) |
143 | + | -- no need to close file, it gets closed and removed automatically |
144 | - | -- Return Values |
144 | + | -- set marker for stringtable |
145 | - | -- return stringtable from string |
145 | + | return file:read( "*a" ).."--|" |
146 | - | if not filename then |
146 | + | -- close file and return 1 |
147 | - | -- set marker for stringtable |
147 | + | else |
148 | - | return file.str.."--|" |
148 | + | file:close() |
149 | - | -- return stringttable from file |
149 | + | return 1 |
150 | - | elseif filename == true or filename == 1 then |
150 | + | end |
151 | - | file:seek ( "set" ) |
151 | + | end |
152 | - | -- no need to close file, it gets closed and removed automatically |
152 | + | |
153 | - | -- set marker for stringtable |
153 | + | --// The Load Function |
154 | - | return file:read( "*a" ).."--|" |
154 | + | function loadTable( sfile ) |
155 | - | -- close file and return 1 |
155 | + | local tables, err, _ |
156 | - | else |
156 | + | -- catch marker for stringtable |
157 | - | file:close() |
157 | + | if string.sub( sfile,-3,-1 ) == "--|" then |
158 | - | return 1 |
158 | + | tables,err = loadstring( sfile ) |
159 | - | end |
159 | + | else |
160 | tables,err = loadfile( sfile ) | |
161 | - | |
161 | + | end |
162 | - | --// The Load Function |
162 | + | if err then return _,err |
163 | - | function loadTable( sfile ) |
163 | + | end |
164 | - | local tables, err, _ |
164 | + | tables = tables() |
165 | - | -- catch marker for stringtable |
165 | + | for idx = 1,#tables do |
166 | - | if string.sub( sfile,-3,-1 ) == "--|" then |
166 | + | local tolinkv,tolinki = {},{} |
167 | - | tables,err = loadstring( sfile ) |
167 | + | for i,v in pairs( tables[idx] ) do |
168 | - | else |
168 | + | if type( v ) == "table" and tables[v[1]] then |
169 | - | tables,err = loadfile( sfile ) |
169 | + | table.insert( tolinkv,{ i,tables[v[1]] } ) |
170 | - | end |
170 | + | end |
171 | - | if err then return _,err |
171 | + | if type( i ) == "table" and tables[i[1]] then |
172 | - | end |
172 | + | table.insert( tolinki,{ i,tables[i[1]] } ) |
173 | - | tables = tables() |
173 | + | end |
174 | - | for idx = 1,#tables do |
174 | + | end |
175 | - | local tolinkv,tolinki = {},{} |
175 | + | -- link values, first due to possible changes of indices |
176 | - | for i,v in pairs( tables[idx] ) do |
176 | + | for _,v in ipairs( tolinkv ) do |
177 | - | if type( v ) == "table" and tables[v[1]] then |
177 | + | tables[idx][v[1]] = v[2] |
178 | - | table.insert( tolinkv,{ i,tables[v[1]] } ) |
178 | + | end |
179 | - | end |
179 | + | -- link indices |
180 | - | if type( i ) == "table" and tables[i[1]] then |
180 | + | for _,v in ipairs( tolinki ) do |
181 | - | table.insert( tolinki,{ i,tables[i[1]] } ) |
181 | + | tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil |
182 | - | end |
182 | + | end |
183 | - | end |
183 | + | end |
184 | - | -- link values, first due to possible changes of indices |
184 | + | return tables[1] |
185 | - | for _,v in ipairs( tolinkv ) do |
185 | + | end |
186 | - | tables[idx][v[1]] = v[2] |
186 | + | |
187 | - | end |
187 | + | |
188 | - | -- link indices |
188 | + | |
189 | - | for _,v in ipairs( tolinki ) do |
189 | + | local function convert( chars, dist, inv ) |
190 | - | tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil |
190 | + | return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 ) |
191 | - | end |
191 | + | end |
192 | - | end |
192 | + | |
193 | - | return tables[1] |
193 | + | |
194 | local function crypt(str,k,inv) | |
195 | - | |
195 | + | local enc= ""; |
196 | - | |
196 | + | for i=1,#str do |
197 | - | |
197 | + | if(#str-k[5] >= i or not inv)then |
198 | - | local function convert( chars, dist, inv ) |
198 | + | for inc=0,3 do |
199 | - | return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 ) |
199 | + | if(i%4 == inc)then |
200 | enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv); | |
201 | - | |
201 | + | break; |
202 | - | |
202 | + | end |
203 | - | local function crypt(str,k,inv) |
203 | + | end |
204 | - | local enc= ""; |
204 | + | end |
205 | - | for i=1,#str do |
205 | + | end |
206 | - | if(#str-k[5] >= i or not inv)then |
206 | + | if(not inv)then |
207 | - | for inc=0,3 do |
207 | + | for i=1,k[5] do |
208 | - | if(i%4 == inc)then |
208 | + | enc = enc .. string.char(math.random(32,126)); |
209 | - | enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv); |
209 | + | end |
210 | - | break; |
210 | + | end |
211 | - | end |
211 | + | return enc; |
212 | - | end |
212 | + | end |
213 | - | end |
213 | + | |
214 | - | end |
214 | + | |
215 | - | if(not inv)then |
215 | + | function buttonCallback(guiID, id) |
216 | - | for i=1,k[5] do |
216 | + | local result = gui.getYesNo("", "Do you really want to exit?", "") |
217 | - | enc = enc .. string.char(math.random(32,126)); |
217 | + | if result == true then |
218 | - | end |
218 | + | gui.exit() |
219 | - | end |
219 | + | end |
220 | - | return enc; |
220 | + | end |
221 | ||
222 | - | |
222 | + | function eventCallback(ev, id) |
223 | - | ----------Callbacks |
223 | + | if ev == "cardInsert" then |
224 | - | function updateServer() |
224 | + | gui.setText(myGui, cardStatusLabel, " Card present") |
225 | - | local data = ser.serialize(userTable) |
225 | + | elseif ev == "cardRemove" then |
226 | - | local crypted = crypt(data, cryptKey) |
226 | + | gui.setText(myGui, cardStatusLabel, " No card ") |
227 | - | if modem.isOpen(modemPort) == false then |
227 | + | end |
228 | - | modem.open(modemPort) |
228 | + | end |
229 | - | end |
229 | + | |
230 | - | modem.broadcast(modemPort, "updateuser", crypted) |
230 | + | function userListCallback(guiID, listID, selectedID, selectedText) |
231 | gui.setText(myGui, userNameText, userTable[selectedID].name) | |
232 | - | |
232 | + | gui.setText(myGui, userLevelLabel, tostring(userTable[selectedID].level)) |
233 | - | function updateList() |
233 | + | gui.setText(myGui, userArmoryLabel, tostring(userTable[selectedID].armory)) |
234 | - | userList:remove() |
234 | + | gui.setText(myGui, userDepLabel, departments[userTable[selectedID].department]) |
235 | - | for key,value in pairs(userTable) do |
235 | + | if userTable[selectedID].blocked == true then |
236 | - | userList.addItem(value.name) |
236 | + | gui.setEnable(myGui, cardBlockedYesButton, false) |
237 | - | end |
237 | + | gui.setEnable(myGui, cardBlockedNoButton, true) |
238 | - | saveTable(userTable, "userlist.txt") |
238 | + | else |
239 | - | updateServer() |
239 | + | gui.setEnable(myGui, cardBlockedYesButton, true) |
240 | gui.setEnable(myGui, cardBlockedNoButton, false) | |
241 | - | |
241 | + | end |
242 | - | function eventCallback(ev, id) |
242 | + | if userTable[selectedID].mtf == true then |
243 | - | if ev == "cardInsert" then |
243 | + | gui.setEnable(myGui, MTFNoButton, true) |
244 | - | cardStatusLabel.text = " Card present" |
244 | + | gui.setEnable(myGui, MTFYesButton, false) |
245 | - | elseif ev == "cardRemove" then |
245 | + | else |
246 | - | cardStatusLabel.text = " No card " |
246 | + | gui.setEnable(myGui, MTFNoButton, false) |
247 | - | end |
247 | + | gui.setEnable(myGui, MTFYesButton, true) |
248 | end | |
249 | - | |
249 | + | if userTable[selectedID].goi == true then |
250 | - | function writeCardCallback() |
250 | + | gui.setEnable(myGui, GOINoButton, true) |
251 | - | local selected = userList.selectedItem |
251 | + | gui.setEnable(myGui, GOIYesButton, false) |
252 | - | local data = userTable[selected].date .. " " .. userTable[selected].name .. " " .. tostring(userTable[selected].level) .. " " .. tostring(userTable[selected].blocked) |
252 | + | else |
253 | - | local crypted = crypt(data, cryptKey) |
253 | + | gui.setEnable(myGui, GOINoButton, false) |
254 | - | writer.write(crypted, userTable[selected].name .. "'s security pass", false, 8) |
254 | + | gui.setEnable(myGui, GOIYesButton, true) |
255 | end | |
256 | - | |
256 | + | if userTable[selectedID].sec == true then |
257 | - | function userListCallback() |
257 | + | gui.setEnable(myGui, SecNoButton, true) |
258 | - | selectedId = userList.selectedItem |
258 | + | gui.setEnable(myGui, SecYesButton, false) |
259 | - | userNameText.text = userTable[selectedId].name |
259 | + | else |
260 | - | userLevelLabel.text = toString(userTable[selectedId].level) |
260 | + | gui.setEnable(myGui, SecNoButton, false) |
261 | - | userArmoryLabel.text = toString(userTable[selectedId].armory) |
261 | + | gui.setEnable(myGui, SecYesButton, true) |
262 | - | userDepLabel.text = departments[userTable[selectedId].department] |
262 | + | end |
263 | - | if userTable[selectedID].blocked == true then |
263 | + | if userTable[selectedID].int == true then |
264 | - | cardBlockedYesButton.pressed = true |
264 | + | gui.setEnable(myGui, IntNoButton, true) |
265 | - | else |
265 | + | gui.setEnable(myGui, IntYesButton, false) |
266 | - | cardBlockedYesButton.pressed = false |
266 | + | else |
267 | - | end |
267 | + | gui.setEnable(myGui, IntNoButton, false) |
268 | - | if userTable[selectedID].mtf == true then |
268 | + | gui.setEnable(myGui, IntYesButton, true) |
269 | - | MTFYesButton.pressed = true |
269 | + | end |
270 | - | else |
270 | + | if userTable[selectedID].staff == true then |
271 | - | MTFYesButton.pressed = false |
271 | + | gui.setEnable(myGui, StaffNoButton, true) |
272 | - | end |
272 | + | gui.setEnable(myGui, StaffYesButton, false) |
273 | - | if userTable[selectedID].goi == true then |
273 | + | else |
274 | - | GOIYesButton.pressed = true |
274 | + | gui.setEnable(myGui, StaffNoButton, false) |
275 | - | else |
275 | + | gui.setEnable(myGui, StaffYesButton, true) |
276 | - | GOIYesButton.pressed = false |
276 | + | end |
277 | - | end |
277 | + | gui.setEnable(myGui, LevelUpButton, true) |
278 | - | if userTable[selectedID].sec == true then |
278 | + | gui.setEnable(myGui, LevelDownButton, true) |
279 | - | SecYesButton.pressed = true |
279 | + | gui.setEnable(myGui, ArmoryUpButton, true) |
280 | - | else |
280 | + | gui.setEnable(myGui, ArmoryDownButton, true) |
281 | - | SecYesButton.pressed = false |
281 | + | gui.setEnable(myGui, DepUpButton, true) |
282 | - | end |
282 | + | gui.setEnable(myGui, DepDownButton, true) |
283 | - | if userTable[selectedID].int == true then |
283 | + | gui.setEnable(myGui, userNameText, true) |
284 | - | IntYesButton.pressed = true |
284 | + | end |
285 | - | else |
285 | + | |
286 | - | IntYesButton.pressed = false |
286 | + | function updateServer() |
287 | - | end |
287 | + | local data = ser.serialize(userTable) |
288 | - | if userTable[selectedID].staff == true then |
288 | + | local crypted = crypt(data, cryptKey) |
289 | - | StaffYesButton.pressed = true |
289 | + | if modem.isOpen(modemPort) == false then |
290 | - | else |
290 | + | modem.open(modemPort) |
291 | - | StaffYesButton.pressed = false |
291 | + | end |
292 | - | end |
292 | + | modem.broadcast(modemPort, "updateuser", crypted) |
293 | end | |
294 | - | |
294 | + | |
295 | - | function inputCallback() |
295 | + | |
296 | - | local selected = userList.selectedItem |
296 | + | function updateList() |
297 | - | userTable[selected].name = userNameText.text |
297 | + | gui.clearList(myGui, userList) |
298 | - | updateList() |
298 | + | for key,value in pairs(userTable) do |
299 | - | userListCallback() |
299 | + | gui.insertList(myGui, userList, value.name) |
300 | end | |
301 | - | |
301 | + | saveTable(userTable, "userlist.txt") |
302 | - | ----------GUI SETUP |
302 | + | updateServer() |
303 | - | |
303 | + | end |
304 | - | local workspace, window, menu = system.addWindow(GUI.filledWindow(2,2,150,45,0xE1E1E1)) |
304 | + | |
305 | - | |
305 | + | function mtfYesUserCallback(guiID, id) |
306 | - | local layout = window:addChild(GUI.layout(1, 1, window.width, window.height, 1, 1)) |
306 | + | local selected = gui.getSelected(myGui, userList) |
307 | - | |
307 | + | userTable[selected].mtf = true |
308 | - | local contextMenu = menu:addContextMenuItem("File") |
308 | + | updateList() |
309 | - | contextMenu:addItem("Close").onTouch = function() |
309 | + | userListCallback(myGui, userList, selected) |
310 | - | window:remove() |
310 | + | end |
311 | ||
312 | - | |
312 | + | function mtfNoUserCallback(guiID, id) |
313 | - | window:addChild(GUI.panel(3,3,60,36,0x6B6E74)) |
313 | + | local selected = gui.getSelected(myGui, userList) |
314 | - | userList = window:addChild(GUI.list(4, 4, 58, 34, 3, 0, 0xE1E1E1, 0x4B4B4B, 0xD2D2D2, 0x4B4B4B, 0x3366CC, 0xFFFFFF, false)) |
314 | + | userTable[selected].mtf = false |
315 | - | userTable = loadTable("userlist.txt") |
315 | + | updateList() |
316 | - | if userTable == nil then |
316 | + | userListCallback(myGui, userList, selected) |
317 | - | userTable = {} |
317 | + | end |
318 | function goiYesUserCallback(guiID, id) | |
319 | - | updateList() |
319 | + | local selected = gui.getSelected(myGui, userList) |
320 | - | |
320 | + | userTable[selected].goi = true |
321 | - | --user infos |
321 | + | updateList() |
322 | - | window:addChild(GUI.label(64,12,3,3,0x165FF2,"User name : ")) |
322 | + | userListCallback(myGui, userList, selected) |
323 | - | window:addChild(GUI.label(64,14,3,3,0x165FF2,"Level : ")) |
323 | + | end |
324 | - | window:addChild(GUI.label(64,16,3,3,0x165FF2,"MTF : ")) |
324 | + | |
325 | - | window:addChild(GUI.label(64,18,3,3,0x165FF2,"GOI : ")) |
325 | + | function goiNoUserCallback(guiID, id) |
326 | - | window:addChild(GUI.label(64,20,3,3,0x165FF2,"Security : ")) |
326 | + | local selected = gui.getSelected(myGui, userList) |
327 | - | window:addChild(GUI.label(64,22,3,3,0x165FF2,"Intercom : ")) |
327 | + | userTable[selected].goi = false |
328 | - | window:addChild(GUI.label(64,24,3,3,0x165FF2,"STAFF : ")) |
328 | + | updateList() |
329 | - | window:addChild(GUI.label(64,26,3,3,0x165FF2,"ArmorLevel: ")) |
329 | + | userListCallback(myGui, userList, selected) |
330 | - | window:addChild(GUI.label(64,28,3,3,0x165FF2,"Department: ")) |
330 | + | end |
331 | - | window:addChild(GUI.label(64,30,3,3,0x165FF2,"Blocked : ")) |
331 | + | |
332 | - | userNameText = window:addChild(GUI.input(88,12,16,1, 0xEEEEEE, 0x555555, 0x999999, 0xFFFFFF, 0x2D2D2D, "", "input name")) |
332 | + | function secYesUserCallback(guiID, id) |
333 | - | --userNameText.onInputFinished = inputCallback() |
333 | + | local selected = gui.getSelected(myGui, userList) |
334 | - | userLevelLabel = window:addChild(GUI.label(88,14,3,3,0x165FF2,"#")) |
334 | + | userTable[selected].sec = true |
335 | - | LevelUpButton = window:addChild(GUI.button(92,14,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "+")) |
335 | + | updateList() |
336 | - | --LevelUpButton.onTouch = |
336 | + | userListCallback(myGui, userList, selected) |
337 | - | LevelDownButton = window:addChild(GUI.button(96,14,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "-")) |
337 | + | end |
338 | - | --LevelDownButton.onTouch = |
338 | + | |
339 | - | MTFYesButton = window:addChild(GUI.button(88,16,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle")) |
339 | + | function secNoUserCallback(guiID, id) |
340 | - | MTFYesButton.switchMode = true |
340 | + | local selected = gui.getSelected(myGui, userList) |
341 | - | --MTFYesButton.onTouch = |
341 | + | userTable[selected].sec = false |
342 | - | |
342 | + | updateList() |
343 | - | --CardWriter frame |
343 | + | userListCallback(myGui, userList, selected) |
344 | - | window:addChild(GUI.panel(114, 2, 38, 6, 0x6B6E74)) |
344 | + | end |
345 | - | cardStatusLabel = window:addChild(GUI.label(116, 4, 3,3,0x165FF2," No card ")) |
345 | + | |
346 | - | |
346 | + | function intYesUserCallback(guiID, id) |
347 | - | event.addHandler(eventCallback) |
347 | + | local selected = gui.getSelected(myGui, userList) |
348 | - | |
348 | + | userTable[selected].int = true |
349 | - | workspace:draw() |
349 | + | updateList() |
350 | - | workspace:start() |
350 | + | userListCallback(myGui, userList, selected) |
351 | - | |
351 | + | end |
352 | ||
353 | function intNoUserCallback(guiID, id) | |
354 | local selected = gui.getSelected(myGui, userList) | |
355 | userTable[selected].int = false | |
356 | updateList() | |
357 | userListCallback(myGui, userList, selected) | |
358 | end | |
359 | function staffYesUserCallback(guiID, id) | |
360 | local selected = gui.getSelected(myGui, userList) | |
361 | userTable[selected].staff = true | |
362 | updateList() | |
363 | userListCallback(myGui, userList, selected) | |
364 | end | |
365 | ||
366 | function staffNoUserCallback(guiID, id) | |
367 | local selected = gui.getSelected(myGui, userList) | |
368 | userTable[selected].staff = false | |
369 | updateList() | |
370 | userListCallback(myGui, userList, selected) | |
371 | end | |
372 | ||
373 | function blockUserCallback(guiID, id) | |
374 | local selected = gui.getSelected(myGui, userList) | |
375 | userTable[selected].blocked = true | |
376 | updateList() | |
377 | userListCallback(myGui, userList, selected) | |
378 | end | |
379 | ||
380 | function unblockUserCallback(guiID, id) | |
381 | local selected = gui.getSelected(myGui, userList) | |
382 | userTable[selected].blocked = false | |
383 | updateList() | |
384 | userListCallback(myGui, userList, selected) | |
385 | end | |
386 | ||
387 | function newUserCallback(guiID, id) | |
388 | local tmpTable = {["name"] = "new", ["blocked"] = false, ["level"] = 1, ["date"] = os.date(), ["armory"] = 0, ["mtf"] = false, ["sec"] = false, ["goi"] = false, ["int"] = false, ["staff"] = false, ["department"] = 1, ["uuid"] = uuid.next()} | |
389 | table.insert(userTable, tmpTable) | |
390 | updateList() | |
391 | end | |
392 | ||
393 | function deleteUserCallback(guiID, id) | |
394 | local selected = gui.getSelected(myGui, userList) | |
395 | userTable[selected] = nil | |
396 | updateList() | |
397 | gui.setText(myGui, userNameText, "") | |
398 | gui.setText(myGui, userLevelLabel, "") | |
399 | gui.setEnable(myGui, cardBlockedYesButton, false) | |
400 | gui.setEnable(myGui, cardBlockedNoButton, false) | |
401 | gui.setEnable(myGui, MTFYesButton, false) | |
402 | gui.setEnable(myGui, MTFNoButton, false) | |
403 | gui.setEnable(myGui, GOIYesButton, false) | |
404 | gui.setEnable(myGui, GOINoButton, false) | |
405 | gui.setEnable(myGui, SecYesButton, false) | |
406 | gui.setEnable(myGui, SecNoButton, false) | |
407 | gui.setEnable(myGui, IntYesButton, false) | |
408 | gui.setEnable(myGui, IntNoButton, false) | |
409 | gui.setEnable(myGui, StaffYesButton, false) | |
410 | gui.setEnable(myGui, StaffNoButton, false) | |
411 | gui.setEnable(myGui, LevelUpButton, false) | |
412 | gui.setEnable(myGui, LevelDownButton, false) | |
413 | gui.setEnable(myGui, ArmoryUpButton, false) | |
414 | gui.setEnable(myGui, ArmoryDownButton, false) | |
415 | gui.setEnable(myGui, DepUpButton, false) | |
416 | gui.setEnable(myGui, DepDownButton, false) | |
417 | gui.setEnable(myGui, userNameText, false) | |
418 | end | |
419 | ||
420 | function changeUUID(guiID, id) | |
421 | local selected = gui.getSelected(myGUI, userList) | |
422 | userTable[selected].uuid = uuid.next() | |
423 | updateList() | |
424 | userListCallback(myGui, userList, selected) | |
425 | end | |
426 | ||
427 | function writeCardCallback(guiID, id) | |
428 | local selected = gui.getSelected(myGui, userList) | |
429 | local data = {["date"]=userTable[selected].date,["name"]=userTable[selected].name,["uuid"]=userTable[selected].uuid} | |
430 | data = ser.serialize(data) | |
431 | local crypted = crypt(data, cryptKey) | |
432 | writer.write(crypted, userTable[selected].name .. "'s security pass", false, 8) | |
433 | end | |
434 | ||
435 | function levelUpCallback(guiID, id) | |
436 | local selected = gui.getSelected(myGui, userList) | |
437 | if userTable[selected].level < 101 then | |
438 | userTable[selected].level = userTable[selected].level + 1 | |
439 | end | |
440 | updateList() | |
441 | userListCallback(myGui, userList, selected) | |
442 | end | |
443 | ||
444 | function levelDownCallback(guiID, id) | |
445 | local selected = gui.getSelected(myGui, userList) | |
446 | if userTable[selected].level > 1 then | |
447 | userTable[selected].level = userTable[selected].level - 1 | |
448 | end | |
449 | updateList() | |
450 | userListCallback(myGui, userList, selected) | |
451 | end | |
452 | ||
453 | function armorUpCallback(guiID, id) | |
454 | local selected = gui.getSelected(myGui, userList) | |
455 | if userTable[selected].armory < 4 then | |
456 | userTable[selected].armory = userTable[selected].armory + 1 | |
457 | end | |
458 | updateList() | |
459 | userListCallback(myGui, userList, selected) | |
460 | end | |
461 | ||
462 | function armorDownCallback(guiID, id) | |
463 | local selected = gui.getSelected(myGui, userList) | |
464 | if userTable[selected].armory > 0 then | |
465 | userTable[selected].armory = userTable[selected].armory - 1 | |
466 | end | |
467 | updateList() | |
468 | userListCallback(myGui, userList, selected) | |
469 | end | |
470 | ||
471 | function depUpCallback(guiID, id) | |
472 | local selected = gui.getSelected(myGui, userList) | |
473 | if userTable[selected].department < 5 then | |
474 | userTable[selected].department = userTable[selected].department + 1 | |
475 | end | |
476 | updateList() | |
477 | userListCallback(myGui, userList, selected) | |
478 | end | |
479 | ||
480 | function depDownCallback(guiID, id) | |
481 | local selected = gui.getSelected(myGui, userList) | |
482 | if userTable[selected].department > 1 then | |
483 | userTable[selected].department = userTable[selected].department - 1 | |
484 | end | |
485 | updateList() | |
486 | userListCallback(myGui, userList, selected) | |
487 | end | |
488 | ||
489 | function inputCallback(guiID, textID, text) | |
490 | local selected = gui.getSelected(myGui, userList) | |
491 | userTable[selected].name = text | |
492 | updateList() | |
493 | userListCallback(myGui, userList, selected) | |
494 | end | |
495 | ||
496 | -- main gui setup | |
497 | myGui = gui.newGui(2, 2, 150, 45, true) | |
498 | button = gui.newButton(myGui, "center", 42, "exit", buttonCallback) | |
499 | ||
500 | -- frame with user list | |
501 | gui.newFrame(myGui, 3, 3, 60, 36) | |
502 | userList = gui.newList(myGui, 4, 4, 58, 34, {}, userListCallback) | |
503 | userTable = loadTable("userlist.txt") | |
504 | if userTable == nil then | |
505 | userTable = {} | |
506 | end | |
507 | updateList() | |
508 | ||
509 | -- user infos | |
510 | gui.newLabel(myGui, 64, 12, "User name : ") | |
511 | gui.newLabel(myGui, 64, 14, "Level : ") | |
512 | gui.newLabel(myGui, 64, 16, "MTF : ") | |
513 | gui.newLabel(myGui, 64, 18, "GOI : ") | |
514 | gui.newLabel(myGui, 64, 20, "Security : ") | |
515 | gui.newLabel(myGui, 64, 22, "Intercom : ") | |
516 | gui.newLabel(myGui, 64, 24, "Staff : ") | |
517 | gui.newLabel(myGui, 64, 26, "ArmorLevel: ") | |
518 | gui.newLabel(myGui, 64, 28, "Department: ") | |
519 | gui.newLabel(myGui, 64, 30, "Blocked : [yes] / [no]") | |
520 | userNameText = gui.newText(myGui, 88, 12, 16, "", inputCallback) | |
521 | userLevelLabel = gui.newLabel(myGui, 88, 14, "") | |
522 | LevelUpButton = gui.newButton(myGui, 92, 14, "+", levelUpCallback) | |
523 | LevelDownButton = gui.newButton(myGui, 96, 14, "-", levelDownCallback) | |
524 | MTFYesButton = gui.newButton(myGui, 88, 16, "yes", mtfYesUserCallback) | |
525 | MTFNoButton = gui.newButton(myGui, 96, 16, "no", mtfNoUserCallback) | |
526 | GOIYesButton = gui.newButton(myGui, 88, 18, "yes", goiYesUserCallback) | |
527 | GOINoButton = gui.newButton(myGui, 96, 18, "no", goiNoUserCallback) | |
528 | SecYesButton = gui.newButton(myGui, 88, 20, "yes", secYesUserCallback) | |
529 | SecNoButton = gui.newButton(myGui, 96, 20, "no", secNoUserCallback) | |
530 | IntYesButton = gui.newButton(myGui, 88, 22, "yes", intYesUserCallback) | |
531 | IntNoButton = gui.newButton(myGui, 96, 22, "no", intNoUserCallback) | |
532 | StaffYesButton = gui.newButton(myGui, 88, 24, "yes", staffYesUserCallback) | |
533 | StaffNoButton = gui.newButton(myGui, 96, 24, "no", staffNoUserCallback) | |
534 | userArmoryLabel = gui.newLabel(myGui, 88, 26, "") | |
535 | ArmoryUpButton = gui.newButton(myGui, 92, 26, "+", armorUpCallback) | |
536 | ArmoryDownButton = gui.newButton(myGui, 96, 26, "-", armorDownCallback) | |
537 | userDepLabel = gui.newLabel(myGui, 88, 28, "") | |
538 | DepUpButton = gui.newButton(myGui, 92, 28, "+", depUpCallback) | |
539 | DepDownButton = gui.newButton(myGui, 96, 28, "-", depDownCallback) | |
540 | cardBlockedYesButton = gui.newButton(myGui, 88, 30, "yes", blockUserCallback) | |
541 | cardBlockedNoButton = gui.newButton(myGui, 96, 30, "no", unblockUserCallback) | |
542 | gui.setEnable(myGui, cardBlockedYesButton, false) | |
543 | gui.setEnable(myGui, cardBlockedNoButton, false) | |
544 | gui.setEnable(myGui, MTFYesButton, false) | |
545 | gui.setEnable(myGui, MTFNoButton, false) | |
546 | gui.setEnable(myGui, GOIYesButton, false) | |
547 | gui.setEnable(myGui, GOINoButton, false) | |
548 | gui.setEnable(myGui, SecYesButton, false) | |
549 | gui.setEnable(myGui, SecNoButton, false) | |
550 | gui.setEnable(myGui, IntYesButton, false) | |
551 | gui.setEnable(myGui, IntNoButton, false) | |
552 | gui.setEnable(myGui, StaffYesButton, false) | |
553 | gui.setEnable(myGui, StaffNoButton, false) | |
554 | gui.setEnable(myGui, LevelUpButton, false) | |
555 | gui.setEnable(myGui, LevelDownButton, false) | |
556 | gui.setEnable(myGui, ArmoryUpButton, false) | |
557 | gui.setEnable(myGui, ArmoryDownButton, false) | |
558 | gui.setEnable(myGui, DepUpButton, false) | |
559 | gui.setEnable(myGui, DepDownButton, false) | |
560 | gui.setEnable(myGui, userNameText, false) | |
561 | ||
562 | gui.newHLine(myGui, 64, 36, 86) | |
563 | userNewButton = gui.newButton(myGui, 4, 42, "new", newUserCallback) | |
564 | userDeleteButton = gui.newButton(myGui, 18, 42, "delete", deleteUserCallback) | |
565 | userResetUUIDButton = gui.newButton(myGui, 32, 42, "reset uuid", changeUUID) | |
566 | ||
567 | -- frame with status of the writer | |
568 | gui.newFrame(myGui, 114, 2, 38, 6, "Writer status") | |
569 | cardStatusLabel = gui.newLabel(myGui, 116, 4, " No card ") | |
570 | ||
571 | --updateServerButton = gui.newButton(myGui, 47, 21, "update server", updateServerCallback) | |
572 | ||
573 | cardWriteButton = gui.newButton(myGui, 128, 42, "write card", writeCardCallback) | |
574 | ||
575 | ||
576 | gui.clearScreen() | |
577 | gui.setTop(prgName .. " " .. version) | |
578 | ||
579 | event.listen("cardInsert", eventCallback) | |
580 | event.listen("cardRemove", eventCallback) | |
581 | while true do | |
582 | gui.runGui(myGui) | |
583 | end |