View difference between Paste ID: HVXe5ZZs and WT15FXJf
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("back", 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 currentSec ~= "WHITELIST" then
335
        sg.closeIris()
336
        drawIris(true)
337
      elseif listType == "WHITELIST" then
338
	    mon.setBackgroundColor(colors.white)
339
	    mon.setTextColor(colors.black)
340
	  elseif listType == "NONE" then
341
	    mon.setBackgroundColor(colors.gray)
342
	    mon.setTextColor(colors.white)
343
	  end
344
	  mon.write(" ")
345
	end
346
  end
347
  mon.setCursorPos((x/2 - tonumber(string.len(listType)/2)+1), y-1)
348
  mon.write(listType)
349
  mon.setCursorPos(x-5, y-1)
350
  mon.write("BACK")
351
  mon.setBackgroundColor(colors.black)
352
end  
353
354
function drawHome() -- draws the home screen
355
  mon.setBackgroundColor(colors.black)
356
  x,y = mon.getSize()
357
  mon.clear()
358
  mon.setCursorPos(1,y)
359
  mon.setTextColor(colors.gray)
360
  mon.setBackgroundColor(colors.black)
361
  mon.write("           ")
362
  drawPowerBar()
363
  drawChevrons()
364
  status, int = sg.stargateState()
365
  drawSgStatus(tostring(status))
366
  drawHistoryButton()
367
  if sg.irisState()  == "Open" then
368
    drawIris(false)
369
  else
370
    drawIris(true)
371
  end
372
  drawLocalAddress()
373
  securityButton()
374
  drawDial()
375
  mon.setCursorBlink(false)
376
  drawTerm()
377
end
378
379
function drawBookmarksPage()
380
  mon.setBackgroundColor(colors.black)
381
  mon.clear()
382
  mon.setTextColor(colors.black)
383
  x,y = mon.getSize()
384
  for yc = 1,y-3 do
385
    if yc%2 == 1 then
386
      mon.setBackgroundColor(colors.lightBlue)
387
	else
388
	  mon.setBackgroundColor(colors.lightGray)
389
	end
390
	for xc = 1,x do
391
	  mon.setCursorPos(xc, yc)
392
	  mon.write(" ")
393
	end
394
  end
395
  for i= 1,y do
396
  	if i%2 == 1 then
397
      mon.setBackgroundColor(colors.lightBlue)
398
	else
399
	  mon.setBackgroundColor(colors.lightGray)
400
	end
401
    if fs.exists(tostring(i)) then
402
      file = fs.open(tostring(i),"r")
403
	  bookmark = textutils.unserialize(file.readAll())
404
	  file.close()
405
	  mon.setCursorPos(1,i)
406
	  for k,v in pairs(bookmark) do
407
	    if k == "name" then
408
	      mon.write(v)
409
		  mon.setCursorPos(x/2, i)
410
		  mon.write(bookmark.address)
411
	      mon.setCursorPos(x,i)
412
	      mon.setBackgroundColor(colors.red)
413
	      mon.write("X")
414
	    end
415
	  end
416
	elseif i < y-2 then
417
	  mon.setCursorPos(1, i)
418
	  mon.write("Add Address")
419
	end
420
  end
421
  mon.setCursorPos(x/2, y-1)
422
  mon.setBackgroundColor(colors.black)
423
  mon.setTextColor(colors.white)
424
  mon.write("BACK")
425
end
426
427
function drawRemoteIris()
428
  mon.setBackgroundColor(colors.black)
429
  x,y = mon.getSize()
430
  mon.setCursorPos(x/2-1, y/2+4)
431
  mon.write("IRIS")
432
end
433
434
function inputPage(type)
435
  mon.clear()
436
  term.redirect(mon)
437
  term.setBackgroundColor(colors.lightGray)
438
  term.clear()
439
  x,y = term.getSize()
440
  term.setCursorPos(x/2-8, y/2-2)
441
  print("Set an address name")
442
  term.setCursorPos(x/2 - 4, y/2)
443
  print("         ")
444
  term.setCursorPos(x/2 - 4, y/2)
445
  nameInput = read()
446
  addressInput = "nil"
447
  term.setBackgroundColor(colors.lightGray)
448
  term.clear()
449
  term.setCursorPos(x/2-9, y/2-4)
450
  print("Enter Stargate address")
451
  if type == "secEntry" then
452
    term.setCursorPos(x/2-10, y/2-2)
453
    print("DO NOT ENTER ANY HYPHONS")
454
  end
455
  term.setBackgroundColor(colors.black)
456
  term.setCursorPos(x/2 - 5, y/2)
457
  print("           ")
458
  term.setCursorPos(x/2 - 5, y/2)
459
  addressInput = string.upper(read())
460
  newGate ={name = nameInput, address = addressInput}
461
  term.redirect(term.native())
462
  return newGate
463
end
464
465
function drawRemoteAddress()
466
  mon.setBackgroundColor(colors.black)
467
  x,y = mon.getSize()
468
  mon.setCursorPos((x/2+1) - string.len(sg.remoteAddress())/2, y/2-2)
469
  mon.write(sg.remoteAddress())
470
end
471
472
function drawHistoryButton()
473
  mon.setBackgroundColor(colors.lightGray)
474
  mon.setTextColor(colors.black)
475
  s = " HISTORY "
476
  i = 1
477
  for  yc = y/3-1, y/3*2 +1 do
478
    char = string.sub(s, i, i)
479
	mon.setCursorPos(x-7, yc)
480
	mon.write(" "..char.." ")
481
	i = i+1
482
  end
483
end
484
485
function addToHistory(address)
486
  if fs.exists("history") then
487
    file = fs.open("history", "r")
488
	history = textutils.unserialize(file.readAll())
489
	file.close()
490
  else
491
	history ={}
492
	print("")
493
	print("")
494
	print("no history file")
495
  end
496
  if textutils.serialize(history) == false then
497
    history = {}
498
	print("")
499
	print("")
500
	print("couldn't serialize")
501
  end
502
  test = textutils.serialize(historyTable)
503
  if string.len(test) < 7 then
504
    history = {}
505
	print("")
506
	print("")
507
	print("string.len too short")
508
  end
509
  table.insert(history, 1, address)
510
  file = fs.open("history", "w")
511
  file.write(textutils.serialize(history))
512
  file.close()
513
end
514
515
function drawHistoryPage()
516
  mon.setBackgroundColor(colors.black)
517
  mon.clear()
518
  mon.setTextColor(colors.black)
519
  x,y = mon.getSize()
520
  for yc = 1,y-3 do
521
    if yc%2 == 1 then
522
      mon.setBackgroundColor(colors.lightBlue)
523
	else
524
	  mon.setBackgroundColor(colors.lightGray)
525
	end
526
	for xc = 1,x do
527
	  mon.setCursorPos(xc, yc)
528
	  mon.write(" ")
529
	end
530
  end
531
  if fs.exists("history") then
532
    file = fs.open("history","r")
533
	historyTable = textutils.unserialize(file.readAll())
534
	file.close()
535
	test = textutils.serialize(historyTable)
536
	if string.len(test) > 7 then
537
      for k,v in pairs(historyTable) do
538
	    if k%2 == 1 then
539
          mon.setBackgroundColor(colors.lightBlue)
540
	    else
541
	      mon.setBackgroundColor(colors.lightGray)
542
	    end
543
	    mon.setCursorPos(1,k)
544
		mon.write(v)
545
	    mon.setCursorPos(x/2+7, k)
546
	    mon.setBackgroundColor(colors.blue)
547
	    mon.write("SAVE")
548
	    mon.setCursorPos(x-8, k)
549
	    mon.setBackgroundColor(colors.red)
550
	    mon.write("BAN/ALLOW")
551
		clickLimit = k
552
	  end
553
	end
554
	test = {}
555
  end 
556
  mon.setBackgroundColor(colors.black)
557
  for yc = y-2, y do
558
    for xc = 1,x do
559
	  mon.setCursorPos(xc, yc)
560
	  mon.write(" ")
561
	end
562
  end
563
  mon.setCursorPos(x/2, y-1)
564
  mon.setTextColor(colors.white)
565
  mon.write("BACK")
566
end
567
568
function historyInputPage(address)
569
  cx, cy = term.getCursorPos()
570
  mon.clear()
571
  term.redirect(mon)
572
  term.setBackgroundColor(colors.lightGray)
573
  term.clear()
574
  x,y = term.getSize()
575
  term.setCursorPos(x/2-8, y/2-2)
576
  print("Set an address name")
577
  term.setCursorPos(x/2 - 4, y/2)
578
  print("         ")
579
  term.setCursorPos(x/2 - 4, y/2)
580
  nameInput = read()
581
  addressInput = "nil"
582
  newGate ={name = nameInput, address = address}
583
  term.redirect(term.native())
584
  term.clear()
585
  term.setCursorPos(1,1)
586
  return newGate
587
end
588
589
590
if fs.exists("currentSec") then -- checks to see if there's list of gates stored for security reasons
591
  file = fs.open("currentSec", "r")
592
  currentSec = file.readAll()
593
  file.close()
594
else
595
  currentSec = "NONE"
596
end
597
mon.setTextScale(1)
598
drawHome()
599
while true do
600
  event, param1, param2, param3 = os.pullEvent()
601
  if event == "monitor_touch" then
602
    x,y = mon.getSize()
603
    if param2 >= 6 and param2 <= 8 and param3 >= y/3-2 and param3 <= y/3*2+1 then --opens or closes the Iris
604
	  if sg.irisState() == "Closed" then
605
	    ok, result = pcall(sg.openIris)
606
	    if ok then
607
		  drawIris(false)
608
		end
609
      else
610
	    ok, result = pcall(sg.closeIris)
611
		  if ok then
612
	        drawIris(true)
613
		  end
614
      end
615
	elseif param2 >= 2 and param2 <= 4 and param3 >= y/3-2 and param3 <= y/3*2+1 then -- click has opened the security menu
616
	  sOK, result = pcall(sg.openIris)
617
      if sOK then
618
	  while true do
619
	    drawSecurityPageTop()
620
	    drawSecurityPageBottom(currentSec)
621
	    event, param1, param2, param3 = os.pullEvent()
622
	    if event == "monitor_touch" then
623
	      if param3 >= y-2 then --checks if the user's touch is at the bottom of the screen with the buttons
624
		    if param2 >= x-8 then -- "back" button has been pushed, returns user to home menu
625
		      drawHome()
626
			  break
627
	        elseif param2 < x-6 then -- Click has changed the security type, cycles through "BLACKLIST", "WHITELIST", "NONE"
628
		      if currentSec == "BLACKLIST" then
629
			    currentSec = "WHITELIST"
630
			  elseif currentSec == "WHITELIST" then
631
			    currentSec = "NONE"			
632
			  elseif currentSec == "NONE" then
633
			      currentSec = "BLACKLIST"
634
			  end
635
			  file = fs.open("currentSec", "w")
636
			  file.write(currentSec)
637
			  file.close()
638
		    end
639
		  elseif param2 > x - 3 then -- delete record
640
              file = fs.open("secList", "r")
641
			  secList = textutils.unserialize(file.readAll())
642
			  file.close()
643
			  table.remove(secList, param3)
644
			  file = fs.open("secList", "w")
645
			  file.write(textutils.serialize(secList))
646
			  file.close()
647
			  drawSecurityPageTop()
648
		  elseif param3 < y - 2 then -- check if empty, if so add new entry	  
649
            if fs.exists("secList") == false then
650
			  secList = {}
651
			  table.insert(secList, 1, inputPage())
652
			  file = fs.open("secList", "w")
653
			  file.write(textutils.serialize(secList))
654
			  file.close()
655
			else
656
              file = fs.open("secList", "r")
657
			  secList = textutils.unserialize(file.readAll())
658
			  file.close()
659
			  table.insert(secList, 1, inputPage("secEntry"))
660
			  file = fs.open("secList", "w")
661
			  file.write(textutils.serialize(secList))
662
			  file.close()
663
			end
664
			drawSecurityPageTop()
665
	        drawSecurityPageBottom(currentSec)
666
		  end
667
	    else -- if an event that isn't a users touch happens the screen will return to the home screen (in case of incoming connection)
668
	      drawHome()
669
	      break
670
	    end
671
	  end
672
	  end
673
	elseif param2 > x/2-5 and param2 <= x/2 and param3 >= y-3 and param3 <= y-1 then -- Click has opened dial menu
674
	  status, int = sg.stargateState()
675
	  if status == "Idle" then
676
	  while true do
677
	    drawBookmarksPage()
678
		event, param1, param2, param3 = os.pullEvent()
679
		if event == "monitor_touch" then
680
		  if param3 >= y-2 then -- user clicked back
681
		    drawHome()
682
			break
683
	      elseif param2 > x-2 then -- user clicked delete on a bookmark
684
		    if fs.exists(tostring(param3)) then
685
			  fs.delete(tostring(param3))
686
			end
687
		  else -- user has clicked on a bookmark
688
		    if fs.exists(tostring(param3)) then
689
			  file = fs.open(tostring(param3), "r")
690
			  gateData = textutils.unserialize(file.readAll()) -- GATE DATA VARIABLE!!!
691
			  file.close()
692
			  drawHome()
693
			  for k,v in pairs(gateData) do
694
			    if k == "address" then
695
                  ok, result = pcall(sg.dial, v)
696
                  if ok then
697
				    status, int = sg.stargateState()
698
                    drawSgStatus(status)
699
					address = v
700
					addToHistory(v)
701
                  else
702
                    drawSgStatus("Error")
703
		          end
704
				end
705
				sleep(.5)
706
			  end
707
			  break
708
			else
709
			  x,y = mon.getSize()
710
			  for i = 1,y do
711
			    if fs.exists(tostring(i)) == false then
712
                  file = fs.open(tostring(i), "w")
713
				  file.write(textutils.serialize(inputPage()))
714
				  file.close()
715
				  break
716
				end
717
			  end
718
			end
719
          end
720
		else
721
	      drawHome()
722
	      break
723
	    end
724
	  end
725
	  end
726
	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
727
	  while true do
728
	    drawHistoryPage()
729
		event, param1, param2, param3 = os.pullEvent()
730
		if event == "monitor_touch" then
731
		  if param3 >= y-2 then -- user clicked back
732
		    drawHome()
733
			break --might break everything
734
          elseif param2 >= x/2+7 and param2 <= x/2+10 and param3 <= clickLimit then -- user has clicked save.
735
			if fs.exists("history") then
736
              file = fs.open("history", "r")
737
		      history = textutils.unserialize(file.readAll())
738
			  file.close()
739
			  for i = 1,y do
740
				if fs.exists(tostring(i)) == false then
741
				  file = fs.open(tostring(i), "w")
742
			      file.write(textutils.serialize(historyInputPage(history[param3])))
743
			      file.close()
744
				  break
745
				end
746
			  end
747
			end
748
		  elseif param2 >= x-9 and param3 <= clickLimit then -- user click "ban/allow"
749
		    if fs.exists("history") then
750
              file = fs.open("history", "r")
751
		      history = textutils.unserialize(file.readAll())
752
			  file.close()
753
			  if fs.exists("secList") == false then
754
			    secList = {}
755
			    table.insert(secList, 1, historyInputPage(history[param3]))
756
			    file = fs.open("secList", "w")
757
			    file.write(textutils.serialize(secList))
758
			    file.close()
759
			  else
760
                file = fs.open("secList", "r")
761
			    secList = textutils.unserialize(file.readAll())
762
			    file.close()
763
			    table.insert(secList, 1, historyInputPage(history[param3]))
764
			    file = fs.open("secList", "w")
765
			    file.write(textutils.serialize(secList))
766
			    file.close()
767
			  end
768
			end
769
		  end
770
		  drawHome()
771
	      break  
772
	    end		
773
	  end
774
	elseif param2 > x/2+2 and param2 <= x/2+7 and param3 >= y-3 and param3 <= y-1 then -- user clicked TERM
775
	  ok, result = pcall(sg.disconnect)
776
	  drawChevrons()
777
	end
778
  elseif event == "sgDialIn" then
779
	mon.setTextColor(colors.orange)
780
	drawRemoteAddress()
781
	alarmSet(true)
782
	if fs.exists("currentSec") then
783
      file = fs.open("currentSec", "r")
784
	  currentSec = file.readAll()
785
	  file.close()
786
	end
787
	if fs.exists("secList") then
788
	  file = fs.open("secList", "r")
789
	  secList = textutils.unserialize(file.readAll())
790
	  for k,v in pairs(secList) do
791
	    address = v.address
792
	    if string.sub(v.address,1,7) == param2 or v.address == param2 then
793
	      if currentSec == "BLACKLIST" then
794
		    sg.closeIris()
795
		    drawIris(true)
796
		  elseif currentSec == "WHITELIST" then
797
		      sg.openIris()
798
			  drawIris(false)
799
		  else
800
		    sg.openIris()
801
			drawIris(false)
802
		  end
803
		  secGate = true
804
	    end
805
	  end
806
	end
807
    if secGate == true and currentSec == "WHITELIST" then
808
	  sg.openIris()
809
	  drawIris(false)
810
	  gateSec = false
811
	end
812
	addToHistory(param2)
813
  elseif event == "sgMessageReceived" then
814
	if param2 == "Open" then
815
	  mon.setTextColor(colors.lime)
816
	  drawRemoteIris()
817
	elseif param2 == "Closed" then
818
	  mon.setTextColor(colors.red)
819
	  drawRemoteIris()
820
	end	  
821
  elseif event == "sgStargateStateChange" or "sgChevronEngaged" then
822
    drawDial()
823
    drawPowerBar()
824
    drawTerm()
825
	status, int = sg.stargateState()
826
    drawSgStatus(tostring(status))
827
	if status == "idle" then
828
	  isConnected = false
829
	else
830
	  isConnected = true
831
	end
832
	if event == "sgChevronEngaged" then
833
	  mon.setTextColor(colors.orange)
834
	  drawChev({param2, param3})
835
	  if param2 == 1 then
836
	    dialling = {}
837
	  end
838
	  table.insert(dialling, param2, param3)
839
	  drawRemoteAddress()
840
	elseif param2 == "Idle" then
841
	  alarmSet(false)
842
	  drawChevrons()
843
	elseif param2 == "Connected" then
844
	  alarmSet(false)
845
	  mon.setTextColor(colors.lightBlue)
846
      drawRemoteAddress()
847
	  for k,v in pairs(dialling) do
848
	    drawChev({k,v})
849
	  end
850
	  sg.sendMessage(sg.irisState())
851
	end
852
  end
853
end