2016-11-06 20:36:22 +01:00
|
|
|
--
|
|
|
|
-- sPhone Application Package
|
|
|
|
-- Built with SPK builder 1
|
|
|
|
--
|
|
|
|
{
|
|
|
|
files = "{\
|
|
|
|
[ \"beam.lua\" ] = \"--[[\\\
|
|
|
|
\\\
|
|
|
|
Beam by Ale32bit\\\
|
|
|
|
base64 by MultMine\\\
|
|
|
|
\\\
|
|
|
|
Beam Engine Build 3\\\
|
|
|
|
\\\
|
|
|
|
]]--\\\
|
|
|
|
\\\
|
|
|
|
local base64 = \\\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\\\"\\\
|
|
|
|
.. \\\"abcdefghijklmnopqrstuvwxyz0123456789+/\\\"\\\
|
|
|
|
\\\
|
|
|
|
local function toBits(n)\\\
|
|
|
|
local t = {}\\\
|
|
|
|
for i = 8, 1, -1 do\\\
|
|
|
|
table.insert(t, bit.brshift(bit.band(n, 2^(i-1)), i - 1))\\\
|
|
|
|
end\\\
|
|
|
|
return t\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function encodeBlock(blk)\\\
|
|
|
|
local b64 = \\\"\\\"\\\
|
|
|
|
local b64_bits = {}\\\
|
|
|
|
for i = 1, 3 do\\\
|
|
|
|
local c = string.byte(blk:sub(i, i)) or 0\\\
|
|
|
|
for j = 8, 1, -1 do\\\
|
|
|
|
table.insert(b64_bits, bit.brshift(bit.band(c, 2^(j-1)), j - 1))\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
local byteCount = #blk + 1\\\
|
|
|
|
for i = 1, byteCount do\\\
|
|
|
|
local n = 0\\\
|
|
|
|
for j = 1, 6 do\\\
|
|
|
|
n = n + (2 ^ (6 - j)) * b64_bits[(i - 1) * 6 + j]\\\
|
|
|
|
end\\\
|
|
|
|
b64 = b64 .. base64:sub(n + 1, n + 1)\\\
|
|
|
|
end\\\
|
|
|
|
return b64 .. string.rep(\\\"=\\\", 4 - byteCount)\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function decodeBlock(b64)\\\
|
|
|
|
local msg = \\\"\\\"\\\
|
|
|
|
local msg_bits = {}\\\
|
|
|
|
for i = 1, 4 do\\\
|
|
|
|
local c = b64:sub(i, i):byte()\\\
|
|
|
|
if c >= 0x30 and c <= 0x39 then\\\
|
|
|
|
local t = toBits(c + 4)\\\
|
|
|
|
for j = 3, 8 do\\\
|
|
|
|
table.insert(msg_bits, t[j])\\\
|
|
|
|
end\\\
|
|
|
|
elseif c == string.byte(\\\"+\\\") then\\\
|
|
|
|
for j = 1, 5 do\\\
|
|
|
|
table.insert(msg_bits, 1)\\\
|
|
|
|
end\\\
|
|
|
|
table.insert(msg_bits, 0)\\\
|
|
|
|
elseif c == string.byte(\\\"/\\\") then\\\
|
|
|
|
for j = 1, 6 do\\\
|
|
|
|
table.insert(msg_bits, 1)\\\
|
|
|
|
end\\\
|
|
|
|
elseif c >= 65 and c <= string.byte(\\\"Z\\\") then\\\
|
|
|
|
local t = toBits(c - 65)\\\
|
|
|
|
for j = 3, 8 do\\\
|
|
|
|
table.insert(msg_bits, t[j])\\\
|
|
|
|
end\\\
|
|
|
|
elseif c >= 97 and c <= string.byte(\\\"z\\\") then\\\
|
|
|
|
local t = toBits(c - 71)\\\
|
|
|
|
for j = 3, 8 do\\\
|
|
|
|
table.insert(msg_bits, t[j])\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
for i = 1, #msg_bits / 8 do\\\
|
|
|
|
local n = 0\\\
|
|
|
|
for j = 1, 8 do\\\
|
|
|
|
n = n + (2 ^ (8 - j)) * msg_bits[(i - 1) * 8 + j]\\\
|
|
|
|
end\\\
|
|
|
|
msg = msg .. string.char(n)\\\
|
|
|
|
end\\\
|
|
|
|
return msg\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function encode(msg)\\\
|
|
|
|
local b64 = \\\"\\\"\\\
|
|
|
|
for i = 1, math.ceil(#msg / 3) do\\\
|
|
|
|
b64 = b64 .. encodeBlock(msg:sub((i - 1) * 3 + 1, i * 3))\\\
|
|
|
|
end\\\
|
|
|
|
return b64\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function decode(b64)\\\
|
|
|
|
local msg = \\\"\\\"\\\
|
|
|
|
for i = 1, #b64 / 4 do\\\
|
|
|
|
msg = msg .. decodeBlock(b64:sub((i - 1) * 4 + 1, i * 4))\\\
|
|
|
|
end\\\
|
|
|
|
return msg\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local version = 2.2\\\
|
|
|
|
local beamEngineBuild = 3\\\
|
|
|
|
local channel = 1\\\
|
|
|
|
local maxDistance = 30000000\\\
|
|
|
|
local status = true\\\
|
|
|
|
local config = \\\".beam-config\\\"\\\
|
|
|
|
local usedIDs = {}\\\
|
|
|
|
\\\
|
|
|
|
\\\
|
|
|
|
w,h = term.getSize()\\\
|
|
|
|
\\\
|
|
|
|
local function center(text)\\\
|
|
|
|
local _,y = term.getCursorPos()\\\
|
|
|
|
if pocket then\\\
|
|
|
|
term.setCursorPos(math.ceil(w/2)-math.ceil(#text/2)+1,y)\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(math.ceil(w/2)-math.ceil(#text/2),y)\\\
|
|
|
|
end\\\
|
|
|
|
term.write(text)\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function redraw()\\\
|
|
|
|
term.setBackgroundColor(colors.white)\\\
|
|
|
|
term.clear()\\\
|
|
|
|
paintutils.drawLine(1,1,w,1,colors.blue)\\\
|
|
|
|
term.setCursorPos(1,1)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.setBackgroundColor(colors.blue)\\\
|
|
|
|
term.write(\\\"Beam ID: \\\"..os.getComputerID())\\\
|
|
|
|
term.setCursorPos(w,1)\\\
|
|
|
|
term.setBackgroundColor(colors.red)\\\
|
|
|
|
term.write(\\\"X\\\")\\\
|
|
|
|
\\\
|
|
|
|
paintutils.drawFilledBox(2,4,w-1,6,colors.blue)\\\
|
|
|
|
term.setCursorPos(1,5)\\\
|
|
|
|
term.setBackgroundColor(colors.blue)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
center(\\\"Send File\\\")\\\
|
|
|
|
\\\
|
|
|
|
paintutils.drawFilledBox(2,8,w-1,10,colors.gray)\\\
|
|
|
|
term.setCursorPos(1,9)\\\
|
|
|
|
term.setBackgroundColor(colors.gray)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
center(\\\"Settings\\\")\\\
|
|
|
|
\\\
|
|
|
|
\\\
|
|
|
|
term.setCursorPos(1,2)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setBackgroundColor(colors.white)\\\
|
|
|
|
term.setCursorPos(1,h-3)\\\
|
|
|
|
if status then\\\
|
|
|
|
term.write(\\\"Status: Listening\\\")\\\
|
|
|
|
else\\\
|
|
|
|
term.write(\\\"Status: Standby\\\")\\\
|
|
|
|
end\\\
|
|
|
|
term.setCursorPos(1,h-2)\\\
|
|
|
|
term.write(\\\"Channel: \\\"..channel)\\\
|
|
|
|
term.setCursorPos(1,h-1)\\\
|
|
|
|
term.write(\\\"Max Distance: \\\"..maxDistance)\\\
|
|
|
|
term.setCursorPos(1,h)\\\
|
|
|
|
term.write(\\\"Version: \\\"..version)\\\
|
|
|
|
term.setCursorBlink(false)\\\
|
|
|
|
end\\\
|
|
|
|
local modem = peripheral.find(\\\"modem\\\")\\\
|
|
|
|
\\\
|
|
|
|
if not modem then\\\
|
|
|
|
term.setBackgroundColor(colors.black)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.clear()\\\
|
|
|
|
term.setCursorPos(1,1)\\\
|
|
|
|
printError(\\\"Modem not found\\\")\\\
|
|
|
|
return\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
modem.open(channel)\\\
|
|
|
|
\\\
|
|
|
|
while true do\\\
|
|
|
|
redraw()\\\
|
|
|
|
local ev = {os.pullEvent()}\\\
|
|
|
|
if ev[1] == \\\"mouse_click\\\" then\\\
|
|
|
|
local x,y = ev[3],ev[4]\\\
|
|
|
|
if x == w and y == 1 then\\\
|
|
|
|
modem.close(channel)\\\
|
|
|
|
term.setBackgroundColor(colors.black)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.clear()\\\
|
|
|
|
term.setCursorPos(1,1)\\\
|
|
|
|
print(\\\"Thanks for using Beam by Ale32bit\\\")\\\
|
|
|
|
break\\\
|
|
|
|
elseif (x >= 2 and y >= 4) and (x <= w-1 and y <= 6) then\\\
|
|
|
|
local function redrawSend()\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Send file\\\")\\\
|
|
|
|
term.setCursorPos(2,5)\\\
|
|
|
|
write(\\\"Receiver ID: \\\")\\\
|
|
|
|
term.setCursorPos(2,7)\\\
|
|
|
|
write(\\\"File: \\\")\\\
|
|
|
|
term.setCursorPos(15,5)\\\
|
|
|
|
term.setCursorBlink(true)\\\
|
|
|
|
end\\\
|
|
|
|
while true do\\\
|
|
|
|
redrawSend()\\\
|
|
|
|
local e,_,x,y = os.pullEvent()\\\
|
|
|
|
if e == \\\"mouse_click\\\" and x == w and y == 1 then\\\
|
|
|
|
break\\\
|
|
|
|
elseif e == \\\"key\\\" then\\\
|
|
|
|
term.setCursorPos(15,5)\\\
|
|
|
|
local receiver = read()\\\
|
|
|
|
if tonumber(receiver) then\\\
|
|
|
|
term.setCursorPos(8,7)\\\
|
|
|
|
local file = read()\\\
|
|
|
|
if file ~= \\\"\\\" and fs.exists(file) then\\\
|
|
|
|
local f = fs.open(file,\\\"r\\\")\\\
|
|
|
|
local con = f.readAll()\\\
|
|
|
|
f.close()\\\
|
|
|
|
-- Beam Engine\\\
|
|
|
|
local randomID = math.random(0,65535)\\\
|
|
|
|
usedIDs[randomID] = true\\\
|
|
|
|
modem.transmit(channel,os.getComputerID(),{\\\
|
|
|
|
content = encode(con),\\\
|
|
|
|
version = beamEngineBuild,\\\
|
|
|
|
receiver = encode(receiver),\\\
|
|
|
|
id = randomID,\\\
|
|
|
|
})\\\
|
|
|
|
\\\
|
|
|
|
term.setCursorPos(1,9)\\\
|
|
|
|
center(\\\"File sent to \\\"..receiver)\\\
|
|
|
|
break\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(2,9)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
term.write(\\\"The file does not exist\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(2,9)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
term.write(\\\"Receiver must be number\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
elseif (x >= 2 and y >= 8) and (x <= w-1 and y <= 10) then\\\
|
|
|
|
local function redrawSett()\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Settings\\\")\\\
|
|
|
|
term.setCursorPos(1,5)\\\
|
|
|
|
if status then\\\
|
|
|
|
center(\\\"Disable receiver\\\")\\\
|
|
|
|
else\\\
|
|
|
|
center(\\\"Enable receiver\\\")\\\
|
|
|
|
end\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
center(\\\"Set channel\\\")\\\
|
|
|
|
term.setCursorPos(1,7)\\\
|
|
|
|
center(\\\"Set max distance\\\")\\\
|
|
|
|
end\\\
|
|
|
|
while true do\\\
|
|
|
|
redrawSett()\\\
|
|
|
|
local _,_,x,y = os.pullEvent(\\\"mouse_click\\\")\\\
|
|
|
|
if x == w and y == 1 then\\\
|
|
|
|
break\\\
|
|
|
|
elseif y == 5 then\\\
|
|
|
|
if status then\\\
|
|
|
|
modem.close(channel)\\\
|
|
|
|
status = false\\\
|
|
|
|
else\\\
|
|
|
|
modem.open(channel)\\\
|
|
|
|
status = true\\\
|
|
|
|
end\\\
|
|
|
|
elseif y == 6 then\\\
|
|
|
|
while true do\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Set channel\\\")\\\
|
|
|
|
term.setCursorPos(2,4)\\\
|
|
|
|
write(\\\"(0-65535) \\\")\\\
|
|
|
|
local cch = read()\\\
|
|
|
|
if tonumber(cch) or (cch >= 0 and cch <= 65535) then\\\
|
|
|
|
modem.close(channel)\\\
|
|
|
|
channel = tonumber(cch)\\\
|
|
|
|
modem.open(channel)\\\
|
|
|
|
break\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
center(\\\"Channel must be number\\\")\\\
|
|
|
|
term.setCursorPos(1,7)\\\
|
|
|
|
center(\\\"and in 0-65535\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
elseif y == 7 then\\\
|
|
|
|
while true do\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Set max distance\\\")\\\
|
|
|
|
term.setCursorPos(2,4)\\\
|
|
|
|
local diss = read()\\\
|
|
|
|
if tonumber(diss) then\\\
|
|
|
|
maxDistance = tonumber(diss)\\\
|
|
|
|
break\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
center(\\\"Channel must be number\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
elseif ev[1] == \\\"modem_message\\\" then\\\
|
|
|
|
ch = ev[3]\\\
|
|
|
|
rch = ev[4]\\\
|
|
|
|
content = ev[5]\\\
|
|
|
|
distance = ev[6]\\\
|
|
|
|
if ch == channel and distance <= maxDistance and (content.content and content.version and content.receiver) then\\\
|
|
|
|
if content.version == beamEngineBuild and tonumber(decode(content.receiver)) == os.getComputerID() and not usedIDs[randomID] then\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"File received from \\\"..rch)\\\
|
|
|
|
term.setCursorPos(1,4)\\\
|
|
|
|
center(\\\"Download file?\\\")\\\
|
|
|
|
paintutils.drawFilledBox(2,6,8,8,colors.lime)\\\
|
|
|
|
term.setCursorPos(4,7)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.write(\\\"Yes\\\")\\\
|
|
|
|
paintutils.drawFilledBox(w-8,6,w-2,8,colors.red)\\\
|
|
|
|
term.setCursorPos(w-6,7)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.write(\\\"No\\\")\\\
|
|
|
|
while true do\\\
|
|
|
|
local _,_,x,y = os.pullEvent(\\\"mouse_click\\\")\\\
|
|
|
|
\\\
|
|
|
|
if y == 1 and x == w then\\\
|
|
|
|
break\\\
|
|
|
|
elseif (x >= 2 and y >= 6) and (x <= 8 and y <= 8) then\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
center(\\\"Save file as\\\")\\\
|
|
|
|
term.setCursorPos(2,4)\\\
|
|
|
|
term.setCursorBlink(true)\\\
|
|
|
|
while true do\\\
|
|
|
|
local e, p, x, y = os.pullEvent()\\\
|
|
|
|
if e == \\\"mouse_click\\\" and x == w and y == 1 then\\\
|
|
|
|
break\\\
|
|
|
|
elseif e == \\\"key\\\" then\\\
|
|
|
|
local name = read()\\\
|
|
|
|
local f = fs.open(name, \\\"w\\\")\\\
|
|
|
|
f.write(decode(content.content))\\\
|
|
|
|
f.close()\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
center(\\\"File saved!\\\")\\\
|
|
|
|
term.setCursorPos(1,8)\\\
|
|
|
|
center(\\\"Press any key\\\")\\\
|
|
|
|
os.pullEvent(\\\"key\\\")\\\
|
|
|
|
break\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
break\\\
|
|
|
|
elseif (x >= w-8 and y >= 6) and (x <= w-2 and y <= 8) then\\\
|
|
|
|
break\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
else\\\
|
|
|
|
if content.version ~= beamEngineBuild then\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Can't get file from \\\"..rch)\\\
|
|
|
|
term.setCursorPos(1,4)\\\
|
|
|
|
if content.version < beamEngineBuild then\\\
|
|
|
|
center(\\\"Sender Beam is obsolete\\\")\\\
|
|
|
|
elseif content.version > beamEngineBuild then\\\
|
|
|
|
center(\\\"Your Beam is obsolete\\\")\\\
|
|
|
|
end\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
center(\\\"Press any key\\\")\\\
|
|
|
|
os.pullEvent(\\\"key\\\")\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
--[[\\\
|
|
|
|
\\\
|
|
|
|
Beam by Ale32bit\\\
|
|
|
|
\\\
|
|
|
|
]]--\\\
|
|
|
|
\\\
|
|
|
|
-- base64 API by MultMine\\\
|
|
|
|
local base64 = \\\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\\\"\\\
|
|
|
|
.. \\\"abcdefghijklmnopqrstuvwxyz0123456789+/\\\"\\\
|
|
|
|
\\\
|
|
|
|
local function toBits(n)\\\
|
|
|
|
local t = {}\\\
|
|
|
|
for i = 8, 1, -1 do\\\
|
|
|
|
table.insert(t, bit.brshift(bit.band(n, 2^(i-1)), i - 1))\\\
|
|
|
|
end\\\
|
|
|
|
return t\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function encodeBlock(blk)\\\
|
|
|
|
local b64 = \\\"\\\"\\\
|
|
|
|
local b64_bits = {}\\\
|
|
|
|
for i = 1, 3 do\\\
|
|
|
|
local c = string.byte(blk:sub(i, i)) or 0\\\
|
|
|
|
for j = 8, 1, -1 do\\\
|
|
|
|
table.insert(b64_bits, bit.brshift(bit.band(c, 2^(j-1)), j - 1))\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
local byteCount = #blk + 1\\\
|
|
|
|
for i = 1, byteCount do\\\
|
|
|
|
local n = 0\\\
|
|
|
|
for j = 1, 6 do\\\
|
|
|
|
n = n + (2 ^ (6 - j)) * b64_bits[(i - 1) * 6 + j]\\\
|
|
|
|
end\\\
|
|
|
|
b64 = b64 .. base64:sub(n + 1, n + 1)\\\
|
|
|
|
end\\\
|
|
|
|
return b64 .. string.rep(\\\"=\\\", 4 - byteCount)\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function decodeBlock(b64)\\\
|
|
|
|
local msg = \\\"\\\"\\\
|
|
|
|
local msg_bits = {}\\\
|
|
|
|
for i = 1, 4 do\\\
|
|
|
|
local c = b64:sub(i, i):byte()\\\
|
|
|
|
if c >= 0x30 and c <= 0x39 then\\\
|
|
|
|
local t = toBits(c + 4)\\\
|
|
|
|
for j = 3, 8 do\\\
|
|
|
|
table.insert(msg_bits, t[j])\\\
|
|
|
|
end\\\
|
|
|
|
elseif c == string.byte(\\\"+\\\") then\\\
|
|
|
|
for j = 1, 5 do\\\
|
|
|
|
table.insert(msg_bits, 1)\\\
|
|
|
|
end\\\
|
|
|
|
table.insert(msg_bits, 0)\\\
|
|
|
|
elseif c == string.byte(\\\"/\\\") then\\\
|
|
|
|
for j = 1, 6 do\\\
|
|
|
|
table.insert(msg_bits, 1)\\\
|
|
|
|
end\\\
|
|
|
|
elseif c >= 65 and c <= string.byte(\\\"Z\\\") then\\\
|
|
|
|
local t = toBits(c - 65)\\\
|
|
|
|
for j = 3, 8 do\\\
|
|
|
|
table.insert(msg_bits, t[j])\\\
|
|
|
|
end\\\
|
|
|
|
elseif c >= 97 and c <= string.byte(\\\"z\\\") then\\\
|
|
|
|
local t = toBits(c - 71)\\\
|
|
|
|
for j = 3, 8 do\\\
|
|
|
|
table.insert(msg_bits, t[j])\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
for i = 1, #msg_bits / 8 do\\\
|
|
|
|
local n = 0\\\
|
|
|
|
for j = 1, 8 do\\\
|
|
|
|
n = n + (2 ^ (8 - j)) * msg_bits[(i - 1) * 8 + j]\\\
|
|
|
|
end\\\
|
|
|
|
msg = msg .. string.char(n)\\\
|
|
|
|
end\\\
|
|
|
|
return msg\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function encode(msg)\\\
|
|
|
|
local b64 = \\\"\\\"\\\
|
|
|
|
for i = 1, math.ceil(#msg / 3) do\\\
|
|
|
|
b64 = b64 .. encodeBlock(msg:sub((i - 1) * 3 + 1, i * 3))\\\
|
|
|
|
end\\\
|
|
|
|
return b64\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function decode(b64)\\\
|
|
|
|
local msg = \\\"\\\"\\\
|
|
|
|
for i = 1, #b64 / 4 do\\\
|
|
|
|
msg = msg .. decodeBlock(b64:sub((i - 1) * 4 + 1, i * 4))\\\
|
|
|
|
end\\\
|
|
|
|
return msg\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local version = 2.2\\\
|
|
|
|
local beamEngineBuild = 3\\\
|
|
|
|
local channel = 1\\\
|
|
|
|
local maxDistance = 30000000\\\
|
|
|
|
local status = true\\\
|
|
|
|
local config = \\\".beam-config\\\"\\\
|
|
|
|
local usedIDs = {}\\\
|
|
|
|
\\\
|
|
|
|
\\\
|
|
|
|
w,h = term.getSize()\\\
|
|
|
|
\\\
|
|
|
|
local function center(text)\\\
|
|
|
|
local _,y = term.getCursorPos()\\\
|
|
|
|
if pocket then\\\
|
|
|
|
term.setCursorPos(math.ceil(w/2)-math.ceil(#text/2)+1,y)\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(math.ceil(w/2)-math.ceil(#text/2),y)\\\
|
|
|
|
end\\\
|
|
|
|
term.write(text)\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
local function redraw()\\\
|
|
|
|
term.setBackgroundColor(colors.white)\\\
|
|
|
|
term.clear()\\\
|
|
|
|
paintutils.drawLine(1,1,w,1,colors.blue)\\\
|
|
|
|
term.setCursorPos(1,1)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.setBackgroundColor(colors.blue)\\\
|
|
|
|
term.write(\\\"Beam ID: \\\"..os.getComputerID())\\\
|
|
|
|
term.setCursorPos(w,1)\\\
|
|
|
|
term.setBackgroundColor(colors.red)\\\
|
|
|
|
term.write(\\\"X\\\")\\\
|
|
|
|
\\\
|
|
|
|
paintutils.drawFilledBox(2,4,w-1,6,colors.blue)\\\
|
|
|
|
term.setCursorPos(1,5)\\\
|
|
|
|
term.setBackgroundColor(colors.blue)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
center(\\\"Send File\\\")\\\
|
|
|
|
\\\
|
|
|
|
paintutils.drawFilledBox(2,8,w-1,10,colors.gray)\\\
|
|
|
|
term.setCursorPos(1,9)\\\
|
|
|
|
term.setBackgroundColor(colors.gray)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
center(\\\"Settings\\\")\\\
|
|
|
|
\\\
|
|
|
|
\\\
|
|
|
|
term.setCursorPos(1,2)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setBackgroundColor(colors.white)\\\
|
|
|
|
term.setCursorPos(1,h-3)\\\
|
|
|
|
if status then\\\
|
|
|
|
term.write(\\\"Status: Listening\\\")\\\
|
|
|
|
else\\\
|
|
|
|
term.write(\\\"Status: Standby\\\")\\\
|
|
|
|
end\\\
|
|
|
|
term.setCursorPos(1,h-2)\\\
|
|
|
|
term.write(\\\"Channel: \\\"..channel)\\\
|
|
|
|
term.setCursorPos(1,h-1)\\\
|
|
|
|
term.write(\\\"Max Distance: \\\"..maxDistance)\\\
|
|
|
|
term.setCursorPos(1,h)\\\
|
|
|
|
term.write(\\\"Version: \\\"..version)\\\
|
|
|
|
term.setCursorBlink(false)\\\
|
|
|
|
end\\\
|
|
|
|
local modem = peripheral.find(\\\"modem\\\")\\\
|
|
|
|
\\\
|
|
|
|
if not modem then\\\
|
|
|
|
term.setBackgroundColor(colors.black)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.clear()\\\
|
|
|
|
term.setCursorPos(1,1)\\\
|
|
|
|
printError(\\\"Modem not found\\\")\\\
|
|
|
|
return\\\
|
|
|
|
end\\\
|
|
|
|
\\\
|
|
|
|
modem.open(channel)\\\
|
|
|
|
\\\
|
|
|
|
while true do\\\
|
|
|
|
redraw()\\\
|
|
|
|
local ev = {os.pullEvent()}\\\
|
|
|
|
if ev[1] == \\\"mouse_click\\\" then\\\
|
|
|
|
local x,y = ev[3],ev[4]\\\
|
|
|
|
if x == w and y == 1 then\\\
|
|
|
|
modem.close(channel)\\\
|
|
|
|
term.setBackgroundColor(colors.black)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.clear()\\\
|
|
|
|
term.setCursorPos(1,1)\\\
|
|
|
|
print(\\\"Thanks for using Beam by Ale32bit\\\")\\\
|
|
|
|
break\\\
|
|
|
|
elseif (x >= 2 and y >= 4) and (x <= w-1 and y <= 6) then\\\
|
|
|
|
local function redrawSend()\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Send file\\\")\\\
|
|
|
|
term.setCursorPos(2,5)\\\
|
|
|
|
write(\\\"Receiver ID: \\\")\\\
|
|
|
|
term.setCursorPos(2,7)\\\
|
|
|
|
write(\\\"File: \\\")\\\
|
|
|
|
term.setCursorPos(15,5)\\\
|
|
|
|
term.setCursorBlink(true)\\\
|
|
|
|
end\\\
|
|
|
|
while true do\\\
|
|
|
|
redrawSend()\\\
|
|
|
|
local e,_,x,y = os.pullEvent()\\\
|
|
|
|
if e == \\\"mouse_click\\\" and x == w and y == 1 then\\\
|
|
|
|
break\\\
|
|
|
|
elseif e == \\\"key\\\" then\\\
|
|
|
|
term.setCursorPos(15,5)\\\
|
|
|
|
local receiver = read()\\\
|
|
|
|
if tonumber(receiver) then\\\
|
|
|
|
term.setCursorPos(8,7)\\\
|
|
|
|
local file = read()\\\
|
|
|
|
if file ~= \\\"\\\" and fs.exists(file) then\\\
|
|
|
|
local f = fs.open(file,\\\"r\\\")\\\
|
|
|
|
local con = f.readAll()\\\
|
|
|
|
f.close()\\\
|
|
|
|
-- Beam Engine\\\
|
|
|
|
local randomID = math.random(0,65535)\\\
|
|
|
|
usedIDs[randomID] = true\\\
|
|
|
|
modem.transmit(channel,os.getComputerID(),{\\\
|
|
|
|
content = encode(con),\\\
|
|
|
|
version = beamEngineBuild,\\\
|
|
|
|
receiver = encode(receiver),\\\
|
|
|
|
id = randomID,\\\
|
|
|
|
})\\\
|
|
|
|
\\\
|
|
|
|
term.setCursorPos(1,9)\\\
|
|
|
|
center(\\\"File sent to \\\"..receiver)\\\
|
|
|
|
break\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(2,9)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
term.write(\\\"The file does not exist\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(2,9)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
term.write(\\\"Receiver must be number\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
elseif (x >= 2 and y >= 8) and (x <= w-1 and y <= 10) then\\\
|
|
|
|
local function redrawSett()\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Settings\\\")\\\
|
|
|
|
term.setCursorPos(1,5)\\\
|
|
|
|
if status then\\\
|
|
|
|
center(\\\"Disable receiver\\\")\\\
|
|
|
|
else\\\
|
|
|
|
center(\\\"Enable receiver\\\")\\\
|
|
|
|
end\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
center(\\\"Set channel\\\")\\\
|
|
|
|
term.setCursorPos(1,7)\\\
|
|
|
|
center(\\\"Set max distance\\\")\\\
|
|
|
|
end\\\
|
|
|
|
while true do\\\
|
|
|
|
redrawSett()\\\
|
|
|
|
local _,_,x,y = os.pullEvent(\\\"mouse_click\\\")\\\
|
|
|
|
if x == w and y == 1 then\\\
|
|
|
|
break\\\
|
|
|
|
elseif y == 5 then\\\
|
|
|
|
if status then\\\
|
|
|
|
modem.close(channel)\\\
|
|
|
|
status = false\\\
|
|
|
|
else\\\
|
|
|
|
modem.open(channel)\\\
|
|
|
|
status = true\\\
|
|
|
|
end\\\
|
|
|
|
elseif y == 6 then\\\
|
|
|
|
while true do\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Set channel\\\")\\\
|
|
|
|
term.setCursorPos(2,4)\\\
|
|
|
|
write(\\\"(0-65535) \\\")\\\
|
|
|
|
local cch = read()\\\
|
|
|
|
if tonumber(cch) or (cch >= 0 and cch <= 65535) then\\\
|
|
|
|
modem.close(channel)\\\
|
|
|
|
channel = tonumber(cch)\\\
|
|
|
|
modem.open(channel)\\\
|
|
|
|
break\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
center(\\\"Channel must be number\\\")\\\
|
|
|
|
term.setCursorPos(1,7)\\\
|
|
|
|
center(\\\"and in 0-65535\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
elseif y == 7 then\\\
|
|
|
|
while true do\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Set max distance\\\")\\\
|
|
|
|
term.setCursorPos(2,4)\\\
|
|
|
|
local diss = read()\\\
|
|
|
|
if tonumber(diss) then\\\
|
|
|
|
maxDistance = tonumber(diss)\\\
|
|
|
|
break\\\
|
|
|
|
else\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
term.setTextColor(colors.red)\\\
|
|
|
|
center(\\\"Channel must be number\\\")\\\
|
|
|
|
sleep(2)\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
elseif ev[1] == \\\"modem_message\\\" then\\\
|
|
|
|
ch = ev[3]\\\
|
|
|
|
rch = ev[4]\\\
|
|
|
|
content = ev[5]\\\
|
|
|
|
distance = ev[6]\\\
|
|
|
|
if ch == channel and distance <= maxDistance and (content.content and content.version and content.receiver) then\\\
|
|
|
|
if content.version == beamEngineBuild and tonumber(decode(content.receiver)) == os.getComputerID() and not usedIDs[randomID] then\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"File received from \\\"..rch)\\\
|
|
|
|
term.setCursorPos(1,4)\\\
|
|
|
|
center(\\\"Download file?\\\")\\\
|
|
|
|
paintutils.drawFilledBox(2,6,8,8,colors.lime)\\\
|
|
|
|
term.setCursorPos(4,7)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.write(\\\"Yes\\\")\\\
|
|
|
|
paintutils.drawFilledBox(w-8,6,w-2,8,colors.red)\\\
|
|
|
|
term.setCursorPos(w-6,7)\\\
|
|
|
|
term.setTextColor(colors.white)\\\
|
|
|
|
term.write(\\\"No\\\")\\\
|
|
|
|
while true do\\\
|
|
|
|
local _,_,x,y = os.pullEvent(\\\"mouse_click\\\")\\\
|
|
|
|
\\\
|
|
|
|
if y == 1 and x == w then\\\
|
|
|
|
break\\\
|
|
|
|
elseif (x >= 2 and y >= 6) and (x <= 8 and y <= 8) then\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
center(\\\"Save file as\\\")\\\
|
|
|
|
term.setCursorPos(2,4)\\\
|
|
|
|
term.setCursorBlink(true)\\\
|
|
|
|
while true do\\\
|
|
|
|
local e, p, x, y = os.pullEvent()\\\
|
|
|
|
if e == \\\"mouse_click\\\" and x == w and y == 1 then\\\
|
|
|
|
break\\\
|
|
|
|
elseif e == \\\"key\\\" then\\\
|
|
|
|
local name = read()\\\
|
|
|
|
local f = fs.open(name, \\\"w\\\")\\\
|
|
|
|
f.write(decode(content.content))\\\
|
|
|
|
f.close()\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
center(\\\"File saved!\\\")\\\
|
|
|
|
term.setCursorPos(1,8)\\\
|
|
|
|
center(\\\"Press any key\\\")\\\
|
|
|
|
os.pullEvent(\\\"key\\\")\\\
|
|
|
|
break\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
break\\\
|
|
|
|
elseif (x >= w-8 and y >= 6) and (x <= w-2 and y <= 8) then\\\
|
|
|
|
break\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
else\\\
|
|
|
|
if content.version ~= beamEngineBuild then\\\
|
|
|
|
paintutils.drawFilledBox(1,2,w,h,colors.white)\\\
|
|
|
|
term.setTextColor(colors.black)\\\
|
|
|
|
term.setCursorPos(1,3)\\\
|
|
|
|
center(\\\"Can't get file from \\\"..rch)\\\
|
|
|
|
term.setCursorPos(1,4)\\\
|
|
|
|
if content.version < beamEngineBuild then\\\
|
|
|
|
center(\\\"Sender Beam is obsolete\\\")\\\
|
|
|
|
elseif content.version > beamEngineBuild then\\\
|
|
|
|
center(\\\"Your Beam is obsolete\\\")\\\
|
|
|
|
end\\\
|
|
|
|
term.setCursorPos(1,6)\\\
|
|
|
|
center(\\\"Press any key\\\")\\\
|
|
|
|
os.pullEvent(\\\"key\\\")\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
end\\\
|
|
|
|
\",\
|
|
|
|
}",
|
|
|
|
config = "{\
|
2016-11-15 22:32:40 +01:00
|
|
|
type = \"Networking\",\
|
2016-11-06 20:36:22 +01:00
|
|
|
author = \"Ale32bit\",\
|
|
|
|
name = \"Beam\",\
|
|
|
|
id = \"ale32bit.beam\",\
|
|
|
|
version = 2.2,\
|
|
|
|
main = \"beam.lua\",\
|
|
|
|
}",
|
|
|
|
}
|