View difference between Paste ID: QxV5sEve and NH0cgCVx
SHOW: | | - or go back to the newest paste.
1
mon = peripheral.find("monitor")
2
sg = peripheral.find("stargate")
3
4
mon.setBackgroundColor(colors.black)
5
mon.clear()
6
maxEng = 50000
7
dialling = {}
8
9
local function alarmSet(set)
10-
  rs.setOutput("bottom", set)
10+
  rs.setOutput("left", set)
11
  return 
12
end
13
  
14
function drawPowerBar() -- checks power levels and writes power bar to monitor
15
  x,y = mon.getSize()
16
  engPercent = (sg.energyAvailable() / (maxEng +1)) * 100 -- returns percent
17
  for i = y, (y - y / 100 * engPercent), -1 do
18
    mon.setCursorPos(x-2,i)
19
    if i > y/4*3 then 
20
      mon.setBackgroundColor(colors.red)
21
	  mon.setTextColor(colors.red)
22
    elseif i > y/2 then
23
      mon.setBackgroundColor(colors.orange)
24
	  mon.setTextColor(colors.orange)
25
    elseif i > y/4 then
26
      mon.setBackgroundColor(colors.green)
27
	  mon.setTextColor(colors.green)
28
    else
29
      mon.setBackgroundColor(colors.lime)
30
	  mon.setTextColor(colors.lime)
31
    end
32
    mon.write("  ")
33
  end
34
  mon.setBackgroundColor(colors.black)
35
  mon.setCursorPos(x-9,y)
36
  mon.write(math.floor(sg.energyAvailable() / 1000).."k SU ")
37
end
38
39
function drawChevrons() --draws cheyvrons on the screen
40
  x,y = mon.getSize()
41
  chevX1 = x/3
42
  chevX2 = x/3*2+1
43
  chevY1 = y/3-2
44
  chevY2 = y/3*2 +2
45
  mon.setBackgroundColor(colors.black)
46
  for yc = chevY1-2, chevY2-2 do
47
    for xc = chevX1-2, chevX2-2 do
48
	  mon.setCursorPos(xc, yc)
49
	  mon.write(" ")
50
	end
51
  end
52
  mon.setBackgroundColor(colors.lightGray)
53
  for i = chevX1+2, chevX2-2 do
54
    mon.setCursorPos(i,chevY1)
55
	mon.write(" ")
56
  end
57
  for i = chevX1+2, chevX2-2 do
58
    mon.setCursorPos(i,chevY2)
59
	mon.write(" ")
60
  end
61
  for i = chevY1+2, chevY2-2 do
62
    mon.setCursorPos(chevX1,i)
63
	mon.write(" ")
64
  end
65
  for i = chevY1+2, chevY2-2 do
66
    mon.setCursorPos(chevX2, i)
67
	mon.write(" ")
68
  end
69
  chev1pos = {chevX1, chevY2 }
70
  mon.setBackgroundColor(colors.gray)
71
  mon.setTextColor(colors.black)
72
  mon.setCursorPos(math.floor(chev1pos[1]), math.floor(chev1pos[2])-1)
73
  mon.write(" > ")
74
  chev2pos = {chevX1, chevY1 + ((chevY2 - chevY1) / 2) }
75
  mon.setCursorPos(math.floor(chev2pos[1]-1), math.floor(chev2pos[2]))
76
  mon.write(" > ")
77
  chev3pos = {chevX1, chevY1 }
78
  mon.setCursorPos(math.floor(chev3pos[1]), math.floor(chev3pos[2]+1))
79
  mon.write(" > ")
80
  chev4pos = {chevX1 + ((chevX2 - chevX1) / 2), chevY1 }
81
  mon.setCursorPos(math.floor(chev4pos[1]-1), math.floor(chev4pos[2]))
82
  mon.write(" V ")
83
  chev5pos = {chevX2, chevY1 }
84
  mon.setCursorPos(math.floor(chev5pos[1]-2), math.floor(chev5pos[2])+1)
85
  mon.write(" < ")
86
  chev6pos = {chevX2, chevY1 + ((chevY2 - chevY1) / 2) }
87
  mon.setCursorPos(math.floor(chev6pos[1]-1), math.floor(chev6pos[2]))
