--Exchange-Converter V.1 written by chill --Based on an idea from Kotten --You need a "txt" folder, "scripts/txt" bot1 = "Converter" EPrefix = "%!%+%-%?" MainCurrency = "EUR" cmd1 = "changehelp" cmd2 = "change" cmd3 = "converthelp" cmd4 = "convert" --10 USD to EUR = 10/USD-EUR == EUR GETHTML = "GET /d/EUR/table.html HTTP/1.1\r\nHost: www.x-rates.com\r\nUser-Agent: Mozilla/4.0 (compatible; LUA 4.0; LUA 4.0)\r\n" Converters = { ["°F-°C"] = "([x]-32)*5/9", ["°C-°F"] = "[x]*5/9+32", ["IN-CM"] = "[x]*2.54", ["CM-IN"] = "[x]/2.54", ["FT-CM"] = "[x]*2.54*12", ["CM-FT"] = "[x]/(2.54*12)", ["MI-CM"] = "[x]*1.6093*1000*100", ["CM-MI"] = "[x]/(1.6093*1000*100)", ["KM-CM"] = "[x]*1000*100", ["CM-KM"] = "[x]/(1000*100)", ["M-CM"] = "[x]*100", ["CM-M"] = "[x]/100", ["LBS-KG"] = "[x]*0.4536", ["KG-LBS"] = "[x]/0.4536", } convhelp = "\tMiles = MI\r\n".. "\tFoot = FT\r\n".. "\tInch = IN\r\n".. "\tCentimeter = CM\r\n".. "\tMeter = M\r\n".. "\tKilometer = KM\r\n".. "\tPound = LBS\r\n".. "\tKilogramms = KG\r\n".. "\tFahrenheit = °F\r\n".. "\tCelsius = °C\r\n" FindRates = { { "American Dollar ","USD" }, { "Australian Dollar ","AUD" }, { "Botswana Pula ","BWP" }, { "Brazilian Real ","BRL" }, { "British Pound ","GBP" }, { "Canadian Dollar ","CAD" }, { "Chinese Yuan ","CNY" }, { "Danish Krone ","DKK" }, { "Hong Kong Dollar ","HKD" }, { "Hungarian Forint ","HUF" }, { "Indian Rupee ","INR" }, { "Japanese Yen ","JPY" }, { "Malaysian Ringgit ","MYR" }, { "Mexican Peso ","MXN" }, { "New Zealand Dollar ","NZD" }, { "Norwegian Kroner ","NOK" }, { "Singapore Dollar ","SGD" }, { "South African Rand ","ZAR" }, { "South Korean Won ","KRW" }, { "Sri Lanka Rupee ","LKR" }, { "Swedish Krona ","SEK" }, { "Swiss Franc ","CHF" }, { "Taiwan Dollar ","TWD" }, { "Thai Baht ","THB" }, { "Venezuelan Bolivar ","VEB" }, } curday = 0 curRates = {} dofile("txt/curRates.txt") hour = 1000*60*60 function Main() SetTimer(hour) StartTimer() local var = 0 if (tonumber(date("%d")) ~= curday) then GetRates() end curhelp = "" for i,_ in FindRates do curhelp = curhelp.."\tCurrency: "..strsub(FindRates[i][1],1,strlen(FindRates[i][1])-5).." Shorten: "..FindRates[i][2].."\r\n" end end function DataArrival(curUser,data) if (strsub(data,1,1) == "<") then data = strsub(data,1,strlen(data)-1) local _,_,cmd = strfind( data, "%b<>%s["..EPrefix.."](%S+)" ) if not cmd then return 0 end if (strlower(cmd) == strlower(cmd1)) then curUser:SendData(bot1,"Syntax: Prefix\""..cmd2.."\" [Amount] [CurrentCurrency] [WantedCurrrency], Prefixa = + - ! ?\r\n\r\n".. "\tAvailable Currencies =\r\n\r\n"..curhelp) return 1 elseif (strlower(cmd) == strlower(cmd2)) then local _,_,str1,str2,str3 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+(%S+)%s+(%S+)") if tonumber(str1) then str2, str3 = strupper(str2),strupper(str3) if curRates[str2.."-"..str3] then local change = format("%0.2f",tonumber(str1)/curRates[str2.."-"..str3]) curUser:SendData(bot1,str1.." "..str2.." = "..change.." "..str3) elseif tonumber(str1) and curRates[str2.."-"..MainCurrency] and curRates[MainCurrency.."-"..str3] then local change = format("%0.2f",tonumber(str1)/(curRates[str2.."-"..MainCurrency]*curRates[MainCurrency.."-"..str3])) curUser:SendData(bot1,str1.." "..str2.." = "..change.." "..str3) end else curUser:SendData(bot1,"Syntax: Prefixa \""..cmd2.."\" [Amount] [CurrentCurrency] [WantedCurrrency], Prefixa = + - ! ?") end return 1 elseif (strlower(cmd) == strlower(cmd3)) then curUser:SendData(bot1,"Syntax: Prefix\""..cmd4.."\" [Number] [CurrentValue] [WantedValue], Prefixa = + - ! ?\r\n\r\n"..convhelp) elseif (strlower(cmd) == strlower(cmd4)) then local _,_,str1,str2,str3 = strfind(data,"%b<>%s+%S+%s+(%S+)%s+(%S+)%s+(%S+)") if tonumber(str1) then str2, str3 = strupper(str2),strupper(str3) conv = "" if Converters[str2.."-"..str3] then local x = gsub(Converters[str2.."-"..str3],"%b[]",str1) dostring("conv="..x) curUser:SendData(bot1,str1.." "..str2.." = "..format("%0.2f",conv).." "..str3) elseif tonumber(str1) and Converters[str2.."-CM"] and Converters["CM-"..str3] then local x1 = gsub(Converters[str2.."-CM"],"%b[]",str1) dostring("conv="..x1) local x2 = gsub(Converters["CM-"..str3],"%b[]",conv) dostring("conv="..x2) curUser:SendData(bot1,str1.." "..str2.." = "..format("%0.2f",conv).." "..str3) end else curUser:SendData(bot1,"Syntax: Prefixa \""..cmd4.."\" [Number] [CurrentValue] [WantedValue], Prefixa = + - ! ?") end return 1 end end end ---------------------------------------------------------------------------------------------------------------------------------------------- function OnTimer() if (tonumber(date("%d")) ~= curday) then GetRates() end end ---------------------------------------------------------------------------------------------------------------------------------------------- function WriteFile(table,tablename,file) local handle = openfile("txt/"..file,"w") Serialize(table,tablename,handle) if table == curRates then write(handle,"\r\ncurday = "..curday) end closefile(handle) end ---------------------------------------------------------------------------------------------------------------------------------------------- function Serialize(tTable,sTableName,hFile,sTab) sTab = sTab or ""; write(hFile,sTab..sTableName.." = {\n"); for key,value in tTable do local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key); if(type(value) == "table") then Serialize(value,sKey,hFile,sTab.."\t"); else local sValue = (type(value) == "string") and format("%q",value) or tostring(value); write(hFile,sTab.."\t"..sKey.." = "..sValue); end write(hFile,",\n"); end write(hFile,sTab.."}"); end ---------------------------------------------------------------------------------------------------------------------------------------------- function GetRates() socket, e = connect("www.x-rates.com", 80) if not e then socket:timeout(2) socket:send(GETHTML..strchar(13,10)) while not e do line1,e = socket:receive("*l") foreachi(FindRates, function(a,b) if strfind(line1, b[1]) then for i = 1,2 do line2,e = socket:receive("*l") local _,_,num = strfind(line2,">(%S+)") if i == 1 and tonumber(num) then curRates[b[2].."-"..MainCurrency] = tonumber(num) elseif i == 2 and tonumber(num) then curRates[MainCurrency.."-"..b[2]] = tonumber(num) end end end end) end socket:close() curday = tonumber(date("%d")) WriteFile(curRates,"curRates","curRates.txt") end end