Beware... Quite a lot of code!
Code: Select all
--Create a frame to show thing on
frame = wx.wxFrame(wx.NULL, wx.wxID_ANY, "IRC Client",
wx.wxDefaultPosition, wx.wxSize(600, 500),
wx.wxDEFAULT_FRAME_STYLE)
newframe = wx.wxFrame(wx.NULL, wx.wxID_ANY, "New Connection",
wx.wxDefaultPosition, wx.wxSize(300, 200),
wx.wxMINIMIZE_BOX + wx.wxRESIZE_BORDER + wx.wxSYSTEM_MENU + wx.wxCAPTION + wx.wxCLOSE_BOX + wx.wxCLIP_CHILDREN)
explode = function(string)
local success = false
local rand = 0
local file = nil
local returnval = {}
while not success do
rand = math.random(1, 1000)
if not os.rename(tostring(rand), tostring(rand)) then
local file = io.open(tostring(rand), "w")
file:write(string)
file:close()
file = io.open(tostring(rand), "r")
for i = 1, #string do
returnval[#returnval+1] = file:read(1)
end
success = true
end
end
return returnval
end
tohex = function(IN)
local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
while IN>0 do
I=I+1
IN,D=math.floor(IN/B),IN%B+1
OUT=string.sub(K,D,D)..OUT
end
return OUT
end
stringtohex = function(string)
local hex = ""
explodedstring = explode(string)
for i = 1, #string do
hex = hex..tohex(string.byte(explodedstring[i]))
end
return hex
end
local idcounter = wx.wxID_HIGHEST + 1
newid = function()
idcounter = idcounter + 1
return idcounter
end
--Little helper function to make writing servers easier
getServerHex = function(servers)
local string = ""
for var, val in pairs(servers) do
string = string..stringtohex(tostring(#val)..val)
end
return string
end
---------------------------------------------------------------------
---------------------------------------------------------------------
--Info function to fetch misc data
infos = {}
infos.getServerList = function()
if not os.rename("internal/saves/serverlist.tirc", "internal/saves/serverlist.tirc") then
io.open("internal/saves/serverlist.tirc", "w"):write(getServerHex{"irc.freenode.net", "irc.esper.net"})
return {"irc.freenode.net", "irc.esper.net"}
else
local list = io.open("internal/saves/serverlist.tirc", "r")
if not io.read(0) then --if file is empty
list:close()
return {}
else
local serverlist = {}
local length = 0x00
local x = 1
while io.read(0) do
length = tonumber(list:read(2), 16)
serverlist[x] = list:read(length)
x = x + 1
end
list:close()
return serverlist
end
end
end
------------------------------------------------------
------------------------------------------------------
infos.addServer = function(server)
if not os.rename("internal/saves/serverlist.tirc", "internal/saves/serverlist.tirc") then
io.open("internal/saves/serverlist.tirc", "a+"):close()
return {}
else
io.open("internal/saves/serverlist.tirc", "w"):write(stringtohex(tostring(#server..server)))
end
end
infos.getLastUsername = function()
if not os.rename("internal/saves/55534552.tirc", "internal/saves/55534552.tirc") then
local user = io.open("internal/saves/serverlist.tirc", "a+")
user:close()
return ""
else
local user = io.open("internal/saves/serverlist.tirc", "r")
local username = user:read(tonumber(user:read(2), 16))
user:close()
return username
end
end
--INSECURE!!!!!!!!!!! %$@%ing make a secure function already ._.
infos.getLastPassword = function()
if not os.rename("internal/saves/55534552.tirc", "internal/saves/55534552.tirc") then
local user = io.open("internal/saves/serverlist.tirc", "a+")
user:close()
return ""
else
local user = io.open("internal/saves/serverlist.tirc", "r")
local junk = user:read(tonumber(user:read(2), 16)) --The junk is basically the username(Only the pass is needed)
local pass = user:read(tonumber(user:read(2), 16))
user:close()
return pass
end
end
--Connection tab
local connections = wx.wxMenu()
local new = newid()
local reconnect = newid()
local close = newid()
local connectionmanager = newid()
connections:Append(new, "&New Connection", "Open a new connection.")
connections:Append(reconnect, "&Reconnect", "Reconnect to the server")
connections:Append(close, "&Close Connection")
connections:Append(connectionmanager, "Connection &Manager", "Open the connection manager.")
--Window tab
local window = wx.wxMenu()
local startoptions = newid()
--Create a menu bar
local menu = wx.wxMenuBar()
menu:Append(connections, "&Connections")
frame:SetMenuBar(menu)
--New connection window setup
local server = wx.wxTextCtrl(newframe, wx.wxID_ANY, "", wx.wxPoint(0, 0), wx.wxSize(300, 20))
local serverlist = wx.wxListBox(newframe, wx.wxID_ANY, wx.wxPoint(0, 20), wx.wxSize(300, 100), infos.getServerList())
local userlabel = wx.wxStaticText(newframe, wx.wxID_ANY, "Username:", wx.wxPoint(0, 120))
local username = wx.wxTextCtrl(newframe, wx.wxID_ANY, infos.getLastUsername(), wx.wxPoint(0, 133), wx.wxSize(140, 20))
local passlabel = wx.wxStaticText(newframe, wx.wxID_ANY, "Password:", wx.wxPoint(144, 120))
local password = wx.wxTextCtrl(newframe, wx.wxID_ANY, infos.getLastPassword(), wx.wxPoint(144, 133), wx.wxSize(140, 20))
--Events for the menu bar
--Events: Connection tab
frame:Connect(new, wx.wxEVT_COMMAND_MENU_SELECTED,
function(event)
newframe:Show(true)
end )
--Events: New Connection
newframe:Connect(wx.wxID_ANY, wx.wxEVT_COMMAND_LISTBOX_SELECTED, function(event)
server:SetValue(event:GetString())
end)
frame:Show(true)
And here's the beautiful error message!
Code: Select all
Error: Lua: Error while running chunk
[string "Client.lua"]:69: attempt to index a nil value
stack traceback:
[string "Client.lua"]:69: in function 'getServerList'
[string "Client.lua"]:150: in main chunk
End lua script : Mon Nov 21 19:27:50 2016
Execution time : 0.047(s)
Oh, by the way: I'm using wxLuaEdit as editor. (just in case the logs differ!)