88
  mon.write(" < ")
89
  chev7pos = {chevX2, chevY2 }
90
  mon.setCursorPos(math.floor(chev7pos[1]-2), math.floor(chev7pos[2]-1))
91
  mon.write(" < ")
92
  chev8pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
93
  mon.setCursorPos(math.floor(chev8pos[1]-1), math.floor(chev8pos[2]))
94
  mon.write("   ")
95
--  chev9pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
96
--  mon.setCursorPos(math.floor(chev8pos[1]-1), chevY1 + ((chevY2 - chevY1) / 2))
97
--  mon.write(" 9 ")
98
  mon.setBackgroundColor(colors.black)
99
  mon.setCursorPos(x/2 - 4, y/2 - 1)
100
  mon.write("           ")
101
  mon.setCursorPos(x/2-1, y/2+4)
102
  mon.write("     ")
103
end
104
105
function drawChev( chevInfo )
106
  mon.setBackgroundColor(colors.gray)
107
  x,y = mon.getSize()
108
  chevX1 = x/3
109
  chevX2 = x/3*2+1
110
  chevY1 = y/3-2
111
  chevY2 = y/3*2 +2
112
  if chevInfo[1] == 1 then
113
    chev1pos = {chevX1, chevY2 }
114
    mon.setBackgroundColor(colors.gray)
115
    mon.setCursorPos(math.floor(chev1pos[1]), math.floor(chev1pos[2])-1)
116
    mon.write(" "..chevInfo[2].." ")
117
  elseif chevInfo[1] == 2 then
118
    chev2pos = {chevX1, chevY1 + ((chevY2 - chevY1) / 2) }
119
    mon.setCursorPos(math.floor(chev2pos[1]-1), math.floor(chev2pos[2]))
120
    mon.write(" "..chevInfo[2].." ")
121
  elseif chevInfo[1] == 3 then
122
    chev3pos = {chevX1, chevY1 }
123
    mon.setCursorPos(math.floor(chev3pos[1]), math.floor(chev3pos[2]+1))
124
    mon.write(" "..chevInfo[2].." ")
125
  elseif chevInfo[1] == 4 then
126
    chev4pos = {chevX1 + ((chevX2 - chevX1) / 2), chevY1 }
127
    mon.setCursorPos(math.floor(chev4pos[1]-1), math.floor(chev4pos[2]))
128
    mon.write(" "..chevInfo[2].." ")
129
  elseif chevInfo[1] == 5 then
130
    chev5pos = {chevX2, chevY1 }
131
    mon.setCursorPos(math.floor(chev5pos[1]-2), math.floor(chev5pos[2])+1)
132
    mon.write(" "..chevInfo[2].." ")
133
  elseif chevInfo[1] == 6 then
134
    chev6pos = {chevX2, chevY1 + ((chevY2 - chevY1) / 2) }
135
    mon.setCursorPos(math.floor(chev6pos[1]-1), math.floor(chev6pos[2]))
136
    mon.write(" "..chevInfo[2].." ")
137
  elseif chevInfo[1] == 7 then
138
    chev7pos = {chevX2, chevY2 }
139
    mon.setCursorPos(math.floor(chev7pos[1]-2), math.floor(chev7pos[2]-1))
140
    mon.write(" "..chevInfo[2].." ")
141
  elseif chevInfo[1] == 8 then
142
    chev8pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
143
    mon.setCursorPos(math.floor(chev8pos[1]-1), math.floor(chev8pos[2]))
144
    mon.write(" "..chevInfo[2].." ")
145
  elseif chevInfo[1] == 9 then
146
    chev9pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
147
    mon.setCursorPos(math.floor(chev8pos[1]-1), chevY1 + ((chevY2 - chevY1) / 2))
148
    mon.write(" "..chevInfo[2].." ")
149
  mon.setBackgroundColor(colors.black)
150
end
151
152
end
153
154
function drawSgStatus(status) -- draws stargate status
155
  if status ~= "Idle" then
156
  term.setCursorPos(1,2)
157
  write(status) --needed for sting length because sting.len() won't work with stargateStatus()
158
  xc, yc = term.getCursorPos()
159
  term.clear()
160
  term.setCursorPos(1,2)
161
  write("> ")
162
  if xc%2 == 1 then
163
    xc = xc+1
164
	even = true
165
  else
166
    even = false
167
  end
168
  mon.setBackgroundColor(colors.black)
169
  if status == "Connected" then
170
    mon.setTextColor(colors.lightBlue)
171
  elseif status == "Dialling" then
172
    mon.setTextColor(colors.orange)
173
  else
174
    mon.setTextColor(colors.green)
175
  end
176
  x,y = mon.getSize()
177
  mon.setCursorPos((x/2+1) - 6, y/2+2)
178
  mon.write("            ")
179
  mon.setCursorPos((x/2+1) - (xc/2-1), y/2+2)
180
  mon.write(status)
181
  if even == true then
182
    mon.write(".")
183
  end
184
  end
185
end
186
187
function drawIris(state) --draws button to control the Iris
188
  mon.setBackgroundColor(colors.lightGray)
189
  ok, result = pcall(sg.openIris)
190
  if ok == false then
191
    mon.setTextColor(colors.red)
192
  elseif state == true then
193
    sg.closeIris()
194
    mon.setTextColor(colors.lime)
195
  else
196
	mon.setTextColor(colors.black)
197
	sg.openIris()
198
  end
199
  s = "   IRIS   "
200
  i = 1
201
  for  yc = y/3-1, y/3*2 +1 do
202
    char = string.sub(s, i, i)
203
	mon.setCursorPos(6, yc)
204
	mon.write(" "..char.." ")
205
	i = i+1
206
  end
207
  if state == true then
208
    mon.setTextColor(colors.lime)
209
  else
210
	mon.setTextColor(colors.black)
211
  end
212
end
213
214
function drawLocalAddress() -- draws the address stargate being controlled 
215
  x,y = mon.getSize()
216
  mon.setBackgroundColor(colors.black)
217
  mon.setTextColor(colors.lightGray)
218
  mon.setCursorPos(x/2-7, 1)
219
  mon.write("Stargate Address:")
220
  mon.setCursorPos(x/2-3, 2)
221
  mon.write(sg.localAddress())
222
end
223
224
function drawDial() -- draws the button to access the dialing menu
225
  x,y = mon.getSize()
226
  state, int = sg.stargateState()
227
  for yc = y-3, y-1 do
228
    for xc = x/2-5, x/2 do
229
	  if state == "Idle" then
230
	    mon.setBackgroundColor(colors.lightGray)
231
	  else
232
	    mon.setBackgroundColor(colors.gray)
233
	  end
234
	  mon.setCursorPos(xc,yc)
235
	  mon.write(" ")
236
	end
237
  end
238
  mon.setCursorPos(x/2-4, y-2)
239
  mon.setTextColor(colors.black)
240
  mon.write("DIAL")
241
end
242
243
function drawTerm() -- draws the button to terminate the stargate connection to another gate
244
  x,y = mon.getSize()
245
  state, int = sg.stargateState()
246
  for yc = y-3, y-1 do
247
    for xc = x/2+2, x/2+7 do
248
	  if state == "Connected" or state == "Connecting" or state == "Dialling" then
249
	    mon.setBackgroundColor(colors.lightGray)
250
	  else
251
	    mon.setBackgroundColor(colors.gray)
252
	  end
253
	  mon.setCursorPos(xc,yc)
254
	  mon.write(" ")
255
	end
256
  end
257
  mon.setCursorPos(x/2+3, y-2)
258
  mon.setTextColor(colors.black)
259
  mon.write("TERM")
260
end 
261
262
function securityButton() -- draws the button to access the security menu
263
  x,y = mon.getSize()
264
  mon.setBackgroundColor(colors.lightGray)
265
  sOK, result = pcall(sg.openIris)
266
  if sOK == false then
267
    mon.setTextColor(colors.red)
268
  else
269
    mon.setTextColor(colors.black)
270
  end
271
  s = " DEFENCE "
272
  i = 1
273
  for  yc = y/3-1, y/3*2 +1 do
274
    char = string.sub(s, i, i)
275
	mon.setCursorPos(2, yc)
276
	mon.write(" "..char.." ")
277
	i = i+1
278
  end
279
  mon.setBackgroundColor(colors.black)
280
end
281
282
function drawSecurityPageTop() --draws the top of the security menu, all the addresses stored in the security table
283
  mon.setBackgroundColor(colors.black)
284
  mon.clear()
285
  mon.setTextColor(colors.black)
286
  x,y = mon.getSize()
287
  for yc = 1,y-3 do
288
    if yc%2 == 1 then
289
      mon.setBackgroundColor(colors.lightBlue)
290
	else
291
	  mon.setBackgroundColor(colors.lightGray)
292
	end
293
	for xc = 1,x do
294
	  mon.setCursorPos(xc, yc)
295
	  mon.write(" ")
296
	end
297
	mon.setCursorPos(x/2-4, yc)
298
	mon.write("Add Address")
299
  end
300
  if fs.exists("secList") then
301
    file = fs.open("secList","r")
302
	secInfo = textutils.unserialize(file.readAll())
303
	file.close()
304
	if string.len(textutils.serialize(secInfo)) > 7 then
305
    for k,v in pairs(secInfo) do
306
	  mon.setCursorPos(1,i)
307
	    if k%2 == 1 then
308
          mon.setBackgroundColor(colors.lightBlue)
309
	    else
310
	      mon.setBackgroundColor(colors.lightGray)
311
	    end
312
	    mon.setCursorPos(1, k)
313
		mon.write(v.name)
314
	    mon.setCursorPos(x/2-4, k)
315
	    mon.write("           ")
316
	    mon.setCursorPos(x/2 - string.len(v.address)/2 +1, k)
317
	    mon.write(v.address)
318
	    mon.setCursorPos(x,k)
319
	    mon.setBackgroundColor(colors.red)
320
	    mon.write("X")
321
	end
322
	end
323
  end 
324
  mon.setBackgroundColor(colors.black)
325
end
326
  
327
function drawSecurityPageBottom(listType) -- draws the buttons at the bottom of the security page
328
  for yc = y-2, y do
329
    for xc = 1, x do
330
      mon.setCursorPos(xc, yc)
331
	  if listType == "BLACKLIST" then
332
	    mon.setBackgroundColor(colors.black)
333
	    mon.setTextColor(colors.white)
334
      elseif listType == "WHITELIST" then
335
	    mon.setBackgroundColor(colors.white)
336
	    mon.setTextColor(colors.black)
337
	  elseif listType == "NONE" then
338
	    mon.setBackgroundColor(colors.gray)
339
	    mon.setTextColor(colors.white)
340
	  end
341
	  mon.write(" ")
342
	end
343
  end
344
  mon.setCursorPos((x/2 - tonumber(string.len(listType)/2)+1), y-1)
345
  mon.write(listType)
346
  mon.setCursorPos(x-5, y-1)
347
  mon.write("BACK")
348
  mon.setBackgroundColor(colors.black)
349
end  
350
351
function drawHome() -- draws the home screen
352
  mon.setBackgroundColor(colors.black)
353
  x,y = mon.getSize()
354
  mon.clear()
355
  mon.setCursorPos(1,y)
356
  mon.setTextColor(colors.gray)
357
  mon.setBackgroundColor(colors.black)
358
  mon.write("SirRedstone")
359
  drawPowerBar()
360
  drawChevrons()
361
  status, int = sg.stargateState()
362
  drawSgStatus(tostring(status))
363
  drawHistoryButton()
364
  if sg.irisState()  == "Open" then
365
    drawIris(false)
366
  else
367
    drawIris(true)
368
  end
369
  drawLocalAddress()
370
  securityButton()
371
  drawDial()
372
  mon.setCursorBlink(false)
373
  drawTerm()
374
end
375
376
function drawBookmarksPage()
377
  mon.setBackgroundColor(colors.black)
378
  mon.clear()
379
  mon.setTextColor(colors.black)
380
  x,y = mon.getSize()
381
  for yc = 1,y-3 do
382
    if yc%2 == 1 then
383
      mon.setBackgroundColor(colors.lightBlue)
384
	else
385
	  mon.setBackgroundColor(colors.lightGray)
386
	end
387
	for xc = 1,x do
388
	  mon.setCursorPos(xc, yc)
389
	  mon.write(" ")
390
	end
391
  end
392
  for i= 1,y do
393
  	if i%2 == 1 then
394
      mon.setBackgroundColor(colors.lightBlue)
395
	else
396
	  mon.setBackgroundColor(colors.lightGray)
397
	end
398
    if fs.exists(tostring(i)) then
399
      file = fs.open(tostring(i),"r")
400
	  bookmark = textutils.unserialize(file.readAll())
401
	  file.close()
402
	  mon.setCursorPos(1,i)
403
	  for k,v in pairs(bookmark) do
404
	    if k == "name" then
405
	      mon.write(v)
406
		  mon.setCursorPos(x/2, i)
407
		  mon.write(bookmark.address)
408
	      mon.setCursorPos(x,i)
409
	      mon.setBackgroundColor(colors.red)
410
	      mon.write("X")
411
	    end
412
	  end
413
	elseif i < y-2 then
414
	  mon.setCursorPos(1, i)
415
	  mon.write("Add Address")
416
	end
417
  end
418
  mon.setCursorPos(x/2, y-1)
419
  mon.setBackgroundColor(colors.black)
420
  mon.setTextColor(colors.white)
421
  mon.write("BACK")
422
end
423
424
function drawRemoteIris()
425
  mon.setBackgroundColor(colors.black)
426
  x,y = mon.getSize()
427
  mon.setCursorPos(x/2-1, y/2+4)
428
  mon.write("IRIS.")
429
end
430
431
function inputPage(type)
432
  mon.clear()
433
  term.redirect(mon)
434
  term.setBackgroundColor(colors.lightGray)
435
  term.clear()
436
  x,y = term.getSize()
437
  term.setCursorPos(x/2-8, y/2-2)
438
  print("Set an address name")
439
  term.setCursorPos(x/2 - 4, y/2)
440
  print("         ")
441
  term.setCursorPos(x/2 - 4, y/2)
442
  nameInput = read()
443
  addressInput = "nil"
444
  term.setBackgroundColor(colors.lightGray)
445
  term.clear()
446
  term.setCursorPos(x/2-9, y/2-4)
447
  print("Enter Stargate address")
448
  if type == "secEntry" then
449
    term.setCursorPos(x/2-10, y/2-2)
450
    print("DO NOT ENTER ANY HYPHONS")
451
  end
452
  term.setBackgroundColor(colors.black)
453
  term.setCursorPos(x/2 - 5, y/2)
454
  print("           ")
455
  term.setCursorPos(x/2 - 5, y/2)
456
  addressInput = string.upper(read())
457
  newGate ={name = nameInput, address = addressInput}
458
  term.redirect(term.native())
459
  return newGate
460
end
461
462
function drawRemoteAddress()
463
  mon.setBackgroundColor(colors.black)
464
  x,y = mon.getSize()
465
  mon.setCursorPos((x/2+1) - string.len(sg.remoteAddress())/2, y/2-2)
466
  mon.write(sg.remoteAddress())
467
end
468
469
function drawHistoryButton()
470
  mon.setBackgroundColor(colors.lightGray)
471
  mon.setTextColor(colors.black)
472
  s = " HISTORY "
473
  i = 1
474
  for  yc = y/3-1, y/3*2 +1 do
475
    char = string.sub(s, i, i)
476
	mon.setCursorPos(x-7, yc)
477
	mon.write(" "..char.." ")
478
	i = i+1
479
  end
480
end
481
482
function addToHistory(address)
483
  if fs.exists("history") then
484
    file = fs.open("history", "r")
485
	history = textutils.unserialize(file.readAll())
486
	file.close()
487
  else
488
	history ={}
489
	print("")
490
	print("")
491
	print("no history file")
492
  end
493
  if textutils.serialize(history) == false then
494
    history = {}
495
	print("")
496
	print("")
497
	print("couldn't serialize")
498
  end
499
  test = textutils.serialize(historyTable)
500
  if string.len(test) < 7 then
501
    history = {}
502
	print("")
503
	print("")
504
	print("string.len too short")
505
  end
506
  table.insert(history, 1, address)
507
  file = fs.open("history", "w")
508
  file.write(textutils.serialize(history))
509
  file.close()
510
end
511
512
function drawHistoryPage()
513
  mon.setBackgroundColor(colors.black)
514
  mon.clear()
515
  mon.setTextColor(colors.black)
516
  x,y = mon.getSize()
517
  for yc = 1,y-3 do
518
    if yc%2 == 1 then
519
      mon.setBackgroundColor(colors.lightBlue)
520
	else
521
	  mon.setBackgroundColor(colors.lightGray)
522
	end
523
	for xc = 1,x do
524
	  mon.setCursorPos(xc, yc)
525
	  mon.write(" ")
526
	end
527
  end
528
  if fs.exists("history") then
529
    file = fs.open("history","r")
530
	historyTable = textutils.unserialize(file.readAll())
531
	file.close()
532
	test = textutils.serialize(historyTable)
533
	if string.len(test) > 7 then
534
      for k,v in pairs(historyTable) do
535
	    if k%2 == 1 then
536
          mon.setBackgroundColor(colors.lightBlue)
537
	    else
538
	      mon.setBackgroundColor(colors.lightGray)
539
	    end
540
	    mon.setCursorPos(1,k)
541
		mon.write(v)
542
	    mon.setCursorPos(x/2+7, k)
543
	    mon.setBackgroundColor(colors.blue)
544
	    mon.write("SAVE")
545
	    mon.setCursorPos(x-8, k)
546
	    mon.setBackgroundColor(colors.red)
547
	    mon.write("BAN/ALLOW")
548
		clickLimit = k
549
	  end
550
	end
551
	test = {}
552
  end 
553
  mon.setBackgroundColor(colors.black)
554
  for yc = y-2, y do
555
    for xc = 1,x do
556
	  mon.setCursorPos(xc, yc)
557
	  mon.write(" ")
558
	end
559
  end
560
  mon.setCursorPos(x/2, y-1)
561
  mon.setTextColor(colors.white)
562
  mon.write("BACK")
563
end
564
565
function historyInputPage(address)
566
  cx, cy = term.getCursorPos()
567
  mon.clear()
568
  term.redirect(mon)
569
  term.setBackgroundColor(colors.lightGray)
570
  term.clear()
571
  x,y = term.getSize()
572
  term.setCursorPos(x/2-8, y/2-2)
573
  print("Set an address name")
574
  term.setCursorPos(x/2 - 4, y/2)
575
  print("         ")
576
  term.setCursorPos(x/2 - 4, y/2)
577
  nameInput = read()
578
  addressInput = "nil"
579
  newGate ={name = nameInput, address = address}
580
  term.redirect(term.native())
581
  term.clear()
582
  term.setCursorPos(1,1)
583
  return newGate
584
end
585
586
587
if fs.exists("currentSec") then -- checks to see if there's list of gates stored for security reasons
588
  file = fs.open("currentSec", "r")
589
  currentSec = file.readAll()
590
  file.close()
591
else
592
  currentSec = "NONE"
593
end
594
mon.setTextScale(1)
595
drawHome()
596
while true do
597
  event, param1, param2, param3 = os.pullEvent()
598
  if event == "monitor_touch" then
599
    x,y = mon.getSize()
600
    if param2 >= 6 and param2 <= 8 and param3 >= y/3-2 and param3 <= y/3*2+1 then --opens or closes the Iris
601
	  if sg.irisState() == "Closed" then
602
	    ok, result = pcall(sg.openIris)
603
	    if ok then
604
		  drawIris(false)
605
		end
606
      else
607
	    ok, result = pcall(sg.closeIris)
608
		  if ok then
609
	        drawIris(true)
610
		  end
611
      end
612
	elseif param2 >= 2 and param2 <= 4 and param3 >= y/3-2 and param3 <= y/3*2+1 then -- click has opened the security menu
613
	  sOK, result = pcall(sg.openIris)
614
      if sOK then
615
	  while true do
616
	    drawSecurityPageTop()
617
	    drawSecurityPageBottom(currentSec)
618
	    event, param1, param2, param3 = os.pullEvent()
619
	    if event == "monitor_touch" then
620
	      if param3 >= y-2 then --checks if the user's touch is at the bottom of the screen with the buttons
621
		    if param2 >= x-8 then -- "back" button has been pushed, returns user to home menu
622
		      drawHome()
623
			  break
624
	        elseif param2 < x-6 then -- Click has changed the security type, cycles through "BLACKLIST", "WHITELIST", "NONE"
625
		      if currentSec == "BLACKLIST" then
626
			    currentSec = "WHITELIST"
627
			  elseif currentSec == "WHITELIST" then
628
			    currentSec = "NONE"			
629
			  elseif currentSec == "NONE" then
630
			      currentSec = "BLACKLIST"
631
			  end
632
			  file = fs.open("currentSec", "w")
633
			  file.write(currentSec)
634
			  file.close()
635
		    end
636
		  elseif param2 > x - 3 then -- delete record
637
              file = fs.open("secList", "r")
638
			  secList = textutils.unserialize(file.readAll())
639
			  file.close()
640
			  table.remove(secList, param3)
641
			  file = fs.open("secList", "w")
642
			  file.write(textutils.serialize(secList))
643
			  file.close()
644
			  drawSecurityPageTop()
645
		  elseif param3 < y - 2 then -- check if empty, if so add new entry	  
646
            if fs.exists("secList") == false then
647
			  secList = {}
648
			  table.insert(secList, 1, inputPage())
649
			  file = fs.open("secList", "w")
650
			  file.write(textutils.serialize(secList))
651
			  file.close()
652
			else
653
              file = fs.open("secList", "r")
654
			  secList = textutils.unserialize(file.readAll())
655
			  file.close()
656
			  table.insert(secList, 1, inputPage("secEntry"))
657
			  file = fs.open("secList", "w")
658
			  file.write(textutils.serialize(secList))
659
			  file.close()
660
			end
661
			drawSecurityPageTop()
662
	        drawSecurityPageBottom(currentSec)
663
		  end
664
	    else -- if an event that isn't a users touch happens the screen will return to the home screen (in case of incoming connection)
665
	      drawHome()
666
	      break
667
	    end
668
	  end
669
	  end
670
	elseif param2 > x/2-5 and param2 <= x/2 and param3 >= y-3 and param3 <= y-1 then -- Click has opened dial menu
671
	  status, int = sg.stargateState()
672
	  if status == "Idle" then
673
	  while true do
674
	    drawBookmarksPage()
675
		event, param1, param2, param3 = os.pullEvent()
676
		if event == "monitor_touch" then
677
		  if param3 >= y-2 then -- user clicked back
678
		    drawHome()
679
			break
680
	      elseif param2 > x-2 then -- user clicked delete on a bookmark
681
		    if fs.exists(tostring(param3)) then
682
			  fs.delete(tostring(param3))
683
			end
684
		  else -- user has clicked on a bookmark
685
		    if fs.exists(tostring(param3)) then
686
			  file = fs.open(tostring(param3), "r")
687
			  gateData = textutils.unserialize(file.readAll()) -- GATE DATA VARIABLE!!!
688
			  file.close()
689
			  drawHome()
690
			  for k,v in pairs(gateData) do
691
			    if k == "address" then
692
                  ok, result = pcall(sg.dial, v)
693
                  if ok then
694
				    status, int = sg.stargateState()
695
                    drawSgStatus(status)
696
					address = v
697
					addToHistory(v)
698
                  else
699
                    drawSgStatus("Error")
700
		          end
701
				end
702
				sleep(.5)
703
			  end
704
			  break
705
			else
706
			  x,y = mon.getSize()
707
			  for i = 1,y do
708
			    if fs.exists(tostring(i)) == false then
709
                  file = fs.open(tostring(i), "w")
710
				  file.write(textutils.serialize(inputPage()))
711
				  file.close()
712
				  break
713
				end
714
			  end
715
			end
716
          end
717
		else
718
	      drawHome()
719
	      break
720
	    end
721
	  end
722
	  end
723
	elseif param2 > x-7 and param2 < x-4 and param3 >= y/3-2 and param3 <= y/3*2+1 then -- Click has opened history menu
724
	  while true do
725
	    drawHistoryPage()
726
		event, param1, param2, param3 = os.pullEvent()
727
		if event == "monitor_touch" then
728
		  if param3 >= y-2 then -- user clicked back
729
		    drawHome()
730
			break --might break everything
731
          elseif param2 >= x/2+7 and param2 <= x/2+10 and param3 <= clickLimit then -- user has clicked save.
732
			if fs.exists("history") then
733
              file = fs.open("history", "r")
734
		      history = textutils.unserialize(file.readAll())
735
			  file.close()
736
			  for i = 1,y do
737
				if fs.exists(tostring(i)) == false then
738
				  file = fs.open(tostring(i), "w")
739
			      file.write(textutils.serialize(historyInputPage(history[param3])))
740
			      file.close()
741
				  break
742
				end
743
			  end
744
			end
745
		  elseif param2 >= x-9 and param3 <= clickLimit then -- user click "ban/allow"
746
		    if fs.exists("history") then
747
              file = fs.open("history", "r")
748
		      history = textutils.unserialize(file.readAll())
749
			  file.close()
750
			  if fs.exists("secList") == false then
751
			    secList = {}
752
			    table.insert(secList, 1, historyInputPage(history[param3]))
753
			    file = fs.open("secList", "w")
754
			    file.write(textutils.serialize(secList))
755
			    file.close()
756
			  else
757
                file = fs.open("secList", "r")
758
			    secList = textutils.unserialize(file.readAll())
759
			    file.close()
760
			    table.insert(secList, 1, historyInputPage(history[param3]))
761
			    file = fs.open("secList", "w")
762
			    file.write(textutils.serialize(secList))
763
			    file.close()
764
			  end
765
			end
766
		  end
767
		  drawHome()
768
	      break  
769
	    end		
770
	  end
771
	elseif param2 > x/2+2 and param2 <= x/2+7 and param3 >= y-3 and param3 <= y-1 then -- user clicked TERM
772
	  ok, result = pcall(sg.disconnect)
773
	  drawChevrons()
774
	end
775
  elseif event == "sgDialIn" then
776
	mon.setTextColor(colors.orange)
777
	drawRemoteAddress()
778
	alarmSet(true)
779
	if fs.exists("currentSec") then
780
      file = fs.open("currentSec", "r")
781
	  currentSec = file.readAll()
782
	  file.close()
783
	end
784
	if fs.exists("secList") then
785
	  file = fs.open("secList", "r")
786
	  secList = textutils.unserialize(file.readAll())
787
	  for k,v in pairs(secList) do
788
	    address = v.address
789
	    if string.sub(v.address,1,7) == param2 or v.address == param2 then
790
	      if currentSec == "BLACKLIST" then
791
		    sg.closeIris()
792
		    drawIris(true)
793
		  elseif currentSec == "WHITELIST" then
794
		      sg.openIris()
795
			  drawIris(false)
796
		  else
797
		    sg.openIris()
798
			drawIris(false)
799
		  end
800
		  secGate = true
801
	    end
802
	  end
803
	end
804
    if secGate == true and currentSec == "WHITELIST" then
805
	  sg.openIris()
806
	  drawIris(false)
807
	  gateSec = false
808
	end
809
	addToHistory(param2)
810
  elseif event == "sgMessageReceived" then
811
	if param2 == "Open" then
812
	  mon.setTextColor(colors.lime)
813
	  drawRemoteIris()
814
	elseif param2 == "Closed" then
815
	  mon.setTextColor(colors.red)
816
	  drawRemoteIris()
817
	end	  
818
  elseif event == "sgStargateStateChange" or "sgChevronEngaged" then
819
    drawDial()
820
    drawPowerBar()
821
    drawTerm()
822
	status, int = sg.stargateState()
823
    drawSgStatus(tostring(status))
824
	if status == "idle" then
825
	  isConnected = false
826
	else
827
	  isConnected = true
828
	end
829
	if event == "sgChevronEngaged" then
830
	  mon.setTextColor(colors.orange)
831
	  drawChev({param2, param3})
832
	  if param2 == 1 then
833
	    dialling = {}
834
	  end
835
	  table.insert(dialling, param2, param3)
836
	  drawRemoteAddress()
837
	elseif param2 == "Idle" then
838
	  alarmSet(false)
839
	  drawChevrons()
840
	elseif param2 == "Connected" then
841
	  alarmSet(false)
842
	  mon.setTextColor(colors.lightBlue)
843
      drawRemoteAddress()
844
	  for k,v in pairs(dialling) do
845
	    drawChev({k,v})
846
	  end
847
	  sg.sendMessage(sg.irisState())
848
	end
849
  end
850
end